builtin-kvm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/evsel.h"
  4. #include "util/util.h"
  5. #include "util/cache.h"
  6. #include "util/symbol.h"
  7. #include "util/thread.h"
  8. #include "util/header.h"
  9. #include "util/session.h"
  10. #include "util/parse-options.h"
  11. #include "util/trace-event.h"
  12. #include "util/debug.h"
  13. #include <lk/debugfs.h>
  14. #include "util/tool.h"
  15. #include "util/stat.h"
  16. #include <sys/prctl.h>
  17. #include <semaphore.h>
  18. #include <pthread.h>
  19. #include <math.h>
  20. #if defined(__i386__) || defined(__x86_64__)
  21. #include <asm/svm.h>
  22. #include <asm/vmx.h>
  23. #include <asm/kvm.h>
  24. struct event_key {
  25. #define INVALID_KEY (~0ULL)
  26. u64 key;
  27. int info;
  28. };
  29. struct kvm_event_stats {
  30. u64 time;
  31. struct stats stats;
  32. };
  33. struct kvm_event {
  34. struct list_head hash_entry;
  35. struct rb_node rb;
  36. struct event_key key;
  37. struct kvm_event_stats total;
  38. #define DEFAULT_VCPU_NUM 8
  39. int max_vcpu;
  40. struct kvm_event_stats *vcpu;
  41. };
  42. typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);
  43. struct kvm_event_key {
  44. const char *name;
  45. key_cmp_fun key;
  46. };
  47. struct perf_kvm_stat;
  48. struct kvm_events_ops {
  49. bool (*is_begin_event)(struct perf_evsel *evsel,
  50. struct perf_sample *sample,
  51. struct event_key *key);
  52. bool (*is_end_event)(struct perf_evsel *evsel,
  53. struct perf_sample *sample, struct event_key *key);
  54. void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
  55. char decode[20]);
  56. const char *name;
  57. };
  58. struct exit_reasons_table {
  59. unsigned long exit_code;
  60. const char *reason;
  61. };
  62. #define EVENTS_BITS 12
  63. #define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
  64. struct perf_kvm_stat {
  65. struct perf_tool tool;
  66. struct perf_session *session;
  67. const char *file_name;
  68. const char *report_event;
  69. const char *sort_key;
  70. int trace_vcpu;
  71. struct exit_reasons_table *exit_reasons;
  72. int exit_reasons_size;
  73. const char *exit_reasons_isa;
  74. struct kvm_events_ops *events_ops;
  75. key_cmp_fun compare;
  76. struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
  77. u64 total_time;
  78. u64 total_count;
  79. struct rb_root result;
  80. };
  81. static void exit_event_get_key(struct perf_evsel *evsel,
  82. struct perf_sample *sample,
  83. struct event_key *key)
  84. {
  85. key->info = 0;
  86. key->key = perf_evsel__intval(evsel, sample, "exit_reason");
  87. }
  88. static bool kvm_exit_event(struct perf_evsel *evsel)
  89. {
  90. return !strcmp(evsel->name, "kvm:kvm_exit");
  91. }
  92. static bool exit_event_begin(struct perf_evsel *evsel,
  93. struct perf_sample *sample, struct event_key *key)
  94. {
  95. if (kvm_exit_event(evsel)) {
  96. exit_event_get_key(evsel, sample, key);
  97. return true;
  98. }
  99. return false;
  100. }
  101. static bool kvm_entry_event(struct perf_evsel *evsel)
  102. {
  103. return !strcmp(evsel->name, "kvm:kvm_entry");
  104. }
  105. static bool exit_event_end(struct perf_evsel *evsel,
  106. struct perf_sample *sample __maybe_unused,
  107. struct event_key *key __maybe_unused)
  108. {
  109. return kvm_entry_event(evsel);
  110. }
  111. static struct exit_reasons_table vmx_exit_reasons[] = {
  112. VMX_EXIT_REASONS
  113. };
  114. static struct exit_reasons_table svm_exit_reasons[] = {
  115. SVM_EXIT_REASONS
  116. };
  117. static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
  118. {
  119. int i = kvm->exit_reasons_size;
  120. struct exit_reasons_table *tbl = kvm->exit_reasons;
  121. while (i--) {
  122. if (tbl->exit_code == exit_code)
  123. return tbl->reason;
  124. tbl++;
  125. }
  126. pr_err("unknown kvm exit code:%lld on %s\n",
  127. (unsigned long long)exit_code, kvm->exit_reasons_isa);
  128. return "UNKNOWN";
  129. }
  130. static void exit_event_decode_key(struct perf_kvm_stat *kvm,
  131. struct event_key *key,
  132. char decode[20])
  133. {
  134. const char *exit_reason = get_exit_reason(kvm, key->key);
  135. scnprintf(decode, 20, "%s", exit_reason);
  136. }
  137. static struct kvm_events_ops exit_events = {
  138. .is_begin_event = exit_event_begin,
  139. .is_end_event = exit_event_end,
  140. .decode_key = exit_event_decode_key,
  141. .name = "VM-EXIT"
  142. };
  143. /*
  144. * For the mmio events, we treat:
  145. * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
  146. * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
  147. */
  148. static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
  149. struct event_key *key)
  150. {
  151. key->key = perf_evsel__intval(evsel, sample, "gpa");
  152. key->info = perf_evsel__intval(evsel, sample, "type");
  153. }
  154. #define KVM_TRACE_MMIO_READ_UNSATISFIED 0
  155. #define KVM_TRACE_MMIO_READ 1
  156. #define KVM_TRACE_MMIO_WRITE 2
  157. static bool mmio_event_begin(struct perf_evsel *evsel,
  158. struct perf_sample *sample, struct event_key *key)
  159. {
  160. /* MMIO read begin event in kernel. */
  161. if (kvm_exit_event(evsel))
  162. return true;
  163. /* MMIO write begin event in kernel. */
  164. if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
  165. perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
  166. mmio_event_get_key(evsel, sample, key);
  167. return true;
  168. }
  169. return false;
  170. }
  171. static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
  172. struct event_key *key)
  173. {
  174. /* MMIO write end event in kernel. */
  175. if (kvm_entry_event(evsel))
  176. return true;
  177. /* MMIO read end event in kernel.*/
  178. if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
  179. perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
  180. mmio_event_get_key(evsel, sample, key);
  181. return true;
  182. }
  183. return false;
  184. }
  185. static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
  186. struct event_key *key,
  187. char decode[20])
  188. {
  189. scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
  190. key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
  191. }
  192. static struct kvm_events_ops mmio_events = {
  193. .is_begin_event = mmio_event_begin,
  194. .is_end_event = mmio_event_end,
  195. .decode_key = mmio_event_decode_key,
  196. .name = "MMIO Access"
  197. };
  198. /* The time of emulation pio access is from kvm_pio to kvm_entry. */
  199. static void ioport_event_get_key(struct perf_evsel *evsel,
  200. struct perf_sample *sample,
  201. struct event_key *key)
  202. {
  203. key->key = perf_evsel__intval(evsel, sample, "port");
  204. key->info = perf_evsel__intval(evsel, sample, "rw");
  205. }
  206. static bool ioport_event_begin(struct perf_evsel *evsel,
  207. struct perf_sample *sample,
  208. struct event_key *key)
  209. {
  210. if (!strcmp(evsel->name, "kvm:kvm_pio")) {
  211. ioport_event_get_key(evsel, sample, key);
  212. return true;
  213. }
  214. return false;
  215. }
  216. static bool ioport_event_end(struct perf_evsel *evsel,
  217. struct perf_sample *sample __maybe_unused,
  218. struct event_key *key __maybe_unused)
  219. {
  220. return kvm_entry_event(evsel);
  221. }
  222. static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
  223. struct event_key *key,
  224. char decode[20])
  225. {
  226. scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
  227. key->info ? "POUT" : "PIN");
  228. }
  229. static struct kvm_events_ops ioport_events = {
  230. .is_begin_event = ioport_event_begin,
  231. .is_end_event = ioport_event_end,
  232. .decode_key = ioport_event_decode_key,
  233. .name = "IO Port Access"
  234. };
  235. static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
  236. {
  237. bool ret = true;
  238. if (!strcmp(kvm->report_event, "vmexit"))
  239. kvm->events_ops = &exit_events;
  240. else if (!strcmp(kvm->report_event, "mmio"))
  241. kvm->events_ops = &mmio_events;
  242. else if (!strcmp(kvm->report_event, "ioport"))
  243. kvm->events_ops = &ioport_events;
  244. else {
  245. pr_err("Unknown report event:%s\n", kvm->report_event);
  246. ret = false;
  247. }
  248. return ret;
  249. }
  250. struct vcpu_event_record {
  251. int vcpu_id;
  252. u64 start_time;
  253. struct kvm_event *last_event;
  254. };
  255. static void init_kvm_event_record(struct perf_kvm_stat *kvm)
  256. {
  257. unsigned int i;
  258. for (i = 0; i < EVENTS_CACHE_SIZE; i++)
  259. INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
  260. }
  261. static int kvm_events_hash_fn(u64 key)
  262. {
  263. return key & (EVENTS_CACHE_SIZE - 1);
  264. }
  265. static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
  266. {
  267. int old_max_vcpu = event->max_vcpu;
  268. if (vcpu_id < event->max_vcpu)
  269. return true;
  270. while (event->max_vcpu <= vcpu_id)
  271. event->max_vcpu += DEFAULT_VCPU_NUM;
  272. event->vcpu = realloc(event->vcpu,
  273. event->max_vcpu * sizeof(*event->vcpu));
  274. if (!event->vcpu) {
  275. pr_err("Not enough memory\n");
  276. return false;
  277. }
  278. memset(event->vcpu + old_max_vcpu, 0,
  279. (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
  280. return true;
  281. }
  282. static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
  283. {
  284. struct kvm_event *event;
  285. event = zalloc(sizeof(*event));
  286. if (!event) {
  287. pr_err("Not enough memory\n");
  288. return NULL;
  289. }
  290. event->key = *key;
  291. return event;
  292. }
  293. static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
  294. struct event_key *key)
  295. {
  296. struct kvm_event *event;
  297. struct list_head *head;
  298. BUG_ON(key->key == INVALID_KEY);
  299. head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
  300. list_for_each_entry(event, head, hash_entry) {
  301. if (event->key.key == key->key && event->key.info == key->info)
  302. return event;
  303. }
  304. event = kvm_alloc_init_event(key);
  305. if (!event)
  306. return NULL;
  307. list_add(&event->hash_entry, head);
  308. return event;
  309. }
  310. static bool handle_begin_event(struct perf_kvm_stat *kvm,
  311. struct vcpu_event_record *vcpu_record,
  312. struct event_key *key, u64 timestamp)
  313. {
  314. struct kvm_event *event = NULL;
  315. if (key->key != INVALID_KEY)
  316. event = find_create_kvm_event(kvm, key);
  317. vcpu_record->last_event = event;
  318. vcpu_record->start_time = timestamp;
  319. return true;
  320. }
  321. static void
  322. kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
  323. {
  324. kvm_stats->time += time_diff;
  325. update_stats(&kvm_stats->stats, time_diff);
  326. }
  327. static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
  328. {
  329. struct kvm_event_stats *kvm_stats = &event->total;
  330. if (vcpu_id != -1)
  331. kvm_stats = &event->vcpu[vcpu_id];
  332. return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
  333. avg_stats(&kvm_stats->stats));
  334. }
  335. static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
  336. u64 time_diff)
  337. {
  338. if (vcpu_id == -1) {
  339. kvm_update_event_stats(&event->total, time_diff);
  340. return true;
  341. }
  342. if (!kvm_event_expand(event, vcpu_id))
  343. return false;
  344. kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
  345. return true;
  346. }
  347. static bool handle_end_event(struct perf_kvm_stat *kvm,
  348. struct vcpu_event_record *vcpu_record,
  349. struct event_key *key,
  350. u64 timestamp)
  351. {
  352. struct kvm_event *event;
  353. u64 time_begin, time_diff;
  354. int vcpu;
  355. if (kvm->trace_vcpu == -1)
  356. vcpu = -1;
  357. else
  358. vcpu = vcpu_record->vcpu_id;
  359. event = vcpu_record->last_event;
  360. time_begin = vcpu_record->start_time;
  361. /* The begin event is not caught. */
  362. if (!time_begin)
  363. return true;
  364. /*
  365. * In some case, the 'begin event' only records the start timestamp,
  366. * the actual event is recognized in the 'end event' (e.g. mmio-event).
  367. */
  368. /* Both begin and end events did not get the key. */
  369. if (!event && key->key == INVALID_KEY)
  370. return true;
  371. if (!event)
  372. event = find_create_kvm_event(kvm, key);
  373. if (!event)
  374. return false;
  375. vcpu_record->last_event = NULL;
  376. vcpu_record->start_time = 0;
  377. BUG_ON(timestamp < time_begin);
  378. time_diff = timestamp - time_begin;
  379. return update_kvm_event(event, vcpu, time_diff);
  380. }
  381. static
  382. struct vcpu_event_record *per_vcpu_record(struct thread *thread,
  383. struct perf_evsel *evsel,
  384. struct perf_sample *sample)
  385. {
  386. /* Only kvm_entry records vcpu id. */
  387. if (!thread->priv && kvm_entry_event(evsel)) {
  388. struct vcpu_event_record *vcpu_record;
  389. vcpu_record = zalloc(sizeof(*vcpu_record));
  390. if (!vcpu_record) {
  391. pr_err("%s: Not enough memory\n", __func__);
  392. return NULL;
  393. }
  394. vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
  395. thread->priv = vcpu_record;
  396. }
  397. return thread->priv;
  398. }
  399. static bool handle_kvm_event(struct perf_kvm_stat *kvm,
  400. struct thread *thread,
  401. struct perf_evsel *evsel,
  402. struct perf_sample *sample)
  403. {
  404. struct vcpu_event_record *vcpu_record;
  405. struct event_key key = {.key = INVALID_KEY};
  406. vcpu_record = per_vcpu_record(thread, evsel, sample);
  407. if (!vcpu_record)
  408. return true;
  409. /* only process events for vcpus user cares about */
  410. if ((kvm->trace_vcpu != -1) &&
  411. (kvm->trace_vcpu != vcpu_record->vcpu_id))
  412. return true;
  413. if (kvm->events_ops->is_begin_event(evsel, sample, &key))
  414. return handle_begin_event(kvm, vcpu_record, &key, sample->time);
  415. if (kvm->events_ops->is_end_event(evsel, sample, &key))
  416. return handle_end_event(kvm, vcpu_record, &key, sample->time);
  417. return true;
  418. }
  419. #define GET_EVENT_KEY(func, field) \
  420. static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
  421. { \
  422. if (vcpu == -1) \
  423. return event->total.field; \
  424. \
  425. if (vcpu >= event->max_vcpu) \
  426. return 0; \
  427. \
  428. return event->vcpu[vcpu].field; \
  429. }
  430. #define COMPARE_EVENT_KEY(func, field) \
  431. GET_EVENT_KEY(func, field) \
  432. static int compare_kvm_event_ ## func(struct kvm_event *one, \
  433. struct kvm_event *two, int vcpu)\
  434. { \
  435. return get_event_ ##func(one, vcpu) > \
  436. get_event_ ##func(two, vcpu); \
  437. }
  438. GET_EVENT_KEY(time, time);
  439. COMPARE_EVENT_KEY(count, stats.n);
  440. COMPARE_EVENT_KEY(mean, stats.mean);
  441. #define DEF_SORT_NAME_KEY(name, compare_key) \
  442. { #name, compare_kvm_event_ ## compare_key }
  443. static struct kvm_event_key keys[] = {
  444. DEF_SORT_NAME_KEY(sample, count),
  445. DEF_SORT_NAME_KEY(time, mean),
  446. { NULL, NULL }
  447. };
  448. static bool select_key(struct perf_kvm_stat *kvm)
  449. {
  450. int i;
  451. for (i = 0; keys[i].name; i++) {
  452. if (!strcmp(keys[i].name, kvm->sort_key)) {
  453. kvm->compare = keys[i].key;
  454. return true;
  455. }
  456. }
  457. pr_err("Unknown compare key:%s\n", kvm->sort_key);
  458. return false;
  459. }
  460. static void insert_to_result(struct rb_root *result, struct kvm_event *event,
  461. key_cmp_fun bigger, int vcpu)
  462. {
  463. struct rb_node **rb = &result->rb_node;
  464. struct rb_node *parent = NULL;
  465. struct kvm_event *p;
  466. while (*rb) {
  467. p = container_of(*rb, struct kvm_event, rb);
  468. parent = *rb;
  469. if (bigger(event, p, vcpu))
  470. rb = &(*rb)->rb_left;
  471. else
  472. rb = &(*rb)->rb_right;
  473. }
  474. rb_link_node(&event->rb, parent, rb);
  475. rb_insert_color(&event->rb, result);
  476. }
  477. static void
  478. update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
  479. {
  480. int vcpu = kvm->trace_vcpu;
  481. kvm->total_count += get_event_count(event, vcpu);
  482. kvm->total_time += get_event_time(event, vcpu);
  483. }
  484. static bool event_is_valid(struct kvm_event *event, int vcpu)
  485. {
  486. return !!get_event_count(event, vcpu);
  487. }
  488. static void sort_result(struct perf_kvm_stat *kvm)
  489. {
  490. unsigned int i;
  491. int vcpu = kvm->trace_vcpu;
  492. struct kvm_event *event;
  493. for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
  494. list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
  495. if (event_is_valid(event, vcpu)) {
  496. update_total_count(kvm, event);
  497. insert_to_result(&kvm->result, event,
  498. kvm->compare, vcpu);
  499. }
  500. }
  501. }
  502. }
  503. /* returns left most element of result, and erase it */
  504. static struct kvm_event *pop_from_result(struct rb_root *result)
  505. {
  506. struct rb_node *node = rb_first(result);
  507. if (!node)
  508. return NULL;
  509. rb_erase(node, result);
  510. return container_of(node, struct kvm_event, rb);
  511. }
  512. static void print_vcpu_info(int vcpu)
  513. {
  514. pr_info("Analyze events for ");
  515. if (vcpu == -1)
  516. pr_info("all VCPUs:\n\n");
  517. else
  518. pr_info("VCPU %d:\n\n", vcpu);
  519. }
  520. static void print_result(struct perf_kvm_stat *kvm)
  521. {
  522. char decode[20];
  523. struct kvm_event *event;
  524. int vcpu = kvm->trace_vcpu;
  525. pr_info("\n\n");
  526. print_vcpu_info(vcpu);
  527. pr_info("%20s ", kvm->events_ops->name);
  528. pr_info("%10s ", "Samples");
  529. pr_info("%9s ", "Samples%");
  530. pr_info("%9s ", "Time%");
  531. pr_info("%16s ", "Avg time");
  532. pr_info("\n\n");
  533. while ((event = pop_from_result(&kvm->result))) {
  534. u64 ecount, etime;
  535. ecount = get_event_count(event, vcpu);
  536. etime = get_event_time(event, vcpu);
  537. kvm->events_ops->decode_key(kvm, &event->key, decode);
  538. pr_info("%20s ", decode);
  539. pr_info("%10llu ", (unsigned long long)ecount);
  540. pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
  541. pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
  542. pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
  543. kvm_event_rel_stddev(vcpu, event));
  544. pr_info("\n");
  545. }
  546. pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
  547. kvm->total_count, kvm->total_time / 1e3);
  548. }
  549. static int process_sample_event(struct perf_tool *tool,
  550. union perf_event *event,
  551. struct perf_sample *sample,
  552. struct perf_evsel *evsel,
  553. struct machine *machine)
  554. {
  555. struct thread *thread = machine__findnew_thread(machine, sample->tid);
  556. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
  557. tool);
  558. if (thread == NULL) {
  559. pr_debug("problem processing %d event, skipping it.\n",
  560. event->header.type);
  561. return -1;
  562. }
  563. if (!handle_kvm_event(kvm, thread, evsel, sample))
  564. return -1;
  565. return 0;
  566. }
  567. static int get_cpu_isa(struct perf_session *session)
  568. {
  569. char *cpuid = session->header.env.cpuid;
  570. int isa;
  571. if (strstr(cpuid, "Intel"))
  572. isa = 1;
  573. else if (strstr(cpuid, "AMD"))
  574. isa = 0;
  575. else {
  576. pr_err("CPU %s is not supported.\n", cpuid);
  577. isa = -ENOTSUP;
  578. }
  579. return isa;
  580. }
  581. static int read_events(struct perf_kvm_stat *kvm)
  582. {
  583. int ret;
  584. struct perf_tool eops = {
  585. .sample = process_sample_event,
  586. .comm = perf_event__process_comm,
  587. .ordered_samples = true,
  588. };
  589. kvm->tool = eops;
  590. kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false,
  591. &kvm->tool);
  592. if (!kvm->session) {
  593. pr_err("Initializing perf session failed\n");
  594. return -EINVAL;
  595. }
  596. if (!perf_session__has_traces(kvm->session, "kvm record"))
  597. return -EINVAL;
  598. /*
  599. * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
  600. * traced in the old kernel.
  601. */
  602. ret = get_cpu_isa(kvm->session);
  603. if (ret < 0)
  604. return ret;
  605. if (ret == 1) {
  606. kvm->exit_reasons = vmx_exit_reasons;
  607. kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
  608. kvm->exit_reasons_isa = "VMX";
  609. }
  610. return perf_session__process_events(kvm->session, &kvm->tool);
  611. }
  612. static bool verify_vcpu(int vcpu)
  613. {
  614. if (vcpu != -1 && vcpu < 0) {
  615. pr_err("Invalid vcpu:%d.\n", vcpu);
  616. return false;
  617. }
  618. return true;
  619. }
  620. static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
  621. {
  622. int ret = -EINVAL;
  623. int vcpu = kvm->trace_vcpu;
  624. if (!verify_vcpu(vcpu))
  625. goto exit;
  626. if (!select_key(kvm))
  627. goto exit;
  628. if (!register_kvm_events_ops(kvm))
  629. goto exit;
  630. init_kvm_event_record(kvm);
  631. setup_pager();
  632. ret = read_events(kvm);
  633. if (ret)
  634. goto exit;
  635. sort_result(kvm);
  636. print_result(kvm);
  637. exit:
  638. return ret;
  639. }
  640. static const char * const record_args[] = {
  641. "record",
  642. "-R",
  643. "-f",
  644. "-m", "1024",
  645. "-c", "1",
  646. "-e", "kvm:kvm_entry",
  647. "-e", "kvm:kvm_exit",
  648. "-e", "kvm:kvm_mmio",
  649. "-e", "kvm:kvm_pio",
  650. };
  651. #define STRDUP_FAIL_EXIT(s) \
  652. ({ char *_p; \
  653. _p = strdup(s); \
  654. if (!_p) \
  655. return -ENOMEM; \
  656. _p; \
  657. })
  658. static int
  659. kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
  660. {
  661. unsigned int rec_argc, i, j;
  662. const char **rec_argv;
  663. rec_argc = ARRAY_SIZE(record_args) + argc + 2;
  664. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  665. if (rec_argv == NULL)
  666. return -ENOMEM;
  667. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  668. rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
  669. rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
  670. rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
  671. for (j = 1; j < (unsigned int)argc; j++, i++)
  672. rec_argv[i] = argv[j];
  673. return cmd_record(i, rec_argv, NULL);
  674. }
  675. static int
  676. kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
  677. {
  678. const struct option kvm_events_report_options[] = {
  679. OPT_STRING(0, "event", &kvm->report_event, "report event",
  680. "event for reporting: vmexit, mmio, ioport"),
  681. OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
  682. "vcpu id to report"),
  683. OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
  684. "key for sorting: sample(sort by samples number)"
  685. " time (sort by avg time)"),
  686. OPT_END()
  687. };
  688. const char * const kvm_events_report_usage[] = {
  689. "perf kvm stat report [<options>]",
  690. NULL
  691. };
  692. symbol__init();
  693. if (argc) {
  694. argc = parse_options(argc, argv,
  695. kvm_events_report_options,
  696. kvm_events_report_usage, 0);
  697. if (argc)
  698. usage_with_options(kvm_events_report_usage,
  699. kvm_events_report_options);
  700. }
  701. return kvm_events_report_vcpu(kvm);
  702. }
  703. static void print_kvm_stat_usage(void)
  704. {
  705. printf("Usage: perf kvm stat <command>\n\n");
  706. printf("# Available commands:\n");
  707. printf("\trecord: record kvm events\n");
  708. printf("\treport: report statistical data of kvm events\n");
  709. printf("\nOtherwise, it is the alias of 'perf stat':\n");
  710. }
  711. static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
  712. {
  713. struct perf_kvm_stat kvm = {
  714. .file_name = file_name,
  715. .trace_vcpu = -1,
  716. .report_event = "vmexit",
  717. .sort_key = "sample",
  718. .exit_reasons = svm_exit_reasons,
  719. .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
  720. .exit_reasons_isa = "SVM",
  721. };
  722. if (argc == 1) {
  723. print_kvm_stat_usage();
  724. goto perf_stat;
  725. }
  726. if (!strncmp(argv[1], "rec", 3))
  727. return kvm_events_record(&kvm, argc - 1, argv + 1);
  728. if (!strncmp(argv[1], "rep", 3))
  729. return kvm_events_report(&kvm, argc - 1 , argv + 1);
  730. perf_stat:
  731. return cmd_stat(argc, argv, NULL);
  732. }
  733. #endif
  734. static int __cmd_record(const char *file_name, int argc, const char **argv)
  735. {
  736. int rec_argc, i = 0, j;
  737. const char **rec_argv;
  738. rec_argc = argc + 2;
  739. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  740. rec_argv[i++] = strdup("record");
  741. rec_argv[i++] = strdup("-o");
  742. rec_argv[i++] = strdup(file_name);
  743. for (j = 1; j < argc; j++, i++)
  744. rec_argv[i] = argv[j];
  745. BUG_ON(i != rec_argc);
  746. return cmd_record(i, rec_argv, NULL);
  747. }
  748. static int __cmd_report(const char *file_name, int argc, const char **argv)
  749. {
  750. int rec_argc, i = 0, j;
  751. const char **rec_argv;
  752. rec_argc = argc + 2;
  753. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  754. rec_argv[i++] = strdup("report");
  755. rec_argv[i++] = strdup("-i");
  756. rec_argv[i++] = strdup(file_name);
  757. for (j = 1; j < argc; j++, i++)
  758. rec_argv[i] = argv[j];
  759. BUG_ON(i != rec_argc);
  760. return cmd_report(i, rec_argv, NULL);
  761. }
  762. static int
  763. __cmd_buildid_list(const char *file_name, int argc, const char **argv)
  764. {
  765. int rec_argc, i = 0, j;
  766. const char **rec_argv;
  767. rec_argc = argc + 2;
  768. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  769. rec_argv[i++] = strdup("buildid-list");
  770. rec_argv[i++] = strdup("-i");
  771. rec_argv[i++] = strdup(file_name);
  772. for (j = 1; j < argc; j++, i++)
  773. rec_argv[i] = argv[j];
  774. BUG_ON(i != rec_argc);
  775. return cmd_buildid_list(i, rec_argv, NULL);
  776. }
  777. int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
  778. {
  779. const char *file_name = NULL;
  780. const struct option kvm_options[] = {
  781. OPT_STRING('i', "input", &file_name, "file",
  782. "Input file name"),
  783. OPT_STRING('o', "output", &file_name, "file",
  784. "Output file name"),
  785. OPT_BOOLEAN(0, "guest", &perf_guest,
  786. "Collect guest os data"),
  787. OPT_BOOLEAN(0, "host", &perf_host,
  788. "Collect host os data"),
  789. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  790. "guest mount directory under which every guest os"
  791. " instance has a subdir"),
  792. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  793. "file", "file saving guest os vmlinux"),
  794. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  795. "file", "file saving guest os /proc/kallsyms"),
  796. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  797. "file", "file saving guest os /proc/modules"),
  798. OPT_END()
  799. };
  800. const char * const kvm_usage[] = {
  801. "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
  802. NULL
  803. };
  804. perf_host = 0;
  805. perf_guest = 1;
  806. argc = parse_options(argc, argv, kvm_options, kvm_usage,
  807. PARSE_OPT_STOP_AT_NON_OPTION);
  808. if (!argc)
  809. usage_with_options(kvm_usage, kvm_options);
  810. if (!perf_host)
  811. perf_guest = 1;
  812. if (!file_name) {
  813. if (perf_host && !perf_guest)
  814. file_name = strdup("perf.data.host");
  815. else if (!perf_host && perf_guest)
  816. file_name = strdup("perf.data.guest");
  817. else
  818. file_name = strdup("perf.data.kvm");
  819. if (!file_name) {
  820. pr_err("Failed to allocate memory for filename\n");
  821. return -ENOMEM;
  822. }
  823. }
  824. if (!strncmp(argv[0], "rec", 3))
  825. return __cmd_record(file_name, argc, argv);
  826. else if (!strncmp(argv[0], "rep", 3))
  827. return __cmd_report(file_name, argc, argv);
  828. else if (!strncmp(argv[0], "diff", 4))
  829. return cmd_diff(argc, argv, NULL);
  830. else if (!strncmp(argv[0], "top", 3))
  831. return cmd_top(argc, argv, NULL);
  832. else if (!strncmp(argv[0], "buildid-list", 12))
  833. return __cmd_buildid_list(file_name, argc, argv);
  834. #if defined(__i386__) || defined(__x86_64__)
  835. else if (!strncmp(argv[0], "stat", 4))
  836. return kvm_cmd_stat(file_name, argc, argv);
  837. #endif
  838. else
  839. usage_with_options(kvm_usage, kvm_options);
  840. return 0;
  841. }