]>
Commit | Line | Data |
---|---|---|
5ddfffbd BC |
1 | /* |
2 | * QEMU throttling infrastructure | |
3 | * | |
4 | * Copyright (C) Nodalink, SARL. 2013 | |
5 | * | |
6 | * Author: | |
7 | * Benoît Canet <benoit.canet@irqsave.net> | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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/>. | |
21 | */ | |
22 | ||
23 | #ifndef THROTTLE_H | |
24 | #define THROTTLE_H | |
25 | ||
26 | #include <stdint.h> | |
27 | #include "qemu-common.h" | |
28 | #include "qemu/timer.h" | |
29 | ||
30 | #define NANOSECONDS_PER_SECOND 1000000000.0 | |
31 | ||
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 */ | |
68 | QEMUTimer * timers[2]; /* timers used to do the throttling */ | |
69 | QEMUClockType clock_type; /* the clock used */ | |
70 | } ThrottleState; | |
71 | ||
72 | /* operations on single leaky buckets */ | |
73 | void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta); | |
74 | ||
75 | int64_t throttle_compute_wait(LeakyBucket *bkt); | |
76 | ||
77 | /* expose timer computation function for unit tests */ | |
78 | bool throttle_compute_timer(ThrottleState *ts, | |
79 | bool is_write, | |
80 | int64_t now, | |
81 | int64_t *next_timestamp); | |
82 | ||
83 | /* init/destroy cycle */ | |
84 | void throttle_init(ThrottleState *ts, | |
85 | QEMUClockType clock_type, | |
86 | void (read_timer)(void *), | |
87 | void (write_timer)(void *), | |
88 | void *timer_opaque); | |
89 | ||
90 | void throttle_destroy(ThrottleState *ts); | |
91 | ||
92 | bool throttle_have_timer(ThrottleState *ts); | |
93 | ||
94 | /* configuration */ | |
95 | bool throttle_enabled(ThrottleConfig *cfg); | |
96 | ||
97 | bool throttle_conflicting(ThrottleConfig *cfg); | |
98 | ||
99 | bool throttle_is_valid(ThrottleConfig *cfg); | |
100 | ||
101 | void throttle_config(ThrottleState *ts, ThrottleConfig *cfg); | |
102 | ||
103 | void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg); | |
104 | ||
105 | /* usage */ | |
106 | bool throttle_schedule_timer(ThrottleState *ts, bool is_write); | |
107 | ||
108 | void throttle_account(ThrottleState *ts, bool is_write, uint64_t size); | |
109 | ||
110 | #endif |