mirror of
https://github.com/xemu-project/xemu.git
synced 2024-11-30 23:10:38 +00:00
ec150c7e09
Back in 2016, we discussed[1] rules for headers, and these were generally liked: 1. Have a carefully curated header that's included everywhere first. We got that already thanks to Peter: osdep.h. 2. Headers should normally include everything they need beyond osdep.h. If exceptions are needed for some reason, they must be documented in the header. If all that's needed from a header is typedefs, put those into qemu/typedefs.h instead of including the header. 3. Cyclic inclusion is forbidden. This patch gets include/ closer to obeying 2. It's actually extracted from my "[RFC] Baby steps towards saner headers" series[2], which demonstrates a possible path towards checking 2 automatically. It passes the RFC test there. [1] Message-ID: <87h9g8j57d.fsf@blackfin.pond.sub.org> https://lists.nongnu.org/archive/html/qemu-devel/2016-03/msg03345.html [2] Message-Id: <20190711122827.18970-1-armbru@redhat.com> https://lists.nongnu.org/archive/html/qemu-devel/2019-07/msg02715.html Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Message-Id: <20190812052359.30071-2-armbru@redhat.com> Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
76 lines
2.2 KiB
C
76 lines
2.2 KiB
C
/*
|
|
* Ratelimiting calculations
|
|
*
|
|
* Copyright IBM, Corp. 2011
|
|
*
|
|
* Authors:
|
|
* Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
*
|
|
*/
|
|
|
|
#ifndef QEMU_RATELIMIT_H
|
|
#define QEMU_RATELIMIT_H
|
|
|
|
#include "qemu/timer.h"
|
|
|
|
typedef struct {
|
|
int64_t slice_start_time;
|
|
int64_t slice_end_time;
|
|
uint64_t slice_quota;
|
|
uint64_t slice_ns;
|
|
uint64_t dispatched;
|
|
} RateLimit;
|
|
|
|
/** Calculate and return delay for next request in ns
|
|
*
|
|
* Record that we sent @n data units (where @n matches the scale chosen
|
|
* during ratelimit_set_speed). If we may send more data units
|
|
* in the current time slice, return 0 (i.e. no delay). Otherwise
|
|
* return the amount of time (in ns) until the start of the next time
|
|
* slice that will permit sending the next chunk of data.
|
|
*
|
|
* Recording sent data units even after exceeding the quota is
|
|
* permitted; the time slice will be extended accordingly.
|
|
*/
|
|
static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)
|
|
{
|
|
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
|
double delay_slices;
|
|
|
|
assert(limit->slice_quota && limit->slice_ns);
|
|
|
|
if (limit->slice_end_time < now) {
|
|
/* Previous, possibly extended, time slice finished; reset the
|
|
* accounting. */
|
|
limit->slice_start_time = now;
|
|
limit->slice_end_time = now + limit->slice_ns;
|
|
limit->dispatched = 0;
|
|
}
|
|
|
|
limit->dispatched += n;
|
|
if (limit->dispatched < limit->slice_quota) {
|
|
/* We may send further data within the current time slice, no
|
|
* need to delay the next request. */
|
|
return 0;
|
|
}
|
|
|
|
/* Quota exceeded. Wait based on the excess amount and then start a new
|
|
* slice. */
|
|
delay_slices = (double)limit->dispatched / limit->slice_quota;
|
|
limit->slice_end_time = limit->slice_start_time +
|
|
(uint64_t)(delay_slices * limit->slice_ns);
|
|
return limit->slice_end_time - now;
|
|
}
|
|
|
|
static inline void ratelimit_set_speed(RateLimit *limit, uint64_t speed,
|
|
uint64_t slice_ns)
|
|
{
|
|
limit->slice_ns = slice_ns;
|
|
limit->slice_quota = MAX(((double)speed * slice_ns) / 1000000000ULL, 1);
|
|
}
|
|
|
|
#endif
|