builtin-timechart.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  1. /*
  2. * builtin-timechart.c - make an svg timechart of system activity
  3. *
  4. * (C) Copyright 2009 Intel Corporation
  5. *
  6. * Authors:
  7. * Arjan van de Ven <arjan@linux.intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2
  12. * of the License.
  13. */
  14. #include <traceevent/event-parse.h>
  15. #include "builtin.h"
  16. #include "util/util.h"
  17. #include "util/color.h"
  18. #include <linux/list.h>
  19. #include "util/cache.h"
  20. #include "util/evlist.h"
  21. #include "util/evsel.h"
  22. #include <linux/rbtree.h>
  23. #include "util/symbol.h"
  24. #include "util/callchain.h"
  25. #include "util/strlist.h"
  26. #include "perf.h"
  27. #include "util/header.h"
  28. #include "util/parse-options.h"
  29. #include "util/parse-events.h"
  30. #include "util/event.h"
  31. #include "util/session.h"
  32. #include "util/svghelper.h"
  33. #include "util/tool.h"
  34. #include "util/data.h"
  35. #include "util/debug.h"
  36. #define SUPPORT_OLD_POWER_EVENTS 1
  37. #define PWR_EVENT_EXIT -1
  38. struct per_pid;
  39. struct power_event;
  40. struct wake_event;
  41. struct timechart {
  42. struct perf_tool tool;
  43. struct per_pid *all_data;
  44. struct power_event *power_events;
  45. struct wake_event *wake_events;
  46. int proc_num;
  47. unsigned int numcpus;
  48. u64 min_freq, /* Lowest CPU frequency seen */
  49. max_freq, /* Highest CPU frequency seen */
  50. turbo_frequency,
  51. first_time, last_time;
  52. bool power_only,
  53. tasks_only,
  54. with_backtrace,
  55. topology;
  56. bool force;
  57. /* IO related settings */
  58. bool io_only,
  59. skip_eagain;
  60. u64 io_events;
  61. u64 min_time,
  62. merge_dist;
  63. };
  64. struct per_pidcomm;
  65. struct cpu_sample;
  66. struct io_sample;
  67. /*
  68. * Datastructure layout:
  69. * We keep an list of "pid"s, matching the kernels notion of a task struct.
  70. * Each "pid" entry, has a list of "comm"s.
  71. * this is because we want to track different programs different, while
  72. * exec will reuse the original pid (by design).
  73. * Each comm has a list of samples that will be used to draw
  74. * final graph.
  75. */
  76. struct per_pid {
  77. struct per_pid *next;
  78. int pid;
  79. int ppid;
  80. u64 start_time;
  81. u64 end_time;
  82. u64 total_time;
  83. u64 total_bytes;
  84. int display;
  85. struct per_pidcomm *all;
  86. struct per_pidcomm *current;
  87. };
  88. struct per_pidcomm {
  89. struct per_pidcomm *next;
  90. u64 start_time;
  91. u64 end_time;
  92. u64 total_time;
  93. u64 max_bytes;
  94. u64 total_bytes;
  95. int Y;
  96. int display;
  97. long state;
  98. u64 state_since;
  99. char *comm;
  100. struct cpu_sample *samples;
  101. struct io_sample *io_samples;
  102. };
  103. struct sample_wrapper {
  104. struct sample_wrapper *next;
  105. u64 timestamp;
  106. unsigned char data[0];
  107. };
  108. #define TYPE_NONE 0
  109. #define TYPE_RUNNING 1
  110. #define TYPE_WAITING 2
  111. #define TYPE_BLOCKED 3
  112. struct cpu_sample {
  113. struct cpu_sample *next;
  114. u64 start_time;
  115. u64 end_time;
  116. int type;
  117. int cpu;
  118. const char *backtrace;
  119. };
  120. enum {
  121. IOTYPE_READ,
  122. IOTYPE_WRITE,
  123. IOTYPE_SYNC,
  124. IOTYPE_TX,
  125. IOTYPE_RX,
  126. IOTYPE_POLL,
  127. };
  128. struct io_sample {
  129. struct io_sample *next;
  130. u64 start_time;
  131. u64 end_time;
  132. u64 bytes;
  133. int type;
  134. int fd;
  135. int err;
  136. int merges;
  137. };
  138. #define CSTATE 1
  139. #define PSTATE 2
  140. struct power_event {
  141. struct power_event *next;
  142. int type;
  143. int state;
  144. u64 start_time;
  145. u64 end_time;
  146. int cpu;
  147. };
  148. struct wake_event {
  149. struct wake_event *next;
  150. int waker;
  151. int wakee;
  152. u64 time;
  153. const char *backtrace;
  154. };
  155. struct process_filter {
  156. char *name;
  157. int pid;
  158. struct process_filter *next;
  159. };
  160. static struct process_filter *process_filter;
  161. static struct per_pid *find_create_pid(struct timechart *tchart, int pid)
  162. {
  163. struct per_pid *cursor = tchart->all_data;
  164. while (cursor) {
  165. if (cursor->pid == pid)
  166. return cursor;
  167. cursor = cursor->next;
  168. }
  169. cursor = zalloc(sizeof(*cursor));
  170. assert(cursor != NULL);
  171. cursor->pid = pid;
  172. cursor->next = tchart->all_data;
  173. tchart->all_data = cursor;
  174. return cursor;
  175. }
  176. static void pid_set_comm(struct timechart *tchart, int pid, char *comm)
  177. {
  178. struct per_pid *p;
  179. struct per_pidcomm *c;
  180. p = find_create_pid(tchart, pid);
  181. c = p->all;
  182. while (c) {
  183. if (c->comm && strcmp(c->comm, comm) == 0) {
  184. p->current = c;
  185. return;
  186. }
  187. if (!c->comm) {
  188. c->comm = strdup(comm);
  189. p->current = c;
  190. return;
  191. }
  192. c = c->next;
  193. }
  194. c = zalloc(sizeof(*c));
  195. assert(c != NULL);
  196. c->comm = strdup(comm);
  197. p->current = c;
  198. c->next = p->all;
  199. p->all = c;
  200. }
  201. static void pid_fork(struct timechart *tchart, int pid, int ppid, u64 timestamp)
  202. {
  203. struct per_pid *p, *pp;
  204. p = find_create_pid(tchart, pid);
  205. pp = find_create_pid(tchart, ppid);
  206. p->ppid = ppid;
  207. if (pp->current && pp->current->comm && !p->current)
  208. pid_set_comm(tchart, pid, pp->current->comm);
  209. p->start_time = timestamp;
  210. if (p->current && !p->current->start_time) {
  211. p->current->start_time = timestamp;
  212. p->current->state_since = timestamp;
  213. }
  214. }
  215. static void pid_exit(struct timechart *tchart, int pid, u64 timestamp)
  216. {
  217. struct per_pid *p;
  218. p = find_create_pid(tchart, pid);
  219. p->end_time = timestamp;
  220. if (p->current)
  221. p->current->end_time = timestamp;
  222. }
  223. static void pid_put_sample(struct timechart *tchart, int pid, int type,
  224. unsigned int cpu, u64 start, u64 end,
  225. const char *backtrace)
  226. {
  227. struct per_pid *p;
  228. struct per_pidcomm *c;
  229. struct cpu_sample *sample;
  230. p = find_create_pid(tchart, pid);
  231. c = p->current;
  232. if (!c) {
  233. c = zalloc(sizeof(*c));
  234. assert(c != NULL);
  235. p->current = c;
  236. c->next = p->all;
  237. p->all = c;
  238. }
  239. sample = zalloc(sizeof(*sample));
  240. assert(sample != NULL);
  241. sample->start_time = start;
  242. sample->end_time = end;
  243. sample->type = type;
  244. sample->next = c->samples;
  245. sample->cpu = cpu;
  246. sample->backtrace = backtrace;
  247. c->samples = sample;
  248. if (sample->type == TYPE_RUNNING && end > start && start > 0) {
  249. c->total_time += (end-start);
  250. p->total_time += (end-start);
  251. }
  252. if (c->start_time == 0 || c->start_time > start)
  253. c->start_time = start;
  254. if (p->start_time == 0 || p->start_time > start)
  255. p->start_time = start;
  256. }
  257. #define MAX_CPUS 4096
  258. static u64 cpus_cstate_start_times[MAX_CPUS];
  259. static int cpus_cstate_state[MAX_CPUS];
  260. static u64 cpus_pstate_start_times[MAX_CPUS];
  261. static u64 cpus_pstate_state[MAX_CPUS];
  262. static int process_comm_event(struct perf_tool *tool,
  263. union perf_event *event,
  264. struct perf_sample *sample __maybe_unused,
  265. struct machine *machine __maybe_unused)
  266. {
  267. struct timechart *tchart = container_of(tool, struct timechart, tool);
  268. pid_set_comm(tchart, event->comm.tid, event->comm.comm);
  269. return 0;
  270. }
  271. static int process_fork_event(struct perf_tool *tool,
  272. union perf_event *event,
  273. struct perf_sample *sample __maybe_unused,
  274. struct machine *machine __maybe_unused)
  275. {
  276. struct timechart *tchart = container_of(tool, struct timechart, tool);
  277. pid_fork(tchart, event->fork.pid, event->fork.ppid, event->fork.time);
  278. return 0;
  279. }
  280. static int process_exit_event(struct perf_tool *tool,
  281. union perf_event *event,
  282. struct perf_sample *sample __maybe_unused,
  283. struct machine *machine __maybe_unused)
  284. {
  285. struct timechart *tchart = container_of(tool, struct timechart, tool);
  286. pid_exit(tchart, event->fork.pid, event->fork.time);
  287. return 0;
  288. }
  289. #ifdef SUPPORT_OLD_POWER_EVENTS
  290. static int use_old_power_events;
  291. #endif
  292. static void c_state_start(int cpu, u64 timestamp, int state)
  293. {
  294. cpus_cstate_start_times[cpu] = timestamp;
  295. cpus_cstate_state[cpu] = state;
  296. }
  297. static void c_state_end(struct timechart *tchart, int cpu, u64 timestamp)
  298. {
  299. struct power_event *pwr = zalloc(sizeof(*pwr));
  300. if (!pwr)
  301. return;
  302. pwr->state = cpus_cstate_state[cpu];
  303. pwr->start_time = cpus_cstate_start_times[cpu];
  304. pwr->end_time = timestamp;
  305. pwr->cpu = cpu;
  306. pwr->type = CSTATE;
  307. pwr->next = tchart->power_events;
  308. tchart->power_events = pwr;
  309. }
  310. static void p_state_change(struct timechart *tchart, int cpu, u64 timestamp, u64 new_freq)
  311. {
  312. struct power_event *pwr;
  313. if (new_freq > 8000000) /* detect invalid data */
  314. return;
  315. pwr = zalloc(sizeof(*pwr));
  316. if (!pwr)
  317. return;
  318. pwr->state = cpus_pstate_state[cpu];
  319. pwr->start_time = cpus_pstate_start_times[cpu];
  320. pwr->end_time = timestamp;
  321. pwr->cpu = cpu;
  322. pwr->type = PSTATE;
  323. pwr->next = tchart->power_events;
  324. if (!pwr->start_time)
  325. pwr->start_time = tchart->first_time;
  326. tchart->power_events = pwr;
  327. cpus_pstate_state[cpu] = new_freq;
  328. cpus_pstate_start_times[cpu] = timestamp;
  329. if ((u64)new_freq > tchart->max_freq)
  330. tchart->max_freq = new_freq;
  331. if (new_freq < tchart->min_freq || tchart->min_freq == 0)
  332. tchart->min_freq = new_freq;
  333. if (new_freq == tchart->max_freq - 1000)
  334. tchart->turbo_frequency = tchart->max_freq;
  335. }
  336. static void sched_wakeup(struct timechart *tchart, int cpu, u64 timestamp,
  337. int waker, int wakee, u8 flags, const char *backtrace)
  338. {
  339. struct per_pid *p;
  340. struct wake_event *we = zalloc(sizeof(*we));
  341. if (!we)
  342. return;
  343. we->time = timestamp;
  344. we->waker = waker;
  345. we->backtrace = backtrace;
  346. if ((flags & TRACE_FLAG_HARDIRQ) || (flags & TRACE_FLAG_SOFTIRQ))
  347. we->waker = -1;
  348. we->wakee = wakee;
  349. we->next = tchart->wake_events;
  350. tchart->wake_events = we;
  351. p = find_create_pid(tchart, we->wakee);
  352. if (p && p->current && p->current->state == TYPE_NONE) {
  353. p->current->state_since = timestamp;
  354. p->current->state = TYPE_WAITING;
  355. }
  356. if (p && p->current && p->current->state == TYPE_BLOCKED) {
  357. pid_put_sample(tchart, p->pid, p->current->state, cpu,
  358. p->current->state_since, timestamp, NULL);
  359. p->current->state_since = timestamp;
  360. p->current->state = TYPE_WAITING;
  361. }
  362. }
  363. static void sched_switch(struct timechart *tchart, int cpu, u64 timestamp,
  364. int prev_pid, int next_pid, u64 prev_state,
  365. const char *backtrace)
  366. {
  367. struct per_pid *p = NULL, *prev_p;
  368. prev_p = find_create_pid(tchart, prev_pid);
  369. p = find_create_pid(tchart, next_pid);
  370. if (prev_p->current && prev_p->current->state != TYPE_NONE)
  371. pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
  372. prev_p->current->state_since, timestamp,
  373. backtrace);
  374. if (p && p->current) {
  375. if (p->current->state != TYPE_NONE)
  376. pid_put_sample(tchart, next_pid, p->current->state, cpu,
  377. p->current->state_since, timestamp,
  378. backtrace);
  379. p->current->state_since = timestamp;
  380. p->current->state = TYPE_RUNNING;
  381. }
  382. if (prev_p->current) {
  383. prev_p->current->state = TYPE_NONE;
  384. prev_p->current->state_since = timestamp;
  385. if (prev_state & 2)
  386. prev_p->current->state = TYPE_BLOCKED;
  387. if (prev_state == 0)
  388. prev_p->current->state = TYPE_WAITING;
  389. }
  390. }
  391. static const char *cat_backtrace(union perf_event *event,
  392. struct perf_sample *sample,
  393. struct machine *machine)
  394. {
  395. struct addr_location al;
  396. unsigned int i;
  397. char *p = NULL;
  398. size_t p_len;
  399. u8 cpumode = PERF_RECORD_MISC_USER;
  400. struct addr_location tal;
  401. struct ip_callchain *chain = sample->callchain;
  402. FILE *f = open_memstream(&p, &p_len);
  403. if (!f) {
  404. perror("open_memstream error");
  405. return NULL;
  406. }
  407. if (!chain)
  408. goto exit;
  409. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  410. fprintf(stderr, "problem processing %d event, skipping it.\n",
  411. event->header.type);
  412. goto exit;
  413. }
  414. for (i = 0; i < chain->nr; i++) {
  415. u64 ip;
  416. if (callchain_param.order == ORDER_CALLEE)
  417. ip = chain->ips[i];
  418. else
  419. ip = chain->ips[chain->nr - i - 1];
  420. if (ip >= PERF_CONTEXT_MAX) {
  421. switch (ip) {
  422. case PERF_CONTEXT_HV:
  423. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  424. break;
  425. case PERF_CONTEXT_KERNEL:
  426. cpumode = PERF_RECORD_MISC_KERNEL;
  427. break;
  428. case PERF_CONTEXT_USER:
  429. cpumode = PERF_RECORD_MISC_USER;
  430. break;
  431. default:
  432. pr_debug("invalid callchain context: "
  433. "%"PRId64"\n", (s64) ip);
  434. /*
  435. * It seems the callchain is corrupted.
  436. * Discard all.
  437. */
  438. zfree(&p);
  439. goto exit_put;
  440. }
  441. continue;
  442. }
  443. tal.filtered = 0;
  444. thread__find_addr_location(al.thread, cpumode,
  445. MAP__FUNCTION, ip, &tal);
  446. if (tal.sym)
  447. fprintf(f, "..... %016" PRIx64 " %s\n", ip,
  448. tal.sym->name);
  449. else
  450. fprintf(f, "..... %016" PRIx64 "\n", ip);
  451. }
  452. exit_put:
  453. addr_location__put(&al);
  454. exit:
  455. fclose(f);
  456. return p;
  457. }
  458. typedef int (*tracepoint_handler)(struct timechart *tchart,
  459. struct perf_evsel *evsel,
  460. struct perf_sample *sample,
  461. const char *backtrace);
  462. static int process_sample_event(struct perf_tool *tool,
  463. union perf_event *event,
  464. struct perf_sample *sample,
  465. struct perf_evsel *evsel,
  466. struct machine *machine)
  467. {
  468. struct timechart *tchart = container_of(tool, struct timechart, tool);
  469. if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
  470. if (!tchart->first_time || tchart->first_time > sample->time)
  471. tchart->first_time = sample->time;
  472. if (tchart->last_time < sample->time)
  473. tchart->last_time = sample->time;
  474. }
  475. if (evsel->handler != NULL) {
  476. tracepoint_handler f = evsel->handler;
  477. return f(tchart, evsel, sample,
  478. cat_backtrace(event, sample, machine));
  479. }
  480. return 0;
  481. }
  482. static int
  483. process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
  484. struct perf_evsel *evsel,
  485. struct perf_sample *sample,
  486. const char *backtrace __maybe_unused)
  487. {
  488. u32 state = perf_evsel__intval(evsel, sample, "state");
  489. u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  490. if (state == (u32)PWR_EVENT_EXIT)
  491. c_state_end(tchart, cpu_id, sample->time);
  492. else
  493. c_state_start(cpu_id, sample->time, state);
  494. return 0;
  495. }
  496. static int
  497. process_sample_cpu_frequency(struct timechart *tchart,
  498. struct perf_evsel *evsel,
  499. struct perf_sample *sample,
  500. const char *backtrace __maybe_unused)
  501. {
  502. u32 state = perf_evsel__intval(evsel, sample, "state");
  503. u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  504. p_state_change(tchart, cpu_id, sample->time, state);
  505. return 0;
  506. }
  507. static int
  508. process_sample_sched_wakeup(struct timechart *tchart,
  509. struct perf_evsel *evsel,
  510. struct perf_sample *sample,
  511. const char *backtrace)
  512. {
  513. u8 flags = perf_evsel__intval(evsel, sample, "common_flags");
  514. int waker = perf_evsel__intval(evsel, sample, "common_pid");
  515. int wakee = perf_evsel__intval(evsel, sample, "pid");
  516. sched_wakeup(tchart, sample->cpu, sample->time, waker, wakee, flags, backtrace);
  517. return 0;
  518. }
  519. static int
  520. process_sample_sched_switch(struct timechart *tchart,
  521. struct perf_evsel *evsel,
  522. struct perf_sample *sample,
  523. const char *backtrace)
  524. {
  525. int prev_pid = perf_evsel__intval(evsel, sample, "prev_pid");
  526. int next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  527. u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  528. sched_switch(tchart, sample->cpu, sample->time, prev_pid, next_pid,
  529. prev_state, backtrace);
  530. return 0;
  531. }
  532. #ifdef SUPPORT_OLD_POWER_EVENTS
  533. static int
  534. process_sample_power_start(struct timechart *tchart __maybe_unused,
  535. struct perf_evsel *evsel,
  536. struct perf_sample *sample,
  537. const char *backtrace __maybe_unused)
  538. {
  539. u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  540. u64 value = perf_evsel__intval(evsel, sample, "value");
  541. c_state_start(cpu_id, sample->time, value);
  542. return 0;
  543. }
  544. static int
  545. process_sample_power_end(struct timechart *tchart,
  546. struct perf_evsel *evsel __maybe_unused,
  547. struct perf_sample *sample,
  548. const char *backtrace __maybe_unused)
  549. {
  550. c_state_end(tchart, sample->cpu, sample->time);
  551. return 0;
  552. }
  553. static int
  554. process_sample_power_frequency(struct timechart *tchart,
  555. struct perf_evsel *evsel,
  556. struct perf_sample *sample,
  557. const char *backtrace __maybe_unused)
  558. {
  559. u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  560. u64 value = perf_evsel__intval(evsel, sample, "value");
  561. p_state_change(tchart, cpu_id, sample->time, value);
  562. return 0;
  563. }
  564. #endif /* SUPPORT_OLD_POWER_EVENTS */
  565. /*
  566. * After the last sample we need to wrap up the current C/P state
  567. * and close out each CPU for these.
  568. */
  569. static void end_sample_processing(struct timechart *tchart)
  570. {
  571. u64 cpu;
  572. struct power_event *pwr;
  573. for (cpu = 0; cpu <= tchart->numcpus; cpu++) {
  574. /* C state */
  575. #if 0
  576. pwr = zalloc(sizeof(*pwr));
  577. if (!pwr)
  578. return;
  579. pwr->state = cpus_cstate_state[cpu];
  580. pwr->start_time = cpus_cstate_start_times[cpu];
  581. pwr->end_time = tchart->last_time;
  582. pwr->cpu = cpu;
  583. pwr->type = CSTATE;
  584. pwr->next = tchart->power_events;
  585. tchart->power_events = pwr;
  586. #endif
  587. /* P state */
  588. pwr = zalloc(sizeof(*pwr));
  589. if (!pwr)
  590. return;
  591. pwr->state = cpus_pstate_state[cpu];
  592. pwr->start_time = cpus_pstate_start_times[cpu];
  593. pwr->end_time = tchart->last_time;
  594. pwr->cpu = cpu;
  595. pwr->type = PSTATE;
  596. pwr->next = tchart->power_events;
  597. if (!pwr->start_time)
  598. pwr->start_time = tchart->first_time;
  599. if (!pwr->state)
  600. pwr->state = tchart->min_freq;
  601. tchart->power_events = pwr;
  602. }
  603. }
  604. static int pid_begin_io_sample(struct timechart *tchart, int pid, int type,
  605. u64 start, int fd)
  606. {
  607. struct per_pid *p = find_create_pid(tchart, pid);
  608. struct per_pidcomm *c = p->current;
  609. struct io_sample *sample;
  610. struct io_sample *prev;
  611. if (!c) {
  612. c = zalloc(sizeof(*c));
  613. if (!c)
  614. return -ENOMEM;
  615. p->current = c;
  616. c->next = p->all;
  617. p->all = c;
  618. }
  619. prev = c->io_samples;
  620. if (prev && prev->start_time && !prev->end_time) {
  621. pr_warning("Skip invalid start event: "
  622. "previous event already started!\n");
  623. /* remove previous event that has been started,
  624. * we are not sure we will ever get an end for it */
  625. c->io_samples = prev->next;
  626. free(prev);
  627. return 0;
  628. }
  629. sample = zalloc(sizeof(*sample));
  630. if (!sample)
  631. return -ENOMEM;
  632. sample->start_time = start;
  633. sample->type = type;
  634. sample->fd = fd;
  635. sample->next = c->io_samples;
  636. c->io_samples = sample;
  637. if (c->start_time == 0 || c->start_time > start)
  638. c->start_time = start;
  639. return 0;
  640. }
  641. static int pid_end_io_sample(struct timechart *tchart, int pid, int type,
  642. u64 end, long ret)
  643. {
  644. struct per_pid *p = find_create_pid(tchart, pid);
  645. struct per_pidcomm *c = p->current;
  646. struct io_sample *sample, *prev;
  647. if (!c) {
  648. pr_warning("Invalid pidcomm!\n");
  649. return -1;
  650. }
  651. sample = c->io_samples;
  652. if (!sample) /* skip partially captured events */
  653. return 0;
  654. if (sample->end_time) {
  655. pr_warning("Skip invalid end event: "
  656. "previous event already ended!\n");
  657. return 0;
  658. }
  659. if (sample->type != type) {
  660. pr_warning("Skip invalid end event: invalid event type!\n");
  661. return 0;
  662. }
  663. sample->end_time = end;
  664. prev = sample->next;
  665. /* we want to be able to see small and fast transfers, so make them
  666. * at least min_time long, but don't overlap them */
  667. if (sample->end_time - sample->start_time < tchart->min_time)
  668. sample->end_time = sample->start_time + tchart->min_time;
  669. if (prev && sample->start_time < prev->end_time) {
  670. if (prev->err) /* try to make errors more visible */
  671. sample->start_time = prev->end_time;
  672. else
  673. prev->end_time = sample->start_time;
  674. }
  675. if (ret < 0) {
  676. sample->err = ret;
  677. } else if (type == IOTYPE_READ || type == IOTYPE_WRITE ||
  678. type == IOTYPE_TX || type == IOTYPE_RX) {
  679. if ((u64)ret > c->max_bytes)
  680. c->max_bytes = ret;
  681. c->total_bytes += ret;
  682. p->total_bytes += ret;
  683. sample->bytes = ret;
  684. }
  685. /* merge two requests to make svg smaller and render-friendly */
  686. if (prev &&
  687. prev->type == sample->type &&
  688. prev->err == sample->err &&
  689. prev->fd == sample->fd &&
  690. prev->end_time + tchart->merge_dist >= sample->start_time) {
  691. sample->bytes += prev->bytes;
  692. sample->merges += prev->merges + 1;
  693. sample->start_time = prev->start_time;
  694. sample->next = prev->next;
  695. free(prev);
  696. if (!sample->err && sample->bytes > c->max_bytes)
  697. c->max_bytes = sample->bytes;
  698. }
  699. tchart->io_events++;
  700. return 0;
  701. }
  702. static int
  703. process_enter_read(struct timechart *tchart,
  704. struct perf_evsel *evsel,
  705. struct perf_sample *sample)
  706. {
  707. long fd = perf_evsel__intval(evsel, sample, "fd");
  708. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_READ,
  709. sample->time, fd);
  710. }
  711. static int
  712. process_exit_read(struct timechart *tchart,
  713. struct perf_evsel *evsel,
  714. struct perf_sample *sample)
  715. {
  716. long ret = perf_evsel__intval(evsel, sample, "ret");
  717. return pid_end_io_sample(tchart, sample->tid, IOTYPE_READ,
  718. sample->time, ret);
  719. }
  720. static int
  721. process_enter_write(struct timechart *tchart,
  722. struct perf_evsel *evsel,
  723. struct perf_sample *sample)
  724. {
  725. long fd = perf_evsel__intval(evsel, sample, "fd");
  726. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_WRITE,
  727. sample->time, fd);
  728. }
  729. static int
  730. process_exit_write(struct timechart *tchart,
  731. struct perf_evsel *evsel,
  732. struct perf_sample *sample)
  733. {
  734. long ret = perf_evsel__intval(evsel, sample, "ret");
  735. return pid_end_io_sample(tchart, sample->tid, IOTYPE_WRITE,
  736. sample->time, ret);
  737. }
  738. static int
  739. process_enter_sync(struct timechart *tchart,
  740. struct perf_evsel *evsel,
  741. struct perf_sample *sample)
  742. {
  743. long fd = perf_evsel__intval(evsel, sample, "fd");
  744. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_SYNC,
  745. sample->time, fd);
  746. }
  747. static int
  748. process_exit_sync(struct timechart *tchart,
  749. struct perf_evsel *evsel,
  750. struct perf_sample *sample)
  751. {
  752. long ret = perf_evsel__intval(evsel, sample, "ret");
  753. return pid_end_io_sample(tchart, sample->tid, IOTYPE_SYNC,
  754. sample->time, ret);
  755. }
  756. static int
  757. process_enter_tx(struct timechart *tchart,
  758. struct perf_evsel *evsel,
  759. struct perf_sample *sample)
  760. {
  761. long fd = perf_evsel__intval(evsel, sample, "fd");
  762. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_TX,
  763. sample->time, fd);
  764. }
  765. static int
  766. process_exit_tx(struct timechart *tchart,
  767. struct perf_evsel *evsel,
  768. struct perf_sample *sample)
  769. {
  770. long ret = perf_evsel__intval(evsel, sample, "ret");
  771. return pid_end_io_sample(tchart, sample->tid, IOTYPE_TX,
  772. sample->time, ret);
  773. }
  774. static int
  775. process_enter_rx(struct timechart *tchart,
  776. struct perf_evsel *evsel,
  777. struct perf_sample *sample)
  778. {
  779. long fd = perf_evsel__intval(evsel, sample, "fd");
  780. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_RX,
  781. sample->time, fd);
  782. }
  783. static int
  784. process_exit_rx(struct timechart *tchart,
  785. struct perf_evsel *evsel,
  786. struct perf_sample *sample)
  787. {
  788. long ret = perf_evsel__intval(evsel, sample, "ret");
  789. return pid_end_io_sample(tchart, sample->tid, IOTYPE_RX,
  790. sample->time, ret);
  791. }
  792. static int
  793. process_enter_poll(struct timechart *tchart,
  794. struct perf_evsel *evsel,
  795. struct perf_sample *sample)
  796. {
  797. long fd = perf_evsel__intval(evsel, sample, "fd");
  798. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_POLL,
  799. sample->time, fd);
  800. }
  801. static int
  802. process_exit_poll(struct timechart *tchart,
  803. struct perf_evsel *evsel,
  804. struct perf_sample *sample)
  805. {
  806. long ret = perf_evsel__intval(evsel, sample, "ret");
  807. return pid_end_io_sample(tchart, sample->tid, IOTYPE_POLL,
  808. sample->time, ret);
  809. }
  810. /*
  811. * Sort the pid datastructure
  812. */
  813. static void sort_pids(struct timechart *tchart)
  814. {
  815. struct per_pid *new_list, *p, *cursor, *prev;
  816. /* sort by ppid first, then by pid, lowest to highest */
  817. new_list = NULL;
  818. while (tchart->all_data) {
  819. p = tchart->all_data;
  820. tchart->all_data = p->next;
  821. p->next = NULL;
  822. if (new_list == NULL) {
  823. new_list = p;
  824. p->next = NULL;
  825. continue;
  826. }
  827. prev = NULL;
  828. cursor = new_list;
  829. while (cursor) {
  830. if (cursor->ppid > p->ppid ||
  831. (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
  832. /* must insert before */
  833. if (prev) {
  834. p->next = prev->next;
  835. prev->next = p;
  836. cursor = NULL;
  837. continue;
  838. } else {
  839. p->next = new_list;
  840. new_list = p;
  841. cursor = NULL;
  842. continue;
  843. }
  844. }
  845. prev = cursor;
  846. cursor = cursor->next;
  847. if (!cursor)
  848. prev->next = p;
  849. }
  850. }
  851. tchart->all_data = new_list;
  852. }
  853. static void draw_c_p_states(struct timechart *tchart)
  854. {
  855. struct power_event *pwr;
  856. pwr = tchart->power_events;
  857. /*
  858. * two pass drawing so that the P state bars are on top of the C state blocks
  859. */
  860. while (pwr) {
  861. if (pwr->type == CSTATE)
  862. svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  863. pwr = pwr->next;
  864. }
  865. pwr = tchart->power_events;
  866. while (pwr) {
  867. if (pwr->type == PSTATE) {
  868. if (!pwr->state)
  869. pwr->state = tchart->min_freq;
  870. svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  871. }
  872. pwr = pwr->next;
  873. }
  874. }
  875. static void draw_wakeups(struct timechart *tchart)
  876. {
  877. struct wake_event *we;
  878. struct per_pid *p;
  879. struct per_pidcomm *c;
  880. we = tchart->wake_events;
  881. while (we) {
  882. int from = 0, to = 0;
  883. char *task_from = NULL, *task_to = NULL;
  884. /* locate the column of the waker and wakee */
  885. p = tchart->all_data;
  886. while (p) {
  887. if (p->pid == we->waker || p->pid == we->wakee) {
  888. c = p->all;
  889. while (c) {
  890. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  891. if (p->pid == we->waker && !from) {
  892. from = c->Y;
  893. task_from = strdup(c->comm);
  894. }
  895. if (p->pid == we->wakee && !to) {
  896. to = c->Y;
  897. task_to = strdup(c->comm);
  898. }
  899. }
  900. c = c->next;
  901. }
  902. c = p->all;
  903. while (c) {
  904. if (p->pid == we->waker && !from) {
  905. from = c->Y;
  906. task_from = strdup(c->comm);
  907. }
  908. if (p->pid == we->wakee && !to) {
  909. to = c->Y;
  910. task_to = strdup(c->comm);
  911. }
  912. c = c->next;
  913. }
  914. }
  915. p = p->next;
  916. }
  917. if (!task_from) {
  918. task_from = malloc(40);
  919. sprintf(task_from, "[%i]", we->waker);
  920. }
  921. if (!task_to) {
  922. task_to = malloc(40);
  923. sprintf(task_to, "[%i]", we->wakee);
  924. }
  925. if (we->waker == -1)
  926. svg_interrupt(we->time, to, we->backtrace);
  927. else if (from && to && abs(from - to) == 1)
  928. svg_wakeline(we->time, from, to, we->backtrace);
  929. else
  930. svg_partial_wakeline(we->time, from, task_from, to,
  931. task_to, we->backtrace);
  932. we = we->next;
  933. free(task_from);
  934. free(task_to);
  935. }
  936. }
  937. static void draw_cpu_usage(struct timechart *tchart)
  938. {
  939. struct per_pid *p;
  940. struct per_pidcomm *c;
  941. struct cpu_sample *sample;
  942. p = tchart->all_data;
  943. while (p) {
  944. c = p->all;
  945. while (c) {
  946. sample = c->samples;
  947. while (sample) {
  948. if (sample->type == TYPE_RUNNING) {
  949. svg_process(sample->cpu,
  950. sample->start_time,
  951. sample->end_time,
  952. p->pid,
  953. c->comm,
  954. sample->backtrace);
  955. }
  956. sample = sample->next;
  957. }
  958. c = c->next;
  959. }
  960. p = p->next;
  961. }
  962. }
  963. static void draw_io_bars(struct timechart *tchart)
  964. {
  965. const char *suf;
  966. double bytes;
  967. char comm[256];
  968. struct per_pid *p;
  969. struct per_pidcomm *c;
  970. struct io_sample *sample;
  971. int Y = 1;
  972. p = tchart->all_data;
  973. while (p) {
  974. c = p->all;
  975. while (c) {
  976. if (!c->display) {
  977. c->Y = 0;
  978. c = c->next;
  979. continue;
  980. }
  981. svg_box(Y, c->start_time, c->end_time, "process3");
  982. sample = c->io_samples;
  983. for (sample = c->io_samples; sample; sample = sample->next) {
  984. double h = (double)sample->bytes / c->max_bytes;
  985. if (tchart->skip_eagain &&
  986. sample->err == -EAGAIN)
  987. continue;
  988. if (sample->err)
  989. h = 1;
  990. if (sample->type == IOTYPE_SYNC)
  991. svg_fbox(Y,
  992. sample->start_time,
  993. sample->end_time,
  994. 1,
  995. sample->err ? "error" : "sync",
  996. sample->fd,
  997. sample->err,
  998. sample->merges);
  999. else if (sample->type == IOTYPE_POLL)
  1000. svg_fbox(Y,
  1001. sample->start_time,
  1002. sample->end_time,
  1003. 1,
  1004. sample->err ? "error" : "poll",
  1005. sample->fd,
  1006. sample->err,
  1007. sample->merges);
  1008. else if (sample->type == IOTYPE_READ)
  1009. svg_ubox(Y,
  1010. sample->start_time,
  1011. sample->end_time,
  1012. h,
  1013. sample->err ? "error" : "disk",
  1014. sample->fd,
  1015. sample->err,
  1016. sample->merges);
  1017. else if (sample->type == IOTYPE_WRITE)
  1018. svg_lbox(Y,
  1019. sample->start_time,
  1020. sample->end_time,
  1021. h,
  1022. sample->err ? "error" : "disk",
  1023. sample->fd,
  1024. sample->err,
  1025. sample->merges);
  1026. else if (sample->type == IOTYPE_RX)
  1027. svg_ubox(Y,
  1028. sample->start_time,
  1029. sample->end_time,
  1030. h,
  1031. sample->err ? "error" : "net",
  1032. sample->fd,
  1033. sample->err,
  1034. sample->merges);
  1035. else if (sample->type == IOTYPE_TX)
  1036. svg_lbox(Y,
  1037. sample->start_time,
  1038. sample->end_time,
  1039. h,
  1040. sample->err ? "error" : "net",
  1041. sample->fd,
  1042. sample->err,
  1043. sample->merges);
  1044. }
  1045. suf = "";
  1046. bytes = c->total_bytes;
  1047. if (bytes > 1024) {
  1048. bytes = bytes / 1024;
  1049. suf = "K";
  1050. }
  1051. if (bytes > 1024) {
  1052. bytes = bytes / 1024;
  1053. suf = "M";
  1054. }
  1055. if (bytes > 1024) {
  1056. bytes = bytes / 1024;
  1057. suf = "G";
  1058. }
  1059. sprintf(comm, "%s:%i (%3.1f %sbytes)", c->comm ?: "", p->pid, bytes, suf);
  1060. svg_text(Y, c->start_time, comm);
  1061. c->Y = Y;
  1062. Y++;
  1063. c = c->next;
  1064. }
  1065. p = p->next;
  1066. }
  1067. }
  1068. static void draw_process_bars(struct timechart *tchart)
  1069. {
  1070. struct per_pid *p;
  1071. struct per_pidcomm *c;
  1072. struct cpu_sample *sample;
  1073. int Y = 0;
  1074. Y = 2 * tchart->numcpus + 2;
  1075. p = tchart->all_data;
  1076. while (p) {
  1077. c = p->all;
  1078. while (c) {
  1079. if (!c->display) {
  1080. c->Y = 0;
  1081. c = c->next;
  1082. continue;
  1083. }
  1084. svg_box(Y, c->start_time, c->end_time, "process");
  1085. sample = c->samples;
  1086. while (sample) {
  1087. if (sample->type == TYPE_RUNNING)
  1088. svg_running(Y, sample->cpu,
  1089. sample->start_time,
  1090. sample->end_time,
  1091. sample->backtrace);
  1092. if (sample->type == TYPE_BLOCKED)
  1093. svg_blocked(Y, sample->cpu,
  1094. sample->start_time,
  1095. sample->end_time,
  1096. sample->backtrace);
  1097. if (sample->type == TYPE_WAITING)
  1098. svg_waiting(Y, sample->cpu,
  1099. sample->start_time,
  1100. sample->end_time,
  1101. sample->backtrace);
  1102. sample = sample->next;
  1103. }
  1104. if (c->comm) {
  1105. char comm[256];
  1106. if (c->total_time > 5000000000) /* 5 seconds */
  1107. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  1108. else
  1109. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  1110. svg_text(Y, c->start_time, comm);
  1111. }
  1112. c->Y = Y;
  1113. Y++;
  1114. c = c->next;
  1115. }
  1116. p = p->next;
  1117. }
  1118. }
  1119. static void add_process_filter(const char *string)
  1120. {
  1121. int pid = strtoull(string, NULL, 10);
  1122. struct process_filter *filt = malloc(sizeof(*filt));
  1123. if (!filt)
  1124. return;
  1125. filt->name = strdup(string);
  1126. filt->pid = pid;
  1127. filt->next = process_filter;
  1128. process_filter = filt;
  1129. }
  1130. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  1131. {
  1132. struct process_filter *filt;
  1133. if (!process_filter)
  1134. return 1;
  1135. filt = process_filter;
  1136. while (filt) {
  1137. if (filt->pid && p->pid == filt->pid)
  1138. return 1;
  1139. if (strcmp(filt->name, c->comm) == 0)
  1140. return 1;
  1141. filt = filt->next;
  1142. }
  1143. return 0;
  1144. }
  1145. static int determine_display_tasks_filtered(struct timechart *tchart)
  1146. {
  1147. struct per_pid *p;
  1148. struct per_pidcomm *c;
  1149. int count = 0;
  1150. p = tchart->all_data;
  1151. while (p) {
  1152. p->display = 0;
  1153. if (p->start_time == 1)
  1154. p->start_time = tchart->first_time;
  1155. /* no exit marker, task kept running to the end */
  1156. if (p->end_time == 0)
  1157. p->end_time = tchart->last_time;
  1158. c = p->all;
  1159. while (c) {
  1160. c->display = 0;
  1161. if (c->start_time == 1)
  1162. c->start_time = tchart->first_time;
  1163. if (passes_filter(p, c)) {
  1164. c->display = 1;
  1165. p->display = 1;
  1166. count++;
  1167. }
  1168. if (c->end_time == 0)
  1169. c->end_time = tchart->last_time;
  1170. c = c->next;
  1171. }
  1172. p = p->next;
  1173. }
  1174. return count;
  1175. }
  1176. static int determine_display_tasks(struct timechart *tchart, u64 threshold)
  1177. {
  1178. struct per_pid *p;
  1179. struct per_pidcomm *c;
  1180. int count = 0;
  1181. p = tchart->all_data;
  1182. while (p) {
  1183. p->display = 0;
  1184. if (p->start_time == 1)
  1185. p->start_time = tchart->first_time;
  1186. /* no exit marker, task kept running to the end */
  1187. if (p->end_time == 0)
  1188. p->end_time = tchart->last_time;
  1189. if (p->total_time >= threshold)
  1190. p->display = 1;
  1191. c = p->all;
  1192. while (c) {
  1193. c->display = 0;
  1194. if (c->start_time == 1)
  1195. c->start_time = tchart->first_time;
  1196. if (c->total_time >= threshold) {
  1197. c->display = 1;
  1198. count++;
  1199. }
  1200. if (c->end_time == 0)
  1201. c->end_time = tchart->last_time;
  1202. c = c->next;
  1203. }
  1204. p = p->next;
  1205. }
  1206. return count;
  1207. }
  1208. static int determine_display_io_tasks(struct timechart *timechart, u64 threshold)
  1209. {
  1210. struct per_pid *p;
  1211. struct per_pidcomm *c;
  1212. int count = 0;
  1213. p = timechart->all_data;
  1214. while (p) {
  1215. /* no exit marker, task kept running to the end */
  1216. if (p->end_time == 0)
  1217. p->end_time = timechart->last_time;
  1218. c = p->all;
  1219. while (c) {
  1220. c->display = 0;
  1221. if (c->total_bytes >= threshold) {
  1222. c->display = 1;
  1223. count++;
  1224. }
  1225. if (c->end_time == 0)
  1226. c->end_time = timechart->last_time;
  1227. c = c->next;
  1228. }
  1229. p = p->next;
  1230. }
  1231. return count;
  1232. }
  1233. #define BYTES_THRESH (1 * 1024 * 1024)
  1234. #define TIME_THRESH 10000000
  1235. static void write_svg_file(struct timechart *tchart, const char *filename)
  1236. {
  1237. u64 i;
  1238. int count;
  1239. int thresh = tchart->io_events ? BYTES_THRESH : TIME_THRESH;
  1240. if (tchart->power_only)
  1241. tchart->proc_num = 0;
  1242. /* We'd like to show at least proc_num tasks;
  1243. * be less picky if we have fewer */
  1244. do {
  1245. if (process_filter)
  1246. count = determine_display_tasks_filtered(tchart);
  1247. else if (tchart->io_events)
  1248. count = determine_display_io_tasks(tchart, thresh);
  1249. else
  1250. count = determine_display_tasks(tchart, thresh);
  1251. thresh /= 10;
  1252. } while (!process_filter && thresh && count < tchart->proc_num);
  1253. if (!tchart->proc_num)
  1254. count = 0;
  1255. if (tchart->io_events) {
  1256. open_svg(filename, 0, count, tchart->first_time, tchart->last_time);
  1257. svg_time_grid(0.5);
  1258. svg_io_legenda();
  1259. draw_io_bars(tchart);
  1260. } else {
  1261. open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
  1262. svg_time_grid(0);
  1263. svg_legenda();
  1264. for (i = 0; i < tchart->numcpus; i++)
  1265. svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
  1266. draw_cpu_usage(tchart);
  1267. if (tchart->proc_num)
  1268. draw_process_bars(tchart);
  1269. if (!tchart->tasks_only)
  1270. draw_c_p_states(tchart);
  1271. if (tchart->proc_num)
  1272. draw_wakeups(tchart);
  1273. }
  1274. svg_close();
  1275. }
  1276. static int process_header(struct perf_file_section *section __maybe_unused,
  1277. struct perf_header *ph,
  1278. int feat,
  1279. int fd __maybe_unused,
  1280. void *data)
  1281. {
  1282. struct timechart *tchart = data;
  1283. switch (feat) {
  1284. case HEADER_NRCPUS:
  1285. tchart->numcpus = ph->env.nr_cpus_avail;
  1286. break;
  1287. case HEADER_CPU_TOPOLOGY:
  1288. if (!tchart->topology)
  1289. break;
  1290. if (svg_build_topology_map(ph->env.sibling_cores,
  1291. ph->env.nr_sibling_cores,
  1292. ph->env.sibling_threads,
  1293. ph->env.nr_sibling_threads))
  1294. fprintf(stderr, "problem building topology\n");
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. return 0;
  1300. }
  1301. static int __cmd_timechart(struct timechart *tchart, const char *output_name)
  1302. {
  1303. const struct perf_evsel_str_handler power_tracepoints[] = {
  1304. { "power:cpu_idle", process_sample_cpu_idle },
  1305. { "power:cpu_frequency", process_sample_cpu_frequency },
  1306. { "sched:sched_wakeup", process_sample_sched_wakeup },
  1307. { "sched:sched_switch", process_sample_sched_switch },
  1308. #ifdef SUPPORT_OLD_POWER_EVENTS
  1309. { "power:power_start", process_sample_power_start },
  1310. { "power:power_end", process_sample_power_end },
  1311. { "power:power_frequency", process_sample_power_frequency },
  1312. #endif
  1313. { "syscalls:sys_enter_read", process_enter_read },
  1314. { "syscalls:sys_enter_pread64", process_enter_read },
  1315. { "syscalls:sys_enter_readv", process_enter_read },
  1316. { "syscalls:sys_enter_preadv", process_enter_read },
  1317. { "syscalls:sys_enter_write", process_enter_write },
  1318. { "syscalls:sys_enter_pwrite64", process_enter_write },
  1319. { "syscalls:sys_enter_writev", process_enter_write },
  1320. { "syscalls:sys_enter_pwritev", process_enter_write },
  1321. { "syscalls:sys_enter_sync", process_enter_sync },
  1322. { "syscalls:sys_enter_sync_file_range", process_enter_sync },
  1323. { "syscalls:sys_enter_fsync", process_enter_sync },
  1324. { "syscalls:sys_enter_msync", process_enter_sync },
  1325. { "syscalls:sys_enter_recvfrom", process_enter_rx },
  1326. { "syscalls:sys_enter_recvmmsg", process_enter_rx },
  1327. { "syscalls:sys_enter_recvmsg", process_enter_rx },
  1328. { "syscalls:sys_enter_sendto", process_enter_tx },
  1329. { "syscalls:sys_enter_sendmsg", process_enter_tx },
  1330. { "syscalls:sys_enter_sendmmsg", process_enter_tx },
  1331. { "syscalls:sys_enter_epoll_pwait", process_enter_poll },
  1332. { "syscalls:sys_enter_epoll_wait", process_enter_poll },
  1333. { "syscalls:sys_enter_poll", process_enter_poll },
  1334. { "syscalls:sys_enter_ppoll", process_enter_poll },
  1335. { "syscalls:sys_enter_pselect6", process_enter_poll },
  1336. { "syscalls:sys_enter_select", process_enter_poll },
  1337. { "syscalls:sys_exit_read", process_exit_read },
  1338. { "syscalls:sys_exit_pread64", process_exit_read },
  1339. { "syscalls:sys_exit_readv", process_exit_read },
  1340. { "syscalls:sys_exit_preadv", process_exit_read },
  1341. { "syscalls:sys_exit_write", process_exit_write },
  1342. { "syscalls:sys_exit_pwrite64", process_exit_write },
  1343. { "syscalls:sys_exit_writev", process_exit_write },
  1344. { "syscalls:sys_exit_pwritev", process_exit_write },
  1345. { "syscalls:sys_exit_sync", process_exit_sync },
  1346. { "syscalls:sys_exit_sync_file_range", process_exit_sync },
  1347. { "syscalls:sys_exit_fsync", process_exit_sync },
  1348. { "syscalls:sys_exit_msync", process_exit_sync },
  1349. { "syscalls:sys_exit_recvfrom", process_exit_rx },
  1350. { "syscalls:sys_exit_recvmmsg", process_exit_rx },
  1351. { "syscalls:sys_exit_recvmsg", process_exit_rx },
  1352. { "syscalls:sys_exit_sendto", process_exit_tx },
  1353. { "syscalls:sys_exit_sendmsg", process_exit_tx },
  1354. { "syscalls:sys_exit_sendmmsg", process_exit_tx },
  1355. { "syscalls:sys_exit_epoll_pwait", process_exit_poll },
  1356. { "syscalls:sys_exit_epoll_wait", process_exit_poll },
  1357. { "syscalls:sys_exit_poll", process_exit_poll },
  1358. { "syscalls:sys_exit_ppoll", process_exit_poll },
  1359. { "syscalls:sys_exit_pselect6", process_exit_poll },
  1360. { "syscalls:sys_exit_select", process_exit_poll },
  1361. };
  1362. struct perf_data_file file = {
  1363. .path = input_name,
  1364. .mode = PERF_DATA_MODE_READ,
  1365. .force = tchart->force,
  1366. };
  1367. struct perf_session *session = perf_session__new(&file, false,
  1368. &tchart->tool);
  1369. int ret = -EINVAL;
  1370. if (session == NULL)
  1371. return -1;
  1372. symbol__init(&session->header.env);
  1373. (void)perf_header__process_sections(&session->header,
  1374. perf_data_file__fd(session->file),
  1375. tchart,
  1376. process_header);
  1377. if (!perf_session__has_traces(session, "timechart record"))
  1378. goto out_delete;
  1379. if (perf_session__set_tracepoints_handlers(session,
  1380. power_tracepoints)) {
  1381. pr_err("Initializing session tracepoint handlers failed\n");
  1382. goto out_delete;
  1383. }
  1384. ret = perf_session__process_events(session);
  1385. if (ret)
  1386. goto out_delete;
  1387. end_sample_processing(tchart);
  1388. sort_pids(tchart);
  1389. write_svg_file(tchart, output_name);
  1390. pr_info("Written %2.1f seconds of trace to %s.\n",
  1391. (tchart->last_time - tchart->first_time) / 1000000000.0, output_name);
  1392. out_delete:
  1393. perf_session__delete(session);
  1394. return ret;
  1395. }
  1396. static int timechart__io_record(int argc, const char **argv)
  1397. {
  1398. unsigned int rec_argc, i;
  1399. const char **rec_argv;
  1400. const char **p;
  1401. char *filter = NULL;
  1402. const char * const common_args[] = {
  1403. "record", "-a", "-R", "-c", "1",
  1404. };
  1405. unsigned int common_args_nr = ARRAY_SIZE(common_args);
  1406. const char * const disk_events[] = {
  1407. "syscalls:sys_enter_read",
  1408. "syscalls:sys_enter_pread64",
  1409. "syscalls:sys_enter_readv",
  1410. "syscalls:sys_enter_preadv",
  1411. "syscalls:sys_enter_write",
  1412. "syscalls:sys_enter_pwrite64",
  1413. "syscalls:sys_enter_writev",
  1414. "syscalls:sys_enter_pwritev",
  1415. "syscalls:sys_enter_sync",
  1416. "syscalls:sys_enter_sync_file_range",
  1417. "syscalls:sys_enter_fsync",
  1418. "syscalls:sys_enter_msync",
  1419. "syscalls:sys_exit_read",
  1420. "syscalls:sys_exit_pread64",
  1421. "syscalls:sys_exit_readv",
  1422. "syscalls:sys_exit_preadv",
  1423. "syscalls:sys_exit_write",
  1424. "syscalls:sys_exit_pwrite64",
  1425. "syscalls:sys_exit_writev",
  1426. "syscalls:sys_exit_pwritev",
  1427. "syscalls:sys_exit_sync",
  1428. "syscalls:sys_exit_sync_file_range",
  1429. "syscalls:sys_exit_fsync",
  1430. "syscalls:sys_exit_msync",
  1431. };
  1432. unsigned int disk_events_nr = ARRAY_SIZE(disk_events);
  1433. const char * const net_events[] = {
  1434. "syscalls:sys_enter_recvfrom",
  1435. "syscalls:sys_enter_recvmmsg",
  1436. "syscalls:sys_enter_recvmsg",
  1437. "syscalls:sys_enter_sendto",
  1438. "syscalls:sys_enter_sendmsg",
  1439. "syscalls:sys_enter_sendmmsg",
  1440. "syscalls:sys_exit_recvfrom",
  1441. "syscalls:sys_exit_recvmmsg",
  1442. "syscalls:sys_exit_recvmsg",
  1443. "syscalls:sys_exit_sendto",
  1444. "syscalls:sys_exit_sendmsg",
  1445. "syscalls:sys_exit_sendmmsg",
  1446. };
  1447. unsigned int net_events_nr = ARRAY_SIZE(net_events);
  1448. const char * const poll_events[] = {
  1449. "syscalls:sys_enter_epoll_pwait",
  1450. "syscalls:sys_enter_epoll_wait",
  1451. "syscalls:sys_enter_poll",
  1452. "syscalls:sys_enter_ppoll",
  1453. "syscalls:sys_enter_pselect6",
  1454. "syscalls:sys_enter_select",
  1455. "syscalls:sys_exit_epoll_pwait",
  1456. "syscalls:sys_exit_epoll_wait",
  1457. "syscalls:sys_exit_poll",
  1458. "syscalls:sys_exit_ppoll",
  1459. "syscalls:sys_exit_pselect6",
  1460. "syscalls:sys_exit_select",
  1461. };
  1462. unsigned int poll_events_nr = ARRAY_SIZE(poll_events);
  1463. rec_argc = common_args_nr +
  1464. disk_events_nr * 4 +
  1465. net_events_nr * 4 +
  1466. poll_events_nr * 4 +
  1467. argc;
  1468. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1469. if (rec_argv == NULL)
  1470. return -ENOMEM;
  1471. if (asprintf(&filter, "common_pid != %d", getpid()) < 0)
  1472. return -ENOMEM;
  1473. p = rec_argv;
  1474. for (i = 0; i < common_args_nr; i++)
  1475. *p++ = strdup(common_args[i]);
  1476. for (i = 0; i < disk_events_nr; i++) {
  1477. if (!is_valid_tracepoint(disk_events[i])) {
  1478. rec_argc -= 4;
  1479. continue;
  1480. }
  1481. *p++ = "-e";
  1482. *p++ = strdup(disk_events[i]);
  1483. *p++ = "--filter";
  1484. *p++ = filter;
  1485. }
  1486. for (i = 0; i < net_events_nr; i++) {
  1487. if (!is_valid_tracepoint(net_events[i])) {
  1488. rec_argc -= 4;
  1489. continue;
  1490. }
  1491. *p++ = "-e";
  1492. *p++ = strdup(net_events[i]);
  1493. *p++ = "--filter";
  1494. *p++ = filter;
  1495. }
  1496. for (i = 0; i < poll_events_nr; i++) {
  1497. if (!is_valid_tracepoint(poll_events[i])) {
  1498. rec_argc -= 4;
  1499. continue;
  1500. }
  1501. *p++ = "-e";
  1502. *p++ = strdup(poll_events[i]);
  1503. *p++ = "--filter";
  1504. *p++ = filter;
  1505. }
  1506. for (i = 0; i < (unsigned int)argc; i++)
  1507. *p++ = argv[i];
  1508. return cmd_record(rec_argc, rec_argv, NULL);
  1509. }
  1510. static int timechart__record(struct timechart *tchart, int argc, const char **argv)
  1511. {
  1512. unsigned int rec_argc, i, j;
  1513. const char **rec_argv;
  1514. const char **p;
  1515. unsigned int record_elems;
  1516. const char * const common_args[] = {
  1517. "record", "-a", "-R", "-c", "1",
  1518. };
  1519. unsigned int common_args_nr = ARRAY_SIZE(common_args);
  1520. const char * const backtrace_args[] = {
  1521. "-g",
  1522. };
  1523. unsigned int backtrace_args_no = ARRAY_SIZE(backtrace_args);
  1524. const char * const power_args[] = {
  1525. "-e", "power:cpu_frequency",
  1526. "-e", "power:cpu_idle",
  1527. };
  1528. unsigned int power_args_nr = ARRAY_SIZE(power_args);
  1529. const char * const old_power_args[] = {
  1530. #ifdef SUPPORT_OLD_POWER_EVENTS
  1531. "-e", "power:power_start",
  1532. "-e", "power:power_end",
  1533. "-e", "power:power_frequency",
  1534. #endif
  1535. };
  1536. unsigned int old_power_args_nr = ARRAY_SIZE(old_power_args);
  1537. const char * const tasks_args[] = {
  1538. "-e", "sched:sched_wakeup",
  1539. "-e", "sched:sched_switch",
  1540. };
  1541. unsigned int tasks_args_nr = ARRAY_SIZE(tasks_args);
  1542. #ifdef SUPPORT_OLD_POWER_EVENTS
  1543. if (!is_valid_tracepoint("power:cpu_idle") &&
  1544. is_valid_tracepoint("power:power_start")) {
  1545. use_old_power_events = 1;
  1546. power_args_nr = 0;
  1547. } else {
  1548. old_power_args_nr = 0;
  1549. }
  1550. #endif
  1551. if (tchart->power_only)
  1552. tasks_args_nr = 0;
  1553. if (tchart->tasks_only) {
  1554. power_args_nr = 0;
  1555. old_power_args_nr = 0;
  1556. }
  1557. if (!tchart->with_backtrace)
  1558. backtrace_args_no = 0;
  1559. record_elems = common_args_nr + tasks_args_nr +
  1560. power_args_nr + old_power_args_nr + backtrace_args_no;
  1561. rec_argc = record_elems + argc;
  1562. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1563. if (rec_argv == NULL)
  1564. return -ENOMEM;
  1565. p = rec_argv;
  1566. for (i = 0; i < common_args_nr; i++)
  1567. *p++ = strdup(common_args[i]);
  1568. for (i = 0; i < backtrace_args_no; i++)
  1569. *p++ = strdup(backtrace_args[i]);
  1570. for (i = 0; i < tasks_args_nr; i++)
  1571. *p++ = strdup(tasks_args[i]);
  1572. for (i = 0; i < power_args_nr; i++)
  1573. *p++ = strdup(power_args[i]);
  1574. for (i = 0; i < old_power_args_nr; i++)
  1575. *p++ = strdup(old_power_args[i]);
  1576. for (j = 0; j < (unsigned int)argc; j++)
  1577. *p++ = argv[j];
  1578. return cmd_record(rec_argc, rec_argv, NULL);
  1579. }
  1580. static int
  1581. parse_process(const struct option *opt __maybe_unused, const char *arg,
  1582. int __maybe_unused unset)
  1583. {
  1584. if (arg)
  1585. add_process_filter(arg);
  1586. return 0;
  1587. }
  1588. static int
  1589. parse_highlight(const struct option *opt __maybe_unused, const char *arg,
  1590. int __maybe_unused unset)
  1591. {
  1592. unsigned long duration = strtoul(arg, NULL, 0);
  1593. if (svg_highlight || svg_highlight_name)
  1594. return -1;
  1595. if (duration)
  1596. svg_highlight = duration;
  1597. else
  1598. svg_highlight_name = strdup(arg);
  1599. return 0;
  1600. }
  1601. static int
  1602. parse_time(const struct option *opt, const char *arg, int __maybe_unused unset)
  1603. {
  1604. char unit = 'n';
  1605. u64 *value = opt->value;
  1606. if (sscanf(arg, "%" PRIu64 "%cs", value, &unit) > 0) {
  1607. switch (unit) {
  1608. case 'm':
  1609. *value *= 1000000;
  1610. break;
  1611. case 'u':
  1612. *value *= 1000;
  1613. break;
  1614. case 'n':
  1615. break;
  1616. default:
  1617. return -1;
  1618. }
  1619. }
  1620. return 0;
  1621. }
  1622. int cmd_timechart(int argc, const char **argv,
  1623. const char *prefix __maybe_unused)
  1624. {
  1625. struct timechart tchart = {
  1626. .tool = {
  1627. .comm = process_comm_event,
  1628. .fork = process_fork_event,
  1629. .exit = process_exit_event,
  1630. .sample = process_sample_event,
  1631. .ordered_events = true,
  1632. },
  1633. .proc_num = 15,
  1634. .min_time = 1000000,
  1635. .merge_dist = 1000,
  1636. };
  1637. const char *output_name = "output.svg";
  1638. const struct option timechart_options[] = {
  1639. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1640. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1641. OPT_INTEGER('w', "width", &svg_page_width, "page width"),
  1642. OPT_CALLBACK(0, "highlight", NULL, "duration or task name",
  1643. "highlight tasks. Pass duration in ns or process name.",
  1644. parse_highlight),
  1645. OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
  1646. OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
  1647. "output processes data only"),
  1648. OPT_CALLBACK('p', "process", NULL, "process",
  1649. "process selector. Pass a pid or process name.",
  1650. parse_process),
  1651. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1652. "Look for files with symbols relative to this directory"),
  1653. OPT_INTEGER('n', "proc-num", &tchart.proc_num,
  1654. "min. number of tasks to print"),
  1655. OPT_BOOLEAN('t', "topology", &tchart.topology,
  1656. "sort CPUs according to topology"),
  1657. OPT_BOOLEAN(0, "io-skip-eagain", &tchart.skip_eagain,
  1658. "skip EAGAIN errors"),
  1659. OPT_CALLBACK(0, "io-min-time", &tchart.min_time, "time",
  1660. "all IO faster than min-time will visually appear longer",
  1661. parse_time),
  1662. OPT_CALLBACK(0, "io-merge-dist", &tchart.merge_dist, "time",
  1663. "merge events that are merge-dist us apart",
  1664. parse_time),
  1665. OPT_BOOLEAN('f', "force", &tchart.force, "don't complain, do it"),
  1666. OPT_END()
  1667. };
  1668. const char * const timechart_subcommands[] = { "record", NULL };
  1669. const char *timechart_usage[] = {
  1670. "perf timechart [<options>] {record}",
  1671. NULL
  1672. };
  1673. const struct option timechart_record_options[] = {
  1674. OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
  1675. OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
  1676. "output processes data only"),
  1677. OPT_BOOLEAN('I', "io-only", &tchart.io_only,
  1678. "record only IO data"),
  1679. OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
  1680. OPT_END()
  1681. };
  1682. const char * const timechart_record_usage[] = {
  1683. "perf timechart record [<options>]",
  1684. NULL
  1685. };
  1686. argc = parse_options_subcommand(argc, argv, timechart_options, timechart_subcommands,
  1687. timechart_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  1688. if (tchart.power_only && tchart.tasks_only) {
  1689. pr_err("-P and -T options cannot be used at the same time.\n");
  1690. return -1;
  1691. }
  1692. if (argc && !strncmp(argv[0], "rec", 3)) {
  1693. argc = parse_options(argc, argv, timechart_record_options,
  1694. timechart_record_usage,
  1695. PARSE_OPT_STOP_AT_NON_OPTION);
  1696. if (tchart.power_only && tchart.tasks_only) {
  1697. pr_err("-P and -T options cannot be used at the same time.\n");
  1698. return -1;
  1699. }
  1700. if (tchart.io_only)
  1701. return timechart__io_record(argc, argv);
  1702. else
  1703. return timechart__record(&tchart, argc, argv);
  1704. } else if (argc)
  1705. usage_with_options(timechart_usage, timechart_options);
  1706. setup_pager();
  1707. return __cmd_timechart(&tchart, output_name);
  1708. }