2 * QMP commands related to machines and CPUs
4 * Copyright (C) 2014 Red Hat Inc
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "hw/acpi/vmgenid.h"
12 #include "hw/boards.h"
13 #include "hw/intc/intc.h"
14 #include "hw/mem/memory-device.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-builtin-visit.h"
17 #include "qapi/qapi-commands-machine.h"
18 #include "qapi/qmp/qobject.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "qapi/type-helpers.h"
21 #include "qemu/uuid.h"
22 #include "qom/qom-qobject.h"
23 #include "sysemu/hostmem.h"
24 #include "sysemu/hw_accel.h"
25 #include "sysemu/numa.h"
26 #include "sysemu/runstate.h"
27 #include "sysemu/sysemu.h"
30 * fast means: we NEVER interrupt vCPU threads to retrieve
31 * information from KVM.
33 CpuInfoFastList
*qmp_query_cpus_fast(Error
**errp
)
35 MachineState
*ms
= MACHINE(qdev_get_machine());
36 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
37 CpuInfoFastList
*head
= NULL
, **tail
= &head
;
38 SysEmuTarget target
= qapi_enum_parse(&SysEmuTarget_lookup
, target_name(),
43 CpuInfoFast
*value
= g_malloc0(sizeof(*value
));
45 value
->cpu_index
= cpu
->cpu_index
;
46 value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
47 value
->thread_id
= cpu
->thread_id
;
49 if (mc
->cpu_index_to_instance_props
) {
50 CpuInstanceProperties
*props
;
51 props
= g_malloc0(sizeof(*props
));
52 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
56 value
->target
= target
;
57 if (cpu
->cc
->query_cpu_fast
) {
58 cpu
->cc
->query_cpu_fast(cpu
, value
);
61 QAPI_LIST_APPEND(tail
, value
);
67 MachineInfoList
*qmp_query_machines(bool has_compat_props
, bool compat_props
,
70 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
71 MachineInfoList
*mach_list
= NULL
;
73 for (el
= machines
; el
; el
= el
->next
) {
74 MachineClass
*mc
= el
->data
;
77 info
= g_malloc0(sizeof(*info
));
79 info
->has_is_default
= true;
80 info
->is_default
= true;
84 info
->alias
= g_strdup(mc
->alias
);
87 info
->name
= g_strdup(mc
->name
);
88 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
89 info
->hotpluggable_cpus
= mc
->has_hotpluggable_cpus
;
90 info
->numa_mem_supported
= mc
->numa_mem_supported
;
91 info
->deprecated
= !!mc
->deprecation_reason
;
92 info
->acpi
= !!object_class_property_find(OBJECT_CLASS(mc
), "acpi");
93 if (mc
->default_cpu_type
) {
94 info
->default_cpu_type
= g_strdup(mc
->default_cpu_type
);
96 if (mc
->default_ram_id
) {
97 info
->default_ram_id
= g_strdup(mc
->default_ram_id
);
100 if (compat_props
&& mc
->compat_props
) {
102 info
->compat_props
= NULL
;
103 CompatPropertyList
**tail
= &(info
->compat_props
);
104 info
->has_compat_props
= true;
106 for (i
= 0; i
< mc
->compat_props
->len
; i
++) {
107 GlobalProperty
*mt_prop
= g_ptr_array_index(mc
->compat_props
,
109 CompatProperty
*prop
;
111 prop
= g_malloc0(sizeof(*prop
));
112 prop
->qom_type
= g_strdup(mt_prop
->driver
);
113 prop
->property
= g_strdup(mt_prop
->property
);
114 prop
->value
= g_strdup(mt_prop
->value
);
116 QAPI_LIST_APPEND(tail
, prop
);
120 QAPI_LIST_PREPEND(mach_list
, info
);
123 g_slist_free(machines
);
127 CurrentMachineParams
*qmp_query_current_machine(Error
**errp
)
129 CurrentMachineParams
*params
= g_malloc0(sizeof(*params
));
130 params
->wakeup_suspend_support
= qemu_wakeup_suspend_enabled();
135 TargetInfo
*qmp_query_target(Error
**errp
)
137 TargetInfo
*info
= g_malloc0(sizeof(*info
));
139 info
->arch
= qapi_enum_parse(&SysEmuTarget_lookup
, target_name(), -1,
145 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
147 MachineState
*ms
= MACHINE(qdev_get_machine());
148 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
150 if (!mc
->has_hotpluggable_cpus
) {
151 error_setg(errp
, "machine does not support hot-plugging CPUs");
155 return machine_query_hotpluggable_cpus(ms
);
158 void qmp_set_numa_node(NumaOptions
*cmd
, Error
**errp
)
160 if (phase_check(PHASE_MACHINE_INITIALIZED
)) {
161 error_setg(errp
, "The command is permitted only before the machine has been created");
165 set_numa_options(MACHINE(qdev_get_machine()), cmd
, errp
);
168 static int query_memdev(Object
*obj
, void *opaque
)
171 MemdevList
**list
= opaque
;
176 if (object_dynamic_cast(obj
, TYPE_MEMORY_BACKEND
)) {
177 m
= g_malloc0(sizeof(*m
));
179 m
->id
= g_strdup(object_get_canonical_path_component(obj
));
181 m
->size
= object_property_get_uint(obj
, "size", &error_abort
);
182 m
->merge
= object_property_get_bool(obj
, "merge", &error_abort
);
183 m
->dump
= object_property_get_bool(obj
, "dump", &error_abort
);
184 m
->prealloc
= object_property_get_bool(obj
, "prealloc", &error_abort
);
185 m
->share
= object_property_get_bool(obj
, "share", &error_abort
);
186 m
->reserve
= object_property_get_bool(obj
, "reserve", &err
);
188 error_free_or_abort(&err
);
190 m
->has_reserve
= true;
192 m
->policy
= object_property_get_enum(obj
, "policy", "HostMemPolicy",
194 host_nodes
= object_property_get_qobject(obj
,
197 v
= qobject_input_visitor_new(host_nodes
);
198 visit_type_uint16List(v
, NULL
, &m
->host_nodes
, &error_abort
);
200 qobject_unref(host_nodes
);
202 QAPI_LIST_PREPEND(*list
, m
);
208 MemdevList
*qmp_query_memdev(Error
**errp
)
210 Object
*obj
= object_get_objects_root();
211 MemdevList
*list
= NULL
;
213 object_child_foreach(obj
, query_memdev
, &list
);
217 HumanReadableText
*qmp_x_query_numa(Error
**errp
)
219 g_autoptr(GString
) buf
= g_string_new("");
220 int i
, nb_numa_nodes
;
221 NumaNodeMem
*node_mem
;
222 CpuInfoFastList
*cpu_list
, *cpu
;
223 MachineState
*ms
= MACHINE(qdev_get_machine());
225 nb_numa_nodes
= ms
->numa_state
? ms
->numa_state
->num_nodes
: 0;
226 g_string_append_printf(buf
, "%d nodes\n", nb_numa_nodes
);
227 if (!nb_numa_nodes
) {
231 cpu_list
= qmp_query_cpus_fast(&error_abort
);
232 node_mem
= g_new0(NumaNodeMem
, nb_numa_nodes
);
234 query_numa_node_mem(node_mem
, ms
);
235 for (i
= 0; i
< nb_numa_nodes
; i
++) {
236 g_string_append_printf(buf
, "node %d cpus:", i
);
237 for (cpu
= cpu_list
; cpu
; cpu
= cpu
->next
) {
238 if (cpu
->value
->props
&& cpu
->value
->props
->has_node_id
&&
239 cpu
->value
->props
->node_id
== i
) {
240 g_string_append_printf(buf
, " %" PRIi64
, cpu
->value
->cpu_index
);
243 g_string_append_printf(buf
, "\n");
244 g_string_append_printf(buf
, "node %d size: %" PRId64
" MB\n", i
,
245 node_mem
[i
].node_mem
>> 20);
246 g_string_append_printf(buf
, "node %d plugged: %" PRId64
" MB\n", i
,
247 node_mem
[i
].node_plugged_mem
>> 20);
249 qapi_free_CpuInfoFastList(cpu_list
);
253 return human_readable_text_from_str(buf
);
256 KvmInfo
*qmp_query_kvm(Error
**errp
)
258 KvmInfo
*info
= g_malloc0(sizeof(*info
));
260 info
->enabled
= kvm_enabled();
261 info
->present
= accel_find("kvm");
266 UuidInfo
*qmp_query_uuid(Error
**errp
)
268 UuidInfo
*info
= g_malloc0(sizeof(*info
));
270 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
274 void qmp_system_reset(Error
**errp
)
276 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
279 void qmp_system_powerdown(Error
**errp
)
281 qemu_system_powerdown_request();
284 void qmp_system_wakeup(Error
**errp
)
286 if (!qemu_wakeup_suspend_enabled()) {
288 "wake-up from suspend is not supported by this guest");
292 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, errp
);
295 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
297 return qmp_memory_device_list();
300 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
302 MemoryInfo
*mem_info
= g_new0(MemoryInfo
, 1);
303 MachineState
*ms
= MACHINE(qdev_get_machine());
305 mem_info
->base_memory
= ms
->ram_size
;
307 mem_info
->plugged_memory
= get_plugged_memory_size();
308 mem_info
->has_plugged_memory
=
309 mem_info
->plugged_memory
!= (uint64_t)-1;
314 HumanReadableText
*qmp_x_query_ramblock(Error
**errp
)
316 g_autoptr(GString
) buf
= ram_block_format();
318 return human_readable_text_from_str(buf
);
321 static int qmp_x_query_irq_foreach(Object
*obj
, void *opaque
)
323 InterruptStatsProvider
*intc
;
324 InterruptStatsProviderClass
*k
;
325 GString
*buf
= opaque
;
327 if (object_dynamic_cast(obj
, TYPE_INTERRUPT_STATS_PROVIDER
)) {
328 intc
= INTERRUPT_STATS_PROVIDER(obj
);
329 k
= INTERRUPT_STATS_PROVIDER_GET_CLASS(obj
);
330 uint64_t *irq_counts
;
331 unsigned int nb_irqs
, i
;
332 if (k
->get_statistics
&&
333 k
->get_statistics(intc
, &irq_counts
, &nb_irqs
)) {
335 g_string_append_printf(buf
, "IRQ statistics for %s:\n",
336 object_get_typename(obj
));
337 for (i
= 0; i
< nb_irqs
; i
++) {
338 if (irq_counts
[i
] > 0) {
339 g_string_append_printf(buf
, "%2d: %" PRId64
"\n", i
,
345 g_string_append_printf(buf
,
346 "IRQ statistics not available for %s.\n",
347 object_get_typename(obj
));
354 HumanReadableText
*qmp_x_query_irq(Error
**errp
)
356 g_autoptr(GString
) buf
= g_string_new("");
358 object_child_foreach_recursive(object_get_root(),
359 qmp_x_query_irq_foreach
, buf
);
361 return human_readable_text_from_str(buf
);
364 static int qmp_x_query_intc_foreach(Object
*obj
, void *opaque
)
366 InterruptStatsProvider
*intc
;
367 InterruptStatsProviderClass
*k
;
368 GString
*buf
= opaque
;
370 if (object_dynamic_cast(obj
, TYPE_INTERRUPT_STATS_PROVIDER
)) {
371 intc
= INTERRUPT_STATS_PROVIDER(obj
);
372 k
= INTERRUPT_STATS_PROVIDER_GET_CLASS(obj
);
374 k
->print_info(intc
, buf
);
376 g_string_append_printf(buf
,
377 "Interrupt controller information not available for %s.\n",
378 object_get_typename(obj
));
385 HumanReadableText
*qmp_x_query_interrupt_controllers(Error
**errp
)
387 g_autoptr(GString
) buf
= g_string_new("");
388 object_child_foreach_recursive(object_get_root(),
389 qmp_x_query_intc_foreach
, buf
);
390 return human_readable_text_from_str(buf
);
393 GuidInfo
*qmp_query_vm_generation_id(Error
**errp
)
397 Object
*obj
= find_vmgenid_dev();
400 error_setg(errp
, "VM Generation ID device not found");
405 info
= g_malloc0(sizeof(*info
));
406 info
->guid
= qemu_uuid_unparse_strdup(&vms
->guid
);