virtio-balloon: reset the statistic timer to load device
[qemu/ar7.git] / hw / virtio / virtio-balloon.c
blobc74101e47921ce305d331997d4241e2cb6eacd05
1 /*
2 * Virtio Balloon Device
4 * Copyright IBM, Corp. 2008
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include "qemu/osdep.h"
17 #include "qemu/iov.h"
18 #include "qemu/timer.h"
19 #include "qemu-common.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/i386/pc.h"
22 #include "cpu.h"
23 #include "sysemu/balloon.h"
24 #include "hw/virtio/virtio-balloon.h"
25 #include "sysemu/kvm.h"
26 #include "exec/address-spaces.h"
27 #include "qapi/visitor.h"
28 #include "qapi-event.h"
29 #include "trace.h"
31 #if defined(__linux__)
32 #include <sys/mman.h>
33 #endif
35 #include "hw/virtio/virtio-bus.h"
36 #include "hw/virtio/virtio-access.h"
38 static void balloon_page(void *addr, int deflate)
40 #if defined(__linux__)
41 if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
42 kvm_has_sync_mmu())) {
43 qemu_madvise(addr, TARGET_PAGE_SIZE,
44 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
46 #endif
49 static const char *balloon_stat_names[] = {
50 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
51 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
52 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
53 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
54 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
55 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
56 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
57 [VIRTIO_BALLOON_S_NR] = NULL
61 * reset_stats - Mark all items in the stats array as unset
63 * This function needs to be called at device initialization and before
64 * updating to a set of newly-generated stats. This will ensure that no
65 * stale values stick around in case the guest reports a subset of the supported
66 * statistics.
68 static inline void reset_stats(VirtIOBalloon *dev)
70 int i;
71 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
74 static bool balloon_stats_supported(const VirtIOBalloon *s)
76 VirtIODevice *vdev = VIRTIO_DEVICE(s);
77 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
80 static bool balloon_stats_enabled(const VirtIOBalloon *s)
82 return s->stats_poll_interval > 0;
85 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
87 if (balloon_stats_enabled(s)) {
88 timer_del(s->stats_timer);
89 timer_free(s->stats_timer);
90 s->stats_timer = NULL;
91 s->stats_poll_interval = 0;
95 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
97 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
100 static void balloon_stats_poll_cb(void *opaque)
102 VirtIOBalloon *s = opaque;
103 VirtIODevice *vdev = VIRTIO_DEVICE(s);
105 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
106 /* re-schedule */
107 balloon_stats_change_timer(s, s->stats_poll_interval);
108 return;
111 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
112 virtio_notify(vdev, s->svq);
113 g_free(s->stats_vq_elem);
114 s->stats_vq_elem = NULL;
117 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
118 void *opaque, Error **errp)
120 Error *err = NULL;
121 VirtIOBalloon *s = opaque;
122 int i;
124 visit_start_struct(v, name, NULL, 0, &err);
125 if (err) {
126 goto out;
128 visit_type_int(v, "last-update", &s->stats_last_update, &err);
129 if (err) {
130 goto out_end;
133 visit_start_struct(v, "stats", NULL, 0, &err);
134 if (err) {
135 goto out_end;
137 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
138 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
139 if (err) {
140 break;
143 error_propagate(errp, err);
144 err = NULL;
145 visit_end_struct(v, &err);
147 out_end:
148 error_propagate(errp, err);
149 err = NULL;
150 visit_end_struct(v, &err);
151 out:
152 error_propagate(errp, err);
155 static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
156 const char *name, void *opaque,
157 Error **errp)
159 VirtIOBalloon *s = opaque;
160 visit_type_int(v, name, &s->stats_poll_interval, errp);
163 static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
164 const char *name, void *opaque,
165 Error **errp)
167 VirtIOBalloon *s = opaque;
168 Error *local_err = NULL;
169 int64_t value;
171 visit_type_int(v, name, &value, &local_err);
172 if (local_err) {
173 error_propagate(errp, local_err);
174 return;
177 if (value < 0) {
178 error_setg(errp, "timer value must be greater than zero");
179 return;
182 if (value > UINT32_MAX) {
183 error_setg(errp, "timer value is too big");
184 return;
187 if (value == s->stats_poll_interval) {
188 return;
191 if (value == 0) {
192 /* timer=0 disables the timer */
193 balloon_stats_destroy_timer(s);
194 return;
197 if (balloon_stats_enabled(s)) {
198 /* timer interval change */
199 s->stats_poll_interval = value;
200 balloon_stats_change_timer(s, value);
201 return;
204 /* create a new timer */
205 g_assert(s->stats_timer == NULL);
206 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
207 s->stats_poll_interval = value;
208 balloon_stats_change_timer(s, 0);
211 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
213 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
214 VirtQueueElement *elem;
215 MemoryRegionSection section;
217 for (;;) {
218 size_t offset = 0;
219 uint32_t pfn;
220 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
221 if (!elem) {
222 return;
225 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
226 ram_addr_t pa;
227 ram_addr_t addr;
228 int p = virtio_ldl_p(vdev, &pfn);
230 pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
231 offset += 4;
233 /* FIXME: remove get_system_memory(), but how? */
234 section = memory_region_find(get_system_memory(), pa, 1);
235 if (!int128_nz(section.size) || !memory_region_is_ram(section.mr))
236 continue;
238 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
239 pa);
240 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
241 should be OK because we only want a single page. */
242 addr = section.offset_within_region;
243 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
244 !!(vq == s->dvq));
245 memory_region_unref(section.mr);
248 virtqueue_push(vq, elem, offset);
249 virtio_notify(vdev, vq);
250 g_free(elem);
254 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
256 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
257 VirtQueueElement *elem;
258 VirtIOBalloonStat stat;
259 size_t offset = 0;
260 qemu_timeval tv;
262 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
263 if (!elem) {
264 goto out;
267 if (s->stats_vq_elem != NULL) {
268 /* This should never happen if the driver follows the spec. */
269 virtqueue_push(vq, s->stats_vq_elem, 0);
270 virtio_notify(vdev, vq);
271 g_free(s->stats_vq_elem);
274 s->stats_vq_elem = elem;
276 /* Initialize the stats to get rid of any stale values. This is only
277 * needed to handle the case where a guest supports fewer stats than it
278 * used to (ie. it has booted into an old kernel).
280 reset_stats(s);
282 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
283 == sizeof(stat)) {
284 uint16_t tag = virtio_tswap16(vdev, stat.tag);
285 uint64_t val = virtio_tswap64(vdev, stat.val);
287 offset += sizeof(stat);
288 if (tag < VIRTIO_BALLOON_S_NR)
289 s->stats[tag] = val;
291 s->stats_vq_offset = offset;
293 if (qemu_gettimeofday(&tv) < 0) {
294 fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);
295 goto out;
298 s->stats_last_update = tv.tv_sec;
300 out:
301 if (balloon_stats_enabled(s)) {
302 balloon_stats_change_timer(s, s->stats_poll_interval);
306 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
308 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
309 struct virtio_balloon_config config;
311 config.num_pages = cpu_to_le32(dev->num_pages);
312 config.actual = cpu_to_le32(dev->actual);
314 trace_virtio_balloon_get_config(config.num_pages, config.actual);
315 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
318 static int build_dimm_list(Object *obj, void *opaque)
320 GSList **list = opaque;
322 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
323 DeviceState *dev = DEVICE(obj);
324 if (dev->realized) { /* only realized DIMMs matter */
325 *list = g_slist_prepend(*list, dev);
329 object_child_foreach(obj, build_dimm_list, opaque);
330 return 0;
333 static ram_addr_t get_current_ram_size(void)
335 GSList *list = NULL, *item;
336 ram_addr_t size = ram_size;
338 build_dimm_list(qdev_get_machine(), &list);
339 for (item = list; item; item = g_slist_next(item)) {
340 Object *obj = OBJECT(item->data);
341 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
342 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
343 &error_abort);
346 g_slist_free(list);
348 return size;
351 static void virtio_balloon_set_config(VirtIODevice *vdev,
352 const uint8_t *config_data)
354 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
355 struct virtio_balloon_config config;
356 uint32_t oldactual = dev->actual;
357 ram_addr_t vm_ram_size = get_current_ram_size();
359 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
360 dev->actual = le32_to_cpu(config.actual);
361 if (dev->actual != oldactual) {
362 qapi_event_send_balloon_change(vm_ram_size -
363 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
364 &error_abort);
366 trace_virtio_balloon_set_config(dev->actual, oldactual);
369 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
370 Error **errp)
372 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
373 f |= dev->host_features;
374 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
375 return f;
378 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
380 VirtIOBalloon *dev = opaque;
381 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
382 VIRTIO_BALLOON_PFN_SHIFT);
385 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
387 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
388 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
389 ram_addr_t vm_ram_size = get_current_ram_size();
391 if (target > vm_ram_size) {
392 target = vm_ram_size;
394 if (target) {
395 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
396 virtio_notify_config(vdev);
398 trace_virtio_balloon_to_target(target, dev->num_pages);
401 static void virtio_balloon_save(QEMUFile *f, void *opaque)
403 virtio_save(VIRTIO_DEVICE(opaque), f);
406 static void virtio_balloon_save_device(VirtIODevice *vdev, QEMUFile *f)
408 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
410 qemu_put_be32(f, s->num_pages);
411 qemu_put_be32(f, s->actual);
414 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
416 if (version_id != 1)
417 return -EINVAL;
419 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
422 static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
423 int version_id)
425 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
427 s->num_pages = qemu_get_be32(f);
428 s->actual = qemu_get_be32(f);
430 if (balloon_stats_enabled(s)) {
431 balloon_stats_change_timer(s, s->stats_poll_interval);
433 return 0;
436 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
438 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
439 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
440 int ret;
442 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
443 sizeof(struct virtio_balloon_config));
445 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
446 virtio_balloon_stat, s);
448 if (ret < 0) {
449 error_setg(errp, "Only one balloon device is supported");
450 virtio_cleanup(vdev);
451 return;
454 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
455 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
456 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
458 reset_stats(s);
460 register_savevm(dev, "virtio-balloon", -1, 1,
461 virtio_balloon_save, virtio_balloon_load, s);
464 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
466 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
467 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
469 balloon_stats_destroy_timer(s);
470 qemu_remove_balloon_handler(s);
471 unregister_savevm(dev, "virtio-balloon", s);
472 virtio_cleanup(vdev);
475 static void virtio_balloon_device_reset(VirtIODevice *vdev)
477 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
479 if (s->stats_vq_elem != NULL) {
480 g_free(s->stats_vq_elem);
481 s->stats_vq_elem = NULL;
485 static void virtio_balloon_instance_init(Object *obj)
487 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
489 object_property_add(obj, "guest-stats", "guest statistics",
490 balloon_stats_get_all, NULL, NULL, s, NULL);
492 object_property_add(obj, "guest-stats-polling-interval", "int",
493 balloon_stats_get_poll_interval,
494 balloon_stats_set_poll_interval,
495 NULL, s, NULL);
498 static Property virtio_balloon_properties[] = {
499 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
500 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
501 DEFINE_PROP_END_OF_LIST(),
504 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
506 DeviceClass *dc = DEVICE_CLASS(klass);
507 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
509 dc->props = virtio_balloon_properties;
510 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
511 vdc->realize = virtio_balloon_device_realize;
512 vdc->unrealize = virtio_balloon_device_unrealize;
513 vdc->reset = virtio_balloon_device_reset;
514 vdc->get_config = virtio_balloon_get_config;
515 vdc->set_config = virtio_balloon_set_config;
516 vdc->get_features = virtio_balloon_get_features;
517 vdc->save = virtio_balloon_save_device;
518 vdc->load = virtio_balloon_load_device;
521 static const TypeInfo virtio_balloon_info = {
522 .name = TYPE_VIRTIO_BALLOON,
523 .parent = TYPE_VIRTIO_DEVICE,
524 .instance_size = sizeof(VirtIOBalloon),
525 .instance_init = virtio_balloon_instance_init,
526 .class_init = virtio_balloon_class_init,
529 static void virtio_register_types(void)
531 type_register_static(&virtio_balloon_info);
534 type_init(virtio_register_types)