builtin-kvm.c 38 KB

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