builtin-timechart.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 timechart {
  40. struct perf_tool tool;
  41. struct per_pid *all_data;
  42. struct power_event *power_events;
  43. int proc_num;
  44. unsigned int numcpus;
  45. u64 min_freq, /* Lowest CPU frequency seen */
  46. max_freq, /* Highest CPU frequency seen */
  47. turbo_frequency,
  48. first_time, last_time;
  49. bool power_only,
  50. tasks_only,
  51. with_backtrace;
  52. };
  53. struct per_pidcomm;
  54. struct cpu_sample;
  55. /*
  56. * Datastructure layout:
  57. * We keep an list of "pid"s, matching the kernels notion of a task struct.
  58. * Each "pid" entry, has a list of "comm"s.
  59. * this is because we want to track different programs different, while
  60. * exec will reuse the original pid (by design).
  61. * Each comm has a list of samples that will be used to draw
  62. * final graph.
  63. */
  64. struct per_pid {
  65. struct per_pid *next;
  66. int pid;
  67. int ppid;
  68. u64 start_time;
  69. u64 end_time;
  70. u64 total_time;
  71. int display;
  72. struct per_pidcomm *all;
  73. struct per_pidcomm *current;
  74. };
  75. struct per_pidcomm {
  76. struct per_pidcomm *next;
  77. u64 start_time;
  78. u64 end_time;
  79. u64 total_time;
  80. int Y;
  81. int display;
  82. long state;
  83. u64 state_since;
  84. char *comm;
  85. struct cpu_sample *samples;
  86. };
  87. struct sample_wrapper {
  88. struct sample_wrapper *next;
  89. u64 timestamp;
  90. unsigned char data[0];
  91. };
  92. #define TYPE_NONE 0
  93. #define TYPE_RUNNING 1
  94. #define TYPE_WAITING 2
  95. #define TYPE_BLOCKED 3
  96. struct cpu_sample {
  97. struct cpu_sample *next;
  98. u64 start_time;
  99. u64 end_time;
  100. int type;
  101. int cpu;
  102. const char *backtrace;
  103. };
  104. #define CSTATE 1
  105. #define PSTATE 2
  106. struct power_event {
  107. struct power_event *next;
  108. int type;
  109. int state;
  110. u64 start_time;
  111. u64 end_time;
  112. int cpu;
  113. };
  114. struct wake_event {
  115. struct wake_event *next;
  116. int waker;
  117. int wakee;
  118. u64 time;
  119. const char *backtrace;
  120. };
  121. static struct wake_event *wake_events;
  122. struct process_filter {
  123. char *name;
  124. int pid;
  125. struct process_filter *next;
  126. };
  127. static struct process_filter *process_filter;
  128. static struct per_pid *find_create_pid(struct timechart *tchart, int pid)
  129. {
  130. struct per_pid *cursor = tchart->all_data;
  131. while (cursor) {
  132. if (cursor->pid == pid)
  133. return cursor;
  134. cursor = cursor->next;
  135. }
  136. cursor = zalloc(sizeof(*cursor));
  137. assert(cursor != NULL);
  138. cursor->pid = pid;
  139. cursor->next = tchart->all_data;
  140. tchart->all_data = cursor;
  141. return cursor;
  142. }
  143. static void pid_set_comm(struct timechart *tchart, int pid, char *comm)
  144. {
  145. struct per_pid *p;
  146. struct per_pidcomm *c;
  147. p = find_create_pid(tchart, pid);
  148. c = p->all;
  149. while (c) {
  150. if (c->comm && strcmp(c->comm, comm) == 0) {
  151. p->current = c;
  152. return;
  153. }
  154. if (!c->comm) {
  155. c->comm = strdup(comm);
  156. p->current = c;
  157. return;
  158. }
  159. c = c->next;
  160. }
  161. c = zalloc(sizeof(*c));
  162. assert(c != NULL);
  163. c->comm = strdup(comm);
  164. p->current = c;
  165. c->next = p->all;
  166. p->all = c;
  167. }
  168. static void pid_fork(struct timechart *tchart, int pid, int ppid, u64 timestamp)
  169. {
  170. struct per_pid *p, *pp;
  171. p = find_create_pid(tchart, pid);
  172. pp = find_create_pid(tchart, ppid);
  173. p->ppid = ppid;
  174. if (pp->current && pp->current->comm && !p->current)
  175. pid_set_comm(tchart, pid, pp->current->comm);
  176. p->start_time = timestamp;
  177. if (p->current) {
  178. p->current->start_time = timestamp;
  179. p->current->state_since = timestamp;
  180. }
  181. }
  182. static void pid_exit(struct timechart *tchart, int pid, u64 timestamp)
  183. {
  184. struct per_pid *p;
  185. p = find_create_pid(tchart, pid);
  186. p->end_time = timestamp;
  187. if (p->current)
  188. p->current->end_time = timestamp;
  189. }
  190. static void pid_put_sample(struct timechart *tchart, int pid, int type,
  191. unsigned int cpu, u64 start, u64 end,
  192. const char *backtrace)
  193. {
  194. struct per_pid *p;
  195. struct per_pidcomm *c;
  196. struct cpu_sample *sample;
  197. p = find_create_pid(tchart, pid);
  198. c = p->current;
  199. if (!c) {
  200. c = zalloc(sizeof(*c));
  201. assert(c != NULL);
  202. p->current = c;
  203. c->next = p->all;
  204. p->all = c;
  205. }
  206. sample = zalloc(sizeof(*sample));
  207. assert(sample != NULL);
  208. sample->start_time = start;
  209. sample->end_time = end;
  210. sample->type = type;
  211. sample->next = c->samples;
  212. sample->cpu = cpu;
  213. sample->backtrace = backtrace;
  214. c->samples = sample;
  215. if (sample->type == TYPE_RUNNING && end > start && start > 0) {
  216. c->total_time += (end-start);
  217. p->total_time += (end-start);
  218. }
  219. if (c->start_time == 0 || c->start_time > start)
  220. c->start_time = start;
  221. if (p->start_time == 0 || p->start_time > start)
  222. p->start_time = start;
  223. }
  224. #define MAX_CPUS 4096
  225. static u64 cpus_cstate_start_times[MAX_CPUS];
  226. static int cpus_cstate_state[MAX_CPUS];
  227. static u64 cpus_pstate_start_times[MAX_CPUS];
  228. static u64 cpus_pstate_state[MAX_CPUS];
  229. static int process_comm_event(struct perf_tool *tool,
  230. union perf_event *event,
  231. struct perf_sample *sample __maybe_unused,
  232. struct machine *machine __maybe_unused)
  233. {
  234. struct timechart *tchart = container_of(tool, struct timechart, tool);
  235. pid_set_comm(tchart, event->comm.tid, event->comm.comm);
  236. return 0;
  237. }
  238. static int process_fork_event(struct perf_tool *tool,
  239. union perf_event *event,
  240. struct perf_sample *sample __maybe_unused,
  241. struct machine *machine __maybe_unused)
  242. {
  243. struct timechart *tchart = container_of(tool, struct timechart, tool);
  244. pid_fork(tchart, event->fork.pid, event->fork.ppid, event->fork.time);
  245. return 0;
  246. }
  247. static int process_exit_event(struct perf_tool *tool,
  248. union perf_event *event,
  249. struct perf_sample *sample __maybe_unused,
  250. struct machine *machine __maybe_unused)
  251. {
  252. struct timechart *tchart = container_of(tool, struct timechart, tool);
  253. pid_exit(tchart, event->fork.pid, event->fork.time);
  254. return 0;
  255. }
  256. #ifdef SUPPORT_OLD_POWER_EVENTS
  257. static int use_old_power_events;
  258. #endif
  259. static void c_state_start(int cpu, u64 timestamp, int state)
  260. {
  261. cpus_cstate_start_times[cpu] = timestamp;
  262. cpus_cstate_state[cpu] = state;
  263. }
  264. static void c_state_end(struct timechart *tchart, int cpu, u64 timestamp)
  265. {
  266. struct power_event *pwr = zalloc(sizeof(*pwr));
  267. if (!pwr)
  268. return;
  269. pwr->state = cpus_cstate_state[cpu];
  270. pwr->start_time = cpus_cstate_start_times[cpu];
  271. pwr->end_time = timestamp;
  272. pwr->cpu = cpu;
  273. pwr->type = CSTATE;
  274. pwr->next = tchart->power_events;
  275. tchart->power_events = pwr;
  276. }
  277. static void p_state_change(struct timechart *tchart, int cpu, u64 timestamp, u64 new_freq)
  278. {
  279. struct power_event *pwr;
  280. if (new_freq > 8000000) /* detect invalid data */
  281. return;
  282. pwr = zalloc(sizeof(*pwr));
  283. if (!pwr)
  284. return;
  285. pwr->state = cpus_pstate_state[cpu];
  286. pwr->start_time = cpus_pstate_start_times[cpu];
  287. pwr->end_time = timestamp;
  288. pwr->cpu = cpu;
  289. pwr->type = PSTATE;
  290. pwr->next = tchart->power_events;
  291. if (!pwr->start_time)
  292. pwr->start_time = tchart->first_time;
  293. tchart->power_events = pwr;
  294. cpus_pstate_state[cpu] = new_freq;
  295. cpus_pstate_start_times[cpu] = timestamp;
  296. if ((u64)new_freq > tchart->max_freq)
  297. tchart->max_freq = new_freq;
  298. if (new_freq < tchart->min_freq || tchart->min_freq == 0)
  299. tchart->min_freq = new_freq;
  300. if (new_freq == tchart->max_freq - 1000)
  301. tchart->turbo_frequency = tchart->max_freq;
  302. }
  303. static void sched_wakeup(struct timechart *tchart, int cpu, u64 timestamp,
  304. int waker, int wakee, u8 flags, const char *backtrace)
  305. {
  306. struct per_pid *p;
  307. struct wake_event *we = zalloc(sizeof(*we));
  308. if (!we)
  309. return;
  310. we->time = timestamp;
  311. we->waker = waker;
  312. we->backtrace = backtrace;
  313. if ((flags & TRACE_FLAG_HARDIRQ) || (flags & TRACE_FLAG_SOFTIRQ))
  314. we->waker = -1;
  315. we->wakee = wakee;
  316. we->next = wake_events;
  317. wake_events = we;
  318. p = find_create_pid(tchart, we->wakee);
  319. if (p && p->current && p->current->state == TYPE_NONE) {
  320. p->current->state_since = timestamp;
  321. p->current->state = TYPE_WAITING;
  322. }
  323. if (p && p->current && p->current->state == TYPE_BLOCKED) {
  324. pid_put_sample(tchart, p->pid, p->current->state, cpu,
  325. p->current->state_since, timestamp, NULL);
  326. p->current->state_since = timestamp;
  327. p->current->state = TYPE_WAITING;
  328. }
  329. }
  330. static void sched_switch(struct timechart *tchart, int cpu, u64 timestamp,
  331. int prev_pid, int next_pid, u64 prev_state,
  332. const char *backtrace)
  333. {
  334. struct per_pid *p = NULL, *prev_p;
  335. prev_p = find_create_pid(tchart, prev_pid);
  336. p = find_create_pid(tchart, next_pid);
  337. if (prev_p->current && prev_p->current->state != TYPE_NONE)
  338. pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
  339. prev_p->current->state_since, timestamp,
  340. backtrace);
  341. if (p && p->current) {
  342. if (p->current->state != TYPE_NONE)
  343. pid_put_sample(tchart, next_pid, p->current->state, cpu,
  344. p->current->state_since, timestamp,
  345. backtrace);
  346. p->current->state_since = timestamp;
  347. p->current->state = TYPE_RUNNING;
  348. }
  349. if (prev_p->current) {
  350. prev_p->current->state = TYPE_NONE;
  351. prev_p->current->state_since = timestamp;
  352. if (prev_state & 2)
  353. prev_p->current->state = TYPE_BLOCKED;
  354. if (prev_state == 0)
  355. prev_p->current->state = TYPE_WAITING;
  356. }
  357. }
  358. static const char *cat_backtrace(union perf_event *event,
  359. struct perf_sample *sample,
  360. struct machine *machine)
  361. {
  362. struct addr_location al;
  363. unsigned int i;
  364. char *p = NULL;
  365. size_t p_len;
  366. u8 cpumode = PERF_RECORD_MISC_USER;
  367. struct addr_location tal;
  368. struct ip_callchain *chain = sample->callchain;
  369. FILE *f = open_memstream(&p, &p_len);
  370. if (!f) {
  371. perror("open_memstream error");
  372. return NULL;
  373. }
  374. if (!chain)
  375. goto exit;
  376. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  377. fprintf(stderr, "problem processing %d event, skipping it.\n",
  378. event->header.type);
  379. goto exit;
  380. }
  381. for (i = 0; i < chain->nr; i++) {
  382. u64 ip;
  383. if (callchain_param.order == ORDER_CALLEE)
  384. ip = chain->ips[i];
  385. else
  386. ip = chain->ips[chain->nr - i - 1];
  387. if (ip >= PERF_CONTEXT_MAX) {
  388. switch (ip) {
  389. case PERF_CONTEXT_HV:
  390. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  391. break;
  392. case PERF_CONTEXT_KERNEL:
  393. cpumode = PERF_RECORD_MISC_KERNEL;
  394. break;
  395. case PERF_CONTEXT_USER:
  396. cpumode = PERF_RECORD_MISC_USER;
  397. break;
  398. default:
  399. pr_debug("invalid callchain context: "
  400. "%"PRId64"\n", (s64) ip);
  401. /*
  402. * It seems the callchain is corrupted.
  403. * Discard all.
  404. */
  405. free(p);
  406. p = NULL;
  407. goto exit;
  408. }
  409. continue;
  410. }
  411. tal.filtered = false;
  412. thread__find_addr_location(al.thread, machine, cpumode,
  413. MAP__FUNCTION, ip, &tal);
  414. if (tal.sym)
  415. fprintf(f, "..... %016" PRIx64 " %s\n", ip,
  416. tal.sym->name);
  417. else
  418. fprintf(f, "..... %016" PRIx64 "\n", ip);
  419. }
  420. exit:
  421. fclose(f);
  422. return p;
  423. }
  424. typedef int (*tracepoint_handler)(struct timechart *tchart,
  425. struct perf_evsel *evsel,
  426. struct perf_sample *sample,
  427. const char *backtrace);
  428. static int process_sample_event(struct perf_tool *tool,
  429. union perf_event *event,
  430. struct perf_sample *sample,
  431. struct perf_evsel *evsel,
  432. struct machine *machine)
  433. {
  434. struct timechart *tchart = container_of(tool, struct timechart, tool);
  435. if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
  436. if (!tchart->first_time || tchart->first_time > sample->time)
  437. tchart->first_time = sample->time;
  438. if (tchart->last_time < sample->time)
  439. tchart->last_time = sample->time;
  440. }
  441. if (sample->cpu > tchart->numcpus)
  442. tchart->numcpus = sample->cpu;
  443. if (evsel->handler != NULL) {
  444. tracepoint_handler f = evsel->handler;
  445. return f(tchart, evsel, sample, cat_backtrace(event, sample, machine));
  446. }
  447. return 0;
  448. }
  449. static int
  450. process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
  451. struct perf_evsel *evsel,
  452. struct perf_sample *sample,
  453. const char *backtrace __maybe_unused)
  454. {
  455. u32 state = perf_evsel__intval(evsel, sample, "state");
  456. u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  457. if (state == (u32)PWR_EVENT_EXIT)
  458. c_state_end(tchart, cpu_id, sample->time);
  459. else
  460. c_state_start(cpu_id, sample->time, state);
  461. return 0;
  462. }
  463. static int
  464. process_sample_cpu_frequency(struct timechart *tchart,
  465. struct perf_evsel *evsel,
  466. struct perf_sample *sample,
  467. const char *backtrace __maybe_unused)
  468. {
  469. u32 state = perf_evsel__intval(evsel, sample, "state");
  470. u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  471. p_state_change(tchart, cpu_id, sample->time, state);
  472. return 0;
  473. }
  474. static int
  475. process_sample_sched_wakeup(struct timechart *tchart,
  476. struct perf_evsel *evsel,
  477. struct perf_sample *sample,
  478. const char *backtrace)
  479. {
  480. u8 flags = perf_evsel__intval(evsel, sample, "common_flags");
  481. int waker = perf_evsel__intval(evsel, sample, "common_pid");
  482. int wakee = perf_evsel__intval(evsel, sample, "pid");
  483. sched_wakeup(tchart, sample->cpu, sample->time, waker, wakee, flags, backtrace);
  484. return 0;
  485. }
  486. static int
  487. process_sample_sched_switch(struct timechart *tchart,
  488. struct perf_evsel *evsel,
  489. struct perf_sample *sample,
  490. const char *backtrace)
  491. {
  492. int prev_pid = perf_evsel__intval(evsel, sample, "prev_pid");
  493. int next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  494. u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  495. sched_switch(tchart, sample->cpu, sample->time, prev_pid, next_pid,
  496. prev_state, backtrace);
  497. return 0;
  498. }
  499. #ifdef SUPPORT_OLD_POWER_EVENTS
  500. static int
  501. process_sample_power_start(struct timechart *tchart __maybe_unused,
  502. struct perf_evsel *evsel,
  503. struct perf_sample *sample,
  504. const char *backtrace __maybe_unused)
  505. {
  506. u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  507. u64 value = perf_evsel__intval(evsel, sample, "value");
  508. c_state_start(cpu_id, sample->time, value);
  509. return 0;
  510. }
  511. static int
  512. process_sample_power_end(struct timechart *tchart,
  513. struct perf_evsel *evsel __maybe_unused,
  514. struct perf_sample *sample,
  515. const char *backtrace __maybe_unused)
  516. {
  517. c_state_end(tchart, sample->cpu, sample->time);
  518. return 0;
  519. }
  520. static int
  521. process_sample_power_frequency(struct timechart *tchart,
  522. struct perf_evsel *evsel,
  523. struct perf_sample *sample,
  524. const char *backtrace __maybe_unused)
  525. {
  526. u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  527. u64 value = perf_evsel__intval(evsel, sample, "value");
  528. p_state_change(tchart, cpu_id, sample->time, value);
  529. return 0;
  530. }
  531. #endif /* SUPPORT_OLD_POWER_EVENTS */
  532. /*
  533. * After the last sample we need to wrap up the current C/P state
  534. * and close out each CPU for these.
  535. */
  536. static void end_sample_processing(struct timechart *tchart)
  537. {
  538. u64 cpu;
  539. struct power_event *pwr;
  540. for (cpu = 0; cpu <= tchart->numcpus; cpu++) {
  541. /* C state */
  542. #if 0
  543. pwr = zalloc(sizeof(*pwr));
  544. if (!pwr)
  545. return;
  546. pwr->state = cpus_cstate_state[cpu];
  547. pwr->start_time = cpus_cstate_start_times[cpu];
  548. pwr->end_time = tchart->last_time;
  549. pwr->cpu = cpu;
  550. pwr->type = CSTATE;
  551. pwr->next = tchart->power_events;
  552. tchart->power_events = pwr;
  553. #endif
  554. /* P state */
  555. pwr = zalloc(sizeof(*pwr));
  556. if (!pwr)
  557. return;
  558. pwr->state = cpus_pstate_state[cpu];
  559. pwr->start_time = cpus_pstate_start_times[cpu];
  560. pwr->end_time = tchart->last_time;
  561. pwr->cpu = cpu;
  562. pwr->type = PSTATE;
  563. pwr->next = tchart->power_events;
  564. if (!pwr->start_time)
  565. pwr->start_time = tchart->first_time;
  566. if (!pwr->state)
  567. pwr->state = tchart->min_freq;
  568. tchart->power_events = pwr;
  569. }
  570. }
  571. /*
  572. * Sort the pid datastructure
  573. */
  574. static void sort_pids(struct timechart *tchart)
  575. {
  576. struct per_pid *new_list, *p, *cursor, *prev;
  577. /* sort by ppid first, then by pid, lowest to highest */
  578. new_list = NULL;
  579. while (tchart->all_data) {
  580. p = tchart->all_data;
  581. tchart->all_data = p->next;
  582. p->next = NULL;
  583. if (new_list == NULL) {
  584. new_list = p;
  585. p->next = NULL;
  586. continue;
  587. }
  588. prev = NULL;
  589. cursor = new_list;
  590. while (cursor) {
  591. if (cursor->ppid > p->ppid ||
  592. (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
  593. /* must insert before */
  594. if (prev) {
  595. p->next = prev->next;
  596. prev->next = p;
  597. cursor = NULL;
  598. continue;
  599. } else {
  600. p->next = new_list;
  601. new_list = p;
  602. cursor = NULL;
  603. continue;
  604. }
  605. }
  606. prev = cursor;
  607. cursor = cursor->next;
  608. if (!cursor)
  609. prev->next = p;
  610. }
  611. }
  612. tchart->all_data = new_list;
  613. }
  614. static void draw_c_p_states(struct timechart *tchart)
  615. {
  616. struct power_event *pwr;
  617. pwr = tchart->power_events;
  618. /*
  619. * two pass drawing so that the P state bars are on top of the C state blocks
  620. */
  621. while (pwr) {
  622. if (pwr->type == CSTATE)
  623. svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  624. pwr = pwr->next;
  625. }
  626. pwr = tchart->power_events;
  627. while (pwr) {
  628. if (pwr->type == PSTATE) {
  629. if (!pwr->state)
  630. pwr->state = tchart->min_freq;
  631. svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  632. }
  633. pwr = pwr->next;
  634. }
  635. }
  636. static void draw_wakeups(struct timechart *tchart)
  637. {
  638. struct wake_event *we;
  639. struct per_pid *p;
  640. struct per_pidcomm *c;
  641. we = wake_events;
  642. while (we) {
  643. int from = 0, to = 0;
  644. char *task_from = NULL, *task_to = NULL;
  645. /* locate the column of the waker and wakee */
  646. p = tchart->all_data;
  647. while (p) {
  648. if (p->pid == we->waker || p->pid == we->wakee) {
  649. c = p->all;
  650. while (c) {
  651. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  652. if (p->pid == we->waker && !from) {
  653. from = c->Y;
  654. task_from = strdup(c->comm);
  655. }
  656. if (p->pid == we->wakee && !to) {
  657. to = c->Y;
  658. task_to = strdup(c->comm);
  659. }
  660. }
  661. c = c->next;
  662. }
  663. c = p->all;
  664. while (c) {
  665. if (p->pid == we->waker && !from) {
  666. from = c->Y;
  667. task_from = strdup(c->comm);
  668. }
  669. if (p->pid == we->wakee && !to) {
  670. to = c->Y;
  671. task_to = strdup(c->comm);
  672. }
  673. c = c->next;
  674. }
  675. }
  676. p = p->next;
  677. }
  678. if (!task_from) {
  679. task_from = malloc(40);
  680. sprintf(task_from, "[%i]", we->waker);
  681. }
  682. if (!task_to) {
  683. task_to = malloc(40);
  684. sprintf(task_to, "[%i]", we->wakee);
  685. }
  686. if (we->waker == -1)
  687. svg_interrupt(we->time, to, we->backtrace);
  688. else if (from && to && abs(from - to) == 1)
  689. svg_wakeline(we->time, from, to, we->backtrace);
  690. else
  691. svg_partial_wakeline(we->time, from, task_from, to,
  692. task_to, we->backtrace);
  693. we = we->next;
  694. free(task_from);
  695. free(task_to);
  696. }
  697. }
  698. static void draw_cpu_usage(struct timechart *tchart)
  699. {
  700. struct per_pid *p;
  701. struct per_pidcomm *c;
  702. struct cpu_sample *sample;
  703. p = tchart->all_data;
  704. while (p) {
  705. c = p->all;
  706. while (c) {
  707. sample = c->samples;
  708. while (sample) {
  709. if (sample->type == TYPE_RUNNING)
  710. svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
  711. sample = sample->next;
  712. }
  713. c = c->next;
  714. }
  715. p = p->next;
  716. }
  717. }
  718. static void draw_process_bars(struct timechart *tchart)
  719. {
  720. struct per_pid *p;
  721. struct per_pidcomm *c;
  722. struct cpu_sample *sample;
  723. int Y = 0;
  724. Y = 2 * tchart->numcpus + 2;
  725. p = tchart->all_data;
  726. while (p) {
  727. c = p->all;
  728. while (c) {
  729. if (!c->display) {
  730. c->Y = 0;
  731. c = c->next;
  732. continue;
  733. }
  734. svg_box(Y, c->start_time, c->end_time, "process");
  735. sample = c->samples;
  736. while (sample) {
  737. if (sample->type == TYPE_RUNNING)
  738. svg_running(Y, sample->cpu,
  739. sample->start_time,
  740. sample->end_time,
  741. sample->backtrace);
  742. if (sample->type == TYPE_BLOCKED)
  743. svg_blocked(Y, sample->cpu,
  744. sample->start_time,
  745. sample->end_time,
  746. sample->backtrace);
  747. if (sample->type == TYPE_WAITING)
  748. svg_waiting(Y, sample->cpu,
  749. sample->start_time,
  750. sample->end_time,
  751. sample->backtrace);
  752. sample = sample->next;
  753. }
  754. if (c->comm) {
  755. char comm[256];
  756. if (c->total_time > 5000000000) /* 5 seconds */
  757. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  758. else
  759. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  760. svg_text(Y, c->start_time, comm);
  761. }
  762. c->Y = Y;
  763. Y++;
  764. c = c->next;
  765. }
  766. p = p->next;
  767. }
  768. }
  769. static void add_process_filter(const char *string)
  770. {
  771. int pid = strtoull(string, NULL, 10);
  772. struct process_filter *filt = malloc(sizeof(*filt));
  773. if (!filt)
  774. return;
  775. filt->name = strdup(string);
  776. filt->pid = pid;
  777. filt->next = process_filter;
  778. process_filter = filt;
  779. }
  780. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  781. {
  782. struct process_filter *filt;
  783. if (!process_filter)
  784. return 1;
  785. filt = process_filter;
  786. while (filt) {
  787. if (filt->pid && p->pid == filt->pid)
  788. return 1;
  789. if (strcmp(filt->name, c->comm) == 0)
  790. return 1;
  791. filt = filt->next;
  792. }
  793. return 0;
  794. }
  795. static int determine_display_tasks_filtered(struct timechart *tchart)
  796. {
  797. struct per_pid *p;
  798. struct per_pidcomm *c;
  799. int count = 0;
  800. p = tchart->all_data;
  801. while (p) {
  802. p->display = 0;
  803. if (p->start_time == 1)
  804. p->start_time = tchart->first_time;
  805. /* no exit marker, task kept running to the end */
  806. if (p->end_time == 0)
  807. p->end_time = tchart->last_time;
  808. c = p->all;
  809. while (c) {
  810. c->display = 0;
  811. if (c->start_time == 1)
  812. c->start_time = tchart->first_time;
  813. if (passes_filter(p, c)) {
  814. c->display = 1;
  815. p->display = 1;
  816. count++;
  817. }
  818. if (c->end_time == 0)
  819. c->end_time = tchart->last_time;
  820. c = c->next;
  821. }
  822. p = p->next;
  823. }
  824. return count;
  825. }
  826. static int determine_display_tasks(struct timechart *tchart, u64 threshold)
  827. {
  828. struct per_pid *p;
  829. struct per_pidcomm *c;
  830. int count = 0;
  831. if (process_filter)
  832. return determine_display_tasks_filtered(tchart);
  833. p = tchart->all_data;
  834. while (p) {
  835. p->display = 0;
  836. if (p->start_time == 1)
  837. p->start_time = tchart->first_time;
  838. /* no exit marker, task kept running to the end */
  839. if (p->end_time == 0)
  840. p->end_time = tchart->last_time;
  841. if (p->total_time >= threshold)
  842. p->display = 1;
  843. c = p->all;
  844. while (c) {
  845. c->display = 0;
  846. if (c->start_time == 1)
  847. c->start_time = tchart->first_time;
  848. if (c->total_time >= threshold) {
  849. c->display = 1;
  850. count++;
  851. }
  852. if (c->end_time == 0)
  853. c->end_time = tchart->last_time;
  854. c = c->next;
  855. }
  856. p = p->next;
  857. }
  858. return count;
  859. }
  860. #define TIME_THRESH 10000000
  861. static void write_svg_file(struct timechart *tchart, const char *filename)
  862. {
  863. u64 i;
  864. int count;
  865. int thresh = TIME_THRESH;
  866. tchart->numcpus++;
  867. if (tchart->power_only)
  868. tchart->proc_num = 0;
  869. /* We'd like to show at least proc_num tasks;
  870. * be less picky if we have fewer */
  871. do {
  872. count = determine_display_tasks(tchart, thresh);
  873. thresh /= 10;
  874. } while (!process_filter && thresh && count < tchart->proc_num);
  875. open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
  876. svg_time_grid();
  877. svg_legenda();
  878. for (i = 0; i < tchart->numcpus; i++)
  879. svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
  880. draw_cpu_usage(tchart);
  881. if (tchart->proc_num)
  882. draw_process_bars(tchart);
  883. if (!tchart->tasks_only)
  884. draw_c_p_states(tchart);
  885. if (tchart->proc_num)
  886. draw_wakeups(tchart);
  887. svg_close();
  888. }
  889. static int __cmd_timechart(struct timechart *tchart, const char *output_name)
  890. {
  891. const struct perf_evsel_str_handler power_tracepoints[] = {
  892. { "power:cpu_idle", process_sample_cpu_idle },
  893. { "power:cpu_frequency", process_sample_cpu_frequency },
  894. { "sched:sched_wakeup", process_sample_sched_wakeup },
  895. { "sched:sched_switch", process_sample_sched_switch },
  896. #ifdef SUPPORT_OLD_POWER_EVENTS
  897. { "power:power_start", process_sample_power_start },
  898. { "power:power_end", process_sample_power_end },
  899. { "power:power_frequency", process_sample_power_frequency },
  900. #endif
  901. };
  902. struct perf_data_file file = {
  903. .path = input_name,
  904. .mode = PERF_DATA_MODE_READ,
  905. };
  906. struct perf_session *session = perf_session__new(&file, false,
  907. &tchart->tool);
  908. int ret = -EINVAL;
  909. if (session == NULL)
  910. return -ENOMEM;
  911. if (!perf_session__has_traces(session, "timechart record"))
  912. goto out_delete;
  913. if (perf_session__set_tracepoints_handlers(session,
  914. power_tracepoints)) {
  915. pr_err("Initializing session tracepoint handlers failed\n");
  916. goto out_delete;
  917. }
  918. ret = perf_session__process_events(session, &tchart->tool);
  919. if (ret)
  920. goto out_delete;
  921. end_sample_processing(tchart);
  922. sort_pids(tchart);
  923. write_svg_file(tchart, output_name);
  924. pr_info("Written %2.1f seconds of trace to %s.\n",
  925. (tchart->last_time - tchart->first_time) / 1000000000.0, output_name);
  926. out_delete:
  927. perf_session__delete(session);
  928. return ret;
  929. }
  930. static int timechart__record(struct timechart *tchart, int argc, const char **argv)
  931. {
  932. unsigned int rec_argc, i, j;
  933. const char **rec_argv;
  934. const char **p;
  935. unsigned int record_elems;
  936. const char * const common_args[] = {
  937. "record", "-a", "-R", "-c", "1",
  938. };
  939. unsigned int common_args_nr = ARRAY_SIZE(common_args);
  940. const char * const backtrace_args[] = {
  941. "-g",
  942. };
  943. unsigned int backtrace_args_no = ARRAY_SIZE(backtrace_args);
  944. const char * const power_args[] = {
  945. "-e", "power:cpu_frequency",
  946. "-e", "power:cpu_idle",
  947. };
  948. unsigned int power_args_nr = ARRAY_SIZE(power_args);
  949. const char * const old_power_args[] = {
  950. #ifdef SUPPORT_OLD_POWER_EVENTS
  951. "-e", "power:power_start",
  952. "-e", "power:power_end",
  953. "-e", "power:power_frequency",
  954. #endif
  955. };
  956. unsigned int old_power_args_nr = ARRAY_SIZE(old_power_args);
  957. const char * const tasks_args[] = {
  958. "-e", "sched:sched_wakeup",
  959. "-e", "sched:sched_switch",
  960. };
  961. unsigned int tasks_args_nr = ARRAY_SIZE(tasks_args);
  962. #ifdef SUPPORT_OLD_POWER_EVENTS
  963. if (!is_valid_tracepoint("power:cpu_idle") &&
  964. is_valid_tracepoint("power:power_start")) {
  965. use_old_power_events = 1;
  966. power_args_nr = 0;
  967. } else {
  968. old_power_args_nr = 0;
  969. }
  970. #endif
  971. if (tchart->power_only)
  972. tasks_args_nr = 0;
  973. if (tchart->tasks_only) {
  974. power_args_nr = 0;
  975. old_power_args_nr = 0;
  976. }
  977. if (!tchart->with_backtrace)
  978. backtrace_args_no = 0;
  979. record_elems = common_args_nr + tasks_args_nr +
  980. power_args_nr + old_power_args_nr + backtrace_args_no;
  981. rec_argc = record_elems + argc;
  982. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  983. if (rec_argv == NULL)
  984. return -ENOMEM;
  985. p = rec_argv;
  986. for (i = 0; i < common_args_nr; i++)
  987. *p++ = strdup(common_args[i]);
  988. for (i = 0; i < backtrace_args_no; i++)
  989. *p++ = strdup(backtrace_args[i]);
  990. for (i = 0; i < tasks_args_nr; i++)
  991. *p++ = strdup(tasks_args[i]);
  992. for (i = 0; i < power_args_nr; i++)
  993. *p++ = strdup(power_args[i]);
  994. for (i = 0; i < old_power_args_nr; i++)
  995. *p++ = strdup(old_power_args[i]);
  996. for (j = 1; j < (unsigned int)argc; j++)
  997. *p++ = argv[j];
  998. return cmd_record(rec_argc, rec_argv, NULL);
  999. }
  1000. static int
  1001. parse_process(const struct option *opt __maybe_unused, const char *arg,
  1002. int __maybe_unused unset)
  1003. {
  1004. if (arg)
  1005. add_process_filter(arg);
  1006. return 0;
  1007. }
  1008. int cmd_timechart(int argc, const char **argv,
  1009. const char *prefix __maybe_unused)
  1010. {
  1011. struct timechart tchart = {
  1012. .tool = {
  1013. .comm = process_comm_event,
  1014. .fork = process_fork_event,
  1015. .exit = process_exit_event,
  1016. .sample = process_sample_event,
  1017. .ordered_samples = true,
  1018. },
  1019. .proc_num = 15,
  1020. };
  1021. const char *output_name = "output.svg";
  1022. const struct option timechart_options[] = {
  1023. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1024. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1025. OPT_INTEGER('w', "width", &svg_page_width, "page width"),
  1026. OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
  1027. OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
  1028. "output processes data only"),
  1029. OPT_CALLBACK('p', "process", NULL, "process",
  1030. "process selector. Pass a pid or process name.",
  1031. parse_process),
  1032. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1033. "Look for files with symbols relative to this directory"),
  1034. OPT_INTEGER('n', "proc-num", &tchart.proc_num,
  1035. "min. number of tasks to print"),
  1036. OPT_END()
  1037. };
  1038. const char * const timechart_usage[] = {
  1039. "perf timechart [<options>] {record}",
  1040. NULL
  1041. };
  1042. const struct option record_options[] = {
  1043. OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
  1044. OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
  1045. "output processes data only"),
  1046. OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
  1047. OPT_END()
  1048. };
  1049. const char * const record_usage[] = {
  1050. "perf timechart record [<options>]",
  1051. NULL
  1052. };
  1053. argc = parse_options(argc, argv, timechart_options, timechart_usage,
  1054. PARSE_OPT_STOP_AT_NON_OPTION);
  1055. if (tchart.power_only && tchart.tasks_only) {
  1056. pr_err("-P and -T options cannot be used at the same time.\n");
  1057. return -1;
  1058. }
  1059. symbol__init();
  1060. if (argc && !strncmp(argv[0], "rec", 3)) {
  1061. argc = parse_options(argc, argv, record_options, record_usage,
  1062. PARSE_OPT_STOP_AT_NON_OPTION);
  1063. if (tchart.power_only && tchart.tasks_only) {
  1064. pr_err("-P and -T options cannot be used at the same time.\n");
  1065. return -1;
  1066. }
  1067. return timechart__record(&tchart, argc, argv);
  1068. } else if (argc)
  1069. usage_with_options(timechart_usage, timechart_options);
  1070. setup_pager();
  1071. return __cmd_timechart(&tchart, output_name);
  1072. }