snapraid/cmdline/bw.c

34 lines
586 B
C
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
2026-04-02 16:25:49 +02:00
// Copyright (C) 2016 Andrea Mazzoleni
2025-03-27 12:57:41 -07:00
#include "portable.h"
#include "bw.h"
void bw_init(struct snapraid_bw* bw, uint64_t limit)
{
bw->limit = limit;
bw->total = 0;
2026-03-02 01:13:19 +01:00
bw->start = os_tick_ms();
2025-03-27 12:57:41 -07:00
}
void bw_limit(struct snapraid_bw* bw, uint64_t bytes)
{
if (!bw || bw->limit == 0)
return;
2026-03-02 01:13:19 +01:00
uint64_t elapsed = os_tick_ms() - bw->start;
2025-03-27 12:57:41 -07:00
uint64_t done;
uint64_t eta;
done = __atomic_fetch_add(&bw->total, bytes, __ATOMIC_SEQ_CST);
2025-03-27 12:57:41 -07:00
eta = done * 1000 / bw->limit;
if (eta > elapsed) {
eta -= elapsed;
usleep(eta * 1000);
}
}
2025-10-26 18:06:08 +01:00