kvm-stat.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "../../util/kvm-stat.h"
  2. #include <asm/svm.h>
  3. #include <asm/vmx.h>
  4. #include <asm/kvm.h>
  5. define_exit_reasons_table(vmx_exit_reasons, VMX_EXIT_REASONS);
  6. define_exit_reasons_table(svm_exit_reasons, SVM_EXIT_REASONS);
  7. static struct kvm_events_ops exit_events = {
  8. .is_begin_event = exit_event_begin,
  9. .is_end_event = exit_event_end,
  10. .decode_key = exit_event_decode_key,
  11. .name = "VM-EXIT"
  12. };
  13. const char *vcpu_id_str = "vcpu_id";
  14. const int decode_str_len = 20;
  15. const char *kvm_exit_reason = "exit_reason";
  16. const char *kvm_entry_trace = "kvm:kvm_entry";
  17. const char *kvm_exit_trace = "kvm:kvm_exit";
  18. /*
  19. * For the mmio events, we treat:
  20. * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
  21. * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
  22. */
  23. static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
  24. struct event_key *key)
  25. {
  26. key->key = perf_evsel__intval(evsel, sample, "gpa");
  27. key->info = perf_evsel__intval(evsel, sample, "type");
  28. }
  29. #define KVM_TRACE_MMIO_READ_UNSATISFIED 0
  30. #define KVM_TRACE_MMIO_READ 1
  31. #define KVM_TRACE_MMIO_WRITE 2
  32. static bool mmio_event_begin(struct perf_evsel *evsel,
  33. struct perf_sample *sample, struct event_key *key)
  34. {
  35. /* MMIO read begin event in kernel. */
  36. if (kvm_exit_event(evsel))
  37. return true;
  38. /* MMIO write begin event in kernel. */
  39. if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
  40. perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
  41. mmio_event_get_key(evsel, sample, key);
  42. return true;
  43. }
  44. return false;
  45. }
  46. static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
  47. struct event_key *key)
  48. {
  49. /* MMIO write end event in kernel. */
  50. if (kvm_entry_event(evsel))
  51. return true;
  52. /* MMIO read end event in kernel.*/
  53. if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
  54. perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
  55. mmio_event_get_key(evsel, sample, key);
  56. return true;
  57. }
  58. return false;
  59. }
  60. static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
  61. struct event_key *key,
  62. char *decode)
  63. {
  64. scnprintf(decode, decode_str_len, "%#lx:%s",
  65. (unsigned long)key->key,
  66. key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
  67. }
  68. static struct kvm_events_ops mmio_events = {
  69. .is_begin_event = mmio_event_begin,
  70. .is_end_event = mmio_event_end,
  71. .decode_key = mmio_event_decode_key,
  72. .name = "MMIO Access"
  73. };
  74. /* The time of emulation pio access is from kvm_pio to kvm_entry. */
  75. static void ioport_event_get_key(struct perf_evsel *evsel,
  76. struct perf_sample *sample,
  77. struct event_key *key)
  78. {
  79. key->key = perf_evsel__intval(evsel, sample, "port");
  80. key->info = perf_evsel__intval(evsel, sample, "rw");
  81. }
  82. static bool ioport_event_begin(struct perf_evsel *evsel,
  83. struct perf_sample *sample,
  84. struct event_key *key)
  85. {
  86. if (!strcmp(evsel->name, "kvm:kvm_pio")) {
  87. ioport_event_get_key(evsel, sample, key);
  88. return true;
  89. }
  90. return false;
  91. }
  92. static bool ioport_event_end(struct perf_evsel *evsel,
  93. struct perf_sample *sample __maybe_unused,
  94. struct event_key *key __maybe_unused)
  95. {
  96. return kvm_entry_event(evsel);
  97. }
  98. static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
  99. struct event_key *key,
  100. char *decode)
  101. {
  102. scnprintf(decode, decode_str_len, "%#llx:%s",
  103. (unsigned long long)key->key,
  104. key->info ? "POUT" : "PIN");
  105. }
  106. static struct kvm_events_ops ioport_events = {
  107. .is_begin_event = ioport_event_begin,
  108. .is_end_event = ioport_event_end,
  109. .decode_key = ioport_event_decode_key,
  110. .name = "IO Port Access"
  111. };
  112. const char *kvm_events_tp[] = {
  113. "kvm:kvm_entry",
  114. "kvm:kvm_exit",
  115. "kvm:kvm_mmio",
  116. "kvm:kvm_pio",
  117. NULL,
  118. };
  119. struct kvm_reg_events_ops kvm_reg_events_ops[] = {
  120. { .name = "vmexit", .ops = &exit_events },
  121. { .name = "mmio", .ops = &mmio_events },
  122. { .name = "ioport", .ops = &ioport_events },
  123. { NULL, NULL },
  124. };
  125. const char * const kvm_skip_events[] = {
  126. "HLT",
  127. NULL,
  128. };
  129. int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid)
  130. {
  131. if (strstr(cpuid, "Intel")) {
  132. kvm->exit_reasons = vmx_exit_reasons;
  133. kvm->exit_reasons_isa = "VMX";
  134. } else if (strstr(cpuid, "AMD")) {
  135. kvm->exit_reasons = svm_exit_reasons;
  136. kvm->exit_reasons_isa = "SVM";
  137. } else
  138. return -ENOTSUP;
  139. return 0;
  140. }