]>
Commit | Line | Data |
---|---|---|
5ddfffbd BC |
1 | /* |
2 | * QEMU throttling infrastructure | |
3 | * | |
a291d5d9 | 4 | * Copyright (C) Nodalink, EURL. 2013-2014 |
100f8f26 | 5 | * Copyright (C) Igalia, S.L. 2015-2016 |
5ddfffbd | 6 | * |
a291d5d9 AG |
7 | * Authors: |
8 | * Benoît Canet <[email protected]> | |
9 | * Alberto Garcia <[email protected]> | |
5ddfffbd BC |
10 | * |
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. | |
15 | * | |
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. | |
20 | * | |
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/>. | |
23 | */ | |
24 | ||
25 | #ifndef THROTTLE_H | |
26 | #define THROTTLE_H | |
27 | ||
5ddfffbd | 28 | #include "qemu-common.h" |
9af23989 | 29 | #include "qapi/qapi-types-block-core.h" |
5ddfffbd BC |
30 | #include "qemu/timer.h" |
31 | ||
972606c4 FZ |
32 | #define THROTTLE_VALUE_MAX 1000000000000000LL |
33 | ||
5ddfffbd BC |
34 | typedef enum { |
35 | THROTTLE_BPS_TOTAL, | |
36 | THROTTLE_BPS_READ, | |
37 | THROTTLE_BPS_WRITE, | |
38 | THROTTLE_OPS_TOTAL, | |
39 | THROTTLE_OPS_READ, | |
40 | THROTTLE_OPS_WRITE, | |
41 | BUCKETS_COUNT, | |
42 | } BucketType; | |
43 | ||
44 | /* | |
100f8f26 AG |
45 | * This module implements I/O limits using the leaky bucket |
46 | * algorithm. The code is independent of the I/O units, but it is | |
47 | * currently used for bytes per second and operations per second. | |
48 | * | |
49 | * Three parameters can be set by the user: | |
50 | * | |
51 | * - avg: the desired I/O limits in units per second. | |
52 | * - max: the limit during bursts, also in units per second. | |
53 | * - burst_length: the maximum length of the burst period, in seconds. | |
54 | * | |
55 | * Here's how it works: | |
56 | * | |
57 | * - The bucket level (number of performed I/O units) is kept in | |
58 | * bkt.level and leaks at a rate of bkt.avg units per second. | |
59 | * | |
60 | * - The size of the bucket is bkt.max * bkt.burst_length. Once the | |
61 | * bucket is full no more I/O is performed until the bucket leaks | |
62 | * again. This is what makes the I/O rate bkt.avg. | |
63 | * | |
64 | * - The bkt.avg rate does not apply until the bucket is full, | |
65 | * allowing the user to do bursts until then. The I/O limit during | |
66 | * bursts is bkt.max. To enforce this limit we keep an additional | |
faa8215c | 67 | * bucket in bkt.burst_level that leaks at a rate of bkt.max units |
100f8f26 AG |
68 | * per second. |
69 | * | |
70 | * - Because of all of the above, the user can perform I/O at a | |
71 | * maximum of bkt.max units per second for at most bkt.burst_length | |
72 | * seconds in a row. After that the bucket will be full and the I/O | |
73 | * rate will go down to bkt.avg. | |
74 | * | |
75 | * - Since the bucket always leaks at a rate of bkt.avg, this also | |
76 | * determines how much the user needs to wait before being able to | |
77 | * do bursts again. | |
5ddfffbd BC |
78 | */ |
79 | ||
80 | typedef struct LeakyBucket { | |
d00e6923 AG |
81 | uint64_t avg; /* average goal in units per second */ |
82 | uint64_t max; /* leaky bucket max burst in units */ | |
5ddfffbd | 83 | double level; /* bucket level in units */ |
100f8f26 | 84 | double burst_level; /* bucket level in units (for computing bursts) */ |
67335a45 | 85 | uint64_t burst_length; /* max length of the burst period, in seconds */ |
5ddfffbd BC |
86 | } LeakyBucket; |
87 | ||
88 | /* The following structure is used to configure a ThrottleState | |
89 | * It contains a bit of state: the bucket field of the LeakyBucket structure. | |
90 | * However it allows to keep the code clean and the bucket field is reset to | |
91 | * zero at the right time. | |
92 | */ | |
93 | typedef struct ThrottleConfig { | |
94 | LeakyBucket buckets[BUCKETS_COUNT]; /* leaky buckets */ | |
95 | uint64_t op_size; /* size of an operation in bytes */ | |
96 | } ThrottleConfig; | |
97 | ||
98 | typedef struct ThrottleState { | |
99 | ThrottleConfig cfg; /* configuration */ | |
100 | int64_t previous_leak; /* timestamp of the last leak done */ | |
0e5b0a2d BC |
101 | } ThrottleState; |
102 | ||
103 | typedef struct ThrottleTimers { | |
104 | QEMUTimer *timers[2]; /* timers used to do the throttling */ | |
5ddfffbd | 105 | QEMUClockType clock_type; /* the clock used */ |
13af91eb SH |
106 | |
107 | /* Callbacks */ | |
108 | QEMUTimerCB *read_timer_cb; | |
109 | QEMUTimerCB *write_timer_cb; | |
110 | void *timer_opaque; | |
0e5b0a2d | 111 | } ThrottleTimers; |
5ddfffbd BC |
112 | |
113 | /* operations on single leaky buckets */ | |
114 | void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta); | |
115 | ||
116 | int64_t throttle_compute_wait(LeakyBucket *bkt); | |
117 | ||
5ddfffbd | 118 | /* init/destroy cycle */ |
0e5b0a2d BC |
119 | void throttle_init(ThrottleState *ts); |
120 | ||
121 | void throttle_timers_init(ThrottleTimers *tt, | |
122 | AioContext *aio_context, | |
123 | QEMUClockType clock_type, | |
124 | QEMUTimerCB *read_timer_cb, | |
125 | QEMUTimerCB *write_timer_cb, | |
126 | void *timer_opaque); | |
5ddfffbd | 127 | |
0e5b0a2d | 128 | void throttle_timers_destroy(ThrottleTimers *tt); |
5ddfffbd | 129 | |
0e5b0a2d | 130 | void throttle_timers_detach_aio_context(ThrottleTimers *tt); |
13af91eb | 131 | |
0e5b0a2d BC |
132 | void throttle_timers_attach_aio_context(ThrottleTimers *tt, |
133 | AioContext *new_context); | |
13af91eb | 134 | |
0e5b0a2d | 135 | bool throttle_timers_are_initialized(ThrottleTimers *tt); |
5ddfffbd BC |
136 | |
137 | /* configuration */ | |
138 | bool throttle_enabled(ThrottleConfig *cfg); | |
139 | ||
03ba36c8 | 140 | bool throttle_is_valid(ThrottleConfig *cfg, Error **errp); |
5ddfffbd | 141 | |
0e5b0a2d | 142 | void throttle_config(ThrottleState *ts, |
dbe824cc | 143 | QEMUClockType clock_type, |
0e5b0a2d | 144 | ThrottleConfig *cfg); |
5ddfffbd BC |
145 | |
146 | void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg); | |
147 | ||
1588ab5d AG |
148 | void throttle_config_init(ThrottleConfig *cfg); |
149 | ||
5ddfffbd | 150 | /* usage */ |
0e5b0a2d BC |
151 | bool throttle_schedule_timer(ThrottleState *ts, |
152 | ThrottleTimers *tt, | |
153 | bool is_write); | |
5ddfffbd BC |
154 | |
155 | void throttle_account(ThrottleState *ts, bool is_write, uint64_t size); | |
432d889e MP |
156 | void throttle_limits_to_config(ThrottleLimits *arg, ThrottleConfig *cfg, |
157 | Error **errp); | |
158 | void throttle_config_to_limits(ThrottleConfig *cfg, ThrottleLimits *var); | |
5ddfffbd BC |
159 | |
160 | #endif |