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