xref: /qemu/hw/core/machine-qmp-cmds.c (revision e4418354)
1 /*
2  * QMP commands related to machines and CPUs
3  *
4  * Copyright (C) 2014 Red Hat Inc
5  *
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.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/boards.h"
12 #include "qapi/error.h"
13 #include "qapi/qapi-builtin-visit.h"
14 #include "qapi/qapi-commands-machine.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qapi/qmp/qobject.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/type-helpers.h"
19 #include "qemu/main-loop.h"
20 #include "qom/qom-qobject.h"
21 #include "sysemu/hostmem.h"
22 #include "sysemu/hw_accel.h"
23 #include "sysemu/numa.h"
24 #include "sysemu/runstate.h"
25 
26 static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
27 {
28 #ifdef TARGET_S390X
29     S390CPU *s390_cpu = S390_CPU(cpu);
30     CPUS390XState *env = &s390_cpu->env;
31 
32     info->cpu_state = env->cpu_state;
33 #else
34     abort();
35 #endif
36 }
37 
38 /*
39  * fast means: we NEVER interrupt vCPU threads to retrieve
40  * information from KVM.
41  */
42 CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
43 {
44     MachineState *ms = MACHINE(qdev_get_machine());
45     MachineClass *mc = MACHINE_GET_CLASS(ms);
46     CpuInfoFastList *head = NULL, **tail = &head;
47     SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
48                                           -1, &error_abort);
49     CPUState *cpu;
50 
51     CPU_FOREACH(cpu) {
52         CpuInfoFast *value = g_malloc0(sizeof(*value));
53 
54         value->cpu_index = cpu->cpu_index;
55         value->qom_path = object_get_canonical_path(OBJECT(cpu));
56         value->thread_id = cpu->thread_id;
57 
58         if (mc->cpu_index_to_instance_props) {
59             CpuInstanceProperties *props;
60             props = g_malloc0(sizeof(*props));
61             *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
62             value->props = props;
63         }
64 
65         value->target = target;
66         if (target == SYS_EMU_TARGET_S390X) {
67             cpustate_to_cpuinfo_s390(&value->u.s390x, cpu);
68         }
69 
70         QAPI_LIST_APPEND(tail, value);
71     }
72 
73     return head;
74 }
75 
76 MachineInfoList *qmp_query_machines(Error **errp)
77 {
78     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
79     MachineInfoList *mach_list = NULL;
80 
81     for (el = machines; el; el = el->next) {
82         MachineClass *mc = el->data;
83         MachineInfo *info;
84 
85         info = g_malloc0(sizeof(*info));
86         if (mc->is_default) {
87             info->has_is_default = true;
88             info->is_default = true;
89         }
90 
91         if (mc->alias) {
92             info->alias = g_strdup(mc->alias);
93         }
94 
95         info->name = g_strdup(mc->name);
96         info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
97         info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
98         info->numa_mem_supported = mc->numa_mem_supported;
99         info->deprecated = !!mc->deprecation_reason;
100         if (mc->default_cpu_type) {
101             info->default_cpu_type = g_strdup(mc->default_cpu_type);
102         }
103         if (mc->default_ram_id) {
104             info->default_ram_id = g_strdup(mc->default_ram_id);
105         }
106 
107         QAPI_LIST_PREPEND(mach_list, info);
108     }
109 
110     g_slist_free(machines);
111     return mach_list;
112 }
113 
114 CurrentMachineParams *qmp_query_current_machine(Error **errp)
115 {
116     CurrentMachineParams *params = g_malloc0(sizeof(*params));
117     params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
118 
119     return params;
120 }
121 
122 TargetInfo *qmp_query_target(Error **errp)
123 {
124     TargetInfo *info = g_malloc0(sizeof(*info));
125 
126     info->arch = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME, -1,
127                                  &error_abort);
128 
129     return info;
130 }
131 
132 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
133 {
134     MachineState *ms = MACHINE(qdev_get_machine());
135     MachineClass *mc = MACHINE_GET_CLASS(ms);
136 
137     if (!mc->has_hotpluggable_cpus) {
138         error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
139         return NULL;
140     }
141 
142     return machine_query_hotpluggable_cpus(ms);
143 }
144 
145 void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
146 {
147     if (phase_check(PHASE_MACHINE_INITIALIZED)) {
148         error_setg(errp, "The command is permitted only before the machine has been created");
149         return;
150     }
151 
152     set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
153 }
154 
155 static int query_memdev(Object *obj, void *opaque)
156 {
157     Error *err = NULL;
158     MemdevList **list = opaque;
159     Memdev *m;
160     QObject *host_nodes;
161     Visitor *v;
162 
163     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
164         m = g_malloc0(sizeof(*m));
165 
166         m->id = g_strdup(object_get_canonical_path_component(obj));
167 
168         m->size = object_property_get_uint(obj, "size", &error_abort);
169         m->merge = object_property_get_bool(obj, "merge", &error_abort);
170         m->dump = object_property_get_bool(obj, "dump", &error_abort);
171         m->prealloc = object_property_get_bool(obj, "prealloc", &error_abort);
172         m->share = object_property_get_bool(obj, "share", &error_abort);
173         m->reserve = object_property_get_bool(obj, "reserve", &err);
174         if (err) {
175             error_free_or_abort(&err);
176         } else {
177             m->has_reserve = true;
178         }
179         m->policy = object_property_get_enum(obj, "policy", "HostMemPolicy",
180                                              &error_abort);
181         host_nodes = object_property_get_qobject(obj,
182                                                  "host-nodes",
183                                                  &error_abort);
184         v = qobject_input_visitor_new(host_nodes);
185         visit_type_uint16List(v, NULL, &m->host_nodes, &error_abort);
186         visit_free(v);
187         qobject_unref(host_nodes);
188 
189         QAPI_LIST_PREPEND(*list, m);
190     }
191 
192     return 0;
193 }
194 
195 MemdevList *qmp_query_memdev(Error **errp)
196 {
197     Object *obj = object_get_objects_root();
198     MemdevList *list = NULL;
199 
200     object_child_foreach(obj, query_memdev, &list);
201     return list;
202 }
203 
204 HumanReadableText *qmp_x_query_numa(Error **errp)
205 {
206     g_autoptr(GString) buf = g_string_new("");
207     int i, nb_numa_nodes;
208     NumaNodeMem *node_mem;
209     CpuInfoFastList *cpu_list, *cpu;
210     MachineState *ms = MACHINE(qdev_get_machine());
211 
212     nb_numa_nodes = ms->numa_state ? ms->numa_state->num_nodes : 0;
213     g_string_append_printf(buf, "%d nodes\n", nb_numa_nodes);
214     if (!nb_numa_nodes) {
215         goto done;
216     }
217 
218     cpu_list = qmp_query_cpus_fast(&error_abort);
219     node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
220 
221     query_numa_node_mem(node_mem, ms);
222     for (i = 0; i < nb_numa_nodes; i++) {
223         g_string_append_printf(buf, "node %d cpus:", i);
224         for (cpu = cpu_list; cpu; cpu = cpu->next) {
225             if (cpu->value->props && cpu->value->props->has_node_id &&
226                 cpu->value->props->node_id == i) {
227                 g_string_append_printf(buf, " %" PRIi64, cpu->value->cpu_index);
228             }
229         }
230         g_string_append_printf(buf, "\n");
231         g_string_append_printf(buf, "node %d size: %" PRId64 " MB\n", i,
232                                node_mem[i].node_mem >> 20);
233         g_string_append_printf(buf, "node %d plugged: %" PRId64 " MB\n", i,
234                                node_mem[i].node_plugged_mem >> 20);
235     }
236     qapi_free_CpuInfoFastList(cpu_list);
237     g_free(node_mem);
238 
239  done:
240     return human_readable_text_from_str(buf);
241 }
242