builtin-kvm.c 38 KB

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