builtin-kvm.c 22 KB

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