ftrace.txt 67 KB

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