kvm-stat.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include "util/kvm-stat.h"
  4. #include "util/parse-events.h"
  5. #include "util/debug.h"
  6. #include "book3s_hv_exits.h"
  7. #include "book3s_hcalls.h"
  8. #define NR_TPS 4
  9. const char *vcpu_id_str = "vcpu_id";
  10. const int decode_str_len = 40;
  11. const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
  12. const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";
  13. define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
  14. define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
  15. /* Tracepoints specific to ppc_book3s_hv */
  16. const char *ppc_book3s_hv_kvm_tp[] = {
  17. "kvm_hv:kvm_guest_enter",
  18. "kvm_hv:kvm_guest_exit",
  19. "kvm_hv:kvm_hcall_enter",
  20. "kvm_hv:kvm_hcall_exit",
  21. NULL,
  22. };
  23. /* 1 extra placeholder for NULL */
  24. const char *kvm_events_tp[NR_TPS + 1];
  25. const char *kvm_exit_reason;
  26. static void hcall_event_get_key(struct perf_evsel *evsel,
  27. struct perf_sample *sample,
  28. struct event_key *key)
  29. {
  30. key->info = 0;
  31. key->key = perf_evsel__intval(evsel, sample, "req");
  32. }
  33. static const char *get_hcall_exit_reason(u64 exit_code)
  34. {
  35. struct exit_reasons_table *tbl = hcall_reasons;
  36. while (tbl->reason != NULL) {
  37. if (tbl->exit_code == exit_code)
  38. return tbl->reason;
  39. tbl++;
  40. }
  41. pr_debug("Unknown hcall code: %lld\n",
  42. (unsigned long long)exit_code);
  43. return "UNKNOWN";
  44. }
  45. static bool hcall_event_end(struct perf_evsel *evsel,
  46. struct perf_sample *sample __maybe_unused,
  47. struct event_key *key __maybe_unused)
  48. {
  49. return (!strcmp(evsel->name, kvm_events_tp[3]));
  50. }
  51. static bool hcall_event_begin(struct perf_evsel *evsel,
  52. struct perf_sample *sample, struct event_key *key)
  53. {
  54. if (!strcmp(evsel->name, kvm_events_tp[2])) {
  55. hcall_event_get_key(evsel, sample, key);
  56. return true;
  57. }
  58. return false;
  59. }
  60. static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
  61. struct event_key *key,
  62. char *decode)
  63. {
  64. const char *hcall_reason = get_hcall_exit_reason(key->key);
  65. scnprintf(decode, decode_str_len, "%s", hcall_reason);
  66. }
  67. static struct kvm_events_ops hcall_events = {
  68. .is_begin_event = hcall_event_begin,
  69. .is_end_event = hcall_event_end,
  70. .decode_key = hcall_event_decode_key,
  71. .name = "HCALL-EVENT",
  72. };
  73. static struct kvm_events_ops exit_events = {
  74. .is_begin_event = exit_event_begin,
  75. .is_end_event = exit_event_end,
  76. .decode_key = exit_event_decode_key,
  77. .name = "VM-EXIT"
  78. };
  79. struct kvm_reg_events_ops kvm_reg_events_ops[] = {
  80. { .name = "vmexit", .ops = &exit_events },
  81. { .name = "hcall", .ops = &hcall_events },
  82. { NULL, NULL },
  83. };
  84. const char * const kvm_skip_events[] = {
  85. NULL,
  86. };
  87. static int is_tracepoint_available(const char *str, struct perf_evlist *evlist)
  88. {
  89. struct parse_events_error err;
  90. int ret;
  91. err.str = NULL;
  92. ret = parse_events(evlist, str, &err);
  93. if (err.str)
  94. pr_err("%s : %s\n", str, err.str);
  95. return ret;
  96. }
  97. static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
  98. struct perf_evlist *evlist)
  99. {
  100. const char **events_ptr;
  101. int i, nr_tp = 0, err = -1;
  102. /* Check for book3s_hv tracepoints */
  103. for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {
  104. err = is_tracepoint_available(*events_ptr, evlist);
  105. if (err)
  106. return -1;
  107. nr_tp++;
  108. }
  109. for (i = 0; i < nr_tp; i++)
  110. kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];
  111. kvm_events_tp[i] = NULL;
  112. kvm_exit_reason = "trap";
  113. kvm->exit_reasons = hv_exit_reasons;
  114. kvm->exit_reasons_isa = "HV";
  115. return 0;
  116. }
  117. /* Wrapper to setup kvm tracepoints */
  118. static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
  119. {
  120. struct perf_evlist *evlist = perf_evlist__new();
  121. if (evlist == NULL)
  122. return -ENOMEM;
  123. /* Right now, only supported on book3s_hv */
  124. return ppc__setup_book3s_hv(kvm, evlist);
  125. }
  126. int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
  127. {
  128. return ppc__setup_kvm_tp(kvm);
  129. }
  130. int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)
  131. {
  132. int ret;
  133. ret = ppc__setup_kvm_tp(kvm);
  134. if (ret) {
  135. kvm->exit_reasons = NULL;
  136. kvm->exit_reasons_isa = NULL;
  137. }
  138. return ret;
  139. }