trace.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963
  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/poll.h>
  28. #include <linux/gfp.h>
  29. #include <linux/fs.h>
  30. #include <linux/stacktrace.h>
  31. #include "trace.h"
  32. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  33. unsigned long __read_mostly tracing_thresh;
  34. static unsigned long __read_mostly tracing_nr_buffers;
  35. static cpumask_t __read_mostly tracing_buffer_mask;
  36. #define for_each_tracing_cpu(cpu) \
  37. for_each_cpu_mask(cpu, tracing_buffer_mask)
  38. /* dummy trace to disable tracing */
  39. static struct tracer no_tracer __read_mostly = {
  40. .name = "none",
  41. };
  42. static int trace_alloc_page(void);
  43. static int trace_free_page(void);
  44. static int tracing_disabled = 1;
  45. long
  46. ns2usecs(cycle_t nsec)
  47. {
  48. nsec += 500;
  49. do_div(nsec, 1000);
  50. return nsec;
  51. }
  52. cycle_t ftrace_now(int cpu)
  53. {
  54. return cpu_clock(cpu);
  55. }
  56. /*
  57. * The global_trace is the descriptor that holds the tracing
  58. * buffers for the live tracing. For each CPU, it contains
  59. * a link list of pages that will store trace entries. The
  60. * page descriptor of the pages in the memory is used to hold
  61. * the link list by linking the lru item in the page descriptor
  62. * to each of the pages in the buffer per CPU.
  63. *
  64. * For each active CPU there is a data field that holds the
  65. * pages for the buffer for that CPU. Each CPU has the same number
  66. * of pages allocated for its buffer.
  67. */
  68. static struct trace_array global_trace;
  69. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  70. /*
  71. * The max_tr is used to snapshot the global_trace when a maximum
  72. * latency is reached. Some tracers will use this to store a maximum
  73. * trace while it continues examining live traces.
  74. *
  75. * The buffers for the max_tr are set up the same as the global_trace.
  76. * When a snapshot is taken, the link list of the max_tr is swapped
  77. * with the link list of the global_trace and the buffers are reset for
  78. * the global_trace so the tracing can continue.
  79. */
  80. static struct trace_array max_tr;
  81. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  82. /* tracer_enabled is used to toggle activation of a tracer */
  83. static int tracer_enabled = 1;
  84. /*
  85. * trace_nr_entries is the number of entries that is allocated
  86. * for a buffer. Note, the number of entries is always rounded
  87. * to ENTRIES_PER_PAGE.
  88. */
  89. static unsigned long trace_nr_entries = 65536UL;
  90. /* trace_types holds a link list of available tracers. */
  91. static struct tracer *trace_types __read_mostly;
  92. /* current_trace points to the tracer that is currently active */
  93. static struct tracer *current_trace __read_mostly;
  94. /*
  95. * max_tracer_type_len is used to simplify the allocating of
  96. * buffers to read userspace tracer names. We keep track of
  97. * the longest tracer name registered.
  98. */
  99. static int max_tracer_type_len;
  100. /*
  101. * trace_types_lock is used to protect the trace_types list.
  102. * This lock is also used to keep user access serialized.
  103. * Accesses from userspace will grab this lock while userspace
  104. * activities happen inside the kernel.
  105. */
  106. static DEFINE_MUTEX(trace_types_lock);
  107. /* trace_wait is a waitqueue for tasks blocked on trace_poll */
  108. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  109. /* trace_flags holds iter_ctrl options */
  110. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
  111. /**
  112. * trace_wake_up - wake up tasks waiting for trace input
  113. *
  114. * Simply wakes up any task that is blocked on the trace_wait
  115. * queue. These is used with trace_poll for tasks polling the trace.
  116. */
  117. void trace_wake_up(void)
  118. {
  119. /*
  120. * The runqueue_is_locked() can fail, but this is the best we
  121. * have for now:
  122. */
  123. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  124. wake_up(&trace_wait);
  125. }
  126. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  127. static int __init set_nr_entries(char *str)
  128. {
  129. unsigned long nr_entries;
  130. int ret;
  131. if (!str)
  132. return 0;
  133. ret = strict_strtoul(str, 0, &nr_entries);
  134. /* nr_entries can not be zero */
  135. if (ret < 0 || nr_entries == 0)
  136. return 0;
  137. trace_nr_entries = nr_entries;
  138. return 1;
  139. }
  140. __setup("trace_entries=", set_nr_entries);
  141. unsigned long nsecs_to_usecs(unsigned long nsecs)
  142. {
  143. return nsecs / 1000;
  144. }
  145. /*
  146. * trace_flag_type is an enumeration that holds different
  147. * states when a trace occurs. These are:
  148. * IRQS_OFF - interrupts were disabled
  149. * NEED_RESCED - reschedule is requested
  150. * HARDIRQ - inside an interrupt handler
  151. * SOFTIRQ - inside a softirq handler
  152. */
  153. enum trace_flag_type {
  154. TRACE_FLAG_IRQS_OFF = 0x01,
  155. TRACE_FLAG_NEED_RESCHED = 0x02,
  156. TRACE_FLAG_HARDIRQ = 0x04,
  157. TRACE_FLAG_SOFTIRQ = 0x08,
  158. };
  159. /*
  160. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  161. * control the output of kernel symbols.
  162. */
  163. #define TRACE_ITER_SYM_MASK \
  164. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  165. /* These must match the bit postions in trace_iterator_flags */
  166. static const char *trace_options[] = {
  167. "print-parent",
  168. "sym-offset",
  169. "sym-addr",
  170. "verbose",
  171. "raw",
  172. "hex",
  173. "bin",
  174. "block",
  175. "stacktrace",
  176. "sched-tree",
  177. NULL
  178. };
  179. /*
  180. * ftrace_max_lock is used to protect the swapping of buffers
  181. * when taking a max snapshot. The buffers themselves are
  182. * protected by per_cpu spinlocks. But the action of the swap
  183. * needs its own lock.
  184. *
  185. * This is defined as a raw_spinlock_t in order to help
  186. * with performance when lockdep debugging is enabled.
  187. */
  188. static raw_spinlock_t ftrace_max_lock =
  189. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  190. /*
  191. * Copy the new maximum trace into the separate maximum-trace
  192. * structure. (this way the maximum trace is permanently saved,
  193. * for later retrieval via /debugfs/tracing/latency_trace)
  194. */
  195. static void
  196. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  197. {
  198. struct trace_array_cpu *data = tr->data[cpu];
  199. max_tr.cpu = cpu;
  200. max_tr.time_start = data->preempt_timestamp;
  201. data = max_tr.data[cpu];
  202. data->saved_latency = tracing_max_latency;
  203. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  204. data->pid = tsk->pid;
  205. data->uid = tsk->uid;
  206. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  207. data->policy = tsk->policy;
  208. data->rt_priority = tsk->rt_priority;
  209. /* record this tasks comm */
  210. tracing_record_cmdline(current);
  211. }
  212. /**
  213. * check_pages - integrity check of trace buffers
  214. *
  215. * As a safty measure we check to make sure the data pages have not
  216. * been corrupted. TODO: configure to disable this because it adds
  217. * a bit of overhead.
  218. */
  219. void check_pages(struct trace_array_cpu *data)
  220. {
  221. struct page *page, *tmp;
  222. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  223. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  224. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  225. BUG_ON(page->lru.next->prev != &page->lru);
  226. BUG_ON(page->lru.prev->next != &page->lru);
  227. }
  228. }
  229. /**
  230. * head_page - page address of the first page in per_cpu buffer.
  231. *
  232. * head_page returns the page address of the first page in
  233. * a per_cpu buffer. This also preforms various consistency
  234. * checks to make sure the buffer has not been corrupted.
  235. */
  236. void *head_page(struct trace_array_cpu *data)
  237. {
  238. struct page *page;
  239. check_pages(data);
  240. if (list_empty(&data->trace_pages))
  241. return NULL;
  242. page = list_entry(data->trace_pages.next, struct page, lru);
  243. BUG_ON(&page->lru == &data->trace_pages);
  244. return page_address(page);
  245. }
  246. /**
  247. * trace_seq_printf - sequence printing of trace information
  248. * @s: trace sequence descriptor
  249. * @fmt: printf format string
  250. *
  251. * The tracer may use either sequence operations or its own
  252. * copy to user routines. To simplify formating of a trace
  253. * trace_seq_printf is used to store strings into a special
  254. * buffer (@s). Then the output may be either used by
  255. * the sequencer or pulled into another buffer.
  256. */
  257. int
  258. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  259. {
  260. int len = (PAGE_SIZE - 1) - s->len;
  261. va_list ap;
  262. int ret;
  263. if (!len)
  264. return 0;
  265. va_start(ap, fmt);
  266. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  267. va_end(ap);
  268. /* If we can't write it all, don't bother writing anything */
  269. if (ret >= len)
  270. return 0;
  271. s->len += ret;
  272. return len;
  273. }
  274. /**
  275. * trace_seq_puts - trace sequence printing of simple string
  276. * @s: trace sequence descriptor
  277. * @str: simple string to record
  278. *
  279. * The tracer may use either the sequence operations or its own
  280. * copy to user routines. This function records a simple string
  281. * into a special buffer (@s) for later retrieval by a sequencer
  282. * or other mechanism.
  283. */
  284. static int
  285. trace_seq_puts(struct trace_seq *s, const char *str)
  286. {
  287. int len = strlen(str);
  288. if (len > ((PAGE_SIZE - 1) - s->len))
  289. return 0;
  290. memcpy(s->buffer + s->len, str, len);
  291. s->len += len;
  292. return len;
  293. }
  294. static int
  295. trace_seq_putc(struct trace_seq *s, unsigned char c)
  296. {
  297. if (s->len >= (PAGE_SIZE - 1))
  298. return 0;
  299. s->buffer[s->len++] = c;
  300. return 1;
  301. }
  302. static int
  303. trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
  304. {
  305. if (len > ((PAGE_SIZE - 1) - s->len))
  306. return 0;
  307. memcpy(s->buffer + s->len, mem, len);
  308. s->len += len;
  309. return len;
  310. }
  311. #define HEX_CHARS 17
  312. static const char hex2asc[] = "0123456789abcdef";
  313. static int
  314. trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
  315. {
  316. unsigned char hex[HEX_CHARS];
  317. unsigned char *data = mem;
  318. unsigned char byte;
  319. int i, j;
  320. BUG_ON(len >= HEX_CHARS);
  321. #ifdef __BIG_ENDIAN
  322. for (i = 0, j = 0; i < len; i++) {
  323. #else
  324. for (i = len-1, j = 0; i >= 0; i--) {
  325. #endif
  326. byte = data[i];
  327. hex[j++] = hex2asc[byte & 0x0f];
  328. hex[j++] = hex2asc[byte >> 4];
  329. }
  330. hex[j++] = ' ';
  331. return trace_seq_putmem(s, hex, j);
  332. }
  333. static void
  334. trace_seq_reset(struct trace_seq *s)
  335. {
  336. s->len = 0;
  337. }
  338. static void
  339. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  340. {
  341. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  342. s->buffer[len] = 0;
  343. seq_puts(m, s->buffer);
  344. trace_seq_reset(s);
  345. }
  346. /*
  347. * flip the trace buffers between two trace descriptors.
  348. * This usually is the buffers between the global_trace and
  349. * the max_tr to record a snapshot of a current trace.
  350. *
  351. * The ftrace_max_lock must be held.
  352. */
  353. static void
  354. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  355. {
  356. struct list_head flip_pages;
  357. INIT_LIST_HEAD(&flip_pages);
  358. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  359. sizeof(struct trace_array_cpu) -
  360. offsetof(struct trace_array_cpu, trace_head_idx));
  361. check_pages(tr1);
  362. check_pages(tr2);
  363. list_splice_init(&tr1->trace_pages, &flip_pages);
  364. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  365. list_splice_init(&flip_pages, &tr2->trace_pages);
  366. BUG_ON(!list_empty(&flip_pages));
  367. check_pages(tr1);
  368. check_pages(tr2);
  369. }
  370. /**
  371. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  372. * @tr: tracer
  373. * @tsk: the task with the latency
  374. * @cpu: The cpu that initiated the trace.
  375. *
  376. * Flip the buffers between the @tr and the max_tr and record information
  377. * about which task was the cause of this latency.
  378. */
  379. void
  380. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  381. {
  382. struct trace_array_cpu *data;
  383. int i;
  384. WARN_ON_ONCE(!irqs_disabled());
  385. __raw_spin_lock(&ftrace_max_lock);
  386. /* clear out all the previous traces */
  387. for_each_tracing_cpu(i) {
  388. data = tr->data[i];
  389. flip_trace(max_tr.data[i], data);
  390. tracing_reset(data);
  391. }
  392. __update_max_tr(tr, tsk, cpu);
  393. __raw_spin_unlock(&ftrace_max_lock);
  394. }
  395. /**
  396. * update_max_tr_single - only copy one trace over, and reset the rest
  397. * @tr - tracer
  398. * @tsk - task with the latency
  399. * @cpu - the cpu of the buffer to copy.
  400. *
  401. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  402. */
  403. void
  404. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  405. {
  406. struct trace_array_cpu *data = tr->data[cpu];
  407. int i;
  408. WARN_ON_ONCE(!irqs_disabled());
  409. __raw_spin_lock(&ftrace_max_lock);
  410. for_each_tracing_cpu(i)
  411. tracing_reset(max_tr.data[i]);
  412. flip_trace(max_tr.data[cpu], data);
  413. tracing_reset(data);
  414. __update_max_tr(tr, tsk, cpu);
  415. __raw_spin_unlock(&ftrace_max_lock);
  416. }
  417. /**
  418. * register_tracer - register a tracer with the ftrace system.
  419. * @type - the plugin for the tracer
  420. *
  421. * Register a new plugin tracer.
  422. */
  423. int register_tracer(struct tracer *type)
  424. {
  425. struct tracer *t;
  426. int len;
  427. int ret = 0;
  428. if (!type->name) {
  429. pr_info("Tracer must have a name\n");
  430. return -1;
  431. }
  432. mutex_lock(&trace_types_lock);
  433. for (t = trace_types; t; t = t->next) {
  434. if (strcmp(type->name, t->name) == 0) {
  435. /* already found */
  436. pr_info("Trace %s already registered\n",
  437. type->name);
  438. ret = -1;
  439. goto out;
  440. }
  441. }
  442. #ifdef CONFIG_FTRACE_STARTUP_TEST
  443. if (type->selftest) {
  444. struct tracer *saved_tracer = current_trace;
  445. struct trace_array_cpu *data;
  446. struct trace_array *tr = &global_trace;
  447. int saved_ctrl = tr->ctrl;
  448. int i;
  449. /*
  450. * Run a selftest on this tracer.
  451. * Here we reset the trace buffer, and set the current
  452. * tracer to be this tracer. The tracer can then run some
  453. * internal tracing to verify that everything is in order.
  454. * If we fail, we do not register this tracer.
  455. */
  456. for_each_tracing_cpu(i) {
  457. data = tr->data[i];
  458. if (!head_page(data))
  459. continue;
  460. tracing_reset(data);
  461. }
  462. current_trace = type;
  463. tr->ctrl = 0;
  464. /* the test is responsible for initializing and enabling */
  465. pr_info("Testing tracer %s: ", type->name);
  466. ret = type->selftest(type, tr);
  467. /* the test is responsible for resetting too */
  468. current_trace = saved_tracer;
  469. tr->ctrl = saved_ctrl;
  470. if (ret) {
  471. printk(KERN_CONT "FAILED!\n");
  472. goto out;
  473. }
  474. /* Only reset on passing, to avoid touching corrupted buffers */
  475. for_each_tracing_cpu(i) {
  476. data = tr->data[i];
  477. if (!head_page(data))
  478. continue;
  479. tracing_reset(data);
  480. }
  481. printk(KERN_CONT "PASSED\n");
  482. }
  483. #endif
  484. type->next = trace_types;
  485. trace_types = type;
  486. len = strlen(type->name);
  487. if (len > max_tracer_type_len)
  488. max_tracer_type_len = len;
  489. out:
  490. mutex_unlock(&trace_types_lock);
  491. return ret;
  492. }
  493. void unregister_tracer(struct tracer *type)
  494. {
  495. struct tracer **t;
  496. int len;
  497. mutex_lock(&trace_types_lock);
  498. for (t = &trace_types; *t; t = &(*t)->next) {
  499. if (*t == type)
  500. goto found;
  501. }
  502. pr_info("Trace %s not registered\n", type->name);
  503. goto out;
  504. found:
  505. *t = (*t)->next;
  506. if (strlen(type->name) != max_tracer_type_len)
  507. goto out;
  508. max_tracer_type_len = 0;
  509. for (t = &trace_types; *t; t = &(*t)->next) {
  510. len = strlen((*t)->name);
  511. if (len > max_tracer_type_len)
  512. max_tracer_type_len = len;
  513. }
  514. out:
  515. mutex_unlock(&trace_types_lock);
  516. }
  517. void tracing_reset(struct trace_array_cpu *data)
  518. {
  519. data->trace_idx = 0;
  520. data->trace_head = data->trace_tail = head_page(data);
  521. data->trace_head_idx = 0;
  522. data->trace_tail_idx = 0;
  523. }
  524. #define SAVED_CMDLINES 128
  525. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  526. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  527. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  528. static int cmdline_idx;
  529. static DEFINE_SPINLOCK(trace_cmdline_lock);
  530. /* trace in all context switches */
  531. atomic_t trace_record_cmdline_enabled __read_mostly;
  532. /* temporary disable recording */
  533. atomic_t trace_record_cmdline_disabled __read_mostly;
  534. static void trace_init_cmdlines(void)
  535. {
  536. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  537. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  538. cmdline_idx = 0;
  539. }
  540. void trace_stop_cmdline_recording(void);
  541. static void trace_save_cmdline(struct task_struct *tsk)
  542. {
  543. unsigned map;
  544. unsigned idx;
  545. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  546. return;
  547. /*
  548. * It's not the end of the world if we don't get
  549. * the lock, but we also don't want to spin
  550. * nor do we want to disable interrupts,
  551. * so if we miss here, then better luck next time.
  552. */
  553. if (!spin_trylock(&trace_cmdline_lock))
  554. return;
  555. idx = map_pid_to_cmdline[tsk->pid];
  556. if (idx >= SAVED_CMDLINES) {
  557. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  558. map = map_cmdline_to_pid[idx];
  559. if (map <= PID_MAX_DEFAULT)
  560. map_pid_to_cmdline[map] = (unsigned)-1;
  561. map_pid_to_cmdline[tsk->pid] = idx;
  562. cmdline_idx = idx;
  563. }
  564. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  565. spin_unlock(&trace_cmdline_lock);
  566. }
  567. static char *trace_find_cmdline(int pid)
  568. {
  569. char *cmdline = "<...>";
  570. unsigned map;
  571. if (!pid)
  572. return "<idle>";
  573. if (pid > PID_MAX_DEFAULT)
  574. goto out;
  575. map = map_pid_to_cmdline[pid];
  576. if (map >= SAVED_CMDLINES)
  577. goto out;
  578. cmdline = saved_cmdlines[map];
  579. out:
  580. return cmdline;
  581. }
  582. void tracing_record_cmdline(struct task_struct *tsk)
  583. {
  584. if (atomic_read(&trace_record_cmdline_disabled))
  585. return;
  586. trace_save_cmdline(tsk);
  587. }
  588. static inline struct list_head *
  589. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  590. {
  591. /*
  592. * Roundrobin - but skip the head (which is not a real page):
  593. */
  594. next = next->next;
  595. if (unlikely(next == &data->trace_pages))
  596. next = next->next;
  597. BUG_ON(next == &data->trace_pages);
  598. return next;
  599. }
  600. static inline void *
  601. trace_next_page(struct trace_array_cpu *data, void *addr)
  602. {
  603. struct list_head *next;
  604. struct page *page;
  605. page = virt_to_page(addr);
  606. next = trace_next_list(data, &page->lru);
  607. page = list_entry(next, struct page, lru);
  608. return page_address(page);
  609. }
  610. static inline struct trace_entry *
  611. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  612. {
  613. unsigned long idx, idx_next;
  614. struct trace_entry *entry;
  615. data->trace_idx++;
  616. idx = data->trace_head_idx;
  617. idx_next = idx + 1;
  618. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  619. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  620. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  621. data->trace_head = trace_next_page(data, data->trace_head);
  622. idx_next = 0;
  623. }
  624. if (data->trace_head == data->trace_tail &&
  625. idx_next == data->trace_tail_idx) {
  626. /* overrun */
  627. data->trace_tail_idx++;
  628. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  629. data->trace_tail =
  630. trace_next_page(data, data->trace_tail);
  631. data->trace_tail_idx = 0;
  632. }
  633. }
  634. data->trace_head_idx = idx_next;
  635. return entry;
  636. }
  637. static inline void
  638. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  639. {
  640. struct task_struct *tsk = current;
  641. unsigned long pc;
  642. pc = preempt_count();
  643. entry->preempt_count = pc & 0xff;
  644. entry->pid = (tsk) ? tsk->pid : 0;
  645. entry->t = ftrace_now(raw_smp_processor_id());
  646. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  647. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  648. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  649. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  650. }
  651. void
  652. trace_function(struct trace_array *tr, struct trace_array_cpu *data,
  653. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  654. {
  655. struct trace_entry *entry;
  656. unsigned long irq_flags;
  657. raw_local_irq_save(irq_flags);
  658. __raw_spin_lock(&data->lock);
  659. entry = tracing_get_trace_entry(tr, data);
  660. tracing_generic_entry_update(entry, flags);
  661. entry->type = TRACE_FN;
  662. entry->fn.ip = ip;
  663. entry->fn.parent_ip = parent_ip;
  664. __raw_spin_unlock(&data->lock);
  665. raw_local_irq_restore(irq_flags);
  666. }
  667. void
  668. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  669. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  670. {
  671. if (likely(!atomic_read(&data->disabled)))
  672. trace_function(tr, data, ip, parent_ip, flags);
  673. }
  674. void
  675. __trace_special(void *__tr, void *__data,
  676. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  677. {
  678. struct trace_array_cpu *data = __data;
  679. struct trace_array *tr = __tr;
  680. struct trace_entry *entry;
  681. unsigned long irq_flags;
  682. raw_local_irq_save(irq_flags);
  683. __raw_spin_lock(&data->lock);
  684. entry = tracing_get_trace_entry(tr, data);
  685. tracing_generic_entry_update(entry, 0);
  686. entry->type = TRACE_SPECIAL;
  687. entry->special.arg1 = arg1;
  688. entry->special.arg2 = arg2;
  689. entry->special.arg3 = arg3;
  690. __raw_spin_unlock(&data->lock);
  691. raw_local_irq_restore(irq_flags);
  692. trace_wake_up();
  693. }
  694. void __trace_stack(struct trace_array *tr,
  695. struct trace_array_cpu *data,
  696. unsigned long flags,
  697. int skip)
  698. {
  699. struct trace_entry *entry;
  700. struct stack_trace trace;
  701. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  702. return;
  703. entry = tracing_get_trace_entry(tr, data);
  704. tracing_generic_entry_update(entry, flags);
  705. entry->type = TRACE_STACK;
  706. memset(&entry->stack, 0, sizeof(entry->stack));
  707. trace.nr_entries = 0;
  708. trace.max_entries = FTRACE_STACK_ENTRIES;
  709. trace.skip = skip;
  710. trace.entries = entry->stack.caller;
  711. save_stack_trace(&trace);
  712. }
  713. void
  714. tracing_sched_switch_trace(struct trace_array *tr,
  715. struct trace_array_cpu *data,
  716. struct task_struct *prev,
  717. struct task_struct *next,
  718. unsigned long flags)
  719. {
  720. struct trace_entry *entry;
  721. unsigned long irq_flags;
  722. raw_local_irq_save(irq_flags);
  723. __raw_spin_lock(&data->lock);
  724. entry = tracing_get_trace_entry(tr, data);
  725. tracing_generic_entry_update(entry, flags);
  726. entry->type = TRACE_CTX;
  727. entry->ctx.prev_pid = prev->pid;
  728. entry->ctx.prev_prio = prev->prio;
  729. entry->ctx.prev_state = prev->state;
  730. entry->ctx.next_pid = next->pid;
  731. entry->ctx.next_prio = next->prio;
  732. entry->ctx.next_state = next->state;
  733. __trace_stack(tr, data, flags, 4);
  734. __raw_spin_unlock(&data->lock);
  735. raw_local_irq_restore(irq_flags);
  736. }
  737. void
  738. tracing_sched_wakeup_trace(struct trace_array *tr,
  739. struct trace_array_cpu *data,
  740. struct task_struct *wakee,
  741. struct task_struct *curr,
  742. unsigned long flags)
  743. {
  744. struct trace_entry *entry;
  745. unsigned long irq_flags;
  746. raw_local_irq_save(irq_flags);
  747. __raw_spin_lock(&data->lock);
  748. entry = tracing_get_trace_entry(tr, data);
  749. tracing_generic_entry_update(entry, flags);
  750. entry->type = TRACE_WAKE;
  751. entry->ctx.prev_pid = curr->pid;
  752. entry->ctx.prev_prio = curr->prio;
  753. entry->ctx.prev_state = curr->state;
  754. entry->ctx.next_pid = wakee->pid;
  755. entry->ctx.next_prio = wakee->prio;
  756. entry->ctx.next_state = wakee->state;
  757. __trace_stack(tr, data, flags, 5);
  758. __raw_spin_unlock(&data->lock);
  759. raw_local_irq_restore(irq_flags);
  760. trace_wake_up();
  761. }
  762. #ifdef CONFIG_FTRACE
  763. static void
  764. function_trace_call(unsigned long ip, unsigned long parent_ip)
  765. {
  766. struct trace_array *tr = &global_trace;
  767. struct trace_array_cpu *data;
  768. unsigned long flags;
  769. long disabled;
  770. int cpu;
  771. if (unlikely(!tracer_enabled))
  772. return;
  773. local_irq_save(flags);
  774. cpu = raw_smp_processor_id();
  775. data = tr->data[cpu];
  776. disabled = atomic_inc_return(&data->disabled);
  777. if (likely(disabled == 1))
  778. trace_function(tr, data, ip, parent_ip, flags);
  779. atomic_dec(&data->disabled);
  780. local_irq_restore(flags);
  781. }
  782. static struct ftrace_ops trace_ops __read_mostly =
  783. {
  784. .func = function_trace_call,
  785. };
  786. void tracing_start_function_trace(void)
  787. {
  788. register_ftrace_function(&trace_ops);
  789. }
  790. void tracing_stop_function_trace(void)
  791. {
  792. unregister_ftrace_function(&trace_ops);
  793. }
  794. #endif
  795. enum trace_file_type {
  796. TRACE_FILE_LAT_FMT = 1,
  797. };
  798. static struct trace_entry *
  799. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  800. struct trace_iterator *iter, int cpu)
  801. {
  802. struct page *page;
  803. struct trace_entry *array;
  804. if (iter->next_idx[cpu] >= tr->entries ||
  805. iter->next_idx[cpu] >= data->trace_idx ||
  806. (data->trace_head == data->trace_tail &&
  807. data->trace_head_idx == data->trace_tail_idx))
  808. return NULL;
  809. if (!iter->next_page[cpu]) {
  810. /* Initialize the iterator for this cpu trace buffer */
  811. WARN_ON(!data->trace_tail);
  812. page = virt_to_page(data->trace_tail);
  813. iter->next_page[cpu] = &page->lru;
  814. iter->next_page_idx[cpu] = data->trace_tail_idx;
  815. }
  816. page = list_entry(iter->next_page[cpu], struct page, lru);
  817. BUG_ON(&data->trace_pages == &page->lru);
  818. array = page_address(page);
  819. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  820. return &array[iter->next_page_idx[cpu]];
  821. }
  822. static struct trace_entry *
  823. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  824. {
  825. struct trace_array *tr = iter->tr;
  826. struct trace_entry *ent, *next = NULL;
  827. int next_cpu = -1;
  828. int cpu;
  829. for_each_tracing_cpu(cpu) {
  830. if (!head_page(tr->data[cpu]))
  831. continue;
  832. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  833. /*
  834. * Pick the entry with the smallest timestamp:
  835. */
  836. if (ent && (!next || ent->t < next->t)) {
  837. next = ent;
  838. next_cpu = cpu;
  839. }
  840. }
  841. if (ent_cpu)
  842. *ent_cpu = next_cpu;
  843. return next;
  844. }
  845. static void trace_iterator_increment(struct trace_iterator *iter)
  846. {
  847. iter->idx++;
  848. iter->next_idx[iter->cpu]++;
  849. iter->next_page_idx[iter->cpu]++;
  850. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  851. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  852. iter->next_page_idx[iter->cpu] = 0;
  853. iter->next_page[iter->cpu] =
  854. trace_next_list(data, iter->next_page[iter->cpu]);
  855. }
  856. }
  857. static void trace_consume(struct trace_iterator *iter)
  858. {
  859. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  860. data->trace_tail_idx++;
  861. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  862. data->trace_tail = trace_next_page(data, data->trace_tail);
  863. data->trace_tail_idx = 0;
  864. }
  865. /* Check if we empty it, then reset the index */
  866. if (data->trace_head == data->trace_tail &&
  867. data->trace_head_idx == data->trace_tail_idx)
  868. data->trace_idx = 0;
  869. }
  870. static void *find_next_entry_inc(struct trace_iterator *iter)
  871. {
  872. struct trace_entry *next;
  873. int next_cpu = -1;
  874. next = find_next_entry(iter, &next_cpu);
  875. iter->prev_ent = iter->ent;
  876. iter->prev_cpu = iter->cpu;
  877. iter->ent = next;
  878. iter->cpu = next_cpu;
  879. if (next)
  880. trace_iterator_increment(iter);
  881. return next ? iter : NULL;
  882. }
  883. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  884. {
  885. struct trace_iterator *iter = m->private;
  886. void *last_ent = iter->ent;
  887. int i = (int)*pos;
  888. void *ent;
  889. (*pos)++;
  890. /* can't go backwards */
  891. if (iter->idx > i)
  892. return NULL;
  893. if (iter->idx < 0)
  894. ent = find_next_entry_inc(iter);
  895. else
  896. ent = iter;
  897. while (ent && iter->idx < i)
  898. ent = find_next_entry_inc(iter);
  899. iter->pos = *pos;
  900. if (last_ent && !ent)
  901. seq_puts(m, "\n\nvim:ft=help\n");
  902. return ent;
  903. }
  904. static void *s_start(struct seq_file *m, loff_t *pos)
  905. {
  906. struct trace_iterator *iter = m->private;
  907. void *p = NULL;
  908. loff_t l = 0;
  909. int i;
  910. mutex_lock(&trace_types_lock);
  911. if (!current_trace || current_trace != iter->trace) {
  912. mutex_unlock(&trace_types_lock);
  913. return NULL;
  914. }
  915. atomic_inc(&trace_record_cmdline_disabled);
  916. /* let the tracer grab locks here if needed */
  917. if (current_trace->start)
  918. current_trace->start(iter);
  919. if (*pos != iter->pos) {
  920. iter->ent = NULL;
  921. iter->cpu = 0;
  922. iter->idx = -1;
  923. iter->prev_ent = NULL;
  924. iter->prev_cpu = -1;
  925. for_each_tracing_cpu(i) {
  926. iter->next_idx[i] = 0;
  927. iter->next_page[i] = NULL;
  928. }
  929. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  930. ;
  931. } else {
  932. l = *pos - 1;
  933. p = s_next(m, p, &l);
  934. }
  935. return p;
  936. }
  937. static void s_stop(struct seq_file *m, void *p)
  938. {
  939. struct trace_iterator *iter = m->private;
  940. atomic_dec(&trace_record_cmdline_disabled);
  941. /* let the tracer release locks here if needed */
  942. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  943. iter->trace->stop(iter);
  944. mutex_unlock(&trace_types_lock);
  945. }
  946. static int
  947. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  948. {
  949. #ifdef CONFIG_KALLSYMS
  950. char str[KSYM_SYMBOL_LEN];
  951. kallsyms_lookup(address, NULL, NULL, NULL, str);
  952. return trace_seq_printf(s, fmt, str);
  953. #endif
  954. return 1;
  955. }
  956. static int
  957. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  958. unsigned long address)
  959. {
  960. #ifdef CONFIG_KALLSYMS
  961. char str[KSYM_SYMBOL_LEN];
  962. sprint_symbol(str, address);
  963. return trace_seq_printf(s, fmt, str);
  964. #endif
  965. return 1;
  966. }
  967. #ifndef CONFIG_64BIT
  968. # define IP_FMT "%08lx"
  969. #else
  970. # define IP_FMT "%016lx"
  971. #endif
  972. static int
  973. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  974. {
  975. int ret;
  976. if (!ip)
  977. return trace_seq_printf(s, "0");
  978. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  979. ret = seq_print_sym_offset(s, "%s", ip);
  980. else
  981. ret = seq_print_sym_short(s, "%s", ip);
  982. if (!ret)
  983. return 0;
  984. if (sym_flags & TRACE_ITER_SYM_ADDR)
  985. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  986. return ret;
  987. }
  988. static void print_lat_help_header(struct seq_file *m)
  989. {
  990. seq_puts(m, "# _------=> CPU# \n");
  991. seq_puts(m, "# / _-----=> irqs-off \n");
  992. seq_puts(m, "# | / _----=> need-resched \n");
  993. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  994. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  995. seq_puts(m, "# |||| / \n");
  996. seq_puts(m, "# ||||| delay \n");
  997. seq_puts(m, "# cmd pid ||||| time | caller \n");
  998. seq_puts(m, "# \\ / ||||| \\ | / \n");
  999. }
  1000. static void print_func_help_header(struct seq_file *m)
  1001. {
  1002. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1003. seq_puts(m, "# | | | | |\n");
  1004. }
  1005. static void
  1006. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1007. {
  1008. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1009. struct trace_array *tr = iter->tr;
  1010. struct trace_array_cpu *data = tr->data[tr->cpu];
  1011. struct tracer *type = current_trace;
  1012. unsigned long total = 0;
  1013. unsigned long entries = 0;
  1014. int cpu;
  1015. const char *name = "preemption";
  1016. if (type)
  1017. name = type->name;
  1018. for_each_tracing_cpu(cpu) {
  1019. if (head_page(tr->data[cpu])) {
  1020. total += tr->data[cpu]->trace_idx;
  1021. if (tr->data[cpu]->trace_idx > tr->entries)
  1022. entries += tr->entries;
  1023. else
  1024. entries += tr->data[cpu]->trace_idx;
  1025. }
  1026. }
  1027. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1028. name, UTS_RELEASE);
  1029. seq_puts(m, "-----------------------------------"
  1030. "---------------------------------\n");
  1031. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1032. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1033. nsecs_to_usecs(data->saved_latency),
  1034. entries,
  1035. total,
  1036. tr->cpu,
  1037. #if defined(CONFIG_PREEMPT_NONE)
  1038. "server",
  1039. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1040. "desktop",
  1041. #elif defined(CONFIG_PREEMPT_DESKTOP)
  1042. "preempt",
  1043. #else
  1044. "unknown",
  1045. #endif
  1046. /* These are reserved for later use */
  1047. 0, 0, 0, 0);
  1048. #ifdef CONFIG_SMP
  1049. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1050. #else
  1051. seq_puts(m, ")\n");
  1052. #endif
  1053. seq_puts(m, " -----------------\n");
  1054. seq_printf(m, " | task: %.16s-%d "
  1055. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1056. data->comm, data->pid, data->uid, data->nice,
  1057. data->policy, data->rt_priority);
  1058. seq_puts(m, " -----------------\n");
  1059. if (data->critical_start) {
  1060. seq_puts(m, " => started at: ");
  1061. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1062. trace_print_seq(m, &iter->seq);
  1063. seq_puts(m, "\n => ended at: ");
  1064. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1065. trace_print_seq(m, &iter->seq);
  1066. seq_puts(m, "\n");
  1067. }
  1068. seq_puts(m, "\n");
  1069. }
  1070. static void
  1071. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1072. {
  1073. int hardirq, softirq;
  1074. char *comm;
  1075. comm = trace_find_cmdline(entry->pid);
  1076. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  1077. trace_seq_printf(s, "%d", cpu);
  1078. trace_seq_printf(s, "%c%c",
  1079. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  1080. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1081. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  1082. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  1083. if (hardirq && softirq) {
  1084. trace_seq_putc(s, 'H');
  1085. } else {
  1086. if (hardirq) {
  1087. trace_seq_putc(s, 'h');
  1088. } else {
  1089. if (softirq)
  1090. trace_seq_putc(s, 's');
  1091. else
  1092. trace_seq_putc(s, '.');
  1093. }
  1094. }
  1095. if (entry->preempt_count)
  1096. trace_seq_printf(s, "%x", entry->preempt_count);
  1097. else
  1098. trace_seq_puts(s, ".");
  1099. }
  1100. unsigned long preempt_mark_thresh = 100;
  1101. static void
  1102. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  1103. unsigned long rel_usecs)
  1104. {
  1105. trace_seq_printf(s, " %4lldus", abs_usecs);
  1106. if (rel_usecs > preempt_mark_thresh)
  1107. trace_seq_puts(s, "!: ");
  1108. else if (rel_usecs > 1)
  1109. trace_seq_puts(s, "+: ");
  1110. else
  1111. trace_seq_puts(s, " : ");
  1112. }
  1113. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1114. static int
  1115. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1116. {
  1117. struct trace_seq *s = &iter->seq;
  1118. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1119. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  1120. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1121. struct trace_entry *entry = iter->ent;
  1122. unsigned long abs_usecs;
  1123. unsigned long rel_usecs;
  1124. char *comm;
  1125. int S, T;
  1126. int i;
  1127. unsigned state;
  1128. if (!next_entry)
  1129. next_entry = entry;
  1130. rel_usecs = ns2usecs(next_entry->t - entry->t);
  1131. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  1132. if (verbose) {
  1133. comm = trace_find_cmdline(entry->pid);
  1134. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  1135. " %ld.%03ldms (+%ld.%03ldms): ",
  1136. comm,
  1137. entry->pid, cpu, entry->flags,
  1138. entry->preempt_count, trace_idx,
  1139. ns2usecs(entry->t),
  1140. abs_usecs/1000,
  1141. abs_usecs % 1000, rel_usecs/1000,
  1142. rel_usecs % 1000);
  1143. } else {
  1144. lat_print_generic(s, entry, cpu);
  1145. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1146. }
  1147. switch (entry->type) {
  1148. case TRACE_FN:
  1149. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1150. trace_seq_puts(s, " (");
  1151. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  1152. trace_seq_puts(s, ")\n");
  1153. break;
  1154. case TRACE_CTX:
  1155. case TRACE_WAKE:
  1156. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1157. state_to_char[entry->ctx.next_state] : 'X';
  1158. state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
  1159. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1160. comm = trace_find_cmdline(entry->ctx.next_pid);
  1161. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1162. entry->ctx.prev_pid,
  1163. entry->ctx.prev_prio,
  1164. S, entry->type == TRACE_CTX ? "==>" : " +",
  1165. entry->ctx.next_pid,
  1166. entry->ctx.next_prio,
  1167. T, comm);
  1168. break;
  1169. case TRACE_SPECIAL:
  1170. trace_seq_printf(s, "# %ld %ld %ld\n",
  1171. entry->special.arg1,
  1172. entry->special.arg2,
  1173. entry->special.arg3);
  1174. break;
  1175. case TRACE_STACK:
  1176. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1177. if (i)
  1178. trace_seq_puts(s, " <= ");
  1179. seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
  1180. }
  1181. trace_seq_puts(s, "\n");
  1182. break;
  1183. default:
  1184. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1185. }
  1186. return 1;
  1187. }
  1188. static int print_trace_fmt(struct trace_iterator *iter)
  1189. {
  1190. struct trace_seq *s = &iter->seq;
  1191. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1192. struct trace_entry *entry;
  1193. unsigned long usec_rem;
  1194. unsigned long long t;
  1195. unsigned long secs;
  1196. char *comm;
  1197. int ret;
  1198. int S, T;
  1199. int i;
  1200. entry = iter->ent;
  1201. comm = trace_find_cmdline(iter->ent->pid);
  1202. t = ns2usecs(entry->t);
  1203. usec_rem = do_div(t, 1000000ULL);
  1204. secs = (unsigned long)t;
  1205. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1206. if (!ret)
  1207. return 0;
  1208. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1209. if (!ret)
  1210. return 0;
  1211. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1212. if (!ret)
  1213. return 0;
  1214. switch (entry->type) {
  1215. case TRACE_FN:
  1216. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1217. if (!ret)
  1218. return 0;
  1219. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1220. entry->fn.parent_ip) {
  1221. ret = trace_seq_printf(s, " <-");
  1222. if (!ret)
  1223. return 0;
  1224. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1225. sym_flags);
  1226. if (!ret)
  1227. return 0;
  1228. }
  1229. ret = trace_seq_printf(s, "\n");
  1230. if (!ret)
  1231. return 0;
  1232. break;
  1233. case TRACE_CTX:
  1234. case TRACE_WAKE:
  1235. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1236. state_to_char[entry->ctx.prev_state] : 'X';
  1237. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1238. state_to_char[entry->ctx.next_state] : 'X';
  1239. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1240. entry->ctx.prev_pid,
  1241. entry->ctx.prev_prio,
  1242. S,
  1243. entry->type == TRACE_CTX ? "==>" : " +",
  1244. entry->ctx.next_pid,
  1245. entry->ctx.next_prio,
  1246. T);
  1247. if (!ret)
  1248. return 0;
  1249. break;
  1250. case TRACE_SPECIAL:
  1251. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1252. entry->special.arg1,
  1253. entry->special.arg2,
  1254. entry->special.arg3);
  1255. if (!ret)
  1256. return 0;
  1257. break;
  1258. case TRACE_STACK:
  1259. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1260. if (i) {
  1261. ret = trace_seq_puts(s, " <= ");
  1262. if (!ret)
  1263. return 0;
  1264. }
  1265. ret = seq_print_ip_sym(s, entry->stack.caller[i],
  1266. sym_flags);
  1267. if (!ret)
  1268. return 0;
  1269. }
  1270. ret = trace_seq_puts(s, "\n");
  1271. if (!ret)
  1272. return 0;
  1273. break;
  1274. }
  1275. return 1;
  1276. }
  1277. static int print_raw_fmt(struct trace_iterator *iter)
  1278. {
  1279. struct trace_seq *s = &iter->seq;
  1280. struct trace_entry *entry;
  1281. int ret;
  1282. int S, T;
  1283. entry = iter->ent;
  1284. ret = trace_seq_printf(s, "%d %d %llu ",
  1285. entry->pid, iter->cpu, entry->t);
  1286. if (!ret)
  1287. return 0;
  1288. switch (entry->type) {
  1289. case TRACE_FN:
  1290. ret = trace_seq_printf(s, "%x %x\n",
  1291. entry->fn.ip, entry->fn.parent_ip);
  1292. if (!ret)
  1293. return 0;
  1294. break;
  1295. case TRACE_CTX:
  1296. case TRACE_WAKE:
  1297. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1298. state_to_char[entry->ctx.prev_state] : 'X';
  1299. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1300. state_to_char[entry->ctx.next_state] : 'X';
  1301. if (entry->type == TRACE_WAKE)
  1302. S = '+';
  1303. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1304. entry->ctx.prev_pid,
  1305. entry->ctx.prev_prio,
  1306. S,
  1307. entry->ctx.next_pid,
  1308. entry->ctx.next_prio,
  1309. T);
  1310. if (!ret)
  1311. return 0;
  1312. break;
  1313. case TRACE_SPECIAL:
  1314. case TRACE_STACK:
  1315. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1316. entry->special.arg1,
  1317. entry->special.arg2,
  1318. entry->special.arg3);
  1319. if (!ret)
  1320. return 0;
  1321. break;
  1322. }
  1323. return 1;
  1324. }
  1325. #define SEQ_PUT_FIELD_RET(s, x) \
  1326. do { \
  1327. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1328. return 0; \
  1329. } while (0)
  1330. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1331. do { \
  1332. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1333. return 0; \
  1334. } while (0)
  1335. static int print_hex_fmt(struct trace_iterator *iter)
  1336. {
  1337. struct trace_seq *s = &iter->seq;
  1338. unsigned char newline = '\n';
  1339. struct trace_entry *entry;
  1340. int S, T;
  1341. entry = iter->ent;
  1342. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1343. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1344. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1345. switch (entry->type) {
  1346. case TRACE_FN:
  1347. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1348. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1349. break;
  1350. case TRACE_CTX:
  1351. case TRACE_WAKE:
  1352. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1353. state_to_char[entry->ctx.prev_state] : 'X';
  1354. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1355. state_to_char[entry->ctx.next_state] : 'X';
  1356. if (entry->type == TRACE_WAKE)
  1357. S = '+';
  1358. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1359. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1360. SEQ_PUT_HEX_FIELD_RET(s, S);
  1361. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1362. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1363. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1364. SEQ_PUT_HEX_FIELD_RET(s, T);
  1365. break;
  1366. case TRACE_SPECIAL:
  1367. case TRACE_STACK:
  1368. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1369. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1370. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1371. break;
  1372. }
  1373. SEQ_PUT_FIELD_RET(s, newline);
  1374. return 1;
  1375. }
  1376. static int print_bin_fmt(struct trace_iterator *iter)
  1377. {
  1378. struct trace_seq *s = &iter->seq;
  1379. struct trace_entry *entry;
  1380. entry = iter->ent;
  1381. SEQ_PUT_FIELD_RET(s, entry->pid);
  1382. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1383. SEQ_PUT_FIELD_RET(s, entry->t);
  1384. switch (entry->type) {
  1385. case TRACE_FN:
  1386. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1387. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1388. break;
  1389. case TRACE_CTX:
  1390. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1391. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1392. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1393. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1394. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1395. SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
  1396. break;
  1397. case TRACE_SPECIAL:
  1398. case TRACE_STACK:
  1399. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1400. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1401. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1402. break;
  1403. }
  1404. return 1;
  1405. }
  1406. static int trace_empty(struct trace_iterator *iter)
  1407. {
  1408. struct trace_array_cpu *data;
  1409. int cpu;
  1410. for_each_tracing_cpu(cpu) {
  1411. data = iter->tr->data[cpu];
  1412. if (head_page(data) && data->trace_idx &&
  1413. (data->trace_tail != data->trace_head ||
  1414. data->trace_tail_idx != data->trace_head_idx))
  1415. return 0;
  1416. }
  1417. return 1;
  1418. }
  1419. static int print_trace_line(struct trace_iterator *iter)
  1420. {
  1421. if (iter->trace && iter->trace->print_line)
  1422. return iter->trace->print_line(iter);
  1423. if (trace_flags & TRACE_ITER_BIN)
  1424. return print_bin_fmt(iter);
  1425. if (trace_flags & TRACE_ITER_HEX)
  1426. return print_hex_fmt(iter);
  1427. if (trace_flags & TRACE_ITER_RAW)
  1428. return print_raw_fmt(iter);
  1429. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1430. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1431. return print_trace_fmt(iter);
  1432. }
  1433. static int s_show(struct seq_file *m, void *v)
  1434. {
  1435. struct trace_iterator *iter = v;
  1436. if (iter->ent == NULL) {
  1437. if (iter->tr) {
  1438. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1439. seq_puts(m, "#\n");
  1440. }
  1441. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1442. /* print nothing if the buffers are empty */
  1443. if (trace_empty(iter))
  1444. return 0;
  1445. print_trace_header(m, iter);
  1446. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1447. print_lat_help_header(m);
  1448. } else {
  1449. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1450. print_func_help_header(m);
  1451. }
  1452. } else {
  1453. print_trace_line(iter);
  1454. trace_print_seq(m, &iter->seq);
  1455. }
  1456. return 0;
  1457. }
  1458. static struct seq_operations tracer_seq_ops = {
  1459. .start = s_start,
  1460. .next = s_next,
  1461. .stop = s_stop,
  1462. .show = s_show,
  1463. };
  1464. static struct trace_iterator *
  1465. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1466. {
  1467. struct trace_iterator *iter;
  1468. if (tracing_disabled) {
  1469. *ret = -ENODEV;
  1470. return NULL;
  1471. }
  1472. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1473. if (!iter) {
  1474. *ret = -ENOMEM;
  1475. goto out;
  1476. }
  1477. mutex_lock(&trace_types_lock);
  1478. if (current_trace && current_trace->print_max)
  1479. iter->tr = &max_tr;
  1480. else
  1481. iter->tr = inode->i_private;
  1482. iter->trace = current_trace;
  1483. iter->pos = -1;
  1484. /* TODO stop tracer */
  1485. *ret = seq_open(file, &tracer_seq_ops);
  1486. if (!*ret) {
  1487. struct seq_file *m = file->private_data;
  1488. m->private = iter;
  1489. /* stop the trace while dumping */
  1490. if (iter->tr->ctrl)
  1491. tracer_enabled = 0;
  1492. if (iter->trace && iter->trace->open)
  1493. iter->trace->open(iter);
  1494. } else {
  1495. kfree(iter);
  1496. iter = NULL;
  1497. }
  1498. mutex_unlock(&trace_types_lock);
  1499. out:
  1500. return iter;
  1501. }
  1502. int tracing_open_generic(struct inode *inode, struct file *filp)
  1503. {
  1504. if (tracing_disabled)
  1505. return -ENODEV;
  1506. filp->private_data = inode->i_private;
  1507. return 0;
  1508. }
  1509. int tracing_release(struct inode *inode, struct file *file)
  1510. {
  1511. struct seq_file *m = (struct seq_file *)file->private_data;
  1512. struct trace_iterator *iter = m->private;
  1513. mutex_lock(&trace_types_lock);
  1514. if (iter->trace && iter->trace->close)
  1515. iter->trace->close(iter);
  1516. /* reenable tracing if it was previously enabled */
  1517. if (iter->tr->ctrl)
  1518. tracer_enabled = 1;
  1519. mutex_unlock(&trace_types_lock);
  1520. seq_release(inode, file);
  1521. kfree(iter);
  1522. return 0;
  1523. }
  1524. static int tracing_open(struct inode *inode, struct file *file)
  1525. {
  1526. int ret;
  1527. __tracing_open(inode, file, &ret);
  1528. return ret;
  1529. }
  1530. static int tracing_lt_open(struct inode *inode, struct file *file)
  1531. {
  1532. struct trace_iterator *iter;
  1533. int ret;
  1534. iter = __tracing_open(inode, file, &ret);
  1535. if (!ret)
  1536. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1537. return ret;
  1538. }
  1539. static void *
  1540. t_next(struct seq_file *m, void *v, loff_t *pos)
  1541. {
  1542. struct tracer *t = m->private;
  1543. (*pos)++;
  1544. if (t)
  1545. t = t->next;
  1546. m->private = t;
  1547. return t;
  1548. }
  1549. static void *t_start(struct seq_file *m, loff_t *pos)
  1550. {
  1551. struct tracer *t = m->private;
  1552. loff_t l = 0;
  1553. mutex_lock(&trace_types_lock);
  1554. for (; t && l < *pos; t = t_next(m, t, &l))
  1555. ;
  1556. return t;
  1557. }
  1558. static void t_stop(struct seq_file *m, void *p)
  1559. {
  1560. mutex_unlock(&trace_types_lock);
  1561. }
  1562. static int t_show(struct seq_file *m, void *v)
  1563. {
  1564. struct tracer *t = v;
  1565. if (!t)
  1566. return 0;
  1567. seq_printf(m, "%s", t->name);
  1568. if (t->next)
  1569. seq_putc(m, ' ');
  1570. else
  1571. seq_putc(m, '\n');
  1572. return 0;
  1573. }
  1574. static struct seq_operations show_traces_seq_ops = {
  1575. .start = t_start,
  1576. .next = t_next,
  1577. .stop = t_stop,
  1578. .show = t_show,
  1579. };
  1580. static int show_traces_open(struct inode *inode, struct file *file)
  1581. {
  1582. int ret;
  1583. if (tracing_disabled)
  1584. return -ENODEV;
  1585. ret = seq_open(file, &show_traces_seq_ops);
  1586. if (!ret) {
  1587. struct seq_file *m = file->private_data;
  1588. m->private = trace_types;
  1589. }
  1590. return ret;
  1591. }
  1592. static struct file_operations tracing_fops = {
  1593. .open = tracing_open,
  1594. .read = seq_read,
  1595. .llseek = seq_lseek,
  1596. .release = tracing_release,
  1597. };
  1598. static struct file_operations tracing_lt_fops = {
  1599. .open = tracing_lt_open,
  1600. .read = seq_read,
  1601. .llseek = seq_lseek,
  1602. .release = tracing_release,
  1603. };
  1604. static struct file_operations show_traces_fops = {
  1605. .open = show_traces_open,
  1606. .read = seq_read,
  1607. .release = seq_release,
  1608. };
  1609. /*
  1610. * Only trace on a CPU if the bitmask is set:
  1611. */
  1612. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1613. /*
  1614. * When tracing/tracing_cpu_mask is modified then this holds
  1615. * the new bitmask we are about to install:
  1616. */
  1617. static cpumask_t tracing_cpumask_new;
  1618. /*
  1619. * The tracer itself will not take this lock, but still we want
  1620. * to provide a consistent cpumask to user-space:
  1621. */
  1622. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1623. /*
  1624. * Temporary storage for the character representation of the
  1625. * CPU bitmask (and one more byte for the newline):
  1626. */
  1627. static char mask_str[NR_CPUS + 1];
  1628. static ssize_t
  1629. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1630. size_t count, loff_t *ppos)
  1631. {
  1632. int len;
  1633. mutex_lock(&tracing_cpumask_update_lock);
  1634. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1635. if (count - len < 2) {
  1636. count = -EINVAL;
  1637. goto out_err;
  1638. }
  1639. len += sprintf(mask_str + len, "\n");
  1640. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1641. out_err:
  1642. mutex_unlock(&tracing_cpumask_update_lock);
  1643. return count;
  1644. }
  1645. static ssize_t
  1646. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1647. size_t count, loff_t *ppos)
  1648. {
  1649. int err, cpu;
  1650. mutex_lock(&tracing_cpumask_update_lock);
  1651. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1652. if (err)
  1653. goto err_unlock;
  1654. raw_local_irq_disable();
  1655. __raw_spin_lock(&ftrace_max_lock);
  1656. for_each_tracing_cpu(cpu) {
  1657. /*
  1658. * Increase/decrease the disabled counter if we are
  1659. * about to flip a bit in the cpumask:
  1660. */
  1661. if (cpu_isset(cpu, tracing_cpumask) &&
  1662. !cpu_isset(cpu, tracing_cpumask_new)) {
  1663. atomic_inc(&global_trace.data[cpu]->disabled);
  1664. }
  1665. if (!cpu_isset(cpu, tracing_cpumask) &&
  1666. cpu_isset(cpu, tracing_cpumask_new)) {
  1667. atomic_dec(&global_trace.data[cpu]->disabled);
  1668. }
  1669. }
  1670. __raw_spin_unlock(&ftrace_max_lock);
  1671. raw_local_irq_enable();
  1672. tracing_cpumask = tracing_cpumask_new;
  1673. mutex_unlock(&tracing_cpumask_update_lock);
  1674. return count;
  1675. err_unlock:
  1676. mutex_unlock(&tracing_cpumask_update_lock);
  1677. return err;
  1678. }
  1679. static struct file_operations tracing_cpumask_fops = {
  1680. .open = tracing_open_generic,
  1681. .read = tracing_cpumask_read,
  1682. .write = tracing_cpumask_write,
  1683. };
  1684. static ssize_t
  1685. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1686. size_t cnt, loff_t *ppos)
  1687. {
  1688. char *buf;
  1689. int r = 0;
  1690. int len = 0;
  1691. int i;
  1692. /* calulate max size */
  1693. for (i = 0; trace_options[i]; i++) {
  1694. len += strlen(trace_options[i]);
  1695. len += 3; /* "no" and space */
  1696. }
  1697. /* +2 for \n and \0 */
  1698. buf = kmalloc(len + 2, GFP_KERNEL);
  1699. if (!buf)
  1700. return -ENOMEM;
  1701. for (i = 0; trace_options[i]; i++) {
  1702. if (trace_flags & (1 << i))
  1703. r += sprintf(buf + r, "%s ", trace_options[i]);
  1704. else
  1705. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1706. }
  1707. r += sprintf(buf + r, "\n");
  1708. WARN_ON(r >= len + 2);
  1709. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1710. kfree(buf);
  1711. return r;
  1712. }
  1713. static ssize_t
  1714. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1715. size_t cnt, loff_t *ppos)
  1716. {
  1717. char buf[64];
  1718. char *cmp = buf;
  1719. int neg = 0;
  1720. int i;
  1721. if (cnt >= sizeof(buf))
  1722. return -EINVAL;
  1723. if (copy_from_user(&buf, ubuf, cnt))
  1724. return -EFAULT;
  1725. buf[cnt] = 0;
  1726. if (strncmp(buf, "no", 2) == 0) {
  1727. neg = 1;
  1728. cmp += 2;
  1729. }
  1730. for (i = 0; trace_options[i]; i++) {
  1731. int len = strlen(trace_options[i]);
  1732. if (strncmp(cmp, trace_options[i], len) == 0) {
  1733. if (neg)
  1734. trace_flags &= ~(1 << i);
  1735. else
  1736. trace_flags |= (1 << i);
  1737. break;
  1738. }
  1739. }
  1740. /*
  1741. * If no option could be set, return an error:
  1742. */
  1743. if (!trace_options[i])
  1744. return -EINVAL;
  1745. filp->f_pos += cnt;
  1746. return cnt;
  1747. }
  1748. static struct file_operations tracing_iter_fops = {
  1749. .open = tracing_open_generic,
  1750. .read = tracing_iter_ctrl_read,
  1751. .write = tracing_iter_ctrl_write,
  1752. };
  1753. static const char readme_msg[] =
  1754. "tracing mini-HOWTO:\n\n"
  1755. "# mkdir /debug\n"
  1756. "# mount -t debugfs nodev /debug\n\n"
  1757. "# cat /debug/tracing/available_tracers\n"
  1758. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1759. "# cat /debug/tracing/current_tracer\n"
  1760. "none\n"
  1761. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1762. "# cat /debug/tracing/current_tracer\n"
  1763. "sched_switch\n"
  1764. "# cat /debug/tracing/iter_ctrl\n"
  1765. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1766. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1767. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1768. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1769. "echo 0 > /debug/tracing/tracing_enabled\n"
  1770. ;
  1771. static ssize_t
  1772. tracing_readme_read(struct file *filp, char __user *ubuf,
  1773. size_t cnt, loff_t *ppos)
  1774. {
  1775. return simple_read_from_buffer(ubuf, cnt, ppos,
  1776. readme_msg, strlen(readme_msg));
  1777. }
  1778. static struct file_operations tracing_readme_fops = {
  1779. .open = tracing_open_generic,
  1780. .read = tracing_readme_read,
  1781. };
  1782. static ssize_t
  1783. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1784. size_t cnt, loff_t *ppos)
  1785. {
  1786. struct trace_array *tr = filp->private_data;
  1787. char buf[64];
  1788. int r;
  1789. r = sprintf(buf, "%ld\n", tr->ctrl);
  1790. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1791. }
  1792. static ssize_t
  1793. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1794. size_t cnt, loff_t *ppos)
  1795. {
  1796. struct trace_array *tr = filp->private_data;
  1797. char buf[64];
  1798. long val;
  1799. int ret;
  1800. if (cnt >= sizeof(buf))
  1801. return -EINVAL;
  1802. if (copy_from_user(&buf, ubuf, cnt))
  1803. return -EFAULT;
  1804. buf[cnt] = 0;
  1805. ret = strict_strtoul(buf, 10, &val);
  1806. if (ret < 0)
  1807. return ret;
  1808. val = !!val;
  1809. mutex_lock(&trace_types_lock);
  1810. if (tr->ctrl ^ val) {
  1811. if (val)
  1812. tracer_enabled = 1;
  1813. else
  1814. tracer_enabled = 0;
  1815. tr->ctrl = val;
  1816. if (current_trace && current_trace->ctrl_update)
  1817. current_trace->ctrl_update(tr);
  1818. }
  1819. mutex_unlock(&trace_types_lock);
  1820. filp->f_pos += cnt;
  1821. return cnt;
  1822. }
  1823. static ssize_t
  1824. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1825. size_t cnt, loff_t *ppos)
  1826. {
  1827. char buf[max_tracer_type_len+2];
  1828. int r;
  1829. mutex_lock(&trace_types_lock);
  1830. if (current_trace)
  1831. r = sprintf(buf, "%s\n", current_trace->name);
  1832. else
  1833. r = sprintf(buf, "\n");
  1834. mutex_unlock(&trace_types_lock);
  1835. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1836. }
  1837. static ssize_t
  1838. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1839. size_t cnt, loff_t *ppos)
  1840. {
  1841. struct trace_array *tr = &global_trace;
  1842. struct tracer *t;
  1843. char buf[max_tracer_type_len+1];
  1844. int i;
  1845. if (cnt > max_tracer_type_len)
  1846. cnt = max_tracer_type_len;
  1847. if (copy_from_user(&buf, ubuf, cnt))
  1848. return -EFAULT;
  1849. buf[cnt] = 0;
  1850. /* strip ending whitespace. */
  1851. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1852. buf[i] = 0;
  1853. mutex_lock(&trace_types_lock);
  1854. for (t = trace_types; t; t = t->next) {
  1855. if (strcmp(t->name, buf) == 0)
  1856. break;
  1857. }
  1858. if (!t || t == current_trace)
  1859. goto out;
  1860. if (current_trace && current_trace->reset)
  1861. current_trace->reset(tr);
  1862. current_trace = t;
  1863. if (t->init)
  1864. t->init(tr);
  1865. out:
  1866. mutex_unlock(&trace_types_lock);
  1867. filp->f_pos += cnt;
  1868. return cnt;
  1869. }
  1870. static ssize_t
  1871. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1872. size_t cnt, loff_t *ppos)
  1873. {
  1874. unsigned long *ptr = filp->private_data;
  1875. char buf[64];
  1876. int r;
  1877. r = snprintf(buf, sizeof(buf), "%ld\n",
  1878. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1879. if (r > sizeof(buf))
  1880. r = sizeof(buf);
  1881. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1882. }
  1883. static ssize_t
  1884. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1885. size_t cnt, loff_t *ppos)
  1886. {
  1887. long *ptr = filp->private_data;
  1888. char buf[64];
  1889. long val;
  1890. int ret;
  1891. if (cnt >= sizeof(buf))
  1892. return -EINVAL;
  1893. if (copy_from_user(&buf, ubuf, cnt))
  1894. return -EFAULT;
  1895. buf[cnt] = 0;
  1896. ret = strict_strtoul(buf, 10, &val);
  1897. if (ret < 0)
  1898. return ret;
  1899. *ptr = val * 1000;
  1900. return cnt;
  1901. }
  1902. static atomic_t tracing_reader;
  1903. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1904. {
  1905. struct trace_iterator *iter;
  1906. if (tracing_disabled)
  1907. return -ENODEV;
  1908. /* We only allow for reader of the pipe */
  1909. if (atomic_inc_return(&tracing_reader) != 1) {
  1910. atomic_dec(&tracing_reader);
  1911. return -EBUSY;
  1912. }
  1913. /* create a buffer to store the information to pass to userspace */
  1914. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1915. if (!iter)
  1916. return -ENOMEM;
  1917. iter->tr = &global_trace;
  1918. iter->trace = current_trace;
  1919. filp->private_data = iter;
  1920. return 0;
  1921. }
  1922. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1923. {
  1924. struct trace_iterator *iter = file->private_data;
  1925. kfree(iter);
  1926. atomic_dec(&tracing_reader);
  1927. return 0;
  1928. }
  1929. static unsigned int
  1930. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1931. {
  1932. struct trace_iterator *iter = filp->private_data;
  1933. if (trace_flags & TRACE_ITER_BLOCK) {
  1934. /*
  1935. * Always select as readable when in blocking mode
  1936. */
  1937. return POLLIN | POLLRDNORM;
  1938. } else {
  1939. if (!trace_empty(iter))
  1940. return POLLIN | POLLRDNORM;
  1941. poll_wait(filp, &trace_wait, poll_table);
  1942. if (!trace_empty(iter))
  1943. return POLLIN | POLLRDNORM;
  1944. return 0;
  1945. }
  1946. }
  1947. /*
  1948. * Consumer reader.
  1949. */
  1950. static ssize_t
  1951. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1952. size_t cnt, loff_t *ppos)
  1953. {
  1954. struct trace_iterator *iter = filp->private_data;
  1955. struct trace_array_cpu *data;
  1956. struct trace_array *tr = iter->tr;
  1957. struct tracer *tracer = iter->trace;
  1958. static cpumask_t mask;
  1959. static int start;
  1960. unsigned long flags;
  1961. #ifdef CONFIG_FTRACE
  1962. int ftrace_save;
  1963. #endif
  1964. int read = 0;
  1965. int cpu;
  1966. int len;
  1967. int ret;
  1968. /* return any leftover data */
  1969. if (iter->seq.len > start) {
  1970. len = iter->seq.len - start;
  1971. if (cnt > len)
  1972. cnt = len;
  1973. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1974. if (ret)
  1975. cnt = -EFAULT;
  1976. start += len;
  1977. return cnt;
  1978. }
  1979. trace_seq_reset(&iter->seq);
  1980. start = 0;
  1981. while (trace_empty(iter)) {
  1982. if ((filp->f_flags & O_NONBLOCK))
  1983. return -EAGAIN;
  1984. /*
  1985. * This is a make-shift waitqueue. The reason we don't use
  1986. * an actual wait queue is because:
  1987. * 1) we only ever have one waiter
  1988. * 2) the tracing, traces all functions, we don't want
  1989. * the overhead of calling wake_up and friends
  1990. * (and tracing them too)
  1991. * Anyway, this is really very primitive wakeup.
  1992. */
  1993. set_current_state(TASK_INTERRUPTIBLE);
  1994. iter->tr->waiter = current;
  1995. /* sleep for one second, and try again. */
  1996. schedule_timeout(HZ);
  1997. iter->tr->waiter = NULL;
  1998. if (signal_pending(current))
  1999. return -EINTR;
  2000. if (iter->trace != current_trace)
  2001. return 0;
  2002. /*
  2003. * We block until we read something and tracing is disabled.
  2004. * We still block if tracing is disabled, but we have never
  2005. * read anything. This allows a user to cat this file, and
  2006. * then enable tracing. But after we have read something,
  2007. * we give an EOF when tracing is again disabled.
  2008. *
  2009. * iter->pos will be 0 if we haven't read anything.
  2010. */
  2011. if (!tracer_enabled && iter->pos)
  2012. break;
  2013. continue;
  2014. }
  2015. /* stop when tracing is finished */
  2016. if (trace_empty(iter))
  2017. return 0;
  2018. if (cnt >= PAGE_SIZE)
  2019. cnt = PAGE_SIZE - 1;
  2020. memset(iter, 0, sizeof(*iter));
  2021. iter->tr = tr;
  2022. iter->trace = tracer;
  2023. iter->pos = -1;
  2024. /*
  2025. * We need to stop all tracing on all CPUS to read the
  2026. * the next buffer. This is a bit expensive, but is
  2027. * not done often. We fill all what we can read,
  2028. * and then release the locks again.
  2029. */
  2030. cpus_clear(mask);
  2031. local_irq_save(flags);
  2032. #ifdef CONFIG_FTRACE
  2033. ftrace_save = ftrace_enabled;
  2034. ftrace_enabled = 0;
  2035. #endif
  2036. smp_wmb();
  2037. for_each_tracing_cpu(cpu) {
  2038. data = iter->tr->data[cpu];
  2039. if (!head_page(data) || !data->trace_idx)
  2040. continue;
  2041. atomic_inc(&data->disabled);
  2042. cpu_set(cpu, mask);
  2043. }
  2044. for_each_cpu_mask(cpu, mask) {
  2045. data = iter->tr->data[cpu];
  2046. __raw_spin_lock(&data->lock);
  2047. }
  2048. while (find_next_entry_inc(iter) != NULL) {
  2049. int len = iter->seq.len;
  2050. ret = print_trace_line(iter);
  2051. if (!ret) {
  2052. /* don't print partial lines */
  2053. iter->seq.len = len;
  2054. break;
  2055. }
  2056. trace_consume(iter);
  2057. if (iter->seq.len >= cnt)
  2058. break;
  2059. }
  2060. for_each_cpu_mask(cpu, mask) {
  2061. data = iter->tr->data[cpu];
  2062. __raw_spin_unlock(&data->lock);
  2063. }
  2064. for_each_cpu_mask(cpu, mask) {
  2065. data = iter->tr->data[cpu];
  2066. atomic_dec(&data->disabled);
  2067. }
  2068. #ifdef CONFIG_FTRACE
  2069. ftrace_enabled = ftrace_save;
  2070. #endif
  2071. local_irq_restore(flags);
  2072. /* Now copy what we have to the user */
  2073. read = iter->seq.len;
  2074. if (read > cnt)
  2075. read = cnt;
  2076. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  2077. if (read < iter->seq.len)
  2078. start = read;
  2079. else
  2080. trace_seq_reset(&iter->seq);
  2081. if (ret)
  2082. read = -EFAULT;
  2083. return read;
  2084. }
  2085. static ssize_t
  2086. tracing_entries_read(struct file *filp, char __user *ubuf,
  2087. size_t cnt, loff_t *ppos)
  2088. {
  2089. struct trace_array *tr = filp->private_data;
  2090. char buf[64];
  2091. int r;
  2092. r = sprintf(buf, "%lu\n", tr->entries);
  2093. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2094. }
  2095. static ssize_t
  2096. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2097. size_t cnt, loff_t *ppos)
  2098. {
  2099. unsigned long val;
  2100. char buf[64];
  2101. int ret;
  2102. if (cnt >= sizeof(buf))
  2103. return -EINVAL;
  2104. if (copy_from_user(&buf, ubuf, cnt))
  2105. return -EFAULT;
  2106. buf[cnt] = 0;
  2107. ret = strict_strtoul(buf, 10, &val);
  2108. if (ret < 0)
  2109. return ret;
  2110. /* must have at least 1 entry */
  2111. if (!val)
  2112. return -EINVAL;
  2113. mutex_lock(&trace_types_lock);
  2114. if (current_trace != &no_tracer) {
  2115. cnt = -EBUSY;
  2116. pr_info("ftrace: set current_tracer to none"
  2117. " before modifying buffer size\n");
  2118. goto out;
  2119. }
  2120. if (val > global_trace.entries) {
  2121. while (global_trace.entries < val) {
  2122. if (trace_alloc_page()) {
  2123. cnt = -ENOMEM;
  2124. goto out;
  2125. }
  2126. }
  2127. } else {
  2128. /* include the number of entries in val (inc of page entries) */
  2129. while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
  2130. trace_free_page();
  2131. }
  2132. filp->f_pos += cnt;
  2133. out:
  2134. max_tr.entries = global_trace.entries;
  2135. mutex_unlock(&trace_types_lock);
  2136. return cnt;
  2137. }
  2138. static struct file_operations tracing_max_lat_fops = {
  2139. .open = tracing_open_generic,
  2140. .read = tracing_max_lat_read,
  2141. .write = tracing_max_lat_write,
  2142. };
  2143. static struct file_operations tracing_ctrl_fops = {
  2144. .open = tracing_open_generic,
  2145. .read = tracing_ctrl_read,
  2146. .write = tracing_ctrl_write,
  2147. };
  2148. static struct file_operations set_tracer_fops = {
  2149. .open = tracing_open_generic,
  2150. .read = tracing_set_trace_read,
  2151. .write = tracing_set_trace_write,
  2152. };
  2153. static struct file_operations tracing_pipe_fops = {
  2154. .open = tracing_open_pipe,
  2155. .poll = tracing_poll_pipe,
  2156. .read = tracing_read_pipe,
  2157. .release = tracing_release_pipe,
  2158. };
  2159. static struct file_operations tracing_entries_fops = {
  2160. .open = tracing_open_generic,
  2161. .read = tracing_entries_read,
  2162. .write = tracing_entries_write,
  2163. };
  2164. #ifdef CONFIG_DYNAMIC_FTRACE
  2165. static ssize_t
  2166. tracing_read_long(struct file *filp, char __user *ubuf,
  2167. size_t cnt, loff_t *ppos)
  2168. {
  2169. unsigned long *p = filp->private_data;
  2170. char buf[64];
  2171. int r;
  2172. r = sprintf(buf, "%ld\n", *p);
  2173. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2174. }
  2175. static struct file_operations tracing_read_long_fops = {
  2176. .open = tracing_open_generic,
  2177. .read = tracing_read_long,
  2178. };
  2179. #endif
  2180. static struct dentry *d_tracer;
  2181. struct dentry *tracing_init_dentry(void)
  2182. {
  2183. static int once;
  2184. if (d_tracer)
  2185. return d_tracer;
  2186. d_tracer = debugfs_create_dir("tracing", NULL);
  2187. if (!d_tracer && !once) {
  2188. once = 1;
  2189. pr_warning("Could not create debugfs directory 'tracing'\n");
  2190. return NULL;
  2191. }
  2192. return d_tracer;
  2193. }
  2194. #ifdef CONFIG_FTRACE_SELFTEST
  2195. /* Let selftest have access to static functions in this file */
  2196. #include "trace_selftest.c"
  2197. #endif
  2198. static __init void tracer_init_debugfs(void)
  2199. {
  2200. struct dentry *d_tracer;
  2201. struct dentry *entry;
  2202. d_tracer = tracing_init_dentry();
  2203. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2204. &global_trace, &tracing_ctrl_fops);
  2205. if (!entry)
  2206. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2207. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2208. NULL, &tracing_iter_fops);
  2209. if (!entry)
  2210. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2211. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2212. NULL, &tracing_cpumask_fops);
  2213. if (!entry)
  2214. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2215. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2216. &global_trace, &tracing_lt_fops);
  2217. if (!entry)
  2218. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2219. entry = debugfs_create_file("trace", 0444, d_tracer,
  2220. &global_trace, &tracing_fops);
  2221. if (!entry)
  2222. pr_warning("Could not create debugfs 'trace' entry\n");
  2223. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2224. &global_trace, &show_traces_fops);
  2225. if (!entry)
  2226. pr_warning("Could not create debugfs 'trace' entry\n");
  2227. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2228. &global_trace, &set_tracer_fops);
  2229. if (!entry)
  2230. pr_warning("Could not create debugfs 'trace' entry\n");
  2231. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2232. &tracing_max_latency,
  2233. &tracing_max_lat_fops);
  2234. if (!entry)
  2235. pr_warning("Could not create debugfs "
  2236. "'tracing_max_latency' entry\n");
  2237. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2238. &tracing_thresh, &tracing_max_lat_fops);
  2239. if (!entry)
  2240. pr_warning("Could not create debugfs "
  2241. "'tracing_threash' entry\n");
  2242. entry = debugfs_create_file("README", 0644, d_tracer,
  2243. NULL, &tracing_readme_fops);
  2244. if (!entry)
  2245. pr_warning("Could not create debugfs 'README' entry\n");
  2246. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2247. NULL, &tracing_pipe_fops);
  2248. if (!entry)
  2249. pr_warning("Could not create debugfs "
  2250. "'tracing_threash' entry\n");
  2251. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2252. &global_trace, &tracing_entries_fops);
  2253. if (!entry)
  2254. pr_warning("Could not create debugfs "
  2255. "'tracing_threash' entry\n");
  2256. #ifdef CONFIG_DYNAMIC_FTRACE
  2257. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2258. &ftrace_update_tot_cnt,
  2259. &tracing_read_long_fops);
  2260. if (!entry)
  2261. pr_warning("Could not create debugfs "
  2262. "'dyn_ftrace_total_info' entry\n");
  2263. #endif
  2264. }
  2265. static int trace_alloc_page(void)
  2266. {
  2267. struct trace_array_cpu *data;
  2268. struct page *page, *tmp;
  2269. LIST_HEAD(pages);
  2270. void *array;
  2271. int i;
  2272. /* first allocate a page for each CPU */
  2273. for_each_tracing_cpu(i) {
  2274. array = (void *)__get_free_page(GFP_KERNEL);
  2275. if (array == NULL) {
  2276. printk(KERN_ERR "tracer: failed to allocate page"
  2277. "for trace buffer!\n");
  2278. goto free_pages;
  2279. }
  2280. page = virt_to_page(array);
  2281. list_add(&page->lru, &pages);
  2282. /* Only allocate if we are actually using the max trace */
  2283. #ifdef CONFIG_TRACER_MAX_TRACE
  2284. array = (void *)__get_free_page(GFP_KERNEL);
  2285. if (array == NULL) {
  2286. printk(KERN_ERR "tracer: failed to allocate page"
  2287. "for trace buffer!\n");
  2288. goto free_pages;
  2289. }
  2290. page = virt_to_page(array);
  2291. list_add(&page->lru, &pages);
  2292. #endif
  2293. }
  2294. /* Now that we successfully allocate a page per CPU, add them */
  2295. for_each_tracing_cpu(i) {
  2296. data = global_trace.data[i];
  2297. page = list_entry(pages.next, struct page, lru);
  2298. list_del_init(&page->lru);
  2299. list_add_tail(&page->lru, &data->trace_pages);
  2300. ClearPageLRU(page);
  2301. #ifdef CONFIG_TRACER_MAX_TRACE
  2302. data = max_tr.data[i];
  2303. page = list_entry(pages.next, struct page, lru);
  2304. list_del_init(&page->lru);
  2305. list_add_tail(&page->lru, &data->trace_pages);
  2306. SetPageLRU(page);
  2307. #endif
  2308. }
  2309. global_trace.entries += ENTRIES_PER_PAGE;
  2310. return 0;
  2311. free_pages:
  2312. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2313. list_del_init(&page->lru);
  2314. __free_page(page);
  2315. }
  2316. return -ENOMEM;
  2317. }
  2318. static int trace_free_page(void)
  2319. {
  2320. struct trace_array_cpu *data;
  2321. struct page *page;
  2322. struct list_head *p;
  2323. int i;
  2324. int ret = 0;
  2325. /* free one page from each buffer */
  2326. for_each_tracing_cpu(i) {
  2327. data = global_trace.data[i];
  2328. p = data->trace_pages.next;
  2329. if (p == &data->trace_pages) {
  2330. /* should never happen */
  2331. WARN_ON(1);
  2332. tracing_disabled = 1;
  2333. ret = -1;
  2334. break;
  2335. }
  2336. page = list_entry(p, struct page, lru);
  2337. ClearPageLRU(page);
  2338. list_del(&page->lru);
  2339. __free_page(page);
  2340. tracing_reset(data);
  2341. #ifdef CONFIG_TRACER_MAX_TRACE
  2342. data = max_tr.data[i];
  2343. p = data->trace_pages.next;
  2344. if (p == &data->trace_pages) {
  2345. /* should never happen */
  2346. WARN_ON(1);
  2347. tracing_disabled = 1;
  2348. ret = -1;
  2349. break;
  2350. }
  2351. page = list_entry(p, struct page, lru);
  2352. ClearPageLRU(page);
  2353. list_del(&page->lru);
  2354. __free_page(page);
  2355. tracing_reset(data);
  2356. #endif
  2357. }
  2358. global_trace.entries -= ENTRIES_PER_PAGE;
  2359. return ret;
  2360. }
  2361. __init static int tracer_alloc_buffers(void)
  2362. {
  2363. struct trace_array_cpu *data;
  2364. void *array;
  2365. struct page *page;
  2366. int pages = 0;
  2367. int ret = -ENOMEM;
  2368. int i;
  2369. global_trace.ctrl = tracer_enabled;
  2370. /* TODO: make the number of buffers hot pluggable with CPUS */
  2371. tracing_nr_buffers = num_possible_cpus();
  2372. tracing_buffer_mask = cpu_possible_map;
  2373. /* Allocate the first page for all buffers */
  2374. for_each_tracing_cpu(i) {
  2375. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2376. max_tr.data[i] = &per_cpu(max_data, i);
  2377. array = (void *)__get_free_page(GFP_KERNEL);
  2378. if (array == NULL) {
  2379. printk(KERN_ERR "tracer: failed to allocate page"
  2380. "for trace buffer!\n");
  2381. goto free_buffers;
  2382. }
  2383. /* set the array to the list */
  2384. INIT_LIST_HEAD(&data->trace_pages);
  2385. page = virt_to_page(array);
  2386. list_add(&page->lru, &data->trace_pages);
  2387. /* use the LRU flag to differentiate the two buffers */
  2388. ClearPageLRU(page);
  2389. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2390. max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2391. /* Only allocate if we are actually using the max trace */
  2392. #ifdef CONFIG_TRACER_MAX_TRACE
  2393. array = (void *)__get_free_page(GFP_KERNEL);
  2394. if (array == NULL) {
  2395. printk(KERN_ERR "tracer: failed to allocate page"
  2396. "for trace buffer!\n");
  2397. goto free_buffers;
  2398. }
  2399. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2400. page = virt_to_page(array);
  2401. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2402. SetPageLRU(page);
  2403. #endif
  2404. }
  2405. /*
  2406. * Since we allocate by orders of pages, we may be able to
  2407. * round up a bit.
  2408. */
  2409. global_trace.entries = ENTRIES_PER_PAGE;
  2410. pages++;
  2411. while (global_trace.entries < trace_nr_entries) {
  2412. if (trace_alloc_page())
  2413. break;
  2414. pages++;
  2415. }
  2416. max_tr.entries = global_trace.entries;
  2417. pr_info("tracer: %d pages allocated for %ld",
  2418. pages, trace_nr_entries);
  2419. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  2420. pr_info(" actual entries %ld\n", global_trace.entries);
  2421. tracer_init_debugfs();
  2422. trace_init_cmdlines();
  2423. register_tracer(&no_tracer);
  2424. current_trace = &no_tracer;
  2425. /* All seems OK, enable tracing */
  2426. tracing_disabled = 0;
  2427. return 0;
  2428. free_buffers:
  2429. for (i-- ; i >= 0; i--) {
  2430. struct page *page, *tmp;
  2431. struct trace_array_cpu *data = global_trace.data[i];
  2432. if (data) {
  2433. list_for_each_entry_safe(page, tmp,
  2434. &data->trace_pages, lru) {
  2435. list_del_init(&page->lru);
  2436. __free_page(page);
  2437. }
  2438. }
  2439. #ifdef CONFIG_TRACER_MAX_TRACE
  2440. data = max_tr.data[i];
  2441. if (data) {
  2442. list_for_each_entry_safe(page, tmp,
  2443. &data->trace_pages, lru) {
  2444. list_del_init(&page->lru);
  2445. __free_page(page);
  2446. }
  2447. }
  2448. #endif
  2449. }
  2450. return ret;
  2451. }
  2452. fs_initcall(tracer_alloc_buffers);