]>
Commit | Line | Data |
---|---|---|
5ddfffbd BC |
1 | /* |
2 | * QEMU throttling infrastructure | |
3 | * | |
a291d5d9 AG |
4 | * Copyright (C) Nodalink, EURL. 2013-2014 |
5 | * Copyright (C) Igalia, S.L. 2015 | |
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 | ||
28 | #include <stdint.h> | |
29 | #include "qemu-common.h" | |
30 | #include "qemu/timer.h" | |
31 | ||
5ddfffbd BC |
32 | typedef enum { |
33 | THROTTLE_BPS_TOTAL, | |
34 | THROTTLE_BPS_READ, | |
35 | THROTTLE_BPS_WRITE, | |
36 | THROTTLE_OPS_TOTAL, | |
37 | THROTTLE_OPS_READ, | |
38 | THROTTLE_OPS_WRITE, | |
39 | BUCKETS_COUNT, | |
40 | } BucketType; | |
41 | ||
42 | /* | |
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. | |
47 | */ | |
48 | ||
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 */ | |
53 | } LeakyBucket; | |
54 | ||
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. | |
59 | */ | |
60 | typedef struct ThrottleConfig { | |
61 | LeakyBucket buckets[BUCKETS_COUNT]; /* leaky buckets */ | |
62 | uint64_t op_size; /* size of an operation in bytes */ | |
63 | } ThrottleConfig; | |
64 | ||
65 | typedef struct ThrottleState { | |
66 | ThrottleConfig cfg; /* configuration */ | |
67 | int64_t previous_leak; /* timestamp of the last leak done */ | |
0e5b0a2d BC |
68 | } ThrottleState; |
69 | ||
70 | typedef struct ThrottleTimers { | |
71 | QEMUTimer *timers[2]; /* timers used to do the throttling */ | |
5ddfffbd | 72 | QEMUClockType clock_type; /* the clock used */ |
13af91eb SH |
73 | |
74 | /* Callbacks */ | |
75 | QEMUTimerCB *read_timer_cb; | |
76 | QEMUTimerCB *write_timer_cb; | |
77 | void *timer_opaque; | |
0e5b0a2d | 78 | } ThrottleTimers; |
5ddfffbd BC |
79 | |
80 | /* operations on single leaky buckets */ | |
81 | void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta); | |
82 | ||
83 | int64_t throttle_compute_wait(LeakyBucket *bkt); | |
84 | ||
85 | /* expose timer computation function for unit tests */ | |
86 | bool throttle_compute_timer(ThrottleState *ts, | |
87 | bool is_write, | |
88 | int64_t now, | |
89 | int64_t *next_timestamp); | |
90 | ||
91 | /* init/destroy cycle */ | |
0e5b0a2d BC |
92 | void throttle_init(ThrottleState *ts); |
93 | ||
94 | void throttle_timers_init(ThrottleTimers *tt, | |
95 | AioContext *aio_context, | |
96 | QEMUClockType clock_type, | |
97 | QEMUTimerCB *read_timer_cb, | |
98 | QEMUTimerCB *write_timer_cb, | |
99 | void *timer_opaque); | |
5ddfffbd | 100 | |
0e5b0a2d | 101 | void throttle_timers_destroy(ThrottleTimers *tt); |
5ddfffbd | 102 | |
0e5b0a2d | 103 | void throttle_timers_detach_aio_context(ThrottleTimers *tt); |
13af91eb | 104 | |
0e5b0a2d BC |
105 | void throttle_timers_attach_aio_context(ThrottleTimers *tt, |
106 | AioContext *new_context); | |
13af91eb | 107 | |
0e5b0a2d | 108 | bool throttle_timers_are_initialized(ThrottleTimers *tt); |
5ddfffbd BC |
109 | |
110 | /* configuration */ | |
111 | bool throttle_enabled(ThrottleConfig *cfg); | |
112 | ||
113 | bool throttle_conflicting(ThrottleConfig *cfg); | |
114 | ||
115 | bool throttle_is_valid(ThrottleConfig *cfg); | |
116 | ||
ee2bdc33 SH |
117 | bool throttle_max_is_missing_limit(ThrottleConfig *cfg); |
118 | ||
0e5b0a2d BC |
119 | void throttle_config(ThrottleState *ts, |
120 | ThrottleTimers *tt, | |
121 | ThrottleConfig *cfg); | |
5ddfffbd BC |
122 | |
123 | void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg); | |
124 | ||
125 | /* usage */ | |
0e5b0a2d BC |
126 | bool throttle_schedule_timer(ThrottleState *ts, |
127 | ThrottleTimers *tt, | |
128 | bool is_write); | |
5ddfffbd BC |
129 | |
130 | void throttle_account(ThrottleState *ts, bool is_write, uint64_t size); | |
131 | ||
132 | #endif |