trace.c 40 KB

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