xref: /qemu/include/qemu/throttle.h (revision 3e4c67c9)
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 
71     /* Callbacks */
72     QEMUTimerCB *read_timer_cb;
73     QEMUTimerCB *write_timer_cb;
74     void *timer_opaque;
75 } ThrottleState;
76 
77 /* operations on single leaky buckets */
78 void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta);
79 
80 int64_t throttle_compute_wait(LeakyBucket *bkt);
81 
82 /* expose timer computation function for unit tests */
83 bool throttle_compute_timer(ThrottleState *ts,
84                             bool is_write,
85                             int64_t now,
86                             int64_t *next_timestamp);
87 
88 /* init/destroy cycle */
89 void throttle_init(ThrottleState *ts,
90                    AioContext *aio_context,
91                    QEMUClockType clock_type,
92                    void (read_timer)(void *),
93                    void (write_timer)(void *),
94                    void *timer_opaque);
95 
96 void throttle_destroy(ThrottleState *ts);
97 
98 void throttle_detach_aio_context(ThrottleState *ts);
99 
100 void throttle_attach_aio_context(ThrottleState *ts, AioContext *new_context);
101 
102 bool throttle_have_timer(ThrottleState *ts);
103 
104 /* configuration */
105 bool throttle_enabled(ThrottleConfig *cfg);
106 
107 bool throttle_conflicting(ThrottleConfig *cfg);
108 
109 bool throttle_is_valid(ThrottleConfig *cfg);
110 
111 void throttle_config(ThrottleState *ts, ThrottleConfig *cfg);
112 
113 void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg);
114 
115 /* usage */
116 bool throttle_schedule_timer(ThrottleState *ts, bool is_write);
117 
118 void throttle_account(ThrottleState *ts, bool is_write, uint64_t size);
119 
120 #endif
121