builtin-kvm.c 38 KB

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