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/boards.h"
12 #include "hw/intc/intc.h"
13 #include "hw/mem/memory-device.h"
14 #include "hw/rdma/rdma.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-builtin-visit.h"
17 #include "qapi/qapi-commands-machine.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qapi/qmp/qobject.h"
20 #include "qapi/qobject-input-visitor.h"
21 #include "qapi/type-helpers.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/uuid.h"
24 #include "qom/qom-qobject.h"
25 #include "sysemu/hostmem.h"
26 #include "sysemu/hw_accel.h"
27 #include "sysemu/numa.h"
28 #include "sysemu/runstate.h"
29 #include "sysemu/sysemu.h"
31 static void cpustate_to_cpuinfo_s390(CpuInfoS390
*info
, const CPUState
*cpu
)
34 S390CPU
*s390_cpu
= S390_CPU(cpu
);
35 CPUS390XState
*env
= &s390_cpu
->env
;
37 info
->cpu_state
= env
->cpu_state
;
44 * fast means: we NEVER interrupt vCPU threads to retrieve
45 * information from KVM.
47 CpuInfoFastList
*qmp_query_cpus_fast(Error
**errp
)
49 MachineState
*ms
= MACHINE(qdev_get_machine());
50 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
51 CpuInfoFastList
*head
= NULL
, **tail
= &head
;
52 SysEmuTarget target
= qapi_enum_parse(&SysEmuTarget_lookup
, TARGET_NAME
,
57 CpuInfoFast
*value
= g_malloc0(sizeof(*value
));
59 value
->cpu_index
= cpu
->cpu_index
;
60 value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
61 value
->thread_id
= cpu
->thread_id
;
63 if (mc
->cpu_index_to_instance_props
) {
64 CpuInstanceProperties
*props
;
65 props
= g_malloc0(sizeof(*props
));
66 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
70 value
->target
= target
;
71 if (target
== SYS_EMU_TARGET_S390X
) {
72 cpustate_to_cpuinfo_s390(&value
->u
.s390x
, cpu
);
75 QAPI_LIST_APPEND(tail
, value
);
81 MachineInfoList
*qmp_query_machines(Error
**errp
)
83 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
84 MachineInfoList
*mach_list
= NULL
;
86 for (el
= machines
; el
; el
= el
->next
) {
87 MachineClass
*mc
= el
->data
;
90 info
= g_malloc0(sizeof(*info
));
92 info
->has_is_default
= true;
93 info
->is_default
= true;
97 info
->alias
= g_strdup(mc
->alias
);
100 info
->name
= g_strdup(mc
->name
);
101 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
102 info
->hotpluggable_cpus
= mc
->has_hotpluggable_cpus
;
103 info
->numa_mem_supported
= mc
->numa_mem_supported
;
104 info
->deprecated
= !!mc
->deprecation_reason
;
105 if (mc
->default_cpu_type
) {
106 info
->default_cpu_type
= g_strdup(mc
->default_cpu_type
);
108 if (mc
->default_ram_id
) {
109 info
->default_ram_id
= g_strdup(mc
->default_ram_id
);
112 QAPI_LIST_PREPEND(mach_list
, info
);
115 g_slist_free(machines
);
119 CurrentMachineParams
*qmp_query_current_machine(Error
**errp
)
121 CurrentMachineParams
*params
= g_malloc0(sizeof(*params
));
122 params
->wakeup_suspend_support
= qemu_wakeup_suspend_enabled();
127 TargetInfo
*qmp_query_target(Error
**errp
)
129 TargetInfo
*info
= g_malloc0(sizeof(*info
));
131 info
->arch
= qapi_enum_parse(&SysEmuTarget_lookup
, TARGET_NAME
, -1,
137 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
139 MachineState
*ms
= MACHINE(qdev_get_machine());
140 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
142 if (!mc
->has_hotpluggable_cpus
) {
143 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
147 return machine_query_hotpluggable_cpus(ms
);
150 void qmp_set_numa_node(NumaOptions
*cmd
, Error
**errp
)
152 if (phase_check(PHASE_MACHINE_INITIALIZED
)) {
153 error_setg(errp
, "The command is permitted only before the machine has been created");
157 set_numa_options(MACHINE(qdev_get_machine()), cmd
, errp
);
160 static int query_memdev(Object
*obj
, void *opaque
)
163 MemdevList
**list
= opaque
;
168 if (object_dynamic_cast(obj
, TYPE_MEMORY_BACKEND
)) {
169 m
= g_malloc0(sizeof(*m
));
171 m
->id
= g_strdup(object_get_canonical_path_component(obj
));
173 m
->size
= object_property_get_uint(obj
, "size", &error_abort
);
174 m
->merge
= object_property_get_bool(obj
, "merge", &error_abort
);
175 m
->dump
= object_property_get_bool(obj
, "dump", &error_abort
);
176 m
->prealloc
= object_property_get_bool(obj
, "prealloc", &error_abort
);
177 m
->share
= object_property_get_bool(obj
, "share", &error_abort
);
178 m
->reserve
= object_property_get_bool(obj
, "reserve", &err
);
180 error_free_or_abort(&err
);
182 m
->has_reserve
= true;
184 m
->policy
= object_property_get_enum(obj
, "policy", "HostMemPolicy",
186 host_nodes
= object_property_get_qobject(obj
,
189 v
= qobject_input_visitor_new(host_nodes
);
190 visit_type_uint16List(v
, NULL
, &m
->host_nodes
, &error_abort
);
192 qobject_unref(host_nodes
);
194 QAPI_LIST_PREPEND(*list
, m
);
200 MemdevList
*qmp_query_memdev(Error
**errp
)
202 Object
*obj
= object_get_objects_root();
203 MemdevList
*list
= NULL
;
205 object_child_foreach(obj
, query_memdev
, &list
);
209 HumanReadableText
*qmp_x_query_numa(Error
**errp
)
211 g_autoptr(GString
) buf
= g_string_new("");
212 int i
, nb_numa_nodes
;
213 NumaNodeMem
*node_mem
;
214 CpuInfoFastList
*cpu_list
, *cpu
;
215 MachineState
*ms
= MACHINE(qdev_get_machine());
217 nb_numa_nodes
= ms
->numa_state
? ms
->numa_state
->num_nodes
: 0;
218 g_string_append_printf(buf
, "%d nodes\n", nb_numa_nodes
);
219 if (!nb_numa_nodes
) {
223 cpu_list
= qmp_query_cpus_fast(&error_abort
);
224 node_mem
= g_new0(NumaNodeMem
, nb_numa_nodes
);
226 query_numa_node_mem(node_mem
, ms
);
227 for (i
= 0; i
< nb_numa_nodes
; i
++) {
228 g_string_append_printf(buf
, "node %d cpus:", i
);
229 for (cpu
= cpu_list
; cpu
; cpu
= cpu
->next
) {
230 if (cpu
->value
->props
&& cpu
->value
->props
->has_node_id
&&
231 cpu
->value
->props
->node_id
== i
) {
232 g_string_append_printf(buf
, " %" PRIi64
, cpu
->value
->cpu_index
);
235 g_string_append_printf(buf
, "\n");
236 g_string_append_printf(buf
, "node %d size: %" PRId64
" MB\n", i
,
237 node_mem
[i
].node_mem
>> 20);
238 g_string_append_printf(buf
, "node %d plugged: %" PRId64
" MB\n", i
,
239 node_mem
[i
].node_plugged_mem
>> 20);
241 qapi_free_CpuInfoFastList(cpu_list
);
245 return human_readable_text_from_str(buf
);
248 KvmInfo
*qmp_query_kvm(Error
**errp
)
250 KvmInfo
*info
= g_malloc0(sizeof(*info
));
252 info
->enabled
= kvm_enabled();
253 info
->present
= accel_find("kvm");
258 UuidInfo
*qmp_query_uuid(Error
**errp
)
260 UuidInfo
*info
= g_malloc0(sizeof(*info
));
262 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
266 void qmp_system_reset(Error
**errp
)
268 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
271 void qmp_system_powerdown(Error
**errp
)
273 qemu_system_powerdown_request();
276 void qmp_system_wakeup(Error
**errp
)
278 if (!qemu_wakeup_suspend_enabled()) {
280 "wake-up from suspend is not supported by this guest");
284 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, errp
);
287 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
289 return qmp_memory_device_list();
292 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
294 MemoryInfo
*mem_info
= g_new0(MemoryInfo
, 1);
295 MachineState
*ms
= MACHINE(qdev_get_machine());
297 mem_info
->base_memory
= ms
->ram_size
;
299 mem_info
->plugged_memory
= get_plugged_memory_size();
300 mem_info
->has_plugged_memory
=
301 mem_info
->plugged_memory
!= (uint64_t)-1;
306 static int qmp_x_query_rdma_foreach(Object
*obj
, void *opaque
)
309 RdmaProviderClass
*k
;
310 GString
*buf
= opaque
;
312 if (object_dynamic_cast(obj
, INTERFACE_RDMA_PROVIDER
)) {
313 rdma
= RDMA_PROVIDER(obj
);
314 k
= RDMA_PROVIDER_GET_CLASS(obj
);
315 if (k
->format_statistics
) {
316 k
->format_statistics(rdma
, buf
);
318 g_string_append_printf(buf
,
319 "RDMA statistics not available for %s.\n",
320 object_get_typename(obj
));
327 HumanReadableText
*qmp_x_query_rdma(Error
**errp
)
329 g_autoptr(GString
) buf
= g_string_new("");
331 object_child_foreach_recursive(object_get_root(),
332 qmp_x_query_rdma_foreach
, buf
);
334 return human_readable_text_from_str(buf
);
337 HumanReadableText
*qmp_x_query_ramblock(Error
**errp
)
339 g_autoptr(GString
) buf
= ram_block_format();
341 return human_readable_text_from_str(buf
);
344 static int qmp_x_query_irq_foreach(Object
*obj
, void *opaque
)
346 InterruptStatsProvider
*intc
;
347 InterruptStatsProviderClass
*k
;
348 GString
*buf
= opaque
;
350 if (object_dynamic_cast(obj
, TYPE_INTERRUPT_STATS_PROVIDER
)) {
351 intc
= INTERRUPT_STATS_PROVIDER(obj
);
352 k
= INTERRUPT_STATS_PROVIDER_GET_CLASS(obj
);
353 uint64_t *irq_counts
;
354 unsigned int nb_irqs
, i
;
355 if (k
->get_statistics
&&
356 k
->get_statistics(intc
, &irq_counts
, &nb_irqs
)) {
358 g_string_append_printf(buf
, "IRQ statistics for %s:\n",
359 object_get_typename(obj
));
360 for (i
= 0; i
< nb_irqs
; i
++) {
361 if (irq_counts
[i
] > 0) {
362 g_string_append_printf(buf
, "%2d: %" PRId64
"\n", i
,
368 g_string_append_printf(buf
,
369 "IRQ statistics not available for %s.\n",
370 object_get_typename(obj
));
377 HumanReadableText
*qmp_x_query_irq(Error
**errp
)
379 g_autoptr(GString
) buf
= g_string_new("");
381 object_child_foreach_recursive(object_get_root(),
382 qmp_x_query_irq_foreach
, buf
);
384 return human_readable_text_from_str(buf
);