builtin-timechart.c 45 KB

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