trace.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally taken from the RT patch by:
  8. * Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code from the latency_tracer, that is:
  11. * Copyright (C) 2004-2006 Ingo Molnar
  12. * Copyright (C) 2004 William Lee Irwin III
  13. */
  14. #include <linux/utsrelease.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/linkage.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/module.h>
  24. #include <linux/percpu.h>
  25. #include <linux/ctype.h>
  26. #include <linux/init.h>
  27. #include <linux/gfp.h>
  28. #include <linux/fs.h>
  29. #include "trace.h"
  30. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  31. unsigned long __read_mostly tracing_thresh;
  32. static int tracing_disabled = 1;
  33. static long notrace
  34. ns2usecs(cycle_t nsec)
  35. {
  36. nsec += 500;
  37. do_div(nsec, 1000);
  38. return nsec;
  39. }
  40. static atomic_t tracer_counter;
  41. static struct trace_array global_trace;
  42. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  43. static struct trace_array max_tr;
  44. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  45. static int tracer_enabled;
  46. static unsigned long trace_nr_entries = 16384UL;
  47. static struct tracer *trace_types __read_mostly;
  48. static struct tracer *current_trace __read_mostly;
  49. static int max_tracer_type_len;
  50. static DEFINE_MUTEX(trace_types_lock);
  51. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  52. static int __init set_nr_entries(char *str)
  53. {
  54. if (!str)
  55. return 0;
  56. trace_nr_entries = simple_strtoul(str, &str, 0);
  57. return 1;
  58. }
  59. __setup("trace_entries=", set_nr_entries);
  60. unsigned long nsecs_to_usecs(unsigned long nsecs)
  61. {
  62. return nsecs / 1000;
  63. }
  64. enum trace_type {
  65. __TRACE_FIRST_TYPE = 0,
  66. TRACE_FN,
  67. TRACE_CTX,
  68. __TRACE_LAST_TYPE
  69. };
  70. enum trace_flag_type {
  71. TRACE_FLAG_IRQS_OFF = 0x01,
  72. TRACE_FLAG_NEED_RESCHED = 0x02,
  73. TRACE_FLAG_HARDIRQ = 0x04,
  74. TRACE_FLAG_SOFTIRQ = 0x08,
  75. };
  76. enum trace_iterator_flags {
  77. TRACE_ITER_PRINT_PARENT = 0x01,
  78. TRACE_ITER_SYM_OFFSET = 0x02,
  79. TRACE_ITER_SYM_ADDR = 0x04,
  80. TRACE_ITER_VERBOSE = 0x08,
  81. };
  82. #define TRACE_ITER_SYM_MASK \
  83. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  84. /* These must match the bit postions above */
  85. static const char *trace_options[] = {
  86. "print-parent",
  87. "sym-offset",
  88. "sym-addr",
  89. "verbose",
  90. NULL
  91. };
  92. static unsigned trace_flags;
  93. static DEFINE_SPINLOCK(ftrace_max_lock);
  94. /*
  95. * Copy the new maximum trace into the separate maximum-trace
  96. * structure. (this way the maximum trace is permanently saved,
  97. * for later retrieval via /debugfs/tracing/latency_trace)
  98. */
  99. static notrace void
  100. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  101. {
  102. struct trace_array_cpu *data = tr->data[cpu];
  103. max_tr.cpu = cpu;
  104. max_tr.time_start = data->preempt_timestamp;
  105. data = max_tr.data[cpu];
  106. data->saved_latency = tracing_max_latency;
  107. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  108. data->pid = tsk->pid;
  109. data->uid = tsk->uid;
  110. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  111. data->policy = tsk->policy;
  112. data->rt_priority = tsk->rt_priority;
  113. /* record this tasks comm */
  114. tracing_record_cmdline(current);
  115. }
  116. void check_pages(struct trace_array_cpu *data)
  117. {
  118. struct page *page, *tmp;
  119. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  120. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  121. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  122. BUG_ON(page->lru.next->prev != &page->lru);
  123. BUG_ON(page->lru.prev->next != &page->lru);
  124. }
  125. }
  126. void *head_page(struct trace_array_cpu *data)
  127. {
  128. struct page *page;
  129. check_pages(data);
  130. if (list_empty(&data->trace_pages))
  131. return NULL;
  132. page = list_entry(data->trace_pages.next, struct page, lru);
  133. BUG_ON(&page->lru == &data->trace_pages);
  134. return page_address(page);
  135. }
  136. notrace static void
  137. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  138. {
  139. struct list_head flip_pages;
  140. INIT_LIST_HEAD(&flip_pages);
  141. tr1->trace_current = NULL;
  142. memcpy(&tr1->trace_current_idx, &tr2->trace_current_idx,
  143. sizeof(struct trace_array_cpu) -
  144. offsetof(struct trace_array_cpu, trace_current_idx));
  145. check_pages(tr1);
  146. check_pages(tr2);
  147. list_splice_init(&tr1->trace_pages, &flip_pages);
  148. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  149. list_splice_init(&flip_pages, &tr2->trace_pages);
  150. BUG_ON(!list_empty(&flip_pages));
  151. check_pages(tr1);
  152. check_pages(tr2);
  153. }
  154. notrace void
  155. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  156. {
  157. struct trace_array_cpu *data;
  158. int i;
  159. WARN_ON_ONCE(!irqs_disabled());
  160. spin_lock(&ftrace_max_lock);
  161. /* clear out all the previous traces */
  162. for_each_possible_cpu(i) {
  163. data = tr->data[i];
  164. flip_trace(max_tr.data[i], data);
  165. tracing_reset(data);
  166. }
  167. __update_max_tr(tr, tsk, cpu);
  168. spin_unlock(&ftrace_max_lock);
  169. }
  170. /**
  171. * update_max_tr_single - only copy one trace over, and reset the rest
  172. * @tr - tracer
  173. * @tsk - task with the latency
  174. * @cpu - the cpu of the buffer to copy.
  175. */
  176. notrace void
  177. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  178. {
  179. struct trace_array_cpu *data = tr->data[cpu];
  180. int i;
  181. WARN_ON_ONCE(!irqs_disabled());
  182. spin_lock(&ftrace_max_lock);
  183. for_each_possible_cpu(i)
  184. tracing_reset(max_tr.data[i]);
  185. flip_trace(max_tr.data[cpu], data);
  186. tracing_reset(data);
  187. __update_max_tr(tr, tsk, cpu);
  188. spin_unlock(&ftrace_max_lock);
  189. }
  190. int register_tracer(struct tracer *type)
  191. {
  192. struct tracer *t;
  193. int len;
  194. int ret = 0;
  195. if (!type->name) {
  196. pr_info("Tracer must have a name\n");
  197. return -1;
  198. }
  199. mutex_lock(&trace_types_lock);
  200. for (t = trace_types; t; t = t->next) {
  201. if (strcmp(type->name, t->name) == 0) {
  202. /* already found */
  203. pr_info("Trace %s already registered\n",
  204. type->name);
  205. ret = -1;
  206. goto out;
  207. }
  208. }
  209. #ifdef CONFIG_FTRACE_STARTUP_TEST
  210. if (type->selftest) {
  211. struct tracer *saved_tracer = current_trace;
  212. struct trace_array_cpu *data;
  213. struct trace_array *tr = &global_trace;
  214. int saved_ctrl = tr->ctrl;
  215. int i;
  216. /*
  217. * Run a selftest on this tracer.
  218. * Here we reset the trace buffer, and set the current
  219. * tracer to be this tracer. The tracer can then run some
  220. * internal tracing to verify that everything is in order.
  221. * If we fail, we do not register this tracer.
  222. */
  223. for_each_possible_cpu(i) {
  224. data = tr->data[i];
  225. if (!head_page(data))
  226. continue;
  227. tracing_reset(data);
  228. }
  229. current_trace = type;
  230. tr->ctrl = 0;
  231. /* the test is responsible for initializing and enabling */
  232. pr_info("Testing tracer %s: ", type->name);
  233. ret = type->selftest(type, tr);
  234. /* the test is responsible for resetting too */
  235. current_trace = saved_tracer;
  236. tr->ctrl = saved_ctrl;
  237. if (ret) {
  238. printk(KERN_CONT "FAILED!\n");
  239. goto out;
  240. }
  241. /* Only reset on passing, to avoid touching corrupted buffers */
  242. for_each_possible_cpu(i) {
  243. data = tr->data[i];
  244. if (!head_page(data))
  245. continue;
  246. tracing_reset(data);
  247. }
  248. printk(KERN_CONT "PASSED\n");
  249. }
  250. #endif
  251. type->next = trace_types;
  252. trace_types = type;
  253. len = strlen(type->name);
  254. if (len > max_tracer_type_len)
  255. max_tracer_type_len = len;
  256. out:
  257. mutex_unlock(&trace_types_lock);
  258. return ret;
  259. }
  260. void unregister_tracer(struct tracer *type)
  261. {
  262. struct tracer **t;
  263. int len;
  264. mutex_lock(&trace_types_lock);
  265. for (t = &trace_types; *t; t = &(*t)->next) {
  266. if (*t == type)
  267. goto found;
  268. }
  269. pr_info("Trace %s not registered\n", type->name);
  270. goto out;
  271. found:
  272. *t = (*t)->next;
  273. if (strlen(type->name) != max_tracer_type_len)
  274. goto out;
  275. max_tracer_type_len = 0;
  276. for (t = &trace_types; *t; t = &(*t)->next) {
  277. len = strlen((*t)->name);
  278. if (len > max_tracer_type_len)
  279. max_tracer_type_len = len;
  280. }
  281. out:
  282. mutex_unlock(&trace_types_lock);
  283. }
  284. notrace void tracing_reset(struct trace_array_cpu *data)
  285. {
  286. data->trace_idx = 0;
  287. data->trace_current = head_page(data);
  288. data->trace_current_idx = 0;
  289. data->time_offset = 0;
  290. }
  291. #ifdef CONFIG_FTRACE
  292. static notrace void
  293. function_trace_call(unsigned long ip, unsigned long parent_ip)
  294. {
  295. struct trace_array *tr = &global_trace;
  296. struct trace_array_cpu *data;
  297. unsigned long flags;
  298. long disabled;
  299. int cpu;
  300. if (unlikely(!tracer_enabled))
  301. return;
  302. local_irq_save(flags);
  303. cpu = raw_smp_processor_id();
  304. data = tr->data[cpu];
  305. disabled = atomic_inc_return(&data->disabled);
  306. if (likely(disabled == 1))
  307. ftrace(tr, data, ip, parent_ip, flags);
  308. atomic_dec(&data->disabled);
  309. local_irq_restore(flags);
  310. }
  311. static struct ftrace_ops trace_ops __read_mostly =
  312. {
  313. .func = function_trace_call,
  314. };
  315. #endif
  316. notrace void tracing_start_function_trace(void)
  317. {
  318. register_ftrace_function(&trace_ops);
  319. }
  320. notrace void tracing_stop_function_trace(void)
  321. {
  322. unregister_ftrace_function(&trace_ops);
  323. }
  324. #define SAVED_CMDLINES 128
  325. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  326. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  327. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  328. static int cmdline_idx;
  329. static DEFINE_SPINLOCK(trace_cmdline_lock);
  330. atomic_t trace_record_cmdline_disabled;
  331. static void trace_init_cmdlines(void)
  332. {
  333. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  334. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  335. cmdline_idx = 0;
  336. }
  337. notrace void trace_stop_cmdline_recording(void);
  338. static notrace void trace_save_cmdline(struct task_struct *tsk)
  339. {
  340. unsigned map;
  341. unsigned idx;
  342. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  343. return;
  344. /*
  345. * It's not the end of the world if we don't get
  346. * the lock, but we also don't want to spin
  347. * nor do we want to disable interrupts,
  348. * so if we miss here, then better luck next time.
  349. */
  350. if (!spin_trylock(&trace_cmdline_lock))
  351. return;
  352. idx = map_pid_to_cmdline[tsk->pid];
  353. if (idx >= SAVED_CMDLINES) {
  354. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  355. map = map_cmdline_to_pid[idx];
  356. if (map <= PID_MAX_DEFAULT)
  357. map_pid_to_cmdline[map] = (unsigned)-1;
  358. map_pid_to_cmdline[tsk->pid] = idx;
  359. cmdline_idx = idx;
  360. }
  361. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  362. spin_unlock(&trace_cmdline_lock);
  363. }
  364. static notrace char *trace_find_cmdline(int pid)
  365. {
  366. char *cmdline = "<...>";
  367. unsigned map;
  368. if (!pid)
  369. return "<idle>";
  370. if (pid > PID_MAX_DEFAULT)
  371. goto out;
  372. map = map_pid_to_cmdline[pid];
  373. if (map >= SAVED_CMDLINES)
  374. goto out;
  375. cmdline = saved_cmdlines[map];
  376. out:
  377. return cmdline;
  378. }
  379. notrace void tracing_record_cmdline(struct task_struct *tsk)
  380. {
  381. if (atomic_read(&trace_record_cmdline_disabled))
  382. return;
  383. trace_save_cmdline(tsk);
  384. }
  385. static inline notrace struct trace_entry *
  386. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  387. {
  388. unsigned long idx, idx_next;
  389. struct trace_entry *entry;
  390. struct list_head *next;
  391. struct page *page;
  392. data->trace_idx++;
  393. idx = data->trace_current_idx;
  394. idx_next = idx + 1;
  395. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  396. entry = data->trace_current + idx * TRACE_ENTRY_SIZE;
  397. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  398. page = virt_to_page(data->trace_current);
  399. /*
  400. * Roundrobin - but skip the head (which is not a real page):
  401. */
  402. next = page->lru.next;
  403. if (unlikely(next == &data->trace_pages))
  404. next = next->next;
  405. BUG_ON(next == &data->trace_pages);
  406. page = list_entry(next, struct page, lru);
  407. data->trace_current = page_address(page);
  408. idx_next = 0;
  409. }
  410. data->trace_current_idx = idx_next;
  411. return entry;
  412. }
  413. static inline notrace void
  414. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  415. {
  416. struct task_struct *tsk = current;
  417. unsigned long pc;
  418. pc = preempt_count();
  419. entry->idx = atomic_inc_return(&tracer_counter);
  420. entry->preempt_count = pc & 0xff;
  421. entry->pid = tsk->pid;
  422. entry->t = now(raw_smp_processor_id());
  423. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  424. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  425. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  426. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  427. }
  428. notrace void
  429. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  430. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  431. {
  432. struct trace_entry *entry;
  433. entry = tracing_get_trace_entry(tr, data);
  434. tracing_generic_entry_update(entry, flags);
  435. entry->type = TRACE_FN;
  436. entry->fn.ip = ip;
  437. entry->fn.parent_ip = parent_ip;
  438. }
  439. notrace void
  440. tracing_sched_switch_trace(struct trace_array *tr,
  441. struct trace_array_cpu *data,
  442. struct task_struct *prev, struct task_struct *next,
  443. unsigned long flags)
  444. {
  445. struct trace_entry *entry;
  446. entry = tracing_get_trace_entry(tr, data);
  447. tracing_generic_entry_update(entry, flags);
  448. entry->type = TRACE_CTX;
  449. entry->ctx.prev_pid = prev->pid;
  450. entry->ctx.prev_prio = prev->prio;
  451. entry->ctx.prev_state = prev->state;
  452. entry->ctx.next_pid = next->pid;
  453. entry->ctx.next_prio = next->prio;
  454. }
  455. enum trace_file_type {
  456. TRACE_FILE_LAT_FMT = 1,
  457. };
  458. static struct trace_entry *
  459. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  460. struct trace_iterator *iter, int cpu)
  461. {
  462. struct page *page;
  463. struct trace_entry *array;
  464. if (iter->next_idx[cpu] >= tr->entries ||
  465. iter->next_idx[cpu] >= data->trace_idx)
  466. return NULL;
  467. if (!iter->next_page[cpu]) {
  468. /*
  469. * Initialize. If the count of elements in
  470. * this buffer is greater than the max entries
  471. * we had an underrun. Which means we looped around.
  472. * We can simply use the current pointer as our
  473. * starting point.
  474. */
  475. if (data->trace_idx >= tr->entries) {
  476. page = virt_to_page(data->trace_current);
  477. iter->next_page[cpu] = &page->lru;
  478. iter->next_page_idx[cpu] = data->trace_current_idx;
  479. } else {
  480. iter->next_page[cpu] = data->trace_pages.next;
  481. iter->next_page_idx[cpu] = 0;
  482. }
  483. }
  484. page = list_entry(iter->next_page[cpu], struct page, lru);
  485. BUG_ON(&data->trace_pages == &page->lru);
  486. array = page_address(page);
  487. return &array[iter->next_page_idx[cpu]];
  488. }
  489. static struct notrace trace_entry *
  490. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  491. {
  492. struct trace_array *tr = iter->tr;
  493. struct trace_entry *ent, *next = NULL;
  494. int next_cpu = -1;
  495. int cpu;
  496. for_each_possible_cpu(cpu) {
  497. if (!head_page(tr->data[cpu]))
  498. continue;
  499. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  500. if (ent &&
  501. (!next || (long)(next->idx - ent->idx) > 0)) {
  502. next = ent;
  503. next_cpu = cpu;
  504. }
  505. }
  506. if (ent_cpu)
  507. *ent_cpu = next_cpu;
  508. return next;
  509. }
  510. static void *find_next_entry_inc(struct trace_iterator *iter)
  511. {
  512. struct trace_entry *next;
  513. int next_cpu = -1;
  514. next = find_next_entry(iter, &next_cpu);
  515. if (next) {
  516. iter->idx++;
  517. iter->next_idx[next_cpu]++;
  518. iter->next_page_idx[next_cpu]++;
  519. if (iter->next_page_idx[next_cpu] >= ENTRIES_PER_PAGE) {
  520. struct trace_array_cpu *data = iter->tr->data[next_cpu];
  521. iter->next_page_idx[next_cpu] = 0;
  522. iter->next_page[next_cpu] =
  523. iter->next_page[next_cpu]->next;
  524. if (iter->next_page[next_cpu] == &data->trace_pages)
  525. iter->next_page[next_cpu] =
  526. data->trace_pages.next;
  527. }
  528. }
  529. iter->prev_ent = iter->ent;
  530. iter->prev_cpu = iter->cpu;
  531. iter->ent = next;
  532. iter->cpu = next_cpu;
  533. return next ? iter : NULL;
  534. }
  535. static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
  536. {
  537. struct trace_iterator *iter = m->private;
  538. void *last_ent = iter->ent;
  539. int i = (int)*pos;
  540. void *ent;
  541. (*pos)++;
  542. /* can't go backwards */
  543. if (iter->idx > i)
  544. return NULL;
  545. if (iter->idx < 0)
  546. ent = find_next_entry_inc(iter);
  547. else
  548. ent = iter;
  549. while (ent && iter->idx < i)
  550. ent = find_next_entry_inc(iter);
  551. iter->pos = *pos;
  552. if (last_ent && !ent)
  553. seq_puts(m, "\n\nvim:ft=help\n");
  554. return ent;
  555. }
  556. static void *s_start(struct seq_file *m, loff_t *pos)
  557. {
  558. struct trace_iterator *iter = m->private;
  559. void *p = NULL;
  560. loff_t l = 0;
  561. int i;
  562. mutex_lock(&trace_types_lock);
  563. if (!current_trace || current_trace != iter->trace)
  564. return NULL;
  565. atomic_inc(&trace_record_cmdline_disabled);
  566. /* let the tracer grab locks here if needed */
  567. if (current_trace->start)
  568. current_trace->start(iter);
  569. if (*pos != iter->pos) {
  570. iter->ent = NULL;
  571. iter->cpu = 0;
  572. iter->idx = -1;
  573. iter->prev_ent = NULL;
  574. iter->prev_cpu = -1;
  575. for_each_possible_cpu(i) {
  576. iter->next_idx[i] = 0;
  577. iter->next_page[i] = NULL;
  578. }
  579. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  580. ;
  581. } else {
  582. l = *pos - 1;
  583. p = s_next(m, p, &l);
  584. }
  585. return p;
  586. }
  587. static void s_stop(struct seq_file *m, void *p)
  588. {
  589. struct trace_iterator *iter = m->private;
  590. atomic_dec(&trace_record_cmdline_disabled);
  591. /* let the tracer release locks here if needed */
  592. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  593. iter->trace->stop(iter);
  594. mutex_unlock(&trace_types_lock);
  595. }
  596. static void
  597. seq_print_sym_short(struct seq_file *m, const char *fmt, unsigned long address)
  598. {
  599. #ifdef CONFIG_KALLSYMS
  600. char str[KSYM_SYMBOL_LEN];
  601. kallsyms_lookup(address, NULL, NULL, NULL, str);
  602. seq_printf(m, fmt, str);
  603. #endif
  604. }
  605. static void
  606. seq_print_sym_offset(struct seq_file *m, const char *fmt, unsigned long address)
  607. {
  608. #ifdef CONFIG_KALLSYMS
  609. char str[KSYM_SYMBOL_LEN];
  610. sprint_symbol(str, address);
  611. seq_printf(m, fmt, str);
  612. #endif
  613. }
  614. #ifndef CONFIG_64BIT
  615. # define IP_FMT "%08lx"
  616. #else
  617. # define IP_FMT "%016lx"
  618. #endif
  619. static notrace void
  620. seq_print_ip_sym(struct seq_file *m, unsigned long ip, unsigned long sym_flags)
  621. {
  622. if (!ip) {
  623. seq_printf(m, "0");
  624. return;
  625. }
  626. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  627. seq_print_sym_offset(m, "%s", ip);
  628. else
  629. seq_print_sym_short(m, "%s", ip);
  630. if (sym_flags & TRACE_ITER_SYM_ADDR)
  631. seq_printf(m, " <" IP_FMT ">", ip);
  632. }
  633. static notrace void print_lat_help_header(struct seq_file *m)
  634. {
  635. seq_puts(m, "# _------=> CPU# \n");
  636. seq_puts(m, "# / _-----=> irqs-off \n");
  637. seq_puts(m, "# | / _----=> need-resched \n");
  638. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  639. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  640. seq_puts(m, "# |||| / \n");
  641. seq_puts(m, "# ||||| delay \n");
  642. seq_puts(m, "# cmd pid ||||| time | caller \n");
  643. seq_puts(m, "# \\ / ||||| \\ | / \n");
  644. }
  645. static notrace void print_func_help_header(struct seq_file *m)
  646. {
  647. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  648. seq_puts(m, "# | | | | |\n");
  649. }
  650. static notrace void
  651. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  652. {
  653. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  654. struct trace_array *tr = iter->tr;
  655. struct trace_array_cpu *data = tr->data[tr->cpu];
  656. struct tracer *type = current_trace;
  657. unsigned long total = 0;
  658. unsigned long entries = 0;
  659. int cpu;
  660. const char *name = "preemption";
  661. if (type)
  662. name = type->name;
  663. for_each_possible_cpu(cpu) {
  664. if (head_page(tr->data[cpu])) {
  665. total += tr->data[cpu]->trace_idx;
  666. if (tr->data[cpu]->trace_idx > tr->entries)
  667. entries += tr->entries;
  668. else
  669. entries += tr->data[cpu]->trace_idx;
  670. }
  671. }
  672. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  673. name, UTS_RELEASE);
  674. seq_puts(m, "-----------------------------------"
  675. "---------------------------------\n");
  676. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  677. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  678. nsecs_to_usecs(data->saved_latency),
  679. entries,
  680. total,
  681. tr->cpu,
  682. #if defined(CONFIG_PREEMPT_NONE)
  683. "server",
  684. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  685. "desktop",
  686. #elif defined(CONFIG_PREEMPT_DESKTOP)
  687. "preempt",
  688. #else
  689. "unknown",
  690. #endif
  691. /* These are reserved for later use */
  692. 0, 0, 0, 0);
  693. #ifdef CONFIG_SMP
  694. seq_printf(m, " #P:%d)\n", num_online_cpus());
  695. #else
  696. seq_puts(m, ")\n");
  697. #endif
  698. seq_puts(m, " -----------------\n");
  699. seq_printf(m, " | task: %.16s-%d "
  700. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  701. data->comm, data->pid, data->uid, data->nice,
  702. data->policy, data->rt_priority);
  703. seq_puts(m, " -----------------\n");
  704. if (data->critical_start) {
  705. seq_puts(m, " => started at: ");
  706. seq_print_ip_sym(m, data->critical_start, sym_flags);
  707. seq_puts(m, "\n => ended at: ");
  708. seq_print_ip_sym(m, data->critical_end, sym_flags);
  709. seq_puts(m, "\n");
  710. }
  711. seq_puts(m, "\n");
  712. }
  713. static notrace void
  714. lat_print_generic(struct seq_file *m, struct trace_entry *entry, int cpu)
  715. {
  716. int hardirq, softirq;
  717. char *comm;
  718. comm = trace_find_cmdline(entry->pid);
  719. seq_printf(m, "%8.8s-%-5d ", comm, entry->pid);
  720. seq_printf(m, "%d", cpu);
  721. seq_printf(m, "%c%c",
  722. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  723. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  724. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  725. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  726. if (hardirq && softirq)
  727. seq_putc(m, 'H');
  728. else {
  729. if (hardirq)
  730. seq_putc(m, 'h');
  731. else {
  732. if (softirq)
  733. seq_putc(m, 's');
  734. else
  735. seq_putc(m, '.');
  736. }
  737. }
  738. if (entry->preempt_count)
  739. seq_printf(m, "%x", entry->preempt_count);
  740. else
  741. seq_puts(m, ".");
  742. }
  743. unsigned long preempt_mark_thresh = 100;
  744. static notrace void
  745. lat_print_timestamp(struct seq_file *m, unsigned long long abs_usecs,
  746. unsigned long rel_usecs)
  747. {
  748. seq_printf(m, " %4lldus", abs_usecs);
  749. if (rel_usecs > preempt_mark_thresh)
  750. seq_puts(m, "!: ");
  751. else if (rel_usecs > 1)
  752. seq_puts(m, "+: ");
  753. else
  754. seq_puts(m, " : ");
  755. }
  756. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  757. static notrace void
  758. print_lat_fmt(struct seq_file *m, struct trace_iterator *iter,
  759. unsigned int trace_idx, int cpu)
  760. {
  761. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  762. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  763. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  764. struct trace_entry *entry = iter->ent;
  765. unsigned long abs_usecs;
  766. unsigned long rel_usecs;
  767. char *comm;
  768. int S;
  769. if (!next_entry)
  770. next_entry = entry;
  771. rel_usecs = ns2usecs(next_entry->t - entry->t);
  772. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  773. if (verbose) {
  774. comm = trace_find_cmdline(entry->pid);
  775. seq_printf(m, "%16s %5d %d %d %08x %08x [%08lx]"
  776. " %ld.%03ldms (+%ld.%03ldms): ",
  777. comm,
  778. entry->pid, cpu, entry->flags,
  779. entry->preempt_count, trace_idx,
  780. ns2usecs(entry->t),
  781. abs_usecs/1000,
  782. abs_usecs % 1000, rel_usecs/1000, rel_usecs % 1000);
  783. } else {
  784. lat_print_generic(m, entry, cpu);
  785. lat_print_timestamp(m, abs_usecs, rel_usecs);
  786. }
  787. switch (entry->type) {
  788. case TRACE_FN:
  789. seq_print_ip_sym(m, entry->fn.ip, sym_flags);
  790. seq_puts(m, " (");
  791. seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
  792. seq_puts(m, ")\n");
  793. break;
  794. case TRACE_CTX:
  795. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  796. state_to_char[entry->ctx.prev_state] : 'X';
  797. comm = trace_find_cmdline(entry->ctx.next_pid);
  798. seq_printf(m, " %d:%d:%c --> %d:%d %s\n",
  799. entry->ctx.prev_pid,
  800. entry->ctx.prev_prio,
  801. S,
  802. entry->ctx.next_pid,
  803. entry->ctx.next_prio,
  804. comm);
  805. break;
  806. default:
  807. seq_printf(m, "Unknown type %d\n", entry->type);
  808. }
  809. }
  810. static notrace void sync_time_offset(struct trace_iterator *iter)
  811. {
  812. struct trace_array_cpu *prev_array, *array;
  813. struct trace_entry *prev_entry, *entry;
  814. cycle_t prev_t, t;
  815. entry = iter->ent;
  816. prev_entry = iter->prev_ent;
  817. if (!prev_entry)
  818. return;
  819. prev_array = iter->tr->data[iter->prev_cpu];
  820. array = iter->tr->data[iter->cpu];
  821. prev_t = prev_entry->t + prev_array->time_offset;
  822. t = entry->t + array->time_offset;
  823. /*
  824. * If time goes backwards we increase the offset of
  825. * the current array, to not have observable time warps.
  826. * This will quickly synchronize the time offsets of
  827. * multiple CPUs:
  828. */
  829. if (t < prev_t)
  830. array->time_offset += prev_t - t;
  831. }
  832. static notrace void
  833. print_trace_fmt(struct seq_file *m, struct trace_iterator *iter)
  834. {
  835. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  836. struct trace_entry *entry;
  837. unsigned long usec_rem;
  838. unsigned long long t;
  839. unsigned long secs;
  840. char *comm;
  841. int S;
  842. sync_time_offset(iter);
  843. entry = iter->ent;
  844. comm = trace_find_cmdline(iter->ent->pid);
  845. t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
  846. usec_rem = do_div(t, 1000000ULL);
  847. secs = (unsigned long)t;
  848. seq_printf(m, "%16s-%-5d ", comm, entry->pid);
  849. seq_printf(m, "[%02d] ", iter->cpu);
  850. seq_printf(m, "%5lu.%06lu: ", secs, usec_rem);
  851. switch (entry->type) {
  852. case TRACE_FN:
  853. seq_print_ip_sym(m, entry->fn.ip, sym_flags);
  854. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  855. entry->fn.parent_ip) {
  856. seq_printf(m, " <-");
  857. seq_print_ip_sym(m, entry->fn.parent_ip, sym_flags);
  858. }
  859. seq_printf(m, "\n");
  860. break;
  861. case TRACE_CTX:
  862. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  863. state_to_char[entry->ctx.prev_state] : 'X';
  864. seq_printf(m, " %d:%d:%c ==> %d:%d\n",
  865. entry->ctx.prev_pid,
  866. entry->ctx.prev_prio,
  867. S,
  868. entry->ctx.next_pid,
  869. entry->ctx.next_prio);
  870. break;
  871. }
  872. }
  873. static int trace_empty(struct trace_iterator *iter)
  874. {
  875. struct trace_array_cpu *data;
  876. int cpu;
  877. for_each_possible_cpu(cpu) {
  878. data = iter->tr->data[cpu];
  879. if (head_page(data) && data->trace_idx)
  880. return 0;
  881. }
  882. return 1;
  883. }
  884. static int s_show(struct seq_file *m, void *v)
  885. {
  886. struct trace_iterator *iter = v;
  887. if (iter->ent == NULL) {
  888. if (iter->tr) {
  889. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  890. seq_puts(m, "#\n");
  891. }
  892. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  893. /* print nothing if the buffers are empty */
  894. if (trace_empty(iter))
  895. return 0;
  896. print_trace_header(m, iter);
  897. if (!(trace_flags & TRACE_ITER_VERBOSE))
  898. print_lat_help_header(m);
  899. } else {
  900. if (!(trace_flags & TRACE_ITER_VERBOSE))
  901. print_func_help_header(m);
  902. }
  903. } else {
  904. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  905. print_lat_fmt(m, iter, iter->idx, iter->cpu);
  906. else
  907. print_trace_fmt(m, iter);
  908. }
  909. return 0;
  910. }
  911. static struct seq_operations tracer_seq_ops = {
  912. .start = s_start,
  913. .next = s_next,
  914. .stop = s_stop,
  915. .show = s_show,
  916. };
  917. static struct trace_iterator notrace *
  918. __tracing_open(struct inode *inode, struct file *file, int *ret)
  919. {
  920. struct trace_iterator *iter;
  921. if (tracing_disabled) {
  922. *ret = -ENODEV;
  923. return NULL;
  924. }
  925. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  926. if (!iter) {
  927. *ret = -ENOMEM;
  928. goto out;
  929. }
  930. mutex_lock(&trace_types_lock);
  931. if (current_trace && current_trace->print_max)
  932. iter->tr = &max_tr;
  933. else
  934. iter->tr = inode->i_private;
  935. iter->trace = current_trace;
  936. iter->pos = -1;
  937. /* TODO stop tracer */
  938. *ret = seq_open(file, &tracer_seq_ops);
  939. if (!*ret) {
  940. struct seq_file *m = file->private_data;
  941. m->private = iter;
  942. /* stop the trace while dumping */
  943. if (iter->tr->ctrl)
  944. tracer_enabled = 0;
  945. if (iter->trace && iter->trace->open)
  946. iter->trace->open(iter);
  947. } else {
  948. kfree(iter);
  949. iter = NULL;
  950. }
  951. mutex_unlock(&trace_types_lock);
  952. out:
  953. return iter;
  954. }
  955. int tracing_open_generic(struct inode *inode, struct file *filp)
  956. {
  957. if (tracing_disabled)
  958. return -ENODEV;
  959. filp->private_data = inode->i_private;
  960. return 0;
  961. }
  962. int tracing_release(struct inode *inode, struct file *file)
  963. {
  964. struct seq_file *m = (struct seq_file *)file->private_data;
  965. struct trace_iterator *iter = m->private;
  966. mutex_lock(&trace_types_lock);
  967. if (iter->trace && iter->trace->close)
  968. iter->trace->close(iter);
  969. /* reenable tracing if it was previously enabled */
  970. if (iter->tr->ctrl)
  971. tracer_enabled = 1;
  972. mutex_unlock(&trace_types_lock);
  973. seq_release(inode, file);
  974. kfree(iter);
  975. return 0;
  976. }
  977. static int tracing_open(struct inode *inode, struct file *file)
  978. {
  979. int ret;
  980. __tracing_open(inode, file, &ret);
  981. return ret;
  982. }
  983. static int tracing_lt_open(struct inode *inode, struct file *file)
  984. {
  985. struct trace_iterator *iter;
  986. int ret;
  987. iter = __tracing_open(inode, file, &ret);
  988. if (!ret)
  989. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  990. return ret;
  991. }
  992. static notrace void *
  993. t_next(struct seq_file *m, void *v, loff_t *pos)
  994. {
  995. struct tracer *t = m->private;
  996. (*pos)++;
  997. if (t)
  998. t = t->next;
  999. m->private = t;
  1000. return t;
  1001. }
  1002. static void *t_start(struct seq_file *m, loff_t *pos)
  1003. {
  1004. struct tracer *t = m->private;
  1005. loff_t l = 0;
  1006. mutex_lock(&trace_types_lock);
  1007. for (; t && l < *pos; t = t_next(m, t, &l))
  1008. ;
  1009. return t;
  1010. }
  1011. static void t_stop(struct seq_file *m, void *p)
  1012. {
  1013. mutex_unlock(&trace_types_lock);
  1014. }
  1015. static int t_show(struct seq_file *m, void *v)
  1016. {
  1017. struct tracer *t = v;
  1018. if (!t)
  1019. return 0;
  1020. seq_printf(m, "%s", t->name);
  1021. if (t->next)
  1022. seq_putc(m, ' ');
  1023. else
  1024. seq_putc(m, '\n');
  1025. return 0;
  1026. }
  1027. static struct seq_operations show_traces_seq_ops = {
  1028. .start = t_start,
  1029. .next = t_next,
  1030. .stop = t_stop,
  1031. .show = t_show,
  1032. };
  1033. static int show_traces_open(struct inode *inode, struct file *file)
  1034. {
  1035. int ret;
  1036. if (tracing_disabled)
  1037. return -ENODEV;
  1038. ret = seq_open(file, &show_traces_seq_ops);
  1039. if (!ret) {
  1040. struct seq_file *m = file->private_data;
  1041. m->private = trace_types;
  1042. }
  1043. return ret;
  1044. }
  1045. static struct file_operations tracing_fops = {
  1046. .open = tracing_open,
  1047. .read = seq_read,
  1048. .llseek = seq_lseek,
  1049. .release = tracing_release,
  1050. };
  1051. static struct file_operations tracing_lt_fops = {
  1052. .open = tracing_lt_open,
  1053. .read = seq_read,
  1054. .llseek = seq_lseek,
  1055. .release = tracing_release,
  1056. };
  1057. static struct file_operations show_traces_fops = {
  1058. .open = show_traces_open,
  1059. .read = seq_read,
  1060. .release = seq_release,
  1061. };
  1062. static ssize_t
  1063. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1064. size_t cnt, loff_t *ppos)
  1065. {
  1066. char *buf;
  1067. int r = 0;
  1068. int len = 0;
  1069. int i;
  1070. /* calulate max size */
  1071. for (i = 0; trace_options[i]; i++) {
  1072. len += strlen(trace_options[i]);
  1073. len += 3; /* "no" and space */
  1074. }
  1075. /* +2 for \n and \0 */
  1076. buf = kmalloc(len + 2, GFP_KERNEL);
  1077. if (!buf)
  1078. return -ENOMEM;
  1079. for (i = 0; trace_options[i]; i++) {
  1080. if (trace_flags & (1 << i))
  1081. r += sprintf(buf + r, "%s ", trace_options[i]);
  1082. else
  1083. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1084. }
  1085. r += sprintf(buf + r, "\n");
  1086. WARN_ON(r >= len + 2);
  1087. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1088. buf, r);
  1089. kfree(buf);
  1090. return r;
  1091. }
  1092. static ssize_t
  1093. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1094. size_t cnt, loff_t *ppos)
  1095. {
  1096. char buf[64];
  1097. char *cmp = buf;
  1098. int neg = 0;
  1099. int i;
  1100. if (cnt > 63)
  1101. cnt = 63;
  1102. if (copy_from_user(&buf, ubuf, cnt))
  1103. return -EFAULT;
  1104. buf[cnt] = 0;
  1105. if (strncmp(buf, "no", 2) == 0) {
  1106. neg = 1;
  1107. cmp += 2;
  1108. }
  1109. for (i = 0; trace_options[i]; i++) {
  1110. int len = strlen(trace_options[i]);
  1111. if (strncmp(cmp, trace_options[i], len) == 0) {
  1112. if (neg)
  1113. trace_flags &= ~(1 << i);
  1114. else
  1115. trace_flags |= (1 << i);
  1116. break;
  1117. }
  1118. }
  1119. filp->f_pos += cnt;
  1120. return cnt;
  1121. }
  1122. static struct file_operations tracing_iter_fops = {
  1123. .open = tracing_open_generic,
  1124. .read = tracing_iter_ctrl_read,
  1125. .write = tracing_iter_ctrl_write,
  1126. };
  1127. static const char readme_msg[] =
  1128. "tracing mini-HOWTO:\n\n"
  1129. "# mkdir /debug\n"
  1130. "# mount -t debugfs nodev /debug\n\n"
  1131. "# cat /debug/tracing/available_tracers\n"
  1132. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1133. "# cat /debug/tracing/current_tracer\n"
  1134. "none\n"
  1135. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1136. "# cat /debug/tracing/current_tracer\n"
  1137. "sched_switch\n"
  1138. "# cat /debug/tracing/iter_ctrl\n"
  1139. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1140. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1141. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1142. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1143. "echo 0 > /debug/tracing/tracing_enabled\n"
  1144. ;
  1145. static ssize_t
  1146. tracing_readme_read(struct file *filp, char __user *ubuf,
  1147. size_t cnt, loff_t *ppos)
  1148. {
  1149. return simple_read_from_buffer(ubuf, cnt, ppos,
  1150. readme_msg, strlen(readme_msg));
  1151. }
  1152. static struct file_operations tracing_readme_fops = {
  1153. .open = tracing_open_generic,
  1154. .read = tracing_readme_read,
  1155. };
  1156. static ssize_t
  1157. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1158. size_t cnt, loff_t *ppos)
  1159. {
  1160. struct trace_array *tr = filp->private_data;
  1161. char buf[64];
  1162. int r;
  1163. r = sprintf(buf, "%ld\n", tr->ctrl);
  1164. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1165. }
  1166. static ssize_t
  1167. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1168. size_t cnt, loff_t *ppos)
  1169. {
  1170. struct trace_array *tr = filp->private_data;
  1171. long val;
  1172. char buf[64];
  1173. if (cnt > 63)
  1174. cnt = 63;
  1175. if (copy_from_user(&buf, ubuf, cnt))
  1176. return -EFAULT;
  1177. buf[cnt] = 0;
  1178. val = simple_strtoul(buf, NULL, 10);
  1179. val = !!val;
  1180. mutex_lock(&trace_types_lock);
  1181. if (tr->ctrl ^ val) {
  1182. if (val)
  1183. tracer_enabled = 1;
  1184. else
  1185. tracer_enabled = 0;
  1186. tr->ctrl = val;
  1187. if (current_trace && current_trace->ctrl_update)
  1188. current_trace->ctrl_update(tr);
  1189. }
  1190. mutex_unlock(&trace_types_lock);
  1191. filp->f_pos += cnt;
  1192. return cnt;
  1193. }
  1194. static ssize_t
  1195. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1196. size_t cnt, loff_t *ppos)
  1197. {
  1198. char buf[max_tracer_type_len+2];
  1199. int r;
  1200. mutex_lock(&trace_types_lock);
  1201. if (current_trace)
  1202. r = sprintf(buf, "%s\n", current_trace->name);
  1203. else
  1204. r = sprintf(buf, "\n");
  1205. mutex_unlock(&trace_types_lock);
  1206. return simple_read_from_buffer(ubuf, cnt, ppos,
  1207. buf, r);
  1208. }
  1209. static ssize_t
  1210. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1211. size_t cnt, loff_t *ppos)
  1212. {
  1213. struct trace_array *tr = &global_trace;
  1214. struct tracer *t;
  1215. char buf[max_tracer_type_len+1];
  1216. int i;
  1217. if (cnt > max_tracer_type_len)
  1218. cnt = max_tracer_type_len;
  1219. if (copy_from_user(&buf, ubuf, cnt))
  1220. return -EFAULT;
  1221. buf[cnt] = 0;
  1222. /* strip ending whitespace. */
  1223. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1224. buf[i] = 0;
  1225. mutex_lock(&trace_types_lock);
  1226. for (t = trace_types; t; t = t->next) {
  1227. if (strcmp(t->name, buf) == 0)
  1228. break;
  1229. }
  1230. if (!t || t == current_trace)
  1231. goto out;
  1232. if (current_trace && current_trace->reset)
  1233. current_trace->reset(tr);
  1234. current_trace = t;
  1235. if (t->init)
  1236. t->init(tr);
  1237. out:
  1238. mutex_unlock(&trace_types_lock);
  1239. filp->f_pos += cnt;
  1240. return cnt;
  1241. }
  1242. static ssize_t
  1243. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1244. size_t cnt, loff_t *ppos)
  1245. {
  1246. unsigned long *ptr = filp->private_data;
  1247. char buf[64];
  1248. int r;
  1249. r = snprintf(buf, 64, "%ld\n",
  1250. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1251. if (r > 64)
  1252. r = 64;
  1253. return simple_read_from_buffer(ubuf, cnt, ppos,
  1254. buf, r);
  1255. }
  1256. static ssize_t
  1257. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1258. size_t cnt, loff_t *ppos)
  1259. {
  1260. long *ptr = filp->private_data;
  1261. long val;
  1262. char buf[64];
  1263. if (cnt > 63)
  1264. cnt = 63;
  1265. if (copy_from_user(&buf, ubuf, cnt))
  1266. return -EFAULT;
  1267. buf[cnt] = 0;
  1268. val = simple_strtoul(buf, NULL, 10);
  1269. *ptr = val * 1000;
  1270. return cnt;
  1271. }
  1272. static struct file_operations tracing_max_lat_fops = {
  1273. .open = tracing_open_generic,
  1274. .read = tracing_max_lat_read,
  1275. .write = tracing_max_lat_write,
  1276. };
  1277. static struct file_operations tracing_ctrl_fops = {
  1278. .open = tracing_open_generic,
  1279. .read = tracing_ctrl_read,
  1280. .write = tracing_ctrl_write,
  1281. };
  1282. static struct file_operations set_tracer_fops = {
  1283. .open = tracing_open_generic,
  1284. .read = tracing_set_trace_read,
  1285. .write = tracing_set_trace_write,
  1286. };
  1287. #ifdef CONFIG_DYNAMIC_FTRACE
  1288. static ssize_t
  1289. tracing_read_long(struct file *filp, char __user *ubuf,
  1290. size_t cnt, loff_t *ppos)
  1291. {
  1292. unsigned long *p = filp->private_data;
  1293. char buf[64];
  1294. int r;
  1295. r = sprintf(buf, "%ld\n", *p);
  1296. return simple_read_from_buffer(ubuf, cnt, ppos,
  1297. buf, r);
  1298. }
  1299. static struct file_operations tracing_read_long_fops = {
  1300. .open = tracing_open_generic,
  1301. .read = tracing_read_long,
  1302. };
  1303. #endif
  1304. static struct dentry *d_tracer;
  1305. struct dentry *tracing_init_dentry(void)
  1306. {
  1307. static int once;
  1308. if (d_tracer)
  1309. return d_tracer;
  1310. d_tracer = debugfs_create_dir("tracing", NULL);
  1311. if (!d_tracer && !once) {
  1312. once = 1;
  1313. pr_warning("Could not create debugfs directory 'tracing'\n");
  1314. return NULL;
  1315. }
  1316. return d_tracer;
  1317. }
  1318. #ifdef CONFIG_FTRACE_SELFTEST
  1319. /* Let selftest have access to static functions in this file */
  1320. #include "trace_selftest.c"
  1321. #endif
  1322. static __init void tracer_init_debugfs(void)
  1323. {
  1324. struct dentry *d_tracer;
  1325. struct dentry *entry;
  1326. d_tracer = tracing_init_dentry();
  1327. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1328. &global_trace, &tracing_ctrl_fops);
  1329. if (!entry)
  1330. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  1331. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  1332. NULL, &tracing_iter_fops);
  1333. if (!entry)
  1334. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  1335. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  1336. &global_trace, &tracing_lt_fops);
  1337. if (!entry)
  1338. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  1339. entry = debugfs_create_file("trace", 0444, d_tracer,
  1340. &global_trace, &tracing_fops);
  1341. if (!entry)
  1342. pr_warning("Could not create debugfs 'trace' entry\n");
  1343. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  1344. &global_trace, &show_traces_fops);
  1345. if (!entry)
  1346. pr_warning("Could not create debugfs 'trace' entry\n");
  1347. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  1348. &global_trace, &set_tracer_fops);
  1349. if (!entry)
  1350. pr_warning("Could not create debugfs 'trace' entry\n");
  1351. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  1352. &tracing_max_latency,
  1353. &tracing_max_lat_fops);
  1354. if (!entry)
  1355. pr_warning("Could not create debugfs "
  1356. "'tracing_max_latency' entry\n");
  1357. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  1358. &tracing_thresh, &tracing_max_lat_fops);
  1359. if (!entry)
  1360. pr_warning("Could not create debugfs "
  1361. "'tracing_threash' entry\n");
  1362. entry = debugfs_create_file("README", 0644, d_tracer,
  1363. NULL, &tracing_readme_fops);
  1364. if (!entry)
  1365. pr_warning("Could not create debugfs 'README' entry\n");
  1366. #ifdef CONFIG_DYNAMIC_FTRACE
  1367. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  1368. &ftrace_update_tot_cnt,
  1369. &tracing_read_long_fops);
  1370. if (!entry)
  1371. pr_warning("Could not create debugfs "
  1372. "'dyn_ftrace_total_info' entry\n");
  1373. #endif
  1374. }
  1375. /* dummy trace to disable tracing */
  1376. static struct tracer no_tracer __read_mostly =
  1377. {
  1378. .name = "none",
  1379. };
  1380. static int trace_alloc_page(void)
  1381. {
  1382. struct trace_array_cpu *data;
  1383. struct page *page, *tmp;
  1384. LIST_HEAD(pages);
  1385. void *array;
  1386. int i;
  1387. /* first allocate a page for each CPU */
  1388. for_each_possible_cpu(i) {
  1389. array = (void *)__get_free_page(GFP_KERNEL);
  1390. if (array == NULL) {
  1391. printk(KERN_ERR "tracer: failed to allocate page"
  1392. "for trace buffer!\n");
  1393. goto free_pages;
  1394. }
  1395. page = virt_to_page(array);
  1396. list_add(&page->lru, &pages);
  1397. /* Only allocate if we are actually using the max trace */
  1398. #ifdef CONFIG_TRACER_MAX_TRACE
  1399. array = (void *)__get_free_page(GFP_KERNEL);
  1400. if (array == NULL) {
  1401. printk(KERN_ERR "tracer: failed to allocate page"
  1402. "for trace buffer!\n");
  1403. goto free_pages;
  1404. }
  1405. page = virt_to_page(array);
  1406. list_add(&page->lru, &pages);
  1407. #endif
  1408. }
  1409. /* Now that we successfully allocate a page per CPU, add them */
  1410. for_each_possible_cpu(i) {
  1411. data = global_trace.data[i];
  1412. page = list_entry(pages.next, struct page, lru);
  1413. list_del_init(&page->lru);
  1414. list_add_tail(&page->lru, &data->trace_pages);
  1415. ClearPageLRU(page);
  1416. #ifdef CONFIG_TRACER_MAX_TRACE
  1417. data = max_tr.data[i];
  1418. page = list_entry(pages.next, struct page, lru);
  1419. list_del_init(&page->lru);
  1420. list_add_tail(&page->lru, &data->trace_pages);
  1421. SetPageLRU(page);
  1422. #endif
  1423. }
  1424. global_trace.entries += ENTRIES_PER_PAGE;
  1425. return 0;
  1426. free_pages:
  1427. list_for_each_entry_safe(page, tmp, &pages, lru) {
  1428. list_del_init(&page->lru);
  1429. __free_page(page);
  1430. }
  1431. return -ENOMEM;
  1432. }
  1433. __init static int tracer_alloc_buffers(void)
  1434. {
  1435. struct trace_array_cpu *data;
  1436. void *array;
  1437. struct page *page;
  1438. int pages = 0;
  1439. int ret = -ENOMEM;
  1440. int i;
  1441. /* Allocate the first page for all buffers */
  1442. for_each_possible_cpu(i) {
  1443. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  1444. max_tr.data[i] = &per_cpu(max_data, i);
  1445. array = (void *)__get_free_page(GFP_KERNEL);
  1446. if (array == NULL) {
  1447. printk(KERN_ERR "tracer: failed to allocate page"
  1448. "for trace buffer!\n");
  1449. goto free_buffers;
  1450. }
  1451. /* set the array to the list */
  1452. INIT_LIST_HEAD(&data->trace_pages);
  1453. page = virt_to_page(array);
  1454. list_add(&page->lru, &data->trace_pages);
  1455. /* use the LRU flag to differentiate the two buffers */
  1456. ClearPageLRU(page);
  1457. /* Only allocate if we are actually using the max trace */
  1458. #ifdef CONFIG_TRACER_MAX_TRACE
  1459. array = (void *)__get_free_page(GFP_KERNEL);
  1460. if (array == NULL) {
  1461. printk(KERN_ERR "tracer: failed to allocate page"
  1462. "for trace buffer!\n");
  1463. goto free_buffers;
  1464. }
  1465. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  1466. page = virt_to_page(array);
  1467. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  1468. SetPageLRU(page);
  1469. #endif
  1470. }
  1471. /*
  1472. * Since we allocate by orders of pages, we may be able to
  1473. * round up a bit.
  1474. */
  1475. global_trace.entries = ENTRIES_PER_PAGE;
  1476. pages++;
  1477. while (global_trace.entries < trace_nr_entries) {
  1478. if (trace_alloc_page())
  1479. break;
  1480. pages++;
  1481. }
  1482. max_tr.entries = global_trace.entries;
  1483. pr_info("tracer: %d pages allocated for %ld",
  1484. pages, trace_nr_entries);
  1485. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  1486. pr_info(" actual entries %ld\n", global_trace.entries);
  1487. tracer_init_debugfs();
  1488. trace_init_cmdlines();
  1489. register_tracer(&no_tracer);
  1490. current_trace = &no_tracer;
  1491. /* All seems OK, enable tracing */
  1492. tracing_disabled = 0;
  1493. return 0;
  1494. free_buffers:
  1495. for (i-- ; i >= 0; i--) {
  1496. struct page *page, *tmp;
  1497. struct trace_array_cpu *data = global_trace.data[i];
  1498. if (data) {
  1499. list_for_each_entry_safe(page, tmp,
  1500. &data->trace_pages, lru) {
  1501. list_del_init(&page->lru);
  1502. __free_page(page);
  1503. }
  1504. }
  1505. #ifdef CONFIG_TRACER_MAX_TRACE
  1506. data = max_tr.data[i];
  1507. if (data) {
  1508. list_for_each_entry_safe(page, tmp,
  1509. &data->trace_pages, lru) {
  1510. list_del_init(&page->lru);
  1511. __free_page(page);
  1512. }
  1513. }
  1514. #endif
  1515. }
  1516. return ret;
  1517. }
  1518. fs_initcall(tracer_alloc_buffers);