blob: d40be39b3b0702a177b69794a4c2e0b00dd22d72 [file] [log] [blame]
Paolo Bonzini28c28972010-04-01 19:57:12 +02001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Paolo Bonzini28c28972010-04-01 19:57:12 +020025#include "monitor.h"
26#include "qjson.h"
27#include "qint.h"
28#include "cpu-common.h"
29#include "kvm.h"
30#include "balloon.h"
Prerna Saxena62dd89d2010-08-11 17:16:03 +053031#include "trace.h"
Paolo Bonzini28c28972010-04-01 19:57:12 +020032
33
Amit Shah0a2a30d2011-07-20 13:08:46 +053034static QEMUBalloonEvent *balloon_event_fn;
35static void *balloon_opaque;
Paolo Bonzini28c28972010-04-01 19:57:12 +020036
37void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
38{
Amit Shah0a2a30d2011-07-20 13:08:46 +053039 balloon_event_fn = func;
40 balloon_opaque = opaque;
Paolo Bonzini28c28972010-04-01 19:57:12 +020041}
42
Amit Shah0a2a30d2011-07-20 13:08:46 +053043static int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
Paolo Bonzini28c28972010-04-01 19:57:12 +020044{
Amit Shah182b9202011-07-20 13:14:12 +053045 if (!balloon_event_fn) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020046 return 0;
47 }
Amit Shah182b9202011-07-20 13:14:12 +053048 trace_balloon_event(balloon_opaque, target);
49 balloon_event_fn(balloon_opaque, target, cb, opaque);
50 return 1;
Paolo Bonzini28c28972010-04-01 19:57:12 +020051}
52
Amit Shah0a2a30d2011-07-20 13:08:46 +053053static int qemu_balloon_status(MonitorCompletion cb, void *opaque)
Paolo Bonzini28c28972010-04-01 19:57:12 +020054{
Amit Shah182b9202011-07-20 13:14:12 +053055 if (!balloon_event_fn) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020056 return 0;
57 }
Amit Shah182b9202011-07-20 13:14:12 +053058 balloon_event_fn(balloon_opaque, 0, cb, opaque);
59 return 1;
Paolo Bonzini28c28972010-04-01 19:57:12 +020060}
61
62static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
63{
64 Monitor *mon = opaque;
65
Amit Shahb80bc1d2011-07-20 13:12:15 +053066 if (strcmp(key, "actual")) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020067 monitor_printf(mon, ",%s=%" PRId64, key,
68 qint_get_int(qobject_to_qint(obj)));
Amit Shahb80bc1d2011-07-20 13:12:15 +053069 }
Paolo Bonzini28c28972010-04-01 19:57:12 +020070}
71
72void monitor_print_balloon(Monitor *mon, const QObject *data)
73{
74 QDict *qdict;
75
76 qdict = qobject_to_qdict(data);
Amit Shahb80bc1d2011-07-20 13:12:15 +053077 if (!qdict_haskey(qdict, "actual")) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020078 return;
Amit Shahb80bc1d2011-07-20 13:12:15 +053079 }
Paolo Bonzini28c28972010-04-01 19:57:12 +020080 monitor_printf(mon, "balloon: actual=%" PRId64,
81 qdict_get_int(qdict, "actual") >> 20);
82 qdict_iter(qdict, print_balloon_stat, mon);
83 monitor_printf(mon, "\n");
84}
85
86/**
87 * do_info_balloon(): Balloon information
88 *
89 * Make an asynchronous request for balloon info. When the request completes
90 * a QDict will be returned according to the following specification:
91 *
92 * - "actual": current balloon value in bytes
93 * The following fields may or may not be present:
94 * - "mem_swapped_in": Amount of memory swapped in (bytes)
95 * - "mem_swapped_out": Amount of memory swapped out (bytes)
96 * - "major_page_faults": Number of major faults
97 * - "minor_page_faults": Number of minor faults
98 * - "free_mem": Total amount of free and unused memory (bytes)
99 * - "total_mem": Total amount of available memory (bytes)
100 *
101 * Example:
102 *
103 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
104 * "major_page_faults": 142, "minor_page_faults": 239245,
105 * "free_mem": 1014185984, "total_mem": 1044668416 }
106 */
107int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
108{
109 int ret;
110
111 if (kvm_enabled() && !kvm_has_sync_mmu()) {
112 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
113 return -1;
114 }
115
116 ret = qemu_balloon_status(cb, opaque);
117 if (!ret) {
118 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
119 return -1;
120 }
121
122 return 0;
123}
124
125/**
126 * do_balloon(): Request VM to change its memory allocation
127 */
128int do_balloon(Monitor *mon, const QDict *params,
129 MonitorCompletion cb, void *opaque)
130{
131 int ret;
132
133 if (kvm_enabled() && !kvm_has_sync_mmu()) {
134 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
135 return -1;
136 }
137
138 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
139 if (ret == 0) {
140 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
141 return -1;
142 }
143
144 cb(opaque, NULL);
145 return 0;
146}