1 /* 2 * Human Monitor Interface commands 3 * 4 * Copyright IBM, Corp. 2011 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Contributions after 2012-01-13 are licensed under the terms of the 13 * GNU GPL, version 2 or (at your option) any later version. 14 */ 15 16 #include "qemu/osdep.h" 17 #include "monitor/hmp.h" 18 #include "qemu/help_option.h" 19 #include "monitor/monitor.h" 20 #include "qapi/error.h" 21 #include "qapi/qapi-commands-control.h" 22 #include "qapi/qapi-commands-misc.h" 23 #include "qapi/qapi-commands-stats.h" 24 #include "qapi/qmp/qdict.h" 25 #include "qapi/qmp/qerror.h" 26 #include "qemu/cutils.h" 27 #include "hw/core/cpu.h" 28 #include "hw/intc/intc.h" 29 30 bool hmp_handle_error(Monitor *mon, Error *err) 31 { 32 if (err) { 33 error_reportf_err(err, "Error: "); 34 return true; 35 } 36 return false; 37 } 38 39 /* 40 * Split @str at comma. 41 * A null @str defaults to "". 42 */ 43 strList *hmp_split_at_comma(const char *str) 44 { 45 char **split = g_strsplit(str ?: "", ",", -1); 46 strList *res = NULL; 47 strList **tail = &res; 48 int i; 49 50 for (i = 0; split[i]; i++) { 51 QAPI_LIST_APPEND(tail, split[i]); 52 } 53 54 g_free(split); 55 return res; 56 } 57 58 void hmp_info_name(Monitor *mon, const QDict *qdict) 59 { 60 NameInfo *info; 61 62 info = qmp_query_name(NULL); 63 if (info->name) { 64 monitor_printf(mon, "%s\n", info->name); 65 } 66 qapi_free_NameInfo(info); 67 } 68 69 void hmp_info_version(Monitor *mon, const QDict *qdict) 70 { 71 VersionInfo *info; 72 73 info = qmp_query_version(NULL); 74 75 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n", 76 info->qemu->major, info->qemu->minor, info->qemu->micro, 77 info->package); 78 79 qapi_free_VersionInfo(info); 80 } 81 82 static int hmp_info_pic_foreach(Object *obj, void *opaque) 83 { 84 InterruptStatsProvider *intc; 85 InterruptStatsProviderClass *k; 86 Monitor *mon = opaque; 87 88 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) { 89 intc = INTERRUPT_STATS_PROVIDER(obj); 90 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj); 91 if (k->print_info) { 92 k->print_info(intc, mon); 93 } else { 94 monitor_printf(mon, "Interrupt controller information not available for %s.\n", 95 object_get_typename(obj)); 96 } 97 } 98 99 return 0; 100 } 101 102 void hmp_info_pic(Monitor *mon, const QDict *qdict) 103 { 104 object_child_foreach_recursive(object_get_root(), 105 hmp_info_pic_foreach, mon); 106 } 107 108 void hmp_quit(Monitor *mon, const QDict *qdict) 109 { 110 monitor_suspend(mon); 111 qmp_quit(NULL); 112 } 113 114 void hmp_stop(Monitor *mon, const QDict *qdict) 115 { 116 qmp_stop(NULL); 117 } 118 119 void hmp_sync_profile(Monitor *mon, const QDict *qdict) 120 { 121 const char *op = qdict_get_try_str(qdict, "op"); 122 123 if (op == NULL) { 124 bool on = qsp_is_enabled(); 125 126 monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off"); 127 return; 128 } 129 if (!strcmp(op, "on")) { 130 qsp_enable(); 131 } else if (!strcmp(op, "off")) { 132 qsp_disable(); 133 } else if (!strcmp(op, "reset")) { 134 qsp_reset(); 135 } else { 136 Error *err = NULL; 137 138 error_setg(&err, QERR_INVALID_PARAMETER, op); 139 hmp_handle_error(mon, err); 140 } 141 } 142 143 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict) 144 { 145 Error *err = NULL; 146 147 qmp_x_exit_preconfig(&err); 148 hmp_handle_error(mon, err); 149 } 150 151 void hmp_cpu(Monitor *mon, const QDict *qdict) 152 { 153 int64_t cpu_index; 154 155 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that 156 use it are converted to the QAPI */ 157 cpu_index = qdict_get_int(qdict, "index"); 158 if (monitor_set_cpu(mon, cpu_index) < 0) { 159 monitor_printf(mon, "invalid CPU index\n"); 160 } 161 } 162 163 void hmp_cont(Monitor *mon, const QDict *qdict) 164 { 165 Error *err = NULL; 166 167 qmp_cont(&err); 168 hmp_handle_error(mon, err); 169 } 170 171 void hmp_change(Monitor *mon, const QDict *qdict) 172 { 173 const char *device = qdict_get_str(qdict, "device"); 174 const char *target = qdict_get_str(qdict, "target"); 175 const char *arg = qdict_get_try_str(qdict, "arg"); 176 const char *read_only = qdict_get_try_str(qdict, "read-only-mode"); 177 bool force = qdict_get_try_bool(qdict, "force", false); 178 Error *err = NULL; 179 180 #ifdef CONFIG_VNC 181 if (strcmp(device, "vnc") == 0) { 182 hmp_change_vnc(mon, device, target, arg, read_only, force, &err); 183 } else 184 #endif 185 { 186 hmp_change_medium(mon, device, target, arg, read_only, force, &err); 187 } 188 189 hmp_handle_error(mon, err); 190 } 191 192 void hmp_getfd(Monitor *mon, const QDict *qdict) 193 { 194 const char *fdname = qdict_get_str(qdict, "fdname"); 195 Error *err = NULL; 196 197 qmp_getfd(fdname, &err); 198 hmp_handle_error(mon, err); 199 } 200 201 void hmp_closefd(Monitor *mon, const QDict *qdict) 202 { 203 const char *fdname = qdict_get_str(qdict, "fdname"); 204 Error *err = NULL; 205 206 qmp_closefd(fdname, &err); 207 hmp_handle_error(mon, err); 208 } 209 210 void hmp_info_iothreads(Monitor *mon, const QDict *qdict) 211 { 212 IOThreadInfoList *info_list = qmp_query_iothreads(NULL); 213 IOThreadInfoList *info; 214 IOThreadInfo *value; 215 216 for (info = info_list; info; info = info->next) { 217 value = info->value; 218 monitor_printf(mon, "%s:\n", value->id); 219 monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id); 220 monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns); 221 monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow); 222 monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink); 223 monitor_printf(mon, " aio-max-batch=%" PRId64 "\n", 224 value->aio_max_batch); 225 } 226 227 qapi_free_IOThreadInfoList(info_list); 228 } 229 230 static void print_stats_schema_value(Monitor *mon, StatsSchemaValue *value) 231 { 232 const char *unit = NULL; 233 monitor_printf(mon, " %s (%s%s", value->name, StatsType_str(value->type), 234 value->has_unit || value->exponent ? ", " : ""); 235 236 if (value->has_unit) { 237 if (value->unit == STATS_UNIT_SECONDS) { 238 unit = "s"; 239 } else if (value->unit == STATS_UNIT_BYTES) { 240 unit = "B"; 241 } 242 } 243 244 if (unit && value->base == 10 && 245 value->exponent >= -18 && value->exponent <= 18 && 246 value->exponent % 3 == 0) { 247 monitor_puts(mon, si_prefix(value->exponent)); 248 } else if (unit && value->base == 2 && 249 value->exponent >= 0 && value->exponent <= 60 && 250 value->exponent % 10 == 0) { 251 252 monitor_puts(mon, iec_binary_prefix(value->exponent)); 253 } else if (value->exponent) { 254 /* Use exponential notation and write the unit's English name */ 255 monitor_printf(mon, "* %d^%d%s", 256 value->base, value->exponent, 257 value->has_unit ? " " : ""); 258 unit = NULL; 259 } 260 261 if (value->has_unit) { 262 monitor_puts(mon, unit ? unit : StatsUnit_str(value->unit)); 263 } 264 265 /* Print bucket size for linear histograms */ 266 if (value->type == STATS_TYPE_LINEAR_HISTOGRAM && value->has_bucket_size) { 267 monitor_printf(mon, ", bucket size=%d", value->bucket_size); 268 } 269 monitor_printf(mon, ")"); 270 } 271 272 static StatsSchemaValueList *find_schema_value_list( 273 StatsSchemaList *list, StatsProvider provider, 274 StatsTarget target) 275 { 276 StatsSchemaList *node; 277 278 for (node = list; node; node = node->next) { 279 if (node->value->provider == provider && 280 node->value->target == target) { 281 return node->value->stats; 282 } 283 } 284 return NULL; 285 } 286 287 static void print_stats_results(Monitor *mon, StatsTarget target, 288 bool show_provider, 289 StatsResult *result, 290 StatsSchemaList *schema) 291 { 292 /* Find provider schema */ 293 StatsSchemaValueList *schema_value_list = 294 find_schema_value_list(schema, result->provider, target); 295 StatsList *stats_list; 296 297 if (!schema_value_list) { 298 monitor_printf(mon, "failed to find schema list for %s\n", 299 StatsProvider_str(result->provider)); 300 return; 301 } 302 303 if (show_provider) { 304 monitor_printf(mon, "provider: %s\n", 305 StatsProvider_str(result->provider)); 306 } 307 308 for (stats_list = result->stats; stats_list; 309 stats_list = stats_list->next, 310 schema_value_list = schema_value_list->next) { 311 312 Stats *stats = stats_list->value; 313 StatsValue *stats_value = stats->value; 314 StatsSchemaValue *schema_value = schema_value_list->value; 315 316 /* Find schema entry */ 317 while (!g_str_equal(stats->name, schema_value->name)) { 318 if (!schema_value_list->next) { 319 monitor_printf(mon, "failed to find schema entry for %s\n", 320 stats->name); 321 return; 322 } 323 schema_value_list = schema_value_list->next; 324 schema_value = schema_value_list->value; 325 } 326 327 print_stats_schema_value(mon, schema_value); 328 329 if (stats_value->type == QTYPE_QNUM) { 330 monitor_printf(mon, ": %" PRId64 "\n", stats_value->u.scalar); 331 } else if (stats_value->type == QTYPE_QBOOL) { 332 monitor_printf(mon, ": %s\n", stats_value->u.boolean ? "yes" : "no"); 333 } else if (stats_value->type == QTYPE_QLIST) { 334 uint64List *list; 335 int i; 336 337 monitor_printf(mon, ": "); 338 for (list = stats_value->u.list, i = 1; 339 list; 340 list = list->next, i++) { 341 monitor_printf(mon, "[%d]=%" PRId64 " ", i, list->value); 342 } 343 monitor_printf(mon, "\n"); 344 } 345 } 346 } 347 348 /* Create the StatsFilter that is needed for an "info stats" invocation. */ 349 static StatsFilter *stats_filter(StatsTarget target, const char *names, 350 int cpu_index, StatsProvider provider) 351 { 352 StatsFilter *filter = g_malloc0(sizeof(*filter)); 353 StatsProvider provider_idx; 354 StatsRequestList *request_list = NULL; 355 356 filter->target = target; 357 switch (target) { 358 case STATS_TARGET_VM: 359 break; 360 case STATS_TARGET_VCPU: 361 { 362 strList *vcpu_list = NULL; 363 CPUState *cpu = qemu_get_cpu(cpu_index); 364 char *canonical_path = object_get_canonical_path(OBJECT(cpu)); 365 366 QAPI_LIST_PREPEND(vcpu_list, canonical_path); 367 filter->u.vcpu.has_vcpus = true; 368 filter->u.vcpu.vcpus = vcpu_list; 369 break; 370 } 371 default: 372 break; 373 } 374 375 if (!names && provider == STATS_PROVIDER__MAX) { 376 return filter; 377 } 378 379 /* 380 * "info stats" can only query either one or all the providers. Querying 381 * by name, but not by provider, requires the creation of one filter per 382 * provider. 383 */ 384 for (provider_idx = 0; provider_idx < STATS_PROVIDER__MAX; provider_idx++) { 385 if (provider == STATS_PROVIDER__MAX || provider == provider_idx) { 386 StatsRequest *request = g_new0(StatsRequest, 1); 387 request->provider = provider_idx; 388 if (names && !g_str_equal(names, "*")) { 389 request->has_names = true; 390 request->names = hmp_split_at_comma(names); 391 } 392 QAPI_LIST_PREPEND(request_list, request); 393 } 394 } 395 396 filter->has_providers = true; 397 filter->providers = request_list; 398 return filter; 399 } 400 401 void hmp_info_stats(Monitor *mon, const QDict *qdict) 402 { 403 const char *target_str = qdict_get_str(qdict, "target"); 404 const char *provider_str = qdict_get_try_str(qdict, "provider"); 405 const char *names = qdict_get_try_str(qdict, "names"); 406 407 StatsProvider provider = STATS_PROVIDER__MAX; 408 StatsTarget target; 409 Error *err = NULL; 410 g_autoptr(StatsSchemaList) schema = NULL; 411 g_autoptr(StatsResultList) stats = NULL; 412 g_autoptr(StatsFilter) filter = NULL; 413 StatsResultList *entry; 414 415 target = qapi_enum_parse(&StatsTarget_lookup, target_str, -1, &err); 416 if (err) { 417 monitor_printf(mon, "invalid stats target %s\n", target_str); 418 goto exit_no_print; 419 } 420 if (provider_str) { 421 provider = qapi_enum_parse(&StatsProvider_lookup, provider_str, -1, &err); 422 if (err) { 423 monitor_printf(mon, "invalid stats provider %s\n", provider_str); 424 goto exit_no_print; 425 } 426 } 427 428 schema = qmp_query_stats_schemas(provider_str ? true : false, 429 provider, &err); 430 if (err) { 431 goto exit; 432 } 433 434 switch (target) { 435 case STATS_TARGET_VM: 436 filter = stats_filter(target, names, -1, provider); 437 break; 438 case STATS_TARGET_VCPU: {} 439 int cpu_index = monitor_get_cpu_index(mon); 440 filter = stats_filter(target, names, cpu_index, provider); 441 break; 442 default: 443 abort(); 444 } 445 446 stats = qmp_query_stats(filter, &err); 447 if (err) { 448 goto exit; 449 } 450 for (entry = stats; entry; entry = entry->next) { 451 print_stats_results(mon, target, provider_str == NULL, entry->value, schema); 452 } 453 454 exit: 455 if (err) { 456 monitor_printf(mon, "%s\n", error_get_pretty(err)); 457 } 458 exit_no_print: 459 error_free(err); 460 } 461