ftrace.txt 50 KB

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