builtin-kvm.c 37 KB

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