builtin-kvm.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/evsel.h"
  4. #include "util/evlist.h"
  5. #include "util/util.h"
  6. #include "util/cache.h"
  7. #include "util/symbol.h"
  8. #include "util/thread.h"
  9. #include "util/header.h"
  10. #include "util/session.h"
  11. #include "util/intlist.h"
  12. #include "util/parse-options.h"
  13. #include "util/trace-event.h"
  14. #include "util/debug.h"
  15. #include <api/fs/debugfs.h>
  16. #include "util/tool.h"
  17. #include "util/stat.h"
  18. #include "util/top.h"
  19. #include "util/data.h"
  20. #include <sys/prctl.h>
  21. #ifdef HAVE_TIMERFD_SUPPORT
  22. #include <sys/timerfd.h>
  23. #endif
  24. #include <termios.h>
  25. #include <semaphore.h>
  26. #include <pthread.h>
  27. #include <math.h>
  28. #ifdef HAVE_KVM_STAT_SUPPORT
  29. #include <asm/kvm_perf.h>
  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/1000);
  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, VCPU_ID);
  312. thread__set_priv(thread, vcpu_record);
  313. }
  314. return thread__priv(thread);
  315. }
  316. static bool handle_kvm_event(struct perf_kvm_stat *kvm,
  317. struct thread *thread,
  318. struct perf_evsel *evsel,
  319. struct perf_sample *sample)
  320. {
  321. struct vcpu_event_record *vcpu_record;
  322. struct event_key key = { .key = INVALID_KEY,
  323. .exit_reasons = kvm->exit_reasons };
  324. vcpu_record = per_vcpu_record(thread, evsel, sample);
  325. if (!vcpu_record)
  326. return true;
  327. /* only process events for vcpus user cares about */
  328. if ((kvm->trace_vcpu != -1) &&
  329. (kvm->trace_vcpu != vcpu_record->vcpu_id))
  330. return true;
  331. if (kvm->events_ops->is_begin_event(evsel, sample, &key))
  332. return handle_begin_event(kvm, vcpu_record, &key, sample->time);
  333. if (is_child_event(kvm, evsel, sample, &key))
  334. return handle_child_event(kvm, vcpu_record, &key, sample);
  335. if (kvm->events_ops->is_end_event(evsel, sample, &key))
  336. return handle_end_event(kvm, vcpu_record, &key, sample);
  337. return true;
  338. }
  339. #define GET_EVENT_KEY(func, field) \
  340. static u64 get_event_ ##func(struct kvm_event *event, int vcpu) \
  341. { \
  342. if (vcpu == -1) \
  343. return event->total.field; \
  344. \
  345. if (vcpu >= event->max_vcpu) \
  346. return 0; \
  347. \
  348. return event->vcpu[vcpu].field; \
  349. }
  350. #define COMPARE_EVENT_KEY(func, field) \
  351. GET_EVENT_KEY(func, field) \
  352. static int compare_kvm_event_ ## func(struct kvm_event *one, \
  353. struct kvm_event *two, int vcpu)\
  354. { \
  355. return get_event_ ##func(one, vcpu) > \
  356. get_event_ ##func(two, vcpu); \
  357. }
  358. GET_EVENT_KEY(time, time);
  359. COMPARE_EVENT_KEY(count, stats.n);
  360. COMPARE_EVENT_KEY(mean, stats.mean);
  361. GET_EVENT_KEY(max, stats.max);
  362. GET_EVENT_KEY(min, stats.min);
  363. #define DEF_SORT_NAME_KEY(name, compare_key) \
  364. { #name, compare_kvm_event_ ## compare_key }
  365. static struct kvm_event_key keys[] = {
  366. DEF_SORT_NAME_KEY(sample, count),
  367. DEF_SORT_NAME_KEY(time, mean),
  368. { NULL, NULL }
  369. };
  370. static bool select_key(struct perf_kvm_stat *kvm)
  371. {
  372. int i;
  373. for (i = 0; keys[i].name; i++) {
  374. if (!strcmp(keys[i].name, kvm->sort_key)) {
  375. kvm->compare = keys[i].key;
  376. return true;
  377. }
  378. }
  379. pr_err("Unknown compare key:%s\n", kvm->sort_key);
  380. return false;
  381. }
  382. static void insert_to_result(struct rb_root *result, struct kvm_event *event,
  383. key_cmp_fun bigger, int vcpu)
  384. {
  385. struct rb_node **rb = &result->rb_node;
  386. struct rb_node *parent = NULL;
  387. struct kvm_event *p;
  388. while (*rb) {
  389. p = container_of(*rb, struct kvm_event, rb);
  390. parent = *rb;
  391. if (bigger(event, p, vcpu))
  392. rb = &(*rb)->rb_left;
  393. else
  394. rb = &(*rb)->rb_right;
  395. }
  396. rb_link_node(&event->rb, parent, rb);
  397. rb_insert_color(&event->rb, result);
  398. }
  399. static void
  400. update_total_count(struct perf_kvm_stat *kvm, struct kvm_event *event)
  401. {
  402. int vcpu = kvm->trace_vcpu;
  403. kvm->total_count += get_event_count(event, vcpu);
  404. kvm->total_time += get_event_time(event, vcpu);
  405. }
  406. static bool event_is_valid(struct kvm_event *event, int vcpu)
  407. {
  408. return !!get_event_count(event, vcpu);
  409. }
  410. static void sort_result(struct perf_kvm_stat *kvm)
  411. {
  412. unsigned int i;
  413. int vcpu = kvm->trace_vcpu;
  414. struct kvm_event *event;
  415. for (i = 0; i < EVENTS_CACHE_SIZE; i++) {
  416. list_for_each_entry(event, &kvm->kvm_events_cache[i], hash_entry) {
  417. if (event_is_valid(event, vcpu)) {
  418. update_total_count(kvm, event);
  419. insert_to_result(&kvm->result, event,
  420. kvm->compare, vcpu);
  421. }
  422. }
  423. }
  424. }
  425. /* returns left most element of result, and erase it */
  426. static struct kvm_event *pop_from_result(struct rb_root *result)
  427. {
  428. struct rb_node *node = rb_first(result);
  429. if (!node)
  430. return NULL;
  431. rb_erase(node, result);
  432. return container_of(node, struct kvm_event, rb);
  433. }
  434. static void print_vcpu_info(struct perf_kvm_stat *kvm)
  435. {
  436. int vcpu = kvm->trace_vcpu;
  437. pr_info("Analyze events for ");
  438. if (kvm->opts.target.system_wide)
  439. pr_info("all VMs, ");
  440. else if (kvm->opts.target.pid)
  441. pr_info("pid(s) %s, ", kvm->opts.target.pid);
  442. else
  443. pr_info("dazed and confused on what is monitored, ");
  444. if (vcpu == -1)
  445. pr_info("all VCPUs:\n\n");
  446. else
  447. pr_info("VCPU %d:\n\n", vcpu);
  448. }
  449. static void show_timeofday(void)
  450. {
  451. char date[64];
  452. struct timeval tv;
  453. struct tm ltime;
  454. gettimeofday(&tv, NULL);
  455. if (localtime_r(&tv.tv_sec, &ltime)) {
  456. strftime(date, sizeof(date), "%H:%M:%S", &ltime);
  457. pr_info("%s.%06ld", date, tv.tv_usec);
  458. } else
  459. pr_info("00:00:00.000000");
  460. return;
  461. }
  462. static void print_result(struct perf_kvm_stat *kvm)
  463. {
  464. char decode[DECODE_STR_LEN];
  465. struct kvm_event *event;
  466. int vcpu = kvm->trace_vcpu;
  467. if (kvm->live) {
  468. puts(CONSOLE_CLEAR);
  469. show_timeofday();
  470. }
  471. pr_info("\n\n");
  472. print_vcpu_info(kvm);
  473. pr_info("%*s ", DECODE_STR_LEN, kvm->events_ops->name);
  474. pr_info("%10s ", "Samples");
  475. pr_info("%9s ", "Samples%");
  476. pr_info("%9s ", "Time%");
  477. pr_info("%11s ", "Min Time");
  478. pr_info("%11s ", "Max Time");
  479. pr_info("%16s ", "Avg time");
  480. pr_info("\n\n");
  481. while ((event = pop_from_result(&kvm->result))) {
  482. u64 ecount, etime, max, min;
  483. ecount = get_event_count(event, vcpu);
  484. etime = get_event_time(event, vcpu);
  485. max = get_event_max(event, vcpu);
  486. min = get_event_min(event, vcpu);
  487. kvm->events_ops->decode_key(kvm, &event->key, decode);
  488. pr_info("%*s ", DECODE_STR_LEN, decode);
  489. pr_info("%10llu ", (unsigned long long)ecount);
  490. pr_info("%8.2f%% ", (double)ecount / kvm->total_count * 100);
  491. pr_info("%8.2f%% ", (double)etime / kvm->total_time * 100);
  492. pr_info("%9.2fus ", (double)min / 1e3);
  493. pr_info("%9.2fus ", (double)max / 1e3);
  494. pr_info("%9.2fus ( +-%7.2f%% )", (double)etime / ecount/1e3,
  495. kvm_event_rel_stddev(vcpu, event));
  496. pr_info("\n");
  497. }
  498. pr_info("\nTotal Samples:%" PRIu64 ", Total events handled time:%.2fus.\n\n",
  499. kvm->total_count, kvm->total_time / 1e3);
  500. if (kvm->lost_events)
  501. pr_info("\nLost events: %" PRIu64 "\n\n", kvm->lost_events);
  502. }
  503. #ifdef HAVE_TIMERFD_SUPPORT
  504. static int process_lost_event(struct perf_tool *tool,
  505. union perf_event *event __maybe_unused,
  506. struct perf_sample *sample __maybe_unused,
  507. struct machine *machine __maybe_unused)
  508. {
  509. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat, tool);
  510. kvm->lost_events++;
  511. return 0;
  512. }
  513. #endif
  514. static bool skip_sample(struct perf_kvm_stat *kvm,
  515. struct perf_sample *sample)
  516. {
  517. if (kvm->pid_list && intlist__find(kvm->pid_list, sample->pid) == NULL)
  518. return true;
  519. return false;
  520. }
  521. static int process_sample_event(struct perf_tool *tool,
  522. union perf_event *event,
  523. struct perf_sample *sample,
  524. struct perf_evsel *evsel,
  525. struct machine *machine)
  526. {
  527. struct thread *thread;
  528. struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
  529. tool);
  530. if (skip_sample(kvm, sample))
  531. return 0;
  532. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  533. if (thread == NULL) {
  534. pr_debug("problem processing %d event, skipping it.\n",
  535. event->header.type);
  536. return -1;
  537. }
  538. if (!handle_kvm_event(kvm, thread, evsel, sample))
  539. return -1;
  540. return 0;
  541. }
  542. static int cpu_isa_config(struct perf_kvm_stat *kvm)
  543. {
  544. char buf[64], *cpuid;
  545. int err;
  546. if (kvm->live) {
  547. err = get_cpuid(buf, sizeof(buf));
  548. if (err != 0) {
  549. pr_err("Failed to look up CPU type\n");
  550. return err;
  551. }
  552. cpuid = buf;
  553. } else
  554. cpuid = kvm->session->header.env.cpuid;
  555. if (!cpuid) {
  556. pr_err("Failed to look up CPU type\n");
  557. return -EINVAL;
  558. }
  559. err = cpu_isa_init(kvm, cpuid);
  560. if (err == -ENOTSUP)
  561. pr_err("CPU %s is not supported.\n", cpuid);
  562. return err;
  563. }
  564. static bool verify_vcpu(int vcpu)
  565. {
  566. if (vcpu != -1 && vcpu < 0) {
  567. pr_err("Invalid vcpu:%d.\n", vcpu);
  568. return false;
  569. }
  570. return true;
  571. }
  572. #ifdef HAVE_TIMERFD_SUPPORT
  573. /* keeping the max events to a modest level to keep
  574. * the processing of samples per mmap smooth.
  575. */
  576. #define PERF_KVM__MAX_EVENTS_PER_MMAP 25
  577. static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
  578. u64 *mmap_time)
  579. {
  580. union perf_event *event;
  581. struct perf_sample sample;
  582. s64 n = 0;
  583. int err;
  584. *mmap_time = ULLONG_MAX;
  585. while ((event = perf_evlist__mmap_read(kvm->evlist, idx)) != NULL) {
  586. err = perf_evlist__parse_sample(kvm->evlist, event, &sample);
  587. if (err) {
  588. perf_evlist__mmap_consume(kvm->evlist, idx);
  589. pr_err("Failed to parse sample\n");
  590. return -1;
  591. }
  592. err = perf_session_queue_event(kvm->session, event, &kvm->tool, &sample, 0);
  593. /*
  594. * FIXME: Here we can't consume the event, as perf_session_queue_event will
  595. * point to it, and it'll get possibly overwritten by the kernel.
  596. */
  597. perf_evlist__mmap_consume(kvm->evlist, idx);
  598. if (err) {
  599. pr_err("Failed to enqueue sample: %d\n", err);
  600. return -1;
  601. }
  602. /* save time stamp of our first sample for this mmap */
  603. if (n == 0)
  604. *mmap_time = sample.time;
  605. /* limit events per mmap handled all at once */
  606. n++;
  607. if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
  608. break;
  609. }
  610. return n;
  611. }
  612. static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
  613. {
  614. int i, err, throttled = 0;
  615. s64 n, ntotal = 0;
  616. u64 flush_time = ULLONG_MAX, mmap_time;
  617. for (i = 0; i < kvm->evlist->nr_mmaps; i++) {
  618. n = perf_kvm__mmap_read_idx(kvm, i, &mmap_time);
  619. if (n < 0)
  620. return -1;
  621. /* flush time is going to be the minimum of all the individual
  622. * mmap times. Essentially, we flush all the samples queued up
  623. * from the last pass under our minimal start time -- that leaves
  624. * a very small race for samples to come in with a lower timestamp.
  625. * The ioctl to return the perf_clock timestamp should close the
  626. * race entirely.
  627. */
  628. if (mmap_time < flush_time)
  629. flush_time = mmap_time;
  630. ntotal += n;
  631. if (n == PERF_KVM__MAX_EVENTS_PER_MMAP)
  632. throttled = 1;
  633. }
  634. /* flush queue after each round in which we processed events */
  635. if (ntotal) {
  636. kvm->session->ordered_events.next_flush = flush_time;
  637. err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session);
  638. if (err) {
  639. if (kvm->lost_events)
  640. pr_info("\nLost events: %" PRIu64 "\n\n",
  641. kvm->lost_events);
  642. return err;
  643. }
  644. }
  645. return throttled;
  646. }
  647. static volatile int done;
  648. static void sig_handler(int sig __maybe_unused)
  649. {
  650. done = 1;
  651. }
  652. static int perf_kvm__timerfd_create(struct perf_kvm_stat *kvm)
  653. {
  654. struct itimerspec new_value;
  655. int rc = -1;
  656. kvm->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
  657. if (kvm->timerfd < 0) {
  658. pr_err("timerfd_create failed\n");
  659. goto out;
  660. }
  661. new_value.it_value.tv_sec = kvm->display_time;
  662. new_value.it_value.tv_nsec = 0;
  663. new_value.it_interval.tv_sec = kvm->display_time;
  664. new_value.it_interval.tv_nsec = 0;
  665. if (timerfd_settime(kvm->timerfd, 0, &new_value, NULL) != 0) {
  666. pr_err("timerfd_settime failed: %d\n", errno);
  667. close(kvm->timerfd);
  668. goto out;
  669. }
  670. rc = 0;
  671. out:
  672. return rc;
  673. }
  674. static int perf_kvm__handle_timerfd(struct perf_kvm_stat *kvm)
  675. {
  676. uint64_t c;
  677. int rc;
  678. rc = read(kvm->timerfd, &c, sizeof(uint64_t));
  679. if (rc < 0) {
  680. if (errno == EAGAIN)
  681. return 0;
  682. pr_err("Failed to read timer fd: %d\n", errno);
  683. return -1;
  684. }
  685. if (rc != sizeof(uint64_t)) {
  686. pr_err("Error reading timer fd - invalid size returned\n");
  687. return -1;
  688. }
  689. if (c != 1)
  690. pr_debug("Missed timer beats: %" PRIu64 "\n", c-1);
  691. /* update display */
  692. sort_result(kvm);
  693. print_result(kvm);
  694. /* reset counts */
  695. clear_events_cache_stats(kvm->kvm_events_cache);
  696. kvm->total_count = 0;
  697. kvm->total_time = 0;
  698. kvm->lost_events = 0;
  699. return 0;
  700. }
  701. static int fd_set_nonblock(int fd)
  702. {
  703. long arg = 0;
  704. arg = fcntl(fd, F_GETFL);
  705. if (arg < 0) {
  706. pr_err("Failed to get current flags for fd %d\n", fd);
  707. return -1;
  708. }
  709. if (fcntl(fd, F_SETFL, arg | O_NONBLOCK) < 0) {
  710. pr_err("Failed to set non-block option on fd %d\n", fd);
  711. return -1;
  712. }
  713. return 0;
  714. }
  715. static int perf_kvm__handle_stdin(void)
  716. {
  717. int c;
  718. c = getc(stdin);
  719. if (c == 'q')
  720. return 1;
  721. return 0;
  722. }
  723. static int kvm_events_live_report(struct perf_kvm_stat *kvm)
  724. {
  725. int nr_stdin, ret, err = -EINVAL;
  726. struct termios save;
  727. /* live flag must be set first */
  728. kvm->live = true;
  729. ret = cpu_isa_config(kvm);
  730. if (ret < 0)
  731. return ret;
  732. if (!verify_vcpu(kvm->trace_vcpu) ||
  733. !select_key(kvm) ||
  734. !register_kvm_events_ops(kvm)) {
  735. goto out;
  736. }
  737. set_term_quiet_input(&save);
  738. init_kvm_event_record(kvm);
  739. signal(SIGINT, sig_handler);
  740. signal(SIGTERM, sig_handler);
  741. /* add timer fd */
  742. if (perf_kvm__timerfd_create(kvm) < 0) {
  743. err = -1;
  744. goto out;
  745. }
  746. if (perf_evlist__add_pollfd(kvm->evlist, kvm->timerfd) < 0)
  747. goto out;
  748. nr_stdin = perf_evlist__add_pollfd(kvm->evlist, fileno(stdin));
  749. if (nr_stdin < 0)
  750. goto out;
  751. if (fd_set_nonblock(fileno(stdin)) != 0)
  752. goto out;
  753. /* everything is good - enable the events and process */
  754. perf_evlist__enable(kvm->evlist);
  755. while (!done) {
  756. struct fdarray *fda = &kvm->evlist->pollfd;
  757. int rc;
  758. rc = perf_kvm__mmap_read(kvm);
  759. if (rc < 0)
  760. break;
  761. err = perf_kvm__handle_timerfd(kvm);
  762. if (err)
  763. goto out;
  764. if (fda->entries[nr_stdin].revents & POLLIN)
  765. done = perf_kvm__handle_stdin();
  766. if (!rc && !done)
  767. err = fdarray__poll(fda, 100);
  768. }
  769. perf_evlist__disable(kvm->evlist);
  770. if (err == 0) {
  771. sort_result(kvm);
  772. print_result(kvm);
  773. }
  774. out:
  775. if (kvm->timerfd >= 0)
  776. close(kvm->timerfd);
  777. tcsetattr(0, TCSAFLUSH, &save);
  778. return err;
  779. }
  780. static int kvm_live_open_events(struct perf_kvm_stat *kvm)
  781. {
  782. int err, rc = -1;
  783. struct perf_evsel *pos;
  784. struct perf_evlist *evlist = kvm->evlist;
  785. char sbuf[STRERR_BUFSIZE];
  786. perf_evlist__config(evlist, &kvm->opts);
  787. /*
  788. * Note: exclude_{guest,host} do not apply here.
  789. * This command processes KVM tracepoints from host only
  790. */
  791. evlist__for_each(evlist, pos) {
  792. struct perf_event_attr *attr = &pos->attr;
  793. /* make sure these *are* set */
  794. perf_evsel__set_sample_bit(pos, TID);
  795. perf_evsel__set_sample_bit(pos, TIME);
  796. perf_evsel__set_sample_bit(pos, CPU);
  797. perf_evsel__set_sample_bit(pos, RAW);
  798. /* make sure these are *not*; want as small a sample as possible */
  799. perf_evsel__reset_sample_bit(pos, PERIOD);
  800. perf_evsel__reset_sample_bit(pos, IP);
  801. perf_evsel__reset_sample_bit(pos, CALLCHAIN);
  802. perf_evsel__reset_sample_bit(pos, ADDR);
  803. perf_evsel__reset_sample_bit(pos, READ);
  804. attr->mmap = 0;
  805. attr->comm = 0;
  806. attr->task = 0;
  807. attr->sample_period = 1;
  808. attr->watermark = 0;
  809. attr->wakeup_events = 1000;
  810. /* will enable all once we are ready */
  811. attr->disabled = 1;
  812. }
  813. err = perf_evlist__open(evlist);
  814. if (err < 0) {
  815. printf("Couldn't create the events: %s\n",
  816. strerror_r(errno, sbuf, sizeof(sbuf)));
  817. goto out;
  818. }
  819. if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
  820. ui__error("Failed to mmap the events: %s\n",
  821. strerror_r(errno, sbuf, sizeof(sbuf)));
  822. perf_evlist__close(evlist);
  823. goto out;
  824. }
  825. rc = 0;
  826. out:
  827. return rc;
  828. }
  829. #endif
  830. static int read_events(struct perf_kvm_stat *kvm)
  831. {
  832. int ret;
  833. struct perf_tool eops = {
  834. .sample = process_sample_event,
  835. .comm = perf_event__process_comm,
  836. .ordered_events = true,
  837. };
  838. struct perf_data_file file = {
  839. .path = kvm->file_name,
  840. .mode = PERF_DATA_MODE_READ,
  841. };
  842. kvm->tool = eops;
  843. kvm->session = perf_session__new(&file, false, &kvm->tool);
  844. if (!kvm->session) {
  845. pr_err("Initializing perf session failed\n");
  846. return -1;
  847. }
  848. symbol__init(&kvm->session->header.env);
  849. if (!perf_session__has_traces(kvm->session, "kvm record"))
  850. return -EINVAL;
  851. /*
  852. * Do not use 'isa' recorded in kvm_exit tracepoint since it is not
  853. * traced in the old kernel.
  854. */
  855. ret = cpu_isa_config(kvm);
  856. if (ret < 0)
  857. return ret;
  858. return perf_session__process_events(kvm->session, &kvm->tool);
  859. }
  860. static int parse_target_str(struct perf_kvm_stat *kvm)
  861. {
  862. if (kvm->opts.target.pid) {
  863. kvm->pid_list = intlist__new(kvm->opts.target.pid);
  864. if (kvm->pid_list == NULL) {
  865. pr_err("Error parsing process id string\n");
  866. return -EINVAL;
  867. }
  868. }
  869. return 0;
  870. }
  871. static int kvm_events_report_vcpu(struct perf_kvm_stat *kvm)
  872. {
  873. int ret = -EINVAL;
  874. int vcpu = kvm->trace_vcpu;
  875. if (parse_target_str(kvm) != 0)
  876. goto exit;
  877. if (!verify_vcpu(vcpu))
  878. goto exit;
  879. if (!select_key(kvm))
  880. goto exit;
  881. if (!register_kvm_events_ops(kvm))
  882. goto exit;
  883. init_kvm_event_record(kvm);
  884. setup_pager();
  885. ret = read_events(kvm);
  886. if (ret)
  887. goto exit;
  888. sort_result(kvm);
  889. print_result(kvm);
  890. exit:
  891. return ret;
  892. }
  893. #define STRDUP_FAIL_EXIT(s) \
  894. ({ char *_p; \
  895. _p = strdup(s); \
  896. if (!_p) \
  897. return -ENOMEM; \
  898. _p; \
  899. })
  900. static int
  901. kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
  902. {
  903. unsigned int rec_argc, i, j, events_tp_size;
  904. const char **rec_argv;
  905. const char * const record_args[] = {
  906. "record",
  907. "-R",
  908. "-m", "1024",
  909. "-c", "1",
  910. };
  911. const char * const *events_tp;
  912. events_tp_size = 0;
  913. for (events_tp = kvm_events_tp; *events_tp; events_tp++)
  914. events_tp_size++;
  915. rec_argc = ARRAY_SIZE(record_args) + argc + 2 +
  916. 2 * events_tp_size;
  917. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  918. if (rec_argv == NULL)
  919. return -ENOMEM;
  920. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  921. rec_argv[i] = STRDUP_FAIL_EXIT(record_args[i]);
  922. for (j = 0; j < events_tp_size; j++) {
  923. rec_argv[i++] = "-e";
  924. rec_argv[i++] = STRDUP_FAIL_EXIT(kvm_events_tp[j]);
  925. }
  926. rec_argv[i++] = STRDUP_FAIL_EXIT("-o");
  927. rec_argv[i++] = STRDUP_FAIL_EXIT(kvm->file_name);
  928. for (j = 1; j < (unsigned int)argc; j++, i++)
  929. rec_argv[i] = argv[j];
  930. return cmd_record(i, rec_argv, NULL);
  931. }
  932. static int
  933. kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
  934. {
  935. const struct option kvm_events_report_options[] = {
  936. OPT_STRING(0, "event", &kvm->report_event, "report event",
  937. "event for reporting: vmexit, "
  938. "mmio (x86 only), ioport (x86 only)"),
  939. OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
  940. "vcpu id to report"),
  941. OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
  942. "key for sorting: sample(sort by samples number)"
  943. " time (sort by avg time)"),
  944. OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
  945. "analyze events only for given process id(s)"),
  946. OPT_END()
  947. };
  948. const char * const kvm_events_report_usage[] = {
  949. "perf kvm stat report [<options>]",
  950. NULL
  951. };
  952. if (argc) {
  953. argc = parse_options(argc, argv,
  954. kvm_events_report_options,
  955. kvm_events_report_usage, 0);
  956. if (argc)
  957. usage_with_options(kvm_events_report_usage,
  958. kvm_events_report_options);
  959. }
  960. if (!kvm->opts.target.pid)
  961. kvm->opts.target.system_wide = true;
  962. return kvm_events_report_vcpu(kvm);
  963. }
  964. #ifdef HAVE_TIMERFD_SUPPORT
  965. static struct perf_evlist *kvm_live_event_list(void)
  966. {
  967. struct perf_evlist *evlist;
  968. char *tp, *name, *sys;
  969. int err = -1;
  970. const char * const *events_tp;
  971. evlist = perf_evlist__new();
  972. if (evlist == NULL)
  973. return NULL;
  974. for (events_tp = kvm_events_tp; *events_tp; events_tp++) {
  975. tp = strdup(*events_tp);
  976. if (tp == NULL)
  977. goto out;
  978. /* split tracepoint into subsystem and name */
  979. sys = tp;
  980. name = strchr(tp, ':');
  981. if (name == NULL) {
  982. pr_err("Error parsing %s tracepoint: subsystem delimiter not found\n",
  983. *events_tp);
  984. free(tp);
  985. goto out;
  986. }
  987. *name = '\0';
  988. name++;
  989. if (perf_evlist__add_newtp(evlist, sys, name, NULL)) {
  990. pr_err("Failed to add %s tracepoint to the list\n", *events_tp);
  991. free(tp);
  992. goto out;
  993. }
  994. free(tp);
  995. }
  996. err = 0;
  997. out:
  998. if (err) {
  999. perf_evlist__delete(evlist);
  1000. evlist = NULL;
  1001. }
  1002. return evlist;
  1003. }
  1004. static int kvm_events_live(struct perf_kvm_stat *kvm,
  1005. int argc, const char **argv)
  1006. {
  1007. char errbuf[BUFSIZ];
  1008. int err;
  1009. const struct option live_options[] = {
  1010. OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
  1011. "record events on existing process id"),
  1012. OPT_CALLBACK('m', "mmap-pages", &kvm->opts.mmap_pages, "pages",
  1013. "number of mmap data pages",
  1014. perf_evlist__parse_mmap_pages),
  1015. OPT_INCR('v', "verbose", &verbose,
  1016. "be more verbose (show counter open errors, etc)"),
  1017. OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
  1018. "system-wide collection from all CPUs"),
  1019. OPT_UINTEGER('d', "display", &kvm->display_time,
  1020. "time in seconds between display updates"),
  1021. OPT_STRING(0, "event", &kvm->report_event, "report event",
  1022. "event for reporting: vmexit, mmio, ioport"),
  1023. OPT_INTEGER(0, "vcpu", &kvm->trace_vcpu,
  1024. "vcpu id to report"),
  1025. OPT_STRING('k', "key", &kvm->sort_key, "sort-key",
  1026. "key for sorting: sample(sort by samples number)"
  1027. " time (sort by avg time)"),
  1028. OPT_U64(0, "duration", &kvm->duration,
  1029. "show events other than"
  1030. " HLT (x86 only) or Wait state (s390 only)"
  1031. " that take longer than duration usecs"),
  1032. OPT_END()
  1033. };
  1034. const char * const live_usage[] = {
  1035. "perf kvm stat live [<options>]",
  1036. NULL
  1037. };
  1038. struct perf_data_file file = {
  1039. .mode = PERF_DATA_MODE_WRITE,
  1040. };
  1041. /* event handling */
  1042. kvm->tool.sample = process_sample_event;
  1043. kvm->tool.comm = perf_event__process_comm;
  1044. kvm->tool.exit = perf_event__process_exit;
  1045. kvm->tool.fork = perf_event__process_fork;
  1046. kvm->tool.lost = process_lost_event;
  1047. kvm->tool.ordered_events = true;
  1048. perf_tool__fill_defaults(&kvm->tool);
  1049. /* set defaults */
  1050. kvm->display_time = 1;
  1051. kvm->opts.user_interval = 1;
  1052. kvm->opts.mmap_pages = 512;
  1053. kvm->opts.target.uses_mmap = false;
  1054. kvm->opts.target.uid_str = NULL;
  1055. kvm->opts.target.uid = UINT_MAX;
  1056. symbol__init(NULL);
  1057. disable_buildid_cache();
  1058. use_browser = 0;
  1059. setup_browser(false);
  1060. if (argc) {
  1061. argc = parse_options(argc, argv, live_options,
  1062. live_usage, 0);
  1063. if (argc)
  1064. usage_with_options(live_usage, live_options);
  1065. }
  1066. kvm->duration *= NSEC_PER_USEC; /* convert usec to nsec */
  1067. /*
  1068. * target related setups
  1069. */
  1070. err = target__validate(&kvm->opts.target);
  1071. if (err) {
  1072. target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ);
  1073. ui__warning("%s", errbuf);
  1074. }
  1075. if (target__none(&kvm->opts.target))
  1076. kvm->opts.target.system_wide = true;
  1077. /*
  1078. * generate the event list
  1079. */
  1080. kvm->evlist = kvm_live_event_list();
  1081. if (kvm->evlist == NULL) {
  1082. err = -1;
  1083. goto out;
  1084. }
  1085. symbol_conf.nr_events = kvm->evlist->nr_entries;
  1086. if (perf_evlist__create_maps(kvm->evlist, &kvm->opts.target) < 0)
  1087. usage_with_options(live_usage, live_options);
  1088. /*
  1089. * perf session
  1090. */
  1091. kvm->session = perf_session__new(&file, false, &kvm->tool);
  1092. if (kvm->session == NULL) {
  1093. err = -1;
  1094. goto out;
  1095. }
  1096. kvm->session->evlist = kvm->evlist;
  1097. perf_session__set_id_hdr_size(kvm->session);
  1098. ordered_events__set_copy_on_queue(&kvm->session->ordered_events, true);
  1099. machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target,
  1100. kvm->evlist->threads, false);
  1101. err = kvm_live_open_events(kvm);
  1102. if (err)
  1103. goto out;
  1104. err = kvm_events_live_report(kvm);
  1105. out:
  1106. exit_browser(0);
  1107. if (kvm->session)
  1108. perf_session__delete(kvm->session);
  1109. kvm->session = NULL;
  1110. if (kvm->evlist)
  1111. perf_evlist__delete(kvm->evlist);
  1112. return err;
  1113. }
  1114. #endif
  1115. static void print_kvm_stat_usage(void)
  1116. {
  1117. printf("Usage: perf kvm stat <command>\n\n");
  1118. printf("# Available commands:\n");
  1119. printf("\trecord: record kvm events\n");
  1120. printf("\treport: report statistical data of kvm events\n");
  1121. printf("\tlive: live reporting of statistical data of kvm events\n");
  1122. printf("\nOtherwise, it is the alias of 'perf stat':\n");
  1123. }
  1124. static int kvm_cmd_stat(const char *file_name, int argc, const char **argv)
  1125. {
  1126. struct perf_kvm_stat kvm = {
  1127. .file_name = file_name,
  1128. .trace_vcpu = -1,
  1129. .report_event = "vmexit",
  1130. .sort_key = "sample",
  1131. };
  1132. if (argc == 1) {
  1133. print_kvm_stat_usage();
  1134. goto perf_stat;
  1135. }
  1136. if (!strncmp(argv[1], "rec", 3))
  1137. return kvm_events_record(&kvm, argc - 1, argv + 1);
  1138. if (!strncmp(argv[1], "rep", 3))
  1139. return kvm_events_report(&kvm, argc - 1 , argv + 1);
  1140. #ifdef HAVE_TIMERFD_SUPPORT
  1141. if (!strncmp(argv[1], "live", 4))
  1142. return kvm_events_live(&kvm, argc - 1 , argv + 1);
  1143. #endif
  1144. perf_stat:
  1145. return cmd_stat(argc, argv, NULL);
  1146. }
  1147. #endif /* HAVE_KVM_STAT_SUPPORT */
  1148. static int __cmd_record(const char *file_name, int argc, const char **argv)
  1149. {
  1150. int rec_argc, i = 0, j;
  1151. const char **rec_argv;
  1152. rec_argc = argc + 2;
  1153. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1154. rec_argv[i++] = strdup("record");
  1155. rec_argv[i++] = strdup("-o");
  1156. rec_argv[i++] = strdup(file_name);
  1157. for (j = 1; j < argc; j++, i++)
  1158. rec_argv[i] = argv[j];
  1159. BUG_ON(i != rec_argc);
  1160. return cmd_record(i, rec_argv, NULL);
  1161. }
  1162. static int __cmd_report(const char *file_name, int argc, const char **argv)
  1163. {
  1164. int rec_argc, i = 0, j;
  1165. const char **rec_argv;
  1166. rec_argc = argc + 2;
  1167. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1168. rec_argv[i++] = strdup("report");
  1169. rec_argv[i++] = strdup("-i");
  1170. rec_argv[i++] = strdup(file_name);
  1171. for (j = 1; j < argc; j++, i++)
  1172. rec_argv[i] = argv[j];
  1173. BUG_ON(i != rec_argc);
  1174. return cmd_report(i, rec_argv, NULL);
  1175. }
  1176. static int
  1177. __cmd_buildid_list(const char *file_name, int argc, const char **argv)
  1178. {
  1179. int rec_argc, i = 0, j;
  1180. const char **rec_argv;
  1181. rec_argc = argc + 2;
  1182. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1183. rec_argv[i++] = strdup("buildid-list");
  1184. rec_argv[i++] = strdup("-i");
  1185. rec_argv[i++] = strdup(file_name);
  1186. for (j = 1; j < argc; j++, i++)
  1187. rec_argv[i] = argv[j];
  1188. BUG_ON(i != rec_argc);
  1189. return cmd_buildid_list(i, rec_argv, NULL);
  1190. }
  1191. int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
  1192. {
  1193. const char *file_name = NULL;
  1194. const struct option kvm_options[] = {
  1195. OPT_STRING('i', "input", &file_name, "file",
  1196. "Input file name"),
  1197. OPT_STRING('o', "output", &file_name, "file",
  1198. "Output file name"),
  1199. OPT_BOOLEAN(0, "guest", &perf_guest,
  1200. "Collect guest os data"),
  1201. OPT_BOOLEAN(0, "host", &perf_host,
  1202. "Collect host os data"),
  1203. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  1204. "guest mount directory under which every guest os"
  1205. " instance has a subdir"),
  1206. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  1207. "file", "file saving guest os vmlinux"),
  1208. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  1209. "file", "file saving guest os /proc/kallsyms"),
  1210. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  1211. "file", "file saving guest os /proc/modules"),
  1212. OPT_INCR('v', "verbose", &verbose,
  1213. "be more verbose (show counter open errors, etc)"),
  1214. OPT_END()
  1215. };
  1216. const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
  1217. "buildid-list", "stat", NULL };
  1218. const char *kvm_usage[] = { NULL, NULL };
  1219. perf_host = 0;
  1220. perf_guest = 1;
  1221. argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
  1222. PARSE_OPT_STOP_AT_NON_OPTION);
  1223. if (!argc)
  1224. usage_with_options(kvm_usage, kvm_options);
  1225. if (!perf_host)
  1226. perf_guest = 1;
  1227. if (!file_name) {
  1228. file_name = get_filename_for_perf_kvm();
  1229. if (!file_name) {
  1230. pr_err("Failed to allocate memory for filename\n");
  1231. return -ENOMEM;
  1232. }
  1233. }
  1234. if (!strncmp(argv[0], "rec", 3))
  1235. return __cmd_record(file_name, argc, argv);
  1236. else if (!strncmp(argv[0], "rep", 3))
  1237. return __cmd_report(file_name, argc, argv);
  1238. else if (!strncmp(argv[0], "diff", 4))
  1239. return cmd_diff(argc, argv, NULL);
  1240. else if (!strncmp(argv[0], "top", 3))
  1241. return cmd_top(argc, argv, NULL);
  1242. else if (!strncmp(argv[0], "buildid-list", 12))
  1243. return __cmd_buildid_list(file_name, argc, argv);
  1244. #ifdef HAVE_KVM_STAT_SUPPORT
  1245. else if (!strncmp(argv[0], "stat", 4))
  1246. return kvm_cmd_stat(file_name, argc, argv);
  1247. #endif
  1248. else
  1249. usage_with_options(kvm_usage, kvm_options);
  1250. return 0;
  1251. }