builtin-kvm.c 39 KB

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