1 /* 2 * Ratelimiting calculations 3 * 4 * Copyright IBM, Corp. 2011 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 8 * 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. 11 * 12 */ 13 14 #ifndef QEMU_RATELIMIT_H 15 #define QEMU_RATELIMIT_H 16 17 #include "qemu/lockable.h" 18 #include "qemu/timer.h" 19 20 typedef struct { 21 QemuMutex lock; 22 int64_t slice_start_time; 23 int64_t slice_end_time; 24 uint64_t slice_quota; 25 uint64_t slice_ns; 26 uint64_t dispatched; 27 } RateLimit; 28 29 /** Calculate and return delay for next request in ns 30 * 31 * Record that we sent @n data units (where @n matches the scale chosen 32 * during ratelimit_set_speed). If we may send more data units 33 * in the current time slice, return 0 (i.e. no delay). Otherwise 34 * return the amount of time (in ns) until the start of the next time 35 * slice that will permit sending the next chunk of data. 36 * 37 * Recording sent data units even after exceeding the quota is 38 * permitted; the time slice will be extended accordingly. 39 */ 40 static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n) 41 { 42 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 43 double delay_slices; 44 45 QEMU_LOCK_GUARD(&limit->lock); 46 assert(limit->slice_quota && limit->slice_ns); 47 48 if (limit->slice_end_time < now) { 49 /* Previous, possibly extended, time slice finished; reset the 50 * accounting. */ 51 limit->slice_start_time = now; 52 limit->slice_end_time = now + limit->slice_ns; 53 limit->dispatched = 0; 54 } 55 56 limit->dispatched += n; 57 if (limit->dispatched < limit->slice_quota) { 58 /* We may send further data within the current time slice, no 59 * need to delay the next request. */ 60 return 0; 61 } 62 63 /* Quota exceeded. Wait based on the excess amount and then start a new 64 * slice. */ 65 delay_slices = (double)limit->dispatched / limit->slice_quota; 66 limit->slice_end_time = limit->slice_start_time + 67 (uint64_t)(delay_slices * limit->slice_ns); 68 return limit->slice_end_time - now; 69 } 70 71 static inline void ratelimit_init(RateLimit *limit) 72 { 73 qemu_mutex_init(&limit->lock); 74 } 75 76 static inline void ratelimit_destroy(RateLimit *limit) 77 { 78 qemu_mutex_destroy(&limit->lock); 79 } 80 81 static inline void ratelimit_set_speed(RateLimit *limit, uint64_t speed, 82 uint64_t slice_ns) 83 { 84 QEMU_LOCK_GUARD(&limit->lock); 85 limit->slice_ns = slice_ns; 86 limit->slice_quota = MAX(((double)speed * slice_ns) / 1000000000ULL, 1); 87 } 88 89 #endif 90