Revision 28c28973

b/Makefile.target
161 161
# System emulator target
162 162
ifdef CONFIG_SOFTMMU
163 163

  
164
obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o vl.o
164
obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o vl.o balloon.o
165 165
# virtio has to be here due to weird dependency between PCI and virtio-net.
166 166
# need to fix this properly
167 167
obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-serial-bus.o
b/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 "sysemu.h"
26
#include "monitor.h"
27
#include "qjson.h"
28
#include "qint.h"
29
#include "cpu-common.h"
30
#include "kvm.h"
31
#include "balloon.h"
32

  
33

  
34
static QEMUBalloonEvent *qemu_balloon_event;
35
void *qemu_balloon_event_opaque;
36

  
37
void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
38
{
39
    qemu_balloon_event = func;
40
    qemu_balloon_event_opaque = opaque;
41
}
42

  
43
int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
44
{
45
    if (qemu_balloon_event) {
46
        qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
47
        return 1;
48
    } else {
49
        return 0;
50
    }
51
}
52

  
53
int qemu_balloon_status(MonitorCompletion cb, void *opaque)
54
{
55
    if (qemu_balloon_event) {
56
        qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
57
        return 1;
58
    } else {
59
        return 0;
60
    }
61
}
62

  
63
static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
64
{
65
    Monitor *mon = opaque;
66

  
67
    if (strcmp(key, "actual"))
68
        monitor_printf(mon, ",%s=%" PRId64, key,
69
                       qint_get_int(qobject_to_qint(obj)));
70
}
71

  
72
void monitor_print_balloon(Monitor *mon, const QObject *data)
73
{
74
    QDict *qdict;
75

  
76
    qdict = qobject_to_qdict(data);
77
    if (!qdict_haskey(qdict, "actual"))
78
        return;
79

  
80
    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
 */
107
int 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
 */
128
int 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
}
b/balloon.h
14 14
#ifndef _QEMU_BALLOON_H
15 15
#define _QEMU_BALLOON_H
16 16

  
17
#include "monitor.h"
18

  
17 19
typedef void (QEMUBalloonEvent)(void *opaque, ram_addr_t target,
18 20
                                MonitorCompletion cb, void *cb_data);
19 21

  
......
23 25

  
24 26
int qemu_balloon_status(MonitorCompletion cb, void *opaque);
25 27

  
28
void monitor_print_balloon(Monitor *mon, const QObject *data);
29
int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque);
30
int do_balloon(Monitor *mon, const QDict *params,
31
               MonitorCompletion cb, void *opaque);
32

  
26 33
#endif
b/monitor.c
2266 2266
                                    vm_running, singlestep);
2267 2267
}
2268 2268

  
2269
static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
2270
{
2271
    Monitor *mon = opaque;
2272

  
2273
    if (strcmp(key, "actual"))
2274
        monitor_printf(mon, ",%s=%" PRId64, key,
2275
                       qint_get_int(qobject_to_qint(obj)));
2276
}
2277

  
2278
static void monitor_print_balloon(Monitor *mon, const QObject *data)
2279
{
2280
    QDict *qdict;
2281

  
2282
    qdict = qobject_to_qdict(data);
2283
    if (!qdict_haskey(qdict, "actual"))
2284
        return;
2285

  
2286
    monitor_printf(mon, "balloon: actual=%" PRId64,
2287
                   qdict_get_int(qdict, "actual") >> 20);
2288
    qdict_iter(qdict, print_balloon_stat, mon);
2289
    monitor_printf(mon, "\n");
2290
}
2291

  
2292
/**
2293
 * do_info_balloon(): Balloon information
2294
 *
2295
 * Make an asynchronous request for balloon info.  When the request completes
2296
 * a QDict will be returned according to the following specification:
2297
 *
2298
 * - "actual": current balloon value in bytes
2299
 * The following fields may or may not be present:
2300
 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2301
 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2302
 * - "major_page_faults": Number of major faults
2303
 * - "minor_page_faults": Number of minor faults
2304
 * - "free_mem": Total amount of free and unused memory (bytes)
2305
 * - "total_mem": Total amount of available memory (bytes)
2306
 *
2307
 * Example:
2308
 *
2309
 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2310
 *   "major_page_faults": 142, "minor_page_faults": 239245,
2311
 *   "free_mem": 1014185984, "total_mem": 1044668416 }
2312
 */
2313
static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
2314
{
2315
    int ret;
2316

  
2317
    if (kvm_enabled() && !kvm_has_sync_mmu()) {
2318
        qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2319
        return -1;
2320
    }
2321

  
2322
    ret = qemu_balloon_status(cb, opaque);
2323
    if (!ret) {
2324
        qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
2325
        return -1;
2326
    }
2327

  
2328
    return 0;
2329
}
2330

  
2331
/**
2332
 * do_balloon(): Request VM to change its memory allocation
2333
 */
2334
static int do_balloon(Monitor *mon, const QDict *params,
2335
                       MonitorCompletion cb, void *opaque)
2336
{
2337
    int ret;
2338

  
2339
    if (kvm_enabled() && !kvm_has_sync_mmu()) {
2340
        qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2341
        return -1;
2342
    }
2343

  
2344
    ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2345
    if (ret == 0) {
2346
        qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
2347
        return -1;
2348
    }
2349

  
2350
    cb(opaque, NULL);
2351
    return 0;
2352
}
2353

  
2354 2269
static qemu_acl *find_acl(Monitor *mon, const char *name)
2355 2270
{
2356 2271
    qemu_acl *acl = qemu_acl_find(name);
b/vl.c
146 146
#include "audio/audio.h"
147 147
#include "migration.h"
148 148
#include "kvm.h"
149
#include "balloon.h"
150 149
#include "qemu-option.h"
151 150
#include "qemu-config.h"
152 151
#include "qemu-objects.h"
......
307 306
#endif    	
308 307
}
309 308
 
310
/***************/
311
/* ballooning */
312

  
313
static QEMUBalloonEvent *qemu_balloon_event;
314
void *qemu_balloon_event_opaque;
315

  
316
void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
317
{
318
    qemu_balloon_event = func;
319
    qemu_balloon_event_opaque = opaque;
320
}
321

  
322
int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
323
{
324
    if (qemu_balloon_event) {
325
        qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
326
        return 1;
327
    } else {
328
        return 0;
329
    }
330
}
331

  
332
int qemu_balloon_status(MonitorCompletion cb, void *opaque)
333
{
334
    if (qemu_balloon_event) {
335
        qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
336
        return 1;
337
    } else {
338
        return 0;
339
    }
340
}
341

  
342

  
343 309
/***********************************************************/
344 310
/* real time host monotonic timer */
345 311

  

Also available in: Unified diff