ftrace.txt 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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. Reviewers: Elias Oltmanns, Randy Dunlap, Andrew Morton,
  7. John Kacur, and David Teigland.
  8. Written for: 2.6.27-rc1
  9. Introduction
  10. ------------
  11. Ftrace is an internal tracer designed to help out developers and
  12. designers of systems to find what is going on inside the kernel.
  13. It can be used for debugging or analyzing latencies and performance
  14. issues that take place outside of user-space.
  15. Although ftrace is the function tracer, it also includes an
  16. infrastructure that allows for other types of tracing. Some of the
  17. tracers that are currently in ftrace include a tracer to trace
  18. context switches, the time it takes for a high priority task to
  19. run after it was woken up, the time interrupts are disabled, and
  20. more (ftrace allows for tracer plugins, which means that the list of
  21. tracers can always grow).
  22. The File System
  23. ---------------
  24. Ftrace uses the debugfs file system to hold the control files as well
  25. as the files to display output.
  26. To mount the debugfs system:
  27. # mkdir /debug
  28. # mount -t debugfs nodev /debug
  29. (Note: it is more common to mount at /sys/kernel/debug, but for simplicity
  30. this document will use /debug)
  31. That's it! (assuming that you have ftrace configured into your kernel)
  32. After mounting the debugfs, you can see a directory called
  33. "tracing". This directory contains the control and output files
  34. of ftrace. Here is a list of some of the key files:
  35. Note: all time values are in microseconds.
  36. current_tracer : This is used to set or display the current tracer
  37. that is configured.
  38. available_tracers : This holds the different types of tracers that
  39. have been compiled into the kernel. The tracers
  40. listed here can be configured by echoing their name
  41. into current_tracer.
  42. tracing_enabled : This sets or displays whether the current_tracer
  43. is activated and tracing or not. Echo 0 into this
  44. file to disable the tracer or 1 to enable it.
  45. trace : This file holds the output of the trace in a human readable
  46. format (described below).
  47. latency_trace : This file shows the same trace but the information
  48. is organized more to display possible latencies
  49. in the system (described below).
  50. trace_pipe : The output is the same as the "trace" file but this
  51. file is meant to be streamed with live tracing.
  52. Reads from this file will block until new data
  53. is retrieved. Unlike the "trace" and "latency_trace"
  54. files, this file is a consumer. This means reading
  55. from this file causes sequential reads to display
  56. more current data. Once data is read from this
  57. file, it is consumed, and will not be read
  58. again with a sequential read. The "trace" and
  59. "latency_trace" files are static, and if the
  60. tracer is not adding more data, they will display
  61. the same information every time they are read.
  62. iter_ctrl : This file lets the user control the amount of data
  63. that is displayed in one of the above output
  64. files.
  65. trace_max_latency : Some of the tracers record the max latency.
  66. For example, the time interrupts are disabled.
  67. This time is saved in this file. The max trace
  68. will also be stored, and displayed by either
  69. "trace" or "latency_trace". A new max trace will
  70. only be recorded if the latency is greater than
  71. the value in this file. (in microseconds)
  72. trace_entries : This sets or displays the number of trace
  73. entries each CPU buffer can hold. The tracer buffers
  74. are the same size for each CPU. The displayed number
  75. is the size of the CPU buffer and not total size. The
  76. trace buffers are allocated in pages (blocks of memory
  77. that the kernel uses for allocation, usually 4 KB in size).
  78. Since each entry is smaller than a page, if the last
  79. allocated page has room for more entries than were
  80. requested, the rest of the page is used to allocate
  81. entries.
  82. This can only be updated when the current_tracer
  83. is set to "none".
  84. NOTE: It is planned on changing the allocated buffers
  85. from being the number of possible CPUS to
  86. the number of online CPUS.
  87. tracing_cpumask : This is a mask that lets the user only trace
  88. on specified CPUS. The format is a hex string
  89. representing the CPUS.
  90. set_ftrace_filter : When dynamic ftrace is configured in (see the
  91. section below "dynamic ftrace"), the code is dynamically
  92. modified (code text rewrite) to disable calling of the
  93. function profiler (mcount). This lets tracing be configured
  94. in with practically no overhead in performance. This also
  95. has a side effect of enabling or disabling specific functions
  96. to be traced. Echoing names of functions into this file
  97. will limit the trace to only those functions.
  98. set_ftrace_notrace: This has an effect opposite to that of
  99. set_ftrace_filter. Any function that is added here will not
  100. be traced. If a function exists in both set_ftrace_filter
  101. and set_ftrace_notrace, the function will _not_ be traced.
  102. available_filter_functions : When a function is encountered the first
  103. time by the dynamic tracer, it is recorded and
  104. later the call is converted into a nop. This file
  105. lists the functions that have been recorded
  106. by the dynamic tracer and these functions can
  107. be used to set the ftrace filter by the above
  108. "set_ftrace_filter" file. (See the section "dynamic ftrace"
  109. below for more details).
  110. The Tracers
  111. -----------
  112. Here is the list of current tracers that may be configured.
  113. ftrace - function tracer that uses mcount to trace all functions.
  114. sched_switch - traces the context switches between tasks.
  115. irqsoff - traces the areas that disable interrupts and saves
  116. the trace with the longest max latency.
  117. See tracing_max_latency. When a new max is recorded,
  118. it replaces the old trace. It is best to view this
  119. trace via the latency_trace file.
  120. preemptoff - Similar to irqsoff but traces and records the amount of
  121. time for which preemption is disabled.
  122. preemptirqsoff - Similar to irqsoff and preemptoff, but traces and
  123. records the largest time for which irqs and/or preemption
  124. is disabled.
  125. wakeup - Traces and records the max latency that it takes for
  126. the highest priority task to get scheduled after
  127. it has been woken up.
  128. none - This is not a tracer. To remove all tracers from tracing
  129. simply echo "none" into current_tracer.
  130. Examples of using the tracer
  131. ----------------------------
  132. Here are typical examples of using the tracers when controlling them only
  133. with the debugfs interface (without using any user-land utilities).
  134. Output format:
  135. --------------
  136. Here is an example of the output format of the file "trace"
  137. --------
  138. # tracer: ftrace
  139. #
  140. # TASK-PID CPU# TIMESTAMP FUNCTION
  141. # | | | | |
  142. bash-4251 [01] 10152.583854: path_put <-path_walk
  143. bash-4251 [01] 10152.583855: dput <-path_put
  144. bash-4251 [01] 10152.583855: _atomic_dec_and_lock <-dput
  145. --------
  146. A header is printed with the tracer name that is represented by the trace.
  147. In this case the tracer is "ftrace". Then a header showing the format. Task
  148. name "bash", the task PID "4251", the CPU that it was running on
  149. "01", the timestamp in <secs>.<usecs> format, the function name that was
  150. traced "path_put" and the parent function that called this function
  151. "path_walk". The timestamp is the time at which the function was
  152. entered.
  153. The sched_switch tracer also includes tracing of task wakeups and
  154. context switches.
  155. ksoftirqd/1-7 [01] 1453.070013: 7:115:R + 2916:115:S
  156. ksoftirqd/1-7 [01] 1453.070013: 7:115:R + 10:115:S
  157. ksoftirqd/1-7 [01] 1453.070013: 7:115:R ==> 10:115:R
  158. events/1-10 [01] 1453.070013: 10:115:S ==> 2916:115:R
  159. kondemand/1-2916 [01] 1453.070013: 2916:115:S ==> 7:115:R
  160. ksoftirqd/1-7 [01] 1453.070013: 7:115:S ==> 0:140:R
  161. Wake ups are represented by a "+" and the context switches are shown as
  162. "==>". The format is:
  163. Context switches:
  164. Previous task Next Task
  165. <pid>:<prio>:<state> ==> <pid>:<prio>:<state>
  166. Wake ups:
  167. Current task Task waking up
  168. <pid>:<prio>:<state> + <pid>:<prio>:<state>
  169. The prio is the internal kernel priority, which is the inverse of the
  170. priority that is usually displayed by user-space tools. Zero represents
  171. the highest priority (99). Prio 100 starts the "nice" priorities with
  172. 100 being equal to nice -20 and 139 being nice 19. The prio "140" is
  173. reserved for the idle task which is the lowest priority thread (pid 0).
  174. Latency trace format
  175. --------------------
  176. For traces that display latency times, the latency_trace file gives
  177. somewhat more information to see why a latency happened. Here is a typical
  178. trace.
  179. # tracer: irqsoff
  180. #
  181. irqsoff latency trace v1.1.5 on 2.6.26-rc8
  182. --------------------------------------------------------------------
  183. latency: 97 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  184. -----------------
  185. | task: swapper-0 (uid:0 nice:0 policy:0 rt_prio:0)
  186. -----------------
  187. => started at: apic_timer_interrupt
  188. => ended at: do_softirq
  189. # _------=> CPU#
  190. # / _-----=> irqs-off
  191. # | / _----=> need-resched
  192. # || / _---=> hardirq/softirq
  193. # ||| / _--=> preempt-depth
  194. # |||| /
  195. # ||||| delay
  196. # cmd pid ||||| time | caller
  197. # \ / ||||| \ | /
  198. <idle>-0 0d..1 0us+: trace_hardirqs_off_thunk (apic_timer_interrupt)
  199. <idle>-0 0d.s. 97us : __do_softirq (do_softirq)
  200. <idle>-0 0d.s1 98us : trace_hardirqs_on (do_softirq)
  201. This shows that the current tracer is "irqsoff" tracing the time for which
  202. interrupts were disabled. It gives the trace version and the version
  203. of the kernel upon which this was executed on (2.6.26-rc8). Then it displays
  204. the max latency in microsecs (97 us). The number of trace entries displayed
  205. and the total number recorded (both are three: #3/3). The type of
  206. preemption that was used (PREEMPT). VP, KP, SP, and HP are always zero
  207. and are reserved for later use. #P is the number of online CPUS (#P:2).
  208. The task is the process that was running when the latency occurred.
  209. (swapper pid: 0).
  210. The start and stop (the functions in which the interrupts were disabled and
  211. enabled respectively) that caused the latencies:
  212. apic_timer_interrupt is where the interrupts were disabled.
  213. do_softirq is where they were enabled again.
  214. The next lines after the header are the trace itself. The header
  215. explains which is which.
  216. cmd: The name of the process in the trace.
  217. pid: The PID of that process.
  218. CPU#: The CPU which the process was running on.
  219. irqs-off: 'd' interrupts are disabled. '.' otherwise.
  220. need-resched: 'N' task need_resched is set, '.' otherwise.
  221. hardirq/softirq:
  222. 'H' - hard irq occurred inside a softirq.
  223. 'h' - hard irq is running
  224. 's' - soft irq is running
  225. '.' - normal context.
  226. preempt-depth: The level of preempt_disabled
  227. The above is mostly meaningful for kernel developers.
  228. time: This differs from the trace file output. The trace file output
  229. includes an absolute timestamp. The timestamp used by the
  230. latency_trace file is relative to the start of the trace.
  231. delay: This is just to help catch your eye a bit better. And
  232. needs to be fixed to be only relative to the same CPU.
  233. The marks are determined by the difference between this
  234. current trace and the next trace.
  235. '!' - greater than preempt_mark_thresh (default 100)
  236. '+' - greater than 1 microsecond
  237. ' ' - less than or equal to 1 microsecond.
  238. The rest is the same as the 'trace' file.
  239. iter_ctrl
  240. ---------
  241. The iter_ctrl file is used to control what gets printed in the trace
  242. output. To see what is available, simply cat the file:
  243. cat /debug/tracing/iter_ctrl
  244. print-parent nosym-offset nosym-addr noverbose noraw nohex nobin \
  245. noblock nostacktrace nosched-tree
  246. To disable one of the options, echo in the option prepended with "no".
  247. echo noprint-parent > /debug/tracing/iter_ctrl
  248. To enable an option, leave off the "no".
  249. echo sym-offset > /debug/tracing/iter_ctrl
  250. Here are the available options:
  251. print-parent - On function traces, display the calling function
  252. as well as the function being traced.
  253. print-parent:
  254. bash-4000 [01] 1477.606694: simple_strtoul <-strict_strtoul
  255. noprint-parent:
  256. bash-4000 [01] 1477.606694: simple_strtoul
  257. sym-offset - Display not only the function name, but also the offset
  258. in the function. For example, instead of seeing just
  259. "ktime_get", you will see "ktime_get+0xb/0x20".
  260. sym-offset:
  261. bash-4000 [01] 1477.606694: simple_strtoul+0x6/0xa0
  262. sym-addr - this will also display the function address as well as
  263. the function name.
  264. sym-addr:
  265. bash-4000 [01] 1477.606694: simple_strtoul <c0339346>
  266. verbose - This deals with the latency_trace file.
  267. bash 4000 1 0 00000000 00010a95 [58127d26] 1720.415ms \
  268. (+0.000ms): simple_strtoul (strict_strtoul)
  269. raw - This will display raw numbers. This option is best for use with
  270. user applications that can translate the raw numbers better than
  271. having it done in the kernel.
  272. hex - Similar to raw, but the numbers will be in a hexadecimal format.
  273. bin - This will print out the formats in raw binary.
  274. block - TBD (needs update)
  275. stacktrace - This is one of the options that changes the trace itself.
  276. When a trace is recorded, so is the stack of functions.
  277. This allows for back traces of trace sites.
  278. sched-tree - TBD (any users??)
  279. sched_switch
  280. ------------
  281. This tracer simply records schedule switches. Here is an example
  282. of how to use it.
  283. # echo sched_switch > /debug/tracing/current_tracer
  284. # echo 1 > /debug/tracing/tracing_enabled
  285. # sleep 1
  286. # echo 0 > /debug/tracing/tracing_enabled
  287. # cat /debug/tracing/trace
  288. # tracer: sched_switch
  289. #
  290. # TASK-PID CPU# TIMESTAMP FUNCTION
  291. # | | | | |
  292. bash-3997 [01] 240.132281: 3997:120:R + 4055:120:R
  293. bash-3997 [01] 240.132284: 3997:120:R ==> 4055:120:R
  294. sleep-4055 [01] 240.132371: 4055:120:S ==> 3997:120:R
  295. bash-3997 [01] 240.132454: 3997:120:R + 4055:120:S
  296. bash-3997 [01] 240.132457: 3997:120:R ==> 4055:120:R
  297. sleep-4055 [01] 240.132460: 4055:120:D ==> 3997:120:R
  298. bash-3997 [01] 240.132463: 3997:120:R + 4055:120:D
  299. bash-3997 [01] 240.132465: 3997:120:R ==> 4055:120:R
  300. <idle>-0 [00] 240.132589: 0:140:R + 4:115:S
  301. <idle>-0 [00] 240.132591: 0:140:R ==> 4:115:R
  302. ksoftirqd/0-4 [00] 240.132595: 4:115:S ==> 0:140:R
  303. <idle>-0 [00] 240.132598: 0:140:R + 4:115:S
  304. <idle>-0 [00] 240.132599: 0:140:R ==> 4:115:R
  305. ksoftirqd/0-4 [00] 240.132603: 4:115:S ==> 0:140:R
  306. sleep-4055 [01] 240.133058: 4055:120:S ==> 3997:120:R
  307. [...]
  308. As we have discussed previously about this format, the header shows
  309. the name of the trace and points to the options. The "FUNCTION"
  310. is a misnomer since here it represents the wake ups and context
  311. switches.
  312. The sched_switch file only lists the wake ups (represented with '+')
  313. and context switches ('==>') with the previous task or current task
  314. first followed by the next task or task waking up. The format for both
  315. of these is PID:KERNEL-PRIO:TASK-STATE. Remember that the KERNEL-PRIO
  316. is the inverse of the actual priority with zero (0) being the highest
  317. priority and the nice values starting at 100 (nice -20). Below is
  318. a quick chart to map the kernel priority to user land priorities.
  319. Kernel priority: 0 to 99 ==> user RT priority 99 to 0
  320. Kernel priority: 100 to 139 ==> user nice -20 to 19
  321. Kernel priority: 140 ==> idle task priority
  322. The task states are:
  323. R - running : wants to run, may not actually be running
  324. S - sleep : process is waiting to be woken up (handles signals)
  325. D - disk sleep (uninterruptible sleep) : process must be woken up
  326. (ignores signals)
  327. T - stopped : process suspended
  328. t - traced : process is being traced (with something like gdb)
  329. Z - zombie : process waiting to be cleaned up
  330. X - unknown
  331. ftrace_enabled
  332. --------------
  333. The following tracers (listed below) give different output depending
  334. on whether or not the sysctl ftrace_enabled is set. To set ftrace_enabled,
  335. one can either use the sysctl function or set it via the proc
  336. file system interface.
  337. sysctl kernel.ftrace_enabled=1
  338. or
  339. echo 1 > /proc/sys/kernel/ftrace_enabled
  340. To disable ftrace_enabled simply replace the '1' with '0' in
  341. the above commands.
  342. When ftrace_enabled is set the tracers will also record the functions
  343. that are within the trace. The descriptions of the tracers
  344. will also show an example with ftrace enabled.
  345. irqsoff
  346. -------
  347. When interrupts are disabled, the CPU can not react to any other
  348. external event (besides NMIs and SMIs). This prevents the timer
  349. interrupt from triggering or the mouse interrupt from letting the
  350. kernel know of a new mouse event. The result is a latency with the
  351. reaction time.
  352. The irqsoff tracer tracks the time for which interrupts are disabled.
  353. When a new maximum latency is hit, the tracer saves the trace leading up
  354. to that latency point so that every time a new maximum is reached, the old
  355. saved trace is discarded and the new trace is saved.
  356. To reset the maximum, echo 0 into tracing_max_latency. Here is an
  357. example:
  358. # echo irqsoff > /debug/tracing/current_tracer
  359. # echo 0 > /debug/tracing/tracing_max_latency
  360. # echo 1 > /debug/tracing/tracing_enabled
  361. # ls -ltr
  362. [...]
  363. # echo 0 > /debug/tracing/tracing_enabled
  364. # cat /debug/tracing/latency_trace
  365. # tracer: irqsoff
  366. #
  367. irqsoff latency trace v1.1.5 on 2.6.26
  368. --------------------------------------------------------------------
  369. latency: 12 us, #3/3, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  370. -----------------
  371. | task: bash-3730 (uid:0 nice:0 policy:0 rt_prio:0)
  372. -----------------
  373. => started at: sys_setpgid
  374. => ended at: sys_setpgid
  375. # _------=> CPU#
  376. # / _-----=> irqs-off
  377. # | / _----=> need-resched
  378. # || / _---=> hardirq/softirq
  379. # ||| / _--=> preempt-depth
  380. # |||| /
  381. # ||||| delay
  382. # cmd pid ||||| time | caller
  383. # \ / ||||| \ | /
  384. bash-3730 1d... 0us : _write_lock_irq (sys_setpgid)
  385. bash-3730 1d..1 1us+: _write_unlock_irq (sys_setpgid)
  386. bash-3730 1d..2 14us : trace_hardirqs_on (sys_setpgid)
  387. Here we see that that we had a latency of 12 microsecs (which is
  388. very good). The _write_lock_irq in sys_setpgid disabled interrupts.
  389. The difference between the 12 and the displayed timestamp 14us occurred
  390. because the clock was incremented between the time of recording the max
  391. latency and the time of 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 enabling
  437. function tracing, we incur an added overhead. This overhead may
  438. extend the latency times. But nevertheless, this trace has provided
  439. some very helpful debugging information.
  440. preemptoff
  441. ----------
  442. When preemption is disabled, we may be able to receive interrupts but
  443. the task cannot be preempted and a higher priority task must wait
  444. for preemption to be enabled again before it can preempt a lower
  445. priority task.
  446. The preemptoff tracer traces the places that disable preemption.
  447. Like the irqsoff tracer, it records the maximum latency for which preemption
  448. was disabled. The control of preemptoff tracer is much like the irqsoff
  449. tracer.
  450. # echo preemptoff > /debug/tracing/current_tracer
  451. # echo 0 > /debug/tracing/tracing_max_latency
  452. # echo 1 > /debug/tracing/tracing_enabled
  453. # ls -ltr
  454. [...]
  455. # echo 0 > /debug/tracing/tracing_enabled
  456. # cat /debug/tracing/latency_trace
  457. # tracer: preemptoff
  458. #
  459. preemptoff latency trace v1.1.5 on 2.6.26-rc8
  460. --------------------------------------------------------------------
  461. latency: 29 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  462. -----------------
  463. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  464. -----------------
  465. => started at: do_IRQ
  466. => ended at: __do_softirq
  467. # _------=> CPU#
  468. # / _-----=> irqs-off
  469. # | / _----=> need-resched
  470. # || / _---=> hardirq/softirq
  471. # ||| / _--=> preempt-depth
  472. # |||| /
  473. # ||||| delay
  474. # cmd pid ||||| time | caller
  475. # \ / ||||| \ | /
  476. sshd-4261 0d.h. 0us+: irq_enter (do_IRQ)
  477. sshd-4261 0d.s. 29us : _local_bh_enable (__do_softirq)
  478. sshd-4261 0d.s1 30us : trace_preempt_on (__do_softirq)
  479. This has some more changes. Preemption was disabled when an interrupt
  480. came in (notice the 'h'), and was enabled while doing a softirq.
  481. (notice the 's'). But we also see that interrupts have been disabled
  482. when entering the preempt off section and leaving it (the 'd').
  483. We do not know if interrupts were enabled in the mean time.
  484. # tracer: preemptoff
  485. #
  486. preemptoff latency trace v1.1.5 on 2.6.26-rc8
  487. --------------------------------------------------------------------
  488. latency: 63 us, #87/87, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  489. -----------------
  490. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  491. -----------------
  492. => started at: remove_wait_queue
  493. => ended at: __do_softirq
  494. # _------=> CPU#
  495. # / _-----=> irqs-off
  496. # | / _----=> need-resched
  497. # || / _---=> hardirq/softirq
  498. # ||| / _--=> preempt-depth
  499. # |||| /
  500. # ||||| delay
  501. # cmd pid ||||| time | caller
  502. # \ / ||||| \ | /
  503. sshd-4261 0d..1 0us : _spin_lock_irqsave (remove_wait_queue)
  504. sshd-4261 0d..1 1us : _spin_unlock_irqrestore (remove_wait_queue)
  505. sshd-4261 0d..1 2us : do_IRQ (common_interrupt)
  506. sshd-4261 0d..1 2us : irq_enter (do_IRQ)
  507. sshd-4261 0d..1 2us : idle_cpu (irq_enter)
  508. sshd-4261 0d..1 3us : add_preempt_count (irq_enter)
  509. sshd-4261 0d.h1 3us : idle_cpu (irq_enter)
  510. sshd-4261 0d.h. 4us : handle_fasteoi_irq (do_IRQ)
  511. [...]
  512. sshd-4261 0d.h. 12us : add_preempt_count (_spin_lock)
  513. sshd-4261 0d.h1 12us : ack_ioapic_quirk_irq (handle_fasteoi_irq)
  514. sshd-4261 0d.h1 13us : move_native_irq (ack_ioapic_quirk_irq)
  515. sshd-4261 0d.h1 13us : _spin_unlock (handle_fasteoi_irq)
  516. sshd-4261 0d.h1 14us : sub_preempt_count (_spin_unlock)
  517. sshd-4261 0d.h1 14us : irq_exit (do_IRQ)
  518. sshd-4261 0d.h1 15us : sub_preempt_count (irq_exit)
  519. sshd-4261 0d..2 15us : do_softirq (irq_exit)
  520. sshd-4261 0d... 15us : __do_softirq (do_softirq)
  521. sshd-4261 0d... 16us : __local_bh_disable (__do_softirq)
  522. sshd-4261 0d... 16us+: add_preempt_count (__local_bh_disable)
  523. sshd-4261 0d.s4 20us : add_preempt_count (__local_bh_disable)
  524. sshd-4261 0d.s4 21us : sub_preempt_count (local_bh_enable)
  525. sshd-4261 0d.s5 21us : sub_preempt_count (local_bh_enable)
  526. [...]
  527. sshd-4261 0d.s6 41us : add_preempt_count (__local_bh_disable)
  528. sshd-4261 0d.s6 42us : sub_preempt_count (local_bh_enable)
  529. sshd-4261 0d.s7 42us : sub_preempt_count (local_bh_enable)
  530. sshd-4261 0d.s5 43us : add_preempt_count (__local_bh_disable)
  531. sshd-4261 0d.s5 43us : sub_preempt_count (local_bh_enable_ip)
  532. sshd-4261 0d.s6 44us : sub_preempt_count (local_bh_enable_ip)
  533. sshd-4261 0d.s5 44us : add_preempt_count (__local_bh_disable)
  534. sshd-4261 0d.s5 45us : sub_preempt_count (local_bh_enable)
  535. [...]
  536. sshd-4261 0d.s. 63us : _local_bh_enable (__do_softirq)
  537. sshd-4261 0d.s1 64us : trace_preempt_on (__do_softirq)
  538. The above is an example of the preemptoff trace with ftrace_enabled
  539. set. Here we see that interrupts were disabled the entire time.
  540. The irq_enter code lets us know that we entered an interrupt 'h'.
  541. Before that, the functions being traced still show that it is not
  542. in an interrupt, but we can see from the functions themselves that
  543. this is not the case.
  544. Notice that __do_softirq when called does not have a preempt_count.
  545. It may seem that we missed a preempt enabling. What really happened
  546. is that the preempt count is held on the thread's stack and we
  547. switched to the softirq stack (4K stacks in effect). The code
  548. does not copy the preempt count, but because interrupts are disabled,
  549. we do not need to worry about it. Having a tracer like this is good
  550. for letting people know what really happens inside the kernel.
  551. preemptirqsoff
  552. --------------
  553. Knowing the locations that have interrupts disabled or preemption
  554. disabled for the longest times is helpful. But sometimes we would
  555. like to know when either preemption and/or interrupts are disabled.
  556. Consider the following code:
  557. local_irq_disable();
  558. call_function_with_irqs_off();
  559. preempt_disable();
  560. call_function_with_irqs_and_preemption_off();
  561. local_irq_enable();
  562. call_function_with_preemption_off();
  563. preempt_enable();
  564. The irqsoff tracer will record the total length of
  565. call_function_with_irqs_off() and
  566. call_function_with_irqs_and_preemption_off().
  567. The preemptoff tracer will record the total length of
  568. call_function_with_irqs_and_preemption_off() and
  569. call_function_with_preemption_off().
  570. But neither will trace the time that interrupts and/or preemption
  571. is disabled. This total time is the time that we can not schedule.
  572. To record this time, use the preemptirqsoff tracer.
  573. Again, using this trace is much like the irqsoff and preemptoff tracers.
  574. # echo preemptirqsoff > /debug/tracing/current_tracer
  575. # echo 0 > /debug/tracing/tracing_max_latency
  576. # echo 1 > /debug/tracing/tracing_enabled
  577. # ls -ltr
  578. [...]
  579. # echo 0 > /debug/tracing/tracing_enabled
  580. # cat /debug/tracing/latency_trace
  581. # tracer: preemptirqsoff
  582. #
  583. preemptirqsoff latency trace v1.1.5 on 2.6.26-rc8
  584. --------------------------------------------------------------------
  585. latency: 293 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  586. -----------------
  587. | task: ls-4860 (uid:0 nice:0 policy:0 rt_prio:0)
  588. -----------------
  589. => started at: apic_timer_interrupt
  590. => ended at: __do_softirq
  591. # _------=> CPU#
  592. # / _-----=> irqs-off
  593. # | / _----=> need-resched
  594. # || / _---=> hardirq/softirq
  595. # ||| / _--=> preempt-depth
  596. # |||| /
  597. # ||||| delay
  598. # cmd pid ||||| time | caller
  599. # \ / ||||| \ | /
  600. ls-4860 0d... 0us!: trace_hardirqs_off_thunk (apic_timer_interrupt)
  601. ls-4860 0d.s. 294us : _local_bh_enable (__do_softirq)
  602. ls-4860 0d.s1 294us : trace_preempt_on (__do_softirq)
  603. The trace_hardirqs_off_thunk is called from assembly on x86 when
  604. interrupts are disabled in the assembly code. Without the function
  605. tracing, we do not know if interrupts were enabled within the preemption
  606. points. We do see that it started with preemption enabled.
  607. Here is a trace with ftrace_enabled set:
  608. # tracer: preemptirqsoff
  609. #
  610. preemptirqsoff latency trace v1.1.5 on 2.6.26-rc8
  611. --------------------------------------------------------------------
  612. latency: 105 us, #183/183, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  613. -----------------
  614. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  615. -----------------
  616. => started at: write_chan
  617. => ended at: __do_softirq
  618. # _------=> CPU#
  619. # / _-----=> irqs-off
  620. # | / _----=> need-resched
  621. # || / _---=> hardirq/softirq
  622. # ||| / _--=> preempt-depth
  623. # |||| /
  624. # ||||| delay
  625. # cmd pid ||||| time | caller
  626. # \ / ||||| \ | /
  627. ls-4473 0.N.. 0us : preempt_schedule (write_chan)
  628. ls-4473 0dN.1 1us : _spin_lock (schedule)
  629. ls-4473 0dN.1 2us : add_preempt_count (_spin_lock)
  630. ls-4473 0d..2 2us : put_prev_task_fair (schedule)
  631. [...]
  632. ls-4473 0d..2 13us : set_normalized_timespec (ktime_get_ts)
  633. ls-4473 0d..2 13us : __switch_to (schedule)
  634. sshd-4261 0d..2 14us : finish_task_switch (schedule)
  635. sshd-4261 0d..2 14us : _spin_unlock_irq (finish_task_switch)
  636. sshd-4261 0d..1 15us : add_preempt_count (_spin_lock_irqsave)
  637. sshd-4261 0d..2 16us : _spin_unlock_irqrestore (hrtick_set)
  638. sshd-4261 0d..2 16us : do_IRQ (common_interrupt)
  639. sshd-4261 0d..2 17us : irq_enter (do_IRQ)
  640. sshd-4261 0d..2 17us : idle_cpu (irq_enter)
  641. sshd-4261 0d..2 18us : add_preempt_count (irq_enter)
  642. sshd-4261 0d.h2 18us : idle_cpu (irq_enter)
  643. sshd-4261 0d.h. 18us : handle_fasteoi_irq (do_IRQ)
  644. sshd-4261 0d.h. 19us : _spin_lock (handle_fasteoi_irq)
  645. sshd-4261 0d.h. 19us : add_preempt_count (_spin_lock)
  646. sshd-4261 0d.h1 20us : _spin_unlock (handle_fasteoi_irq)
  647. sshd-4261 0d.h1 20us : sub_preempt_count (_spin_unlock)
  648. [...]
  649. sshd-4261 0d.h1 28us : _spin_unlock (handle_fasteoi_irq)
  650. sshd-4261 0d.h1 29us : sub_preempt_count (_spin_unlock)
  651. sshd-4261 0d.h2 29us : irq_exit (do_IRQ)
  652. sshd-4261 0d.h2 29us : sub_preempt_count (irq_exit)
  653. sshd-4261 0d..3 30us : do_softirq (irq_exit)
  654. sshd-4261 0d... 30us : __do_softirq (do_softirq)
  655. sshd-4261 0d... 31us : __local_bh_disable (__do_softirq)
  656. sshd-4261 0d... 31us+: add_preempt_count (__local_bh_disable)
  657. sshd-4261 0d.s4 34us : add_preempt_count (__local_bh_disable)
  658. [...]
  659. sshd-4261 0d.s3 43us : sub_preempt_count (local_bh_enable_ip)
  660. sshd-4261 0d.s4 44us : sub_preempt_count (local_bh_enable_ip)
  661. sshd-4261 0d.s3 44us : smp_apic_timer_interrupt (apic_timer_interrupt)
  662. sshd-4261 0d.s3 45us : irq_enter (smp_apic_timer_interrupt)
  663. sshd-4261 0d.s3 45us : idle_cpu (irq_enter)
  664. sshd-4261 0d.s3 46us : add_preempt_count (irq_enter)
  665. sshd-4261 0d.H3 46us : idle_cpu (irq_enter)
  666. sshd-4261 0d.H3 47us : hrtimer_interrupt (smp_apic_timer_interrupt)
  667. sshd-4261 0d.H3 47us : ktime_get (hrtimer_interrupt)
  668. [...]
  669. sshd-4261 0d.H3 81us : tick_program_event (hrtimer_interrupt)
  670. sshd-4261 0d.H3 82us : ktime_get (tick_program_event)
  671. sshd-4261 0d.H3 82us : ktime_get_ts (ktime_get)
  672. sshd-4261 0d.H3 83us : getnstimeofday (ktime_get_ts)
  673. sshd-4261 0d.H3 83us : set_normalized_timespec (ktime_get_ts)
  674. sshd-4261 0d.H3 84us : clockevents_program_event (tick_program_event)
  675. sshd-4261 0d.H3 84us : lapic_next_event (clockevents_program_event)
  676. sshd-4261 0d.H3 85us : irq_exit (smp_apic_timer_interrupt)
  677. sshd-4261 0d.H3 85us : sub_preempt_count (irq_exit)
  678. sshd-4261 0d.s4 86us : sub_preempt_count (irq_exit)
  679. sshd-4261 0d.s3 86us : add_preempt_count (__local_bh_disable)
  680. [...]
  681. sshd-4261 0d.s1 98us : sub_preempt_count (net_rx_action)
  682. sshd-4261 0d.s. 99us : add_preempt_count (_spin_lock_irq)
  683. sshd-4261 0d.s1 99us+: _spin_unlock_irq (run_timer_softirq)
  684. sshd-4261 0d.s. 104us : _local_bh_enable (__do_softirq)
  685. sshd-4261 0d.s. 104us : sub_preempt_count (_local_bh_enable)
  686. sshd-4261 0d.s. 105us : _local_bh_enable (__do_softirq)
  687. sshd-4261 0d.s1 105us : trace_preempt_on (__do_softirq)
  688. This is a very interesting trace. It started with the preemption of
  689. the ls task. We see that the task had the "need_resched" bit set
  690. via the 'N' in the trace. Interrupts were disabled before the spin_lock
  691. at the beginning of the trace. We see that a schedule took place to run
  692. sshd. When the interrupts were enabled, we took an interrupt.
  693. On return from the interrupt handler, the softirq ran. We took another
  694. interrupt while running the softirq as we see from the capital 'H'.
  695. wakeup
  696. ------
  697. In a Real-Time environment it is very important to know the wakeup
  698. time it takes for the highest priority task that is woken up to the
  699. time that it executes. This is also known as "schedule latency".
  700. I stress the point that this is about RT tasks. It is also important
  701. to know the scheduling latency of non-RT tasks, but the average
  702. schedule latency is better for non-RT tasks. Tools like
  703. LatencyTop are more appropriate for such measurements.
  704. Real-Time environments are interested in the worst case latency.
  705. That is the longest latency it takes for something to happen, and
  706. not the average. We can have a very fast scheduler that may only
  707. have a large latency once in a while, but that would not work well
  708. with Real-Time tasks. The wakeup tracer was designed to record
  709. the worst case wakeups of RT tasks. Non-RT tasks are not recorded
  710. because the tracer only records one worst case and tracing non-RT
  711. tasks that are unpredictable will overwrite the worst case latency
  712. of RT tasks.
  713. Since this tracer only deals with RT tasks, we will run this slightly
  714. differently than we did with the previous tracers. Instead of performing
  715. an 'ls', we will run 'sleep 1' under 'chrt' which changes the
  716. priority of the task.
  717. # echo wakeup > /debug/tracing/current_tracer
  718. # echo 0 > /debug/tracing/tracing_max_latency
  719. # echo 1 > /debug/tracing/tracing_enabled
  720. # chrt -f 5 sleep 1
  721. # echo 0 > /debug/tracing/tracing_enabled
  722. # cat /debug/tracing/latency_trace
  723. # tracer: wakeup
  724. #
  725. wakeup latency trace v1.1.5 on 2.6.26-rc8
  726. --------------------------------------------------------------------
  727. latency: 4 us, #2/2, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  728. -----------------
  729. | task: sleep-4901 (uid:0 nice:0 policy:1 rt_prio:5)
  730. -----------------
  731. # _------=> CPU#
  732. # / _-----=> irqs-off
  733. # | / _----=> need-resched
  734. # || / _---=> hardirq/softirq
  735. # ||| / _--=> preempt-depth
  736. # |||| /
  737. # ||||| delay
  738. # cmd pid ||||| time | caller
  739. # \ / ||||| \ | /
  740. <idle>-0 1d.h4 0us+: try_to_wake_up (wake_up_process)
  741. <idle>-0 1d..4 4us : schedule (cpu_idle)
  742. Running this on an idle system, we see that it only took 4 microseconds
  743. to perform the task switch. Note, since the trace marker in the
  744. schedule is before the actual "switch", we stop the tracing when
  745. the recorded task is about to schedule in. This may change if
  746. we add a new marker at the end of the scheduler.
  747. Notice that the recorded task is 'sleep' with the PID of 4901 and it
  748. has an rt_prio of 5. This priority is user-space priority and not
  749. the internal kernel priority. The policy is 1 for SCHED_FIFO and 2
  750. for SCHED_RR.
  751. Doing the same with chrt -r 5 and ftrace_enabled set.
  752. # tracer: wakeup
  753. #
  754. wakeup latency trace v1.1.5 on 2.6.26-rc8
  755. --------------------------------------------------------------------
  756. latency: 50 us, #60/60, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  757. -----------------
  758. | task: sleep-4068 (uid:0 nice:0 policy:2 rt_prio:5)
  759. -----------------
  760. # _------=> CPU#
  761. # / _-----=> irqs-off
  762. # | / _----=> need-resched
  763. # || / _---=> hardirq/softirq
  764. # ||| / _--=> preempt-depth
  765. # |||| /
  766. # ||||| delay
  767. # cmd pid ||||| time | caller
  768. # \ / ||||| \ | /
  769. ksoftirq-7 1d.H3 0us : try_to_wake_up (wake_up_process)
  770. ksoftirq-7 1d.H4 1us : sub_preempt_count (marker_probe_cb)
  771. ksoftirq-7 1d.H3 2us : check_preempt_wakeup (try_to_wake_up)
  772. ksoftirq-7 1d.H3 3us : update_curr (check_preempt_wakeup)
  773. ksoftirq-7 1d.H3 4us : calc_delta_mine (update_curr)
  774. ksoftirq-7 1d.H3 5us : __resched_task (check_preempt_wakeup)
  775. ksoftirq-7 1d.H3 6us : task_wake_up_rt (try_to_wake_up)
  776. ksoftirq-7 1d.H3 7us : _spin_unlock_irqrestore (try_to_wake_up)
  777. [...]
  778. ksoftirq-7 1d.H2 17us : irq_exit (smp_apic_timer_interrupt)
  779. ksoftirq-7 1d.H2 18us : sub_preempt_count (irq_exit)
  780. ksoftirq-7 1d.s3 19us : sub_preempt_count (irq_exit)
  781. ksoftirq-7 1..s2 20us : rcu_process_callbacks (__do_softirq)
  782. [...]
  783. ksoftirq-7 1..s2 26us : __rcu_process_callbacks (rcu_process_callbacks)
  784. ksoftirq-7 1d.s2 27us : _local_bh_enable (__do_softirq)
  785. ksoftirq-7 1d.s2 28us : sub_preempt_count (_local_bh_enable)
  786. ksoftirq-7 1.N.3 29us : sub_preempt_count (ksoftirqd)
  787. ksoftirq-7 1.N.2 30us : _cond_resched (ksoftirqd)
  788. ksoftirq-7 1.N.2 31us : __cond_resched (_cond_resched)
  789. ksoftirq-7 1.N.2 32us : add_preempt_count (__cond_resched)
  790. ksoftirq-7 1.N.2 33us : schedule (__cond_resched)
  791. ksoftirq-7 1.N.2 33us : add_preempt_count (schedule)
  792. ksoftirq-7 1.N.3 34us : hrtick_clear (schedule)
  793. ksoftirq-7 1dN.3 35us : _spin_lock (schedule)
  794. ksoftirq-7 1dN.3 36us : add_preempt_count (_spin_lock)
  795. ksoftirq-7 1d..4 37us : put_prev_task_fair (schedule)
  796. ksoftirq-7 1d..4 38us : update_curr (put_prev_task_fair)
  797. [...]
  798. ksoftirq-7 1d..5 47us : _spin_trylock (tracing_record_cmdline)
  799. ksoftirq-7 1d..5 48us : add_preempt_count (_spin_trylock)
  800. ksoftirq-7 1d..6 49us : _spin_unlock (tracing_record_cmdline)
  801. ksoftirq-7 1d..6 49us : sub_preempt_count (_spin_unlock)
  802. ksoftirq-7 1d..4 50us : schedule (__cond_resched)
  803. The interrupt went off while running ksoftirqd. This task runs at
  804. SCHED_OTHER. Why did not we see the 'N' set early? This may be
  805. a harmless bug with x86_32 and 4K stacks. On x86_32 with 4K stacks
  806. configured, the interrupt and softirq run with their own stack.
  807. Some information is held on the top of the task's stack (need_resched
  808. and preempt_count are both stored there). The setting of the NEED_RESCHED
  809. bit is done directly to the task's stack, but the reading of the
  810. NEED_RESCHED is done by looking at the current stack, which in this case
  811. is the stack for the hard interrupt. This hides the fact that NEED_RESCHED
  812. has been set. We do not see the 'N' until we switch back to the task's
  813. assigned stack.
  814. ftrace
  815. ------
  816. ftrace is not only the name of the tracing infrastructure, but it
  817. is also a name of one of the tracers. The tracer is the function
  818. tracer. Enabling the function tracer can be done from the
  819. debug file system. Make sure the ftrace_enabled is set otherwise
  820. this tracer is a nop.
  821. # sysctl kernel.ftrace_enabled=1
  822. # echo ftrace > /debug/tracing/current_tracer
  823. # echo 1 > /debug/tracing/tracing_enabled
  824. # usleep 1
  825. # echo 0 > /debug/tracing/tracing_enabled
  826. # cat /debug/tracing/trace
  827. # tracer: ftrace
  828. #
  829. # TASK-PID CPU# TIMESTAMP FUNCTION
  830. # | | | | |
  831. bash-4003 [00] 123.638713: finish_task_switch <-schedule
  832. bash-4003 [00] 123.638714: _spin_unlock_irq <-finish_task_switch
  833. bash-4003 [00] 123.638714: sub_preempt_count <-_spin_unlock_irq
  834. bash-4003 [00] 123.638715: hrtick_set <-schedule
  835. bash-4003 [00] 123.638715: _spin_lock_irqsave <-hrtick_set
  836. bash-4003 [00] 123.638716: add_preempt_count <-_spin_lock_irqsave
  837. bash-4003 [00] 123.638716: _spin_unlock_irqrestore <-hrtick_set
  838. bash-4003 [00] 123.638717: sub_preempt_count <-_spin_unlock_irqrestore
  839. bash-4003 [00] 123.638717: hrtick_clear <-hrtick_set
  840. bash-4003 [00] 123.638718: sub_preempt_count <-schedule
  841. bash-4003 [00] 123.638718: sub_preempt_count <-preempt_schedule
  842. bash-4003 [00] 123.638719: wait_for_completion <-__stop_machine_run
  843. bash-4003 [00] 123.638719: wait_for_common <-wait_for_completion
  844. bash-4003 [00] 123.638720: _spin_lock_irq <-wait_for_common
  845. bash-4003 [00] 123.638720: add_preempt_count <-_spin_lock_irq
  846. [...]
  847. Note: ftrace uses ring buffers to store the above entries. The newest data
  848. may overwrite the oldest data. Sometimes using echo to stop the trace
  849. is not sufficient because the tracing could have overwritten the data
  850. that you wanted to record. For this reason, it is sometimes better to
  851. disable tracing directly from a program. This allows you to stop the
  852. tracing at the point that you hit the part that you are interested in.
  853. To disable the tracing directly from a C program, something like following
  854. code snippet can be used:
  855. int trace_fd;
  856. [...]
  857. int main(int argc, char *argv[]) {
  858. [...]
  859. trace_fd = open("/debug/tracing/tracing_enabled", O_WRONLY);
  860. [...]
  861. if (condition_hit()) {
  862. write(trace_fd, "0", 1);
  863. }
  864. [...]
  865. }
  866. Note: Here we hard coded the path name. The debugfs mount is not
  867. guaranteed to be at /debug (and is more commonly at /sys/kernel/debug).
  868. For simple one time traces, the above is sufficent. For anything else,
  869. a search through /proc/mounts may be needed to find where the debugfs
  870. file-system is mounted.
  871. dynamic ftrace
  872. --------------
  873. If CONFIG_DYNAMIC_FTRACE is set, the system will run with
  874. virtually no overhead when function tracing is disabled. The way
  875. this works is the mcount function call (placed at the start of
  876. every kernel function, produced by the -pg switch in gcc), starts
  877. of pointing to a simple return. (Enabling FTRACE will include the
  878. -pg switch in the compiling of the kernel.)
  879. When dynamic ftrace is initialized, it calls kstop_machine to make
  880. the machine act like a uniprocessor so that it can freely modify code
  881. without worrying about other processors executing that same code. At
  882. initialization, the mcount calls are changed to call a "record_ip"
  883. function. After this, the first time a kernel function is called,
  884. it has the calling address saved in a hash table.
  885. Later on the ftraced kernel thread is awoken and will again call
  886. kstop_machine if new functions have been recorded. The ftraced thread
  887. will change all calls to mcount to "nop". Just calling mcount
  888. and having mcount return has shown a 10% overhead. By converting
  889. it to a nop, there is no measurable overhead to the system.
  890. One special side-effect to the recording of the functions being
  891. traced is that we can now selectively choose which functions we
  892. wish to trace and which ones we want the mcount calls to remain as
  893. nops.
  894. Two files are used, one for enabling and one for disabling the tracing
  895. of specified functions. They are:
  896. set_ftrace_filter
  897. and
  898. set_ftrace_notrace
  899. A list of available functions that you can add to these files is listed
  900. in:
  901. available_filter_functions
  902. # cat /debug/tracing/available_filter_functions
  903. put_prev_task_idle
  904. kmem_cache_create
  905. pick_next_task_rt
  906. get_online_cpus
  907. pick_next_task_fair
  908. mutex_lock
  909. [...]
  910. If I am only interested in sys_nanosleep and hrtimer_interrupt:
  911. # echo sys_nanosleep hrtimer_interrupt \
  912. > /debug/tracing/set_ftrace_filter
  913. # echo ftrace > /debug/tracing/current_tracer
  914. # echo 1 > /debug/tracing/tracing_enabled
  915. # usleep 1
  916. # echo 0 > /debug/tracing/tracing_enabled
  917. # cat /debug/tracing/trace
  918. # tracer: ftrace
  919. #
  920. # TASK-PID CPU# TIMESTAMP FUNCTION
  921. # | | | | |
  922. usleep-4134 [00] 1317.070017: hrtimer_interrupt <-smp_apic_timer_interrupt
  923. usleep-4134 [00] 1317.070111: sys_nanosleep <-syscall_call
  924. <idle>-0 [00] 1317.070115: hrtimer_interrupt <-smp_apic_timer_interrupt
  925. To see which functions are being traced, you can cat the file:
  926. # cat /debug/tracing/set_ftrace_filter
  927. hrtimer_interrupt
  928. sys_nanosleep
  929. Perhaps this is not enough. The filters also allow simple wild cards.
  930. Only the following are currently available
  931. <match>* - will match functions that begin with <match>
  932. *<match> - will match functions that end with <match>
  933. *<match>* - will match functions that have <match> in it
  934. These are the only wild cards which are supported.
  935. <match>*<match> will not work.
  936. # echo hrtimer_* > /debug/tracing/set_ftrace_filter
  937. Produces:
  938. # tracer: ftrace
  939. #
  940. # TASK-PID CPU# TIMESTAMP FUNCTION
  941. # | | | | |
  942. bash-4003 [00] 1480.611794: hrtimer_init <-copy_process
  943. bash-4003 [00] 1480.611941: hrtimer_start <-hrtick_set
  944. bash-4003 [00] 1480.611956: hrtimer_cancel <-hrtick_clear
  945. bash-4003 [00] 1480.611956: hrtimer_try_to_cancel <-hrtimer_cancel
  946. <idle>-0 [00] 1480.612019: hrtimer_get_next_event <-get_next_timer_interrupt
  947. <idle>-0 [00] 1480.612025: hrtimer_get_next_event <-get_next_timer_interrupt
  948. <idle>-0 [00] 1480.612032: hrtimer_get_next_event <-get_next_timer_interrupt
  949. <idle>-0 [00] 1480.612037: hrtimer_get_next_event <-get_next_timer_interrupt
  950. <idle>-0 [00] 1480.612382: hrtimer_get_next_event <-get_next_timer_interrupt
  951. Notice that we lost the sys_nanosleep.
  952. # cat /debug/tracing/set_ftrace_filter
  953. hrtimer_run_queues
  954. hrtimer_run_pending
  955. hrtimer_init
  956. hrtimer_cancel
  957. hrtimer_try_to_cancel
  958. hrtimer_forward
  959. hrtimer_start
  960. hrtimer_reprogram
  961. hrtimer_force_reprogram
  962. hrtimer_get_next_event
  963. hrtimer_interrupt
  964. hrtimer_nanosleep
  965. hrtimer_wakeup
  966. hrtimer_get_remaining
  967. hrtimer_get_res
  968. hrtimer_init_sleeper
  969. This is because the '>' and '>>' act just like they do in bash.
  970. To rewrite the filters, use '>'
  971. To append to the filters, use '>>'
  972. To clear out a filter so that all functions will be recorded again:
  973. # echo > /debug/tracing/set_ftrace_filter
  974. # cat /debug/tracing/set_ftrace_filter
  975. #
  976. Again, now we want to append.
  977. # echo sys_nanosleep > /debug/tracing/set_ftrace_filter
  978. # cat /debug/tracing/set_ftrace_filter
  979. sys_nanosleep
  980. # echo hrtimer_* >> /debug/tracing/set_ftrace_filter
  981. # cat /debug/tracing/set_ftrace_filter
  982. hrtimer_run_queues
  983. hrtimer_run_pending
  984. hrtimer_init
  985. hrtimer_cancel
  986. hrtimer_try_to_cancel
  987. hrtimer_forward
  988. hrtimer_start
  989. hrtimer_reprogram
  990. hrtimer_force_reprogram
  991. hrtimer_get_next_event
  992. hrtimer_interrupt
  993. sys_nanosleep
  994. hrtimer_nanosleep
  995. hrtimer_wakeup
  996. hrtimer_get_remaining
  997. hrtimer_get_res
  998. hrtimer_init_sleeper
  999. The set_ftrace_notrace prevents those functions from being traced.
  1000. # echo '*preempt*' '*lock*' > /debug/tracing/set_ftrace_notrace
  1001. Produces:
  1002. # tracer: ftrace
  1003. #
  1004. # TASK-PID CPU# TIMESTAMP FUNCTION
  1005. # | | | | |
  1006. bash-4043 [01] 115.281644: finish_task_switch <-schedule
  1007. bash-4043 [01] 115.281645: hrtick_set <-schedule
  1008. bash-4043 [01] 115.281645: hrtick_clear <-hrtick_set
  1009. bash-4043 [01] 115.281646: wait_for_completion <-__stop_machine_run
  1010. bash-4043 [01] 115.281647: wait_for_common <-wait_for_completion
  1011. bash-4043 [01] 115.281647: kthread_stop <-stop_machine_run
  1012. bash-4043 [01] 115.281648: init_waitqueue_head <-kthread_stop
  1013. bash-4043 [01] 115.281648: wake_up_process <-kthread_stop
  1014. bash-4043 [01] 115.281649: try_to_wake_up <-wake_up_process
  1015. We can see that there's no more lock or preempt tracing.
  1016. ftraced
  1017. -------
  1018. As mentioned above, when dynamic ftrace is configured in, a kernel
  1019. thread wakes up once a second and checks to see if there are mcount
  1020. calls that need to be converted into nops. If there are not any, then
  1021. it simply goes back to sleep. But if there are some, it will call
  1022. kstop_machine to convert the calls to nops.
  1023. There may be a case in which you do not want this added latency.
  1024. Perhaps you are doing some audio recording and this activity might
  1025. cause skips in the playback. There is an interface to disable
  1026. and enable the "ftraced" kernel thread.
  1027. # echo 0 > /debug/tracing/ftraced_enabled
  1028. This will disable the calling of kstop_machine to update the
  1029. mcount calls to nops. Remember that there is a large overhead
  1030. to calling mcount. Without this kernel thread, that overhead will
  1031. exist.
  1032. If there are recorded calls to mcount, any write to the ftraced_enabled
  1033. file will cause the kstop_machine to run. This means that a
  1034. user can manually perform the updates when they want to by simply
  1035. echoing a '0' into the ftraced_enabled file.
  1036. The updates are also done at the beginning of enabling a tracer
  1037. that uses ftrace function recording.
  1038. trace_pipe
  1039. ----------
  1040. The trace_pipe outputs the same content as the trace file, but the effect
  1041. on the tracing is different. Every read from trace_pipe is consumed.
  1042. This means that subsequent reads will be different. The trace
  1043. is live.
  1044. # echo ftrace > /debug/tracing/current_tracer
  1045. # cat /debug/tracing/trace_pipe > /tmp/trace.out &
  1046. [1] 4153
  1047. # echo 1 > /debug/tracing/tracing_enabled
  1048. # usleep 1
  1049. # echo 0 > /debug/tracing/tracing_enabled
  1050. # cat /debug/tracing/trace
  1051. # tracer: ftrace
  1052. #
  1053. # TASK-PID CPU# TIMESTAMP FUNCTION
  1054. # | | | | |
  1055. #
  1056. # cat /tmp/trace.out
  1057. bash-4043 [00] 41.267106: finish_task_switch <-schedule
  1058. bash-4043 [00] 41.267106: hrtick_set <-schedule
  1059. bash-4043 [00] 41.267107: hrtick_clear <-hrtick_set
  1060. bash-4043 [00] 41.267108: wait_for_completion <-__stop_machine_run
  1061. bash-4043 [00] 41.267108: wait_for_common <-wait_for_completion
  1062. bash-4043 [00] 41.267109: kthread_stop <-stop_machine_run
  1063. bash-4043 [00] 41.267109: init_waitqueue_head <-kthread_stop
  1064. bash-4043 [00] 41.267110: wake_up_process <-kthread_stop
  1065. bash-4043 [00] 41.267110: try_to_wake_up <-wake_up_process
  1066. bash-4043 [00] 41.267111: select_task_rq_rt <-try_to_wake_up
  1067. Note, reading the trace_pipe file will block until more input is added.
  1068. By changing the tracer, trace_pipe will issue an EOF. We needed
  1069. to set the ftrace tracer _before_ cating the trace_pipe file.
  1070. trace entries
  1071. -------------
  1072. Having too much or not enough data can be troublesome in diagnosing
  1073. an issue in the kernel. The file trace_entries is used to modify
  1074. the size of the internal trace buffers. The number listed
  1075. is the number of entries that can be recorded per CPU. To know
  1076. the full size, multiply the number of possible CPUS with the
  1077. number of entries.
  1078. # cat /debug/tracing/trace_entries
  1079. 65620
  1080. Note, to modify this, you must have tracing completely disabled. To do that,
  1081. echo "none" into the current_tracer. If the current_tracer is not set
  1082. to "none", an EINVAL error will be returned.
  1083. # echo none > /debug/tracing/current_tracer
  1084. # echo 100000 > /debug/tracing/trace_entries
  1085. # cat /debug/tracing/trace_entries
  1086. 100045
  1087. Notice that we echoed in 100,000 but the size is 100,045. The entries
  1088. are held in individual pages. It allocates the number of pages it takes
  1089. to fulfill the request. If more entries may fit on the last page
  1090. then they will be added.
  1091. # echo 1 > /debug/tracing/trace_entries
  1092. # cat /debug/tracing/trace_entries
  1093. 85
  1094. This shows us that 85 entries can fit in a single page.
  1095. The number of pages which will be allocated is limited to a percentage
  1096. of available memory. Allocating too much will produce an error.
  1097. # echo 1000000000000 > /debug/tracing/trace_entries
  1098. -bash: echo: write error: Cannot allocate memory
  1099. # cat /debug/tracing/trace_entries
  1100. 85