builtin-kvm.c 35 KB

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