builtin-kvm.c 39 KB

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