builtin-kvm.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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 <lk/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 perf_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. return event;
  333. }
  334. static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm,
  335. struct event_key *key)
  336. {
  337. struct kvm_event *event;
  338. struct list_head *head;
  339. BUG_ON(key->key == INVALID_KEY);
  340. head = &kvm->kvm_events_cache[kvm_events_hash_fn(key->key)];
  341. list_for_each_entry(event, head, hash_entry) {
  342. if (event->key.key == key->key && event->key.info == key->info)
  343. return event;
  344. }
  345. event = kvm_alloc_init_event(key);
  346. if (!event)
  347. return NULL;
  348. list_add(&event->hash_entry, head);
  349. return event;
  350. }
  351. static bool handle_begin_event(struct perf_kvm_stat *kvm,
  352. struct vcpu_event_record *vcpu_record,
  353. struct event_key *key, u64 timestamp)
  354. {
  355. struct kvm_event *event = NULL;
  356. if (key->key != INVALID_KEY)
  357. event = find_create_kvm_event(kvm, key);
  358. vcpu_record->last_event = event;
  359. vcpu_record->start_time = timestamp;
  360. return true;
  361. }
  362. static void
  363. kvm_update_event_stats(struct kvm_event_stats *kvm_stats, u64 time_diff)
  364. {
  365. kvm_stats->time += time_diff;
  366. update_stats(&kvm_stats->stats, time_diff);
  367. }
  368. static double kvm_event_rel_stddev(int vcpu_id, struct kvm_event *event)
  369. {
  370. struct kvm_event_stats *kvm_stats = &event->total;
  371. if (vcpu_id != -1)
  372. kvm_stats = &event->vcpu[vcpu_id];
  373. return rel_stddev_stats(stddev_stats(&kvm_stats->stats),
  374. avg_stats(&kvm_stats->stats));
  375. }
  376. static bool update_kvm_event(struct kvm_event *event, int vcpu_id,
  377. u64 time_diff)
  378. {
  379. if (vcpu_id == -1) {
  380. kvm_update_event_stats(&event->total, time_diff);
  381. return true;
  382. }
  383. if (!kvm_event_expand(event, vcpu_id))
  384. return false;
  385. kvm_update_event_stats(&event->vcpu[vcpu_id], time_diff);
  386. return true;
  387. }
  388. static bool handle_end_event(struct perf_kvm_stat *kvm,
  389. struct vcpu_event_record *vcpu_record,
  390. struct event_key *key,
  391. struct perf_sample *sample)
  392. {
  393. struct kvm_event *event;
  394. u64 time_begin, time_diff;
  395. int vcpu;
  396. if (kvm->trace_vcpu == -1)
  397. vcpu = -1;
  398. else
  399. vcpu = vcpu_record->vcpu_id;
  400. event = vcpu_record->last_event;
  401. time_begin = vcpu_record->start_time;
  402. /* The begin event is not caught. */
  403. if (!time_begin)
  404. return true;
  405. /*
  406. * In some case, the 'begin event' only records the start timestamp,
  407. * the actual event is recognized in the 'end event' (e.g. mmio-event).
  408. */
  409. /* Both begin and end events did not get the key. */
  410. if (!event && key->key == INVALID_KEY)
  411. return true;
  412. if (!event)
  413. event = find_create_kvm_event(kvm, key);
  414. if (!event)
  415. return false;
  416. vcpu_record->last_event = NULL;
  417. vcpu_record->start_time = 0;
  418. /* seems to happen once in a while during live mode */
  419. if (sample->time < time_begin) {
  420. pr_debug("End time before begin time; skipping event.\n");
  421. return true;
  422. }
  423. time_diff = sample->time - time_begin;
  424. if (kvm->duration && time_diff > kvm->duration) {
  425. char decode[32];
  426. kvm->events_ops->decode_key(kvm, &event->key, decode);
  427. if (strcmp(decode, "HLT")) {
  428. pr_info("%" PRIu64 " VM %d, vcpu %d: %s event took %" PRIu64 "usec\n",
  429. sample->time, sample->pid, vcpu_record->vcpu_id,
  430. decode, time_diff/1000);
  431. }
  432. }
  433. return update_kvm_event(event, vcpu, time_diff);
  434. }
  435. static
  436. struct vcpu_event_record *per_vcpu_record(struct thread *thread,
  437. struct perf_evsel *evsel,
  438. struct perf_sample *sample)
  439. {
  440. /* Only kvm_entry records vcpu id. */
  441. if (!thread->priv && kvm_entry_event(evsel)) {
  442. struct vcpu_event_record *vcpu_record;
  443. vcpu_record = zalloc(sizeof(*vcpu_record));
  444. if (!vcpu_record) {
  445. pr_err("%s: Not enough memory\n", __func__);
  446. return NULL;
  447. }
  448. vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, "vcpu_id");
  449. thread->priv = vcpu_record;
  450. }
  451. return thread->priv;
  452. }
  453. static bool handle_kvm_event(struct perf_kvm_stat *kvm,
  454. struct thread *thread,
  455. struct perf_evsel *evsel,
  456. struct perf_sample *sample)
  457. {
  458. struct vcpu_event_record *vcpu_record;
  459. struct event_key key = {.key = INVALID_KEY};
  460. vcpu_record = per_vcpu_record(thread, evsel, sample);
  461. if (!vcpu_record)
  462. return true;
  463. /* only process events for vcpus user cares about */
  464. if ((kvm->trace_vcpu != -1) &&
  465. (kvm->trace_vcpu != vcpu_record->vcpu_id))
  466. return true;
  467. if (kvm->events_ops->is_begin_event(evsel, sample, &key))
  468. return handle_begin_event(kvm, vcpu_record, &key, sample->time);
  469. if (kvm->events_ops->is_end_event(evsel, sample, &key))
  470. return handle_end_event(kvm, vcpu_record, &key, sample);
  471. return true;
  472. }
  473. #define GET_EVENT_KEY(func, field) \
  474. static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
  475. { \
  476. if (vcpu == -1) \
  477. return event->total.field; \
  478. \
  479. if (vcpu >= event->max_vcpu) \
  480. return 0; \
  481. \
  482. return event->vcpu[vcpu].field; \
  483. }
  484. #define COMPARE_EVENT_KEY(func, field) \
  485. GET_EVENT_KEY(func, field) \
  486. static int compare_kvm_event_ ## func(struct kvm_event *one, \
  487. struct kvm_event *two, int vcpu)\
  488. { \
  489. return get_event_ ##func(one, vcpu) > \
  490. get_event_ ##func(two, vcpu); \
  491. }
  492. GET_EVENT_KEY(time, time);
  493. COMPARE_EVENT_KEY(count, stats.n);
  494. COMPARE_EVENT_KEY(mean, stats.mean);
  495. GET_EVENT_KEY(max, stats.max);
  496. GET_EVENT_KEY(min, stats.min);
  497. #define DEF_SORT_NAME_KEY(name, compare_key) \
  498. { #name, compare_kvm_event_ ## compare_key }
  499. static struct kvm_event_key keys[] = {
  500. DEF_SORT_NAME_KEY(sample, count),
  501. DEF_SORT_NAME_KEY(time, mean),
  502. { NULL, NULL }
  503. };
  504. static bool select_key(struct perf_kvm_stat *kvm)
  505. {
  506. int i;
  507. for (i = 0; keys[i].name; i++) {
  508. if (!strcmp(keys[i].name, kvm->sort_key)) {
  509. kvm->compare = keys[i].key;
  510. return true;
  511. }
  512. }
  513. pr_err("Unknown compare key:%s\n", kvm->sort_key);
  514. return false;
  515. }
  516. static void insert_to_result(struct rb_root *result, struct kvm_event *event,
  517. key_cmp_fun bigger, int vcpu)
  518. {
  519. struct rb_node **rb = &result->rb_node;
  520. struct rb_node *parent = NULL;
  521. struct kvm_event *p;
  522. while (*rb) {
  523. p = container_of(*rb, struct kvm_event, rb);
  524. parent = *rb;
  525. if (bigger(event, p, vcpu))
  526. rb = &(*rb)->rb_left;
  527. else
  528. rb = &(*rb)->rb_right;
  529. }
  530. rb_link_node(&event->rb, parent, rb);
  531. rb_insert_color(&event->rb, result);
  532. }
  533. static void
  534. update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
  535. {
  536. int vcpu = kvm->trace_vcpu;
  537. kvm->total_count += get_event_count(event, vcpu);
  538. kvm->total_time += get_event_time(event, vcpu);
  539. }
  540. static bool event_is_valid(struct kvm_event *event, int vcpu)
  541. {
  542. return !!get_event_count(event, vcpu);
  543. }
  544. static void sort_result(struct perf_kvm_stat *kvm)
  545. {
  546. unsigned int i;
  547. int vcpu = kvm->trace_vcpu;
  548. struct kvm_event *event;
  549. for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
  550. list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
  551. if (event_is_valid(event, vcpu)) {
  552. update_total_count(kvm, event);
  553. insert_to_result(&kvm->result, event,
  554. kvm->compare, vcpu);
  555. }
  556. }
  557. }
  558. }
  559. /* returns left most element of result, and erase it */
  560. static struct kvm_event *pop_from_result(struct rb_root *result)
  561. {
  562. struct rb_node *node = rb_first(result);
  563. if (!node)
  564. return NULL;
  565. rb_erase(node, result);
  566. return container_of(node, struct kvm_event, rb);
  567. }
  568. static void print_vcpu_info(struct perf_kvm_stat *kvm)
  569. {
  570. int vcpu = kvm->trace_vcpu;
  571. pr_info("Analyze events for ");
  572. if (kvm->live) {
  573. if (kvm->opts.target.system_wide)
  574. pr_info("all VMs, ");
  575. else if (kvm->opts.target.pid)
  576. pr_info("pid(s) %s, ", kvm->opts.target.pid);
  577. else
  578. pr_info("dazed and confused on what is monitored, ");
  579. }
  580. if (vcpu == -1)
  581. pr_info("all VCPUs:\n\n");
  582. else
  583. pr_info("VCPU %d:\n\n", vcpu);
  584. }
  585. static void show_timeofday(void)
  586. {
  587. char date[64];
  588. struct timeval tv;
  589. struct tm ltime;
  590. gettimeofday(&tv, NULL);
  591. if (localtime_r(&tv.tv_sec, &ltime)) {
  592. strftime(date, sizeof(date), "%H:%M:%S", &ltime);
  593. pr_info("%s.%06ld", date, tv.tv_usec);
  594. } else
  595. pr_info("00:00:00.000000");
  596. return;
  597. }
  598. static void print_result(struct perf_kvm_stat *kvm)
  599. {
  600. char decode[20];
  601. struct kvm_event *event;
  602. int vcpu = kvm->trace_vcpu;
  603. if (kvm->live) {
  604. puts(CONSOLE_CLEAR);
  605. show_timeofday();
  606. }
  607. pr_info("\n\n");
  608. print_vcpu_info(kvm);
  609. pr_info("%20s ", kvm->events_ops->name);
  610. pr_info("%10s ", "Samples");
  611. pr_info("%9s ", "Samples%");
  612. pr_info("%9s ", "Time%");
  613. pr_info("%10s ", "Min Time");
  614. pr_info("%10s ", "Max Time");
  615. pr_info("%16s ", "Avg time");
  616. pr_info("\n\n");
  617. while ((event = pop_from_result(&kvm->result))) {
  618. u64 ecount, etime, max, min;
  619. ecount = get_event_count(event, vcpu);
  620. etime = get_event_time(event, vcpu);
  621. max = get_event_max(event, vcpu);
  622. min = get_event_min(event, vcpu);
  623. kvm->events_ops->decode_key(kvm, &event->key, decode);
  624. pr_info("%20s ", decode);
  625. pr_info("%10llu ", (unsigned long long)ecount);
  626. pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
  627. pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
  628. pr_info("%8" PRIu64 "us ", min / 1000);
  629. pr_info("%8" PRIu64 "us ", max / 1000);
  630. pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
  631. kvm_event_rel_stddev(vcpu, event));
  632. pr_info("\n");
  633. }
  634. pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
  635. kvm->total_count, kvm->total_time / 1e3);
  636. if (kvm->lost_events)
  637. pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
  638. }
  639. #ifdef HAVE_TIMERFD_SUPPORT
  640. static int process_lost_event(struct perf_tool *tool,
  641. union perf_event *event __maybe_unused,
  642. struct perf_sample *sample __maybe_unused,
  643. struct machine *machine __maybe_unused)
  644. {
  645. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
  646. kvm->lost_events++;
  647. return 0;
  648. }
  649. #endif
  650. static bool skip_sample(struct perf_kvm_stat *kvm,
  651. struct perf_sample *sample)
  652. {
  653. if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
  654. return true;
  655. return false;
  656. }
  657. static int process_sample_event(struct perf_tool *tool,
  658. union perf_event *event,
  659. struct perf_sample *sample,
  660. struct perf_evsel *evsel,
  661. struct machine *machine)
  662. {
  663. struct thread *thread;
  664. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
  665. tool);
  666. if (skip_sample(kvm, sample))
  667. return 0;
  668. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  669. if (thread == NULL) {
  670. pr_debug("problem processing %d event, skipping it.\n",
  671. event->header.type);
  672. return -1;
  673. }
  674. if (!handle_kvm_event(kvm, thread, evsel, sample))
  675. return -1;
  676. return 0;
  677. }
  678. static int cpu_isa_config(struct perf_kvm_stat *kvm)
  679. {
  680. char buf[64], *cpuid;
  681. int err, isa;
  682. if (kvm->live) {
  683. err = get_cpuid(buf, sizeof(buf));
  684. if (err != 0) {
  685. pr_err("Failed to look up CPU type (Intel or AMD)\n");
  686. return err;
  687. }
  688. cpuid = buf;
  689. } else
  690. cpuid = kvm->session->header.env.cpuid;
  691. if (strstr(cpuid, "Intel"))
  692. isa = 1;
  693. else if (strstr(cpuid, "AMD"))
  694. isa = 0;
  695. else {
  696. pr_err("CPU %s is not supported.\n", cpuid);
  697. return -ENOTSUP;
  698. }
  699. if (isa == 1) {
  700. kvm->exit_reasons = vmx_exit_reasons;
  701. kvm->exit_reasons_size = ARRAY_SIZE(vmx_exit_reasons);
  702. kvm->exit_reasons_isa = "VMX";
  703. }
  704. return 0;
  705. }
  706. static bool verify_vcpu(int vcpu)
  707. {
  708. if (vcpu != -1 && vcpu < 0) {
  709. pr_err("Invalid vcpu:%d.\n", vcpu);
  710. return false;
  711. }
  712. return true;
  713. }
  714. #ifdef HAVE_TIMERFD_SUPPORT
  715. /* keeping the max events to a modest level to keep
  716. * the processing of samples per mmap smooth.
  717. */
  718. #define PERF_KVM__MAX_EVENTS_PER_MMAP 25
  719. static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
  720. u64 *mmap_time)
  721. {
  722. union perf_event *event;
  723. struct perf_sample sample;
  724. s64 n = 0;
  725. int err;
  726. *mmap_time = ULLONG_MAX;
  727. while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
  728. err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
  729. if (err) {
  730. perf_evlist__mmap_consume(kvm->evlist, idx);
  731. pr_err("Failed to parse sample\n");
  732. return -1;
  733. }
  734. err = perf_session_queue_event(kvm->session, event, &sample, 0);
  735. /*
  736. * FIXME: Here we can't consume the event, as perf_session_queue_event will
  737. * point to it, and it'll get possibly overwritten by the kernel.
  738. */
  739. perf_evlist__mmap_consume(kvm->evlist, idx);
  740. if (err) {
  741. pr_err("Failed to enqueue sample: %d\n", err);
  742. return -1;
  743. }
  744. /* save time stamp of our first sample for this mmap */
  745. if (n == 0)
  746. *mmap_time = sample.time;
  747. /* limit events per mmap handled all at once */
  748. n++;
  749. if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
  750. break;
  751. }
  752. return n;
  753. }
  754. static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
  755. {
  756. int i, err, throttled = 0;
  757. s64 n, ntotal = 0;
  758. u64 flush_time = ULLONG_MAX, mmap_time;
  759. for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
  760. n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
  761. if (n < 0)
  762. return -1;
  763. /* flush time is going to be the minimum of all the individual
  764. * mmap times. Essentially, we flush all the samples queued up
  765. * from the last pass under our minimal start time -- that leaves
  766. * a very small race for samples to come in with a lower timestamp.
  767. * The ioctl to return the perf_clock timestamp should close the
  768. * race entirely.
  769. */
  770. if (mmap_time < flush_time)
  771. flush_time = mmap_time;
  772. ntotal += n;
  773. if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
  774. throttled = 1;
  775. }
  776. /* flush queue after each round in which we processed events */
  777. if (ntotal) {
  778. kvm->session->ordered_samples.next_flush = flush_time;
  779. err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session);
  780. if (err) {
  781. if (kvm->lost_events)
  782. pr_info("\nLost events: %" PRIu64 "\n\n",
  783. kvm->lost_events);
  784. return err;
  785. }
  786. }
  787. return throttled;
  788. }
  789. static volatile int done;
  790. static void sig_handler(int sig __maybe_unused)
  791. {
  792. done = 1;
  793. }
  794. static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
  795. {
  796. struct itimerspec new_value;
  797. int rc = -1;
  798. kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
  799. if (kvm->timerfd < 0) {
  800. pr_err("timerfd_create failed\n");
  801. goto out;
  802. }
  803. new_value.it_value.tv_sec = kvm->display_time;
  804. new_value.it_value.tv_nsec = 0;
  805. new_value.it_interval.tv_sec = kvm->display_time;
  806. new_value.it_interval.tv_nsec = 0;
  807. if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
  808. pr_err("timerfd_settime failed: %d\n", errno);
  809. close(kvm->timerfd);
  810. goto out;
  811. }
  812. rc = 0;
  813. out:
  814. return rc;
  815. }
  816. static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
  817. {
  818. uint64_t c;
  819. int rc;
  820. rc = read(kvm->timerfd, &c, sizeof(uint64_t));
  821. if (rc < 0) {
  822. if (errno == EAGAIN)
  823. return 0;
  824. pr_err("Failed to read timer fd: %d\n", errno);
  825. return -1;
  826. }
  827. if (rc != sizeof(uint64_t)) {
  828. pr_err("Error reading timer fd - invalid size returned\n");
  829. return -1;
  830. }
  831. if (c != 1)
  832. pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
  833. /* update display */
  834. sort_result(kvm);
  835. print_result(kvm);
  836. /* reset counts */
  837. clear_events_cache_stats(kvm->kvm_events_cache);
  838. kvm->total_count = 0;
  839. kvm->total_time = 0;
  840. kvm->lost_events = 0;
  841. return 0;
  842. }
  843. static int fd_set_nonblock(int fd)
  844. {
  845. long arg = 0;
  846. arg = fcntl(fd, F_GETFL);
  847. if (arg < 0) {
  848. pr_err("Failed to get current flags for fd %d\n", fd);
  849. return -1;
  850. }
  851. if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
  852. pr_err("Failed to set non-block option on fd %d\n", fd);
  853. return -1;
  854. }
  855. return 0;
  856. }
  857. static
  858. int perf_kvm__handle_stdin(struct termios *tc_now, struct termios *tc_save)
  859. {
  860. int c;
  861. tcsetattr(0, TCSANOW, tc_now);
  862. c = getc(stdin);
  863. tcsetattr(0, TCSAFLUSH, tc_save);
  864. if (c == 'q')
  865. return 1;
  866. return 0;
  867. }
  868. static int kvm_events_live_report(struct perf_kvm_stat *kvm)
  869. {
  870. struct pollfd *pollfds = NULL;
  871. int nr_fds, nr_stdin, ret, err = -EINVAL;
  872. struct termios tc, save;
  873. /* live flag must be set first */
  874. kvm->live = true;
  875. ret = cpu_isa_config(kvm);
  876. if (ret < 0)
  877. return ret;
  878. if (!verify_vcpu(kvm->trace_vcpu) ||
  879. !select_key(kvm) ||
  880. !register_kvm_events_ops(kvm)) {
  881. goto out;
  882. }
  883. init_kvm_event_record(kvm);
  884. tcgetattr(0, &save);
  885. tc = save;
  886. tc.c_lflag &= ~(ICANON | ECHO);
  887. tc.c_cc[VMIN] = 0;
  888. tc.c_cc[VTIME] = 0;
  889. signal(SIGINT, sig_handler);
  890. signal(SIGTERM, sig_handler);
  891. /* copy pollfds -- need to add timerfd and stdin */
  892. nr_fds = kvm->evlist->nr_fds;
  893. pollfds = zalloc(sizeof(struct pollfd) * (nr_fds + 2));
  894. if (!pollfds) {
  895. err = -ENOMEM;
  896. goto out;
  897. }
  898. memcpy(pollfds, kvm->evlist->pollfd,
  899. sizeof(struct pollfd) * kvm->evlist->nr_fds);
  900. /* add timer fd */
  901. if (perf_kvm__timerfd_create(kvm) < 0) {
  902. err = -1;
  903. goto out;
  904. }
  905. pollfds[nr_fds].fd = kvm->timerfd;
  906. pollfds[nr_fds].events = POLLIN;
  907. nr_fds++;
  908. pollfds[nr_fds].fd = fileno(stdin);
  909. pollfds[nr_fds].events = POLLIN;
  910. nr_stdin = nr_fds;
  911. nr_fds++;
  912. if (fd_set_nonblock(fileno(stdin)) != 0)
  913. goto out;
  914. /* everything is good - enable the events and process */
  915. perf_evlist__enable(kvm->evlist);
  916. while (!done) {
  917. int rc;
  918. rc = perf_kvm__mmap_read(kvm);
  919. if (rc < 0)
  920. break;
  921. err = perf_kvm__handle_timerfd(kvm);
  922. if (err)
  923. goto out;
  924. if (pollfds[nr_stdin].revents & POLLIN)
  925. done = perf_kvm__handle_stdin(&tc, &save);
  926. if (!rc && !done)
  927. err = poll(pollfds, nr_fds, 100);
  928. }
  929. perf_evlist__disable(kvm->evlist);
  930. if (err == 0) {
  931. sort_result(kvm);
  932. print_result(kvm);
  933. }
  934. out:
  935. if (kvm->timerfd >= 0)
  936. close(kvm->timerfd);
  937. if (pollfds)
  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. list_for_each_entry(pos, &evlist->entries, node) {
  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 = input_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 = perf_target__validate(&kvm->opts.target);
  1226. if (err) {
  1227. perf_target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
  1228. ui__warning("%s", errbuf);
  1229. }
  1230. if (perf_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. if (perf_target__has_task(&kvm->opts.target))
  1254. perf_event__synthesize_thread_map(&kvm->tool,
  1255. kvm->evlist->threads,
  1256. perf_event__process,
  1257. &kvm->session->machines.host);
  1258. else
  1259. perf_event__synthesize_threads(&kvm->tool, perf_event__process,
  1260. &kvm->session->machines.host);
  1261. err = kvm_live_open_events(kvm);
  1262. if (err)
  1263. goto out;
  1264. err = kvm_events_live_report(kvm);
  1265. out:
  1266. exit_browser(0);
  1267. if (kvm->session)
  1268. perf_session__delete(kvm->session);
  1269. kvm->session = NULL;
  1270. if (kvm->evlist) {
  1271. perf_evlist__delete_maps(kvm->evlist);
  1272. perf_evlist__delete(kvm->evlist);
  1273. }
  1274. return err;
  1275. }
  1276. #endif
  1277. static void print_kvm_stat_usage(void)
  1278. {
  1279. printf("Usage: perf kvm stat <command>\n\n");
  1280. printf("# Available commands:\n");
  1281. printf("\trecord: record kvm events\n");
  1282. printf("\treport: report statistical data of kvm events\n");
  1283. printf("\tlive: live reporting of statistical data of kvm events\n");
  1284. printf("\nOtherwise, it is the alias of 'perf stat':\n");
  1285. }
  1286. static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
  1287. {
  1288. struct perf_kvm_stat kvm = {
  1289. .file_name = file_name,
  1290. .trace_vcpu = -1,
  1291. .report_event = "vmexit",
  1292. .sort_key = "sample",
  1293. .exit_reasons = svm_exit_reasons,
  1294. .exit_reasons_size = ARRAY_SIZE(svm_exit_reasons),
  1295. .exit_reasons_isa = "SVM",
  1296. };
  1297. if (argc == 1) {
  1298. print_kvm_stat_usage();
  1299. goto perf_stat;
  1300. }
  1301. if (!strncmp(argv[1], "rec", 3))
  1302. return kvm_events_record(&kvm, argc - 1, argv + 1);
  1303. if (!strncmp(argv[1], "rep", 3))
  1304. return kvm_events_report(&kvm, argc - 1 , argv + 1);
  1305. #ifdef HAVE_TIMERFD_SUPPORT
  1306. if (!strncmp(argv[1], "live", 4))
  1307. return kvm_events_live(&kvm, argc - 1 , argv + 1);
  1308. #endif
  1309. perf_stat:
  1310. return cmd_stat(argc, argv, NULL);
  1311. }
  1312. #endif
  1313. static int __cmd_record(const char *file_name, int argc, const char **argv)
  1314. {
  1315. int rec_argc, i = 0, j;
  1316. const char **rec_argv;
  1317. rec_argc = argc + 2;
  1318. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1319. rec_argv[i++] = strdup("record");
  1320. rec_argv[i++] = strdup("-o");
  1321. rec_argv[i++] = strdup(file_name);
  1322. for (j = 1; j < argc; j++, i++)
  1323. rec_argv[i] = argv[j];
  1324. BUG_ON(i != rec_argc);
  1325. return cmd_record(i, rec_argv, NULL);
  1326. }
  1327. static int __cmd_report(const char *file_name, int argc, const char **argv)
  1328. {
  1329. int rec_argc, i = 0, j;
  1330. const char **rec_argv;
  1331. rec_argc = argc + 2;
  1332. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1333. rec_argv[i++] = strdup("report");
  1334. rec_argv[i++] = strdup("-i");
  1335. rec_argv[i++] = strdup(file_name);
  1336. for (j = 1; j < argc; j++, i++)
  1337. rec_argv[i] = argv[j];
  1338. BUG_ON(i != rec_argc);
  1339. return cmd_report(i, rec_argv, NULL);
  1340. }
  1341. static int
  1342. __cmd_buildid_list(const char *file_name, int argc, const char **argv)
  1343. {
  1344. int rec_argc, i = 0, j;
  1345. const char **rec_argv;
  1346. rec_argc = argc + 2;
  1347. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1348. rec_argv[i++] = strdup("buildid-list");
  1349. rec_argv[i++] = strdup("-i");
  1350. rec_argv[i++] = strdup(file_name);
  1351. for (j = 1; j < argc; j++, i++)
  1352. rec_argv[i] = argv[j];
  1353. BUG_ON(i != rec_argc);
  1354. return cmd_buildid_list(i, rec_argv, NULL);
  1355. }
  1356. int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
  1357. {
  1358. const char *file_name = NULL;
  1359. const struct option kvm_options[] = {
  1360. OPT_STRING('i', "input", &file_name, "file",
  1361. "Input file name"),
  1362. OPT_STRING('o', "output", &file_name, "file",
  1363. "Output file name"),
  1364. OPT_BOOLEAN(0, "guest", &perf_guest,
  1365. "Collect guest os data"),
  1366. OPT_BOOLEAN(0, "host", &perf_host,
  1367. "Collect host os data"),
  1368. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  1369. "guest mount directory under which every guest os"
  1370. " instance has a subdir"),
  1371. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  1372. "file", "file saving guest os vmlinux"),
  1373. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  1374. "file", "file saving guest os /proc/kallsyms"),
  1375. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  1376. "file", "file saving guest os /proc/modules"),
  1377. OPT_END()
  1378. };
  1379. const char * const kvm_usage[] = {
  1380. "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
  1381. NULL
  1382. };
  1383. perf_host = 0;
  1384. perf_guest = 1;
  1385. argc = parse_options(argc, argv, kvm_options, kvm_usage,
  1386. PARSE_OPT_STOP_AT_NON_OPTION);
  1387. if (!argc)
  1388. usage_with_options(kvm_usage, kvm_options);
  1389. if (!perf_host)
  1390. perf_guest = 1;
  1391. if (!file_name) {
  1392. if (perf_host && !perf_guest)
  1393. file_name = strdup("perf.data.host");
  1394. else if (!perf_host && perf_guest)
  1395. file_name = strdup("perf.data.guest");
  1396. else
  1397. file_name = strdup("perf.data.kvm");
  1398. if (!file_name) {
  1399. pr_err("Failed to allocate memory for filename\n");
  1400. return -ENOMEM;
  1401. }
  1402. }
  1403. if (!strncmp(argv[0], "rec", 3))
  1404. return __cmd_record(file_name, argc, argv);
  1405. else if (!strncmp(argv[0], "rep", 3))
  1406. return __cmd_report(file_name, argc, argv);
  1407. else if (!strncmp(argv[0], "diff", 4))
  1408. return cmd_diff(argc, argv, NULL);
  1409. else if (!strncmp(argv[0], "top", 3))
  1410. return cmd_top(argc, argv, NULL);
  1411. else if (!strncmp(argv[0], "buildid-list", 12))
  1412. return __cmd_buildid_list(file_name, argc, argv);
  1413. #if defined(__i386__) || defined(__x86_64__)
  1414. else if (!strncmp(argv[0], "stat", 4))
  1415. return kvm_cmd_stat(file_name, argc, argv);
  1416. #endif
  1417. else
  1418. usage_with_options(kvm_usage, kvm_options);
  1419. return 0;
  1420. }