builtin-kvm.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/evsel.h"
  4. #include "util/evlist.h"
  5. #include "util/util.h"
  6. #include "util/cache.h"
  7. #include "util/symbol.h"
  8. #include "util/thread.h"
  9. #include "util/header.h"
  10. #include "util/session.h"
  11. #include "util/intlist.h"
  12. #include "util/parse-options.h"
  13. #include "util/trace-event.h"
  14. #include "util/debug.h"
  15. #include <api/fs/debugfs.h>
  16. #include "util/tool.h"
  17. #include "util/stat.h"
  18. #include "util/top.h"
  19. #include "util/data.h"
  20. #include <sys/prctl.h>
  21. #ifdef HAVE_TIMERFD_SUPPORT
  22. #include <sys/timerfd.h>
  23. #endif
  24. #include <termios.h>
  25. #include <semaphore.h>
  26. #include <pthread.h>
  27. #include <math.h>
  28. #if defined(__i386__) || defined(__x86_64__)
  29. #include <asm/svm.h>
  30. #include <asm/vmx.h>
  31. #include <asm/kvm.h>
  32. struct event_key {
  33. #define INVALID_KEY (~0ULL)
  34. u64 key;
  35. int info;
  36. };
  37. struct kvm_event_stats {
  38. u64 time;
  39. struct stats stats;
  40. };
  41. struct kvm_event {
  42. struct list_head hash_entry;
  43. struct rb_node rb;
  44. struct event_key key;
  45. struct kvm_event_stats total;
  46. #define DEFAULT_VCPU_NUM 8
  47. int max_vcpu;
  48. struct kvm_event_stats *vcpu;
  49. };
  50. typedef int (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int);
  51. struct kvm_event_key {
  52. const char *name;
  53. key_cmp_fun key;
  54. };
  55. struct perf_kvm_stat;
  56. struct kvm_events_ops {
  57. bool (*is_begin_event)(struct perf_evsel *evsel,
  58. struct perf_sample *sample,
  59. struct event_key *key);
  60. bool (*is_end_event)(struct perf_evsel *evsel,
  61. struct perf_sample *sample, struct event_key *key);
  62. void (*decode_key)(struct perf_kvm_stat *kvm, struct event_key *key,
  63. char decode[20]);
  64. const char *name;
  65. };
  66. struct exit_reasons_table {
  67. unsigned long exit_code;
  68. const char *reason;
  69. };
  70. #define EVENTS_BITS 12
  71. #define EVENTS_CACHE_SIZE (1UL << EVENTS_BITS)
  72. struct perf_kvm_stat {
  73. struct perf_tool tool;
  74. struct record_opts opts;
  75. struct perf_evlist *evlist;
  76. struct perf_session *session;
  77. const char *file_name;
  78. const char *report_event;
  79. const char *sort_key;
  80. int trace_vcpu;
  81. struct exit_reasons_table *exit_reasons;
  82. int exit_reasons_size;
  83. const char *exit_reasons_isa;
  84. struct kvm_events_ops *events_ops;
  85. key_cmp_fun compare;
  86. struct list_head kvm_events_cache[EVENTS_CACHE_SIZE];
  87. u64 total_time;
  88. u64 total_count;
  89. u64 lost_events;
  90. u64 duration;
  91. const char *pid_str;
  92. struct intlist *pid_list;
  93. struct rb_root result;
  94. int timerfd;
  95. unsigned int display_time;
  96. bool live;
  97. };
  98. static void exit_event_get_key(struct perf_evsel *evsel,
  99. struct perf_sample *sample,
  100. struct event_key *key)
  101. {
  102. key->info = 0;
  103. key->key = perf_evsel__intval(evsel, sample, "exit_reason");
  104. }
  105. static bool kvm_exit_event(struct perf_evsel *evsel)
  106. {
  107. return !strcmp(evsel->name, "kvm:kvm_exit");
  108. }
  109. static bool exit_event_begin(struct perf_evsel *evsel,
  110. struct perf_sample *sample, struct event_key *key)
  111. {
  112. if (kvm_exit_event(evsel)) {
  113. exit_event_get_key(evsel, sample, key);
  114. return true;
  115. }
  116. return false;
  117. }
  118. static bool kvm_entry_event(struct perf_evsel *evsel)
  119. {
  120. return !strcmp(evsel->name, "kvm:kvm_entry");
  121. }
  122. static bool exit_event_end(struct perf_evsel *evsel,
  123. struct perf_sample *sample __maybe_unused,
  124. struct event_key *key __maybe_unused)
  125. {
  126. return kvm_entry_event(evsel);
  127. }
  128. static struct exit_reasons_table vmx_exit_reasons[] = {
  129. VMX_EXIT_REASONS
  130. };
  131. static struct exit_reasons_table svm_exit_reasons[] = {
  132. SVM_EXIT_REASONS
  133. };
  134. static const char *get_exit_reason(struct perf_kvm_stat *kvm, u64 exit_code)
  135. {
  136. int i = kvm->exit_reasons_size;
  137. struct exit_reasons_table *tbl = kvm->exit_reasons;
  138. while (i--) {
  139. if (tbl->exit_code == exit_code)
  140. return tbl->reason;
  141. tbl++;
  142. }
  143. pr_err("unknown kvm exit code:%lld on %s\n",
  144. (unsigned long long)exit_code, kvm->exit_reasons_isa);
  145. return "UNKNOWN";
  146. }
  147. static void exit_event_decode_key(struct perf_kvm_stat *kvm,
  148. struct event_key *key,
  149. char decode[20])
  150. {
  151. const char *exit_reason = get_exit_reason(kvm, key->key);
  152. scnprintf(decode, 20, "%s", exit_reason);
  153. }
  154. static struct kvm_events_ops exit_events = {
  155. .is_begin_event = exit_event_begin,
  156. .is_end_event = exit_event_end,
  157. .decode_key = exit_event_decode_key,
  158. .name = "VM-EXIT"
  159. };
  160. /*
  161. * For the mmio events, we treat:
  162. * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
  163. * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
  164. */
  165. static void mmio_event_get_key(struct perf_evsel *evsel, struct perf_sample *sample,
  166. struct event_key *key)
  167. {
  168. key->key = perf_evsel__intval(evsel, sample, "gpa");
  169. key->info = perf_evsel__intval(evsel, sample, "type");
  170. }
  171. #define KVM_TRACE_MMIO_READ_UNSATISFIED 0
  172. #define KVM_TRACE_MMIO_READ 1
  173. #define KVM_TRACE_MMIO_WRITE 2
  174. static bool mmio_event_begin(struct perf_evsel *evsel,
  175. struct perf_sample *sample, struct event_key *key)
  176. {
  177. /* MMIO read begin event in kernel. */
  178. if (kvm_exit_event(evsel))
  179. return true;
  180. /* MMIO write begin event in kernel. */
  181. if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
  182. perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
  183. mmio_event_get_key(evsel, sample, key);
  184. return true;
  185. }
  186. return false;
  187. }
  188. static bool mmio_event_end(struct perf_evsel *evsel, struct perf_sample *sample,
  189. struct event_key *key)
  190. {
  191. /* MMIO write end event in kernel. */
  192. if (kvm_entry_event(evsel))
  193. return true;
  194. /* MMIO read end event in kernel.*/
  195. if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
  196. perf_evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
  197. mmio_event_get_key(evsel, sample, key);
  198. return true;
  199. }
  200. return false;
  201. }
  202. static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
  203. struct event_key *key,
  204. char decode[20])
  205. {
  206. scnprintf(decode, 20, "%#lx:%s", (unsigned long)key->key,
  207. key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
  208. }
  209. static struct kvm_events_ops mmio_events = {
  210. .is_begin_event = mmio_event_begin,
  211. .is_end_event = mmio_event_end,
  212. .decode_key = mmio_event_decode_key,
  213. .name = "MMIO Access"
  214. };
  215. /* The time of emulation pio access is from kvm_pio to kvm_entry. */
  216. static void ioport_event_get_key(struct perf_evsel *evsel,
  217. struct perf_sample *sample,
  218. struct event_key *key)
  219. {
  220. key->key = perf_evsel__intval(evsel, sample, "port");
  221. key->info = perf_evsel__intval(evsel, sample, "rw");
  222. }
  223. static bool ioport_event_begin(struct perf_evsel *evsel,
  224. struct perf_sample *sample,
  225. struct event_key *key)
  226. {
  227. if (!strcmp(evsel->name, "kvm:kvm_pio")) {
  228. ioport_event_get_key(evsel, sample, key);
  229. return true;
  230. }
  231. return false;
  232. }
  233. static bool ioport_event_end(struct perf_evsel *evsel,
  234. struct perf_sample *sample __maybe_unused,
  235. struct event_key *key __maybe_unused)
  236. {
  237. return kvm_entry_event(evsel);
  238. }
  239. static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
  240. struct event_key *key,
  241. char decode[20])
  242. {
  243. scnprintf(decode, 20, "%#llx:%s", (unsigned long long)key->key,
  244. key->info ? "POUT" : "PIN");
  245. }
  246. static struct kvm_events_ops ioport_events = {
  247. .is_begin_event = ioport_event_begin,
  248. .is_end_event = ioport_event_end,
  249. .decode_key = ioport_event_decode_key,
  250. .name = "IO Port Access"
  251. };
  252. static bool register_kvm_events_ops(struct perf_kvm_stat *kvm)
  253. {
  254. bool ret = true;
  255. if (!strcmp(kvm->report_event, "vmexit"))
  256. kvm->events_ops = &exit_events;
  257. else if (!strcmp(kvm->report_event, "mmio"))
  258. kvm->events_ops = &mmio_events;
  259. else if (!strcmp(kvm->report_event, "ioport"))
  260. kvm->events_ops = &ioport_events;
  261. else {
  262. pr_err("Unknown report event:%s\n", kvm->report_event);
  263. ret = false;
  264. }
  265. return ret;
  266. }
  267. struct vcpu_event_record {
  268. int vcpu_id;
  269. u64 start_time;
  270. struct kvm_event *last_event;
  271. };
  272. static void init_kvm_event_record(struct perf_kvm_stat *kvm)
  273. {
  274. unsigned int i;
  275. for (i = 0; i < EVENTS_CACHE_SIZE; i++)
  276. INIT_LIST_HEAD(&kvm->kvm_events_cache[i]);
  277. }
  278. #ifdef HAVE_TIMERFD_SUPPORT
  279. static void clear_events_cache_stats(struct list_head *kvm_events_cache)
  280. {
  281. struct list_head *head;
  282. struct kvm_event *event;
  283. unsigned int i;
  284. int j;
  285. for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
  286. head = &kvm_events_cache[i];
  287. list_for_each_entry(event, head, hash_entry) {
  288. /* reset stats for event */
  289. event->total.time = 0;
  290. init_stats(&event->total.stats);
  291. for (j = 0; j < event->max_vcpu; ++j) {
  292. event->vcpu[j].time = 0;
  293. init_stats(&event->vcpu[j].stats);
  294. }
  295. }
  296. }
  297. }
  298. #endif
  299. static int kvm_events_hash_fn(u64 key)
  300. {
  301. return key & (EVENTS_CACHE_SIZE - 1);
  302. }
  303. static bool kvm_event_expand(struct kvm_event *event, int vcpu_id)
  304. {
  305. int old_max_vcpu = event->max_vcpu;
  306. void *prev;
  307. if (vcpu_id < event->max_vcpu)
  308. return true;
  309. while (event->max_vcpu <= vcpu_id)
  310. event->max_vcpu += DEFAULT_VCPU_NUM;
  311. prev = event->vcpu;
  312. event->vcpu = realloc(event->vcpu,
  313. event->max_vcpu * sizeof(*event->vcpu));
  314. if (!event->vcpu) {
  315. free(prev);
  316. pr_err("Not enough memory\n");
  317. return false;
  318. }
  319. memset(event->vcpu + old_max_vcpu, 0,
  320. (event->max_vcpu - old_max_vcpu) * sizeof(*event->vcpu));
  321. return true;
  322. }
  323. static struct kvm_event *kvm_alloc_init_event(struct event_key *key)
  324. {
  325. struct kvm_event *event;
  326. event = zalloc(sizeof(*event));
  327. if (!event) {
  328. pr_err("Not enough memory\n");
  329. return NULL;
  330. }
  331. event->key = *key;
  332. init_stats(&event->total.stats);
  333. return event;
  334. }
  335. static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
  336. struct event_key *key)
  337. {
  338. struct kvm_event *event;
  339. struct list_head *head;
  340. BUG_ON(key->key == INVALID_KEY);
  341. head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
  342. list_for_each_entry(event, head, hash_entry) {
  343. if (event->key.key == key->key && event->key.info == key->info)
  344. return event;
  345. }
  346. event = kvm_alloc_init_event(key);
  347. if (!event)
  348. return NULL;
  349. list_add(&event->hash_entry, head);
  350. return event;
  351. }
  352. static bool handle_begin_event(struct perf_kvm_stat *kvm,
  353. struct vcpu_event_record *vcpu_record,
  354. struct event_key *key, u64 timestamp)
  355. {
  356. struct kvm_event *event = NULL;
  357. if (key->key != INVALID_KEY)
  358. event = find_create_kvm_event(kvm, key);
  359. vcpu_record->last_event = event;
  360. vcpu_record->start_time = timestamp;
  361. return true;
  362. }
  363. static void
  364. kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
  365. {
  366. kvm_stats->time += time_diff;
  367. update_stats(&kvm_stats->stats, time_diff);
  368. }
  369. static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
  370. {
  371. struct kvm_event_stats *kvm_stats = &event->total;
  372. if (vcpu_id != -1)
  373. kvm_stats = &event->vcpu[vcpu_id];
  374. return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
  375. avg_stats(&kvm_stats->stats));
  376. }
  377. static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
  378. u64 time_diff)
  379. {
  380. if (vcpu_id == -1) {
  381. kvm_update_event_stats(&event->total, time_diff);
  382. return true;
  383. }
  384. if (!kvm_event_expand(event, vcpu_id))
  385. return false;
  386. kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
  387. return true;
  388. }
  389. static bool handle_end_event(struct perf_kvm_stat *kvm,
  390. struct vcpu_event_record *vcpu_record,
  391. struct event_key *key,
  392. struct perf_sample *sample)
  393. {
  394. struct kvm_event *event;
  395. u64 time_begin, time_diff;
  396. int vcpu;
  397. if (kvm->trace_vcpu == -1)
  398. vcpu = -1;
  399. else
  400. vcpu = vcpu_record->vcpu_id;
  401. event = vcpu_record->last_event;
  402. time_begin = vcpu_record->start_time;
  403. /* The begin event is not caught. */
  404. if (!time_begin)
  405. return true;
  406. /*
  407. * In some case, the 'begin event' only records the start timestamp,
  408. * the actual event is recognized in the 'end event' (e.g. mmio-event).
  409. */
  410. /* Both begin and end events did not get the key. */
  411. if (!event && key->key == INVALID_KEY)
  412. return true;
  413. if (!event)
  414. event = find_create_kvm_event(kvm, key);
  415. if (!event)
  416. return false;
  417. vcpu_record->last_event = NULL;
  418. vcpu_record->start_time = 0;
  419. /* seems to happen once in a while during live mode */
  420. if (sample->time < time_begin) {
  421. pr_debug("End time before begin time; skipping event.\n");
  422. return true;
  423. }
  424. time_diff = sample->time - time_begin;
  425. if (kvm->duration && time_diff > kvm->duration) {
  426. char decode[32];
  427. kvm->events_ops->decode_key(kvm, &event->key, decode);
  428. if (strcmp(decode, "HLT")) {
  429. pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
  430. sample->time, sample->pid, vcpu_record->vcpu_id,
  431. decode, time_diff/1000);
  432. }
  433. }
  434. return update_kvm_event(event, vcpu, time_diff);
  435. }
  436. static
  437. struct vcpu_event_record *per_vcpu_record(struct thread *thread,
  438. struct perf_evsel *evsel,
  439. struct perf_sample *sample)
  440. {
  441. /* Only kvm_entry records vcpu id. */
  442. if (!thread->priv && kvm_entry_event(evsel)) {
  443. struct vcpu_event_record *vcpu_record;
  444. vcpu_record = zalloc(sizeof(*vcpu_record));
  445. if (!vcpu_record) {
  446. pr_err("%s: Not enough memory\n", __func__);
  447. return NULL;
  448. }
  449. vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
  450. thread->priv = vcpu_record;
  451. }
  452. return thread->priv;
  453. }
  454. static bool handle_kvm_event(struct perf_kvm_stat *kvm,
  455. struct thread *thread,
  456. struct perf_evsel *evsel,
  457. struct perf_sample *sample)
  458. {
  459. struct vcpu_event_record *vcpu_record;
  460. struct event_key key = {.key = INVALID_KEY};
  461. vcpu_record = per_vcpu_record(thread, evsel, sample);
  462. if (!vcpu_record)
  463. return true;
  464. /* only process events for vcpus user cares about */
  465. if ((kvm->trace_vcpu != -1) &&
  466. (kvm->trace_vcpu != vcpu_record->vcpu_id))
  467. return true;
  468. if (kvm->events_ops->is_begin_event(evsel, sample, &key))
  469. return handle_begin_event(kvm, vcpu_record, &key, sample->time);
  470. if (kvm->events_ops->is_end_event(evsel, sample, &key))
  471. return handle_end_event(kvm, vcpu_record, &key, sample);
  472. return true;
  473. }
  474. #define GET_EVENT_KEY(func, field) \
  475. static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
  476. { \
  477. if (vcpu == -1) \
  478. return event->total.field; \
  479. \
  480. if (vcpu >= event->max_vcpu) \
  481. return 0; \
  482. \
  483. return event->vcpu[vcpu].field; \
  484. }
  485. #define COMPARE_EVENT_KEY(func, field) \
  486. GET_EVENT_KEY(func, field) \
  487. static int compare_kvm_event_ ## func(struct kvm_event *one, \
  488. struct kvm_event *two, int vcpu)\
  489. { \
  490. return get_event_ ##func(one, vcpu) > \
  491. get_event_ ##func(two, vcpu); \
  492. }
  493. GET_EVENT_KEY(time, time);
  494. COMPARE_EVENT_KEY(count, stats.n);
  495. COMPARE_EVENT_KEY(mean, stats.mean);
  496. GET_EVENT_KEY(max, stats.max);
  497. GET_EVENT_KEY(min, stats.min);
  498. #define DEF_SORT_NAME_KEY(name, compare_key) \
  499. { #name, compare_kvm_event_ ## compare_key }
  500. static struct kvm_event_key keys[] = {
  501. DEF_SORT_NAME_KEY(sample, count),
  502. DEF_SORT_NAME_KEY(time, mean),
  503. { NULL, NULL }
  504. };
  505. static bool select_key(struct perf_kvm_stat *kvm)
  506. {
  507. int i;
  508. for (i = 0; keys[i].name; i++) {
  509. if (!strcmp(keys[i].name, kvm->sort_key)) {
  510. kvm->compare = keys[i].key;
  511. return true;
  512. }
  513. }
  514. pr_err("Unknown compare key:%s\n", kvm->sort_key);
  515. return false;
  516. }
  517. static void insert_to_result(struct rb_root *result, struct kvm_event *event,
  518. key_cmp_fun bigger, int vcpu)
  519. {
  520. struct rb_node **rb = &result->rb_node;
  521. struct rb_node *parent = NULL;
  522. struct kvm_event *p;
  523. while (*rb) {
  524. p = container_of(*rb, struct kvm_event, rb);
  525. parent = *rb;
  526. if (bigger(event, p, vcpu))
  527. rb = &(*rb)->rb_left;
  528. else
  529. rb = &(*rb)->rb_right;
  530. }
  531. rb_link_node(&event->rb, parent, rb);
  532. rb_insert_color(&event->rb, result);
  533. }
  534. static void
  535. update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
  536. {
  537. int vcpu = kvm->trace_vcpu;
  538. kvm->total_count += get_event_count(event, vcpu);
  539. kvm->total_time += get_event_time(event, vcpu);
  540. }
  541. static bool event_is_valid(struct kvm_event *event, int vcpu)
  542. {
  543. return !!get_event_count(event, vcpu);
  544. }
  545. static void sort_result(struct perf_kvm_stat *kvm)
  546. {
  547. unsigned int i;
  548. int vcpu = kvm->trace_vcpu;
  549. struct kvm_event *event;
  550. for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
  551. list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
  552. if (event_is_valid(event, vcpu)) {
  553. update_total_count(kvm, event);
  554. insert_to_result(&kvm->result, event,
  555. kvm->compare, vcpu);
  556. }
  557. }
  558. }
  559. }
  560. /* returns left most element of result, and erase it */
  561. static struct kvm_event *pop_from_result(struct rb_root *result)
  562. {
  563. struct rb_node *node = rb_first(result);
  564. if (!node)
  565. return NULL;
  566. rb_erase(node, result);
  567. return container_of(node, struct kvm_event, rb);
  568. }
  569. static void print_vcpu_info(struct perf_kvm_stat *kvm)
  570. {
  571. int vcpu = kvm->trace_vcpu;
  572. pr_info("Analyze events for ");
  573. if (kvm->live) {
  574. if (kvm->opts.target.system_wide)
  575. pr_info("all VMs, ");
  576. else if (kvm->opts.target.pid)
  577. pr_info("pid(s) %s, ", kvm->opts.target.pid);
  578. else
  579. pr_info("dazed and confused on what is monitored, ");
  580. }
  581. if (vcpu == -1)
  582. pr_info("all VCPUs:\n\n");
  583. else
  584. pr_info("VCPU %d:\n\n", vcpu);
  585. }
  586. static void show_timeofday(void)
  587. {
  588. char date[64];
  589. struct timeval tv;
  590. struct tm ltime;
  591. gettimeofday(&tv, NULL);
  592. if (localtime_r(&tv.tv_sec, &ltime)) {
  593. strftime(date, sizeof(date), "%H:%M:%S", &ltime);
  594. pr_info("%s.%06ld", date, tv.tv_usec);
  595. } else
  596. pr_info("00:00:00.000000");
  597. return;
  598. }
  599. static void print_result(struct perf_kvm_stat *kvm)
  600. {
  601. char decode[20];
  602. struct kvm_event *event;
  603. int vcpu = kvm->trace_vcpu;
  604. if (kvm->live) {
  605. puts(CONSOLE_CLEAR);
  606. show_timeofday();
  607. }
  608. pr_info("\n\n");
  609. print_vcpu_info(kvm);
  610. pr_info("%20s ", kvm->events_ops->name);
  611. pr_info("%10s ", "Samples");
  612. pr_info("%9s ", "Samples%");
  613. pr_info("%9s ", "Time%");
  614. pr_info("%10s ", "Min Time");
  615. pr_info("%10s ", "Max Time");
  616. pr_info("%16s ", "Avg time");
  617. pr_info("\n\n");
  618. while ((event = pop_from_result(&kvm->result))) {
  619. u64 ecount, etime, max, min;
  620. ecount = get_event_count(event, vcpu);
  621. etime = get_event_time(event, vcpu);
  622. max = get_event_max(event, vcpu);
  623. min = get_event_min(event, vcpu);
  624. kvm->events_ops->decode_key(kvm, &event->key, decode);
  625. pr_info("%20s ", decode);
  626. pr_info("%10llu ", (unsigned long long)ecount);
  627. pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
  628. pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
  629. pr_info("%8" PRIu64 "us ", min / 1000);
  630. pr_info("%8" PRIu64 "us ", max / 1000);
  631. pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
  632. kvm_event_rel_stddev(vcpu, event));
  633. pr_info("\n");
  634. }
  635. pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
  636. kvm->total_count, kvm->total_time / 1e3);
  637. if (kvm->lost_events)
  638. pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
  639. }
  640. #ifdef HAVE_TIMERFD_SUPPORT
  641. static int process_lost_event(struct perf_tool *tool,
  642. union perf_event *event __maybe_unused,
  643. struct perf_sample *sample __maybe_unused,
  644. struct machine *machine __maybe_unused)
  645. {
  646. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
  647. kvm->lost_events++;
  648. return 0;
  649. }
  650. #endif
  651. static bool skip_sample(struct perf_kvm_stat *kvm,
  652. struct perf_sample *sample)
  653. {
  654. if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
  655. return true;
  656. return false;
  657. }
  658. static int process_sample_event(struct perf_tool *tool,
  659. union perf_event *event,
  660. struct perf_sample *sample,
  661. struct perf_evsel *evsel,
  662. struct machine *machine)
  663. {
  664. struct thread *thread;
  665. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
  666. tool);
  667. if (skip_sample(kvm, sample))
  668. return 0;
  669. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  670. if (thread == NULL) {
  671. pr_debug("problem processing %d event, skipping it.\n",
  672. event->header.type);
  673. return -1;
  674. }
  675. if (!handle_kvm_event(kvm, thread, evsel, sample))
  676. return -1;
  677. return 0;
  678. }
  679. static int cpu_isa_config(struct perf_kvm_stat *kvm)
  680. {
  681. char buf[64], *cpuid;
  682. int err, isa;
  683. if (kvm->live) {
  684. err = get_cpuid(buf, sizeof(buf));
  685. if (err != 0) {
  686. pr_err("Failed to look up CPU type (Intel or AMD)\n");
  687. return err;
  688. }
  689. cpuid = buf;
  690. } else
  691. cpuid = kvm->session->header.env.cpuid;
  692. if (strstr(cpuid, "Intel"))
  693. isa = 1;
  694. else if (strstr(cpuid, "AMD"))
  695. isa = 0;
  696. else {
  697. pr_err("CPU %s is not supported.\n", cpuid);
  698. return -ENOTSUP;
  699. }
  700. if (isa == 1) {
  701. kvm->exit_reasons = vmx_exit_reasons;
  702. kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
  703. kvm->exit_reasons_isa = "VMX";
  704. }
  705. return 0;
  706. }
  707. static bool verify_vcpu(int vcpu)
  708. {
  709. if (vcpu != -1 && vcpu < 0) {
  710. pr_err("Invalid vcpu:%d.\n", vcpu);
  711. return false;
  712. }
  713. return true;
  714. }
  715. #ifdef HAVE_TIMERFD_SUPPORT
  716. /* keeping the max events to a modest level to keep
  717. * the processing of samples per mmap smooth.
  718. */
  719. #define PERF_KVM__MAX_EVENTS_PER_MMAP 25
  720. static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
  721. u64 *mmap_time)
  722. {
  723. union perf_event *event;
  724. struct perf_sample sample;
  725. s64 n = 0;
  726. int err;
  727. *mmap_time = ULLONG_MAX;
  728. while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
  729. err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
  730. if (err) {
  731. perf_evlist__mmap_consume(kvm->evlist, idx);
  732. pr_err("Failed to parse sample\n");
  733. return -1;
  734. }
  735. err = perf_session_queue_event(kvm->session, event, &sample, 0);
  736. /*
  737. * FIXME: Here we can't consume the event, as perf_session_queue_event will
  738. * point to it, and it'll get possibly overwritten by the kernel.
  739. */
  740. perf_evlist__mmap_consume(kvm->evlist, idx);
  741. if (err) {
  742. pr_err("Failed to enqueue sample: %d\n", err);
  743. return -1;
  744. }
  745. /* save time stamp of our first sample for this mmap */
  746. if (n == 0)
  747. *mmap_time = sample.time;
  748. /* limit events per mmap handled all at once */
  749. n++;
  750. if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
  751. break;
  752. }
  753. return n;
  754. }
  755. static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
  756. {
  757. int i, err, throttled = 0;
  758. s64 n, ntotal = 0;
  759. u64 flush_time = ULLONG_MAX, mmap_time;
  760. for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
  761. n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
  762. if (n < 0)
  763. return -1;
  764. /* flush time is going to be the minimum of all the individual
  765. * mmap times. Essentially, we flush all the samples queued up
  766. * from the last pass under our minimal start time -- that leaves
  767. * a very small race for samples to come in with a lower timestamp.
  768. * The ioctl to return the perf_clock timestamp should close the
  769. * race entirely.
  770. */
  771. if (mmap_time < flush_time)
  772. flush_time = mmap_time;
  773. ntotal += n;
  774. if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
  775. throttled = 1;
  776. }
  777. /* flush queue after each round in which we processed events */
  778. if (ntotal) {
  779. kvm->session->ordered_samples.next_flush = flush_time;
  780. err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session);
  781. if (err) {
  782. if (kvm->lost_events)
  783. pr_info("\nLost events: %" PRIu64 "\n\n",
  784. kvm->lost_events);
  785. return err;
  786. }
  787. }
  788. return throttled;
  789. }
  790. static volatile int done;
  791. static void sig_handler(int sig __maybe_unused)
  792. {
  793. done = 1;
  794. }
  795. static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
  796. {
  797. struct itimerspec new_value;
  798. int rc = -1;
  799. kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
  800. if (kvm->timerfd < 0) {
  801. pr_err("timerfd_create failed\n");
  802. goto out;
  803. }
  804. new_value.it_value.tv_sec = kvm->display_time;
  805. new_value.it_value.tv_nsec = 0;
  806. new_value.it_interval.tv_sec = kvm->display_time;
  807. new_value.it_interval.tv_nsec = 0;
  808. if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
  809. pr_err("timerfd_settime failed: %d\n", errno);
  810. close(kvm->timerfd);
  811. goto out;
  812. }
  813. rc = 0;
  814. out:
  815. return rc;
  816. }
  817. static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
  818. {
  819. uint64_t c;
  820. int rc;
  821. rc = read(kvm->timerfd, &c, sizeof(uint64_t));
  822. if (rc < 0) {
  823. if (errno == EAGAIN)
  824. return 0;
  825. pr_err("Failed to read timer fd: %d\n", errno);
  826. return -1;
  827. }
  828. if (rc != sizeof(uint64_t)) {
  829. pr_err("Error reading timer fd - invalid size returned\n");
  830. return -1;
  831. }
  832. if (c != 1)
  833. pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
  834. /* update display */
  835. sort_result(kvm);
  836. print_result(kvm);
  837. /* reset counts */
  838. clear_events_cache_stats(kvm->kvm_events_cache);
  839. kvm->total_count = 0;
  840. kvm->total_time = 0;
  841. kvm->lost_events = 0;
  842. return 0;
  843. }
  844. static int fd_set_nonblock(int fd)
  845. {
  846. long arg = 0;
  847. arg = fcntl(fd, F_GETFL);
  848. if (arg < 0) {
  849. pr_err("Failed to get current flags for fd %d\n", fd);
  850. return -1;
  851. }
  852. if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
  853. pr_err("Failed to set non-block option on fd %d\n", fd);
  854. return -1;
  855. }
  856. return 0;
  857. }
  858. static
  859. int perf_kvm__handle_stdin(struct termios *tc_now, struct termios *tc_save)
  860. {
  861. int c;
  862. tcsetattr(0, TCSANOW, tc_now);
  863. c = getc(stdin);
  864. tcsetattr(0, TCSAFLUSH, tc_save);
  865. if (c == 'q')
  866. return 1;
  867. return 0;
  868. }
  869. static int kvm_events_live_report(struct perf_kvm_stat *kvm)
  870. {
  871. struct pollfd *pollfds = NULL;
  872. int nr_fds, nr_stdin, ret, err = -EINVAL;
  873. struct termios tc, save;
  874. /* live flag must be set first */
  875. kvm->live = true;
  876. ret = cpu_isa_config(kvm);
  877. if (ret < 0)
  878. return ret;
  879. if (!verify_vcpu(kvm->trace_vcpu) ||
  880. !select_key(kvm) ||
  881. !register_kvm_events_ops(kvm)) {
  882. goto out;
  883. }
  884. init_kvm_event_record(kvm);
  885. tcgetattr(0, &save);
  886. tc = save;
  887. tc.c_lflag &= ~(ICANON | ECHO);
  888. tc.c_cc[VMIN] = 0;
  889. tc.c_cc[VTIME] = 0;
  890. signal(SIGINT, sig_handler);
  891. signal(SIGTERM, sig_handler);
  892. /* copy pollfds -- need to add timerfd and stdin */
  893. nr_fds = kvm->evlist->nr_fds;
  894. pollfds = zalloc(sizeof(struct pollfd) * (nr_fds + 2));
  895. if (!pollfds) {
  896. err = -ENOMEM;
  897. goto out;
  898. }
  899. memcpy(pollfds, kvm->evlist->pollfd,
  900. sizeof(struct pollfd) * kvm->evlist->nr_fds);
  901. /* add timer fd */
  902. if (perf_kvm__timerfd_create(kvm) < 0) {
  903. err = -1;
  904. goto out;
  905. }
  906. pollfds[nr_fds].fd = kvm->timerfd;
  907. pollfds[nr_fds].events = POLLIN;
  908. nr_fds++;
  909. pollfds[nr_fds].fd = fileno(stdin);
  910. pollfds[nr_fds].events = POLLIN;
  911. nr_stdin = nr_fds;
  912. nr_fds++;
  913. if (fd_set_nonblock(fileno(stdin)) != 0)
  914. goto out;
  915. /* everything is good - enable the events and process */
  916. perf_evlist__enable(kvm->evlist);
  917. while (!done) {
  918. int rc;
  919. rc = perf_kvm__mmap_read(kvm);
  920. if (rc < 0)
  921. break;
  922. err = perf_kvm__handle_timerfd(kvm);
  923. if (err)
  924. goto out;
  925. if (pollfds[nr_stdin].revents & POLLIN)
  926. done = perf_kvm__handle_stdin(&tc, &save);
  927. if (!rc && !done)
  928. err = poll(pollfds, nr_fds, 100);
  929. }
  930. perf_evlist__disable(kvm->evlist);
  931. if (err == 0) {
  932. sort_result(kvm);
  933. print_result(kvm);
  934. }
  935. out:
  936. if (kvm->timerfd >= 0)
  937. close(kvm->timerfd);
  938. free(pollfds);
  939. return err;
  940. }
  941. static int kvm_live_open_events(struct perf_kvm_stat *kvm)
  942. {
  943. int err, rc = -1;
  944. struct perf_evsel *pos;
  945. struct perf_evlist *evlist = kvm->evlist;
  946. perf_evlist__config(evlist, &kvm->opts);
  947. /*
  948. * Note: exclude_{guest,host} do not apply here.
  949. * This command processes KVM tracepoints from host only
  950. */
  951. evlist__for_each(evlist, pos) {
  952. struct perf_event_attr *attr = &pos->attr;
  953. /* make sure these *are* set */
  954. perf_evsel__set_sample_bit(pos, TID);
  955. perf_evsel__set_sample_bit(pos, TIME);
  956. perf_evsel__set_sample_bit(pos, CPU);
  957. perf_evsel__set_sample_bit(pos, RAW);
  958. /* make sure these are *not*; want as small a sample as possible */
  959. perf_evsel__reset_sample_bit(pos, PERIOD);
  960. perf_evsel__reset_sample_bit(pos, IP);
  961. perf_evsel__reset_sample_bit(pos, CALLCHAIN);
  962. perf_evsel__reset_sample_bit(pos, ADDR);
  963. perf_evsel__reset_sample_bit(pos, READ);
  964. attr->mmap = 0;
  965. attr->comm = 0;
  966. attr->task = 0;
  967. attr->sample_period = 1;
  968. attr->watermark = 0;
  969. attr->wakeup_events = 1000;
  970. /* will enable all once we are ready */
  971. attr->disabled = 1;
  972. }
  973. err = perf_evlist__open(evlist);
  974. if (err < 0) {
  975. printf("Couldn't create the events: %s\n", strerror(errno));
  976. goto out;
  977. }
  978. if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
  979. ui__error("Failed to mmap the events: %s\n", strerror(errno));
  980. perf_evlist__close(evlist);
  981. goto out;
  982. }
  983. rc = 0;
  984. out:
  985. return rc;
  986. }
  987. #endif
  988. static int read_events(struct perf_kvm_stat *kvm)
  989. {
  990. int ret;
  991. struct perf_tool eops = {
  992. .sample = process_sample_event,
  993. .comm = perf_event__process_comm,
  994. .ordered_samples = true,
  995. };
  996. struct perf_data_file file = {
  997. .path = kvm->file_name,
  998. .mode = PERF_DATA_MODE_READ,
  999. };
  1000. kvm->tool = eops;
  1001. kvm->session = perf_session__new(&file, false, &kvm->tool);
  1002. if (!kvm->session) {
  1003. pr_err("Initializing perf session failed\n");
  1004. return -EINVAL;
  1005. }
  1006. if (!perf_session__has_traces(kvm->session, "kvm record"))
  1007. return -EINVAL;
  1008. /*
  1009. * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
  1010. * traced in the old kernel.
  1011. */
  1012. ret = cpu_isa_config(kvm);
  1013. if (ret < 0)
  1014. return ret;
  1015. return perf_session__process_events(kvm->session, &kvm->tool);
  1016. }
  1017. static int parse_target_str(struct perf_kvm_stat *kvm)
  1018. {
  1019. if (kvm->pid_str) {
  1020. kvm->pid_list = intlist__new(kvm->pid_str);
  1021. if (kvm->pid_list == NULL) {
  1022. pr_err("Error parsing process id string\n");
  1023. return -EINVAL;
  1024. }
  1025. }
  1026. return 0;
  1027. }
  1028. static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
  1029. {
  1030. int ret = -EINVAL;
  1031. int vcpu = kvm->trace_vcpu;
  1032. if (parse_target_str(kvm) != 0)
  1033. goto exit;
  1034. if (!verify_vcpu(vcpu))
  1035. goto exit;
  1036. if (!select_key(kvm))
  1037. goto exit;
  1038. if (!register_kvm_events_ops(kvm))
  1039. goto exit;
  1040. init_kvm_event_record(kvm);
  1041. setup_pager();
  1042. ret = read_events(kvm);
  1043. if (ret)
  1044. goto exit;
  1045. sort_result(kvm);
  1046. print_result(kvm);
  1047. exit:
  1048. return ret;
  1049. }
  1050. static const char * const kvm_events_tp[] = {
  1051. "kvm:kvm_entry",
  1052. "kvm:kvm_exit",
  1053. "kvm:kvm_mmio",
  1054. "kvm:kvm_pio",
  1055. };
  1056. #define STRDUP_FAIL_EXIT(s) \
  1057. ({ char *_p; \
  1058. _p = strdup(s); \
  1059. if (!_p) \
  1060. return -ENOMEM; \
  1061. _p; \
  1062. })
  1063. static int
  1064. kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
  1065. {
  1066. unsigned int rec_argc, i, j;
  1067. const char **rec_argv;
  1068. const char * const record_args[] = {
  1069. "record",
  1070. "-R",
  1071. "-m", "1024",
  1072. "-c", "1",
  1073. };
  1074. rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
  1075. 2 * ARRAY_SIZE(kvm_events_tp);
  1076. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1077. if (rec_argv == NULL)
  1078. return -ENOMEM;
  1079. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1080. rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
  1081. for (j = 0; j < ARRAY_SIZE(kvm_events_tp); j++) {
  1082. rec_argv[i++] = "-e";
  1083. rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
  1084. }
  1085. rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
  1086. rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
  1087. for (j = 1; j < (unsigned int)argc; j++, i++)
  1088. rec_argv[i] = argv[j];
  1089. return cmd_record(i, rec_argv, NULL);
  1090. }
  1091. static int
  1092. kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
  1093. {
  1094. const struct option kvm_events_report_options[] = {
  1095. OPT_STRING(0, "event", &kvm->report_event, "report event",
  1096. "event for reporting: vmexit, mmio, ioport"),
  1097. OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
  1098. "vcpu id to report"),
  1099. OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
  1100. "key for sorting: sample(sort by samples number)"
  1101. " time (sort by avg time)"),
  1102. OPT_STRING('p', "pid", &kvm->pid_str, "pid",
  1103. "analyze events only for given process id(s)"),
  1104. OPT_END()
  1105. };
  1106. const char * const kvm_events_report_usage[] = {
  1107. "perf kvm stat report [<options>]",
  1108. NULL
  1109. };
  1110. symbol__init();
  1111. if (argc) {
  1112. argc = parse_options(argc, argv,
  1113. kvm_events_report_options,
  1114. kvm_events_report_usage, 0);
  1115. if (argc)
  1116. usage_with_options(kvm_events_report_usage,
  1117. kvm_events_report_options);
  1118. }
  1119. return kvm_events_report_vcpu(kvm);
  1120. }
  1121. #ifdef HAVE_TIMERFD_SUPPORT
  1122. static struct perf_evlist *kvm_live_event_list(void)
  1123. {
  1124. struct perf_evlist *evlist;
  1125. char *tp, *name, *sys;
  1126. unsigned int j;
  1127. int err = -1;
  1128. evlist = perf_evlist__new();
  1129. if (evlist == NULL)
  1130. return NULL;
  1131. for (j = 0; j < ARRAY_SIZE(kvm_events_tp); j++) {
  1132. tp = strdup(kvm_events_tp[j]);
  1133. if (tp == NULL)
  1134. goto out;
  1135. /* split tracepoint into subsystem and name */
  1136. sys = tp;
  1137. name = strchr(tp, ':');
  1138. if (name == NULL) {
  1139. pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
  1140. kvm_events_tp[j]);
  1141. free(tp);
  1142. goto out;
  1143. }
  1144. *name = '\0';
  1145. name++;
  1146. if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
  1147. pr_err("Failed to add %s tracepoint to the list\n", kvm_events_tp[j]);
  1148. free(tp);
  1149. goto out;
  1150. }
  1151. free(tp);
  1152. }
  1153. err = 0;
  1154. out:
  1155. if (err) {
  1156. perf_evlist__delete(evlist);
  1157. evlist = NULL;
  1158. }
  1159. return evlist;
  1160. }
  1161. static int kvm_events_live(struct perf_kvm_stat *kvm,
  1162. int argc, const char **argv)
  1163. {
  1164. char errbuf[BUFSIZ];
  1165. int err;
  1166. const struct option live_options[] = {
  1167. OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
  1168. "record events on existing process id"),
  1169. OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
  1170. "number of mmap data pages",
  1171. perf_evlist__parse_mmap_pages),
  1172. OPT_INCR('v', "verbose", &verbose,
  1173. "be more verbose (show counter open errors, etc)"),
  1174. OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
  1175. "system-wide collection from all CPUs"),
  1176. OPT_UINTEGER('d', "display", &kvm->display_time,
  1177. "time in seconds between display updates"),
  1178. OPT_STRING(0, "event", &kvm->report_event, "report event",
  1179. "event for reporting: vmexit, mmio, ioport"),
  1180. OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
  1181. "vcpu id to report"),
  1182. OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
  1183. "key for sorting: sample(sort by samples number)"
  1184. " time (sort by avg time)"),
  1185. OPT_U64(0, "duration", &kvm->duration,
  1186. "show events other than HALT that take longer than duration usecs"),
  1187. OPT_END()
  1188. };
  1189. const char * const live_usage[] = {
  1190. "perf kvm stat live [<options>]",
  1191. NULL
  1192. };
  1193. struct perf_data_file file = {
  1194. .mode = PERF_DATA_MODE_WRITE,
  1195. };
  1196. /* event handling */
  1197. kvm->tool.sample = process_sample_event;
  1198. kvm->tool.comm = perf_event__process_comm;
  1199. kvm->tool.exit = perf_event__process_exit;
  1200. kvm->tool.fork = perf_event__process_fork;
  1201. kvm->tool.lost = process_lost_event;
  1202. kvm->tool.ordered_samples = true;
  1203. perf_tool__fill_defaults(&kvm->tool);
  1204. /* set defaults */
  1205. kvm->display_time = 1;
  1206. kvm->opts.user_interval = 1;
  1207. kvm->opts.mmap_pages = 512;
  1208. kvm->opts.target.uses_mmap = false;
  1209. kvm->opts.target.uid_str = NULL;
  1210. kvm->opts.target.uid = UINT_MAX;
  1211. symbol__init();
  1212. disable_buildid_cache();
  1213. use_browser = 0;
  1214. setup_browser(false);
  1215. if (argc) {
  1216. argc = parse_options(argc, argv, live_options,
  1217. live_usage, 0);
  1218. if (argc)
  1219. usage_with_options(live_usage, live_options);
  1220. }
  1221. kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
  1222. /*
  1223. * target related setups
  1224. */
  1225. err = target__validate(&kvm->opts.target);
  1226. if (err) {
  1227. target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
  1228. ui__warning("%s", errbuf);
  1229. }
  1230. if (target__none(&kvm->opts.target))
  1231. kvm->opts.target.system_wide = true;
  1232. /*
  1233. * generate the event list
  1234. */
  1235. kvm->evlist = kvm_live_event_list();
  1236. if (kvm->evlist == NULL) {
  1237. err = -1;
  1238. goto out;
  1239. }
  1240. symbol_conf.nr_events = kvm->evlist->nr_entries;
  1241. if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
  1242. usage_with_options(live_usage, live_options);
  1243. /*
  1244. * perf session
  1245. */
  1246. kvm->session = perf_session__new(&file, false, &kvm->tool);
  1247. if (kvm->session == NULL) {
  1248. err = -ENOMEM;
  1249. goto out;
  1250. }
  1251. kvm->session->evlist = kvm->evlist;
  1252. perf_session__set_id_hdr_size(kvm->session);
  1253. machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
  1254. kvm->evlist->threads, false);
  1255. err = kvm_live_open_events(kvm);
  1256. if (err)
  1257. goto out;
  1258. err = kvm_events_live_report(kvm);
  1259. out:
  1260. exit_browser(0);
  1261. if (kvm->session)
  1262. perf_session__delete(kvm->session);
  1263. kvm->session = NULL;
  1264. if (kvm->evlist)
  1265. perf_evlist__delete(kvm->evlist);
  1266. return err;
  1267. }
  1268. #endif
  1269. static void print_kvm_stat_usage(void)
  1270. {
  1271. printf("Usage: perf kvm stat <command>\n\n");
  1272. printf("# Available commands:\n");
  1273. printf("\trecord: record kvm events\n");
  1274. printf("\treport: report statistical data of kvm events\n");
  1275. printf("\tlive: live reporting of statistical data of kvm events\n");
  1276. printf("\nOtherwise, it is the alias of 'perf stat':\n");
  1277. }
  1278. static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
  1279. {
  1280. struct perf_kvm_stat kvm = {
  1281. .file_name = file_name,
  1282. .trace_vcpu = -1,
  1283. .report_event = "vmexit",
  1284. .sort_key = "sample",
  1285. .exit_reasons = svm_exit_reasons,
  1286. .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
  1287. .exit_reasons_isa = "SVM",
  1288. };
  1289. if (argc == 1) {
  1290. print_kvm_stat_usage();
  1291. goto perf_stat;
  1292. }
  1293. if (!strncmp(argv[1], "rec", 3))
  1294. return kvm_events_record(&kvm, argc - 1, argv + 1);
  1295. if (!strncmp(argv[1], "rep", 3))
  1296. return kvm_events_report(&kvm, argc - 1 , argv + 1);
  1297. #ifdef HAVE_TIMERFD_SUPPORT
  1298. if (!strncmp(argv[1], "live", 4))
  1299. return kvm_events_live(&kvm, argc - 1 , argv + 1);
  1300. #endif
  1301. perf_stat:
  1302. return cmd_stat(argc, argv, NULL);
  1303. }
  1304. #endif
  1305. static int __cmd_record(const char *file_name, int argc, const char **argv)
  1306. {
  1307. int rec_argc, i = 0, j;
  1308. const char **rec_argv;
  1309. rec_argc = argc + 2;
  1310. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1311. rec_argv[i++] = strdup("record");
  1312. rec_argv[i++] = strdup("-o");
  1313. rec_argv[i++] = strdup(file_name);
  1314. for (j = 1; j < argc; j++, i++)
  1315. rec_argv[i] = argv[j];
  1316. BUG_ON(i != rec_argc);
  1317. return cmd_record(i, rec_argv, NULL);
  1318. }
  1319. static int __cmd_report(const char *file_name, int argc, const char **argv)
  1320. {
  1321. int rec_argc, i = 0, j;
  1322. const char **rec_argv;
  1323. rec_argc = argc + 2;
  1324. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1325. rec_argv[i++] = strdup("report");
  1326. rec_argv[i++] = strdup("-i");
  1327. rec_argv[i++] = strdup(file_name);
  1328. for (j = 1; j < argc; j++, i++)
  1329. rec_argv[i] = argv[j];
  1330. BUG_ON(i != rec_argc);
  1331. return cmd_report(i, rec_argv, NULL);
  1332. }
  1333. static int
  1334. __cmd_buildid_list(const char *file_name, int argc, const char **argv)
  1335. {
  1336. int rec_argc, i = 0, j;
  1337. const char **rec_argv;
  1338. rec_argc = argc + 2;
  1339. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1340. rec_argv[i++] = strdup("buildid-list");
  1341. rec_argv[i++] = strdup("-i");
  1342. rec_argv[i++] = strdup(file_name);
  1343. for (j = 1; j < argc; j++, i++)
  1344. rec_argv[i] = argv[j];
  1345. BUG_ON(i != rec_argc);
  1346. return cmd_buildid_list(i, rec_argv, NULL);
  1347. }
  1348. int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
  1349. {
  1350. const char *file_name = NULL;
  1351. const struct option kvm_options[] = {
  1352. OPT_STRING('i', "input", &file_name, "file",
  1353. "Input file name"),
  1354. OPT_STRING('o', "output", &file_name, "file",
  1355. "Output file name"),
  1356. OPT_BOOLEAN(0, "guest", &perf_guest,
  1357. "Collect guest os data"),
  1358. OPT_BOOLEAN(0, "host", &perf_host,
  1359. "Collect host os data"),
  1360. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  1361. "guest mount directory under which every guest os"
  1362. " instance has a subdir"),
  1363. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  1364. "file", "file saving guest os vmlinux"),
  1365. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  1366. "file", "file saving guest os /proc/kallsyms"),
  1367. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  1368. "file", "file saving guest os /proc/modules"),
  1369. OPT_INCR('v', "verbose", &verbose,
  1370. "be more verbose (show counter open errors, etc)"),
  1371. OPT_END()
  1372. };
  1373. const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
  1374. "buildid-list", "stat", NULL };
  1375. const char *kvm_usage[] = { NULL, NULL };
  1376. perf_host = 0;
  1377. perf_guest = 1;
  1378. argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
  1379. PARSE_OPT_STOP_AT_NON_OPTION);
  1380. if (!argc)
  1381. usage_with_options(kvm_usage, kvm_options);
  1382. if (!perf_host)
  1383. perf_guest = 1;
  1384. if (!file_name) {
  1385. file_name = get_filename_for_perf_kvm();
  1386. if (!file_name) {
  1387. pr_err("Failed to allocate memory for filename\n");
  1388. return -ENOMEM;
  1389. }
  1390. }
  1391. if (!strncmp(argv[0], "rec", 3))
  1392. return __cmd_record(file_name, argc, argv);
  1393. else if (!strncmp(argv[0], "rep", 3))
  1394. return __cmd_report(file_name, argc, argv);
  1395. else if (!strncmp(argv[0], "diff", 4))
  1396. return cmd_diff(argc, argv, NULL);
  1397. else if (!strncmp(argv[0], "top", 3))
  1398. return cmd_top(argc, argv, NULL);
  1399. else if (!strncmp(argv[0], "buildid-list", 12))
  1400. return __cmd_buildid_list(file_name, argc, argv);
  1401. #if defined(__i386__) || defined(__x86_64__)
  1402. else if (!strncmp(argv[0], "stat", 4))
  1403. return kvm_cmd_stat(file_name, argc, argv);
  1404. #endif
  1405. else
  1406. usage_with_options(kvm_usage, kvm_options);
  1407. return 0;
  1408. }