2026-04-02 16:00:27 +02:00
|
|
|
// 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
|
|
|
|
2026-10-13 11:20:19 +02:00
|
|
|
#ifndef BW_H
|
|
|
|
|
#define BW_H
|
2025-03-27 12:57:41 -07:00
|
|
|
|
|
|
|
|
#include "state.h"
|
|
|
|
|
#include "support.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Bandwidth limiting
|
|
|
|
|
*/
|
|
|
|
|
struct snapraid_bw {
|
|
|
|
|
uint64_t limit; /**< Bandwidth limit in bytes per second */
|
|
|
|
|
uint64_t total; /**< Remaining bytes allowed in current second */
|
|
|
|
|
uint64_t start; /**< Time when to reset the bandwidth counter */
|
2026-08-12 00:22:27 +02:00
|
|
|
#if HAVE_THREAD
|
|
|
|
|
thread_mutex_t lock; /**< Mutex protecting bw fields */
|
|
|
|
|
#endif
|
2025-03-27 12:57:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the bandwidth limit
|
|
|
|
|
*/
|
|
|
|
|
void bw_init(struct snapraid_bw* bw, uint64_t limit);
|
|
|
|
|
|
2026-08-12 00:22:27 +02:00
|
|
|
/**
|
|
|
|
|
* Destroy the bandwidth limit
|
|
|
|
|
*/
|
|
|
|
|
void bw_done(struct snapraid_bw* bw);
|
|
|
|
|
|
2025-03-27 12:57:41 -07:00
|
|
|
/**
|
|
|
|
|
* Limit IO bandwidth to stay within the configured limit.
|
|
|
|
|
* If no limit is set, returns immediately.
|
|
|
|
|
* Otherwise sleeps as needed to maintain the rate limit.
|
|
|
|
|
*/
|
|
|
|
|
void bw_limit(struct snapraid_bw* bw, uint64_t bytes);
|
|
|
|
|
|
|
|
|
|
#endif
|
2025-10-26 18:06:08 +01:00
|
|
|
|