trace.c 41 KB

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