ftrace.txt 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. ftrace - Function Tracer
  2. ========================
  3. Copyright 2008 Red Hat Inc.
  4. Author: Steven Rostedt <srostedt@redhat.com>
  5. License: The GNU Free Documentation License, Version 1.2
  6. (dual licensed under the GPL v2)
  7. Reviewers: Elias Oltmanns, Randy Dunlap, Andrew Morton,
  8. John Kacur, and David Teigland.
  9. Written for: 2.6.28-rc2
  10. Introduction
  11. ------------
  12. Ftrace is an internal tracer designed to help out developers and
  13. designers of systems to find what is going on inside the kernel.
  14. It can be used for debugging or analyzing latencies and
  15. performance issues that take place outside of user-space.
  16. Although ftrace is the function tracer, it also includes an
  17. infrastructure that allows for other types of tracing. Some of
  18. the tracers that are currently in ftrace include a tracer to
  19. trace context switches, the time it takes for a high priority
  20. task to run after it was woken up, the time interrupts are
  21. disabled, and more (ftrace allows for tracer plugins, which
  22. means that the list of tracers can always grow).
  23. Implementation Details
  24. ----------------------
  25. See ftrace-design.txt for details for arch porters and such.
  26. The File System
  27. ---------------
  28. Ftrace uses the debugfs file system to hold the control files as
  29. well as the files to display output.
  30. When debugfs is configured into the kernel (which selecting any ftrace
  31. option will do) the directory /sys/kernel/debug will be created. To mount
  32. this directory, you can add to your /etc/fstab file:
  33. debugfs /sys/kernel/debug debugfs defaults 0 0
  34. Or you can mount it at run time with:
  35. mount -t debugfs nodev /sys/kernel/debug
  36. For quicker access to that directory you may want to make a soft link to
  37. it:
  38. ln -s /sys/kernel/debug /debug
  39. Any selected ftrace option will also create a directory called tracing
  40. within the debugfs. The rest of the document will assume that you are in
  41. the ftrace directory (cd /sys/kernel/debug/tracing) and will only concentrate
  42. on the files within that directory and not distract from the content with
  43. the extended "/sys/kernel/debug/tracing" path name.
  44. That's it! (assuming that you have ftrace configured into your kernel)
  45. After mounting the debugfs, you can see a directory called
  46. "tracing". This directory contains the control and output files
  47. of ftrace. Here is a list of some of the key files:
  48. Note: all time values are in microseconds.
  49. current_tracer:
  50. This is used to set or display the current tracer
  51. that is configured.
  52. available_tracers:
  53. This holds the different types of tracers that
  54. have been compiled into the kernel. The
  55. tracers listed here can be configured by
  56. echoing their name into current_tracer.
  57. tracing_enabled:
  58. This sets or displays whether the current_tracer
  59. is activated and tracing or not. Echo 0 into this
  60. file to disable the tracer or 1 to enable it.
  61. trace:
  62. This file holds the output of the trace in a human
  63. readable format (described below).
  64. trace_pipe:
  65. The output is the same as the "trace" file but this
  66. file is meant to be streamed with live tracing.
  67. Reads from this file will block until new data is
  68. retrieved. Unlike the "trace" file, this file is a
  69. consumer. This means reading from this file causes
  70. sequential reads to display more current data. Once
  71. data is read from this file, it is consumed, and
  72. will not be read again with a sequential read. The
  73. "trace" file is static, and if the tracer is not
  74. adding more data,they will display the same
  75. information every time they are read.
  76. trace_options:
  77. This file lets the user control the amount of data
  78. that is displayed in one of the above output
  79. files.
  80. tracing_max_latency:
  81. Some of the tracers record the max latency.
  82. For example, the time interrupts are disabled.
  83. This time is saved in this file. The max trace
  84. will also be stored, and displayed by "trace".
  85. A new max trace will only be recorded if the
  86. latency is greater than the value in this
  87. file. (in microseconds)
  88. buffer_size_kb:
  89. This sets or displays the number of kilobytes each CPU
  90. buffer can hold. The tracer buffers are the same size
  91. for each CPU. The displayed number is the size of the
  92. CPU buffer and not total size of all buffers. The
  93. trace buffers are allocated in pages (blocks of memory
  94. that the kernel uses for allocation, usually 4 KB in size).
  95. If the last page allocated has room for more bytes
  96. than requested, the rest of the page will be used,
  97. making the actual allocation bigger than requested.
  98. ( Note, the size may not be a multiple of the page size
  99. due to buffer management overhead. )
  100. This can only be updated when the current_tracer
  101. is set to "nop".
  102. tracing_cpumask:
  103. This is a mask that lets the user only trace
  104. on specified CPUS. The format is a hex string
  105. representing the CPUS.
  106. set_ftrace_filter:
  107. When dynamic ftrace is configured in (see the
  108. section below "dynamic ftrace"), the code is dynamically
  109. modified (code text rewrite) to disable calling of the
  110. function profiler (mcount). This lets tracing be configured
  111. in with practically no overhead in performance. This also
  112. has a side effect of enabling or disabling specific functions
  113. to be traced. Echoing names of functions into this file
  114. will limit the trace to only those functions.
  115. This interface also allows for commands to be used. See the
  116. "Filter commands" section for more details.
  117. set_ftrace_notrace:
  118. This has an effect opposite to that of
  119. set_ftrace_filter. Any function that is added here will not
  120. be traced. If a function exists in both set_ftrace_filter
  121. and set_ftrace_notrace, the function will _not_ be traced.
  122. set_ftrace_pid:
  123. Have the function tracer only trace a single thread.
  124. set_graph_function:
  125. Set a "trigger" function where tracing should start
  126. with the function graph tracer (See the section
  127. "dynamic ftrace" for more details).
  128. available_filter_functions:
  129. This lists the functions that ftrace
  130. has processed and can trace. These are the function
  131. names that you can pass to "set_ftrace_filter" or
  132. "set_ftrace_notrace". (See the section "dynamic ftrace"
  133. below for more details.)
  134. The Tracers
  135. -----------
  136. Here is the list of current tracers that may be configured.
  137. "function"
  138. Function call tracer to trace all kernel functions.
  139. "function_graph"
  140. Similar to the function tracer except that the
  141. function tracer probes the functions on their entry
  142. whereas the function graph tracer traces on both entry
  143. and exit of the functions. It then provides the ability
  144. to draw a graph of function calls similar to C code
  145. source.
  146. "irqsoff"
  147. Traces the areas that disable interrupts and saves
  148. the trace with the longest max latency.
  149. See tracing_max_latency. When a new max is recorded,
  150. it replaces the old trace. It is best to view this
  151. trace with the latency-format option enabled.
  152. "preemptoff"
  153. Similar to irqsoff but traces and records the amount of
  154. time for which preemption is disabled.
  155. "preemptirqsoff"
  156. Similar to irqsoff and preemptoff, but traces and
  157. records the largest time for which irqs and/or preemption
  158. is disabled.
  159. "wakeup"
  160. Traces and records the max latency that it takes for
  161. the highest priority task to get scheduled after
  162. it has been woken up.
  163. "hw-branch-tracer"
  164. Uses the BTS CPU feature on x86 CPUs to traces all
  165. branches executed.
  166. "nop"
  167. This is the "trace nothing" tracer. To remove all
  168. tracers from tracing simply echo "nop" into
  169. current_tracer.
  170. Examples of using the tracer
  171. ----------------------------
  172. Here are typical examples of using the tracers when controlling
  173. them only with the debugfs interface (without using any
  174. user-land utilities).
  175. Output format:
  176. --------------
  177. Here is an example of the output format of the file "trace"
  178. --------
  179. # tracer: function
  180. #
  181. # TASK-PID CPU# TIMESTAMP FUNCTION
  182. # | | | | |
  183. bash-4251 [01] 10152.583854: path_put <-path_walk
  184. bash-4251 [01] 10152.583855: dput <-path_put
  185. bash-4251 [01] 10152.583855: _atomic_dec_and_lock <-dput
  186. --------
  187. A header is printed with the tracer name that is represented by
  188. the trace. In this case the tracer is "function". Then a header
  189. showing the format. Task name "bash", the task PID "4251", the
  190. CPU that it was running on "01", the timestamp in <secs>.<usecs>
  191. format, the function name that was traced "path_put" and the
  192. parent function that called this function "path_walk". The
  193. timestamp is the time at which the function was entered.
  194. Latency trace format
  195. --------------------
  196. When the latency-format option is enabled, the trace file gives
  197. somewhat more information to see why a latency happened.
  198. Here is a typical trace.
  199. # tracer: irqsoff
  200. #
  201. irqsoff latency trace v1.1.5 on 2.6.26-rc8
  202. --------------------------------------------------------------------
  203. latency: 97 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  204. -----------------
  205. | task: swapper-0 (uid:0 nice:0 policy:0 rt_prio:0)
  206. -----------------
  207. => started at: apic_timer_interrupt
  208. => ended at: do_softirq
  209. # _------=> CPU#
  210. # / _-----=> irqs-off
  211. # | / _----=> need-resched
  212. # || / _---=> hardirq/softirq
  213. # ||| / _--=> preempt-depth
  214. # |||| /
  215. # ||||| delay
  216. # cmd pid ||||| time | caller
  217. # \ / ||||| \ | /
  218. <idle>-0 0d..1 0us+: trace_hardirqs_off_thunk (apic_timer_interrupt)
  219. <idle>-0 0d.s. 97us : __do_softirq (do_softirq)
  220. <idle>-0 0d.s1 98us : trace_hardirqs_on (do_softirq)
  221. This shows that the current tracer is "irqsoff" tracing the time
  222. for which interrupts were disabled. It gives the trace version
  223. and the version of the kernel upon which this was executed on
  224. (2.6.26-rc8). Then it displays the max latency in microsecs (97
  225. us). The number of trace entries displayed and the total number
  226. recorded (both are three: #3/3). The type of preemption that was
  227. used (PREEMPT). VP, KP, SP, and HP are always zero and are
  228. reserved for later use. #P is the number of online CPUS (#P:2).
  229. The task is the process that was running when the latency
  230. occurred. (swapper pid: 0).
  231. The start and stop (the functions in which the interrupts were
  232. disabled and enabled respectively) that caused the latencies:
  233. apic_timer_interrupt is where the interrupts were disabled.
  234. do_softirq is where they were enabled again.
  235. The next lines after the header are the trace itself. The header
  236. explains which is which.
  237. cmd: The name of the process in the trace.
  238. pid: The PID of that process.
  239. CPU#: The CPU which the process was running on.
  240. irqs-off: 'd' interrupts are disabled. '.' otherwise.
  241. Note: If the architecture does not support a way to
  242. read the irq flags variable, an 'X' will always
  243. be printed here.
  244. need-resched: 'N' task need_resched is set, '.' otherwise.
  245. hardirq/softirq:
  246. 'H' - hard irq occurred inside a softirq.
  247. 'h' - hard irq is running
  248. 's' - soft irq is running
  249. '.' - normal context.
  250. preempt-depth: The level of preempt_disabled
  251. The above is mostly meaningful for kernel developers.
  252. time: When the latency-format option is enabled, the trace file
  253. output includes a timestamp relative to the start of the
  254. trace. This differs from the output when latency-format
  255. is disabled, which includes an absolute timestamp.
  256. delay: This is just to help catch your eye a bit better. And
  257. needs to be fixed to be only relative to the same CPU.
  258. The marks are determined by the difference between this
  259. current trace and the next trace.
  260. '!' - greater than preempt_mark_thresh (default 100)
  261. '+' - greater than 1 microsecond
  262. ' ' - less than or equal to 1 microsecond.
  263. The rest is the same as the 'trace' file.
  264. trace_options
  265. -------------
  266. The trace_options file is used to control what gets printed in
  267. the trace output. To see what is available, simply cat the file:
  268. cat trace_options
  269. print-parent nosym-offset nosym-addr noverbose noraw nohex nobin \
  270. noblock nostacktrace nosched-tree nouserstacktrace nosym-userobj
  271. To disable one of the options, echo in the option prepended with
  272. "no".
  273. echo noprint-parent > trace_options
  274. To enable an option, leave off the "no".
  275. echo sym-offset > trace_options
  276. Here are the available options:
  277. print-parent - On function traces, display the calling (parent)
  278. function as well as the function being traced.
  279. print-parent:
  280. bash-4000 [01] 1477.606694: simple_strtoul <-strict_strtoul
  281. noprint-parent:
  282. bash-4000 [01] 1477.606694: simple_strtoul
  283. sym-offset - Display not only the function name, but also the
  284. offset in the function. For example, instead of
  285. seeing just "ktime_get", you will see
  286. "ktime_get+0xb/0x20".
  287. sym-offset:
  288. bash-4000 [01] 1477.606694: simple_strtoul+0x6/0xa0
  289. sym-addr - this will also display the function address as well
  290. as the function name.
  291. sym-addr:
  292. bash-4000 [01] 1477.606694: simple_strtoul <c0339346>
  293. verbose - This deals with the trace file when the
  294. latency-format option is enabled.
  295. bash 4000 1 0 00000000 00010a95 [58127d26] 1720.415ms \
  296. (+0.000ms): simple_strtoul (strict_strtoul)
  297. raw - This will display raw numbers. This option is best for
  298. use with user applications that can translate the raw
  299. numbers better than having it done in the kernel.
  300. hex - Similar to raw, but the numbers will be in a hexadecimal
  301. format.
  302. bin - This will print out the formats in raw binary.
  303. block - TBD (needs update)
  304. stacktrace - This is one of the options that changes the trace
  305. itself. When a trace is recorded, so is the stack
  306. of functions. This allows for back traces of
  307. trace sites.
  308. userstacktrace - This option changes the trace. It records a
  309. stacktrace of the current userspace thread.
  310. sym-userobj - when user stacktrace are enabled, look up which
  311. object the address belongs to, and print a
  312. relative address. This is especially useful when
  313. ASLR is on, otherwise you don't get a chance to
  314. resolve the address to object/file/line after
  315. the app is no longer running
  316. The lookup is performed when you read
  317. trace,trace_pipe. Example:
  318. a.out-1623 [000] 40874.465068: /root/a.out[+0x480] <-/root/a.out[+0
  319. x494] <- /root/a.out[+0x4a8] <- /lib/libc-2.7.so[+0x1e1a6]
  320. sched-tree - trace all tasks that are on the runqueue, at
  321. every scheduling event. Will add overhead if
  322. there's a lot of tasks running at once.
  323. latency-format - This option changes the trace. When
  324. it is enabled, the trace displays
  325. additional information about the
  326. latencies, as described in "Latency
  327. trace format".
  328. ftrace_enabled
  329. --------------
  330. The following tracers (listed below) give different output
  331. depending on whether or not the sysctl ftrace_enabled is set. To
  332. set ftrace_enabled, one can either use the sysctl function or
  333. set it via the proc file system interface.
  334. sysctl kernel.ftrace_enabled=1
  335. or
  336. echo 1 > /proc/sys/kernel/ftrace_enabled
  337. To disable ftrace_enabled simply replace the '1' with '0' in the
  338. above commands.
  339. When ftrace_enabled is set the tracers will also record the
  340. functions that are within the trace. The descriptions of the
  341. tracers will also show an example with ftrace enabled.
  342. irqsoff
  343. -------
  344. When interrupts are disabled, the CPU can not react to any other
  345. external event (besides NMIs and SMIs). This prevents the timer
  346. interrupt from triggering or the mouse interrupt from letting
  347. the kernel know of a new mouse event. The result is a latency
  348. with the reaction time.
  349. The irqsoff tracer tracks the time for which interrupts are
  350. disabled. When a new maximum latency is hit, the tracer saves
  351. the trace leading up to that latency point so that every time a
  352. new maximum is reached, the old saved trace is discarded and the
  353. new trace is saved.
  354. To reset the maximum, echo 0 into tracing_max_latency. Here is
  355. an example:
  356. # echo irqsoff > current_tracer
  357. # echo latency-format > trace_options
  358. # echo 0 > tracing_max_latency
  359. # echo 1 > tracing_enabled
  360. # ls -ltr
  361. [...]
  362. # echo 0 > tracing_enabled
  363. # cat trace
  364. # tracer: irqsoff
  365. #
  366. irqsoff latency trace v1.1.5 on 2.6.26
  367. --------------------------------------------------------------------
  368. latency: 12 us, #3/3, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  369. -----------------
  370. | task: bash-3730 (uid:0 nice:0 policy:0 rt_prio:0)
  371. -----------------
  372. => started at: sys_setpgid
  373. => ended at: sys_setpgid
  374. # _------=> CPU#
  375. # / _-----=> irqs-off
  376. # | / _----=> need-resched
  377. # || / _---=> hardirq/softirq
  378. # ||| / _--=> preempt-depth
  379. # |||| /
  380. # ||||| delay
  381. # cmd pid ||||| time | caller
  382. # \ / ||||| \ | /
  383. bash-3730 1d... 0us : _write_lock_irq (sys_setpgid)
  384. bash-3730 1d..1 1us+: _write_unlock_irq (sys_setpgid)
  385. bash-3730 1d..2 14us : trace_hardirqs_on (sys_setpgid)
  386. Here we see that that we had a latency of 12 microsecs (which is
  387. very good). The _write_lock_irq in sys_setpgid disabled
  388. interrupts. The difference between the 12 and the displayed
  389. timestamp 14us occurred because the clock was incremented
  390. between the time of recording the max latency and the time of
  391. recording the function that had that latency.
  392. Note the above example had ftrace_enabled not set. If we set the
  393. ftrace_enabled, we get a much larger output:
  394. # tracer: irqsoff
  395. #
  396. irqsoff latency trace v1.1.5 on 2.6.26-rc8
  397. --------------------------------------------------------------------
  398. latency: 50 us, #101/101, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  399. -----------------
  400. | task: ls-4339 (uid:0 nice:0 policy:0 rt_prio:0)
  401. -----------------
  402. => started at: __alloc_pages_internal
  403. => ended at: __alloc_pages_internal
  404. # _------=> CPU#
  405. # / _-----=> irqs-off
  406. # | / _----=> need-resched
  407. # || / _---=> hardirq/softirq
  408. # ||| / _--=> preempt-depth
  409. # |||| /
  410. # ||||| delay
  411. # cmd pid ||||| time | caller
  412. # \ / ||||| \ | /
  413. ls-4339 0...1 0us+: get_page_from_freelist (__alloc_pages_internal)
  414. ls-4339 0d..1 3us : rmqueue_bulk (get_page_from_freelist)
  415. ls-4339 0d..1 3us : _spin_lock (rmqueue_bulk)
  416. ls-4339 0d..1 4us : add_preempt_count (_spin_lock)
  417. ls-4339 0d..2 4us : __rmqueue (rmqueue_bulk)
  418. ls-4339 0d..2 5us : __rmqueue_smallest (__rmqueue)
  419. ls-4339 0d..2 5us : __mod_zone_page_state (__rmqueue_smallest)
  420. ls-4339 0d..2 6us : __rmqueue (rmqueue_bulk)
  421. ls-4339 0d..2 6us : __rmqueue_smallest (__rmqueue)
  422. ls-4339 0d..2 7us : __mod_zone_page_state (__rmqueue_smallest)
  423. ls-4339 0d..2 7us : __rmqueue (rmqueue_bulk)
  424. ls-4339 0d..2 8us : __rmqueue_smallest (__rmqueue)
  425. [...]
  426. ls-4339 0d..2 46us : __rmqueue_smallest (__rmqueue)
  427. ls-4339 0d..2 47us : __mod_zone_page_state (__rmqueue_smallest)
  428. ls-4339 0d..2 47us : __rmqueue (rmqueue_bulk)
  429. ls-4339 0d..2 48us : __rmqueue_smallest (__rmqueue)
  430. ls-4339 0d..2 48us : __mod_zone_page_state (__rmqueue_smallest)
  431. ls-4339 0d..2 49us : _spin_unlock (rmqueue_bulk)
  432. ls-4339 0d..2 49us : sub_preempt_count (_spin_unlock)
  433. ls-4339 0d..1 50us : get_page_from_freelist (__alloc_pages_internal)
  434. ls-4339 0d..2 51us : trace_hardirqs_on (__alloc_pages_internal)
  435. Here we traced a 50 microsecond latency. But we also see all the
  436. functions that were called during that time. Note that by
  437. enabling function tracing, we incur an added overhead. This
  438. overhead may extend the latency times. But nevertheless, this
  439. trace has provided some very helpful debugging information.
  440. preemptoff
  441. ----------
  442. When preemption is disabled, we may be able to receive
  443. interrupts but the task cannot be preempted and a higher
  444. priority task must wait for preemption to be enabled again
  445. before it can preempt a lower priority task.
  446. The preemptoff tracer traces the places that disable preemption.
  447. Like the irqsoff tracer, it records the maximum latency for
  448. which preemption was disabled. The control of preemptoff tracer
  449. is much like the irqsoff tracer.
  450. # echo preemptoff > current_tracer
  451. # echo latency-format > trace_options
  452. # echo 0 > tracing_max_latency
  453. # echo 1 > tracing_enabled
  454. # ls -ltr
  455. [...]
  456. # echo 0 > tracing_enabled
  457. # cat trace
  458. # tracer: preemptoff
  459. #
  460. preemptoff latency trace v1.1.5 on 2.6.26-rc8
  461. --------------------------------------------------------------------
  462. latency: 29 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  463. -----------------
  464. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  465. -----------------
  466. => started at: do_IRQ
  467. => ended at: __do_softirq
  468. # _------=> CPU#
  469. # / _-----=> irqs-off
  470. # | / _----=> need-resched
  471. # || / _---=> hardirq/softirq
  472. # ||| / _--=> preempt-depth
  473. # |||| /
  474. # ||||| delay
  475. # cmd pid ||||| time | caller
  476. # \ / ||||| \ | /
  477. sshd-4261 0d.h. 0us+: irq_enter (do_IRQ)
  478. sshd-4261 0d.s. 29us : _local_bh_enable (__do_softirq)
  479. sshd-4261 0d.s1 30us : trace_preempt_on (__do_softirq)
  480. This has some more changes. Preemption was disabled when an
  481. interrupt came in (notice the 'h'), and was enabled while doing
  482. a softirq. (notice the 's'). But we also see that interrupts
  483. have been disabled when entering the preempt off section and
  484. leaving it (the 'd'). We do not know if interrupts were enabled
  485. in the mean time.
  486. # tracer: preemptoff
  487. #
  488. preemptoff latency trace v1.1.5 on 2.6.26-rc8
  489. --------------------------------------------------------------------
  490. latency: 63 us, #87/87, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  491. -----------------
  492. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  493. -----------------
  494. => started at: remove_wait_queue
  495. => ended at: __do_softirq
  496. # _------=> CPU#
  497. # / _-----=> irqs-off
  498. # | / _----=> need-resched
  499. # || / _---=> hardirq/softirq
  500. # ||| / _--=> preempt-depth
  501. # |||| /
  502. # ||||| delay
  503. # cmd pid ||||| time | caller
  504. # \ / ||||| \ | /
  505. sshd-4261 0d..1 0us : _spin_lock_irqsave (remove_wait_queue)
  506. sshd-4261 0d..1 1us : _spin_unlock_irqrestore (remove_wait_queue)
  507. sshd-4261 0d..1 2us : do_IRQ (common_interrupt)
  508. sshd-4261 0d..1 2us : irq_enter (do_IRQ)
  509. sshd-4261 0d..1 2us : idle_cpu (irq_enter)
  510. sshd-4261 0d..1 3us : add_preempt_count (irq_enter)
  511. sshd-4261 0d.h1 3us : idle_cpu (irq_enter)
  512. sshd-4261 0d.h. 4us : handle_fasteoi_irq (do_IRQ)
  513. [...]
  514. sshd-4261 0d.h. 12us : add_preempt_count (_spin_lock)
  515. sshd-4261 0d.h1 12us : ack_ioapic_quirk_irq (handle_fasteoi_irq)
  516. sshd-4261 0d.h1 13us : move_native_irq (ack_ioapic_quirk_irq)
  517. sshd-4261 0d.h1 13us : _spin_unlock (handle_fasteoi_irq)
  518. sshd-4261 0d.h1 14us : sub_preempt_count (_spin_unlock)
  519. sshd-4261 0d.h1 14us : irq_exit (do_IRQ)
  520. sshd-4261 0d.h1 15us : sub_preempt_count (irq_exit)
  521. sshd-4261 0d..2 15us : do_softirq (irq_exit)
  522. sshd-4261 0d... 15us : __do_softirq (do_softirq)
  523. sshd-4261 0d... 16us : __local_bh_disable (__do_softirq)
  524. sshd-4261 0d... 16us+: add_preempt_count (__local_bh_disable)
  525. sshd-4261 0d.s4 20us : add_preempt_count (__local_bh_disable)
  526. sshd-4261 0d.s4 21us : sub_preempt_count (local_bh_enable)
  527. sshd-4261 0d.s5 21us : sub_preempt_count (local_bh_enable)
  528. [...]
  529. sshd-4261 0d.s6 41us : add_preempt_count (__local_bh_disable)
  530. sshd-4261 0d.s6 42us : sub_preempt_count (local_bh_enable)
  531. sshd-4261 0d.s7 42us : sub_preempt_count (local_bh_enable)
  532. sshd-4261 0d.s5 43us : add_preempt_count (__local_bh_disable)
  533. sshd-4261 0d.s5 43us : sub_preempt_count (local_bh_enable_ip)
  534. sshd-4261 0d.s6 44us : sub_preempt_count (local_bh_enable_ip)
  535. sshd-4261 0d.s5 44us : add_preempt_count (__local_bh_disable)
  536. sshd-4261 0d.s5 45us : sub_preempt_count (local_bh_enable)
  537. [...]
  538. sshd-4261 0d.s. 63us : _local_bh_enable (__do_softirq)
  539. sshd-4261 0d.s1 64us : trace_preempt_on (__do_softirq)
  540. The above is an example of the preemptoff trace with
  541. ftrace_enabled set. Here we see that interrupts were disabled
  542. the entire time. The irq_enter code lets us know that we entered
  543. an interrupt 'h'. Before that, the functions being traced still
  544. show that it is not in an interrupt, but we can see from the
  545. functions themselves that this is not the case.
  546. Notice that __do_softirq when called does not have a
  547. preempt_count. It may seem that we missed a preempt enabling.
  548. What really happened is that the preempt count is held on the
  549. thread's stack and we switched to the softirq stack (4K stacks
  550. in effect). The code does not copy the preempt count, but
  551. because interrupts are disabled, we do not need to worry about
  552. it. Having a tracer like this is good for letting people know
  553. what really happens inside the kernel.
  554. preemptirqsoff
  555. --------------
  556. Knowing the locations that have interrupts disabled or
  557. preemption disabled for the longest times is helpful. But
  558. sometimes we would like to know when either preemption and/or
  559. interrupts are disabled.
  560. Consider the following code:
  561. local_irq_disable();
  562. call_function_with_irqs_off();
  563. preempt_disable();
  564. call_function_with_irqs_and_preemption_off();
  565. local_irq_enable();
  566. call_function_with_preemption_off();
  567. preempt_enable();
  568. The irqsoff tracer will record the total length of
  569. call_function_with_irqs_off() and
  570. call_function_with_irqs_and_preemption_off().
  571. The preemptoff tracer will record the total length of
  572. call_function_with_irqs_and_preemption_off() and
  573. call_function_with_preemption_off().
  574. But neither will trace the time that interrupts and/or
  575. preemption is disabled. This total time is the time that we can
  576. not schedule. To record this time, use the preemptirqsoff
  577. tracer.
  578. Again, using this trace is much like the irqsoff and preemptoff
  579. tracers.
  580. # echo preemptirqsoff > current_tracer
  581. # echo latency-format > trace_options
  582. # echo 0 > tracing_max_latency
  583. # echo 1 > tracing_enabled
  584. # ls -ltr
  585. [...]
  586. # echo 0 > tracing_enabled
  587. # cat trace
  588. # tracer: preemptirqsoff
  589. #
  590. preemptirqsoff latency trace v1.1.5 on 2.6.26-rc8
  591. --------------------------------------------------------------------
  592. latency: 293 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  593. -----------------
  594. | task: ls-4860 (uid:0 nice:0 policy:0 rt_prio:0)
  595. -----------------
  596. => started at: apic_timer_interrupt
  597. => ended at: __do_softirq
  598. # _------=> CPU#
  599. # / _-----=> irqs-off
  600. # | / _----=> need-resched
  601. # || / _---=> hardirq/softirq
  602. # ||| / _--=> preempt-depth
  603. # |||| /
  604. # ||||| delay
  605. # cmd pid ||||| time | caller
  606. # \ / ||||| \ | /
  607. ls-4860 0d... 0us!: trace_hardirqs_off_thunk (apic_timer_interrupt)
  608. ls-4860 0d.s. 294us : _local_bh_enable (__do_softirq)
  609. ls-4860 0d.s1 294us : trace_preempt_on (__do_softirq)
  610. The trace_hardirqs_off_thunk is called from assembly on x86 when
  611. interrupts are disabled in the assembly code. Without the
  612. function tracing, we do not know if interrupts were enabled
  613. within the preemption points. We do see that it started with
  614. preemption enabled.
  615. Here is a trace with ftrace_enabled set:
  616. # tracer: preemptirqsoff
  617. #
  618. preemptirqsoff latency trace v1.1.5 on 2.6.26-rc8
  619. --------------------------------------------------------------------
  620. latency: 105 us, #183/183, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  621. -----------------
  622. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  623. -----------------
  624. => started at: write_chan
  625. => ended at: __do_softirq
  626. # _------=> CPU#
  627. # / _-----=> irqs-off
  628. # | / _----=> need-resched
  629. # || / _---=> hardirq/softirq
  630. # ||| / _--=> preempt-depth
  631. # |||| /
  632. # ||||| delay
  633. # cmd pid ||||| time | caller
  634. # \ / ||||| \ | /
  635. ls-4473 0.N.. 0us : preempt_schedule (write_chan)
  636. ls-4473 0dN.1 1us : _spin_lock (schedule)
  637. ls-4473 0dN.1 2us : add_preempt_count (_spin_lock)
  638. ls-4473 0d..2 2us : put_prev_task_fair (schedule)
  639. [...]
  640. ls-4473 0d..2 13us : set_normalized_timespec (ktime_get_ts)
  641. ls-4473 0d..2 13us : __switch_to (schedule)
  642. sshd-4261 0d..2 14us : finish_task_switch (schedule)
  643. sshd-4261 0d..2 14us : _spin_unlock_irq (finish_task_switch)
  644. sshd-4261 0d..1 15us : add_preempt_count (_spin_lock_irqsave)
  645. sshd-4261 0d..2 16us : _spin_unlock_irqrestore (hrtick_set)
  646. sshd-4261 0d..2 16us : do_IRQ (common_interrupt)
  647. sshd-4261 0d..2 17us : irq_enter (do_IRQ)
  648. sshd-4261 0d..2 17us : idle_cpu (irq_enter)
  649. sshd-4261 0d..2 18us : add_preempt_count (irq_enter)
  650. sshd-4261 0d.h2 18us : idle_cpu (irq_enter)
  651. sshd-4261 0d.h. 18us : handle_fasteoi_irq (do_IRQ)
  652. sshd-4261 0d.h. 19us : _spin_lock (handle_fasteoi_irq)
  653. sshd-4261 0d.h. 19us : add_preempt_count (_spin_lock)
  654. sshd-4261 0d.h1 20us : _spin_unlock (handle_fasteoi_irq)
  655. sshd-4261 0d.h1 20us : sub_preempt_count (_spin_unlock)
  656. [...]
  657. sshd-4261 0d.h1 28us : _spin_unlock (handle_fasteoi_irq)
  658. sshd-4261 0d.h1 29us : sub_preempt_count (_spin_unlock)
  659. sshd-4261 0d.h2 29us : irq_exit (do_IRQ)
  660. sshd-4261 0d.h2 29us : sub_preempt_count (irq_exit)
  661. sshd-4261 0d..3 30us : do_softirq (irq_exit)
  662. sshd-4261 0d... 30us : __do_softirq (do_softirq)
  663. sshd-4261 0d... 31us : __local_bh_disable (__do_softirq)
  664. sshd-4261 0d... 31us+: add_preempt_count (__local_bh_disable)
  665. sshd-4261 0d.s4 34us : add_preempt_count (__local_bh_disable)
  666. [...]
  667. sshd-4261 0d.s3 43us : sub_preempt_count (local_bh_enable_ip)
  668. sshd-4261 0d.s4 44us : sub_preempt_count (local_bh_enable_ip)
  669. sshd-4261 0d.s3 44us : smp_apic_timer_interrupt (apic_timer_interrupt)
  670. sshd-4261 0d.s3 45us : irq_enter (smp_apic_timer_interrupt)
  671. sshd-4261 0d.s3 45us : idle_cpu (irq_enter)
  672. sshd-4261 0d.s3 46us : add_preempt_count (irq_enter)
  673. sshd-4261 0d.H3 46us : idle_cpu (irq_enter)
  674. sshd-4261 0d.H3 47us : hrtimer_interrupt (smp_apic_timer_interrupt)
  675. sshd-4261 0d.H3 47us : ktime_get (hrtimer_interrupt)
  676. [...]
  677. sshd-4261 0d.H3 81us : tick_program_event (hrtimer_interrupt)
  678. sshd-4261 0d.H3 82us : ktime_get (tick_program_event)
  679. sshd-4261 0d.H3 82us : ktime_get_ts (ktime_get)
  680. sshd-4261 0d.H3 83us : getnstimeofday (ktime_get_ts)
  681. sshd-4261 0d.H3 83us : set_normalized_timespec (ktime_get_ts)
  682. sshd-4261 0d.H3 84us : clockevents_program_event (tick_program_event)
  683. sshd-4261 0d.H3 84us : lapic_next_event (clockevents_program_event)
  684. sshd-4261 0d.H3 85us : irq_exit (smp_apic_timer_interrupt)
  685. sshd-4261 0d.H3 85us : sub_preempt_count (irq_exit)
  686. sshd-4261 0d.s4 86us : sub_preempt_count (irq_exit)
  687. sshd-4261 0d.s3 86us : add_preempt_count (__local_bh_disable)
  688. [...]
  689. sshd-4261 0d.s1 98us : sub_preempt_count (net_rx_action)
  690. sshd-4261 0d.s. 99us : add_preempt_count (_spin_lock_irq)
  691. sshd-4261 0d.s1 99us+: _spin_unlock_irq (run_timer_softirq)
  692. sshd-4261 0d.s. 104us : _local_bh_enable (__do_softirq)
  693. sshd-4261 0d.s. 104us : sub_preempt_count (_local_bh_enable)
  694. sshd-4261 0d.s. 105us : _local_bh_enable (__do_softirq)
  695. sshd-4261 0d.s1 105us : trace_preempt_on (__do_softirq)
  696. This is a very interesting trace. It started with the preemption
  697. of the ls task. We see that the task had the "need_resched" bit
  698. set via the 'N' in the trace. Interrupts were disabled before
  699. the spin_lock at the beginning of the trace. We see that a
  700. schedule took place to run sshd. When the interrupts were
  701. enabled, we took an interrupt. On return from the interrupt
  702. handler, the softirq ran. We took another interrupt while
  703. running the softirq as we see from the capital 'H'.
  704. wakeup
  705. ------
  706. In a Real-Time environment it is very important to know the
  707. wakeup time it takes for the highest priority task that is woken
  708. up to the time that it executes. This is also known as "schedule
  709. latency". I stress the point that this is about RT tasks. It is
  710. also important to know the scheduling latency of non-RT tasks,
  711. but the average schedule latency is better for non-RT tasks.
  712. Tools like LatencyTop are more appropriate for such
  713. measurements.
  714. Real-Time environments are interested in the worst case latency.
  715. That is the longest latency it takes for something to happen,
  716. and not the average. We can have a very fast scheduler that may
  717. only have a large latency once in a while, but that would not
  718. work well with Real-Time tasks. The wakeup tracer was designed
  719. to record the worst case wakeups of RT tasks. Non-RT tasks are
  720. not recorded because the tracer only records one worst case and
  721. tracing non-RT tasks that are unpredictable will overwrite the
  722. worst case latency of RT tasks.
  723. Since this tracer only deals with RT tasks, we will run this
  724. slightly differently than we did with the previous tracers.
  725. Instead of performing an 'ls', we will run 'sleep 1' under
  726. 'chrt' which changes the priority of the task.
  727. # echo wakeup > current_tracer
  728. # echo latency-format > trace_options
  729. # echo 0 > tracing_max_latency
  730. # echo 1 > tracing_enabled
  731. # chrt -f 5 sleep 1
  732. # echo 0 > tracing_enabled
  733. # cat trace
  734. # tracer: wakeup
  735. #
  736. wakeup latency trace v1.1.5 on 2.6.26-rc8
  737. --------------------------------------------------------------------
  738. latency: 4 us, #2/2, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  739. -----------------
  740. | task: sleep-4901 (uid:0 nice:0 policy:1 rt_prio:5)
  741. -----------------
  742. # _------=> CPU#
  743. # / _-----=> irqs-off
  744. # | / _----=> need-resched
  745. # || / _---=> hardirq/softirq
  746. # ||| / _--=> preempt-depth
  747. # |||| /
  748. # ||||| delay
  749. # cmd pid ||||| time | caller
  750. # \ / ||||| \ | /
  751. <idle>-0 1d.h4 0us+: try_to_wake_up (wake_up_process)
  752. <idle>-0 1d..4 4us : schedule (cpu_idle)
  753. Running this on an idle system, we see that it only took 4
  754. microseconds to perform the task switch. Note, since the trace
  755. marker in the schedule is before the actual "switch", we stop
  756. the tracing when the recorded task is about to schedule in. This
  757. may change if we add a new marker at the end of the scheduler.
  758. Notice that the recorded task is 'sleep' with the PID of 4901
  759. and it has an rt_prio of 5. This priority is user-space priority
  760. and not the internal kernel priority. The policy is 1 for
  761. SCHED_FIFO and 2 for SCHED_RR.
  762. Doing the same with chrt -r 5 and ftrace_enabled set.
  763. # tracer: wakeup
  764. #
  765. wakeup latency trace v1.1.5 on 2.6.26-rc8
  766. --------------------------------------------------------------------
  767. latency: 50 us, #60/60, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  768. -----------------
  769. | task: sleep-4068 (uid:0 nice:0 policy:2 rt_prio:5)
  770. -----------------
  771. # _------=> CPU#
  772. # / _-----=> irqs-off
  773. # | / _----=> need-resched
  774. # || / _---=> hardirq/softirq
  775. # ||| / _--=> preempt-depth
  776. # |||| /
  777. # ||||| delay
  778. # cmd pid ||||| time | caller
  779. # \ / ||||| \ | /
  780. ksoftirq-7 1d.H3 0us : try_to_wake_up (wake_up_process)
  781. ksoftirq-7 1d.H4 1us : sub_preempt_count (marker_probe_cb)
  782. ksoftirq-7 1d.H3 2us : check_preempt_wakeup (try_to_wake_up)
  783. ksoftirq-7 1d.H3 3us : update_curr (check_preempt_wakeup)
  784. ksoftirq-7 1d.H3 4us : calc_delta_mine (update_curr)
  785. ksoftirq-7 1d.H3 5us : __resched_task (check_preempt_wakeup)
  786. ksoftirq-7 1d.H3 6us : task_wake_up_rt (try_to_wake_up)
  787. ksoftirq-7 1d.H3 7us : _spin_unlock_irqrestore (try_to_wake_up)
  788. [...]
  789. ksoftirq-7 1d.H2 17us : irq_exit (smp_apic_timer_interrupt)
  790. ksoftirq-7 1d.H2 18us : sub_preempt_count (irq_exit)
  791. ksoftirq-7 1d.s3 19us : sub_preempt_count (irq_exit)
  792. ksoftirq-7 1..s2 20us : rcu_process_callbacks (__do_softirq)
  793. [...]
  794. ksoftirq-7 1..s2 26us : __rcu_process_callbacks (rcu_process_callbacks)
  795. ksoftirq-7 1d.s2 27us : _local_bh_enable (__do_softirq)
  796. ksoftirq-7 1d.s2 28us : sub_preempt_count (_local_bh_enable)
  797. ksoftirq-7 1.N.3 29us : sub_preempt_count (ksoftirqd)
  798. ksoftirq-7 1.N.2 30us : _cond_resched (ksoftirqd)
  799. ksoftirq-7 1.N.2 31us : __cond_resched (_cond_resched)
  800. ksoftirq-7 1.N.2 32us : add_preempt_count (__cond_resched)
  801. ksoftirq-7 1.N.2 33us : schedule (__cond_resched)
  802. ksoftirq-7 1.N.2 33us : add_preempt_count (schedule)
  803. ksoftirq-7 1.N.3 34us : hrtick_clear (schedule)
  804. ksoftirq-7 1dN.3 35us : _spin_lock (schedule)
  805. ksoftirq-7 1dN.3 36us : add_preempt_count (_spin_lock)
  806. ksoftirq-7 1d..4 37us : put_prev_task_fair (schedule)
  807. ksoftirq-7 1d..4 38us : update_curr (put_prev_task_fair)
  808. [...]
  809. ksoftirq-7 1d..5 47us : _spin_trylock (tracing_record_cmdline)
  810. ksoftirq-7 1d..5 48us : add_preempt_count (_spin_trylock)
  811. ksoftirq-7 1d..6 49us : _spin_unlock (tracing_record_cmdline)
  812. ksoftirq-7 1d..6 49us : sub_preempt_count (_spin_unlock)
  813. ksoftirq-7 1d..4 50us : schedule (__cond_resched)
  814. The interrupt went off while running ksoftirqd. This task runs
  815. at SCHED_OTHER. Why did not we see the 'N' set early? This may
  816. be a harmless bug with x86_32 and 4K stacks. On x86_32 with 4K
  817. stacks configured, the interrupt and softirq run with their own
  818. stack. Some information is held on the top of the task's stack
  819. (need_resched and preempt_count are both stored there). The
  820. setting of the NEED_RESCHED bit is done directly to the task's
  821. stack, but the reading of the NEED_RESCHED is done by looking at
  822. the current stack, which in this case is the stack for the hard
  823. interrupt. This hides the fact that NEED_RESCHED has been set.
  824. We do not see the 'N' until we switch back to the task's
  825. assigned stack.
  826. function
  827. --------
  828. This tracer is the function tracer. Enabling the function tracer
  829. can be done from the debug file system. Make sure the
  830. ftrace_enabled is set; otherwise this tracer is a nop.
  831. # sysctl kernel.ftrace_enabled=1
  832. # echo function > current_tracer
  833. # echo 1 > tracing_enabled
  834. # usleep 1
  835. # echo 0 > tracing_enabled
  836. # cat trace
  837. # tracer: function
  838. #
  839. # TASK-PID CPU# TIMESTAMP FUNCTION
  840. # | | | | |
  841. bash-4003 [00] 123.638713: finish_task_switch <-schedule
  842. bash-4003 [00] 123.638714: _spin_unlock_irq <-finish_task_switch
  843. bash-4003 [00] 123.638714: sub_preempt_count <-_spin_unlock_irq
  844. bash-4003 [00] 123.638715: hrtick_set <-schedule
  845. bash-4003 [00] 123.638715: _spin_lock_irqsave <-hrtick_set
  846. bash-4003 [00] 123.638716: add_preempt_count <-_spin_lock_irqsave
  847. bash-4003 [00] 123.638716: _spin_unlock_irqrestore <-hrtick_set
  848. bash-4003 [00] 123.638717: sub_preempt_count <-_spin_unlock_irqrestore
  849. bash-4003 [00] 123.638717: hrtick_clear <-hrtick_set
  850. bash-4003 [00] 123.638718: sub_preempt_count <-schedule
  851. bash-4003 [00] 123.638718: sub_preempt_count <-preempt_schedule
  852. bash-4003 [00] 123.638719: wait_for_completion <-__stop_machine_run
  853. bash-4003 [00] 123.638719: wait_for_common <-wait_for_completion
  854. bash-4003 [00] 123.638720: _spin_lock_irq <-wait_for_common
  855. bash-4003 [00] 123.638720: add_preempt_count <-_spin_lock_irq
  856. [...]
  857. Note: function tracer uses ring buffers to store the above
  858. entries. The newest data may overwrite the oldest data.
  859. Sometimes using echo to stop the trace is not sufficient because
  860. the tracing could have overwritten the data that you wanted to
  861. record. For this reason, it is sometimes better to disable
  862. tracing directly from a program. This allows you to stop the
  863. tracing at the point that you hit the part that you are
  864. interested in. To disable the tracing directly from a C program,
  865. something like following code snippet can be used:
  866. int trace_fd;
  867. [...]
  868. int main(int argc, char *argv[]) {
  869. [...]
  870. trace_fd = open(tracing_file("tracing_enabled"), O_WRONLY);
  871. [...]
  872. if (condition_hit()) {
  873. write(trace_fd, "0", 1);
  874. }
  875. [...]
  876. }
  877. Single thread tracing
  878. ---------------------
  879. By writing into set_ftrace_pid you can trace a
  880. single thread. For example:
  881. # cat set_ftrace_pid
  882. no pid
  883. # echo 3111 > set_ftrace_pid
  884. # cat set_ftrace_pid
  885. 3111
  886. # echo function > current_tracer
  887. # cat trace | head
  888. # tracer: function
  889. #
  890. # TASK-PID CPU# TIMESTAMP FUNCTION
  891. # | | | | |
  892. yum-updatesd-3111 [003] 1637.254676: finish_task_switch <-thread_return
  893. yum-updatesd-3111 [003] 1637.254681: hrtimer_cancel <-schedule_hrtimeout_range
  894. yum-updatesd-3111 [003] 1637.254682: hrtimer_try_to_cancel <-hrtimer_cancel
  895. yum-updatesd-3111 [003] 1637.254683: lock_hrtimer_base <-hrtimer_try_to_cancel
  896. yum-updatesd-3111 [003] 1637.254685: fget_light <-do_sys_poll
  897. yum-updatesd-3111 [003] 1637.254686: pipe_poll <-do_sys_poll
  898. # echo -1 > set_ftrace_pid
  899. # cat trace |head
  900. # tracer: function
  901. #
  902. # TASK-PID CPU# TIMESTAMP FUNCTION
  903. # | | | | |
  904. ##### CPU 3 buffer started ####
  905. yum-updatesd-3111 [003] 1701.957688: free_poll_entry <-poll_freewait
  906. yum-updatesd-3111 [003] 1701.957689: remove_wait_queue <-free_poll_entry
  907. yum-updatesd-3111 [003] 1701.957691: fput <-free_poll_entry
  908. yum-updatesd-3111 [003] 1701.957692: audit_syscall_exit <-sysret_audit
  909. yum-updatesd-3111 [003] 1701.957693: path_put <-audit_syscall_exit
  910. If you want to trace a function when executing, you could use
  911. something like this simple program:
  912. #include <stdio.h>
  913. #include <stdlib.h>
  914. #include <sys/types.h>
  915. #include <sys/stat.h>
  916. #include <fcntl.h>
  917. #include <unistd.h>
  918. #include <string.h>
  919. #define _STR(x) #x
  920. #define STR(x) _STR(x)
  921. #define MAX_PATH 256
  922. const char *find_debugfs(void)
  923. {
  924. static char debugfs[MAX_PATH+1];
  925. static int debugfs_found;
  926. char type[100];
  927. FILE *fp;
  928. if (debugfs_found)
  929. return debugfs;
  930. if ((fp = fopen("/proc/mounts","r")) == NULL) {
  931. perror("/proc/mounts");
  932. return NULL;
  933. }
  934. while (fscanf(fp, "%*s %"
  935. STR(MAX_PATH)
  936. "s %99s %*s %*d %*d\n",
  937. debugfs, type) == 2) {
  938. if (strcmp(type, "debugfs") == 0)
  939. break;
  940. }
  941. fclose(fp);
  942. if (strcmp(type, "debugfs") != 0) {
  943. fprintf(stderr, "debugfs not mounted");
  944. return NULL;
  945. }
  946. strcat(debugfs, "/tracing/");
  947. debugfs_found = 1;
  948. return debugfs;
  949. }
  950. const char *tracing_file(const char *file_name)
  951. {
  952. static char trace_file[MAX_PATH+1];
  953. snprintf(trace_file, MAX_PATH, "%s/%s", find_debugfs(), file_name);
  954. return trace_file;
  955. }
  956. int main (int argc, char **argv)
  957. {
  958. if (argc < 1)
  959. exit(-1);
  960. if (fork() > 0) {
  961. int fd, ffd;
  962. char line[64];
  963. int s;
  964. ffd = open(tracing_file("current_tracer"), O_WRONLY);
  965. if (ffd < 0)
  966. exit(-1);
  967. write(ffd, "nop", 3);
  968. fd = open(tracing_file("set_ftrace_pid"), O_WRONLY);
  969. s = sprintf(line, "%d\n", getpid());
  970. write(fd, line, s);
  971. write(ffd, "function", 8);
  972. close(fd);
  973. close(ffd);
  974. execvp(argv[1], argv+1);
  975. }
  976. return 0;
  977. }
  978. hw-branch-tracer (x86 only)
  979. ---------------------------
  980. This tracer uses the x86 last branch tracing hardware feature to
  981. collect a branch trace on all cpus with relatively low overhead.
  982. The tracer uses a fixed-size circular buffer per cpu and only
  983. traces ring 0 branches. The trace file dumps that buffer in the
  984. following format:
  985. # tracer: hw-branch-tracer
  986. #
  987. # CPU# TO <- FROM
  988. 0 scheduler_tick+0xb5/0x1bf <- task_tick_idle+0x5/0x6
  989. 2 run_posix_cpu_timers+0x2b/0x72a <- run_posix_cpu_timers+0x25/0x72a
  990. 0 scheduler_tick+0x139/0x1bf <- scheduler_tick+0xed/0x1bf
  991. 0 scheduler_tick+0x17c/0x1bf <- scheduler_tick+0x148/0x1bf
  992. 2 run_posix_cpu_timers+0x9e/0x72a <- run_posix_cpu_timers+0x5e/0x72a
  993. 0 scheduler_tick+0x1b6/0x1bf <- scheduler_tick+0x1aa/0x1bf
  994. The tracer may be used to dump the trace for the oops'ing cpu on
  995. a kernel oops into the system log. To enable this,
  996. ftrace_dump_on_oops must be set. To set ftrace_dump_on_oops, one
  997. can either use the sysctl function or set it via the proc system
  998. interface.
  999. sysctl kernel.ftrace_dump_on_oops=n
  1000. or
  1001. echo n > /proc/sys/kernel/ftrace_dump_on_oops
  1002. If n = 1, ftrace will dump buffers of all CPUs, if n = 2 ftrace will
  1003. only dump the buffer of the CPU that triggered the oops.
  1004. Here's an example of such a dump after a null pointer
  1005. dereference in a kernel module:
  1006. [57848.105921] BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
  1007. [57848.106019] IP: [<ffffffffa0000006>] open+0x6/0x14 [oops]
  1008. [57848.106019] PGD 2354e9067 PUD 2375e7067 PMD 0
  1009. [57848.106019] Oops: 0002 [#1] SMP
  1010. [57848.106019] last sysfs file: /sys/devices/pci0000:00/0000:00:1e.0/0000:20:05.0/local_cpus
  1011. [57848.106019] Dumping ftrace buffer:
  1012. [57848.106019] ---------------------------------
  1013. [...]
  1014. [57848.106019] 0 chrdev_open+0xe6/0x165 <- cdev_put+0x23/0x24
  1015. [57848.106019] 0 chrdev_open+0x117/0x165 <- chrdev_open+0xfa/0x165
  1016. [57848.106019] 0 chrdev_open+0x120/0x165 <- chrdev_open+0x11c/0x165
  1017. [57848.106019] 0 chrdev_open+0x134/0x165 <- chrdev_open+0x12b/0x165
  1018. [57848.106019] 0 open+0x0/0x14 [oops] <- chrdev_open+0x144/0x165
  1019. [57848.106019] 0 page_fault+0x0/0x30 <- open+0x6/0x14 [oops]
  1020. [57848.106019] 0 error_entry+0x0/0x5b <- page_fault+0x4/0x30
  1021. [57848.106019] 0 error_kernelspace+0x0/0x31 <- error_entry+0x59/0x5b
  1022. [57848.106019] 0 error_sti+0x0/0x1 <- error_kernelspace+0x2d/0x31
  1023. [57848.106019] 0 page_fault+0x9/0x30 <- error_sti+0x0/0x1
  1024. [57848.106019] 0 do_page_fault+0x0/0x881 <- page_fault+0x1a/0x30
  1025. [...]
  1026. [57848.106019] 0 do_page_fault+0x66b/0x881 <- is_prefetch+0x1ee/0x1f2
  1027. [57848.106019] 0 do_page_fault+0x6e0/0x881 <- do_page_fault+0x67a/0x881
  1028. [57848.106019] 0 oops_begin+0x0/0x96 <- do_page_fault+0x6e0/0x881
  1029. [57848.106019] 0 trace_hw_branch_oops+0x0/0x2d <- oops_begin+0x9/0x96
  1030. [...]
  1031. [57848.106019] 0 ds_suspend_bts+0x2a/0xe3 <- ds_suspend_bts+0x1a/0xe3
  1032. [57848.106019] ---------------------------------
  1033. [57848.106019] CPU 0
  1034. [57848.106019] Modules linked in: oops
  1035. [57848.106019] Pid: 5542, comm: cat Tainted: G W 2.6.28 #23
  1036. [57848.106019] RIP: 0010:[<ffffffffa0000006>] [<ffffffffa0000006>] open+0x6/0x14 [oops]
  1037. [57848.106019] RSP: 0018:ffff880235457d48 EFLAGS: 00010246
  1038. [...]
  1039. function graph tracer
  1040. ---------------------------
  1041. This tracer is similar to the function tracer except that it
  1042. probes a function on its entry and its exit. This is done by
  1043. using a dynamically allocated stack of return addresses in each
  1044. task_struct. On function entry the tracer overwrites the return
  1045. address of each function traced to set a custom probe. Thus the
  1046. original return address is stored on the stack of return address
  1047. in the task_struct.
  1048. Probing on both ends of a function leads to special features
  1049. such as:
  1050. - measure of a function's time execution
  1051. - having a reliable call stack to draw function calls graph
  1052. This tracer is useful in several situations:
  1053. - you want to find the reason of a strange kernel behavior and
  1054. need to see what happens in detail on any areas (or specific
  1055. ones).
  1056. - you are experiencing weird latencies but it's difficult to
  1057. find its origin.
  1058. - you want to find quickly which path is taken by a specific
  1059. function
  1060. - you just want to peek inside a working kernel and want to see
  1061. what happens there.
  1062. # tracer: function_graph
  1063. #
  1064. # CPU DURATION FUNCTION CALLS
  1065. # | | | | | | |
  1066. 0) | sys_open() {
  1067. 0) | do_sys_open() {
  1068. 0) | getname() {
  1069. 0) | kmem_cache_alloc() {
  1070. 0) 1.382 us | __might_sleep();
  1071. 0) 2.478 us | }
  1072. 0) | strncpy_from_user() {
  1073. 0) | might_fault() {
  1074. 0) 1.389 us | __might_sleep();
  1075. 0) 2.553 us | }
  1076. 0) 3.807 us | }
  1077. 0) 7.876 us | }
  1078. 0) | alloc_fd() {
  1079. 0) 0.668 us | _spin_lock();
  1080. 0) 0.570 us | expand_files();
  1081. 0) 0.586 us | _spin_unlock();
  1082. There are several columns that can be dynamically
  1083. enabled/disabled. You can use every combination of options you
  1084. want, depending on your needs.
  1085. - The cpu number on which the function executed is default
  1086. enabled. It is sometimes better to only trace one cpu (see
  1087. tracing_cpu_mask file) or you might sometimes see unordered
  1088. function calls while cpu tracing switch.
  1089. hide: echo nofuncgraph-cpu > trace_options
  1090. show: echo funcgraph-cpu > trace_options
  1091. - The duration (function's time of execution) is displayed on
  1092. the closing bracket line of a function or on the same line
  1093. than the current function in case of a leaf one. It is default
  1094. enabled.
  1095. hide: echo nofuncgraph-duration > trace_options
  1096. show: echo funcgraph-duration > trace_options
  1097. - The overhead field precedes the duration field in case of
  1098. reached duration thresholds.
  1099. hide: echo nofuncgraph-overhead > trace_options
  1100. show: echo funcgraph-overhead > trace_options
  1101. depends on: funcgraph-duration
  1102. ie:
  1103. 0) | up_write() {
  1104. 0) 0.646 us | _spin_lock_irqsave();
  1105. 0) 0.684 us | _spin_unlock_irqrestore();
  1106. 0) 3.123 us | }
  1107. 0) 0.548 us | fput();
  1108. 0) + 58.628 us | }
  1109. [...]
  1110. 0) | putname() {
  1111. 0) | kmem_cache_free() {
  1112. 0) 0.518 us | __phys_addr();
  1113. 0) 1.757 us | }
  1114. 0) 2.861 us | }
  1115. 0) ! 115.305 us | }
  1116. 0) ! 116.402 us | }
  1117. + means that the function exceeded 10 usecs.
  1118. ! means that the function exceeded 100 usecs.
  1119. - The task/pid field displays the thread cmdline and pid which
  1120. executed the function. It is default disabled.
  1121. hide: echo nofuncgraph-proc > trace_options
  1122. show: echo funcgraph-proc > trace_options
  1123. ie:
  1124. # tracer: function_graph
  1125. #
  1126. # CPU TASK/PID DURATION FUNCTION CALLS
  1127. # | | | | | | | | |
  1128. 0) sh-4802 | | d_free() {
  1129. 0) sh-4802 | | call_rcu() {
  1130. 0) sh-4802 | | __call_rcu() {
  1131. 0) sh-4802 | 0.616 us | rcu_process_gp_end();
  1132. 0) sh-4802 | 0.586 us | check_for_new_grace_period();
  1133. 0) sh-4802 | 2.899 us | }
  1134. 0) sh-4802 | 4.040 us | }
  1135. 0) sh-4802 | 5.151 us | }
  1136. 0) sh-4802 | + 49.370 us | }
  1137. - The absolute time field is an absolute timestamp given by the
  1138. system clock since it started. A snapshot of this time is
  1139. given on each entry/exit of functions
  1140. hide: echo nofuncgraph-abstime > trace_options
  1141. show: echo funcgraph-abstime > trace_options
  1142. ie:
  1143. #
  1144. # TIME CPU DURATION FUNCTION CALLS
  1145. # | | | | | | | |
  1146. 360.774522 | 1) 0.541 us | }
  1147. 360.774522 | 1) 4.663 us | }
  1148. 360.774523 | 1) 0.541 us | __wake_up_bit();
  1149. 360.774524 | 1) 6.796 us | }
  1150. 360.774524 | 1) 7.952 us | }
  1151. 360.774525 | 1) 9.063 us | }
  1152. 360.774525 | 1) 0.615 us | journal_mark_dirty();
  1153. 360.774527 | 1) 0.578 us | __brelse();
  1154. 360.774528 | 1) | reiserfs_prepare_for_journal() {
  1155. 360.774528 | 1) | unlock_buffer() {
  1156. 360.774529 | 1) | wake_up_bit() {
  1157. 360.774529 | 1) | bit_waitqueue() {
  1158. 360.774530 | 1) 0.594 us | __phys_addr();
  1159. You can put some comments on specific functions by using
  1160. trace_printk() For example, if you want to put a comment inside
  1161. the __might_sleep() function, you just have to include
  1162. <linux/ftrace.h> and call trace_printk() inside __might_sleep()
  1163. trace_printk("I'm a comment!\n")
  1164. will produce:
  1165. 1) | __might_sleep() {
  1166. 1) | /* I'm a comment! */
  1167. 1) 1.449 us | }
  1168. You might find other useful features for this tracer in the
  1169. following "dynamic ftrace" section such as tracing only specific
  1170. functions or tasks.
  1171. dynamic ftrace
  1172. --------------
  1173. If CONFIG_DYNAMIC_FTRACE is set, the system will run with
  1174. virtually no overhead when function tracing is disabled. The way
  1175. this works is the mcount function call (placed at the start of
  1176. every kernel function, produced by the -pg switch in gcc),
  1177. starts of pointing to a simple return. (Enabling FTRACE will
  1178. include the -pg switch in the compiling of the kernel.)
  1179. At compile time every C file object is run through the
  1180. recordmcount.pl script (located in the scripts directory). This
  1181. script will process the C object using objdump to find all the
  1182. locations in the .text section that call mcount. (Note, only the
  1183. .text section is processed, since processing other sections like
  1184. .init.text may cause races due to those sections being freed).
  1185. A new section called "__mcount_loc" is created that holds
  1186. references to all the mcount call sites in the .text section.
  1187. This section is compiled back into the original object. The
  1188. final linker will add all these references into a single table.
  1189. On boot up, before SMP is initialized, the dynamic ftrace code
  1190. scans this table and updates all the locations into nops. It
  1191. also records the locations, which are added to the
  1192. available_filter_functions list. Modules are processed as they
  1193. are loaded and before they are executed. When a module is
  1194. unloaded, it also removes its functions from the ftrace function
  1195. list. This is automatic in the module unload code, and the
  1196. module author does not need to worry about it.
  1197. When tracing is enabled, kstop_machine is called to prevent
  1198. races with the CPUS executing code being modified (which can
  1199. cause the CPU to do undesirable things), and the nops are
  1200. patched back to calls. But this time, they do not call mcount
  1201. (which is just a function stub). They now call into the ftrace
  1202. infrastructure.
  1203. One special side-effect to the recording of the functions being
  1204. traced is that we can now selectively choose which functions we
  1205. wish to trace and which ones we want the mcount calls to remain
  1206. as nops.
  1207. Two files are used, one for enabling and one for disabling the
  1208. tracing of specified functions. They are:
  1209. set_ftrace_filter
  1210. and
  1211. set_ftrace_notrace
  1212. A list of available functions that you can add to these files is
  1213. listed in:
  1214. available_filter_functions
  1215. # cat available_filter_functions
  1216. put_prev_task_idle
  1217. kmem_cache_create
  1218. pick_next_task_rt
  1219. get_online_cpus
  1220. pick_next_task_fair
  1221. mutex_lock
  1222. [...]
  1223. If I am only interested in sys_nanosleep and hrtimer_interrupt:
  1224. # echo sys_nanosleep hrtimer_interrupt \
  1225. > set_ftrace_filter
  1226. # echo function > current_tracer
  1227. # echo 1 > tracing_enabled
  1228. # usleep 1
  1229. # echo 0 > tracing_enabled
  1230. # cat trace
  1231. # tracer: ftrace
  1232. #
  1233. # TASK-PID CPU# TIMESTAMP FUNCTION
  1234. # | | | | |
  1235. usleep-4134 [00] 1317.070017: hrtimer_interrupt <-smp_apic_timer_interrupt
  1236. usleep-4134 [00] 1317.070111: sys_nanosleep <-syscall_call
  1237. <idle>-0 [00] 1317.070115: hrtimer_interrupt <-smp_apic_timer_interrupt
  1238. To see which functions are being traced, you can cat the file:
  1239. # cat set_ftrace_filter
  1240. hrtimer_interrupt
  1241. sys_nanosleep
  1242. Perhaps this is not enough. The filters also allow simple wild
  1243. cards. Only the following are currently available
  1244. <match>* - will match functions that begin with <match>
  1245. *<match> - will match functions that end with <match>
  1246. *<match>* - will match functions that have <match> in it
  1247. These are the only wild cards which are supported.
  1248. <match>*<match> will not work.
  1249. Note: It is better to use quotes to enclose the wild cards,
  1250. otherwise the shell may expand the parameters into names
  1251. of files in the local directory.
  1252. # echo 'hrtimer_*' > set_ftrace_filter
  1253. Produces:
  1254. # tracer: ftrace
  1255. #
  1256. # TASK-PID CPU# TIMESTAMP FUNCTION
  1257. # | | | | |
  1258. bash-4003 [00] 1480.611794: hrtimer_init <-copy_process
  1259. bash-4003 [00] 1480.611941: hrtimer_start <-hrtick_set
  1260. bash-4003 [00] 1480.611956: hrtimer_cancel <-hrtick_clear
  1261. bash-4003 [00] 1480.611956: hrtimer_try_to_cancel <-hrtimer_cancel
  1262. <idle>-0 [00] 1480.612019: hrtimer_get_next_event <-get_next_timer_interrupt
  1263. <idle>-0 [00] 1480.612025: hrtimer_get_next_event <-get_next_timer_interrupt
  1264. <idle>-0 [00] 1480.612032: hrtimer_get_next_event <-get_next_timer_interrupt
  1265. <idle>-0 [00] 1480.612037: hrtimer_get_next_event <-get_next_timer_interrupt
  1266. <idle>-0 [00] 1480.612382: hrtimer_get_next_event <-get_next_timer_interrupt
  1267. Notice that we lost the sys_nanosleep.
  1268. # cat set_ftrace_filter
  1269. hrtimer_run_queues
  1270. hrtimer_run_pending
  1271. hrtimer_init
  1272. hrtimer_cancel
  1273. hrtimer_try_to_cancel
  1274. hrtimer_forward
  1275. hrtimer_start
  1276. hrtimer_reprogram
  1277. hrtimer_force_reprogram
  1278. hrtimer_get_next_event
  1279. hrtimer_interrupt
  1280. hrtimer_nanosleep
  1281. hrtimer_wakeup
  1282. hrtimer_get_remaining
  1283. hrtimer_get_res
  1284. hrtimer_init_sleeper
  1285. This is because the '>' and '>>' act just like they do in bash.
  1286. To rewrite the filters, use '>'
  1287. To append to the filters, use '>>'
  1288. To clear out a filter so that all functions will be recorded
  1289. again:
  1290. # echo > set_ftrace_filter
  1291. # cat set_ftrace_filter
  1292. #
  1293. Again, now we want to append.
  1294. # echo sys_nanosleep > set_ftrace_filter
  1295. # cat set_ftrace_filter
  1296. sys_nanosleep
  1297. # echo 'hrtimer_*' >> set_ftrace_filter
  1298. # cat set_ftrace_filter
  1299. hrtimer_run_queues
  1300. hrtimer_run_pending
  1301. hrtimer_init
  1302. hrtimer_cancel
  1303. hrtimer_try_to_cancel
  1304. hrtimer_forward
  1305. hrtimer_start
  1306. hrtimer_reprogram
  1307. hrtimer_force_reprogram
  1308. hrtimer_get_next_event
  1309. hrtimer_interrupt
  1310. sys_nanosleep
  1311. hrtimer_nanosleep
  1312. hrtimer_wakeup
  1313. hrtimer_get_remaining
  1314. hrtimer_get_res
  1315. hrtimer_init_sleeper
  1316. The set_ftrace_notrace prevents those functions from being
  1317. traced.
  1318. # echo '*preempt*' '*lock*' > set_ftrace_notrace
  1319. Produces:
  1320. # tracer: ftrace
  1321. #
  1322. # TASK-PID CPU# TIMESTAMP FUNCTION
  1323. # | | | | |
  1324. bash-4043 [01] 115.281644: finish_task_switch <-schedule
  1325. bash-4043 [01] 115.281645: hrtick_set <-schedule
  1326. bash-4043 [01] 115.281645: hrtick_clear <-hrtick_set
  1327. bash-4043 [01] 115.281646: wait_for_completion <-__stop_machine_run
  1328. bash-4043 [01] 115.281647: wait_for_common <-wait_for_completion
  1329. bash-4043 [01] 115.281647: kthread_stop <-stop_machine_run
  1330. bash-4043 [01] 115.281648: init_waitqueue_head <-kthread_stop
  1331. bash-4043 [01] 115.281648: wake_up_process <-kthread_stop
  1332. bash-4043 [01] 115.281649: try_to_wake_up <-wake_up_process
  1333. We can see that there's no more lock or preempt tracing.
  1334. Dynamic ftrace with the function graph tracer
  1335. ---------------------------------------------
  1336. Although what has been explained above concerns both the
  1337. function tracer and the function-graph-tracer, there are some
  1338. special features only available in the function-graph tracer.
  1339. If you want to trace only one function and all of its children,
  1340. you just have to echo its name into set_graph_function:
  1341. echo __do_fault > set_graph_function
  1342. will produce the following "expanded" trace of the __do_fault()
  1343. function:
  1344. 0) | __do_fault() {
  1345. 0) | filemap_fault() {
  1346. 0) | find_lock_page() {
  1347. 0) 0.804 us | find_get_page();
  1348. 0) | __might_sleep() {
  1349. 0) 1.329 us | }
  1350. 0) 3.904 us | }
  1351. 0) 4.979 us | }
  1352. 0) 0.653 us | _spin_lock();
  1353. 0) 0.578 us | page_add_file_rmap();
  1354. 0) 0.525 us | native_set_pte_at();
  1355. 0) 0.585 us | _spin_unlock();
  1356. 0) | unlock_page() {
  1357. 0) 0.541 us | page_waitqueue();
  1358. 0) 0.639 us | __wake_up_bit();
  1359. 0) 2.786 us | }
  1360. 0) + 14.237 us | }
  1361. 0) | __do_fault() {
  1362. 0) | filemap_fault() {
  1363. 0) | find_lock_page() {
  1364. 0) 0.698 us | find_get_page();
  1365. 0) | __might_sleep() {
  1366. 0) 1.412 us | }
  1367. 0) 3.950 us | }
  1368. 0) 5.098 us | }
  1369. 0) 0.631 us | _spin_lock();
  1370. 0) 0.571 us | page_add_file_rmap();
  1371. 0) 0.526 us | native_set_pte_at();
  1372. 0) 0.586 us | _spin_unlock();
  1373. 0) | unlock_page() {
  1374. 0) 0.533 us | page_waitqueue();
  1375. 0) 0.638 us | __wake_up_bit();
  1376. 0) 2.793 us | }
  1377. 0) + 14.012 us | }
  1378. You can also expand several functions at once:
  1379. echo sys_open > set_graph_function
  1380. echo sys_close >> set_graph_function
  1381. Now if you want to go back to trace all functions you can clear
  1382. this special filter via:
  1383. echo > set_graph_function
  1384. Filter commands
  1385. ---------------
  1386. A few commands are supported by the set_ftrace_filter interface.
  1387. Trace commands have the following format:
  1388. <function>:<command>:<parameter>
  1389. The following commands are supported:
  1390. - mod
  1391. This command enables function filtering per module. The
  1392. parameter defines the module. For example, if only the write*
  1393. functions in the ext3 module are desired, run:
  1394. echo 'write*:mod:ext3' > set_ftrace_filter
  1395. This command interacts with the filter in the same way as
  1396. filtering based on function names. Thus, adding more functions
  1397. in a different module is accomplished by appending (>>) to the
  1398. filter file. Remove specific module functions by prepending
  1399. '!':
  1400. echo '!writeback*:mod:ext3' >> set_ftrace_filter
  1401. - traceon/traceoff
  1402. These commands turn tracing on and off when the specified
  1403. functions are hit. The parameter determines how many times the
  1404. tracing system is turned on and off. If unspecified, there is
  1405. no limit. For example, to disable tracing when a schedule bug
  1406. is hit the first 5 times, run:
  1407. echo '__schedule_bug:traceoff:5' > set_ftrace_filter
  1408. These commands are cumulative whether or not they are appended
  1409. to set_ftrace_filter. To remove a command, prepend it by '!'
  1410. and drop the parameter:
  1411. echo '!__schedule_bug:traceoff' > set_ftrace_filter
  1412. trace_pipe
  1413. ----------
  1414. The trace_pipe outputs the same content as the trace file, but
  1415. the effect on the tracing is different. Every read from
  1416. trace_pipe is consumed. This means that subsequent reads will be
  1417. different. The trace is live.
  1418. # echo function > current_tracer
  1419. # cat trace_pipe > /tmp/trace.out &
  1420. [1] 4153
  1421. # echo 1 > tracing_enabled
  1422. # usleep 1
  1423. # echo 0 > tracing_enabled
  1424. # cat trace
  1425. # tracer: function
  1426. #
  1427. # TASK-PID CPU# TIMESTAMP FUNCTION
  1428. # | | | | |
  1429. #
  1430. # cat /tmp/trace.out
  1431. bash-4043 [00] 41.267106: finish_task_switch <-schedule
  1432. bash-4043 [00] 41.267106: hrtick_set <-schedule
  1433. bash-4043 [00] 41.267107: hrtick_clear <-hrtick_set
  1434. bash-4043 [00] 41.267108: wait_for_completion <-__stop_machine_run
  1435. bash-4043 [00] 41.267108: wait_for_common <-wait_for_completion
  1436. bash-4043 [00] 41.267109: kthread_stop <-stop_machine_run
  1437. bash-4043 [00] 41.267109: init_waitqueue_head <-kthread_stop
  1438. bash-4043 [00] 41.267110: wake_up_process <-kthread_stop
  1439. bash-4043 [00] 41.267110: try_to_wake_up <-wake_up_process
  1440. bash-4043 [00] 41.267111: select_task_rq_rt <-try_to_wake_up
  1441. Note, reading the trace_pipe file will block until more input is
  1442. added. By changing the tracer, trace_pipe will issue an EOF. We
  1443. needed to set the function tracer _before_ we "cat" the
  1444. trace_pipe file.
  1445. trace entries
  1446. -------------
  1447. Having too much or not enough data can be troublesome in
  1448. diagnosing an issue in the kernel. The file buffer_size_kb is
  1449. used to modify the size of the internal trace buffers. The
  1450. number listed is the number of entries that can be recorded per
  1451. CPU. To know the full size, multiply the number of possible CPUS
  1452. with the number of entries.
  1453. # cat buffer_size_kb
  1454. 1408 (units kilobytes)
  1455. Note, to modify this, you must have tracing completely disabled.
  1456. To do that, echo "nop" into the current_tracer. If the
  1457. current_tracer is not set to "nop", an EINVAL error will be
  1458. returned.
  1459. # echo nop > current_tracer
  1460. # echo 10000 > buffer_size_kb
  1461. # cat buffer_size_kb
  1462. 10000 (units kilobytes)
  1463. The number of pages which will be allocated is limited to a
  1464. percentage of available memory. Allocating too much will produce
  1465. an error.
  1466. # echo 1000000000000 > buffer_size_kb
  1467. -bash: echo: write error: Cannot allocate memory
  1468. # cat buffer_size_kb
  1469. 85
  1470. -----------
  1471. More details can be found in the source code, in the
  1472. kernel/trace/*.c files.