2 * QEMU throttling infrastructure
4 * Copyright (C) Nodalink, SARL. 2013
7 * BenoƮt Canet <benoit.canet@irqsave.net>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 or
12 * (at your option) version 3 of the License.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "qemu-common.h"
28 #include "qemu/timer.h"
30 #define NANOSECONDS_PER_SECOND 1000000000.0
43 * The max parameter of the leaky bucket throttling algorithm can be used to
44 * allow the guest to do bursts.
45 * The max value is a pool of I/O that the guest can use without being throttled
46 * at all. Throttling is triggered once this pool is empty.
49 typedef struct LeakyBucket
{
50 double avg
; /* average goal in units per second */
51 double max
; /* leaky bucket max burst in units */
52 double level
; /* bucket level in units */
55 /* The following structure is used to configure a ThrottleState
56 * It contains a bit of state: the bucket field of the LeakyBucket structure.
57 * However it allows to keep the code clean and the bucket field is reset to
58 * zero at the right time.
60 typedef struct ThrottleConfig
{
61 LeakyBucket buckets
[BUCKETS_COUNT
]; /* leaky buckets */
62 uint64_t op_size
; /* size of an operation in bytes */
65 typedef struct ThrottleState
{
66 ThrottleConfig cfg
; /* configuration */
67 int64_t previous_leak
; /* timestamp of the last leak done */
68 QEMUTimer
* timers
[2]; /* timers used to do the throttling */
69 QEMUClockType clock_type
; /* the clock used */
72 QEMUTimerCB
*read_timer_cb
;
73 QEMUTimerCB
*write_timer_cb
;
77 /* operations on single leaky buckets */
78 void throttle_leak_bucket(LeakyBucket
*bkt
, int64_t delta
);
80 int64_t throttle_compute_wait(LeakyBucket
*bkt
);
82 /* expose timer computation function for unit tests */
83 bool throttle_compute_timer(ThrottleState
*ts
,
86 int64_t *next_timestamp
);
88 /* init/destroy cycle */
89 void throttle_init(ThrottleState
*ts
,
90 AioContext
*aio_context
,
91 QEMUClockType clock_type
,
92 void (read_timer
)(void *),
93 void (write_timer
)(void *),
96 void throttle_destroy(ThrottleState
*ts
);
98 void throttle_detach_aio_context(ThrottleState
*ts
);
100 void throttle_attach_aio_context(ThrottleState
*ts
, AioContext
*new_context
);
102 bool throttle_have_timer(ThrottleState
*ts
);
105 bool throttle_enabled(ThrottleConfig
*cfg
);
107 bool throttle_conflicting(ThrottleConfig
*cfg
);
109 bool throttle_is_valid(ThrottleConfig
*cfg
);
111 void throttle_config(ThrottleState
*ts
, ThrottleConfig
*cfg
);
113 void throttle_get_config(ThrottleState
*ts
, ThrottleConfig
*cfg
);
116 bool throttle_schedule_timer(ThrottleState
*ts
, bool is_write
);
118 void throttle_account(ThrottleState
*ts
, bool is_write
, uint64_t size
);