builtin-kvm.c 38 KB

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