ftrace.txt 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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 performance
  15. 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 the
  18. tracers that are currently in ftrace include a tracer to trace
  19. context switches, the time it takes for a high priority task to
  20. run after it was woken up, the time interrupts are disabled, and
  21. more (ftrace allows for tracer plugins, which means that the list of
  22. tracers can always grow).
  23. The File System
  24. ---------------
  25. Ftrace uses the debugfs file system to hold the control files as well
  26. as the files to display output.
  27. To mount the debugfs system:
  28. # mkdir /debug
  29. # mount -t debugfs nodev /debug
  30. (Note: it is more common to mount at /sys/kernel/debug, but for simplicity
  31. this document will use /debug)
  32. That's it! (assuming that you have ftrace configured into your kernel)
  33. After mounting the debugfs, you can see a directory called
  34. "tracing". This directory contains the control and output files
  35. of ftrace. Here is a list of some of the key files:
  36. Note: all time values are in microseconds.
  37. current_tracer: This is used to set or display the current tracer
  38. that is configured.
  39. available_tracers: This holds the different types of tracers that
  40. have been compiled into the kernel. The tracers
  41. listed here can be configured by echoing their name
  42. into current_tracer.
  43. tracing_enabled: This sets or displays whether the current_tracer
  44. is activated and tracing or not. Echo 0 into this
  45. file to disable the tracer or 1 to enable it.
  46. trace: This file holds the output of the trace in a human readable
  47. format (described below).
  48. latency_trace: This file shows the same trace but the information
  49. is organized more to display possible latencies
  50. in the system (described below).
  51. trace_pipe: The output is the same as the "trace" file but this
  52. file is meant to be streamed with live tracing.
  53. Reads from this file will block until new data
  54. is retrieved. Unlike the "trace" and "latency_trace"
  55. files, this file is a consumer. This means reading
  56. from this file causes sequential reads to display
  57. more current data. Once data is read from this
  58. file, it is consumed, and will not be read
  59. again with a sequential read. The "trace" and
  60. "latency_trace" files are static, and if the
  61. tracer is not adding more data, they will display
  62. the same information every time they are read.
  63. trace_options: This file lets the user control the amount of data
  64. that is displayed in one of the above output
  65. files.
  66. trace_max_latency: Some of the tracers record the max latency.
  67. For example, the time interrupts are disabled.
  68. This time is saved in this file. The max trace
  69. will also be stored, and displayed by either
  70. "trace" or "latency_trace". A new max trace will
  71. only be recorded if the latency is greater than
  72. the value in this file. (in microseconds)
  73. buffer_size_kb: This sets or displays the number of kilobytes each CPU
  74. buffer can hold. The tracer buffers are the same size
  75. for each CPU. The displayed number is the size of the
  76. CPU buffer and not total size of all buffers. The
  77. trace buffers are allocated in pages (blocks of memory
  78. that the kernel uses for allocation, usually 4 KB in size).
  79. If the last page allocated has room for more bytes
  80. than requested, the rest of the page will be used,
  81. making the actual allocation bigger than requested.
  82. (Note, the size may not be a multiple of the page size due
  83. to buffer managment overhead.)
  84. This can only be updated when the current_tracer
  85. is set to "nop".
  86. tracing_cpumask: This is a mask that lets the user only trace
  87. on specified CPUS. The format is a hex string
  88. representing the CPUS.
  89. set_ftrace_filter: When dynamic ftrace is configured in (see the
  90. section below "dynamic ftrace"), the code is dynamically
  91. modified (code text rewrite) to disable calling of the
  92. function profiler (mcount). This lets tracing be configured
  93. in with practically no overhead in performance. This also
  94. has a side effect of enabling or disabling specific functions
  95. to be traced. Echoing names of functions into this file
  96. will limit the trace to only those functions.
  97. set_ftrace_notrace: This has an effect opposite to that of
  98. set_ftrace_filter. Any function that is added here will not
  99. be traced. If a function exists in both set_ftrace_filter
  100. and set_ftrace_notrace, the function will _not_ be traced.
  101. set_ftrace_pid: Have the function tracer only trace a single thread.
  102. available_filter_functions: This lists the functions that ftrace
  103. has processed and can trace. These are the function
  104. names that you can pass to "set_ftrace_filter" or
  105. "set_ftrace_notrace". (See the section "dynamic ftrace"
  106. below for more details.)
  107. The Tracers
  108. -----------
  109. Here is the list of current tracers that may be configured.
  110. function - function tracer that uses mcount to trace all functions.
  111. sched_switch - traces the context switches between tasks.
  112. irqsoff - traces the areas that disable interrupts and saves
  113. the trace with the longest max latency.
  114. See tracing_max_latency. When a new max is recorded,
  115. it replaces the old trace. It is best to view this
  116. trace via the latency_trace file.
  117. preemptoff - Similar to irqsoff but traces and records the amount of
  118. time for which preemption is disabled.
  119. preemptirqsoff - Similar to irqsoff and preemptoff, but traces and
  120. records the largest time for which irqs and/or preemption
  121. is disabled.
  122. wakeup - Traces and records the max latency that it takes for
  123. the highest priority task to get scheduled after
  124. it has been woken up.
  125. nop - This is not a tracer. To remove all tracers from tracing
  126. simply echo "nop" into current_tracer.
  127. hw-branch-tracer - traces branches on all cpu's in a circular buffer.
  128. Examples of using the tracer
  129. ----------------------------
  130. Here are typical examples of using the tracers when controlling them only
  131. with the debugfs interface (without using any user-land utilities).
  132. Output format:
  133. --------------
  134. Here is an example of the output format of the file "trace"
  135. --------
  136. # tracer: function
  137. #
  138. # TASK-PID CPU# TIMESTAMP FUNCTION
  139. # | | | | |
  140. bash-4251 [01] 10152.583854: path_put <-path_walk
  141. bash-4251 [01] 10152.583855: dput <-path_put
  142. bash-4251 [01] 10152.583855: _atomic_dec_and_lock <-dput
  143. --------
  144. A header is printed with the tracer name that is represented by the trace.
  145. In this case the tracer is "function". Then a header showing the format. Task
  146. name "bash", the task PID "4251", the CPU that it was running on
  147. "01", the timestamp in <secs>.<usecs> format, the function name that was
  148. traced "path_put" and the parent function that called this function
  149. "path_walk". The timestamp is the time at which the function was
  150. entered.
  151. The sched_switch tracer also includes tracing of task wakeups and
  152. context switches.
  153. ksoftirqd/1-7 [01] 1453.070013: 7:115:R + 2916:115:S
  154. ksoftirqd/1-7 [01] 1453.070013: 7:115:R + 10:115:S
  155. ksoftirqd/1-7 [01] 1453.070013: 7:115:R ==> 10:115:R
  156. events/1-10 [01] 1453.070013: 10:115:S ==> 2916:115:R
  157. kondemand/1-2916 [01] 1453.070013: 2916:115:S ==> 7:115:R
  158. ksoftirqd/1-7 [01] 1453.070013: 7:115:S ==> 0:140:R
  159. Wake ups are represented by a "+" and the context switches are shown as
  160. "==>". The format is:
  161. Context switches:
  162. Previous task Next Task
  163. <pid>:<prio>:<state> ==> <pid>:<prio>:<state>
  164. Wake ups:
  165. Current task Task waking up
  166. <pid>:<prio>:<state> + <pid>:<prio>:<state>
  167. The prio is the internal kernel priority, which is the inverse of the
  168. priority that is usually displayed by user-space tools. Zero represents
  169. the highest priority (99). Prio 100 starts the "nice" priorities with
  170. 100 being equal to nice -20 and 139 being nice 19. The prio "140" is
  171. reserved for the idle task which is the lowest priority thread (pid 0).
  172. Latency trace format
  173. --------------------
  174. For traces that display latency times, the latency_trace file gives
  175. somewhat more information to see why a latency happened. Here is a typical
  176. trace.
  177. # tracer: irqsoff
  178. #
  179. irqsoff latency trace v1.1.5 on 2.6.26-rc8
  180. --------------------------------------------------------------------
  181. latency: 97 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  182. -----------------
  183. | task: swapper-0 (uid:0 nice:0 policy:0 rt_prio:0)
  184. -----------------
  185. => started at: apic_timer_interrupt
  186. => ended at: do_softirq
  187. # _------=> CPU#
  188. # / _-----=> irqs-off
  189. # | / _----=> need-resched
  190. # || / _---=> hardirq/softirq
  191. # ||| / _--=> preempt-depth
  192. # |||| /
  193. # ||||| delay
  194. # cmd pid ||||| time | caller
  195. # \ / ||||| \ | /
  196. <idle>-0 0d..1 0us+: trace_hardirqs_off_thunk (apic_timer_interrupt)
  197. <idle>-0 0d.s. 97us : __do_softirq (do_softirq)
  198. <idle>-0 0d.s1 98us : trace_hardirqs_on (do_softirq)
  199. This shows that the current tracer is "irqsoff" tracing the time for which
  200. interrupts were disabled. It gives the trace version and the version
  201. of the kernel upon which this was executed on (2.6.26-rc8). Then it displays
  202. the max latency in microsecs (97 us). The number of trace entries displayed
  203. and the total number recorded (both are three: #3/3). The type of
  204. preemption that was used (PREEMPT). VP, KP, SP, and HP are always zero
  205. and are reserved for later use. #P is the number of online CPUS (#P:2).
  206. The task is the process that was running when the latency occurred.
  207. (swapper pid: 0).
  208. The start and stop (the functions in which the interrupts were disabled and
  209. enabled respectively) that caused the latencies:
  210. apic_timer_interrupt is where the interrupts were disabled.
  211. do_softirq is where they were enabled again.
  212. The next lines after the header are the trace itself. The header
  213. explains which is which.
  214. cmd: The name of the process in the trace.
  215. pid: The PID of that process.
  216. CPU#: The CPU which the process was running on.
  217. irqs-off: 'd' interrupts are disabled. '.' otherwise.
  218. Note: If the architecture does not support a way to
  219. read the irq flags variable, an 'X' will always
  220. be printed here.
  221. need-resched: 'N' task need_resched is set, '.' otherwise.
  222. hardirq/softirq:
  223. 'H' - hard irq occurred inside a softirq.
  224. 'h' - hard irq is running
  225. 's' - soft irq is running
  226. '.' - normal context.
  227. preempt-depth: The level of preempt_disabled
  228. The above is mostly meaningful for kernel developers.
  229. time: This differs from the trace file output. The trace file output
  230. includes an absolute timestamp. The timestamp used by the
  231. latency_trace file is relative to the start of the trace.
  232. delay: This is just to help catch your eye a bit better. And
  233. needs to be fixed to be only relative to the same CPU.
  234. The marks are determined by the difference between this
  235. current trace and the next trace.
  236. '!' - greater than preempt_mark_thresh (default 100)
  237. '+' - greater than 1 microsecond
  238. ' ' - less than or equal to 1 microsecond.
  239. The rest is the same as the 'trace' file.
  240. trace_options
  241. -------------
  242. The trace_options file is used to control what gets printed in the trace
  243. output. To see what is available, simply cat the file:
  244. cat /debug/tracing/trace_options
  245. print-parent nosym-offset nosym-addr noverbose noraw nohex nobin \
  246. noblock nostacktrace nosched-tree nouserstacktrace nosym-userobj
  247. To disable one of the options, echo in the option prepended with "no".
  248. echo noprint-parent > /debug/tracing/trace_options
  249. To enable an option, leave off the "no".
  250. echo sym-offset > /debug/tracing/trace_options
  251. Here are the available options:
  252. print-parent - On function traces, display the calling function
  253. as well as the function being traced.
  254. print-parent:
  255. bash-4000 [01] 1477.606694: simple_strtoul <-strict_strtoul
  256. noprint-parent:
  257. bash-4000 [01] 1477.606694: simple_strtoul
  258. sym-offset - Display not only the function name, but also the offset
  259. in the function. For example, instead of seeing just
  260. "ktime_get", you will see "ktime_get+0xb/0x20".
  261. sym-offset:
  262. bash-4000 [01] 1477.606694: simple_strtoul+0x6/0xa0
  263. sym-addr - this will also display the function address as well as
  264. the function name.
  265. sym-addr:
  266. bash-4000 [01] 1477.606694: simple_strtoul <c0339346>
  267. verbose - This deals with the latency_trace file.
  268. bash 4000 1 0 00000000 00010a95 [58127d26] 1720.415ms \
  269. (+0.000ms): simple_strtoul (strict_strtoul)
  270. raw - This will display raw numbers. This option is best for use with
  271. user applications that can translate the raw numbers better than
  272. having it done in the kernel.
  273. hex - Similar to raw, but the numbers will be in a hexadecimal format.
  274. bin - This will print out the formats in raw binary.
  275. block - TBD (needs update)
  276. stacktrace - This is one of the options that changes the trace itself.
  277. When a trace is recorded, so is the stack of functions.
  278. This allows for back traces of trace sites.
  279. userstacktrace - This option changes the trace.
  280. It records a stacktrace of the current userspace thread.
  281. sym-userobj - when user stacktrace are enabled, look up which object the
  282. address belongs to, and print a relative address
  283. This is especially useful when ASLR is on, otherwise you don't
  284. get a chance to resolve the address to object/file/line after the app is no
  285. longer running
  286. The lookup is performed when you read trace,trace_pipe,latency_trace. Example:
  287. a.out-1623 [000] 40874.465068: /root/a.out[+0x480] <-/root/a.out[+0
  288. x494] <- /root/a.out[+0x4a8] <- /lib/libc-2.7.so[+0x1e1a6]
  289. sched-tree - TBD (any users??)
  290. sched_switch
  291. ------------
  292. This tracer simply records schedule switches. Here is an example
  293. of how to use it.
  294. # echo sched_switch > /debug/tracing/current_tracer
  295. # echo 1 > /debug/tracing/tracing_enabled
  296. # sleep 1
  297. # echo 0 > /debug/tracing/tracing_enabled
  298. # cat /debug/tracing/trace
  299. # tracer: sched_switch
  300. #
  301. # TASK-PID CPU# TIMESTAMP FUNCTION
  302. # | | | | |
  303. bash-3997 [01] 240.132281: 3997:120:R + 4055:120:R
  304. bash-3997 [01] 240.132284: 3997:120:R ==> 4055:120:R
  305. sleep-4055 [01] 240.132371: 4055:120:S ==> 3997:120:R
  306. bash-3997 [01] 240.132454: 3997:120:R + 4055:120:S
  307. bash-3997 [01] 240.132457: 3997:120:R ==> 4055:120:R
  308. sleep-4055 [01] 240.132460: 4055:120:D ==> 3997:120:R
  309. bash-3997 [01] 240.132463: 3997:120:R + 4055:120:D
  310. bash-3997 [01] 240.132465: 3997:120:R ==> 4055:120:R
  311. <idle>-0 [00] 240.132589: 0:140:R + 4:115:S
  312. <idle>-0 [00] 240.132591: 0:140:R ==> 4:115:R
  313. ksoftirqd/0-4 [00] 240.132595: 4:115:S ==> 0:140:R
  314. <idle>-0 [00] 240.132598: 0:140:R + 4:115:S
  315. <idle>-0 [00] 240.132599: 0:140:R ==> 4:115:R
  316. ksoftirqd/0-4 [00] 240.132603: 4:115:S ==> 0:140:R
  317. sleep-4055 [01] 240.133058: 4055:120:S ==> 3997:120:R
  318. [...]
  319. As we have discussed previously about this format, the header shows
  320. the name of the trace and points to the options. The "FUNCTION"
  321. is a misnomer since here it represents the wake ups and context
  322. switches.
  323. The sched_switch file only lists the wake ups (represented with '+')
  324. and context switches ('==>') with the previous task or current task
  325. first followed by the next task or task waking up. The format for both
  326. of these is PID:KERNEL-PRIO:TASK-STATE. Remember that the KERNEL-PRIO
  327. is the inverse of the actual priority with zero (0) being the highest
  328. priority and the nice values starting at 100 (nice -20). Below is
  329. a quick chart to map the kernel priority to user land priorities.
  330. Kernel priority: 0 to 99 ==> user RT priority 99 to 0
  331. Kernel priority: 100 to 139 ==> user nice -20 to 19
  332. Kernel priority: 140 ==> idle task priority
  333. The task states are:
  334. R - running : wants to run, may not actually be running
  335. S - sleep : process is waiting to be woken up (handles signals)
  336. D - disk sleep (uninterruptible sleep) : process must be woken up
  337. (ignores signals)
  338. T - stopped : process suspended
  339. t - traced : process is being traced (with something like gdb)
  340. Z - zombie : process waiting to be cleaned up
  341. X - unknown
  342. ftrace_enabled
  343. --------------
  344. The following tracers (listed below) give different output depending
  345. on whether or not the sysctl ftrace_enabled is set. To set ftrace_enabled,
  346. one can either use the sysctl function or set it via the proc
  347. file system interface.
  348. sysctl kernel.ftrace_enabled=1
  349. or
  350. echo 1 > /proc/sys/kernel/ftrace_enabled
  351. To disable ftrace_enabled simply replace the '1' with '0' in
  352. the above commands.
  353. When ftrace_enabled is set the tracers will also record the functions
  354. that are within the trace. The descriptions of the tracers
  355. will also show an example with ftrace enabled.
  356. irqsoff
  357. -------
  358. When interrupts are disabled, the CPU can not react to any other
  359. external event (besides NMIs and SMIs). This prevents the timer
  360. interrupt from triggering or the mouse interrupt from letting the
  361. kernel know of a new mouse event. The result is a latency with the
  362. reaction time.
  363. The irqsoff tracer tracks the time for which interrupts are disabled.
  364. When a new maximum latency is hit, the tracer saves the trace leading up
  365. to that latency point so that every time a new maximum is reached, the old
  366. saved trace is discarded and the new trace is saved.
  367. To reset the maximum, echo 0 into tracing_max_latency. Here is an
  368. example:
  369. # echo irqsoff > /debug/tracing/current_tracer
  370. # echo 0 > /debug/tracing/tracing_max_latency
  371. # echo 1 > /debug/tracing/tracing_enabled
  372. # ls -ltr
  373. [...]
  374. # echo 0 > /debug/tracing/tracing_enabled
  375. # cat /debug/tracing/latency_trace
  376. # tracer: irqsoff
  377. #
  378. irqsoff latency trace v1.1.5 on 2.6.26
  379. --------------------------------------------------------------------
  380. latency: 12 us, #3/3, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  381. -----------------
  382. | task: bash-3730 (uid:0 nice:0 policy:0 rt_prio:0)
  383. -----------------
  384. => started at: sys_setpgid
  385. => ended at: sys_setpgid
  386. # _------=> CPU#
  387. # / _-----=> irqs-off
  388. # | / _----=> need-resched
  389. # || / _---=> hardirq/softirq
  390. # ||| / _--=> preempt-depth
  391. # |||| /
  392. # ||||| delay
  393. # cmd pid ||||| time | caller
  394. # \ / ||||| \ | /
  395. bash-3730 1d... 0us : _write_lock_irq (sys_setpgid)
  396. bash-3730 1d..1 1us+: _write_unlock_irq (sys_setpgid)
  397. bash-3730 1d..2 14us : trace_hardirqs_on (sys_setpgid)
  398. Here we see that that we had a latency of 12 microsecs (which is
  399. very good). The _write_lock_irq in sys_setpgid disabled interrupts.
  400. The difference between the 12 and the displayed timestamp 14us occurred
  401. because the clock was incremented between the time of recording the max
  402. latency and the time of recording the function that had that latency.
  403. Note the above example had ftrace_enabled not set. If we set the
  404. ftrace_enabled, we get a much larger output:
  405. # tracer: irqsoff
  406. #
  407. irqsoff latency trace v1.1.5 on 2.6.26-rc8
  408. --------------------------------------------------------------------
  409. latency: 50 us, #101/101, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  410. -----------------
  411. | task: ls-4339 (uid:0 nice:0 policy:0 rt_prio:0)
  412. -----------------
  413. => started at: __alloc_pages_internal
  414. => ended at: __alloc_pages_internal
  415. # _------=> CPU#
  416. # / _-----=> irqs-off
  417. # | / _----=> need-resched
  418. # || / _---=> hardirq/softirq
  419. # ||| / _--=> preempt-depth
  420. # |||| /
  421. # ||||| delay
  422. # cmd pid ||||| time | caller
  423. # \ / ||||| \ | /
  424. ls-4339 0...1 0us+: get_page_from_freelist (__alloc_pages_internal)
  425. ls-4339 0d..1 3us : rmqueue_bulk (get_page_from_freelist)
  426. ls-4339 0d..1 3us : _spin_lock (rmqueue_bulk)
  427. ls-4339 0d..1 4us : add_preempt_count (_spin_lock)
  428. ls-4339 0d..2 4us : __rmqueue (rmqueue_bulk)
  429. ls-4339 0d..2 5us : __rmqueue_smallest (__rmqueue)
  430. ls-4339 0d..2 5us : __mod_zone_page_state (__rmqueue_smallest)
  431. ls-4339 0d..2 6us : __rmqueue (rmqueue_bulk)
  432. ls-4339 0d..2 6us : __rmqueue_smallest (__rmqueue)
  433. ls-4339 0d..2 7us : __mod_zone_page_state (__rmqueue_smallest)
  434. ls-4339 0d..2 7us : __rmqueue (rmqueue_bulk)
  435. ls-4339 0d..2 8us : __rmqueue_smallest (__rmqueue)
  436. [...]
  437. ls-4339 0d..2 46us : __rmqueue_smallest (__rmqueue)
  438. ls-4339 0d..2 47us : __mod_zone_page_state (__rmqueue_smallest)
  439. ls-4339 0d..2 47us : __rmqueue (rmqueue_bulk)
  440. ls-4339 0d..2 48us : __rmqueue_smallest (__rmqueue)
  441. ls-4339 0d..2 48us : __mod_zone_page_state (__rmqueue_smallest)
  442. ls-4339 0d..2 49us : _spin_unlock (rmqueue_bulk)
  443. ls-4339 0d..2 49us : sub_preempt_count (_spin_unlock)
  444. ls-4339 0d..1 50us : get_page_from_freelist (__alloc_pages_internal)
  445. ls-4339 0d..2 51us : trace_hardirqs_on (__alloc_pages_internal)
  446. Here we traced a 50 microsecond latency. But we also see all the
  447. functions that were called during that time. Note that by enabling
  448. function tracing, we incur an added overhead. This overhead may
  449. extend the latency times. But nevertheless, this trace has provided
  450. some very helpful debugging information.
  451. preemptoff
  452. ----------
  453. When preemption is disabled, we may be able to receive interrupts but
  454. the task cannot be preempted and a higher priority task must wait
  455. for preemption to be enabled again before it can preempt a lower
  456. priority task.
  457. The preemptoff tracer traces the places that disable preemption.
  458. Like the irqsoff tracer, it records the maximum latency for which preemption
  459. was disabled. The control of preemptoff tracer is much like the irqsoff
  460. tracer.
  461. # echo preemptoff > /debug/tracing/current_tracer
  462. # echo 0 > /debug/tracing/tracing_max_latency
  463. # echo 1 > /debug/tracing/tracing_enabled
  464. # ls -ltr
  465. [...]
  466. # echo 0 > /debug/tracing/tracing_enabled
  467. # cat /debug/tracing/latency_trace
  468. # tracer: preemptoff
  469. #
  470. preemptoff latency trace v1.1.5 on 2.6.26-rc8
  471. --------------------------------------------------------------------
  472. latency: 29 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  473. -----------------
  474. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  475. -----------------
  476. => started at: do_IRQ
  477. => ended at: __do_softirq
  478. # _------=> CPU#
  479. # / _-----=> irqs-off
  480. # | / _----=> need-resched
  481. # || / _---=> hardirq/softirq
  482. # ||| / _--=> preempt-depth
  483. # |||| /
  484. # ||||| delay
  485. # cmd pid ||||| time | caller
  486. # \ / ||||| \ | /
  487. sshd-4261 0d.h. 0us+: irq_enter (do_IRQ)
  488. sshd-4261 0d.s. 29us : _local_bh_enable (__do_softirq)
  489. sshd-4261 0d.s1 30us : trace_preempt_on (__do_softirq)
  490. This has some more changes. Preemption was disabled when an interrupt
  491. came in (notice the 'h'), and was enabled while doing a softirq.
  492. (notice the 's'). But we also see that interrupts have been disabled
  493. when entering the preempt off section and leaving it (the 'd').
  494. We do not know if interrupts were enabled 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 ftrace_enabled
  550. set. Here we see that interrupts were disabled the entire time.
  551. The irq_enter code lets us know that we entered an interrupt 'h'.
  552. Before that, the functions being traced still show that it is not
  553. in an interrupt, but we can see from the functions themselves that
  554. this is not the case.
  555. Notice that __do_softirq when called does not have a preempt_count.
  556. It may seem that we missed a preempt enabling. What really happened
  557. is that the preempt count is held on the thread's stack and we
  558. switched to the softirq stack (4K stacks in effect). The code
  559. does not copy the preempt count, but because interrupts are disabled,
  560. we do not need to worry about it. Having a tracer like this is good
  561. for letting people know what really happens inside the kernel.
  562. preemptirqsoff
  563. --------------
  564. Knowing the locations that have interrupts disabled or preemption
  565. disabled for the longest times is helpful. But sometimes we would
  566. like to know when either preemption and/or interrupts are disabled.
  567. Consider the following code:
  568. local_irq_disable();
  569. call_function_with_irqs_off();
  570. preempt_disable();
  571. call_function_with_irqs_and_preemption_off();
  572. local_irq_enable();
  573. call_function_with_preemption_off();
  574. preempt_enable();
  575. The irqsoff tracer will record the total length of
  576. call_function_with_irqs_off() and
  577. call_function_with_irqs_and_preemption_off().
  578. The preemptoff tracer will record the total length of
  579. call_function_with_irqs_and_preemption_off() and
  580. call_function_with_preemption_off().
  581. But neither will trace the time that interrupts and/or preemption
  582. is disabled. This total time is the time that we can not schedule.
  583. To record this time, use the preemptirqsoff tracer.
  584. Again, using this trace is much like the irqsoff and preemptoff tracers.
  585. # echo preemptirqsoff > /debug/tracing/current_tracer
  586. # echo 0 > /debug/tracing/tracing_max_latency
  587. # echo 1 > /debug/tracing/tracing_enabled
  588. # ls -ltr
  589. [...]
  590. # echo 0 > /debug/tracing/tracing_enabled
  591. # cat /debug/tracing/latency_trace
  592. # tracer: preemptirqsoff
  593. #
  594. preemptirqsoff latency trace v1.1.5 on 2.6.26-rc8
  595. --------------------------------------------------------------------
  596. latency: 293 us, #3/3, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  597. -----------------
  598. | task: ls-4860 (uid:0 nice:0 policy:0 rt_prio:0)
  599. -----------------
  600. => started at: apic_timer_interrupt
  601. => ended at: __do_softirq
  602. # _------=> CPU#
  603. # / _-----=> irqs-off
  604. # | / _----=> need-resched
  605. # || / _---=> hardirq/softirq
  606. # ||| / _--=> preempt-depth
  607. # |||| /
  608. # ||||| delay
  609. # cmd pid ||||| time | caller
  610. # \ / ||||| \ | /
  611. ls-4860 0d... 0us!: trace_hardirqs_off_thunk (apic_timer_interrupt)
  612. ls-4860 0d.s. 294us : _local_bh_enable (__do_softirq)
  613. ls-4860 0d.s1 294us : trace_preempt_on (__do_softirq)
  614. The trace_hardirqs_off_thunk is called from assembly on x86 when
  615. interrupts are disabled in the assembly code. Without the function
  616. tracing, we do not know if interrupts were enabled within the preemption
  617. points. We do see that it started with preemption enabled.
  618. Here is a trace with ftrace_enabled set:
  619. # tracer: preemptirqsoff
  620. #
  621. preemptirqsoff latency trace v1.1.5 on 2.6.26-rc8
  622. --------------------------------------------------------------------
  623. latency: 105 us, #183/183, CPU#0 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  624. -----------------
  625. | task: sshd-4261 (uid:0 nice:0 policy:0 rt_prio:0)
  626. -----------------
  627. => started at: write_chan
  628. => ended at: __do_softirq
  629. # _------=> CPU#
  630. # / _-----=> irqs-off
  631. # | / _----=> need-resched
  632. # || / _---=> hardirq/softirq
  633. # ||| / _--=> preempt-depth
  634. # |||| /
  635. # ||||| delay
  636. # cmd pid ||||| time | caller
  637. # \ / ||||| \ | /
  638. ls-4473 0.N.. 0us : preempt_schedule (write_chan)
  639. ls-4473 0dN.1 1us : _spin_lock (schedule)
  640. ls-4473 0dN.1 2us : add_preempt_count (_spin_lock)
  641. ls-4473 0d..2 2us : put_prev_task_fair (schedule)
  642. [...]
  643. ls-4473 0d..2 13us : set_normalized_timespec (ktime_get_ts)
  644. ls-4473 0d..2 13us : __switch_to (schedule)
  645. sshd-4261 0d..2 14us : finish_task_switch (schedule)
  646. sshd-4261 0d..2 14us : _spin_unlock_irq (finish_task_switch)
  647. sshd-4261 0d..1 15us : add_preempt_count (_spin_lock_irqsave)
  648. sshd-4261 0d..2 16us : _spin_unlock_irqrestore (hrtick_set)
  649. sshd-4261 0d..2 16us : do_IRQ (common_interrupt)
  650. sshd-4261 0d..2 17us : irq_enter (do_IRQ)
  651. sshd-4261 0d..2 17us : idle_cpu (irq_enter)
  652. sshd-4261 0d..2 18us : add_preempt_count (irq_enter)
  653. sshd-4261 0d.h2 18us : idle_cpu (irq_enter)
  654. sshd-4261 0d.h. 18us : handle_fasteoi_irq (do_IRQ)
  655. sshd-4261 0d.h. 19us : _spin_lock (handle_fasteoi_irq)
  656. sshd-4261 0d.h. 19us : add_preempt_count (_spin_lock)
  657. sshd-4261 0d.h1 20us : _spin_unlock (handle_fasteoi_irq)
  658. sshd-4261 0d.h1 20us : sub_preempt_count (_spin_unlock)
  659. [...]
  660. sshd-4261 0d.h1 28us : _spin_unlock (handle_fasteoi_irq)
  661. sshd-4261 0d.h1 29us : sub_preempt_count (_spin_unlock)
  662. sshd-4261 0d.h2 29us : irq_exit (do_IRQ)
  663. sshd-4261 0d.h2 29us : sub_preempt_count (irq_exit)
  664. sshd-4261 0d..3 30us : do_softirq (irq_exit)
  665. sshd-4261 0d... 30us : __do_softirq (do_softirq)
  666. sshd-4261 0d... 31us : __local_bh_disable (__do_softirq)
  667. sshd-4261 0d... 31us+: add_preempt_count (__local_bh_disable)
  668. sshd-4261 0d.s4 34us : add_preempt_count (__local_bh_disable)
  669. [...]
  670. sshd-4261 0d.s3 43us : sub_preempt_count (local_bh_enable_ip)
  671. sshd-4261 0d.s4 44us : sub_preempt_count (local_bh_enable_ip)
  672. sshd-4261 0d.s3 44us : smp_apic_timer_interrupt (apic_timer_interrupt)
  673. sshd-4261 0d.s3 45us : irq_enter (smp_apic_timer_interrupt)
  674. sshd-4261 0d.s3 45us : idle_cpu (irq_enter)
  675. sshd-4261 0d.s3 46us : add_preempt_count (irq_enter)
  676. sshd-4261 0d.H3 46us : idle_cpu (irq_enter)
  677. sshd-4261 0d.H3 47us : hrtimer_interrupt (smp_apic_timer_interrupt)
  678. sshd-4261 0d.H3 47us : ktime_get (hrtimer_interrupt)
  679. [...]
  680. sshd-4261 0d.H3 81us : tick_program_event (hrtimer_interrupt)
  681. sshd-4261 0d.H3 82us : ktime_get (tick_program_event)
  682. sshd-4261 0d.H3 82us : ktime_get_ts (ktime_get)
  683. sshd-4261 0d.H3 83us : getnstimeofday (ktime_get_ts)
  684. sshd-4261 0d.H3 83us : set_normalized_timespec (ktime_get_ts)
  685. sshd-4261 0d.H3 84us : clockevents_program_event (tick_program_event)
  686. sshd-4261 0d.H3 84us : lapic_next_event (clockevents_program_event)
  687. sshd-4261 0d.H3 85us : irq_exit (smp_apic_timer_interrupt)
  688. sshd-4261 0d.H3 85us : sub_preempt_count (irq_exit)
  689. sshd-4261 0d.s4 86us : sub_preempt_count (irq_exit)
  690. sshd-4261 0d.s3 86us : add_preempt_count (__local_bh_disable)
  691. [...]
  692. sshd-4261 0d.s1 98us : sub_preempt_count (net_rx_action)
  693. sshd-4261 0d.s. 99us : add_preempt_count (_spin_lock_irq)
  694. sshd-4261 0d.s1 99us+: _spin_unlock_irq (run_timer_softirq)
  695. sshd-4261 0d.s. 104us : _local_bh_enable (__do_softirq)
  696. sshd-4261 0d.s. 104us : sub_preempt_count (_local_bh_enable)
  697. sshd-4261 0d.s. 105us : _local_bh_enable (__do_softirq)
  698. sshd-4261 0d.s1 105us : trace_preempt_on (__do_softirq)
  699. This is a very interesting trace. It started with the preemption of
  700. the ls task. We see that the task had the "need_resched" bit set
  701. via the 'N' in the trace. Interrupts were disabled before the spin_lock
  702. at the beginning of the trace. We see that a schedule took place to run
  703. sshd. When the interrupts were enabled, we took an interrupt.
  704. On return from the interrupt handler, the softirq ran. We took another
  705. interrupt while running the softirq as we see from the capital 'H'.
  706. wakeup
  707. ------
  708. In a Real-Time environment it is very important to know the wakeup
  709. time it takes for the highest priority task that is woken up to the
  710. time that it executes. This is also known as "schedule latency".
  711. I stress the point that this is about RT tasks. It is also important
  712. to know the scheduling latency of non-RT tasks, but the average
  713. schedule latency is better for non-RT tasks. Tools like
  714. LatencyTop are more appropriate for such measurements.
  715. Real-Time environments are interested in the worst case latency.
  716. That is the longest latency it takes for something to happen, and
  717. not the average. We can have a very fast scheduler that may only
  718. have a large latency once in a while, but that would not work well
  719. with Real-Time tasks. The wakeup tracer was designed to record
  720. the worst case wakeups of RT tasks. Non-RT tasks are not recorded
  721. because the tracer only records one worst case and tracing non-RT
  722. tasks that are unpredictable will overwrite the worst case latency
  723. of RT tasks.
  724. Since this tracer only deals with RT tasks, we will run this slightly
  725. differently than we did with the previous tracers. Instead of performing
  726. an 'ls', we will run 'sleep 1' under 'chrt' which changes the
  727. priority of the task.
  728. # echo wakeup > /debug/tracing/current_tracer
  729. # echo 0 > /debug/tracing/tracing_max_latency
  730. # echo 1 > /debug/tracing/tracing_enabled
  731. # chrt -f 5 sleep 1
  732. # echo 0 > /debug/tracing/tracing_enabled
  733. # cat /debug/tracing/latency_trace
  734. # tracer: wakeup
  735. #
  736. wakeup latency trace v1.1.5 on 2.6.26-rc8
  737. --------------------------------------------------------------------
  738. latency: 4 us, #2/2, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  739. -----------------
  740. | task: sleep-4901 (uid:0 nice:0 policy:1 rt_prio:5)
  741. -----------------
  742. # _------=> CPU#
  743. # / _-----=> irqs-off
  744. # | / _----=> need-resched
  745. # || / _---=> hardirq/softirq
  746. # ||| / _--=> preempt-depth
  747. # |||| /
  748. # ||||| delay
  749. # cmd pid ||||| time | caller
  750. # \ / ||||| \ | /
  751. <idle>-0 1d.h4 0us+: try_to_wake_up (wake_up_process)
  752. <idle>-0 1d..4 4us : schedule (cpu_idle)
  753. Running this on an idle system, we see that it only took 4 microseconds
  754. to perform the task switch. Note, since the trace marker in the
  755. schedule is before the actual "switch", we stop the tracing when
  756. the recorded task is about to schedule in. This may change if
  757. we add a new marker at the end of the scheduler.
  758. Notice that the recorded task is 'sleep' with the PID of 4901 and it
  759. has an rt_prio of 5. This priority is user-space priority and not
  760. the internal kernel priority. The policy is 1 for SCHED_FIFO and 2
  761. for SCHED_RR.
  762. Doing the same with chrt -r 5 and ftrace_enabled set.
  763. # tracer: wakeup
  764. #
  765. wakeup latency trace v1.1.5 on 2.6.26-rc8
  766. --------------------------------------------------------------------
  767. latency: 50 us, #60/60, CPU#1 | (M:preempt VP:0, KP:0, SP:0 HP:0 #P:2)
  768. -----------------
  769. | task: sleep-4068 (uid:0 nice:0 policy:2 rt_prio:5)
  770. -----------------
  771. # _------=> CPU#
  772. # / _-----=> irqs-off
  773. # | / _----=> need-resched
  774. # || / _---=> hardirq/softirq
  775. # ||| / _--=> preempt-depth
  776. # |||| /
  777. # ||||| delay
  778. # cmd pid ||||| time | caller
  779. # \ / ||||| \ | /
  780. ksoftirq-7 1d.H3 0us : try_to_wake_up (wake_up_process)
  781. ksoftirq-7 1d.H4 1us : sub_preempt_count (marker_probe_cb)
  782. ksoftirq-7 1d.H3 2us : check_preempt_wakeup (try_to_wake_up)
  783. ksoftirq-7 1d.H3 3us : update_curr (check_preempt_wakeup)
  784. ksoftirq-7 1d.H3 4us : calc_delta_mine (update_curr)
  785. ksoftirq-7 1d.H3 5us : __resched_task (check_preempt_wakeup)
  786. ksoftirq-7 1d.H3 6us : task_wake_up_rt (try_to_wake_up)
  787. ksoftirq-7 1d.H3 7us : _spin_unlock_irqrestore (try_to_wake_up)
  788. [...]
  789. ksoftirq-7 1d.H2 17us : irq_exit (smp_apic_timer_interrupt)
  790. ksoftirq-7 1d.H2 18us : sub_preempt_count (irq_exit)
  791. ksoftirq-7 1d.s3 19us : sub_preempt_count (irq_exit)
  792. ksoftirq-7 1..s2 20us : rcu_process_callbacks (__do_softirq)
  793. [...]
  794. ksoftirq-7 1..s2 26us : __rcu_process_callbacks (rcu_process_callbacks)
  795. ksoftirq-7 1d.s2 27us : _local_bh_enable (__do_softirq)
  796. ksoftirq-7 1d.s2 28us : sub_preempt_count (_local_bh_enable)
  797. ksoftirq-7 1.N.3 29us : sub_preempt_count (ksoftirqd)
  798. ksoftirq-7 1.N.2 30us : _cond_resched (ksoftirqd)
  799. ksoftirq-7 1.N.2 31us : __cond_resched (_cond_resched)
  800. ksoftirq-7 1.N.2 32us : add_preempt_count (__cond_resched)
  801. ksoftirq-7 1.N.2 33us : schedule (__cond_resched)
  802. ksoftirq-7 1.N.2 33us : add_preempt_count (schedule)
  803. ksoftirq-7 1.N.3 34us : hrtick_clear (schedule)
  804. ksoftirq-7 1dN.3 35us : _spin_lock (schedule)
  805. ksoftirq-7 1dN.3 36us : add_preempt_count (_spin_lock)
  806. ksoftirq-7 1d..4 37us : put_prev_task_fair (schedule)
  807. ksoftirq-7 1d..4 38us : update_curr (put_prev_task_fair)
  808. [...]
  809. ksoftirq-7 1d..5 47us : _spin_trylock (tracing_record_cmdline)
  810. ksoftirq-7 1d..5 48us : add_preempt_count (_spin_trylock)
  811. ksoftirq-7 1d..6 49us : _spin_unlock (tracing_record_cmdline)
  812. ksoftirq-7 1d..6 49us : sub_preempt_count (_spin_unlock)
  813. ksoftirq-7 1d..4 50us : schedule (__cond_resched)
  814. The interrupt went off while running ksoftirqd. This task runs at
  815. SCHED_OTHER. Why did not we see the 'N' set early? This may be
  816. a harmless bug with x86_32 and 4K stacks. On x86_32 with 4K stacks
  817. configured, the interrupt and softirq run with their own stack.
  818. Some information is held on the top of the task's stack (need_resched
  819. and preempt_count are both stored there). The setting of the NEED_RESCHED
  820. bit is done directly to the task's stack, but the reading of the
  821. NEED_RESCHED is done by looking at the current stack, which in this case
  822. is the stack for the hard interrupt. This hides the fact that NEED_RESCHED
  823. has been set. We do not see the 'N' until we switch back to the task's
  824. assigned stack.
  825. function
  826. --------
  827. This tracer is the function tracer. Enabling the function tracer
  828. can be done from the debug file system. Make sure the ftrace_enabled is
  829. set; otherwise this tracer is a nop.
  830. # sysctl kernel.ftrace_enabled=1
  831. # echo function > /debug/tracing/current_tracer
  832. # echo 1 > /debug/tracing/tracing_enabled
  833. # usleep 1
  834. # echo 0 > /debug/tracing/tracing_enabled
  835. # cat /debug/tracing/trace
  836. # tracer: function
  837. #
  838. # TASK-PID CPU# TIMESTAMP FUNCTION
  839. # | | | | |
  840. bash-4003 [00] 123.638713: finish_task_switch <-schedule
  841. bash-4003 [00] 123.638714: _spin_unlock_irq <-finish_task_switch
  842. bash-4003 [00] 123.638714: sub_preempt_count <-_spin_unlock_irq
  843. bash-4003 [00] 123.638715: hrtick_set <-schedule
  844. bash-4003 [00] 123.638715: _spin_lock_irqsave <-hrtick_set
  845. bash-4003 [00] 123.638716: add_preempt_count <-_spin_lock_irqsave
  846. bash-4003 [00] 123.638716: _spin_unlock_irqrestore <-hrtick_set
  847. bash-4003 [00] 123.638717: sub_preempt_count <-_spin_unlock_irqrestore
  848. bash-4003 [00] 123.638717: hrtick_clear <-hrtick_set
  849. bash-4003 [00] 123.638718: sub_preempt_count <-schedule
  850. bash-4003 [00] 123.638718: sub_preempt_count <-preempt_schedule
  851. bash-4003 [00] 123.638719: wait_for_completion <-__stop_machine_run
  852. bash-4003 [00] 123.638719: wait_for_common <-wait_for_completion
  853. bash-4003 [00] 123.638720: _spin_lock_irq <-wait_for_common
  854. bash-4003 [00] 123.638720: add_preempt_count <-_spin_lock_irq
  855. [...]
  856. Note: function tracer uses ring buffers to store the above entries.
  857. The newest data may overwrite the oldest data. Sometimes using echo to
  858. stop the trace is not sufficient because the tracing could have overwritten
  859. the data that you wanted to record. For this reason, it is sometimes better to
  860. disable tracing directly from a program. This allows you to stop the
  861. tracing at the point that you hit the part that you are interested in.
  862. To disable the tracing directly from a C program, something like following
  863. code snippet can be used:
  864. int trace_fd;
  865. [...]
  866. int main(int argc, char *argv[]) {
  867. [...]
  868. trace_fd = open("/debug/tracing/tracing_enabled", O_WRONLY);
  869. [...]
  870. if (condition_hit()) {
  871. write(trace_fd, "0", 1);
  872. }
  873. [...]
  874. }
  875. Note: Here we hard coded the path name. The debugfs mount is not
  876. guaranteed to be at /debug (and is more commonly at /sys/kernel/debug).
  877. For simple one time traces, the above is sufficent. For anything else,
  878. a search through /proc/mounts may be needed to find where the debugfs
  879. file-system is mounted.
  880. Single thread tracing
  881. ---------------------
  882. By writing into /debug/tracing/set_ftrace_pid you can trace a
  883. single thread. For example:
  884. # cat /debug/tracing/set_ftrace_pid
  885. no pid
  886. # echo 3111 > /debug/tracing/set_ftrace_pid
  887. # cat /debug/tracing/set_ftrace_pid
  888. 3111
  889. # echo function > /debug/tracing/current_tracer
  890. # cat /debug/tracing/trace | head
  891. # tracer: function
  892. #
  893. # TASK-PID CPU# TIMESTAMP FUNCTION
  894. # | | | | |
  895. yum-updatesd-3111 [003] 1637.254676: finish_task_switch <-thread_return
  896. yum-updatesd-3111 [003] 1637.254681: hrtimer_cancel <-schedule_hrtimeout_range
  897. yum-updatesd-3111 [003] 1637.254682: hrtimer_try_to_cancel <-hrtimer_cancel
  898. yum-updatesd-3111 [003] 1637.254683: lock_hrtimer_base <-hrtimer_try_to_cancel
  899. yum-updatesd-3111 [003] 1637.254685: fget_light <-do_sys_poll
  900. yum-updatesd-3111 [003] 1637.254686: pipe_poll <-do_sys_poll
  901. # echo -1 > /debug/tracing/set_ftrace_pid
  902. # cat /debug/tracing/trace |head
  903. # tracer: function
  904. #
  905. # TASK-PID CPU# TIMESTAMP FUNCTION
  906. # | | | | |
  907. ##### CPU 3 buffer started ####
  908. yum-updatesd-3111 [003] 1701.957688: free_poll_entry <-poll_freewait
  909. yum-updatesd-3111 [003] 1701.957689: remove_wait_queue <-free_poll_entry
  910. yum-updatesd-3111 [003] 1701.957691: fput <-free_poll_entry
  911. yum-updatesd-3111 [003] 1701.957692: audit_syscall_exit <-sysret_audit
  912. yum-updatesd-3111 [003] 1701.957693: path_put <-audit_syscall_exit
  913. If you want to trace a function when executing, you could use
  914. something like this simple program:
  915. #include <stdio.h>
  916. #include <stdlib.h>
  917. #include <sys/types.h>
  918. #include <sys/stat.h>
  919. #include <fcntl.h>
  920. #include <unistd.h>
  921. int main (int argc, char **argv)
  922. {
  923. if (argc < 1)
  924. exit(-1);
  925. if (fork() > 0) {
  926. int fd, ffd;
  927. char line[64];
  928. int s;
  929. ffd = open("/debug/tracing/current_tracer", O_WRONLY);
  930. if (ffd < 0)
  931. exit(-1);
  932. write(ffd, "nop", 3);
  933. fd = open("/debug/tracing/set_ftrace_pid", O_WRONLY);
  934. s = sprintf(line, "%d\n", getpid());
  935. write(fd, line, s);
  936. write(ffd, "function", 8);
  937. close(fd);
  938. close(ffd);
  939. execvp(argv[1], argv+1);
  940. }
  941. return 0;
  942. }
  943. hw-branch-tracer (x86 only)
  944. ---------------------------
  945. This tracer uses the x86 last branch tracing hardware feature to
  946. collect a branch trace on all cpus with relatively low overhead.
  947. The tracer uses a fixed-size circular buffer per cpu and only
  948. traces ring 0 branches. The trace file dumps that buffer in the
  949. following format:
  950. # tracer: hw-branch-tracer
  951. #
  952. # CPU# TO <- FROM
  953. 0 scheduler_tick+0xb5/0x1bf <- task_tick_idle+0x5/0x6
  954. 2 run_posix_cpu_timers+0x2b/0x72a <- run_posix_cpu_timers+0x25/0x72a
  955. 0 scheduler_tick+0x139/0x1bf <- scheduler_tick+0xed/0x1bf
  956. 0 scheduler_tick+0x17c/0x1bf <- scheduler_tick+0x148/0x1bf
  957. 2 run_posix_cpu_timers+0x9e/0x72a <- run_posix_cpu_timers+0x5e/0x72a
  958. 0 scheduler_tick+0x1b6/0x1bf <- scheduler_tick+0x1aa/0x1bf
  959. The tracer may be used to dump the trace for the oops'ing cpu on a
  960. kernel oops into the system log. To enable this, ftrace_dump_on_oops
  961. must be set. To set ftrace_dump_on_oops, one can either use the sysctl
  962. function or set it via the proc system interface.
  963. sysctl kernel.ftrace_dump_on_oops=1
  964. or
  965. echo 1 > /proc/sys/kernel/ftrace_dump_on_oops
  966. Here's an example of such a dump after a null pointer dereference in a
  967. kernel module:
  968. [57848.105921] BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
  969. [57848.106019] IP: [<ffffffffa0000006>] open+0x6/0x14 [oops]
  970. [57848.106019] PGD 2354e9067 PUD 2375e7067 PMD 0
  971. [57848.106019] Oops: 0002 [#1] SMP
  972. [57848.106019] last sysfs file: /sys/devices/pci0000:00/0000:00:1e.0/0000:20:05.0/local_cpus
  973. [57848.106019] Dumping ftrace buffer:
  974. [57848.106019] ---------------------------------
  975. [...]
  976. [57848.106019] 0 chrdev_open+0xe6/0x165 <- cdev_put+0x23/0x24
  977. [57848.106019] 0 chrdev_open+0x117/0x165 <- chrdev_open+0xfa/0x165
  978. [57848.106019] 0 chrdev_open+0x120/0x165 <- chrdev_open+0x11c/0x165
  979. [57848.106019] 0 chrdev_open+0x134/0x165 <- chrdev_open+0x12b/0x165
  980. [57848.106019] 0 open+0x0/0x14 [oops] <- chrdev_open+0x144/0x165
  981. [57848.106019] 0 page_fault+0x0/0x30 <- open+0x6/0x14 [oops]
  982. [57848.106019] 0 error_entry+0x0/0x5b <- page_fault+0x4/0x30
  983. [57848.106019] 0 error_kernelspace+0x0/0x31 <- error_entry+0x59/0x5b
  984. [57848.106019] 0 error_sti+0x0/0x1 <- error_kernelspace+0x2d/0x31
  985. [57848.106019] 0 page_fault+0x9/0x30 <- error_sti+0x0/0x1
  986. [57848.106019] 0 do_page_fault+0x0/0x881 <- page_fault+0x1a/0x30
  987. [...]
  988. [57848.106019] 0 do_page_fault+0x66b/0x881 <- is_prefetch+0x1ee/0x1f2
  989. [57848.106019] 0 do_page_fault+0x6e0/0x881 <- do_page_fault+0x67a/0x881
  990. [57848.106019] 0 oops_begin+0x0/0x96 <- do_page_fault+0x6e0/0x881
  991. [57848.106019] 0 trace_hw_branch_oops+0x0/0x2d <- oops_begin+0x9/0x96
  992. [...]
  993. [57848.106019] 0 ds_suspend_bts+0x2a/0xe3 <- ds_suspend_bts+0x1a/0xe3
  994. [57848.106019] ---------------------------------
  995. [57848.106019] CPU 0
  996. [57848.106019] Modules linked in: oops
  997. [57848.106019] Pid: 5542, comm: cat Tainted: G W 2.6.28 #23
  998. [57848.106019] RIP: 0010:[<ffffffffa0000006>] [<ffffffffa0000006>] open+0x6/0x14 [oops]
  999. [57848.106019] RSP: 0018:ffff880235457d48 EFLAGS: 00010246
  1000. [...]
  1001. dynamic ftrace
  1002. --------------
  1003. If CONFIG_DYNAMIC_FTRACE is set, the system will run with
  1004. virtually no overhead when function tracing is disabled. The way
  1005. this works is the mcount function call (placed at the start of
  1006. every kernel function, produced by the -pg switch in gcc), starts
  1007. of pointing to a simple return. (Enabling FTRACE will include the
  1008. -pg switch in the compiling of the kernel.)
  1009. At compile time every C file object is run through the
  1010. recordmcount.pl script (located in the scripts directory). This
  1011. script will process the C object using objdump to find all the
  1012. locations in the .text section that call mcount. (Note, only
  1013. the .text section is processed, since processing other sections
  1014. like .init.text may cause races due to those sections being freed).
  1015. A new section called "__mcount_loc" is created that holds references
  1016. to all the mcount call sites in the .text section. This section is
  1017. compiled back into the original object. The final linker will add
  1018. all these references into a single table.
  1019. On boot up, before SMP is initialized, the dynamic ftrace code
  1020. scans this table and updates all the locations into nops. It also
  1021. records the locations, which are added to the available_filter_functions
  1022. list. Modules are processed as they are loaded and before they are
  1023. executed. When a module is unloaded, it also removes its functions from
  1024. the ftrace function list. This is automatic in the module unload
  1025. code, and the module author does not need to worry about it.
  1026. When tracing is enabled, kstop_machine is called to prevent races
  1027. with the CPUS executing code being modified (which can cause the
  1028. CPU to do undesireable things), and the nops are patched back
  1029. to calls. But this time, they do not call mcount (which is just
  1030. a function stub). They now call into the ftrace infrastructure.
  1031. One special side-effect to the recording of the functions being
  1032. traced is that we can now selectively choose which functions we
  1033. wish to trace and which ones we want the mcount calls to remain as
  1034. nops.
  1035. Two files are used, one for enabling and one for disabling the tracing
  1036. of specified functions. They are:
  1037. set_ftrace_filter
  1038. and
  1039. set_ftrace_notrace
  1040. A list of available functions that you can add to these files is listed
  1041. in:
  1042. available_filter_functions
  1043. # cat /debug/tracing/available_filter_functions
  1044. put_prev_task_idle
  1045. kmem_cache_create
  1046. pick_next_task_rt
  1047. get_online_cpus
  1048. pick_next_task_fair
  1049. mutex_lock
  1050. [...]
  1051. If I am only interested in sys_nanosleep and hrtimer_interrupt:
  1052. # echo sys_nanosleep hrtimer_interrupt \
  1053. > /debug/tracing/set_ftrace_filter
  1054. # echo ftrace > /debug/tracing/current_tracer
  1055. # echo 1 > /debug/tracing/tracing_enabled
  1056. # usleep 1
  1057. # echo 0 > /debug/tracing/tracing_enabled
  1058. # cat /debug/tracing/trace
  1059. # tracer: ftrace
  1060. #
  1061. # TASK-PID CPU# TIMESTAMP FUNCTION
  1062. # | | | | |
  1063. usleep-4134 [00] 1317.070017: hrtimer_interrupt <-smp_apic_timer_interrupt
  1064. usleep-4134 [00] 1317.070111: sys_nanosleep <-syscall_call
  1065. <idle>-0 [00] 1317.070115: hrtimer_interrupt <-smp_apic_timer_interrupt
  1066. To see which functions are being traced, you can cat the file:
  1067. # cat /debug/tracing/set_ftrace_filter
  1068. hrtimer_interrupt
  1069. sys_nanosleep
  1070. Perhaps this is not enough. The filters also allow simple wild cards.
  1071. Only the following are currently available
  1072. <match>* - will match functions that begin with <match>
  1073. *<match> - will match functions that end with <match>
  1074. *<match>* - will match functions that have <match> in it
  1075. These are the only wild cards which are supported.
  1076. <match>*<match> will not work.
  1077. Note: It is better to use quotes to enclose the wild cards, otherwise
  1078. the shell may expand the parameters into names of files in the local
  1079. directory.
  1080. # echo 'hrtimer_*' > /debug/tracing/set_ftrace_filter
  1081. Produces:
  1082. # tracer: ftrace
  1083. #
  1084. # TASK-PID CPU# TIMESTAMP FUNCTION
  1085. # | | | | |
  1086. bash-4003 [00] 1480.611794: hrtimer_init <-copy_process
  1087. bash-4003 [00] 1480.611941: hrtimer_start <-hrtick_set
  1088. bash-4003 [00] 1480.611956: hrtimer_cancel <-hrtick_clear
  1089. bash-4003 [00] 1480.611956: hrtimer_try_to_cancel <-hrtimer_cancel
  1090. <idle>-0 [00] 1480.612019: hrtimer_get_next_event <-get_next_timer_interrupt
  1091. <idle>-0 [00] 1480.612025: hrtimer_get_next_event <-get_next_timer_interrupt
  1092. <idle>-0 [00] 1480.612032: hrtimer_get_next_event <-get_next_timer_interrupt
  1093. <idle>-0 [00] 1480.612037: hrtimer_get_next_event <-get_next_timer_interrupt
  1094. <idle>-0 [00] 1480.612382: hrtimer_get_next_event <-get_next_timer_interrupt
  1095. Notice that we lost the sys_nanosleep.
  1096. # cat /debug/tracing/set_ftrace_filter
  1097. hrtimer_run_queues
  1098. hrtimer_run_pending
  1099. hrtimer_init
  1100. hrtimer_cancel
  1101. hrtimer_try_to_cancel
  1102. hrtimer_forward
  1103. hrtimer_start
  1104. hrtimer_reprogram
  1105. hrtimer_force_reprogram
  1106. hrtimer_get_next_event
  1107. hrtimer_interrupt
  1108. hrtimer_nanosleep
  1109. hrtimer_wakeup
  1110. hrtimer_get_remaining
  1111. hrtimer_get_res
  1112. hrtimer_init_sleeper
  1113. This is because the '>' and '>>' act just like they do in bash.
  1114. To rewrite the filters, use '>'
  1115. To append to the filters, use '>>'
  1116. To clear out a filter so that all functions will be recorded again:
  1117. # echo > /debug/tracing/set_ftrace_filter
  1118. # cat /debug/tracing/set_ftrace_filter
  1119. #
  1120. Again, now we want to append.
  1121. # echo sys_nanosleep > /debug/tracing/set_ftrace_filter
  1122. # cat /debug/tracing/set_ftrace_filter
  1123. sys_nanosleep
  1124. # echo 'hrtimer_*' >> /debug/tracing/set_ftrace_filter
  1125. # cat /debug/tracing/set_ftrace_filter
  1126. hrtimer_run_queues
  1127. hrtimer_run_pending
  1128. hrtimer_init
  1129. hrtimer_cancel
  1130. hrtimer_try_to_cancel
  1131. hrtimer_forward
  1132. hrtimer_start
  1133. hrtimer_reprogram
  1134. hrtimer_force_reprogram
  1135. hrtimer_get_next_event
  1136. hrtimer_interrupt
  1137. sys_nanosleep
  1138. hrtimer_nanosleep
  1139. hrtimer_wakeup
  1140. hrtimer_get_remaining
  1141. hrtimer_get_res
  1142. hrtimer_init_sleeper
  1143. The set_ftrace_notrace prevents those functions from being traced.
  1144. # echo '*preempt*' '*lock*' > /debug/tracing/set_ftrace_notrace
  1145. Produces:
  1146. # tracer: ftrace
  1147. #
  1148. # TASK-PID CPU# TIMESTAMP FUNCTION
  1149. # | | | | |
  1150. bash-4043 [01] 115.281644: finish_task_switch <-schedule
  1151. bash-4043 [01] 115.281645: hrtick_set <-schedule
  1152. bash-4043 [01] 115.281645: hrtick_clear <-hrtick_set
  1153. bash-4043 [01] 115.281646: wait_for_completion <-__stop_machine_run
  1154. bash-4043 [01] 115.281647: wait_for_common <-wait_for_completion
  1155. bash-4043 [01] 115.281647: kthread_stop <-stop_machine_run
  1156. bash-4043 [01] 115.281648: init_waitqueue_head <-kthread_stop
  1157. bash-4043 [01] 115.281648: wake_up_process <-kthread_stop
  1158. bash-4043 [01] 115.281649: try_to_wake_up <-wake_up_process
  1159. We can see that there's no more lock or preempt tracing.
  1160. trace_pipe
  1161. ----------
  1162. The trace_pipe outputs the same content as the trace file, but the effect
  1163. on the tracing is different. Every read from trace_pipe is consumed.
  1164. This means that subsequent reads will be different. The trace
  1165. is live.
  1166. # echo function > /debug/tracing/current_tracer
  1167. # cat /debug/tracing/trace_pipe > /tmp/trace.out &
  1168. [1] 4153
  1169. # echo 1 > /debug/tracing/tracing_enabled
  1170. # usleep 1
  1171. # echo 0 > /debug/tracing/tracing_enabled
  1172. # cat /debug/tracing/trace
  1173. # tracer: function
  1174. #
  1175. # TASK-PID CPU# TIMESTAMP FUNCTION
  1176. # | | | | |
  1177. #
  1178. # cat /tmp/trace.out
  1179. bash-4043 [00] 41.267106: finish_task_switch <-schedule
  1180. bash-4043 [00] 41.267106: hrtick_set <-schedule
  1181. bash-4043 [00] 41.267107: hrtick_clear <-hrtick_set
  1182. bash-4043 [00] 41.267108: wait_for_completion <-__stop_machine_run
  1183. bash-4043 [00] 41.267108: wait_for_common <-wait_for_completion
  1184. bash-4043 [00] 41.267109: kthread_stop <-stop_machine_run
  1185. bash-4043 [00] 41.267109: init_waitqueue_head <-kthread_stop
  1186. bash-4043 [00] 41.267110: wake_up_process <-kthread_stop
  1187. bash-4043 [00] 41.267110: try_to_wake_up <-wake_up_process
  1188. bash-4043 [00] 41.267111: select_task_rq_rt <-try_to_wake_up
  1189. Note, reading the trace_pipe file will block until more input is added.
  1190. By changing the tracer, trace_pipe will issue an EOF. We needed
  1191. to set the function tracer _before_ we "cat" the trace_pipe file.
  1192. trace entries
  1193. -------------
  1194. Having too much or not enough data can be troublesome in diagnosing
  1195. an issue in the kernel. The file buffer_size_kb is used to modify
  1196. the size of the internal trace buffers. The number listed
  1197. is the number of entries that can be recorded per CPU. To know
  1198. the full size, multiply the number of possible CPUS with the
  1199. number of entries.
  1200. # cat /debug/tracing/buffer_size_kb
  1201. 1408 (units kilobytes)
  1202. Note, to modify this, you must have tracing completely disabled. To do that,
  1203. echo "nop" into the current_tracer. If the current_tracer is not set
  1204. to "nop", an EINVAL error will be returned.
  1205. # echo nop > /debug/tracing/current_tracer
  1206. # echo 10000 > /debug/tracing/buffer_size_kb
  1207. # cat /debug/tracing/buffer_size_kb
  1208. 10000 (units kilobytes)
  1209. The number of pages which will be allocated is limited to a percentage
  1210. of available memory. Allocating too much will produce an error.
  1211. # echo 1000000000000 > /debug/tracing/buffer_size_kb
  1212. -bash: echo: write error: Cannot allocate memory
  1213. # cat /debug/tracing/buffer_size_kb
  1214. 85