Merge remote-tracking branch 'remotes/kraxel/tags/pull-vga-20141002-1' into staging
[qemu-kvm.git] / include / qemu / throttle.h
blobb890613a9ca0d0c937fdc00b624680dfa7c72e0f
1 /*
2 * QEMU throttling infrastructure
4 * Copyright (C) Nodalink, SARL. 2013
6 * Author:
7 * BenoƮt Canet <benoit.canet@irqsave.net>
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.
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.
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/>.
23 #ifndef THROTTLE_H
24 #define THROTTLE_H
26 #include <stdint.h>
27 #include "qemu-common.h"
28 #include "qemu/timer.h"
30 #define NANOSECONDS_PER_SECOND 1000000000.0
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;
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.
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;
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.
60 typedef struct ThrottleConfig {
61 LeakyBucket buckets[BUCKETS_COUNT]; /* leaky buckets */
62 uint64_t op_size; /* size of an operation in bytes */
63 } ThrottleConfig;
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 */
71 /* Callbacks */
72 QEMUTimerCB *read_timer_cb;
73 QEMUTimerCB *write_timer_cb;
74 void *timer_opaque;
75 } ThrottleState;
77 /* operations on single leaky buckets */
78 void throttle_leak_bucket(LeakyBucket *bkt, int64_t delta);
80 int64_t throttle_compute_wait(LeakyBucket *bkt);
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);
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);
96 void throttle_destroy(ThrottleState *ts);
98 void throttle_detach_aio_context(ThrottleState *ts);
100 void throttle_attach_aio_context(ThrottleState *ts, AioContext *new_context);
102 bool throttle_have_timer(ThrottleState *ts);
104 /* configuration */
105 bool throttle_enabled(ThrottleConfig *cfg);
107 bool throttle_conflicting(ThrottleConfig *cfg);
109 bool throttle_is_valid(ThrottleConfig *cfg);
111 void throttle_config(ThrottleState *ts, ThrottleConfig *cfg);
113 void throttle_get_config(ThrottleState *ts, ThrottleConfig *cfg);
115 /* usage */
116 bool throttle_schedule_timer(ThrottleState *ts, bool is_write);
118 void throttle_account(ThrottleState *ts, bool is_write, uint64_t size);
120 #endif