builtin-kvm.c 37 KB

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