ftrace.txt 52 KB

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