builtin-kvm.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. #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. int i;
  258. for (i = 0; i < (int)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. event = kvm_alloc_init_event(key);
  304. if (!event)
  305. return NULL;
  306. list_add(&event->hash_entry, head);
  307. return event;
  308. }
  309. static bool handle_begin_event(struct perf_kvm_stat *kvm,
  310. struct vcpu_event_record *vcpu_record,
  311. struct event_key *key, u64 timestamp)
  312. {
  313. struct kvm_event *event = NULL;
  314. if (key->key != INVALID_KEY)
  315. event = find_create_kvm_event(kvm, key);
  316. vcpu_record->last_event = event;
  317. vcpu_record->start_time = timestamp;
  318. return true;
  319. }
  320. static void
  321. kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
  322. {
  323. kvm_stats->time += time_diff;
  324. update_stats(&kvm_stats->stats, time_diff);
  325. }
  326. static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
  327. {
  328. struct kvm_event_stats *kvm_stats = &event->total;
  329. if (vcpu_id != -1)
  330. kvm_stats = &event->vcpu[vcpu_id];
  331. return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
  332. avg_stats(&kvm_stats->stats));
  333. }
  334. static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
  335. u64 time_diff)
  336. {
  337. kvm_update_event_stats(&event->total, time_diff);
  338. if (!kvm_event_expand(event, vcpu_id))
  339. return false;
  340. kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
  341. return true;
  342. }
  343. static bool handle_end_event(struct perf_kvm_stat *kvm,
  344. struct vcpu_event_record *vcpu_record,
  345. struct event_key *key,
  346. u64 timestamp)
  347. {
  348. struct kvm_event *event;
  349. u64 time_begin, time_diff;
  350. event = vcpu_record->last_event;
  351. time_begin = vcpu_record->start_time;
  352. /* The begin event is not caught. */
  353. if (!time_begin)
  354. return true;
  355. /*
  356. * In some case, the 'begin event' only records the start timestamp,
  357. * the actual event is recognized in the 'end event' (e.g. mmio-event).
  358. */
  359. /* Both begin and end events did not get the key. */
  360. if (!event && key->key == INVALID_KEY)
  361. return true;
  362. if (!event)
  363. event = find_create_kvm_event(kvm, key);
  364. if (!event)
  365. return false;
  366. vcpu_record->last_event = NULL;
  367. vcpu_record->start_time = 0;
  368. BUG_ON(timestamp < time_begin);
  369. time_diff = timestamp - time_begin;
  370. return update_kvm_event(event, vcpu_record->vcpu_id, time_diff);
  371. }
  372. static
  373. struct vcpu_event_record *per_vcpu_record(struct thread *thread,
  374. struct perf_evsel *evsel,
  375. struct perf_sample *sample)
  376. {
  377. /* Only kvm_entry records vcpu id. */
  378. if (!thread->priv && kvm_entry_event(evsel)) {
  379. struct vcpu_event_record *vcpu_record;
  380. vcpu_record = zalloc(sizeof(*vcpu_record));
  381. if (!vcpu_record) {
  382. pr_err("%s: Not enough memory\n", __func__);
  383. return NULL;
  384. }
  385. vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
  386. thread->priv = vcpu_record;
  387. }
  388. return thread->priv;
  389. }
  390. static bool handle_kvm_event(struct perf_kvm_stat *kvm,
  391. struct thread *thread,
  392. struct perf_evsel *evsel,
  393. struct perf_sample *sample)
  394. {
  395. struct vcpu_event_record *vcpu_record;
  396. struct event_key key = {.key = INVALID_KEY};
  397. vcpu_record = per_vcpu_record(thread, evsel, sample);
  398. if (!vcpu_record)
  399. return true;
  400. if (kvm->events_ops->is_begin_event(evsel, sample, &key))
  401. return handle_begin_event(kvm, vcpu_record, &key, sample->time);
  402. if (kvm->events_ops->is_end_event(evsel, sample, &key))
  403. return handle_end_event(kvm, vcpu_record, &key, sample->time);
  404. return true;
  405. }
  406. #define GET_EVENT_KEY(func, field) \
  407. static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
  408. { \
  409. if (vcpu == -1) \
  410. return event->total.field; \
  411. \
  412. if (vcpu >= event->max_vcpu) \
  413. return 0; \
  414. \
  415. return event->vcpu[vcpu].field; \
  416. }
  417. #define COMPARE_EVENT_KEY(func, field) \
  418. GET_EVENT_KEY(func, field) \
  419. static int compare_kvm_event_ ## func(struct kvm_event *one, \
  420. struct kvm_event *two, int vcpu)\
  421. { \
  422. return get_event_ ##func(one, vcpu) > \
  423. get_event_ ##func(two, vcpu); \
  424. }
  425. GET_EVENT_KEY(time, time);
  426. COMPARE_EVENT_KEY(count, stats.n);
  427. COMPARE_EVENT_KEY(mean, stats.mean);
  428. #define DEF_SORT_NAME_KEY(name, compare_key) \
  429. { #name, compare_kvm_event_ ## compare_key }
  430. static struct kvm_event_key keys[] = {
  431. DEF_SORT_NAME_KEY(sample, count),
  432. DEF_SORT_NAME_KEY(time, mean),
  433. { NULL, NULL }
  434. };
  435. static bool select_key(struct perf_kvm_stat *kvm)
  436. {
  437. int i;
  438. for (i = 0; keys[i].name; i++) {
  439. if (!strcmp(keys[i].name, kvm->sort_key)) {
  440. kvm->compare = keys[i].key;
  441. return true;
  442. }
  443. }
  444. pr_err("Unknown compare key:%s\n", kvm->sort_key);
  445. return false;
  446. }
  447. static void insert_to_result(struct rb_root *result, struct kvm_event *event,
  448. key_cmp_fun bigger, int vcpu)
  449. {
  450. struct rb_node **rb = &result->rb_node;
  451. struct rb_node *parent = NULL;
  452. struct kvm_event *p;
  453. while (*rb) {
  454. p = container_of(*rb, struct kvm_event, rb);
  455. parent = *rb;
  456. if (bigger(event, p, vcpu))
  457. rb = &(*rb)->rb_left;
  458. else
  459. rb = &(*rb)->rb_right;
  460. }
  461. rb_link_node(&event->rb, parent, rb);
  462. rb_insert_color(&event->rb, result);
  463. }
  464. static void
  465. update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
  466. {
  467. int vcpu = kvm->trace_vcpu;
  468. kvm->total_count += get_event_count(event, vcpu);
  469. kvm->total_time += get_event_time(event, vcpu);
  470. }
  471. static bool event_is_valid(struct kvm_event *event, int vcpu)
  472. {
  473. return !!get_event_count(event, vcpu);
  474. }
  475. static void sort_result(struct perf_kvm_stat *kvm)
  476. {
  477. unsigned int i;
  478. int vcpu = kvm->trace_vcpu;
  479. struct kvm_event *event;
  480. for (i = 0; i < EVENTS_CACHE_SIZE; i++)
  481. list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry)
  482. if (event_is_valid(event, vcpu)) {
  483. update_total_count(kvm, event);
  484. insert_to_result(&kvm->result, event,
  485. kvm->compare, vcpu);
  486. }
  487. }
  488. /* returns left most element of result, and erase it */
  489. static struct kvm_event *pop_from_result(struct rb_root *result)
  490. {
  491. struct rb_node *node = rb_first(result);
  492. if (!node)
  493. return NULL;
  494. rb_erase(node, result);
  495. return container_of(node, struct kvm_event, rb);
  496. }
  497. static void print_vcpu_info(int vcpu)
  498. {
  499. pr_info("Analyze events for ");
  500. if (vcpu == -1)
  501. pr_info("all VCPUs:\n\n");
  502. else
  503. pr_info("VCPU %d:\n\n", vcpu);
  504. }
  505. static void print_result(struct perf_kvm_stat *kvm)
  506. {
  507. char decode[20];
  508. struct kvm_event *event;
  509. int vcpu = kvm->trace_vcpu;
  510. pr_info("\n\n");
  511. print_vcpu_info(vcpu);
  512. pr_info("%20s ", kvm->events_ops->name);
  513. pr_info("%10s ", "Samples");
  514. pr_info("%9s ", "Samples%");
  515. pr_info("%9s ", "Time%");
  516. pr_info("%16s ", "Avg time");
  517. pr_info("\n\n");
  518. while ((event = pop_from_result(&kvm->result))) {
  519. u64 ecount, etime;
  520. ecount = get_event_count(event, vcpu);
  521. etime = get_event_time(event, vcpu);
  522. kvm->events_ops->decode_key(kvm, &event->key, decode);
  523. pr_info("%20s ", decode);
  524. pr_info("%10llu ", (unsigned long long)ecount);
  525. pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
  526. pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
  527. pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
  528. kvm_event_rel_stddev(vcpu, event));
  529. pr_info("\n");
  530. }
  531. pr_info("\nTotal Samples:%lld, Total events handled time:%.2fus.\n\n",
  532. (unsigned long long)kvm->total_count, kvm->total_time / 1e3);
  533. }
  534. static int process_sample_event(struct perf_tool *tool,
  535. union perf_event *event,
  536. struct perf_sample *sample,
  537. struct perf_evsel *evsel,
  538. struct machine *machine)
  539. {
  540. struct thread *thread = machine__findnew_thread(machine, sample->tid);
  541. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
  542. tool);
  543. if (thread == NULL) {
  544. pr_debug("problem processing %d event, skipping it.\n",
  545. event->header.type);
  546. return -1;
  547. }
  548. if (!handle_kvm_event(kvm, thread, evsel, sample))
  549. return -1;
  550. return 0;
  551. }
  552. static int get_cpu_isa(struct perf_session *session)
  553. {
  554. char *cpuid = session->header.env.cpuid;
  555. int isa;
  556. if (strstr(cpuid, "Intel"))
  557. isa = 1;
  558. else if (strstr(cpuid, "AMD"))
  559. isa = 0;
  560. else {
  561. pr_err("CPU %s is not supported.\n", cpuid);
  562. isa = -ENOTSUP;
  563. }
  564. return isa;
  565. }
  566. static int read_events(struct perf_kvm_stat *kvm)
  567. {
  568. int ret;
  569. struct perf_tool eops = {
  570. .sample = process_sample_event,
  571. .comm = perf_event__process_comm,
  572. .ordered_samples = true,
  573. };
  574. kvm->tool = eops;
  575. kvm->session = perf_session__new(kvm->file_name, O_RDONLY, 0, false,
  576. &kvm->tool);
  577. if (!kvm->session) {
  578. pr_err("Initializing perf session failed\n");
  579. return -EINVAL;
  580. }
  581. if (!perf_session__has_traces(kvm->session, "kvm record"))
  582. return -EINVAL;
  583. /*
  584. * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
  585. * traced in the old kernel.
  586. */
  587. ret = get_cpu_isa(kvm->session);
  588. if (ret < 0)
  589. return ret;
  590. if (ret == 1) {
  591. kvm->exit_reasons = vmx_exit_reasons;
  592. kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
  593. kvm->exit_reasons_isa = "VMX";
  594. }
  595. return perf_session__process_events(kvm->session, &kvm->tool);
  596. }
  597. static bool verify_vcpu(int vcpu)
  598. {
  599. if (vcpu != -1 && vcpu < 0) {
  600. pr_err("Invalid vcpu:%d.\n", vcpu);
  601. return false;
  602. }
  603. return true;
  604. }
  605. static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
  606. {
  607. int ret = -EINVAL;
  608. int vcpu = kvm->trace_vcpu;
  609. if (!verify_vcpu(vcpu))
  610. goto exit;
  611. if (!select_key(kvm))
  612. goto exit;
  613. if (!register_kvm_events_ops(kvm))
  614. goto exit;
  615. init_kvm_event_record(kvm);
  616. setup_pager();
  617. ret = read_events(kvm);
  618. if (ret)
  619. goto exit;
  620. sort_result(kvm);
  621. print_result(kvm);
  622. exit:
  623. return ret;
  624. }
  625. static const char * const record_args[] = {
  626. "record",
  627. "-R",
  628. "-f",
  629. "-m", "1024",
  630. "-c", "1",
  631. "-e", "kvm:kvm_entry",
  632. "-e", "kvm:kvm_exit",
  633. "-e", "kvm:kvm_mmio",
  634. "-e", "kvm:kvm_pio",
  635. };
  636. #define STRDUP_FAIL_EXIT(s) \
  637. ({ char *_p; \
  638. _p = strdup(s); \
  639. if (!_p) \
  640. return -ENOMEM; \
  641. _p; \
  642. })
  643. static int
  644. kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
  645. {
  646. unsigned int rec_argc, i, j;
  647. const char **rec_argv;
  648. rec_argc = ARRAY_SIZE(record_args) + argc + 2;
  649. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  650. if (rec_argv == NULL)
  651. return -ENOMEM;
  652. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  653. rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
  654. rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
  655. rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
  656. for (j = 1; j < (unsigned int)argc; j++, i++)
  657. rec_argv[i] = argv[j];
  658. return cmd_record(i, rec_argv, NULL);
  659. }
  660. static int
  661. kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
  662. {
  663. const struct option kvm_events_report_options[] = {
  664. OPT_STRING(0, "event", &kvm->report_event, "report event",
  665. "event for reporting: vmexit, mmio, ioport"),
  666. OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
  667. "vcpu id to report"),
  668. OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
  669. "key for sorting: sample(sort by samples number)"
  670. " time (sort by avg time)"),
  671. OPT_END()
  672. };
  673. const char * const kvm_events_report_usage[] = {
  674. "perf kvm stat report [<options>]",
  675. NULL
  676. };
  677. symbol__init();
  678. if (argc) {
  679. argc = parse_options(argc, argv,
  680. kvm_events_report_options,
  681. kvm_events_report_usage, 0);
  682. if (argc)
  683. usage_with_options(kvm_events_report_usage,
  684. kvm_events_report_options);
  685. }
  686. return kvm_events_report_vcpu(kvm);
  687. }
  688. static void print_kvm_stat_usage(void)
  689. {
  690. printf("Usage: perf kvm stat <command>\n\n");
  691. printf("# Available commands:\n");
  692. printf("\trecord: record kvm events\n");
  693. printf("\treport: report statistical data of kvm events\n");
  694. printf("\nOtherwise, it is the alias of 'perf stat':\n");
  695. }
  696. static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
  697. {
  698. struct perf_kvm_stat kvm = {
  699. .file_name = file_name,
  700. .trace_vcpu = -1,
  701. .report_event = "vmexit",
  702. .sort_key = "sample",
  703. .exit_reasons = svm_exit_reasons,
  704. .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
  705. .exit_reasons_isa = "SVM",
  706. };
  707. if (argc == 1) {
  708. print_kvm_stat_usage();
  709. goto perf_stat;
  710. }
  711. if (!strncmp(argv[1], "rec", 3))
  712. return kvm_events_record(&kvm, argc - 1, argv + 1);
  713. if (!strncmp(argv[1], "rep", 3))
  714. return kvm_events_report(&kvm, argc - 1 , argv + 1);
  715. perf_stat:
  716. return cmd_stat(argc, argv, NULL);
  717. }
  718. #endif
  719. static int __cmd_record(const char *file_name, int argc, const char **argv)
  720. {
  721. int rec_argc, i = 0, j;
  722. const char **rec_argv;
  723. rec_argc = argc + 2;
  724. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  725. rec_argv[i++] = strdup("record");
  726. rec_argv[i++] = strdup("-o");
  727. rec_argv[i++] = strdup(file_name);
  728. for (j = 1; j < argc; j++, i++)
  729. rec_argv[i] = argv[j];
  730. BUG_ON(i != rec_argc);
  731. return cmd_record(i, rec_argv, NULL);
  732. }
  733. static int __cmd_report(const char *file_name, int argc, const char **argv)
  734. {
  735. int rec_argc, i = 0, j;
  736. const char **rec_argv;
  737. rec_argc = argc + 2;
  738. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  739. rec_argv[i++] = strdup("report");
  740. rec_argv[i++] = strdup("-i");
  741. rec_argv[i++] = strdup(file_name);
  742. for (j = 1; j < argc; j++, i++)
  743. rec_argv[i] = argv[j];
  744. BUG_ON(i != rec_argc);
  745. return cmd_report(i, rec_argv, NULL);
  746. }
  747. static int
  748. __cmd_buildid_list(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("buildid-list");
  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_buildid_list(i, rec_argv, NULL);
  761. }
  762. int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
  763. {
  764. const char *file_name;
  765. const struct option kvm_options[] = {
  766. OPT_STRING('i', "input", &file_name, "file",
  767. "Input file name"),
  768. OPT_STRING('o', "output", &file_name, "file",
  769. "Output file name"),
  770. OPT_BOOLEAN(0, "guest", &perf_guest,
  771. "Collect guest os data"),
  772. OPT_BOOLEAN(0, "host", &perf_host,
  773. "Collect host os data"),
  774. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  775. "guest mount directory under which every guest os"
  776. " instance has a subdir"),
  777. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  778. "file", "file saving guest os vmlinux"),
  779. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  780. "file", "file saving guest os /proc/kallsyms"),
  781. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  782. "file", "file saving guest os /proc/modules"),
  783. OPT_END()
  784. };
  785. const char * const kvm_usage[] = {
  786. "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
  787. NULL
  788. };
  789. perf_host = 0;
  790. perf_guest = 1;
  791. argc = parse_options(argc, argv, kvm_options, kvm_usage,
  792. PARSE_OPT_STOP_AT_NON_OPTION);
  793. if (!argc)
  794. usage_with_options(kvm_usage, kvm_options);
  795. if (!perf_host)
  796. perf_guest = 1;
  797. if (!file_name) {
  798. if (perf_host && !perf_guest)
  799. file_name = strdup("perf.data.host");
  800. else if (!perf_host && perf_guest)
  801. file_name = strdup("perf.data.guest");
  802. else
  803. file_name = strdup("perf.data.kvm");
  804. if (!file_name) {
  805. pr_err("Failed to allocate memory for filename\n");
  806. return -ENOMEM;
  807. }
  808. }
  809. if (!strncmp(argv[0], "rec", 3))
  810. return __cmd_record(file_name, argc, argv);
  811. else if (!strncmp(argv[0], "rep", 3))
  812. return __cmd_report(file_name, argc, argv);
  813. else if (!strncmp(argv[0], "diff", 4))
  814. return cmd_diff(argc, argv, NULL);
  815. else if (!strncmp(argv[0], "top", 3))
  816. return cmd_top(argc, argv, NULL);
  817. else if (!strncmp(argv[0], "buildid-list", 12))
  818. return __cmd_buildid_list(file_name, argc, argv);
  819. #if defined(__i386__) || defined(__x86_64__)
  820. else if (!strncmp(argv[0], "stat", 4))
  821. return kvm_cmd_stat(file_name, argc, argv);
  822. #endif
  823. else
  824. usage_with_options(kvm_usage, kvm_options);
  825. return 0;
  826. }