blob: 8be3812ee28ec41ee4aad0881a27060e09389e21 [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;
Amit Shah30fb2ca2011-07-20 13:30:56 +053035static QEMUBalloonStatus *balloon_stat_fn;
Amit Shah0a2a30d2011-07-20 13:08:46 +053036static void *balloon_opaque;
Paolo Bonzini28c28972010-04-01 19:57:12 +020037
Amit Shah30fb2ca2011-07-20 13:30:56 +053038void qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
39 QEMUBalloonStatus *stat_func, void *opaque)
Paolo Bonzini28c28972010-04-01 19:57:12 +020040{
Amit Shah30fb2ca2011-07-20 13:30:56 +053041 balloon_event_fn = event_func;
42 balloon_stat_fn = stat_func;
Amit Shah0a2a30d2011-07-20 13:08:46 +053043 balloon_opaque = opaque;
Paolo Bonzini28c28972010-04-01 19:57:12 +020044}
45
Amit Shah30fb2ca2011-07-20 13:30:56 +053046static int qemu_balloon(ram_addr_t target)
Paolo Bonzini28c28972010-04-01 19:57:12 +020047{
Amit Shah182b9202011-07-20 13:14:12 +053048 if (!balloon_event_fn) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020049 return 0;
50 }
Amit Shah182b9202011-07-20 13:14:12 +053051 trace_balloon_event(balloon_opaque, target);
Amit Shah30fb2ca2011-07-20 13:30:56 +053052 balloon_event_fn(balloon_opaque, target);
Amit Shah182b9202011-07-20 13:14:12 +053053 return 1;
Paolo Bonzini28c28972010-04-01 19:57:12 +020054}
55
Amit Shah0a2a30d2011-07-20 13:08:46 +053056static int qemu_balloon_status(MonitorCompletion cb, void *opaque)
Paolo Bonzini28c28972010-04-01 19:57:12 +020057{
Amit Shah30fb2ca2011-07-20 13:30:56 +053058 if (!balloon_stat_fn) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020059 return 0;
60 }
Amit Shah30fb2ca2011-07-20 13:30:56 +053061 balloon_stat_fn(balloon_opaque, cb, opaque);
Amit Shah182b9202011-07-20 13:14:12 +053062 return 1;
Paolo Bonzini28c28972010-04-01 19:57:12 +020063}
64
65static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
66{
67 Monitor *mon = opaque;
68
Amit Shahb80bc1d2011-07-20 13:12:15 +053069 if (strcmp(key, "actual")) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020070 monitor_printf(mon, ",%s=%" PRId64, key,
71 qint_get_int(qobject_to_qint(obj)));
Amit Shahb80bc1d2011-07-20 13:12:15 +053072 }
Paolo Bonzini28c28972010-04-01 19:57:12 +020073}
74
75void monitor_print_balloon(Monitor *mon, const QObject *data)
76{
77 QDict *qdict;
78
79 qdict = qobject_to_qdict(data);
Amit Shahb80bc1d2011-07-20 13:12:15 +053080 if (!qdict_haskey(qdict, "actual")) {
Paolo Bonzini28c28972010-04-01 19:57:12 +020081 return;
Amit Shahb80bc1d2011-07-20 13:12:15 +053082 }
Paolo Bonzini28c28972010-04-01 19:57:12 +020083 monitor_printf(mon, "balloon: actual=%" PRId64,
84 qdict_get_int(qdict, "actual") >> 20);
85 qdict_iter(qdict, print_balloon_stat, mon);
86 monitor_printf(mon, "\n");
87}
88
89/**
90 * do_info_balloon(): Balloon information
91 *
92 * Make an asynchronous request for balloon info. When the request completes
93 * a QDict will be returned according to the following specification:
94 *
95 * - "actual": current balloon value in bytes
96 * The following fields may or may not be present:
97 * - "mem_swapped_in": Amount of memory swapped in (bytes)
98 * - "mem_swapped_out": Amount of memory swapped out (bytes)
99 * - "major_page_faults": Number of major faults
100 * - "minor_page_faults": Number of minor faults
101 * - "free_mem": Total amount of free and unused memory (bytes)
102 * - "total_mem": Total amount of available memory (bytes)
103 *
104 * Example:
105 *
106 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
107 * "major_page_faults": 142, "minor_page_faults": 239245,
108 * "free_mem": 1014185984, "total_mem": 1044668416 }
109 */
110int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
111{
112 int ret;
113
114 if (kvm_enabled() && !kvm_has_sync_mmu()) {
115 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
116 return -1;
117 }
118
119 ret = qemu_balloon_status(cb, opaque);
120 if (!ret) {
121 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
122 return -1;
123 }
124
125 return 0;
126}
127
128/**
129 * do_balloon(): Request VM to change its memory allocation
130 */
131int do_balloon(Monitor *mon, const QDict *params,
132 MonitorCompletion cb, void *opaque)
133{
134 int ret;
135
136 if (kvm_enabled() && !kvm_has_sync_mmu()) {
137 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
138 return -1;
139 }
140
Amit Shah30fb2ca2011-07-20 13:30:56 +0530141 ret = qemu_balloon(qdict_get_int(params, "value"));
Paolo Bonzini28c28972010-04-01 19:57:12 +0200142 if (ret == 0) {
143 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
144 return -1;
145 }
146
147 cb(opaque, NULL);
148 return 0;
149}