]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - balloon.c
balloon: Make functions, local vars static
[lisovros/qemu_apohw.git] / balloon.c
1 /*
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
25 #include "monitor.h"
26 #include "qjson.h"
27 #include "qint.h"
28 #include "cpu-common.h"
29 #include "kvm.h"
30 #include "balloon.h"
31 #include "trace.h"
32
33
34 static QEMUBalloonEvent *balloon_event_fn;
35 static void *balloon_opaque;
36
37 void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
38 {
39     balloon_event_fn = func;
40     balloon_opaque = opaque;
41 }
42
43 static int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
44 {
45     if (balloon_event_fn) {
46         trace_balloon_event(balloon_opaque, target);
47         balloon_event_fn(balloon_opaque, target, cb, opaque);
48         return 1;
49     } else {
50         return 0;
51     }
52 }
53
54 static int qemu_balloon_status(MonitorCompletion cb, void *opaque)
55 {
56     if (balloon_event_fn) {
57         balloon_event_fn(balloon_opaque, 0, cb, opaque);
58         return 1;
59     } else {
60         return 0;
61     }
62 }
63
64 static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
65 {
66     Monitor *mon = opaque;
67
68     if (strcmp(key, "actual"))
69         monitor_printf(mon, ",%s=%" PRId64, key,
70                        qint_get_int(qobject_to_qint(obj)));
71 }
72
73 void monitor_print_balloon(Monitor *mon, const QObject *data)
74 {
75     QDict *qdict;
76
77     qdict = qobject_to_qdict(data);
78     if (!qdict_haskey(qdict, "actual"))
79         return;
80
81     monitor_printf(mon, "balloon: actual=%" PRId64,
82                    qdict_get_int(qdict, "actual") >> 20);
83     qdict_iter(qdict, print_balloon_stat, mon);
84     monitor_printf(mon, "\n");
85 }
86
87 /**
88  * do_info_balloon(): Balloon information
89  *
90  * Make an asynchronous request for balloon info.  When the request completes
91  * a QDict will be returned according to the following specification:
92  *
93  * - "actual": current balloon value in bytes
94  * The following fields may or may not be present:
95  * - "mem_swapped_in": Amount of memory swapped in (bytes)
96  * - "mem_swapped_out": Amount of memory swapped out (bytes)
97  * - "major_page_faults": Number of major faults
98  * - "minor_page_faults": Number of minor faults
99  * - "free_mem": Total amount of free and unused memory (bytes)
100  * - "total_mem": Total amount of available memory (bytes)
101  *
102  * Example:
103  *
104  * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
105  *   "major_page_faults": 142, "minor_page_faults": 239245,
106  *   "free_mem": 1014185984, "total_mem": 1044668416 }
107  */
108 int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
109 {
110     int ret;
111
112     if (kvm_enabled() && !kvm_has_sync_mmu()) {
113         qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
114         return -1;
115     }
116
117     ret = qemu_balloon_status(cb, opaque);
118     if (!ret) {
119         qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
120         return -1;
121     }
122
123     return 0;
124 }
125
126 /**
127  * do_balloon(): Request VM to change its memory allocation
128  */
129 int do_balloon(Monitor *mon, const QDict *params,
130                MonitorCompletion cb, void *opaque)
131 {
132     int ret;
133
134     if (kvm_enabled() && !kvm_has_sync_mmu()) {
135         qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
136         return -1;
137     }
138
139     ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
140     if (ret == 0) {
141         qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
142         return -1;
143     }
144
145     cb(opaque, NULL);
146     return 0;
147 }