trace.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  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 const int time_sync_freq_max = 128;
  41. static const cycle_t time_sync_thresh = 100000;
  42. static DEFINE_PER_CPU(cycle_t, time_offset);
  43. static DEFINE_PER_CPU(cycle_t, prev_cpu_time);
  44. static DEFINE_PER_CPU(int, time_sync_count);
  45. static DEFINE_PER_CPU(int, time_sync_freq);
  46. /*
  47. * Global lock which we take every now and then to synchronize
  48. * the CPUs time. This method is not warp-safe, but it's good
  49. * enough to synchronize slowly diverging time sources and thus
  50. * it's good enough for tracing:
  51. */
  52. static DEFINE_SPINLOCK(time_sync_lock);
  53. static cycle_t prev_global_time;
  54. static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&time_sync_lock, flags);
  58. /*
  59. * Update the synchronization frequency:
  60. */
  61. if (per_cpu(time_sync_freq, cpu) < time_sync_freq_max)
  62. per_cpu(time_sync_freq, cpu) *= 2;
  63. per_cpu(time_sync_count, cpu) = per_cpu(time_sync_freq, cpu);
  64. if (time < prev_global_time) {
  65. per_cpu(time_offset, cpu) += prev_global_time - time;
  66. time = prev_global_time;
  67. } else {
  68. prev_global_time = time;
  69. }
  70. spin_unlock_irqrestore(&time_sync_lock, flags);
  71. return time;
  72. }
  73. notrace cycle_t ftrace_now(int cpu)
  74. {
  75. cycle_t prev_cpu_time, time, delta_time;
  76. prev_cpu_time = per_cpu(prev_cpu_time, cpu);
  77. time = sched_clock() + per_cpu(time_offset, cpu);
  78. delta_time = time-prev_cpu_time;
  79. if (unlikely(delta_time > time_sync_thresh ||
  80. --per_cpu(time_sync_count, cpu) <= 0))
  81. time = __ftrace_now_sync(time, cpu);
  82. return time;
  83. }
  84. static struct trace_array global_trace;
  85. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  86. static struct trace_array max_tr;
  87. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  88. static int tracer_enabled;
  89. static unsigned long trace_nr_entries = 16384UL;
  90. static struct tracer *trace_types __read_mostly;
  91. static struct tracer *current_trace __read_mostly;
  92. static int max_tracer_type_len;
  93. static DEFINE_MUTEX(trace_types_lock);
  94. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  95. static int __init set_nr_entries(char *str)
  96. {
  97. if (!str)
  98. return 0;
  99. trace_nr_entries = simple_strtoul(str, &str, 0);
  100. return 1;
  101. }
  102. __setup("trace_entries=", set_nr_entries);
  103. unsigned long nsecs_to_usecs(unsigned long nsecs)
  104. {
  105. return nsecs / 1000;
  106. }
  107. enum trace_type {
  108. __TRACE_FIRST_TYPE = 0,
  109. TRACE_FN,
  110. TRACE_CTX,
  111. __TRACE_LAST_TYPE
  112. };
  113. enum trace_flag_type {
  114. TRACE_FLAG_IRQS_OFF = 0x01,
  115. TRACE_FLAG_NEED_RESCHED = 0x02,
  116. TRACE_FLAG_HARDIRQ = 0x04,
  117. TRACE_FLAG_SOFTIRQ = 0x08,
  118. };
  119. enum trace_iterator_flags {
  120. TRACE_ITER_PRINT_PARENT = 0x01,
  121. TRACE_ITER_SYM_OFFSET = 0x02,
  122. TRACE_ITER_SYM_ADDR = 0x04,
  123. TRACE_ITER_VERBOSE = 0x08,
  124. TRACE_ITER_RAW = 0x10,
  125. };
  126. #define TRACE_ITER_SYM_MASK \
  127. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  128. /* These must match the bit postions above */
  129. static const char *trace_options[] = {
  130. "print-parent",
  131. "sym-offset",
  132. "sym-addr",
  133. "verbose",
  134. "raw",
  135. NULL
  136. };
  137. static unsigned trace_flags;
  138. static DEFINE_SPINLOCK(ftrace_max_lock);
  139. /*
  140. * Copy the new maximum trace into the separate maximum-trace
  141. * structure. (this way the maximum trace is permanently saved,
  142. * for later retrieval via /debugfs/tracing/latency_trace)
  143. */
  144. static notrace void
  145. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  146. {
  147. struct trace_array_cpu *data = tr->data[cpu];
  148. max_tr.cpu = cpu;
  149. max_tr.time_start = data->preempt_timestamp;
  150. data = max_tr.data[cpu];
  151. data->saved_latency = tracing_max_latency;
  152. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  153. data->pid = tsk->pid;
  154. data->uid = tsk->uid;
  155. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  156. data->policy = tsk->policy;
  157. data->rt_priority = tsk->rt_priority;
  158. /* record this tasks comm */
  159. tracing_record_cmdline(current);
  160. }
  161. void check_pages(struct trace_array_cpu *data)
  162. {
  163. struct page *page, *tmp;
  164. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  165. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  166. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  167. BUG_ON(page->lru.next->prev != &page->lru);
  168. BUG_ON(page->lru.prev->next != &page->lru);
  169. }
  170. }
  171. void *head_page(struct trace_array_cpu *data)
  172. {
  173. struct page *page;
  174. check_pages(data);
  175. if (list_empty(&data->trace_pages))
  176. return NULL;
  177. page = list_entry(data->trace_pages.next, struct page, lru);
  178. BUG_ON(&page->lru == &data->trace_pages);
  179. return page_address(page);
  180. }
  181. static notrace int
  182. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  183. {
  184. int len = (PAGE_SIZE - 1) - s->len;
  185. va_list ap;
  186. int ret;
  187. if (!len)
  188. return 0;
  189. va_start(ap, fmt);
  190. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  191. va_end(ap);
  192. /* If we can't write it all, don't bother writing anything */
  193. if (ret > len)
  194. return 0;
  195. s->len += ret;
  196. return len;
  197. }
  198. static notrace int
  199. trace_seq_puts(struct trace_seq *s, const char *str)
  200. {
  201. int len = strlen(str);
  202. if (len > ((PAGE_SIZE - 1) - s->len))
  203. return 0;
  204. memcpy(s->buffer + s->len, str, len);
  205. s->len += len;
  206. return len;
  207. }
  208. static notrace int
  209. trace_seq_putc(struct trace_seq *s, unsigned char c)
  210. {
  211. if (s->len >= (PAGE_SIZE - 1))
  212. return 0;
  213. s->buffer[s->len++] = c;
  214. return 1;
  215. }
  216. static notrace void
  217. trace_seq_reset(struct trace_seq *s)
  218. {
  219. s->len = 0;
  220. }
  221. static notrace void
  222. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  223. {
  224. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  225. s->buffer[len] = 0;
  226. seq_puts(m, s->buffer);
  227. trace_seq_reset(s);
  228. }
  229. notrace static void
  230. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  231. {
  232. struct list_head flip_pages;
  233. INIT_LIST_HEAD(&flip_pages);
  234. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  235. sizeof(struct trace_array_cpu) -
  236. offsetof(struct trace_array_cpu, trace_head_idx));
  237. check_pages(tr1);
  238. check_pages(tr2);
  239. list_splice_init(&tr1->trace_pages, &flip_pages);
  240. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  241. list_splice_init(&flip_pages, &tr2->trace_pages);
  242. BUG_ON(!list_empty(&flip_pages));
  243. check_pages(tr1);
  244. check_pages(tr2);
  245. }
  246. notrace void
  247. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  248. {
  249. struct trace_array_cpu *data;
  250. int i;
  251. WARN_ON_ONCE(!irqs_disabled());
  252. spin_lock(&ftrace_max_lock);
  253. /* clear out all the previous traces */
  254. for_each_possible_cpu(i) {
  255. data = tr->data[i];
  256. flip_trace(max_tr.data[i], data);
  257. tracing_reset(data);
  258. }
  259. __update_max_tr(tr, tsk, cpu);
  260. spin_unlock(&ftrace_max_lock);
  261. }
  262. /**
  263. * update_max_tr_single - only copy one trace over, and reset the rest
  264. * @tr - tracer
  265. * @tsk - task with the latency
  266. * @cpu - the cpu of the buffer to copy.
  267. */
  268. notrace void
  269. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  270. {
  271. struct trace_array_cpu *data = tr->data[cpu];
  272. int i;
  273. WARN_ON_ONCE(!irqs_disabled());
  274. spin_lock(&ftrace_max_lock);
  275. for_each_possible_cpu(i)
  276. tracing_reset(max_tr.data[i]);
  277. flip_trace(max_tr.data[cpu], data);
  278. tracing_reset(data);
  279. __update_max_tr(tr, tsk, cpu);
  280. spin_unlock(&ftrace_max_lock);
  281. }
  282. int register_tracer(struct tracer *type)
  283. {
  284. struct tracer *t;
  285. int len;
  286. int ret = 0;
  287. if (!type->name) {
  288. pr_info("Tracer must have a name\n");
  289. return -1;
  290. }
  291. mutex_lock(&trace_types_lock);
  292. for (t = trace_types; t; t = t->next) {
  293. if (strcmp(type->name, t->name) == 0) {
  294. /* already found */
  295. pr_info("Trace %s already registered\n",
  296. type->name);
  297. ret = -1;
  298. goto out;
  299. }
  300. }
  301. #ifdef CONFIG_FTRACE_STARTUP_TEST
  302. if (type->selftest) {
  303. struct tracer *saved_tracer = current_trace;
  304. struct trace_array_cpu *data;
  305. struct trace_array *tr = &global_trace;
  306. int saved_ctrl = tr->ctrl;
  307. int i;
  308. /*
  309. * Run a selftest on this tracer.
  310. * Here we reset the trace buffer, and set the current
  311. * tracer to be this tracer. The tracer can then run some
  312. * internal tracing to verify that everything is in order.
  313. * If we fail, we do not register this tracer.
  314. */
  315. for_each_possible_cpu(i) {
  316. data = tr->data[i];
  317. if (!head_page(data))
  318. continue;
  319. tracing_reset(data);
  320. }
  321. current_trace = type;
  322. tr->ctrl = 0;
  323. /* the test is responsible for initializing and enabling */
  324. pr_info("Testing tracer %s: ", type->name);
  325. ret = type->selftest(type, tr);
  326. /* the test is responsible for resetting too */
  327. current_trace = saved_tracer;
  328. tr->ctrl = saved_ctrl;
  329. if (ret) {
  330. printk(KERN_CONT "FAILED!\n");
  331. goto out;
  332. }
  333. /* Only reset on passing, to avoid touching corrupted buffers */
  334. for_each_possible_cpu(i) {
  335. data = tr->data[i];
  336. if (!head_page(data))
  337. continue;
  338. tracing_reset(data);
  339. }
  340. printk(KERN_CONT "PASSED\n");
  341. }
  342. #endif
  343. type->next = trace_types;
  344. trace_types = type;
  345. len = strlen(type->name);
  346. if (len > max_tracer_type_len)
  347. max_tracer_type_len = len;
  348. out:
  349. mutex_unlock(&trace_types_lock);
  350. return ret;
  351. }
  352. void unregister_tracer(struct tracer *type)
  353. {
  354. struct tracer **t;
  355. int len;
  356. mutex_lock(&trace_types_lock);
  357. for (t = &trace_types; *t; t = &(*t)->next) {
  358. if (*t == type)
  359. goto found;
  360. }
  361. pr_info("Trace %s not registered\n", type->name);
  362. goto out;
  363. found:
  364. *t = (*t)->next;
  365. if (strlen(type->name) != max_tracer_type_len)
  366. goto out;
  367. max_tracer_type_len = 0;
  368. for (t = &trace_types; *t; t = &(*t)->next) {
  369. len = strlen((*t)->name);
  370. if (len > max_tracer_type_len)
  371. max_tracer_type_len = len;
  372. }
  373. out:
  374. mutex_unlock(&trace_types_lock);
  375. }
  376. notrace void tracing_reset(struct trace_array_cpu *data)
  377. {
  378. data->trace_idx = 0;
  379. data->trace_head = data->trace_tail = head_page(data);
  380. data->trace_head_idx = 0;
  381. data->trace_tail_idx = 0;
  382. }
  383. #ifdef CONFIG_FTRACE
  384. static notrace void
  385. function_trace_call(unsigned long ip, unsigned long parent_ip)
  386. {
  387. struct trace_array *tr = &global_trace;
  388. struct trace_array_cpu *data;
  389. unsigned long flags;
  390. long disabled;
  391. int cpu;
  392. if (unlikely(!tracer_enabled))
  393. return;
  394. local_irq_save(flags);
  395. cpu = raw_smp_processor_id();
  396. data = tr->data[cpu];
  397. disabled = atomic_inc_return(&data->disabled);
  398. if (likely(disabled == 1))
  399. ftrace(tr, data, ip, parent_ip, flags);
  400. atomic_dec(&data->disabled);
  401. local_irq_restore(flags);
  402. }
  403. static struct ftrace_ops trace_ops __read_mostly =
  404. {
  405. .func = function_trace_call,
  406. };
  407. #endif
  408. notrace void tracing_start_function_trace(void)
  409. {
  410. register_ftrace_function(&trace_ops);
  411. }
  412. notrace void tracing_stop_function_trace(void)
  413. {
  414. unregister_ftrace_function(&trace_ops);
  415. }
  416. #define SAVED_CMDLINES 128
  417. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  418. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  419. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  420. static int cmdline_idx;
  421. static DEFINE_SPINLOCK(trace_cmdline_lock);
  422. atomic_t trace_record_cmdline_disabled;
  423. static void trace_init_cmdlines(void)
  424. {
  425. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  426. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  427. cmdline_idx = 0;
  428. }
  429. notrace void trace_stop_cmdline_recording(void);
  430. static notrace void trace_save_cmdline(struct task_struct *tsk)
  431. {
  432. unsigned map;
  433. unsigned idx;
  434. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  435. return;
  436. /*
  437. * It's not the end of the world if we don't get
  438. * the lock, but we also don't want to spin
  439. * nor do we want to disable interrupts,
  440. * so if we miss here, then better luck next time.
  441. */
  442. if (!spin_trylock(&trace_cmdline_lock))
  443. return;
  444. idx = map_pid_to_cmdline[tsk->pid];
  445. if (idx >= SAVED_CMDLINES) {
  446. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  447. map = map_cmdline_to_pid[idx];
  448. if (map <= PID_MAX_DEFAULT)
  449. map_pid_to_cmdline[map] = (unsigned)-1;
  450. map_pid_to_cmdline[tsk->pid] = idx;
  451. cmdline_idx = idx;
  452. }
  453. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  454. spin_unlock(&trace_cmdline_lock);
  455. }
  456. static notrace char *trace_find_cmdline(int pid)
  457. {
  458. char *cmdline = "<...>";
  459. unsigned map;
  460. if (!pid)
  461. return "<idle>";
  462. if (pid > PID_MAX_DEFAULT)
  463. goto out;
  464. map = map_pid_to_cmdline[pid];
  465. if (map >= SAVED_CMDLINES)
  466. goto out;
  467. cmdline = saved_cmdlines[map];
  468. out:
  469. return cmdline;
  470. }
  471. notrace void tracing_record_cmdline(struct task_struct *tsk)
  472. {
  473. if (atomic_read(&trace_record_cmdline_disabled))
  474. return;
  475. trace_save_cmdline(tsk);
  476. }
  477. static inline notrace struct list_head *
  478. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  479. {
  480. /*
  481. * Roundrobin - but skip the head (which is not a real page):
  482. */
  483. next = next->next;
  484. if (unlikely(next == &data->trace_pages))
  485. next = next->next;
  486. BUG_ON(next == &data->trace_pages);
  487. return next;
  488. }
  489. static inline notrace void *
  490. trace_next_page(struct trace_array_cpu *data, void *addr)
  491. {
  492. struct list_head *next;
  493. struct page *page;
  494. page = virt_to_page(addr);
  495. next = trace_next_list(data, &page->lru);
  496. page = list_entry(next, struct page, lru);
  497. return page_address(page);
  498. }
  499. static inline notrace struct trace_entry *
  500. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  501. {
  502. unsigned long idx, idx_next;
  503. struct trace_entry *entry;
  504. data->trace_idx++;
  505. idx = data->trace_head_idx;
  506. idx_next = idx + 1;
  507. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  508. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  509. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  510. data->trace_head = trace_next_page(data, data->trace_head);
  511. idx_next = 0;
  512. }
  513. if (data->trace_head == data->trace_tail &&
  514. idx_next == data->trace_tail_idx) {
  515. /* overrun */
  516. data->trace_tail_idx++;
  517. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  518. data->trace_tail =
  519. trace_next_page(data, data->trace_tail);
  520. data->trace_tail_idx = 0;
  521. }
  522. }
  523. data->trace_head_idx = idx_next;
  524. return entry;
  525. }
  526. static inline notrace void
  527. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  528. {
  529. struct task_struct *tsk = current;
  530. unsigned long pc;
  531. pc = preempt_count();
  532. entry->preempt_count = pc & 0xff;
  533. entry->pid = tsk->pid;
  534. entry->t = ftrace_now(raw_smp_processor_id());
  535. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  536. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  537. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  538. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  539. }
  540. notrace void
  541. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  542. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  543. {
  544. struct trace_entry *entry;
  545. spin_lock(&data->lock);
  546. entry = tracing_get_trace_entry(tr, data);
  547. tracing_generic_entry_update(entry, flags);
  548. entry->type = TRACE_FN;
  549. entry->fn.ip = ip;
  550. entry->fn.parent_ip = parent_ip;
  551. spin_unlock(&data->lock);
  552. }
  553. notrace void
  554. tracing_sched_switch_trace(struct trace_array *tr,
  555. struct trace_array_cpu *data,
  556. struct task_struct *prev, struct task_struct *next,
  557. unsigned long flags)
  558. {
  559. struct trace_entry *entry;
  560. spin_lock(&data->lock);
  561. entry = tracing_get_trace_entry(tr, data);
  562. tracing_generic_entry_update(entry, flags);
  563. entry->type = TRACE_CTX;
  564. entry->ctx.prev_pid = prev->pid;
  565. entry->ctx.prev_prio = prev->prio;
  566. entry->ctx.prev_state = prev->state;
  567. entry->ctx.next_pid = next->pid;
  568. entry->ctx.next_prio = next->prio;
  569. spin_unlock(&data->lock);
  570. }
  571. enum trace_file_type {
  572. TRACE_FILE_LAT_FMT = 1,
  573. };
  574. static struct trace_entry *
  575. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  576. struct trace_iterator *iter, int cpu)
  577. {
  578. struct page *page;
  579. struct trace_entry *array;
  580. if (iter->next_idx[cpu] >= tr->entries ||
  581. iter->next_idx[cpu] >= data->trace_idx ||
  582. (data->trace_head == data->trace_tail &&
  583. data->trace_head_idx == data->trace_tail_idx))
  584. return NULL;
  585. if (!iter->next_page[cpu]) {
  586. /* Initialize the iterator for this cpu trace buffer */
  587. WARN_ON(!data->trace_tail);
  588. page = virt_to_page(data->trace_tail);
  589. iter->next_page[cpu] = &page->lru;
  590. iter->next_page_idx[cpu] = data->trace_tail_idx;
  591. }
  592. page = list_entry(iter->next_page[cpu], struct page, lru);
  593. BUG_ON(&data->trace_pages == &page->lru);
  594. array = page_address(page);
  595. /* Still possible to catch up to the tail */
  596. if (iter->next_idx[cpu] && array == data->trace_tail &&
  597. iter->next_page_idx[cpu] == data->trace_tail_idx)
  598. return NULL;
  599. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  600. return &array[iter->next_page_idx[cpu]];
  601. }
  602. static struct notrace trace_entry *
  603. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  604. {
  605. struct trace_array *tr = iter->tr;
  606. struct trace_entry *ent, *next = NULL;
  607. int next_cpu = -1;
  608. int cpu;
  609. for_each_possible_cpu(cpu) {
  610. if (!head_page(tr->data[cpu]))
  611. continue;
  612. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  613. /*
  614. * Pick the entry with the smallest timestamp:
  615. */
  616. if (ent && (!next || ent->t < next->t)) {
  617. next = ent;
  618. next_cpu = cpu;
  619. }
  620. }
  621. if (ent_cpu)
  622. *ent_cpu = next_cpu;
  623. return next;
  624. }
  625. static notrace void trace_iterator_increment(struct trace_iterator *iter)
  626. {
  627. iter->idx++;
  628. iter->next_idx[iter->cpu]++;
  629. iter->next_page_idx[iter->cpu]++;
  630. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  631. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  632. iter->next_page_idx[iter->cpu] = 0;
  633. iter->next_page[iter->cpu] =
  634. trace_next_list(data, iter->next_page[iter->cpu]);
  635. }
  636. }
  637. static notrace void trace_consume(struct trace_iterator *iter)
  638. {
  639. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  640. data->trace_tail_idx++;
  641. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  642. data->trace_tail = trace_next_page(data, data->trace_tail);
  643. data->trace_tail_idx = 0;
  644. }
  645. /* Check if we empty it, then reset the index */
  646. if (data->trace_head == data->trace_tail &&
  647. data->trace_head_idx == data->trace_tail_idx)
  648. data->trace_idx = 0;
  649. }
  650. static notrace void *find_next_entry_inc(struct trace_iterator *iter)
  651. {
  652. struct trace_entry *next;
  653. int next_cpu = -1;
  654. next = find_next_entry(iter, &next_cpu);
  655. iter->prev_ent = iter->ent;
  656. iter->prev_cpu = iter->cpu;
  657. iter->ent = next;
  658. iter->cpu = next_cpu;
  659. if (next)
  660. trace_iterator_increment(iter);
  661. return next ? iter : NULL;
  662. }
  663. static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
  664. {
  665. struct trace_iterator *iter = m->private;
  666. void *last_ent = iter->ent;
  667. int i = (int)*pos;
  668. void *ent;
  669. (*pos)++;
  670. /* can't go backwards */
  671. if (iter->idx > i)
  672. return NULL;
  673. if (iter->idx < 0)
  674. ent = find_next_entry_inc(iter);
  675. else
  676. ent = iter;
  677. while (ent && iter->idx < i)
  678. ent = find_next_entry_inc(iter);
  679. iter->pos = *pos;
  680. if (last_ent && !ent)
  681. seq_puts(m, "\n\nvim:ft=help\n");
  682. return ent;
  683. }
  684. static void *s_start(struct seq_file *m, loff_t *pos)
  685. {
  686. struct trace_iterator *iter = m->private;
  687. void *p = NULL;
  688. loff_t l = 0;
  689. int i;
  690. mutex_lock(&trace_types_lock);
  691. if (!current_trace || current_trace != iter->trace)
  692. return NULL;
  693. atomic_inc(&trace_record_cmdline_disabled);
  694. /* let the tracer grab locks here if needed */
  695. if (current_trace->start)
  696. current_trace->start(iter);
  697. if (*pos != iter->pos) {
  698. iter->ent = NULL;
  699. iter->cpu = 0;
  700. iter->idx = -1;
  701. iter->prev_ent = NULL;
  702. iter->prev_cpu = -1;
  703. for_each_possible_cpu(i) {
  704. iter->next_idx[i] = 0;
  705. iter->next_page[i] = NULL;
  706. }
  707. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  708. ;
  709. } else {
  710. l = *pos - 1;
  711. p = s_next(m, p, &l);
  712. }
  713. return p;
  714. }
  715. static void s_stop(struct seq_file *m, void *p)
  716. {
  717. struct trace_iterator *iter = m->private;
  718. atomic_dec(&trace_record_cmdline_disabled);
  719. /* let the tracer release locks here if needed */
  720. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  721. iter->trace->stop(iter);
  722. mutex_unlock(&trace_types_lock);
  723. }
  724. static int
  725. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  726. {
  727. #ifdef CONFIG_KALLSYMS
  728. char str[KSYM_SYMBOL_LEN];
  729. kallsyms_lookup(address, NULL, NULL, NULL, str);
  730. return trace_seq_printf(s, fmt, str);
  731. #endif
  732. return 1;
  733. }
  734. static int
  735. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  736. unsigned long address)
  737. {
  738. #ifdef CONFIG_KALLSYMS
  739. char str[KSYM_SYMBOL_LEN];
  740. sprint_symbol(str, address);
  741. return trace_seq_printf(s, fmt, str);
  742. #endif
  743. return 1;
  744. }
  745. #ifndef CONFIG_64BIT
  746. # define IP_FMT "%08lx"
  747. #else
  748. # define IP_FMT "%016lx"
  749. #endif
  750. static notrace int
  751. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  752. {
  753. int ret;
  754. if (!ip)
  755. return trace_seq_printf(s, "0");
  756. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  757. ret = seq_print_sym_offset(s, "%s", ip);
  758. else
  759. ret = seq_print_sym_short(s, "%s", ip);
  760. if (!ret)
  761. return 0;
  762. if (sym_flags & TRACE_ITER_SYM_ADDR)
  763. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  764. return ret;
  765. }
  766. static notrace void print_lat_help_header(struct seq_file *m)
  767. {
  768. seq_puts(m, "# _------=> CPU# \n");
  769. seq_puts(m, "# / _-----=> irqs-off \n");
  770. seq_puts(m, "# | / _----=> need-resched \n");
  771. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  772. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  773. seq_puts(m, "# |||| / \n");
  774. seq_puts(m, "# ||||| delay \n");
  775. seq_puts(m, "# cmd pid ||||| time | caller \n");
  776. seq_puts(m, "# \\ / ||||| \\ | / \n");
  777. }
  778. static notrace void print_func_help_header(struct seq_file *m)
  779. {
  780. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  781. seq_puts(m, "# | | | | |\n");
  782. }
  783. static notrace void
  784. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  785. {
  786. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  787. struct trace_array *tr = iter->tr;
  788. struct trace_array_cpu *data = tr->data[tr->cpu];
  789. struct tracer *type = current_trace;
  790. unsigned long total = 0;
  791. unsigned long entries = 0;
  792. int cpu;
  793. const char *name = "preemption";
  794. if (type)
  795. name = type->name;
  796. for_each_possible_cpu(cpu) {
  797. if (head_page(tr->data[cpu])) {
  798. total += tr->data[cpu]->trace_idx;
  799. if (tr->data[cpu]->trace_idx > tr->entries)
  800. entries += tr->entries;
  801. else
  802. entries += tr->data[cpu]->trace_idx;
  803. }
  804. }
  805. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  806. name, UTS_RELEASE);
  807. seq_puts(m, "-----------------------------------"
  808. "---------------------------------\n");
  809. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  810. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  811. nsecs_to_usecs(data->saved_latency),
  812. entries,
  813. total,
  814. tr->cpu,
  815. #if defined(CONFIG_PREEMPT_NONE)
  816. "server",
  817. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  818. "desktop",
  819. #elif defined(CONFIG_PREEMPT_DESKTOP)
  820. "preempt",
  821. #else
  822. "unknown",
  823. #endif
  824. /* These are reserved for later use */
  825. 0, 0, 0, 0);
  826. #ifdef CONFIG_SMP
  827. seq_printf(m, " #P:%d)\n", num_online_cpus());
  828. #else
  829. seq_puts(m, ")\n");
  830. #endif
  831. seq_puts(m, " -----------------\n");
  832. seq_printf(m, " | task: %.16s-%d "
  833. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  834. data->comm, data->pid, data->uid, data->nice,
  835. data->policy, data->rt_priority);
  836. seq_puts(m, " -----------------\n");
  837. if (data->critical_start) {
  838. seq_puts(m, " => started at: ");
  839. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  840. trace_print_seq(m, &iter->seq);
  841. seq_puts(m, "\n => ended at: ");
  842. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  843. trace_print_seq(m, &iter->seq);
  844. seq_puts(m, "\n");
  845. }
  846. seq_puts(m, "\n");
  847. }
  848. static notrace void
  849. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  850. {
  851. int hardirq, softirq;
  852. char *comm;
  853. comm = trace_find_cmdline(entry->pid);
  854. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  855. trace_seq_printf(s, "%d", cpu);
  856. trace_seq_printf(s, "%c%c",
  857. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  858. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  859. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  860. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  861. if (hardirq && softirq)
  862. trace_seq_putc(s, 'H');
  863. else {
  864. if (hardirq)
  865. trace_seq_putc(s, 'h');
  866. else {
  867. if (softirq)
  868. trace_seq_putc(s, 's');
  869. else
  870. trace_seq_putc(s, '.');
  871. }
  872. }
  873. if (entry->preempt_count)
  874. trace_seq_printf(s, "%x", entry->preempt_count);
  875. else
  876. trace_seq_puts(s, ".");
  877. }
  878. unsigned long preempt_mark_thresh = 100;
  879. static notrace void
  880. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  881. unsigned long rel_usecs)
  882. {
  883. trace_seq_printf(s, " %4lldus", abs_usecs);
  884. if (rel_usecs > preempt_mark_thresh)
  885. trace_seq_puts(s, "!: ");
  886. else if (rel_usecs > 1)
  887. trace_seq_puts(s, "+: ");
  888. else
  889. trace_seq_puts(s, " : ");
  890. }
  891. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  892. static notrace int
  893. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  894. {
  895. struct trace_seq *s = &iter->seq;
  896. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  897. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  898. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  899. struct trace_entry *entry = iter->ent;
  900. unsigned long abs_usecs;
  901. unsigned long rel_usecs;
  902. char *comm;
  903. int S;
  904. if (!next_entry)
  905. next_entry = entry;
  906. rel_usecs = ns2usecs(next_entry->t - entry->t);
  907. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  908. if (verbose) {
  909. comm = trace_find_cmdline(entry->pid);
  910. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  911. " %ld.%03ldms (+%ld.%03ldms): ",
  912. comm,
  913. entry->pid, cpu, entry->flags,
  914. entry->preempt_count, trace_idx,
  915. ns2usecs(entry->t),
  916. abs_usecs/1000,
  917. abs_usecs % 1000, rel_usecs/1000,
  918. rel_usecs % 1000);
  919. } else {
  920. lat_print_generic(s, entry, cpu);
  921. lat_print_timestamp(s, abs_usecs, rel_usecs);
  922. }
  923. switch (entry->type) {
  924. case TRACE_FN:
  925. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  926. trace_seq_puts(s, " (");
  927. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  928. trace_seq_puts(s, ")\n");
  929. break;
  930. case TRACE_CTX:
  931. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  932. state_to_char[entry->ctx.prev_state] : 'X';
  933. comm = trace_find_cmdline(entry->ctx.next_pid);
  934. trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
  935. entry->ctx.prev_pid,
  936. entry->ctx.prev_prio,
  937. S,
  938. entry->ctx.next_pid,
  939. entry->ctx.next_prio,
  940. comm);
  941. break;
  942. default:
  943. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  944. }
  945. return 1;
  946. }
  947. static notrace int print_trace_fmt(struct trace_iterator *iter)
  948. {
  949. struct trace_seq *s = &iter->seq;
  950. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  951. struct trace_entry *entry;
  952. unsigned long usec_rem;
  953. unsigned long long t;
  954. unsigned long secs;
  955. char *comm;
  956. int S;
  957. int ret;
  958. entry = iter->ent;
  959. comm = trace_find_cmdline(iter->ent->pid);
  960. t = ns2usecs(entry->t);
  961. usec_rem = do_div(t, 1000000ULL);
  962. secs = (unsigned long)t;
  963. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  964. if (!ret)
  965. return 0;
  966. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  967. if (!ret)
  968. return 0;
  969. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  970. if (!ret)
  971. return 0;
  972. switch (entry->type) {
  973. case TRACE_FN:
  974. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  975. if (!ret)
  976. return 0;
  977. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  978. entry->fn.parent_ip) {
  979. ret = trace_seq_printf(s, " <-");
  980. if (!ret)
  981. return 0;
  982. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  983. sym_flags);
  984. if (!ret)
  985. return 0;
  986. }
  987. ret = trace_seq_printf(s, "\n");
  988. if (!ret)
  989. return 0;
  990. break;
  991. case TRACE_CTX:
  992. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  993. state_to_char[entry->ctx.prev_state] : 'X';
  994. ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
  995. entry->ctx.prev_pid,
  996. entry->ctx.prev_prio,
  997. S,
  998. entry->ctx.next_pid,
  999. entry->ctx.next_prio);
  1000. if (!ret)
  1001. return 0;
  1002. break;
  1003. }
  1004. return 1;
  1005. }
  1006. static notrace int print_raw_fmt(struct trace_iterator *iter)
  1007. {
  1008. struct trace_seq *s = &iter->seq;
  1009. struct trace_entry *entry;
  1010. int ret;
  1011. int S;
  1012. entry = iter->ent;
  1013. ret = trace_seq_printf(s, "%d %d %llu ",
  1014. entry->pid, iter->cpu, entry->t);
  1015. if (!ret)
  1016. return 0;
  1017. switch (entry->type) {
  1018. case TRACE_FN:
  1019. ret = trace_seq_printf(s, "%x %x\n",
  1020. entry->fn.ip, entry->fn.parent_ip);
  1021. if (!ret)
  1022. return 0;
  1023. break;
  1024. case TRACE_CTX:
  1025. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1026. state_to_char[entry->ctx.prev_state] : 'X';
  1027. ret = trace_seq_printf(s, "%d %d %c %d %d\n",
  1028. entry->ctx.prev_pid,
  1029. entry->ctx.prev_prio,
  1030. S,
  1031. entry->ctx.next_pid,
  1032. entry->ctx.next_prio);
  1033. if (!ret)
  1034. return 0;
  1035. break;
  1036. }
  1037. return 1;
  1038. }
  1039. static int trace_empty(struct trace_iterator *iter)
  1040. {
  1041. struct trace_array_cpu *data;
  1042. int cpu;
  1043. for_each_possible_cpu(cpu) {
  1044. data = iter->tr->data[cpu];
  1045. if (head_page(data) && data->trace_idx &&
  1046. (data->trace_tail != data->trace_head ||
  1047. data->trace_tail_idx != data->trace_head_idx))
  1048. return 0;
  1049. }
  1050. return 1;
  1051. }
  1052. static int print_trace_line(struct trace_iterator *iter)
  1053. {
  1054. if (trace_flags & TRACE_ITER_RAW)
  1055. return print_raw_fmt(iter);
  1056. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1057. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1058. return print_trace_fmt(iter);
  1059. }
  1060. static int s_show(struct seq_file *m, void *v)
  1061. {
  1062. struct trace_iterator *iter = v;
  1063. if (iter->ent == NULL) {
  1064. if (iter->tr) {
  1065. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1066. seq_puts(m, "#\n");
  1067. }
  1068. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1069. /* print nothing if the buffers are empty */
  1070. if (trace_empty(iter))
  1071. return 0;
  1072. print_trace_header(m, iter);
  1073. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1074. print_lat_help_header(m);
  1075. } else {
  1076. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1077. print_func_help_header(m);
  1078. }
  1079. } else {
  1080. print_trace_line(iter);
  1081. trace_print_seq(m, &iter->seq);
  1082. }
  1083. return 0;
  1084. }
  1085. static struct seq_operations tracer_seq_ops = {
  1086. .start = s_start,
  1087. .next = s_next,
  1088. .stop = s_stop,
  1089. .show = s_show,
  1090. };
  1091. static struct trace_iterator notrace *
  1092. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1093. {
  1094. struct trace_iterator *iter;
  1095. if (tracing_disabled) {
  1096. *ret = -ENODEV;
  1097. return NULL;
  1098. }
  1099. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1100. if (!iter) {
  1101. *ret = -ENOMEM;
  1102. goto out;
  1103. }
  1104. mutex_lock(&trace_types_lock);
  1105. if (current_trace && current_trace->print_max)
  1106. iter->tr = &max_tr;
  1107. else
  1108. iter->tr = inode->i_private;
  1109. iter->trace = current_trace;
  1110. iter->pos = -1;
  1111. /* TODO stop tracer */
  1112. *ret = seq_open(file, &tracer_seq_ops);
  1113. if (!*ret) {
  1114. struct seq_file *m = file->private_data;
  1115. m->private = iter;
  1116. /* stop the trace while dumping */
  1117. if (iter->tr->ctrl)
  1118. tracer_enabled = 0;
  1119. if (iter->trace && iter->trace->open)
  1120. iter->trace->open(iter);
  1121. } else {
  1122. kfree(iter);
  1123. iter = NULL;
  1124. }
  1125. mutex_unlock(&trace_types_lock);
  1126. out:
  1127. return iter;
  1128. }
  1129. int tracing_open_generic(struct inode *inode, struct file *filp)
  1130. {
  1131. if (tracing_disabled)
  1132. return -ENODEV;
  1133. filp->private_data = inode->i_private;
  1134. return 0;
  1135. }
  1136. int tracing_release(struct inode *inode, struct file *file)
  1137. {
  1138. struct seq_file *m = (struct seq_file *)file->private_data;
  1139. struct trace_iterator *iter = m->private;
  1140. mutex_lock(&trace_types_lock);
  1141. if (iter->trace && iter->trace->close)
  1142. iter->trace->close(iter);
  1143. /* reenable tracing if it was previously enabled */
  1144. if (iter->tr->ctrl)
  1145. tracer_enabled = 1;
  1146. mutex_unlock(&trace_types_lock);
  1147. seq_release(inode, file);
  1148. kfree(iter);
  1149. return 0;
  1150. }
  1151. static int tracing_open(struct inode *inode, struct file *file)
  1152. {
  1153. int ret;
  1154. __tracing_open(inode, file, &ret);
  1155. return ret;
  1156. }
  1157. static int tracing_lt_open(struct inode *inode, struct file *file)
  1158. {
  1159. struct trace_iterator *iter;
  1160. int ret;
  1161. iter = __tracing_open(inode, file, &ret);
  1162. if (!ret)
  1163. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1164. return ret;
  1165. }
  1166. static notrace void *
  1167. t_next(struct seq_file *m, void *v, loff_t *pos)
  1168. {
  1169. struct tracer *t = m->private;
  1170. (*pos)++;
  1171. if (t)
  1172. t = t->next;
  1173. m->private = t;
  1174. return t;
  1175. }
  1176. static void *t_start(struct seq_file *m, loff_t *pos)
  1177. {
  1178. struct tracer *t = m->private;
  1179. loff_t l = 0;
  1180. mutex_lock(&trace_types_lock);
  1181. for (; t && l < *pos; t = t_next(m, t, &l))
  1182. ;
  1183. return t;
  1184. }
  1185. static void t_stop(struct seq_file *m, void *p)
  1186. {
  1187. mutex_unlock(&trace_types_lock);
  1188. }
  1189. static int t_show(struct seq_file *m, void *v)
  1190. {
  1191. struct tracer *t = v;
  1192. if (!t)
  1193. return 0;
  1194. seq_printf(m, "%s", t->name);
  1195. if (t->next)
  1196. seq_putc(m, ' ');
  1197. else
  1198. seq_putc(m, '\n');
  1199. return 0;
  1200. }
  1201. static struct seq_operations show_traces_seq_ops = {
  1202. .start = t_start,
  1203. .next = t_next,
  1204. .stop = t_stop,
  1205. .show = t_show,
  1206. };
  1207. static int show_traces_open(struct inode *inode, struct file *file)
  1208. {
  1209. int ret;
  1210. if (tracing_disabled)
  1211. return -ENODEV;
  1212. ret = seq_open(file, &show_traces_seq_ops);
  1213. if (!ret) {
  1214. struct seq_file *m = file->private_data;
  1215. m->private = trace_types;
  1216. }
  1217. return ret;
  1218. }
  1219. static struct file_operations tracing_fops = {
  1220. .open = tracing_open,
  1221. .read = seq_read,
  1222. .llseek = seq_lseek,
  1223. .release = tracing_release,
  1224. };
  1225. static struct file_operations tracing_lt_fops = {
  1226. .open = tracing_lt_open,
  1227. .read = seq_read,
  1228. .llseek = seq_lseek,
  1229. .release = tracing_release,
  1230. };
  1231. static struct file_operations show_traces_fops = {
  1232. .open = show_traces_open,
  1233. .read = seq_read,
  1234. .release = seq_release,
  1235. };
  1236. static ssize_t
  1237. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1238. size_t cnt, loff_t *ppos)
  1239. {
  1240. char *buf;
  1241. int r = 0;
  1242. int len = 0;
  1243. int i;
  1244. /* calulate max size */
  1245. for (i = 0; trace_options[i]; i++) {
  1246. len += strlen(trace_options[i]);
  1247. len += 3; /* "no" and space */
  1248. }
  1249. /* +2 for \n and \0 */
  1250. buf = kmalloc(len + 2, GFP_KERNEL);
  1251. if (!buf)
  1252. return -ENOMEM;
  1253. for (i = 0; trace_options[i]; i++) {
  1254. if (trace_flags & (1 << i))
  1255. r += sprintf(buf + r, "%s ", trace_options[i]);
  1256. else
  1257. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1258. }
  1259. r += sprintf(buf + r, "\n");
  1260. WARN_ON(r >= len + 2);
  1261. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1262. buf, r);
  1263. kfree(buf);
  1264. return r;
  1265. }
  1266. static ssize_t
  1267. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1268. size_t cnt, loff_t *ppos)
  1269. {
  1270. char buf[64];
  1271. char *cmp = buf;
  1272. int neg = 0;
  1273. int i;
  1274. if (cnt > 63)
  1275. cnt = 63;
  1276. if (copy_from_user(&buf, ubuf, cnt))
  1277. return -EFAULT;
  1278. buf[cnt] = 0;
  1279. if (strncmp(buf, "no", 2) == 0) {
  1280. neg = 1;
  1281. cmp += 2;
  1282. }
  1283. for (i = 0; trace_options[i]; i++) {
  1284. int len = strlen(trace_options[i]);
  1285. if (strncmp(cmp, trace_options[i], len) == 0) {
  1286. if (neg)
  1287. trace_flags &= ~(1 << i);
  1288. else
  1289. trace_flags |= (1 << i);
  1290. break;
  1291. }
  1292. }
  1293. filp->f_pos += cnt;
  1294. return cnt;
  1295. }
  1296. static struct file_operations tracing_iter_fops = {
  1297. .open = tracing_open_generic,
  1298. .read = tracing_iter_ctrl_read,
  1299. .write = tracing_iter_ctrl_write,
  1300. };
  1301. static const char readme_msg[] =
  1302. "tracing mini-HOWTO:\n\n"
  1303. "# mkdir /debug\n"
  1304. "# mount -t debugfs nodev /debug\n\n"
  1305. "# cat /debug/tracing/available_tracers\n"
  1306. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1307. "# cat /debug/tracing/current_tracer\n"
  1308. "none\n"
  1309. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1310. "# cat /debug/tracing/current_tracer\n"
  1311. "sched_switch\n"
  1312. "# cat /debug/tracing/iter_ctrl\n"
  1313. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1314. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1315. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1316. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1317. "echo 0 > /debug/tracing/tracing_enabled\n"
  1318. ;
  1319. static ssize_t
  1320. tracing_readme_read(struct file *filp, char __user *ubuf,
  1321. size_t cnt, loff_t *ppos)
  1322. {
  1323. return simple_read_from_buffer(ubuf, cnt, ppos,
  1324. readme_msg, strlen(readme_msg));
  1325. }
  1326. static struct file_operations tracing_readme_fops = {
  1327. .open = tracing_open_generic,
  1328. .read = tracing_readme_read,
  1329. };
  1330. static ssize_t
  1331. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1332. size_t cnt, loff_t *ppos)
  1333. {
  1334. struct trace_array *tr = filp->private_data;
  1335. char buf[64];
  1336. int r;
  1337. r = sprintf(buf, "%ld\n", tr->ctrl);
  1338. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1339. }
  1340. static ssize_t
  1341. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1342. size_t cnt, loff_t *ppos)
  1343. {
  1344. struct trace_array *tr = filp->private_data;
  1345. long val;
  1346. char buf[64];
  1347. if (cnt > 63)
  1348. cnt = 63;
  1349. if (copy_from_user(&buf, ubuf, cnt))
  1350. return -EFAULT;
  1351. buf[cnt] = 0;
  1352. val = simple_strtoul(buf, NULL, 10);
  1353. val = !!val;
  1354. mutex_lock(&trace_types_lock);
  1355. if (tr->ctrl ^ val) {
  1356. if (val)
  1357. tracer_enabled = 1;
  1358. else
  1359. tracer_enabled = 0;
  1360. tr->ctrl = val;
  1361. if (current_trace && current_trace->ctrl_update)
  1362. current_trace->ctrl_update(tr);
  1363. }
  1364. mutex_unlock(&trace_types_lock);
  1365. filp->f_pos += cnt;
  1366. return cnt;
  1367. }
  1368. static ssize_t
  1369. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1370. size_t cnt, loff_t *ppos)
  1371. {
  1372. char buf[max_tracer_type_len+2];
  1373. int r;
  1374. mutex_lock(&trace_types_lock);
  1375. if (current_trace)
  1376. r = sprintf(buf, "%s\n", current_trace->name);
  1377. else
  1378. r = sprintf(buf, "\n");
  1379. mutex_unlock(&trace_types_lock);
  1380. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1381. }
  1382. static ssize_t
  1383. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1384. size_t cnt, loff_t *ppos)
  1385. {
  1386. struct trace_array *tr = &global_trace;
  1387. struct tracer *t;
  1388. char buf[max_tracer_type_len+1];
  1389. int i;
  1390. if (cnt > max_tracer_type_len)
  1391. cnt = max_tracer_type_len;
  1392. if (copy_from_user(&buf, ubuf, cnt))
  1393. return -EFAULT;
  1394. buf[cnt] = 0;
  1395. /* strip ending whitespace. */
  1396. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1397. buf[i] = 0;
  1398. mutex_lock(&trace_types_lock);
  1399. for (t = trace_types; t; t = t->next) {
  1400. if (strcmp(t->name, buf) == 0)
  1401. break;
  1402. }
  1403. if (!t || t == current_trace)
  1404. goto out;
  1405. if (current_trace && current_trace->reset)
  1406. current_trace->reset(tr);
  1407. current_trace = t;
  1408. if (t->init)
  1409. t->init(tr);
  1410. out:
  1411. mutex_unlock(&trace_types_lock);
  1412. filp->f_pos += cnt;
  1413. return cnt;
  1414. }
  1415. static ssize_t
  1416. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1417. size_t cnt, loff_t *ppos)
  1418. {
  1419. unsigned long *ptr = filp->private_data;
  1420. char buf[64];
  1421. int r;
  1422. r = snprintf(buf, 64, "%ld\n",
  1423. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1424. if (r > 64)
  1425. r = 64;
  1426. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1427. }
  1428. static ssize_t
  1429. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1430. size_t cnt, loff_t *ppos)
  1431. {
  1432. long *ptr = filp->private_data;
  1433. long val;
  1434. char buf[64];
  1435. if (cnt > 63)
  1436. cnt = 63;
  1437. if (copy_from_user(&buf, ubuf, cnt))
  1438. return -EFAULT;
  1439. buf[cnt] = 0;
  1440. val = simple_strtoul(buf, NULL, 10);
  1441. *ptr = val * 1000;
  1442. return cnt;
  1443. }
  1444. static atomic_t tracing_reader;
  1445. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1446. {
  1447. struct trace_iterator *iter;
  1448. if (tracing_disabled)
  1449. return -ENODEV;
  1450. /* We only allow for reader of the pipe */
  1451. if (atomic_inc_return(&tracing_reader) != 1) {
  1452. atomic_dec(&tracing_reader);
  1453. return -EBUSY;
  1454. }
  1455. /* create a buffer to store the information to pass to userspace */
  1456. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1457. if (!iter)
  1458. return -ENOMEM;
  1459. iter->tr = &global_trace;
  1460. filp->private_data = iter;
  1461. return 0;
  1462. }
  1463. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1464. {
  1465. struct trace_iterator *iter = file->private_data;
  1466. kfree(iter);
  1467. atomic_dec(&tracing_reader);
  1468. return 0;
  1469. }
  1470. /*
  1471. * Consumer reader.
  1472. */
  1473. static ssize_t
  1474. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1475. size_t cnt, loff_t *ppos)
  1476. {
  1477. struct trace_iterator *iter = filp->private_data;
  1478. struct trace_array_cpu *data;
  1479. static cpumask_t mask;
  1480. struct trace_entry *entry;
  1481. static int start;
  1482. unsigned long flags;
  1483. int read = 0;
  1484. int cpu;
  1485. int len;
  1486. int ret;
  1487. /* return any leftover data */
  1488. if (iter->seq.len > start) {
  1489. len = iter->seq.len - start;
  1490. if (cnt > len)
  1491. cnt = len;
  1492. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1493. if (ret)
  1494. cnt = -EFAULT;
  1495. start += len;
  1496. return cnt;
  1497. }
  1498. trace_seq_reset(&iter->seq);
  1499. start = 0;
  1500. while (trace_empty(iter)) {
  1501. /*
  1502. * This is a make-shift waitqueue. The reason we don't use
  1503. * an actual wait queue is because:
  1504. * 1) we only ever have one waiter
  1505. * 2) the tracing, traces all functions, we don't want
  1506. * the overhead of calling wake_up and friends
  1507. * (and tracing them too)
  1508. * Anyway, this is really very primitive wakeup.
  1509. */
  1510. set_current_state(TASK_INTERRUPTIBLE);
  1511. iter->tr->waiter = current;
  1512. /* sleep for one second, and try again. */
  1513. schedule_timeout(HZ);
  1514. iter->tr->waiter = NULL;
  1515. if (signal_pending(current))
  1516. return -EINTR;
  1517. /*
  1518. * We block until we read something and tracing is disabled.
  1519. * We still block if tracing is disabled, but we have never
  1520. * read anything. This allows a user to cat this file, and
  1521. * then enable tracing. But after we have read something,
  1522. * we give an EOF when tracing is again disabled.
  1523. *
  1524. * iter->pos will be 0 if we haven't read anything.
  1525. */
  1526. if (!tracer_enabled && iter->pos)
  1527. break;
  1528. continue;
  1529. }
  1530. /* stop when tracing is finished */
  1531. if (trace_empty(iter))
  1532. return 0;
  1533. if (cnt >= PAGE_SIZE)
  1534. cnt = PAGE_SIZE - 1;
  1535. memset(iter, 0, sizeof(*iter));
  1536. iter->tr = &global_trace;
  1537. iter->pos = -1;
  1538. /*
  1539. * We need to stop all tracing on all CPUS to read the
  1540. * the next buffer. This is a bit expensive, but is
  1541. * not done often. We fill all what we can read,
  1542. * and then release the locks again.
  1543. */
  1544. cpus_clear(mask);
  1545. local_irq_save(flags);
  1546. for_each_possible_cpu(cpu) {
  1547. data = iter->tr->data[cpu];
  1548. if (!head_page(data) || !data->trace_idx)
  1549. continue;
  1550. atomic_inc(&data->disabled);
  1551. spin_lock(&data->lock);
  1552. cpu_set(cpu, mask);
  1553. }
  1554. while ((entry = find_next_entry_inc(iter)) != NULL) {
  1555. ret = print_trace_line(iter);
  1556. if (!ret)
  1557. break;
  1558. trace_consume(iter);
  1559. if (iter->seq.len >= cnt)
  1560. break;
  1561. }
  1562. for_each_cpu_mask(cpu, mask) {
  1563. data = iter->tr->data[cpu];
  1564. spin_unlock(&data->lock);
  1565. atomic_dec(&data->disabled);
  1566. }
  1567. local_irq_restore(flags);
  1568. /* Now copy what we have to the user */
  1569. read = iter->seq.len;
  1570. if (read > cnt)
  1571. read = cnt;
  1572. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1573. if (read < iter->seq.len)
  1574. start = read;
  1575. else
  1576. trace_seq_reset(&iter->seq);
  1577. if (ret)
  1578. read = -EFAULT;
  1579. return read;
  1580. }
  1581. static struct file_operations tracing_max_lat_fops = {
  1582. .open = tracing_open_generic,
  1583. .read = tracing_max_lat_read,
  1584. .write = tracing_max_lat_write,
  1585. };
  1586. static struct file_operations tracing_ctrl_fops = {
  1587. .open = tracing_open_generic,
  1588. .read = tracing_ctrl_read,
  1589. .write = tracing_ctrl_write,
  1590. };
  1591. static struct file_operations set_tracer_fops = {
  1592. .open = tracing_open_generic,
  1593. .read = tracing_set_trace_read,
  1594. .write = tracing_set_trace_write,
  1595. };
  1596. static struct file_operations tracing_pipe_fops = {
  1597. .open = tracing_open_pipe,
  1598. .read = tracing_read_pipe,
  1599. .release = tracing_release_pipe,
  1600. };
  1601. #ifdef CONFIG_DYNAMIC_FTRACE
  1602. static ssize_t
  1603. tracing_read_long(struct file *filp, char __user *ubuf,
  1604. size_t cnt, loff_t *ppos)
  1605. {
  1606. unsigned long *p = filp->private_data;
  1607. char buf[64];
  1608. int r;
  1609. r = sprintf(buf, "%ld\n", *p);
  1610. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1611. }
  1612. static struct file_operations tracing_read_long_fops = {
  1613. .open = tracing_open_generic,
  1614. .read = tracing_read_long,
  1615. };
  1616. #endif
  1617. static struct dentry *d_tracer;
  1618. struct dentry *tracing_init_dentry(void)
  1619. {
  1620. static int once;
  1621. if (d_tracer)
  1622. return d_tracer;
  1623. d_tracer = debugfs_create_dir("tracing", NULL);
  1624. if (!d_tracer && !once) {
  1625. once = 1;
  1626. pr_warning("Could not create debugfs directory 'tracing'\n");
  1627. return NULL;
  1628. }
  1629. return d_tracer;
  1630. }
  1631. #ifdef CONFIG_FTRACE_SELFTEST
  1632. /* Let selftest have access to static functions in this file */
  1633. #include "trace_selftest.c"
  1634. #endif
  1635. static __init void tracer_init_debugfs(void)
  1636. {
  1637. struct dentry *d_tracer;
  1638. struct dentry *entry;
  1639. d_tracer = tracing_init_dentry();
  1640. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1641. &global_trace, &tracing_ctrl_fops);
  1642. if (!entry)
  1643. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  1644. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  1645. NULL, &tracing_iter_fops);
  1646. if (!entry)
  1647. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  1648. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  1649. &global_trace, &tracing_lt_fops);
  1650. if (!entry)
  1651. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  1652. entry = debugfs_create_file("trace", 0444, d_tracer,
  1653. &global_trace, &tracing_fops);
  1654. if (!entry)
  1655. pr_warning("Could not create debugfs 'trace' entry\n");
  1656. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  1657. &global_trace, &show_traces_fops);
  1658. if (!entry)
  1659. pr_warning("Could not create debugfs 'trace' entry\n");
  1660. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  1661. &global_trace, &set_tracer_fops);
  1662. if (!entry)
  1663. pr_warning("Could not create debugfs 'trace' entry\n");
  1664. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  1665. &tracing_max_latency,
  1666. &tracing_max_lat_fops);
  1667. if (!entry)
  1668. pr_warning("Could not create debugfs "
  1669. "'tracing_max_latency' entry\n");
  1670. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  1671. &tracing_thresh, &tracing_max_lat_fops);
  1672. if (!entry)
  1673. pr_warning("Could not create debugfs "
  1674. "'tracing_threash' entry\n");
  1675. entry = debugfs_create_file("README", 0644, d_tracer,
  1676. NULL, &tracing_readme_fops);
  1677. if (!entry)
  1678. pr_warning("Could not create debugfs 'README' entry\n");
  1679. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  1680. NULL, &tracing_pipe_fops);
  1681. if (!entry)
  1682. pr_warning("Could not create debugfs "
  1683. "'tracing_threash' entry\n");
  1684. #ifdef CONFIG_DYNAMIC_FTRACE
  1685. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  1686. &ftrace_update_tot_cnt,
  1687. &tracing_read_long_fops);
  1688. if (!entry)
  1689. pr_warning("Could not create debugfs "
  1690. "'dyn_ftrace_total_info' entry\n");
  1691. #endif
  1692. }
  1693. /* dummy trace to disable tracing */
  1694. static struct tracer no_tracer __read_mostly =
  1695. {
  1696. .name = "none",
  1697. };
  1698. static int trace_alloc_page(void)
  1699. {
  1700. struct trace_array_cpu *data;
  1701. struct page *page, *tmp;
  1702. LIST_HEAD(pages);
  1703. void *array;
  1704. int i;
  1705. /* first allocate a page for each CPU */
  1706. for_each_possible_cpu(i) {
  1707. array = (void *)__get_free_page(GFP_KERNEL);
  1708. if (array == NULL) {
  1709. printk(KERN_ERR "tracer: failed to allocate page"
  1710. "for trace buffer!\n");
  1711. goto free_pages;
  1712. }
  1713. page = virt_to_page(array);
  1714. list_add(&page->lru, &pages);
  1715. /* Only allocate if we are actually using the max trace */
  1716. #ifdef CONFIG_TRACER_MAX_TRACE
  1717. array = (void *)__get_free_page(GFP_KERNEL);
  1718. if (array == NULL) {
  1719. printk(KERN_ERR "tracer: failed to allocate page"
  1720. "for trace buffer!\n");
  1721. goto free_pages;
  1722. }
  1723. page = virt_to_page(array);
  1724. list_add(&page->lru, &pages);
  1725. #endif
  1726. }
  1727. /* Now that we successfully allocate a page per CPU, add them */
  1728. for_each_possible_cpu(i) {
  1729. data = global_trace.data[i];
  1730. spin_lock_init(&data->lock);
  1731. lockdep_set_class(&data->lock, &data->lock_key);
  1732. page = list_entry(pages.next, struct page, lru);
  1733. list_del_init(&page->lru);
  1734. list_add_tail(&page->lru, &data->trace_pages);
  1735. ClearPageLRU(page);
  1736. #ifdef CONFIG_TRACER_MAX_TRACE
  1737. data = max_tr.data[i];
  1738. spin_lock_init(&data->lock);
  1739. lockdep_set_class(&data->lock, &data->lock_key);
  1740. page = list_entry(pages.next, struct page, lru);
  1741. list_del_init(&page->lru);
  1742. list_add_tail(&page->lru, &data->trace_pages);
  1743. SetPageLRU(page);
  1744. #endif
  1745. }
  1746. global_trace.entries += ENTRIES_PER_PAGE;
  1747. return 0;
  1748. free_pages:
  1749. list_for_each_entry_safe(page, tmp, &pages, lru) {
  1750. list_del_init(&page->lru);
  1751. __free_page(page);
  1752. }
  1753. return -ENOMEM;
  1754. }
  1755. __init static int tracer_alloc_buffers(void)
  1756. {
  1757. struct trace_array_cpu *data;
  1758. void *array;
  1759. struct page *page;
  1760. int pages = 0;
  1761. int ret = -ENOMEM;
  1762. int i;
  1763. /* Allocate the first page for all buffers */
  1764. for_each_possible_cpu(i) {
  1765. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  1766. max_tr.data[i] = &per_cpu(max_data, i);
  1767. array = (void *)__get_free_page(GFP_KERNEL);
  1768. if (array == NULL) {
  1769. printk(KERN_ERR "tracer: failed to allocate page"
  1770. "for trace buffer!\n");
  1771. goto free_buffers;
  1772. }
  1773. /* set the array to the list */
  1774. INIT_LIST_HEAD(&data->trace_pages);
  1775. page = virt_to_page(array);
  1776. list_add(&page->lru, &data->trace_pages);
  1777. /* use the LRU flag to differentiate the two buffers */
  1778. ClearPageLRU(page);
  1779. /* Only allocate if we are actually using the max trace */
  1780. #ifdef CONFIG_TRACER_MAX_TRACE
  1781. array = (void *)__get_free_page(GFP_KERNEL);
  1782. if (array == NULL) {
  1783. printk(KERN_ERR "tracer: failed to allocate page"
  1784. "for trace buffer!\n");
  1785. goto free_buffers;
  1786. }
  1787. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  1788. page = virt_to_page(array);
  1789. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  1790. SetPageLRU(page);
  1791. #endif
  1792. }
  1793. /*
  1794. * Since we allocate by orders of pages, we may be able to
  1795. * round up a bit.
  1796. */
  1797. global_trace.entries = ENTRIES_PER_PAGE;
  1798. pages++;
  1799. while (global_trace.entries < trace_nr_entries) {
  1800. if (trace_alloc_page())
  1801. break;
  1802. pages++;
  1803. }
  1804. max_tr.entries = global_trace.entries;
  1805. pr_info("tracer: %d pages allocated for %ld",
  1806. pages, trace_nr_entries);
  1807. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  1808. pr_info(" actual entries %ld\n", global_trace.entries);
  1809. tracer_init_debugfs();
  1810. trace_init_cmdlines();
  1811. register_tracer(&no_tracer);
  1812. current_trace = &no_tracer;
  1813. /* All seems OK, enable tracing */
  1814. tracing_disabled = 0;
  1815. return 0;
  1816. free_buffers:
  1817. for (i-- ; i >= 0; i--) {
  1818. struct page *page, *tmp;
  1819. struct trace_array_cpu *data = global_trace.data[i];
  1820. if (data) {
  1821. list_for_each_entry_safe(page, tmp,
  1822. &data->trace_pages, lru) {
  1823. list_del_init(&page->lru);
  1824. __free_page(page);
  1825. }
  1826. }
  1827. #ifdef CONFIG_TRACER_MAX_TRACE
  1828. data = max_tr.data[i];
  1829. if (data) {
  1830. list_for_each_entry_safe(page, tmp,
  1831. &data->trace_pages, lru) {
  1832. list_del_init(&page->lru);
  1833. __free_page(page);
  1834. }
  1835. }
  1836. #endif
  1837. }
  1838. return ret;
  1839. }
  1840. fs_initcall(tracer_alloc_buffers);