stats-hmp-cmds.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * HMP commands related to stats
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2 or
  5. * (at your option) any later version.
  6. */
  7. #include "qemu/osdep.h"
  8. #include "qapi/qapi-commands-stats.h"
  9. #include "monitor/hmp.h"
  10. #include "monitor/monitor.h"
  11. #include "qemu/cutils.h"
  12. #include "hw/core/cpu.h"
  13. #include "qobject/qdict.h"
  14. #include "qapi/error.h"
  15. static void print_stats_schema_value(Monitor *mon, StatsSchemaValue *value)
  16. {
  17. const char *unit = NULL;
  18. monitor_printf(mon, " %s (%s%s", value->name, StatsType_str(value->type),
  19. value->has_unit || value->exponent ? ", " : "");
  20. if (value->has_unit) {
  21. if (value->unit == STATS_UNIT_SECONDS) {
  22. unit = "s";
  23. } else if (value->unit == STATS_UNIT_BYTES) {
  24. unit = "B";
  25. }
  26. }
  27. if (unit && value->base == 10 &&
  28. value->exponent >= -18 && value->exponent <= 18 &&
  29. value->exponent % 3 == 0) {
  30. monitor_puts(mon, si_prefix(value->exponent));
  31. } else if (unit && value->base == 2 &&
  32. value->exponent >= 0 && value->exponent <= 60 &&
  33. value->exponent % 10 == 0) {
  34. monitor_puts(mon, iec_binary_prefix(value->exponent));
  35. } else if (value->exponent) {
  36. /* Use exponential notation and write the unit's English name */
  37. monitor_printf(mon, "* %d^%d%s",
  38. value->base, value->exponent,
  39. value->has_unit ? " " : "");
  40. unit = NULL;
  41. }
  42. if (value->has_unit) {
  43. monitor_puts(mon, unit ? unit : StatsUnit_str(value->unit));
  44. }
  45. /* Print bucket size for linear histograms */
  46. if (value->type == STATS_TYPE_LINEAR_HISTOGRAM && value->has_bucket_size) {
  47. monitor_printf(mon, ", bucket size=%d", value->bucket_size);
  48. }
  49. monitor_printf(mon, ")");
  50. }
  51. static StatsSchemaValueList *find_schema_value_list(
  52. StatsSchemaList *list, StatsProvider provider,
  53. StatsTarget target)
  54. {
  55. StatsSchemaList *node;
  56. for (node = list; node; node = node->next) {
  57. if (node->value->provider == provider &&
  58. node->value->target == target) {
  59. return node->value->stats;
  60. }
  61. }
  62. return NULL;
  63. }
  64. static void print_stats_results(Monitor *mon, StatsTarget target,
  65. bool show_provider,
  66. StatsResult *result,
  67. StatsSchemaList *schema)
  68. {
  69. /* Find provider schema */
  70. StatsSchemaValueList *schema_value_list =
  71. find_schema_value_list(schema, result->provider, target);
  72. StatsList *stats_list;
  73. if (!schema_value_list) {
  74. monitor_printf(mon, "failed to find schema list for %s\n",
  75. StatsProvider_str(result->provider));
  76. return;
  77. }
  78. if (show_provider) {
  79. monitor_printf(mon, "provider: %s\n",
  80. StatsProvider_str(result->provider));
  81. }
  82. for (stats_list = result->stats; stats_list;
  83. stats_list = stats_list->next,
  84. schema_value_list = schema_value_list->next) {
  85. Stats *stats = stats_list->value;
  86. StatsValue *stats_value = stats->value;
  87. StatsSchemaValue *schema_value = schema_value_list->value;
  88. /* Find schema entry */
  89. while (!g_str_equal(stats->name, schema_value->name)) {
  90. if (!schema_value_list->next) {
  91. monitor_printf(mon, "failed to find schema entry for %s\n",
  92. stats->name);
  93. return;
  94. }
  95. schema_value_list = schema_value_list->next;
  96. schema_value = schema_value_list->value;
  97. }
  98. print_stats_schema_value(mon, schema_value);
  99. if (stats_value->type == QTYPE_QNUM) {
  100. monitor_printf(mon, ": %" PRId64 "\n", stats_value->u.scalar);
  101. } else if (stats_value->type == QTYPE_QBOOL) {
  102. monitor_printf(mon, ": %s\n", stats_value->u.boolean ? "yes" : "no");
  103. } else if (stats_value->type == QTYPE_QLIST) {
  104. uint64List *list;
  105. int i;
  106. monitor_printf(mon, ": ");
  107. for (list = stats_value->u.list, i = 1;
  108. list;
  109. list = list->next, i++) {
  110. monitor_printf(mon, "[%d]=%" PRId64 " ", i, list->value);
  111. }
  112. monitor_printf(mon, "\n");
  113. }
  114. }
  115. }
  116. /* Create the StatsFilter that is needed for an "info stats" invocation. */
  117. static StatsFilter *stats_filter(StatsTarget target, const char *names,
  118. int cpu_index, StatsProvider provider)
  119. {
  120. StatsFilter *filter = g_malloc0(sizeof(*filter));
  121. StatsProvider provider_idx;
  122. StatsRequestList *request_list = NULL;
  123. filter->target = target;
  124. switch (target) {
  125. case STATS_TARGET_VM:
  126. break;
  127. case STATS_TARGET_VCPU:
  128. {
  129. strList *vcpu_list = NULL;
  130. CPUState *cpu = qemu_get_cpu(cpu_index);
  131. char *canonical_path = object_get_canonical_path(OBJECT(cpu));
  132. QAPI_LIST_PREPEND(vcpu_list, canonical_path);
  133. filter->u.vcpu.has_vcpus = true;
  134. filter->u.vcpu.vcpus = vcpu_list;
  135. break;
  136. }
  137. case STATS_TARGET_CRYPTODEV:
  138. break;
  139. default:
  140. break;
  141. }
  142. if (!names && provider == STATS_PROVIDER__MAX) {
  143. return filter;
  144. }
  145. /*
  146. * "info stats" can only query either one or all the providers. Querying
  147. * by name, but not by provider, requires the creation of one filter per
  148. * provider.
  149. */
  150. for (provider_idx = 0; provider_idx < STATS_PROVIDER__MAX; provider_idx++) {
  151. if (provider == STATS_PROVIDER__MAX || provider == provider_idx) {
  152. StatsRequest *request = g_new0(StatsRequest, 1);
  153. request->provider = provider_idx;
  154. if (names && !g_str_equal(names, "*")) {
  155. request->has_names = true;
  156. request->names = hmp_split_at_comma(names);
  157. }
  158. QAPI_LIST_PREPEND(request_list, request);
  159. }
  160. }
  161. filter->has_providers = true;
  162. filter->providers = request_list;
  163. return filter;
  164. }
  165. void hmp_info_stats(Monitor *mon, const QDict *qdict)
  166. {
  167. const char *target_str = qdict_get_str(qdict, "target");
  168. const char *provider_str = qdict_get_try_str(qdict, "provider");
  169. const char *names = qdict_get_try_str(qdict, "names");
  170. StatsProvider provider = STATS_PROVIDER__MAX;
  171. StatsTarget target;
  172. Error *err = NULL;
  173. g_autoptr(StatsSchemaList) schema = NULL;
  174. g_autoptr(StatsResultList) stats = NULL;
  175. g_autoptr(StatsFilter) filter = NULL;
  176. StatsResultList *entry;
  177. target = qapi_enum_parse(&StatsTarget_lookup, target_str, -1, &err);
  178. if (err) {
  179. monitor_printf(mon, "invalid stats target %s\n", target_str);
  180. goto exit_no_print;
  181. }
  182. if (provider_str) {
  183. provider = qapi_enum_parse(&StatsProvider_lookup, provider_str, -1, &err);
  184. if (err) {
  185. monitor_printf(mon, "invalid stats provider %s\n", provider_str);
  186. goto exit_no_print;
  187. }
  188. }
  189. schema = qmp_query_stats_schemas(provider_str ? true : false,
  190. provider, &err);
  191. if (err) {
  192. goto exit;
  193. }
  194. switch (target) {
  195. case STATS_TARGET_VM:
  196. filter = stats_filter(target, names, -1, provider);
  197. break;
  198. case STATS_TARGET_VCPU: {}
  199. int cpu_index = monitor_get_cpu_index(mon);
  200. filter = stats_filter(target, names, cpu_index, provider);
  201. break;
  202. case STATS_TARGET_CRYPTODEV:
  203. filter = stats_filter(target, names, -1, provider);
  204. break;
  205. default:
  206. abort();
  207. }
  208. stats = qmp_query_stats(filter, &err);
  209. if (err) {
  210. goto exit;
  211. }
  212. for (entry = stats; entry; entry = entry->next) {
  213. print_stats_results(mon, target, provider_str == NULL, entry->value, schema);
  214. }
  215. exit:
  216. if (err) {
  217. monitor_printf(mon, "%s\n", error_get_pretty(err));
  218. }
  219. exit_no_print:
  220. error_free(err);
  221. }