2 * Ratelimiting calculations
4 * Copyright IBM, Corp. 2011
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #ifndef QEMU_RATELIMIT_H
15 #define QEMU_RATELIMIT_H 1
18 int64_t next_slice_time
;
24 static inline int64_t ratelimit_calculate_delay(RateLimit
*limit
, uint64_t n
)
26 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
28 if (limit
->next_slice_time
< now
) {
29 limit
->next_slice_time
= now
+ limit
->slice_ns
;
30 limit
->dispatched
= 0;
32 if (limit
->dispatched
== 0 || limit
->dispatched
+ n
<= limit
->slice_quota
) {
33 limit
->dispatched
+= n
;
36 limit
->dispatched
= n
;
37 return limit
->next_slice_time
- now
;
41 static inline void ratelimit_set_speed(RateLimit
*limit
, uint64_t speed
,
44 limit
->slice_ns
= slice_ns
;
45 limit
->slice_quota
= ((double)speed
* slice_ns
)/1000000000ULL;