ftrace.txt 66 KB

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