balloon.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Generic Balloon handlers and management
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (C) 2011 Red Hat, Inc.
  6. * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  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. #include "trace.h"
  33. static QEMUBalloonEvent *balloon_event_fn;
  34. static QEMUBalloonStatus *balloon_stat_fn;
  35. static void *balloon_opaque;
  36. int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
  37. QEMUBalloonStatus *stat_func, void *opaque)
  38. {
  39. if (balloon_event_fn || balloon_stat_fn || balloon_opaque) {
  40. /* We're already registered one balloon handler. How many can
  41. * a guest really have?
  42. */
  43. error_report("Another balloon device already registered");
  44. return -1;
  45. }
  46. balloon_event_fn = event_func;
  47. balloon_stat_fn = stat_func;
  48. balloon_opaque = opaque;
  49. return 0;
  50. }
  51. static int qemu_balloon(ram_addr_t target)
  52. {
  53. if (!balloon_event_fn) {
  54. return 0;
  55. }
  56. trace_balloon_event(balloon_opaque, target);
  57. balloon_event_fn(balloon_opaque, target);
  58. return 1;
  59. }
  60. static int qemu_balloon_status(MonitorCompletion cb, void *opaque)
  61. {
  62. if (!balloon_stat_fn) {
  63. return 0;
  64. }
  65. balloon_stat_fn(balloon_opaque, cb, opaque);
  66. return 1;
  67. }
  68. static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
  69. {
  70. Monitor *mon = opaque;
  71. if (strcmp(key, "actual")) {
  72. monitor_printf(mon, ",%s=%" PRId64, key,
  73. qint_get_int(qobject_to_qint(obj)));
  74. }
  75. }
  76. void monitor_print_balloon(Monitor *mon, const QObject *data)
  77. {
  78. QDict *qdict;
  79. qdict = qobject_to_qdict(data);
  80. if (!qdict_haskey(qdict, "actual")) {
  81. return;
  82. }
  83. 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. * do_info_balloon(): Balloon information
  90. *
  91. * Make an asynchronous request for balloon info. When the request completes
  92. * a QDict will be returned according to the following specification:
  93. *
  94. * - "actual": current balloon value in bytes
  95. * The following fields may or may not be present:
  96. * - "mem_swapped_in": Amount of memory swapped in (bytes)
  97. * - "mem_swapped_out": Amount of memory swapped out (bytes)
  98. * - "major_page_faults": Number of major faults
  99. * - "minor_page_faults": Number of minor faults
  100. * - "free_mem": Total amount of free and unused memory (bytes)
  101. * - "total_mem": Total amount of available memory (bytes)
  102. *
  103. * Example:
  104. *
  105. * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
  106. * "major_page_faults": 142, "minor_page_faults": 239245,
  107. * "free_mem": 1014185984, "total_mem": 1044668416 }
  108. */
  109. int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
  110. {
  111. int ret;
  112. if (kvm_enabled() && !kvm_has_sync_mmu()) {
  113. qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
  114. return -1;
  115. }
  116. ret = qemu_balloon_status(cb, opaque);
  117. if (!ret) {
  118. qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. /**
  124. * do_balloon(): Request VM to change its memory allocation
  125. */
  126. int do_balloon(Monitor *mon, const QDict *params,
  127. MonitorCompletion cb, void *opaque)
  128. {
  129. int64_t target;
  130. int ret;
  131. if (kvm_enabled() && !kvm_has_sync_mmu()) {
  132. qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
  133. return -1;
  134. }
  135. target = qdict_get_int(params, "value");
  136. if (target <= 0) {
  137. qerror_report(QERR_INVALID_PARAMETER_VALUE, "target", "a size");
  138. return -1;
  139. }
  140. ret = qemu_balloon(target);
  141. if (ret == 0) {
  142. qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
  143. return -1;
  144. }
  145. cb(opaque, NULL);
  146. return 0;
  147. }