2 * QEMU throttling infrastructure
4 * Copyright (C) Nodalink, EURL. 2013-2014
5 * Copyright (C) Igalia, S.L. 2015
8 * BenoƮt Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 or
14 * (at your option) version 3 of the License.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include "qemu-common.h"
30 #include "qemu/timer.h"
32 #define NANOSECONDS_PER_SECOND 1000000000.0
45 * The max parameter of the leaky bucket throttling algorithm can be used to
46 * allow the guest to do bursts.
47 * The max value is a pool of I/O that the guest can use without being throttled
48 * at all. Throttling is triggered once this pool is empty.
51 typedef struct LeakyBucket
{
52 double avg
; /* average goal in units per second */
53 double max
; /* leaky bucket max burst in units */
54 double level
; /* bucket level in units */
57 /* The following structure is used to configure a ThrottleState
58 * It contains a bit of state: the bucket field of the LeakyBucket structure.
59 * However it allows to keep the code clean and the bucket field is reset to
60 * zero at the right time.
62 typedef struct ThrottleConfig
{
63 LeakyBucket buckets
[BUCKETS_COUNT
]; /* leaky buckets */
64 uint64_t op_size
; /* size of an operation in bytes */
67 typedef struct ThrottleState
{
68 ThrottleConfig cfg
; /* configuration */
69 int64_t previous_leak
; /* timestamp of the last leak done */
72 typedef struct ThrottleTimers
{
73 QEMUTimer
*timers
[2]; /* timers used to do the throttling */
74 QEMUClockType clock_type
; /* the clock used */
77 QEMUTimerCB
*read_timer_cb
;
78 QEMUTimerCB
*write_timer_cb
;
82 /* operations on single leaky buckets */
83 void throttle_leak_bucket(LeakyBucket
*bkt
, int64_t delta
);
85 int64_t throttle_compute_wait(LeakyBucket
*bkt
);
87 /* expose timer computation function for unit tests */
88 bool throttle_compute_timer(ThrottleState
*ts
,
91 int64_t *next_timestamp
);
93 /* init/destroy cycle */
94 void throttle_init(ThrottleState
*ts
);
96 void throttle_timers_init(ThrottleTimers
*tt
,
97 AioContext
*aio_context
,
98 QEMUClockType clock_type
,
99 QEMUTimerCB
*read_timer_cb
,
100 QEMUTimerCB
*write_timer_cb
,
103 void throttle_timers_destroy(ThrottleTimers
*tt
);
105 void throttle_timers_detach_aio_context(ThrottleTimers
*tt
);
107 void throttle_timers_attach_aio_context(ThrottleTimers
*tt
,
108 AioContext
*new_context
);
110 bool throttle_timers_are_initialized(ThrottleTimers
*tt
);
113 bool throttle_enabled(ThrottleConfig
*cfg
);
115 bool throttle_conflicting(ThrottleConfig
*cfg
);
117 bool throttle_is_valid(ThrottleConfig
*cfg
);
119 void throttle_config(ThrottleState
*ts
,
121 ThrottleConfig
*cfg
);
123 void throttle_get_config(ThrottleState
*ts
, ThrottleConfig
*cfg
);
126 bool throttle_schedule_timer(ThrottleState
*ts
,
130 void throttle_account(ThrottleState
*ts
, bool is_write
, uint64_t size
);