profile-manual-usage.xml 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985
  1. <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  2. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
  3. [<!ENTITY % poky SYSTEM "../poky.ent"> %poky; ] >
  4. <chapter id='profile-manual-usage'>
  5. <title>Basic Usage (with examples) for each of the Yocto Tracing Tools</title>
  6. <para>
  7. This chapter presents basic usage examples for each of the tracing
  8. tools.
  9. </para>
  10. <section id='profile-manual-perf'>
  11. <title>perf</title>
  12. <para>
  13. The 'perf' tool is the profiling and tracing tool that comes
  14. bundled with the Linux kernel.
  15. </para>
  16. <para>
  17. Don't let the fact that it's part of the kernel fool you into thinking
  18. that it's only for tracing and profiling the kernel - you can indeed
  19. use it to trace and profile just the kernel, but you can also use it
  20. to profile specific applications separately (with or without kernel
  21. context), and you can also use it to trace and profile the kernel
  22. and all applications on the system simultaneously to gain a system-wide
  23. view of what's going on.
  24. </para>
  25. <para>
  26. In many ways, perf aims to be a superset of all the tracing and profiling
  27. tools available in Linux today, including all the other tools covered
  28. in this HOWTO. The past couple of years have seen perf subsume a lot
  29. of the functionality of those other tools and, at the same time, those
  30. other tools have removed large portions of their previous functionality
  31. and replaced it with calls to the equivalent functionality now
  32. implemented by the perf subsystem. Extrapolation suggests that at
  33. some point those other tools will simply become completely redundant
  34. and go away; until then, we'll cover those other tools in these pages
  35. and in many cases show how the same things can be accomplished in
  36. perf and the other tools when it seems useful to do so.
  37. </para>
  38. <para>
  39. The coverage below details some of the most common ways you'll likely
  40. want to apply the tool; full documentation can be found either within
  41. the tool itself or in the man pages at
  42. <ulink url='http://linux.die.net/man/1/perf'>perf(1)</ulink>.
  43. </para>
  44. <section id='perf-setup'>
  45. <title>Setup</title>
  46. <para>
  47. For this section, we'll assume you've already performed the basic
  48. setup outlined in the General Setup section.
  49. </para>
  50. <para>
  51. In particular, you'll get the most mileage out of perf if you
  52. profile an image built with the following in your
  53. <filename>local.conf</filename> file:
  54. <literallayout class='monospaced'>
  55. <ulink url='&YOCTO_DOCS_REF_URL;#var-INHIBIT_PACKAGE_STRIP'>INHIBIT_PACKAGE_STRIP</ulink> = "1"
  56. </literallayout>
  57. </para>
  58. <para>
  59. perf runs on the target system for the most part. You can archive
  60. profile data and copy it to the host for analysis, but for the
  61. rest of this document we assume you've ssh'ed to the host and
  62. will be running the perf commands on the target.
  63. </para>
  64. </section>
  65. <section id='perf-basic-usage'>
  66. <title>Basic Usage</title>
  67. <para>
  68. The perf tool is pretty much self-documenting. To remind yourself
  69. of the available commands, simply type 'perf', which will show you
  70. basic usage along with the available perf subcommands:
  71. <literallayout class='monospaced'>
  72. root@crownbay:~# perf
  73. usage: perf [--version] [--help] COMMAND [ARGS]
  74. The most commonly used perf commands are:
  75. annotate Read perf.data (created by perf record) and display annotated code
  76. archive Create archive with object files with build-ids found in perf.data file
  77. bench General framework for benchmark suites
  78. buildid-cache Manage build-id cache.
  79. buildid-list List the buildids in a perf.data file
  80. diff Read two perf.data files and display the differential profile
  81. evlist List the event names in a perf.data file
  82. inject Filter to augment the events stream with additional information
  83. kmem Tool to trace/measure kernel memory(slab) properties
  84. kvm Tool to trace/measure kvm guest os
  85. list List all symbolic event types
  86. lock Analyze lock events
  87. probe Define new dynamic tracepoints
  88. record Run a command and record its profile into perf.data
  89. report Read perf.data (created by perf record) and display the profile
  90. sched Tool to trace/measure scheduler properties (latencies)
  91. script Read perf.data (created by perf record) and display trace output
  92. stat Run a command and gather performance counter statistics
  93. test Runs sanity tests.
  94. timechart Tool to visualize total system behavior during a workload
  95. top System profiling tool.
  96. See 'perf help COMMAND' for more information on a specific command.
  97. </literallayout>
  98. </para>
  99. <section id='using-perf-to-do-basic-profiling'>
  100. <title>Using perf to do Basic Profiling</title>
  101. <para>
  102. As a simple test case, we'll profile the 'wget' of a fairly large
  103. file, which is a minimally interesting case because it has both
  104. file and network I/O aspects, and at least in the case of standard
  105. Yocto images, it's implemented as part of busybox, so the methods
  106. we use to analyze it can be used in a very similar way to the whole
  107. host of supported busybox applets in Yocto.
  108. <literallayout class='monospaced'>
  109. root@crownbay:~# rm linux-2.6.19.2.tar.bz2; \
  110. wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  111. </literallayout>
  112. The quickest and easiest way to get some basic overall data about
  113. what's going on for a particular workload is to profile it using
  114. 'perf stat'. 'perf stat' basically profiles using a few default
  115. counters and displays the summed counts at the end of the run:
  116. <literallayout class='monospaced'>
  117. root@crownbay:~# perf stat wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  118. Connecting to downloads.yoctoproject.org (140.211.169.59:80)
  119. linux-2.6.19.2.tar.b 100% |***************************************************| 41727k 0:00:00 ETA
  120. Performance counter stats for 'wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>':
  121. 4597.223902 task-clock # 0.077 CPUs utilized
  122. 23568 context-switches # 0.005 M/sec
  123. 68 CPU-migrations # 0.015 K/sec
  124. 241 page-faults # 0.052 K/sec
  125. 3045817293 cycles # 0.663 GHz
  126. &lt;not supported&gt; stalled-cycles-frontend
  127. &lt;not supported&gt; stalled-cycles-backend
  128. 858909167 instructions # 0.28 insns per cycle
  129. 165441165 branches # 35.987 M/sec
  130. 19550329 branch-misses # 11.82% of all branches
  131. 59.836627620 seconds time elapsed
  132. </literallayout>
  133. Many times such a simple-minded test doesn't yield much of
  134. interest, but sometimes it does (see Real-world Yocto bug
  135. (slow loop-mounted write speed)).
  136. </para>
  137. <para>
  138. Also, note that 'perf stat' isn't restricted to a fixed set of
  139. counters - basically any event listed in the output of 'perf list'
  140. can be tallied by 'perf stat'. For example, suppose we wanted to
  141. see a summary of all the events related to kernel memory
  142. allocation/freeing along with cache hits and misses:
  143. <literallayout class='monospaced'>
  144. root@crownbay:~# perf stat -e kmem:* -e cache-references -e cache-misses wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  145. Connecting to downloads.yoctoproject.org (140.211.169.59:80)
  146. linux-2.6.19.2.tar.b 100% |***************************************************| 41727k 0:00:00 ETA
  147. Performance counter stats for 'wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>':
  148. 5566 kmem:kmalloc
  149. 125517 kmem:kmem_cache_alloc
  150. 0 kmem:kmalloc_node
  151. 0 kmem:kmem_cache_alloc_node
  152. 34401 kmem:kfree
  153. 69920 kmem:kmem_cache_free
  154. 133 kmem:mm_page_free
  155. 41 kmem:mm_page_free_batched
  156. 11502 kmem:mm_page_alloc
  157. 11375 kmem:mm_page_alloc_zone_locked
  158. 0 kmem:mm_page_pcpu_drain
  159. 0 kmem:mm_page_alloc_extfrag
  160. 66848602 cache-references
  161. 2917740 cache-misses # 4.365 % of all cache refs
  162. 44.831023415 seconds time elapsed
  163. </literallayout>
  164. So 'perf stat' gives us a nice easy way to get a quick overview of
  165. what might be happening for a set of events, but normally we'd
  166. need a little more detail in order to understand what's going on
  167. in a way that we can act on in a useful way.
  168. </para>
  169. <para>
  170. To dive down into a next level of detail, we can use 'perf
  171. record'/'perf report' which will collect profiling data and
  172. present it to use using an interactive text-based UI (or
  173. simply as text if we specify --stdio to 'perf report').
  174. </para>
  175. <para>
  176. As our first attempt at profiling this workload, we'll simply
  177. run 'perf record', handing it the workload we want to profile
  178. (everything after 'perf record' and any perf options we hand
  179. it - here none - will be executed in a new shell). perf collects
  180. samples until the process exits and records them in a file named
  181. 'perf.data' in the current working directory.
  182. <literallayout class='monospaced'>
  183. root@crownbay:~# perf record wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  184. Connecting to downloads.yoctoproject.org (140.211.169.59:80)
  185. linux-2.6.19.2.tar.b 100% |************************************************| 41727k 0:00:00 ETA
  186. [ perf record: Woken up 1 times to write data ]
  187. [ perf record: Captured and wrote 0.176 MB perf.data (~7700 samples) ]
  188. </literallayout>
  189. To see the results in a 'text-based UI' (tui), simply run
  190. 'perf report', which will read the perf.data file in the current
  191. working directory and display the results in an interactive UI:
  192. <literallayout class='monospaced'>
  193. root@crownbay:~# perf report
  194. </literallayout>
  195. </para>
  196. <para>
  197. <imagedata fileref="figures/perf-wget-flat-stripped.png" width="6in" depth="7in" align="center" scalefit="1" />
  198. </para>
  199. <para>
  200. The above screenshot displays a 'flat' profile, one entry for
  201. each 'bucket' corresponding to the functions that were profiled
  202. during the profiling run, ordered from the most popular to the
  203. least (perf has options to sort in various orders and keys as
  204. well as display entries only above a certain threshold and so
  205. on - see the perf documentation for details). Note that this
  206. includes both userspace functions (entries containing a [.]) and
  207. kernel functions accounted to the process (entries containing
  208. a [k]). (perf has command-line modifiers that can be used to
  209. restrict the profiling to kernel or userspace, among others).
  210. </para>
  211. <para>
  212. Notice also that the above report shows an entry for 'busybox',
  213. which is the executable that implements 'wget' in Yocto, but that
  214. instead of a useful function name in that entry, it displays
  215. a not-so-friendly hex value instead. The steps below will show
  216. how to fix that problem.
  217. </para>
  218. <para>
  219. Before we do that, however, let's try running a different profile,
  220. one which shows something a little more interesting. The only
  221. difference between the new profile and the previous one is that
  222. we'll add the -g option, which will record not just the address
  223. of a sampled function, but the entire callchain to the sampled
  224. function as well:
  225. <literallayout class='monospaced'>
  226. root@crownbay:~# perf record -g wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  227. Connecting to downloads.yoctoproject.org (140.211.169.59:80)
  228. linux-2.6.19.2.tar.b 100% |************************************************| 41727k 0:00:00 ETA
  229. [ perf record: Woken up 3 times to write data ]
  230. [ perf record: Captured and wrote 0.652 MB perf.data (~28476 samples) ]
  231. root@crownbay:~# perf report
  232. </literallayout>
  233. </para>
  234. <para>
  235. <imagedata fileref="figures/perf-wget-g-copy-to-user-expanded-stripped.png" width="6in" depth="7in" align="center" scalefit="1" />
  236. </para>
  237. <para>
  238. Using the callgraph view, we can actually see not only which
  239. functions took the most time, but we can also see a summary of
  240. how those functions were called and learn something about how the
  241. program interacts with the kernel in the process.
  242. </para>
  243. <para>
  244. Notice that each entry in the above screenshot now contains a '+'
  245. on the left-hand side. This means that we can expand the entry and
  246. drill down into the callchains that feed into that entry.
  247. Pressing 'enter' on any one of them will expand the callchain
  248. (you can also press 'E' to expand them all at the same time or 'C'
  249. to collapse them all).
  250. </para>
  251. <para>
  252. In the screenshot above, we've toggled the __copy_to_user_ll()
  253. entry and several subnodes all the way down. This lets us see
  254. which callchains contributed to the profiled __copy_to_user_ll()
  255. function which contributed 1.77% to the total profile.
  256. </para>
  257. <para>
  258. As a bit of background explanation for these callchains, think
  259. about what happens at a high level when you run wget to get a file
  260. out on the network. Basically what happens is that the data comes
  261. into the kernel via the network connection (socket) and is passed
  262. to the userspace program 'wget' (which is actually a part of
  263. busybox, but that's not important for now), which takes the buffers
  264. the kernel passes to it and writes it to a disk file to save it.
  265. </para>
  266. <para>
  267. The part of this process that we're looking at in the above call
  268. stacks is the part where the kernel passes the data it's read from
  269. the socket down to wget i.e. a copy-to-user.
  270. </para>
  271. <para>
  272. Notice also that here there's also a case where the hex value
  273. is displayed in the callstack, here in the expanded
  274. sys_clock_gettime() function. Later we'll see it resolve to a
  275. userspace function call in busybox.
  276. </para>
  277. <para>
  278. <imagedata fileref="figures/perf-wget-g-copy-from-user-expanded-stripped.png" width="6in" depth="7in" align="center" scalefit="1" />
  279. </para>
  280. <para>
  281. The above screenshot shows the other half of the journey for the
  282. data - from the wget program's userspace buffers to disk. To get
  283. the buffers to disk, the wget program issues a write(2), which
  284. does a copy-from-user to the kernel, which then takes care via
  285. some circuitous path (probably also present somewhere in the
  286. profile data), to get it safely to disk.
  287. </para>
  288. <para>
  289. Now that we've seen the basic layout of the profile data and the
  290. basics of how to extract useful information out of it, let's get
  291. back to the task at hand and see if we can get some basic idea
  292. about where the time is spent in the program we're profiling,
  293. wget. Remember that wget is actually implemented as an applet
  294. in busybox, so while the process name is 'wget', the executable
  295. we're actually interested in is busybox. So let's expand the
  296. first entry containing busybox:
  297. </para>
  298. <para>
  299. <imagedata fileref="figures/perf-wget-busybox-expanded-stripped.png" width="6in" depth="7in" align="center" scalefit="1" />
  300. </para>
  301. <para>
  302. Again, before we expanded we saw that the function was labeled
  303. with a hex value instead of a symbol as with most of the kernel
  304. entries. Expanding the busybox entry doesn't make it any better.
  305. </para>
  306. <para>
  307. The problem is that perf can't find the symbol information for the
  308. busybox binary, which is actually stripped out by the Yocto build
  309. system.
  310. </para>
  311. <para>
  312. One way around that is to put the following in your
  313. <filename>local.conf</filename> file when you build the image:
  314. <literallayout class='monospaced'>
  315. <ulink url='&YOCTO_DOCS_REF_URL;#var-INHIBIT_PACKAGE_STRIP'>INHIBIT_PACKAGE_STRIP</ulink> = "1"
  316. </literallayout>
  317. However, we already have an image with the binaries stripped,
  318. so what can we do to get perf to resolve the symbols? Basically
  319. we need to install the debuginfo for the busybox package.
  320. </para>
  321. <para>
  322. To generate the debug info for the packages in the image, we can
  323. add dbg-pkgs to EXTRA_IMAGE_FEATURES in local.conf. For example:
  324. <literallayout class='monospaced'>
  325. EXTRA_IMAGE_FEATURES = "debug-tweaks tools-profile dbg-pkgs"
  326. </literallayout>
  327. Additionally, in order to generate the type of debuginfo that
  328. perf understands, we also need to set
  329. <ulink url='&YOCTO_DOCS_REF_URL;#var-PACKAGE_DEBUG_SPLIT_STYLE'><filename>PACKAGE_DEBUG_SPLIT_STYLE</filename></ulink>
  330. in the <filename>local.conf</filename> file:
  331. <literallayout class='monospaced'>
  332. PACKAGE_DEBUG_SPLIT_STYLE = 'debug-file-directory'
  333. </literallayout>
  334. Once we've done that, we can install the debuginfo for busybox.
  335. The debug packages once built can be found in
  336. build/tmp/deploy/rpm/* on the host system. Find the
  337. busybox-dbg-...rpm file and copy it to the target. For example:
  338. <literallayout class='monospaced'>
  339. [trz@empanada core2]$ scp /home/trz/yocto/crownbay-tracing-dbg/build/tmp/deploy/rpm/core2_32/busybox-dbg-1.20.2-r2.core2_32.rpm root@192.168.1.31:
  340. root@192.168.1.31's password:
  341. busybox-dbg-1.20.2-r2.core2_32.rpm 100% 1826KB 1.8MB/s 00:01
  342. </literallayout>
  343. Now install the debug rpm on the target:
  344. <literallayout class='monospaced'>
  345. root@crownbay:~# rpm -i busybox-dbg-1.20.2-r2.core2_32.rpm
  346. </literallayout>
  347. Now that the debuginfo is installed, we see that the busybox
  348. entries now display their functions symbolically:
  349. </para>
  350. <para>
  351. <imagedata fileref="figures/perf-wget-busybox-debuginfo.png" width="6in" depth="7in" align="center" scalefit="1" />
  352. </para>
  353. <para>
  354. If we expand one of the entries and press 'enter' on a leaf node,
  355. we're presented with a menu of actions we can take to get more
  356. information related to that entry:
  357. </para>
  358. <para>
  359. <imagedata fileref="figures/perf-wget-busybox-dso-zoom-menu.png" width="6in" depth="2in" align="center" scalefit="1" />
  360. </para>
  361. <para>
  362. One of these actions allows us to show a view that displays a
  363. busybox-centric view of the profiled functions (in this case we've
  364. also expanded all the nodes using the 'E' key):
  365. </para>
  366. <para>
  367. <imagedata fileref="figures/perf-wget-busybox-dso-zoom.png" width="6in" depth="7in" align="center" scalefit="1" />
  368. </para>
  369. <para>
  370. Finally, we can see that now that the busybox debuginfo is
  371. installed, the previously unresolved symbol in the
  372. sys_clock_gettime() entry mentioned previously is now resolved,
  373. and shows that the sys_clock_gettime system call that was the
  374. source of 6.75% of the copy-to-user overhead was initiated by
  375. the handle_input() busybox function:
  376. </para>
  377. <para>
  378. <imagedata fileref="figures/perf-wget-g-copy-to-user-expanded-debuginfo.png" width="6in" depth="7in" align="center" scalefit="1" />
  379. </para>
  380. <para>
  381. At the lowest level of detail, we can dive down to the assembly
  382. level and see which instructions caused the most overhead in a
  383. function. Pressing 'enter' on the 'udhcpc_main' function, we're
  384. again presented with a menu:
  385. </para>
  386. <para>
  387. <imagedata fileref="figures/perf-wget-busybox-annotate-menu.png" width="6in" depth="2in" align="center" scalefit="1" />
  388. </para>
  389. <para>
  390. Selecting 'Annotate udhcpc_main', we get a detailed listing of
  391. percentages by instruction for the udhcpc_main function. From the
  392. display, we can see that over 50% of the time spent in this
  393. function is taken up by a couple tests and the move of a
  394. constant (1) to a register:
  395. </para>
  396. <para>
  397. <imagedata fileref="figures/perf-wget-busybox-annotate-udhcpc.png" width="6in" depth="7in" align="center" scalefit="1" />
  398. </para>
  399. <para>
  400. As a segue into tracing, let's try another profile using a
  401. different counter, something other than the default 'cycles'.
  402. </para>
  403. <para>
  404. The tracing and profiling infrastructure in Linux has become
  405. unified in a way that allows us to use the same tool with a
  406. completely different set of counters, not just the standard
  407. hardware counters that traditional tools have had to restrict
  408. themselves to (of course the traditional tools can also make use
  409. of the expanded possibilities now available to them, and in some
  410. cases have, as mentioned previously).
  411. </para>
  412. <para>
  413. We can get a list of the available events that can be used to
  414. profile a workload via 'perf list':
  415. <literallayout class='monospaced'>
  416. root@crownbay:~# perf list
  417. List of pre-defined events (to be used in -e):
  418. cpu-cycles OR cycles [Hardware event]
  419. stalled-cycles-frontend OR idle-cycles-frontend [Hardware event]
  420. stalled-cycles-backend OR idle-cycles-backend [Hardware event]
  421. instructions [Hardware event]
  422. cache-references [Hardware event]
  423. cache-misses [Hardware event]
  424. branch-instructions OR branches [Hardware event]
  425. branch-misses [Hardware event]
  426. bus-cycles [Hardware event]
  427. ref-cycles [Hardware event]
  428. cpu-clock [Software event]
  429. task-clock [Software event]
  430. page-faults OR faults [Software event]
  431. minor-faults [Software event]
  432. major-faults [Software event]
  433. context-switches OR cs [Software event]
  434. cpu-migrations OR migrations [Software event]
  435. alignment-faults [Software event]
  436. emulation-faults [Software event]
  437. L1-dcache-loads [Hardware cache event]
  438. L1-dcache-load-misses [Hardware cache event]
  439. L1-dcache-prefetch-misses [Hardware cache event]
  440. L1-icache-loads [Hardware cache event]
  441. L1-icache-load-misses [Hardware cache event]
  442. .
  443. .
  444. .
  445. rNNN [Raw hardware event descriptor]
  446. cpu/t1=v1[,t2=v2,t3 ...]/modifier [Raw hardware event descriptor]
  447. (see 'perf list --help' on how to encode it)
  448. mem:&lt;addr&gt;[:access] [Hardware breakpoint]
  449. sunrpc:rpc_call_status [Tracepoint event]
  450. sunrpc:rpc_bind_status [Tracepoint event]
  451. sunrpc:rpc_connect_status [Tracepoint event]
  452. sunrpc:rpc_task_begin [Tracepoint event]
  453. skb:kfree_skb [Tracepoint event]
  454. skb:consume_skb [Tracepoint event]
  455. skb:skb_copy_datagram_iovec [Tracepoint event]
  456. net:net_dev_xmit [Tracepoint event]
  457. net:net_dev_queue [Tracepoint event]
  458. net:netif_receive_skb [Tracepoint event]
  459. net:netif_rx [Tracepoint event]
  460. napi:napi_poll [Tracepoint event]
  461. sock:sock_rcvqueue_full [Tracepoint event]
  462. sock:sock_exceed_buf_limit [Tracepoint event]
  463. udp:udp_fail_queue_rcv_skb [Tracepoint event]
  464. hda:hda_send_cmd [Tracepoint event]
  465. hda:hda_get_response [Tracepoint event]
  466. hda:hda_bus_reset [Tracepoint event]
  467. scsi:scsi_dispatch_cmd_start [Tracepoint event]
  468. scsi:scsi_dispatch_cmd_error [Tracepoint event]
  469. scsi:scsi_eh_wakeup [Tracepoint event]
  470. drm:drm_vblank_event [Tracepoint event]
  471. drm:drm_vblank_event_queued [Tracepoint event]
  472. drm:drm_vblank_event_delivered [Tracepoint event]
  473. random:mix_pool_bytes [Tracepoint event]
  474. random:mix_pool_bytes_nolock [Tracepoint event]
  475. random:credit_entropy_bits [Tracepoint event]
  476. gpio:gpio_direction [Tracepoint event]
  477. gpio:gpio_value [Tracepoint event]
  478. block:block_rq_abort [Tracepoint event]
  479. block:block_rq_requeue [Tracepoint event]
  480. block:block_rq_issue [Tracepoint event]
  481. block:block_bio_bounce [Tracepoint event]
  482. block:block_bio_complete [Tracepoint event]
  483. block:block_bio_backmerge [Tracepoint event]
  484. .
  485. .
  486. writeback:writeback_wake_thread [Tracepoint event]
  487. writeback:writeback_wake_forker_thread [Tracepoint event]
  488. writeback:writeback_bdi_register [Tracepoint event]
  489. .
  490. .
  491. writeback:writeback_single_inode_requeue [Tracepoint event]
  492. writeback:writeback_single_inode [Tracepoint event]
  493. kmem:kmalloc [Tracepoint event]
  494. kmem:kmem_cache_alloc [Tracepoint event]
  495. kmem:mm_page_alloc [Tracepoint event]
  496. kmem:mm_page_alloc_zone_locked [Tracepoint event]
  497. kmem:mm_page_pcpu_drain [Tracepoint event]
  498. kmem:mm_page_alloc_extfrag [Tracepoint event]
  499. vmscan:mm_vmscan_kswapd_sleep [Tracepoint event]
  500. vmscan:mm_vmscan_kswapd_wake [Tracepoint event]
  501. vmscan:mm_vmscan_wakeup_kswapd [Tracepoint event]
  502. vmscan:mm_vmscan_direct_reclaim_begin [Tracepoint event]
  503. .
  504. .
  505. module:module_get [Tracepoint event]
  506. module:module_put [Tracepoint event]
  507. module:module_request [Tracepoint event]
  508. sched:sched_kthread_stop [Tracepoint event]
  509. sched:sched_wakeup [Tracepoint event]
  510. sched:sched_wakeup_new [Tracepoint event]
  511. sched:sched_process_fork [Tracepoint event]
  512. sched:sched_process_exec [Tracepoint event]
  513. sched:sched_stat_runtime [Tracepoint event]
  514. rcu:rcu_utilization [Tracepoint event]
  515. workqueue:workqueue_queue_work [Tracepoint event]
  516. workqueue:workqueue_execute_end [Tracepoint event]
  517. signal:signal_generate [Tracepoint event]
  518. signal:signal_deliver [Tracepoint event]
  519. timer:timer_init [Tracepoint event]
  520. timer:timer_start [Tracepoint event]
  521. timer:hrtimer_cancel [Tracepoint event]
  522. timer:itimer_state [Tracepoint event]
  523. timer:itimer_expire [Tracepoint event]
  524. irq:irq_handler_entry [Tracepoint event]
  525. irq:irq_handler_exit [Tracepoint event]
  526. irq:softirq_entry [Tracepoint event]
  527. irq:softirq_exit [Tracepoint event]
  528. irq:softirq_raise [Tracepoint event]
  529. printk:console [Tracepoint event]
  530. task:task_newtask [Tracepoint event]
  531. task:task_rename [Tracepoint event]
  532. syscalls:sys_enter_socketcall [Tracepoint event]
  533. syscalls:sys_exit_socketcall [Tracepoint event]
  534. .
  535. .
  536. .
  537. syscalls:sys_enter_unshare [Tracepoint event]
  538. syscalls:sys_exit_unshare [Tracepoint event]
  539. raw_syscalls:sys_enter [Tracepoint event]
  540. raw_syscalls:sys_exit [Tracepoint event]
  541. </literallayout>
  542. </para>
  543. <informalexample>
  544. <emphasis>Tying it Together:</emphasis> These are exactly the same set of events defined
  545. by the trace event subsystem and exposed by
  546. ftrace/tracecmd/kernelshark as files in
  547. /sys/kernel/debug/tracing/events, by SystemTap as
  548. kernel.trace("tracepoint_name") and (partially) accessed by LTTng.
  549. </informalexample>
  550. <para>
  551. Only a subset of these would be of interest to us when looking at
  552. this workload, so let's choose the most likely subsystems
  553. (identified by the string before the colon in the Tracepoint events)
  554. and do a 'perf stat' run using only those wildcarded subsystems:
  555. <literallayout class='monospaced'>
  556. root@crownbay:~# perf stat -e skb:* -e net:* -e napi:* -e sched:* -e workqueue:* -e irq:* -e syscalls:* wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  557. Performance counter stats for 'wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>':
  558. 23323 skb:kfree_skb
  559. 0 skb:consume_skb
  560. 49897 skb:skb_copy_datagram_iovec
  561. 6217 net:net_dev_xmit
  562. 6217 net:net_dev_queue
  563. 7962 net:netif_receive_skb
  564. 2 net:netif_rx
  565. 8340 napi:napi_poll
  566. 0 sched:sched_kthread_stop
  567. 0 sched:sched_kthread_stop_ret
  568. 3749 sched:sched_wakeup
  569. 0 sched:sched_wakeup_new
  570. 0 sched:sched_switch
  571. 29 sched:sched_migrate_task
  572. 0 sched:sched_process_free
  573. 1 sched:sched_process_exit
  574. 0 sched:sched_wait_task
  575. 0 sched:sched_process_wait
  576. 0 sched:sched_process_fork
  577. 1 sched:sched_process_exec
  578. 0 sched:sched_stat_wait
  579. 2106519415641 sched:sched_stat_sleep
  580. 0 sched:sched_stat_iowait
  581. 147453613 sched:sched_stat_blocked
  582. 12903026955 sched:sched_stat_runtime
  583. 0 sched:sched_pi_setprio
  584. 3574 workqueue:workqueue_queue_work
  585. 3574 workqueue:workqueue_activate_work
  586. 0 workqueue:workqueue_execute_start
  587. 0 workqueue:workqueue_execute_end
  588. 16631 irq:irq_handler_entry
  589. 16631 irq:irq_handler_exit
  590. 28521 irq:softirq_entry
  591. 28521 irq:softirq_exit
  592. 28728 irq:softirq_raise
  593. 1 syscalls:sys_enter_sendmmsg
  594. 1 syscalls:sys_exit_sendmmsg
  595. 0 syscalls:sys_enter_recvmmsg
  596. 0 syscalls:sys_exit_recvmmsg
  597. 14 syscalls:sys_enter_socketcall
  598. 14 syscalls:sys_exit_socketcall
  599. .
  600. .
  601. .
  602. 16965 syscalls:sys_enter_read
  603. 16965 syscalls:sys_exit_read
  604. 12854 syscalls:sys_enter_write
  605. 12854 syscalls:sys_exit_write
  606. .
  607. .
  608. .
  609. 58.029710972 seconds time elapsed
  610. </literallayout>
  611. Let's pick one of these tracepoints and tell perf to do a profile
  612. using it as the sampling event:
  613. <literallayout class='monospaced'>
  614. root@crownbay:~# perf record -g -e sched:sched_wakeup wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  615. </literallayout>
  616. </para>
  617. <para>
  618. <imagedata fileref="figures/sched-wakeup-profile.png" width="6in" depth="7in" align="center" scalefit="1" />
  619. </para>
  620. <para>
  621. The screenshot above shows the results of running a profile using
  622. sched:sched_switch tracepoint, which shows the relative costs of
  623. various paths to sched_wakeup (note that sched_wakeup is the
  624. name of the tracepoint - it's actually defined just inside
  625. ttwu_do_wakeup(), which accounts for the function name actually
  626. displayed in the profile:
  627. <literallayout class='monospaced'>
  628. /*
  629. * Mark the task runnable and perform wakeup-preemption.
  630. */
  631. static void
  632. ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  633. {
  634. trace_sched_wakeup(p, true);
  635. .
  636. .
  637. .
  638. }
  639. </literallayout>
  640. A couple of the more interesting callchains are expanded and
  641. displayed above, basically some network receive paths that
  642. presumably end up waking up wget (busybox) when network data is
  643. ready.
  644. </para>
  645. <para>
  646. Note that because tracepoints are normally used for tracing,
  647. the default sampling period for tracepoints is 1 i.e. for
  648. tracepoints perf will sample on every event occurrence (this
  649. can be changed using the -c option). This is in contrast to
  650. hardware counters such as for example the default 'cycles'
  651. hardware counter used for normal profiling, where sampling
  652. periods are much higher (in the thousands) because profiling should
  653. have as low an overhead as possible and sampling on every cycle
  654. would be prohibitively expensive.
  655. </para>
  656. </section>
  657. <section id='using-perf-to-do-basic-tracing'>
  658. <title>Using perf to do Basic Tracing</title>
  659. <para>
  660. Profiling is a great tool for solving many problems or for
  661. getting a high-level view of what's going on with a workload or
  662. across the system. It is however by definition an approximation,
  663. as suggested by the most prominent word associated with it,
  664. 'sampling'. On the one hand, it allows a representative picture of
  665. what's going on in the system to be cheaply taken, but on the other
  666. hand, that cheapness limits its utility when that data suggests a
  667. need to 'dive down' more deeply to discover what's really going
  668. on. In such cases, the only way to see what's really going on is
  669. to be able to look at (or summarize more intelligently) the
  670. individual steps that go into the higher-level behavior exposed
  671. by the coarse-grained profiling data.
  672. </para>
  673. <para>
  674. As a concrete example, we can trace all the events we think might
  675. be applicable to our workload:
  676. <literallayout class='monospaced'>
  677. root@crownbay:~# perf record -g -e skb:* -e net:* -e napi:* -e sched:sched_switch -e sched:sched_wakeup -e irq:*
  678. -e syscalls:sys_enter_read -e syscalls:sys_exit_read -e syscalls:sys_enter_write -e syscalls:sys_exit_write
  679. wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  680. </literallayout>
  681. We can look at the raw trace output using 'perf script' with no
  682. arguments:
  683. <literallayout class='monospaced'>
  684. root@crownbay:~# perf script
  685. perf 1262 [000] 11624.857082: sys_exit_read: 0x0
  686. perf 1262 [000] 11624.857193: sched_wakeup: comm=migration/0 pid=6 prio=0 success=1 target_cpu=000
  687. wget 1262 [001] 11624.858021: softirq_raise: vec=1 [action=TIMER]
  688. wget 1262 [001] 11624.858074: softirq_entry: vec=1 [action=TIMER]
  689. wget 1262 [001] 11624.858081: softirq_exit: vec=1 [action=TIMER]
  690. wget 1262 [001] 11624.858166: sys_enter_read: fd: 0x0003, buf: 0xbf82c940, count: 0x0200
  691. wget 1262 [001] 11624.858177: sys_exit_read: 0x200
  692. wget 1262 [001] 11624.858878: kfree_skb: skbaddr=0xeb248d80 protocol=0 location=0xc15a5308
  693. wget 1262 [001] 11624.858945: kfree_skb: skbaddr=0xeb248000 protocol=0 location=0xc15a5308
  694. wget 1262 [001] 11624.859020: softirq_raise: vec=1 [action=TIMER]
  695. wget 1262 [001] 11624.859076: softirq_entry: vec=1 [action=TIMER]
  696. wget 1262 [001] 11624.859083: softirq_exit: vec=1 [action=TIMER]
  697. wget 1262 [001] 11624.859167: sys_enter_read: fd: 0x0003, buf: 0xb7720000, count: 0x0400
  698. wget 1262 [001] 11624.859192: sys_exit_read: 0x1d7
  699. wget 1262 [001] 11624.859228: sys_enter_read: fd: 0x0003, buf: 0xb7720000, count: 0x0400
  700. wget 1262 [001] 11624.859233: sys_exit_read: 0x0
  701. wget 1262 [001] 11624.859573: sys_enter_read: fd: 0x0003, buf: 0xbf82c580, count: 0x0200
  702. wget 1262 [001] 11624.859584: sys_exit_read: 0x200
  703. wget 1262 [001] 11624.859864: sys_enter_read: fd: 0x0003, buf: 0xb7720000, count: 0x0400
  704. wget 1262 [001] 11624.859888: sys_exit_read: 0x400
  705. wget 1262 [001] 11624.859935: sys_enter_read: fd: 0x0003, buf: 0xb7720000, count: 0x0400
  706. wget 1262 [001] 11624.859944: sys_exit_read: 0x400
  707. </literallayout>
  708. This gives us a detailed timestamped sequence of events that
  709. occurred within the workload with respect to those events.
  710. </para>
  711. <para>
  712. In many ways, profiling can be viewed as a subset of tracing -
  713. theoretically, if you have a set of trace events that's sufficient
  714. to capture all the important aspects of a workload, you can derive
  715. any of the results or views that a profiling run can.
  716. </para>
  717. <para>
  718. Another aspect of traditional profiling is that while powerful in
  719. many ways, it's limited by the granularity of the underlying data.
  720. Profiling tools offer various ways of sorting and presenting the
  721. sample data, which make it much more useful and amenable to user
  722. experimentation, but in the end it can't be used in an open-ended
  723. way to extract data that just isn't present as a consequence of
  724. the fact that conceptually, most of it has been thrown away.
  725. </para>
  726. <para>
  727. Full-blown detailed tracing data does however offer the opportunity
  728. to manipulate and present the information collected during a
  729. tracing run in an infinite variety of ways.
  730. </para>
  731. <para>
  732. Another way to look at it is that there are only so many ways that
  733. the 'primitive' counters can be used on their own to generate
  734. interesting output; to get anything more complicated than simple
  735. counts requires some amount of additional logic, which is typically
  736. very specific to the problem at hand. For example, if we wanted to
  737. make use of a 'counter' that maps to the value of the time
  738. difference between when a process was scheduled to run on a
  739. processor and the time it actually ran, we wouldn't expect such
  740. a counter to exist on its own, but we could derive one called say
  741. 'wakeup_latency' and use it to extract a useful view of that metric
  742. from trace data. Likewise, we really can't figure out from standard
  743. profiling tools how much data every process on the system reads and
  744. writes, along with how many of those reads and writes fail
  745. completely. If we have sufficient trace data, however, we could
  746. with the right tools easily extract and present that information,
  747. but we'd need something other than pre-canned profiling tools to
  748. do that.
  749. </para>
  750. <para>
  751. Luckily, there is a general-purpose way to handle such needs,
  752. called 'programming languages'. Making programming languages
  753. easily available to apply to such problems given the specific
  754. format of data is called a 'programming language binding' for
  755. that data and language. Perf supports two programming language
  756. bindings, one for Python and one for Perl.
  757. </para>
  758. <informalexample>
  759. <emphasis>Tying it Together:</emphasis> Language bindings for manipulating and
  760. aggregating trace data are of course not a new
  761. idea. One of the first projects to do this was IBM's DProbes
  762. dpcc compiler, an ANSI C compiler which targeted a low-level
  763. assembly language running on an in-kernel interpreter on the
  764. target system. This is exactly analogous to what Sun's DTrace
  765. did, except that DTrace invented its own language for the purpose.
  766. Systemtap, heavily inspired by DTrace, also created its own
  767. one-off language, but rather than running the product on an
  768. in-kernel interpreter, created an elaborate compiler-based
  769. machinery to translate its language into kernel modules written
  770. in C.
  771. </informalexample>
  772. <para>
  773. Now that we have the trace data in perf.data, we can use
  774. 'perf script -g' to generate a skeleton script with handlers
  775. for the read/write entry/exit events we recorded:
  776. <literallayout class='monospaced'>
  777. root@crownbay:~# perf script -g python
  778. generated Python script: perf-script.py
  779. </literallayout>
  780. The skeleton script simply creates a python function for each
  781. event type in the perf.data file. The body of each function simply
  782. prints the event name along with its parameters. For example:
  783. <literallayout class='monospaced'>
  784. def net__netif_rx(event_name, context, common_cpu,
  785. common_secs, common_nsecs, common_pid, common_comm,
  786. skbaddr, len, name):
  787. print_header(event_name, common_cpu, common_secs, common_nsecs,
  788. common_pid, common_comm)
  789. print "skbaddr=%u, len=%u, name=%s\n" % (skbaddr, len, name),
  790. </literallayout>
  791. We can run that script directly to print all of the events
  792. contained in the perf.data file:
  793. <literallayout class='monospaced'>
  794. root@crownbay:~# perf script -s perf-script.py
  795. in trace_begin
  796. syscalls__sys_exit_read 0 11624.857082795 1262 perf nr=3, ret=0
  797. sched__sched_wakeup 0 11624.857193498 1262 perf comm=migration/0, pid=6, prio=0, success=1, target_cpu=0
  798. irq__softirq_raise 1 11624.858021635 1262 wget vec=TIMER
  799. irq__softirq_entry 1 11624.858074075 1262 wget vec=TIMER
  800. irq__softirq_exit 1 11624.858081389 1262 wget vec=TIMER
  801. syscalls__sys_enter_read 1 11624.858166434 1262 wget nr=3, fd=3, buf=3213019456, count=512
  802. syscalls__sys_exit_read 1 11624.858177924 1262 wget nr=3, ret=512
  803. skb__kfree_skb 1 11624.858878188 1262 wget skbaddr=3945041280, location=3243922184, protocol=0
  804. skb__kfree_skb 1 11624.858945608 1262 wget skbaddr=3945037824, location=3243922184, protocol=0
  805. irq__softirq_raise 1 11624.859020942 1262 wget vec=TIMER
  806. irq__softirq_entry 1 11624.859076935 1262 wget vec=TIMER
  807. irq__softirq_exit 1 11624.859083469 1262 wget vec=TIMER
  808. syscalls__sys_enter_read 1 11624.859167565 1262 wget nr=3, fd=3, buf=3077701632, count=1024
  809. syscalls__sys_exit_read 1 11624.859192533 1262 wget nr=3, ret=471
  810. syscalls__sys_enter_read 1 11624.859228072 1262 wget nr=3, fd=3, buf=3077701632, count=1024
  811. syscalls__sys_exit_read 1 11624.859233707 1262 wget nr=3, ret=0
  812. syscalls__sys_enter_read 1 11624.859573008 1262 wget nr=3, fd=3, buf=3213018496, count=512
  813. syscalls__sys_exit_read 1 11624.859584818 1262 wget nr=3, ret=512
  814. syscalls__sys_enter_read 1 11624.859864562 1262 wget nr=3, fd=3, buf=3077701632, count=1024
  815. syscalls__sys_exit_read 1 11624.859888770 1262 wget nr=3, ret=1024
  816. syscalls__sys_enter_read 1 11624.859935140 1262 wget nr=3, fd=3, buf=3077701632, count=1024
  817. syscalls__sys_exit_read 1 11624.859944032 1262 wget nr=3, ret=1024
  818. </literallayout>
  819. That in itself isn't very useful; after all, we can accomplish
  820. pretty much the same thing by simply running 'perf script'
  821. without arguments in the same directory as the perf.data file.
  822. </para>
  823. <para>
  824. We can however replace the print statements in the generated
  825. function bodies with whatever we want, and thereby make it
  826. infinitely more useful.
  827. </para>
  828. <para>
  829. As a simple example, let's just replace the print statements in
  830. the function bodies with a simple function that does nothing but
  831. increment a per-event count. When the program is run against a
  832. perf.data file, each time a particular event is encountered,
  833. a tally is incremented for that event. For example:
  834. <literallayout class='monospaced'>
  835. def net__netif_rx(event_name, context, common_cpu,
  836. common_secs, common_nsecs, common_pid, common_comm,
  837. skbaddr, len, name):
  838. inc_counts(event_name)
  839. </literallayout>
  840. Each event handler function in the generated code is modified
  841. to do this. For convenience, we define a common function called
  842. inc_counts() that each handler calls; inc_counts() simply tallies
  843. a count for each event using the 'counts' hash, which is a
  844. specialized hash function that does Perl-like autovivification, a
  845. capability that's extremely useful for kinds of multi-level
  846. aggregation commonly used in processing traces (see perf's
  847. documentation on the Python language binding for details):
  848. <literallayout class='monospaced'>
  849. counts = autodict()
  850. def inc_counts(event_name):
  851. try:
  852. counts[event_name] += 1
  853. except TypeError:
  854. counts[event_name] = 1
  855. </literallayout>
  856. Finally, at the end of the trace processing run, we want to
  857. print the result of all the per-event tallies. For that, we
  858. use the special 'trace_end()' function:
  859. <literallayout class='monospaced'>
  860. def trace_end():
  861. for event_name, count in counts.iteritems():
  862. print "%-40s %10s\n" % (event_name, count)
  863. </literallayout>
  864. The end result is a summary of all the events recorded in the
  865. trace:
  866. <literallayout class='monospaced'>
  867. skb__skb_copy_datagram_iovec 13148
  868. irq__softirq_entry 4796
  869. irq__irq_handler_exit 3805
  870. irq__softirq_exit 4795
  871. syscalls__sys_enter_write 8990
  872. net__net_dev_xmit 652
  873. skb__kfree_skb 4047
  874. sched__sched_wakeup 1155
  875. irq__irq_handler_entry 3804
  876. irq__softirq_raise 4799
  877. net__net_dev_queue 652
  878. syscalls__sys_enter_read 17599
  879. net__netif_receive_skb 1743
  880. syscalls__sys_exit_read 17598
  881. net__netif_rx 2
  882. napi__napi_poll 1877
  883. syscalls__sys_exit_write 8990
  884. </literallayout>
  885. Note that this is pretty much exactly the same information we get
  886. from 'perf stat', which goes a little way to support the idea
  887. mentioned previously that given the right kind of trace data,
  888. higher-level profiling-type summaries can be derived from it.
  889. </para>
  890. <para>
  891. Documentation on using the
  892. <ulink url='http://linux.die.net/man/1/perf-script-python'>'perf script' python binding</ulink>.
  893. </para>
  894. </section>
  895. <section id='system-wide-tracing-and-profiling'>
  896. <title>System-Wide Tracing and Profiling</title>
  897. <para>
  898. The examples so far have focused on tracing a particular program or
  899. workload - in other words, every profiling run has specified the
  900. program to profile in the command-line e.g. 'perf record wget ...'.
  901. </para>
  902. <para>
  903. It's also possible, and more interesting in many cases, to run a
  904. system-wide profile or trace while running the workload in a
  905. separate shell.
  906. </para>
  907. <para>
  908. To do system-wide profiling or tracing, you typically use
  909. the -a flag to 'perf record'.
  910. </para>
  911. <para>
  912. To demonstrate this, open up one window and start the profile
  913. using the -a flag (press Ctrl-C to stop tracing):
  914. <literallayout class='monospaced'>
  915. root@crownbay:~# perf record -g -a
  916. ^C[ perf record: Woken up 6 times to write data ]
  917. [ perf record: Captured and wrote 1.400 MB perf.data (~61172 samples) ]
  918. </literallayout>
  919. In another window, run the wget test:
  920. <literallayout class='monospaced'>
  921. root@crownbay:~# wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>
  922. Connecting to downloads.yoctoproject.org (140.211.169.59:80)
  923. linux-2.6.19.2.tar.b 100% |*******************************| 41727k 0:00:00 ETA
  924. </literallayout>
  925. Here we see entries not only for our wget load, but for other
  926. processes running on the system as well:
  927. </para>
  928. <para>
  929. <imagedata fileref="figures/perf-systemwide.png" width="6in" depth="7in" align="center" scalefit="1" />
  930. </para>
  931. <para>
  932. In the snapshot above, we can see callchains that originate in
  933. libc, and a callchain from Xorg that demonstrates that we're
  934. using a proprietary X driver in userspace (notice the presence
  935. of 'PVR' and some other unresolvable symbols in the expanded
  936. Xorg callchain).
  937. </para>
  938. <para>
  939. Note also that we have both kernel and userspace entries in the
  940. above snapshot. We can also tell perf to focus on userspace but
  941. providing a modifier, in this case 'u', to the 'cycles' hardware
  942. counter when we record a profile:
  943. <literallayout class='monospaced'>
  944. root@crownbay:~# perf record -g -a -e cycles:u
  945. ^C[ perf record: Woken up 2 times to write data ]
  946. [ perf record: Captured and wrote 0.376 MB perf.data (~16443 samples) ]
  947. </literallayout>
  948. </para>
  949. <para>
  950. <imagedata fileref="figures/perf-report-cycles-u.png" width="6in" depth="7in" align="center" scalefit="1" />
  951. </para>
  952. <para>
  953. Notice in the screenshot above, we see only userspace entries ([.])
  954. </para>
  955. <para>
  956. Finally, we can press 'enter' on a leaf node and select the 'Zoom
  957. into DSO' menu item to show only entries associated with a
  958. specific DSO. In the screenshot below, we've zoomed into the
  959. 'libc' DSO which shows all the entries associated with the
  960. libc-xxx.so DSO.
  961. </para>
  962. <para>
  963. <imagedata fileref="figures/perf-systemwide-libc.png" width="6in" depth="7in" align="center" scalefit="1" />
  964. </para>
  965. <para>
  966. We can also use the system-wide -a switch to do system-wide
  967. tracing. Here we'll trace a couple of scheduler events:
  968. <literallayout class='monospaced'>
  969. root@crownbay:~# perf record -a -e sched:sched_switch -e sched:sched_wakeup
  970. ^C[ perf record: Woken up 38 times to write data ]
  971. [ perf record: Captured and wrote 9.780 MB perf.data (~427299 samples) ]
  972. </literallayout>
  973. We can look at the raw output using 'perf script' with no
  974. arguments:
  975. <literallayout class='monospaced'>
  976. root@crownbay:~# perf script
  977. perf 1383 [001] 6171.460045: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  978. perf 1383 [001] 6171.460066: sched_switch: prev_comm=perf prev_pid=1383 prev_prio=120 prev_state=R+ ==> next_comm=kworker/1:1 next_pid=21 next_prio=120
  979. kworker/1:1 21 [001] 6171.460093: sched_switch: prev_comm=kworker/1:1 prev_pid=21 prev_prio=120 prev_state=S ==> next_comm=perf next_pid=1383 next_prio=120
  980. swapper 0 [000] 6171.468063: sched_wakeup: comm=kworker/0:3 pid=1209 prio=120 success=1 target_cpu=000
  981. swapper 0 [000] 6171.468107: sched_switch: prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=kworker/0:3 next_pid=1209 next_prio=120
  982. kworker/0:3 1209 [000] 6171.468143: sched_switch: prev_comm=kworker/0:3 prev_pid=1209 prev_prio=120 prev_state=S ==> next_comm=swapper/0 next_pid=0 next_prio=120
  983. perf 1383 [001] 6171.470039: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  984. perf 1383 [001] 6171.470058: sched_switch: prev_comm=perf prev_pid=1383 prev_prio=120 prev_state=R+ ==> next_comm=kworker/1:1 next_pid=21 next_prio=120
  985. kworker/1:1 21 [001] 6171.470082: sched_switch: prev_comm=kworker/1:1 prev_pid=21 prev_prio=120 prev_state=S ==> next_comm=perf next_pid=1383 next_prio=120
  986. perf 1383 [001] 6171.480035: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  987. </literallayout>
  988. </para>
  989. <section id='perf-filtering'>
  990. <title>Filtering</title>
  991. <para>
  992. Notice that there are a lot of events that don't really have
  993. anything to do with what we're interested in, namely events
  994. that schedule 'perf' itself in and out or that wake perf up.
  995. We can get rid of those by using the '--filter' option -
  996. for each event we specify using -e, we can add a --filter
  997. after that to filter out trace events that contain fields
  998. with specific values:
  999. <literallayout class='monospaced'>
  1000. root@crownbay:~# perf record -a -e sched:sched_switch --filter 'next_comm != perf &amp;&amp; prev_comm != perf' -e sched:sched_wakeup --filter 'comm != perf'
  1001. ^C[ perf record: Woken up 38 times to write data ]
  1002. [ perf record: Captured and wrote 9.688 MB perf.data (~423279 samples) ]
  1003. root@crownbay:~# perf script
  1004. swapper 0 [000] 7932.162180: sched_switch: prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=kworker/0:3 next_pid=1209 next_prio=120
  1005. kworker/0:3 1209 [000] 7932.162236: sched_switch: prev_comm=kworker/0:3 prev_pid=1209 prev_prio=120 prev_state=S ==> next_comm=swapper/0 next_pid=0 next_prio=120
  1006. perf 1407 [001] 7932.170048: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  1007. perf 1407 [001] 7932.180044: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  1008. perf 1407 [001] 7932.190038: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  1009. perf 1407 [001] 7932.200044: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  1010. perf 1407 [001] 7932.210044: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  1011. perf 1407 [001] 7932.220044: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  1012. swapper 0 [001] 7932.230111: sched_wakeup: comm=kworker/1:1 pid=21 prio=120 success=1 target_cpu=001
  1013. swapper 0 [001] 7932.230146: sched_switch: prev_comm=swapper/1 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=kworker/1:1 next_pid=21 next_prio=120
  1014. kworker/1:1 21 [001] 7932.230205: sched_switch: prev_comm=kworker/1:1 prev_pid=21 prev_prio=120 prev_state=S ==> next_comm=swapper/1 next_pid=0 next_prio=120
  1015. swapper 0 [000] 7932.326109: sched_wakeup: comm=kworker/0:3 pid=1209 prio=120 success=1 target_cpu=000
  1016. swapper 0 [000] 7932.326171: sched_switch: prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=kworker/0:3 next_pid=1209 next_prio=120
  1017. kworker/0:3 1209 [000] 7932.326214: sched_switch: prev_comm=kworker/0:3 prev_pid=1209 prev_prio=120 prev_state=S ==> next_comm=swapper/0 next_pid=0 next_prio=120
  1018. </literallayout>
  1019. In this case, we've filtered out all events that have 'perf'
  1020. in their 'comm' or 'comm_prev' or 'comm_next' fields. Notice
  1021. that there are still events recorded for perf, but notice
  1022. that those events don't have values of 'perf' for the filtered
  1023. fields. To completely filter out anything from perf will
  1024. require a bit more work, but for the purpose of demonstrating
  1025. how to use filters, it's close enough.
  1026. </para>
  1027. <informalexample>
  1028. <emphasis>Tying it Together:</emphasis> These are exactly the same set of event
  1029. filters defined by the trace event subsystem. See the
  1030. ftrace/tracecmd/kernelshark section for more discussion about
  1031. these event filters.
  1032. </informalexample>
  1033. <informalexample>
  1034. <emphasis>Tying it Together:</emphasis> These event filters are implemented by a
  1035. special-purpose pseudo-interpreter in the kernel and are an
  1036. integral and indispensable part of the perf design as it
  1037. relates to tracing. kernel-based event filters provide a
  1038. mechanism to precisely throttle the event stream that appears
  1039. in user space, where it makes sense to provide bindings to real
  1040. programming languages for postprocessing the event stream.
  1041. This architecture allows for the intelligent and flexible
  1042. partitioning of processing between the kernel and user space.
  1043. Contrast this with other tools such as SystemTap, which does
  1044. all of its processing in the kernel and as such requires a
  1045. special project-defined language in order to accommodate that
  1046. design, or LTTng, where everything is sent to userspace and
  1047. as such requires a super-efficient kernel-to-userspace
  1048. transport mechanism in order to function properly. While
  1049. perf certainly can benefit from for instance advances in
  1050. the design of the transport, it doesn't fundamentally depend
  1051. on them. Basically, if you find that your perf tracing
  1052. application is causing buffer I/O overruns, it probably
  1053. means that you aren't taking enough advantage of the
  1054. kernel filtering engine.
  1055. </informalexample>
  1056. </section>
  1057. </section>
  1058. <section id='using-dynamic-tracepoints'>
  1059. <title>Using Dynamic Tracepoints</title>
  1060. <para>
  1061. perf isn't restricted to the fixed set of static tracepoints
  1062. listed by 'perf list'. Users can also add their own 'dynamic'
  1063. tracepoints anywhere in the kernel. For instance, suppose we
  1064. want to define our own tracepoint on do_fork(). We can do that
  1065. using the 'perf probe' perf subcommand:
  1066. <literallayout class='monospaced'>
  1067. root@crownbay:~# perf probe do_fork
  1068. Added new event:
  1069. probe:do_fork (on do_fork)
  1070. You can now use it in all perf tools, such as:
  1071. perf record -e probe:do_fork -aR sleep 1
  1072. </literallayout>
  1073. Adding a new tracepoint via 'perf probe' results in an event
  1074. with all the expected files and format in
  1075. /sys/kernel/debug/tracing/events, just the same as for static
  1076. tracepoints (as discussed in more detail in the trace events
  1077. subsystem section:
  1078. <literallayout class='monospaced'>
  1079. root@crownbay:/sys/kernel/debug/tracing/events/probe/do_fork# ls -al
  1080. drwxr-xr-x 2 root root 0 Oct 28 11:42 .
  1081. drwxr-xr-x 3 root root 0 Oct 28 11:42 ..
  1082. -rw-r--r-- 1 root root 0 Oct 28 11:42 enable
  1083. -rw-r--r-- 1 root root 0 Oct 28 11:42 filter
  1084. -r--r--r-- 1 root root 0 Oct 28 11:42 format
  1085. -r--r--r-- 1 root root 0 Oct 28 11:42 id
  1086. root@crownbay:/sys/kernel/debug/tracing/events/probe/do_fork# cat format
  1087. name: do_fork
  1088. ID: 944
  1089. format:
  1090. field:unsigned short common_type; offset:0; size:2; signed:0;
  1091. field:unsigned char common_flags; offset:2; size:1; signed:0;
  1092. field:unsigned char common_preempt_count; offset:3; size:1; signed:0;
  1093. field:int common_pid; offset:4; size:4; signed:1;
  1094. field:int common_padding; offset:8; size:4; signed:1;
  1095. field:unsigned long __probe_ip; offset:12; size:4; signed:0;
  1096. print fmt: "(%lx)", REC->__probe_ip
  1097. </literallayout>
  1098. We can list all dynamic tracepoints currently in existence:
  1099. <literallayout class='monospaced'>
  1100. root@crownbay:~# perf probe -l
  1101. probe:do_fork (on do_fork)
  1102. probe:schedule (on schedule)
  1103. </literallayout>
  1104. Let's record system-wide ('sleep 30' is a trick for recording
  1105. system-wide but basically do nothing and then wake up after
  1106. 30 seconds):
  1107. <literallayout class='monospaced'>
  1108. root@crownbay:~# perf record -g -a -e probe:do_fork sleep 30
  1109. [ perf record: Woken up 1 times to write data ]
  1110. [ perf record: Captured and wrote 0.087 MB perf.data (~3812 samples) ]
  1111. </literallayout>
  1112. Using 'perf script' we can see each do_fork event that fired:
  1113. <literallayout class='monospaced'>
  1114. root@crownbay:~# perf script
  1115. # ========
  1116. # captured on: Sun Oct 28 11:55:18 2012
  1117. # hostname : crownbay
  1118. # os release : 3.4.11-yocto-standard
  1119. # perf version : 3.4.11
  1120. # arch : i686
  1121. # nrcpus online : 2
  1122. # nrcpus avail : 2
  1123. # cpudesc : Intel(R) Atom(TM) CPU E660 @ 1.30GHz
  1124. # cpuid : GenuineIntel,6,38,1
  1125. # total memory : 1017184 kB
  1126. # cmdline : /usr/bin/perf record -g -a -e probe:do_fork sleep 30
  1127. # event : name = probe:do_fork, type = 2, config = 0x3b0, config1 = 0x0, config2 = 0x0, excl_usr = 0, excl_kern
  1128. = 0, id = { 5, 6 }
  1129. # HEADER_CPU_TOPOLOGY info available, use -I to display
  1130. # ========
  1131. #
  1132. matchbox-deskto 1197 [001] 34211.378318: do_fork: (c1028460)
  1133. matchbox-deskto 1295 [001] 34211.380388: do_fork: (c1028460)
  1134. pcmanfm 1296 [000] 34211.632350: do_fork: (c1028460)
  1135. pcmanfm 1296 [000] 34211.639917: do_fork: (c1028460)
  1136. matchbox-deskto 1197 [001] 34217.541603: do_fork: (c1028460)
  1137. matchbox-deskto 1299 [001] 34217.543584: do_fork: (c1028460)
  1138. gthumb 1300 [001] 34217.697451: do_fork: (c1028460)
  1139. gthumb 1300 [001] 34219.085734: do_fork: (c1028460)
  1140. gthumb 1300 [000] 34219.121351: do_fork: (c1028460)
  1141. gthumb 1300 [001] 34219.264551: do_fork: (c1028460)
  1142. pcmanfm 1296 [000] 34219.590380: do_fork: (c1028460)
  1143. matchbox-deskto 1197 [001] 34224.955965: do_fork: (c1028460)
  1144. matchbox-deskto 1306 [001] 34224.957972: do_fork: (c1028460)
  1145. matchbox-termin 1307 [000] 34225.038214: do_fork: (c1028460)
  1146. matchbox-termin 1307 [001] 34225.044218: do_fork: (c1028460)
  1147. matchbox-termin 1307 [000] 34225.046442: do_fork: (c1028460)
  1148. matchbox-deskto 1197 [001] 34237.112138: do_fork: (c1028460)
  1149. matchbox-deskto 1311 [001] 34237.114106: do_fork: (c1028460)
  1150. gaku 1312 [000] 34237.202388: do_fork: (c1028460)
  1151. </literallayout>
  1152. And using 'perf report' on the same file, we can see the
  1153. callgraphs from starting a few programs during those 30 seconds:
  1154. </para>
  1155. <para>
  1156. <imagedata fileref="figures/perf-probe-do_fork-profile.png" width="6in" depth="7in" align="center" scalefit="1" />
  1157. </para>
  1158. <informalexample>
  1159. <emphasis>Tying it Together:</emphasis> The trace events subsystem accommodate static
  1160. and dynamic tracepoints in exactly the same way - there's no
  1161. difference as far as the infrastructure is concerned. See the
  1162. ftrace section for more details on the trace event subsystem.
  1163. </informalexample>
  1164. <informalexample>
  1165. <emphasis>Tying it Together:</emphasis> Dynamic tracepoints are implemented under the
  1166. covers by kprobes and uprobes. kprobes and uprobes are also used
  1167. by and in fact are the main focus of SystemTap.
  1168. </informalexample>
  1169. </section>
  1170. </section>
  1171. <section id='perf-documentation'>
  1172. <title>Documentation</title>
  1173. <para>
  1174. Online versions of the man pages for the commands discussed in this
  1175. section can be found here:
  1176. <itemizedlist>
  1177. <listitem><para>The <ulink url='http://linux.die.net/man/1/perf-stat'>'perf stat' manpage</ulink>.
  1178. </para></listitem>
  1179. <listitem><para>The <ulink url='http://linux.die.net/man/1/perf-record'>'perf record' manpage</ulink>.
  1180. </para></listitem>
  1181. <listitem><para>The <ulink url='http://linux.die.net/man/1/perf-report'>'perf report' manpage</ulink>.
  1182. </para></listitem>
  1183. <listitem><para>The <ulink url='http://linux.die.net/man/1/perf-probe'>'perf probe' manpage</ulink>.
  1184. </para></listitem>
  1185. <listitem><para>The <ulink url='http://linux.die.net/man/1/perf-script'>'perf script' manpage</ulink>.
  1186. </para></listitem>
  1187. <listitem><para>Documentation on using the
  1188. <ulink url='http://linux.die.net/man/1/perf-script-python'>'perf script' python binding</ulink>.
  1189. </para></listitem>
  1190. <listitem><para>The top-level
  1191. <ulink url='http://linux.die.net/man/1/perf'>perf(1) manpage</ulink>.
  1192. </para></listitem>
  1193. </itemizedlist>
  1194. </para>
  1195. <para>
  1196. Normally, you should be able to invoke the man pages via perf
  1197. itself e.g. 'perf help' or 'perf help record'.
  1198. </para>
  1199. <para>
  1200. However, by default Yocto doesn't install man pages, but perf
  1201. invokes the man pages for most help functionality. This is a bug
  1202. and is being addressed by a Yocto bug:
  1203. <ulink url='https://bugzilla.yoctoproject.org/show_bug.cgi?id=3388'>Bug 3388 - perf: enable man pages for basic 'help' functionality</ulink>.
  1204. </para>
  1205. <para>
  1206. The man pages in text form, along with some other files, such as
  1207. a set of examples, can be found in the 'perf' directory of the
  1208. kernel tree:
  1209. <literallayout class='monospaced'>
  1210. tools/perf/Documentation
  1211. </literallayout>
  1212. There's also a nice perf tutorial on the perf wiki that goes
  1213. into more detail than we do here in certain areas:
  1214. <ulink url='https://perf.wiki.kernel.org/index.php/Tutorial'>Perf Tutorial</ulink>
  1215. </para>
  1216. </section>
  1217. </section>
  1218. <section id='profile-manual-ftrace'>
  1219. <title>ftrace</title>
  1220. <para>
  1221. 'ftrace' literally refers to the 'ftrace function tracer' but in
  1222. reality this encompasses a number of related tracers along with
  1223. the infrastructure that they all make use of.
  1224. </para>
  1225. <section id='ftrace-setup'>
  1226. <title>Setup</title>
  1227. <para>
  1228. For this section, we'll assume you've already performed the basic
  1229. setup outlined in the General Setup section.
  1230. </para>
  1231. <para>
  1232. ftrace, trace-cmd, and kernelshark run on the target system,
  1233. and are ready to go out-of-the-box - no additional setup is
  1234. necessary. For the rest of this section we assume you've ssh'ed
  1235. to the host and will be running ftrace on the target. kernelshark
  1236. is a GUI application and if you use the '-X' option to ssh you
  1237. can have the kernelshark GUI run on the target but display
  1238. remotely on the host if you want.
  1239. </para>
  1240. </section>
  1241. <section id='basic-ftrace-usage'>
  1242. <title>Basic ftrace usage</title>
  1243. <para>
  1244. 'ftrace' essentially refers to everything included in
  1245. the /tracing directory of the mounted debugfs filesystem
  1246. (Yocto follows the standard convention and mounts it
  1247. at /sys/kernel/debug). Here's a listing of all the files
  1248. found in /sys/kernel/debug/tracing on a Yocto system:
  1249. <literallayout class='monospaced'>
  1250. root@sugarbay:/sys/kernel/debug/tracing# ls
  1251. README kprobe_events trace
  1252. available_events kprobe_profile trace_clock
  1253. available_filter_functions options trace_marker
  1254. available_tracers per_cpu trace_options
  1255. buffer_size_kb printk_formats trace_pipe
  1256. buffer_total_size_kb saved_cmdlines tracing_cpumask
  1257. current_tracer set_event tracing_enabled
  1258. dyn_ftrace_total_info set_ftrace_filter tracing_on
  1259. enabled_functions set_ftrace_notrace tracing_thresh
  1260. events set_ftrace_pid
  1261. free_buffer set_graph_function
  1262. </literallayout>
  1263. The files listed above are used for various purposes -
  1264. some relate directly to the tracers themselves, others are
  1265. used to set tracing options, and yet others actually contain
  1266. the tracing output when a tracer is in effect. Some of the
  1267. functions can be guessed from their names, others need
  1268. explanation; in any case, we'll cover some of the files we
  1269. see here below but for an explanation of the others, please
  1270. see the ftrace documentation.
  1271. </para>
  1272. <para>
  1273. We'll start by looking at some of the available built-in
  1274. tracers.
  1275. </para>
  1276. <para>
  1277. cat'ing the 'available_tracers' file lists the set of
  1278. available tracers:
  1279. <literallayout class='monospaced'>
  1280. root@sugarbay:/sys/kernel/debug/tracing# cat available_tracers
  1281. blk function_graph function nop
  1282. </literallayout>
  1283. The 'current_tracer' file contains the tracer currently in
  1284. effect:
  1285. <literallayout class='monospaced'>
  1286. root@sugarbay:/sys/kernel/debug/tracing# cat current_tracer
  1287. nop
  1288. </literallayout>
  1289. The above listing of current_tracer shows that
  1290. the 'nop' tracer is in effect, which is just another
  1291. way of saying that there's actually no tracer
  1292. currently in effect.
  1293. </para>
  1294. <para>
  1295. echo'ing one of the available_tracers into current_tracer
  1296. makes the specified tracer the current tracer:
  1297. <literallayout class='monospaced'>
  1298. root@sugarbay:/sys/kernel/debug/tracing# echo function > current_tracer
  1299. root@sugarbay:/sys/kernel/debug/tracing# cat current_tracer
  1300. function
  1301. </literallayout>
  1302. The above sets the current tracer to be the
  1303. 'function tracer'. This tracer traces every function
  1304. call in the kernel and makes it available as the
  1305. contents of the 'trace' file. Reading the 'trace' file
  1306. lists the currently buffered function calls that have been
  1307. traced by the function tracer:
  1308. <literallayout class='monospaced'>
  1309. root@sugarbay:/sys/kernel/debug/tracing# cat trace | less
  1310. # tracer: function
  1311. #
  1312. # entries-in-buffer/entries-written: 310629/766471 #P:8
  1313. #
  1314. # _-----=&gt; irqs-off
  1315. # / _----=&gt; need-resched
  1316. # | / _---=&gt; hardirq/softirq
  1317. # || / _--=&gt; preempt-depth
  1318. # ||| / delay
  1319. # TASK-PID CPU# |||| TIMESTAMP FUNCTION
  1320. # | | | |||| | |
  1321. &lt;idle&gt;-0 [004] d..1 470.867169: ktime_get_real &lt;-intel_idle
  1322. &lt;idle&gt;-0 [004] d..1 470.867170: getnstimeofday &lt;-ktime_get_real
  1323. &lt;idle&gt;-0 [004] d..1 470.867171: ns_to_timeval &lt;-intel_idle
  1324. &lt;idle&gt;-0 [004] d..1 470.867171: ns_to_timespec &lt;-ns_to_timeval
  1325. &lt;idle&gt;-0 [004] d..1 470.867172: smp_apic_timer_interrupt &lt;-apic_timer_interrupt
  1326. &lt;idle&gt;-0 [004] d..1 470.867172: native_apic_mem_write &lt;-smp_apic_timer_interrupt
  1327. &lt;idle&gt;-0 [004] d..1 470.867172: irq_enter &lt;-smp_apic_timer_interrupt
  1328. &lt;idle&gt;-0 [004] d..1 470.867172: rcu_irq_enter &lt;-irq_enter
  1329. &lt;idle&gt;-0 [004] d..1 470.867173: rcu_idle_exit_common.isra.33 &lt;-rcu_irq_enter
  1330. &lt;idle&gt;-0 [004] d..1 470.867173: local_bh_disable &lt;-irq_enter
  1331. &lt;idle&gt;-0 [004] d..1 470.867173: add_preempt_count &lt;-local_bh_disable
  1332. &lt;idle&gt;-0 [004] d.s1 470.867174: tick_check_idle &lt;-irq_enter
  1333. &lt;idle&gt;-0 [004] d.s1 470.867174: tick_check_oneshot_broadcast &lt;-tick_check_idle
  1334. &lt;idle&gt;-0 [004] d.s1 470.867174: ktime_get &lt;-tick_check_idle
  1335. &lt;idle&gt;-0 [004] d.s1 470.867174: tick_nohz_stop_idle &lt;-tick_check_idle
  1336. &lt;idle&gt;-0 [004] d.s1 470.867175: update_ts_time_stats &lt;-tick_nohz_stop_idle
  1337. &lt;idle&gt;-0 [004] d.s1 470.867175: nr_iowait_cpu &lt;-update_ts_time_stats
  1338. &lt;idle&gt;-0 [004] d.s1 470.867175: tick_do_update_jiffies64 &lt;-tick_check_idle
  1339. &lt;idle&gt;-0 [004] d.s1 470.867175: _raw_spin_lock &lt;-tick_do_update_jiffies64
  1340. &lt;idle&gt;-0 [004] d.s1 470.867176: add_preempt_count &lt;-_raw_spin_lock
  1341. &lt;idle&gt;-0 [004] d.s2 470.867176: do_timer &lt;-tick_do_update_jiffies64
  1342. &lt;idle&gt;-0 [004] d.s2 470.867176: _raw_spin_lock &lt;-do_timer
  1343. &lt;idle&gt;-0 [004] d.s2 470.867176: add_preempt_count &lt;-_raw_spin_lock
  1344. &lt;idle&gt;-0 [004] d.s3 470.867177: ntp_tick_length &lt;-do_timer
  1345. &lt;idle&gt;-0 [004] d.s3 470.867177: _raw_spin_lock_irqsave &lt;-ntp_tick_length
  1346. .
  1347. .
  1348. .
  1349. </literallayout>
  1350. Each line in the trace above shows what was happening in
  1351. the kernel on a given cpu, to the level of detail of
  1352. function calls. Each entry shows the function called,
  1353. followed by its caller (after the arrow).
  1354. </para>
  1355. <para>
  1356. The function tracer gives you an extremely detailed idea
  1357. of what the kernel was doing at the point in time the trace
  1358. was taken, and is a great way to learn about how the kernel
  1359. code works in a dynamic sense.
  1360. </para>
  1361. <informalexample>
  1362. <emphasis>Tying it Together:</emphasis> The ftrace function tracer is also
  1363. available from within perf, as the ftrace:function tracepoint.
  1364. </informalexample>
  1365. <para>
  1366. It is a little more difficult to follow the call chains than
  1367. it needs to be - luckily there's a variant of the function
  1368. tracer that displays the callchains explicitly, called the
  1369. 'function_graph' tracer:
  1370. <literallayout class='monospaced'>
  1371. root@sugarbay:/sys/kernel/debug/tracing# echo function_graph &gt; current_tracer
  1372. root@sugarbay:/sys/kernel/debug/tracing# cat trace | less
  1373. tracer: function_graph
  1374. CPU DURATION FUNCTION CALLS
  1375. | | | | | | |
  1376. 7) 0.046 us | pick_next_task_fair();
  1377. 7) 0.043 us | pick_next_task_stop();
  1378. 7) 0.042 us | pick_next_task_rt();
  1379. 7) 0.032 us | pick_next_task_fair();
  1380. 7) 0.030 us | pick_next_task_idle();
  1381. 7) | _raw_spin_unlock_irq() {
  1382. 7) 0.033 us | sub_preempt_count();
  1383. 7) 0.258 us | }
  1384. 7) 0.032 us | sub_preempt_count();
  1385. 7) + 13.341 us | } /* __schedule */
  1386. 7) 0.095 us | } /* sub_preempt_count */
  1387. 7) | schedule() {
  1388. 7) | __schedule() {
  1389. 7) 0.060 us | add_preempt_count();
  1390. 7) 0.044 us | rcu_note_context_switch();
  1391. 7) | _raw_spin_lock_irq() {
  1392. 7) 0.033 us | add_preempt_count();
  1393. 7) 0.247 us | }
  1394. 7) | idle_balance() {
  1395. 7) | _raw_spin_unlock() {
  1396. 7) 0.031 us | sub_preempt_count();
  1397. 7) 0.246 us | }
  1398. 7) | update_shares() {
  1399. 7) 0.030 us | __rcu_read_lock();
  1400. 7) 0.029 us | __rcu_read_unlock();
  1401. 7) 0.484 us | }
  1402. 7) 0.030 us | __rcu_read_lock();
  1403. 7) | load_balance() {
  1404. 7) | find_busiest_group() {
  1405. 7) 0.031 us | idle_cpu();
  1406. 7) 0.029 us | idle_cpu();
  1407. 7) 0.035 us | idle_cpu();
  1408. 7) 0.906 us | }
  1409. 7) 1.141 us | }
  1410. 7) 0.022 us | msecs_to_jiffies();
  1411. 7) | load_balance() {
  1412. 7) | find_busiest_group() {
  1413. 7) 0.031 us | idle_cpu();
  1414. .
  1415. .
  1416. .
  1417. 4) 0.062 us | msecs_to_jiffies();
  1418. 4) 0.062 us | __rcu_read_unlock();
  1419. 4) | _raw_spin_lock() {
  1420. 4) 0.073 us | add_preempt_count();
  1421. 4) 0.562 us | }
  1422. 4) + 17.452 us | }
  1423. 4) 0.108 us | put_prev_task_fair();
  1424. 4) 0.102 us | pick_next_task_fair();
  1425. 4) 0.084 us | pick_next_task_stop();
  1426. 4) 0.075 us | pick_next_task_rt();
  1427. 4) 0.062 us | pick_next_task_fair();
  1428. 4) 0.066 us | pick_next_task_idle();
  1429. ------------------------------------------
  1430. 4) kworker-74 =&gt; &lt;idle&gt;-0
  1431. ------------------------------------------
  1432. 4) | finish_task_switch() {
  1433. 4) | _raw_spin_unlock_irq() {
  1434. 4) 0.100 us | sub_preempt_count();
  1435. 4) 0.582 us | }
  1436. 4) 1.105 us | }
  1437. 4) 0.088 us | sub_preempt_count();
  1438. 4) ! 100.066 us | }
  1439. .
  1440. .
  1441. .
  1442. 3) | sys_ioctl() {
  1443. 3) 0.083 us | fget_light();
  1444. 3) | security_file_ioctl() {
  1445. 3) 0.066 us | cap_file_ioctl();
  1446. 3) 0.562 us | }
  1447. 3) | do_vfs_ioctl() {
  1448. 3) | drm_ioctl() {
  1449. 3) 0.075 us | drm_ut_debug_printk();
  1450. 3) | i915_gem_pwrite_ioctl() {
  1451. 3) | i915_mutex_lock_interruptible() {
  1452. 3) 0.070 us | mutex_lock_interruptible();
  1453. 3) 0.570 us | }
  1454. 3) | drm_gem_object_lookup() {
  1455. 3) | _raw_spin_lock() {
  1456. 3) 0.080 us | add_preempt_count();
  1457. 3) 0.620 us | }
  1458. 3) | _raw_spin_unlock() {
  1459. 3) 0.085 us | sub_preempt_count();
  1460. 3) 0.562 us | }
  1461. 3) 2.149 us | }
  1462. 3) 0.133 us | i915_gem_object_pin();
  1463. 3) | i915_gem_object_set_to_gtt_domain() {
  1464. 3) 0.065 us | i915_gem_object_flush_gpu_write_domain();
  1465. 3) 0.065 us | i915_gem_object_wait_rendering();
  1466. 3) 0.062 us | i915_gem_object_flush_cpu_write_domain();
  1467. 3) 1.612 us | }
  1468. 3) | i915_gem_object_put_fence() {
  1469. 3) 0.097 us | i915_gem_object_flush_fence.constprop.36();
  1470. 3) 0.645 us | }
  1471. 3) 0.070 us | add_preempt_count();
  1472. 3) 0.070 us | sub_preempt_count();
  1473. 3) 0.073 us | i915_gem_object_unpin();
  1474. 3) 0.068 us | mutex_unlock();
  1475. 3) 9.924 us | }
  1476. 3) + 11.236 us | }
  1477. 3) + 11.770 us | }
  1478. 3) + 13.784 us | }
  1479. 3) | sys_ioctl() {
  1480. </literallayout>
  1481. As you can see, the function_graph display is much easier to
  1482. follow. Also note that in addition to the function calls and
  1483. associated braces, other events such as scheduler events
  1484. are displayed in context. In fact, you can freely include
  1485. any tracepoint available in the trace events subsystem described
  1486. in the next section by simply enabling those events, and they'll
  1487. appear in context in the function graph display. Quite a
  1488. powerful tool for understanding kernel dynamics.
  1489. </para>
  1490. <para>
  1491. Also notice that there are various annotations on the left
  1492. hand side of the display. For example if the total time it
  1493. took for a given function to execute is above a certain
  1494. threshold, an exclamation point or plus sign appears on the
  1495. left hand side. Please see the ftrace documentation for
  1496. details on all these fields.
  1497. </para>
  1498. </section>
  1499. <section id='the-trace-events-subsystem'>
  1500. <title>The 'trace events' Subsystem</title>
  1501. <para>
  1502. One especially important directory contained within
  1503. the /sys/kernel/debug/tracing directory is the 'events'
  1504. subdirectory, which contains representations of every
  1505. tracepoint in the system. Listing out the contents of
  1506. the 'events' subdirectory, we see mainly another set of
  1507. subdirectories:
  1508. <literallayout class='monospaced'>
  1509. root@sugarbay:/sys/kernel/debug/tracing# cd events
  1510. root@sugarbay:/sys/kernel/debug/tracing/events# ls -al
  1511. drwxr-xr-x 38 root root 0 Nov 14 23:19 .
  1512. drwxr-xr-x 5 root root 0 Nov 14 23:19 ..
  1513. drwxr-xr-x 19 root root 0 Nov 14 23:19 block
  1514. drwxr-xr-x 32 root root 0 Nov 14 23:19 btrfs
  1515. drwxr-xr-x 5 root root 0 Nov 14 23:19 drm
  1516. -rw-r--r-- 1 root root 0 Nov 14 23:19 enable
  1517. drwxr-xr-x 40 root root 0 Nov 14 23:19 ext3
  1518. drwxr-xr-x 79 root root 0 Nov 14 23:19 ext4
  1519. drwxr-xr-x 14 root root 0 Nov 14 23:19 ftrace
  1520. drwxr-xr-x 8 root root 0 Nov 14 23:19 hda
  1521. -r--r--r-- 1 root root 0 Nov 14 23:19 header_event
  1522. -r--r--r-- 1 root root 0 Nov 14 23:19 header_page
  1523. drwxr-xr-x 25 root root 0 Nov 14 23:19 i915
  1524. drwxr-xr-x 7 root root 0 Nov 14 23:19 irq
  1525. drwxr-xr-x 12 root root 0 Nov 14 23:19 jbd
  1526. drwxr-xr-x 14 root root 0 Nov 14 23:19 jbd2
  1527. drwxr-xr-x 14 root root 0 Nov 14 23:19 kmem
  1528. drwxr-xr-x 7 root root 0 Nov 14 23:19 module
  1529. drwxr-xr-x 3 root root 0 Nov 14 23:19 napi
  1530. drwxr-xr-x 6 root root 0 Nov 14 23:19 net
  1531. drwxr-xr-x 3 root root 0 Nov 14 23:19 oom
  1532. drwxr-xr-x 12 root root 0 Nov 14 23:19 power
  1533. drwxr-xr-x 3 root root 0 Nov 14 23:19 printk
  1534. drwxr-xr-x 8 root root 0 Nov 14 23:19 random
  1535. drwxr-xr-x 4 root root 0 Nov 14 23:19 raw_syscalls
  1536. drwxr-xr-x 3 root root 0 Nov 14 23:19 rcu
  1537. drwxr-xr-x 6 root root 0 Nov 14 23:19 rpm
  1538. drwxr-xr-x 20 root root 0 Nov 14 23:19 sched
  1539. drwxr-xr-x 7 root root 0 Nov 14 23:19 scsi
  1540. drwxr-xr-x 4 root root 0 Nov 14 23:19 signal
  1541. drwxr-xr-x 5 root root 0 Nov 14 23:19 skb
  1542. drwxr-xr-x 4 root root 0 Nov 14 23:19 sock
  1543. drwxr-xr-x 10 root root 0 Nov 14 23:19 sunrpc
  1544. drwxr-xr-x 538 root root 0 Nov 14 23:19 syscalls
  1545. drwxr-xr-x 4 root root 0 Nov 14 23:19 task
  1546. drwxr-xr-x 14 root root 0 Nov 14 23:19 timer
  1547. drwxr-xr-x 3 root root 0 Nov 14 23:19 udp
  1548. drwxr-xr-x 21 root root 0 Nov 14 23:19 vmscan
  1549. drwxr-xr-x 3 root root 0 Nov 14 23:19 vsyscall
  1550. drwxr-xr-x 6 root root 0 Nov 14 23:19 workqueue
  1551. drwxr-xr-x 26 root root 0 Nov 14 23:19 writeback
  1552. </literallayout>
  1553. Each one of these subdirectories corresponds to a
  1554. 'subsystem' and contains yet again more subdirectories,
  1555. each one of those finally corresponding to a tracepoint.
  1556. For example, here are the contents of the 'kmem' subsystem:
  1557. <literallayout class='monospaced'>
  1558. root@sugarbay:/sys/kernel/debug/tracing/events# cd kmem
  1559. root@sugarbay:/sys/kernel/debug/tracing/events/kmem# ls -al
  1560. drwxr-xr-x 14 root root 0 Nov 14 23:19 .
  1561. drwxr-xr-x 38 root root 0 Nov 14 23:19 ..
  1562. -rw-r--r-- 1 root root 0 Nov 14 23:19 enable
  1563. -rw-r--r-- 1 root root 0 Nov 14 23:19 filter
  1564. drwxr-xr-x 2 root root 0 Nov 14 23:19 kfree
  1565. drwxr-xr-x 2 root root 0 Nov 14 23:19 kmalloc
  1566. drwxr-xr-x 2 root root 0 Nov 14 23:19 kmalloc_node
  1567. drwxr-xr-x 2 root root 0 Nov 14 23:19 kmem_cache_alloc
  1568. drwxr-xr-x 2 root root 0 Nov 14 23:19 kmem_cache_alloc_node
  1569. drwxr-xr-x 2 root root 0 Nov 14 23:19 kmem_cache_free
  1570. drwxr-xr-x 2 root root 0 Nov 14 23:19 mm_page_alloc
  1571. drwxr-xr-x 2 root root 0 Nov 14 23:19 mm_page_alloc_extfrag
  1572. drwxr-xr-x 2 root root 0 Nov 14 23:19 mm_page_alloc_zone_locked
  1573. drwxr-xr-x 2 root root 0 Nov 14 23:19 mm_page_free
  1574. drwxr-xr-x 2 root root 0 Nov 14 23:19 mm_page_free_batched
  1575. drwxr-xr-x 2 root root 0 Nov 14 23:19 mm_page_pcpu_drain
  1576. </literallayout>
  1577. Let's see what's inside the subdirectory for a specific
  1578. tracepoint, in this case the one for kmalloc:
  1579. <literallayout class='monospaced'>
  1580. root@sugarbay:/sys/kernel/debug/tracing/events/kmem# cd kmalloc
  1581. root@sugarbay:/sys/kernel/debug/tracing/events/kmem/kmalloc# ls -al
  1582. drwxr-xr-x 2 root root 0 Nov 14 23:19 .
  1583. drwxr-xr-x 14 root root 0 Nov 14 23:19 ..
  1584. -rw-r--r-- 1 root root 0 Nov 14 23:19 enable
  1585. -rw-r--r-- 1 root root 0 Nov 14 23:19 filter
  1586. -r--r--r-- 1 root root 0 Nov 14 23:19 format
  1587. -r--r--r-- 1 root root 0 Nov 14 23:19 id
  1588. </literallayout>
  1589. The 'format' file for the tracepoint describes the event
  1590. in memory, which is used by the various tracing tools
  1591. that now make use of these tracepoint to parse the event
  1592. and make sense of it, along with a 'print fmt' field that
  1593. allows tools like ftrace to display the event as text.
  1594. Here's what the format of the kmalloc event looks like:
  1595. <literallayout class='monospaced'>
  1596. root@sugarbay:/sys/kernel/debug/tracing/events/kmem/kmalloc# cat format
  1597. name: kmalloc
  1598. ID: 313
  1599. format:
  1600. field:unsigned short common_type; offset:0; size:2; signed:0;
  1601. field:unsigned char common_flags; offset:2; size:1; signed:0;
  1602. field:unsigned char common_preempt_count; offset:3; size:1; signed:0;
  1603. field:int common_pid; offset:4; size:4; signed:1;
  1604. field:int common_padding; offset:8; size:4; signed:1;
  1605. field:unsigned long call_site; offset:16; size:8; signed:0;
  1606. field:const void * ptr; offset:24; size:8; signed:0;
  1607. field:size_t bytes_req; offset:32; size:8; signed:0;
  1608. field:size_t bytes_alloc; offset:40; size:8; signed:0;
  1609. field:gfp_t gfp_flags; offset:48; size:4; signed:0;
  1610. print fmt: "call_site=%lx ptr=%p bytes_req=%zu bytes_alloc=%zu gfp_flags=%s", REC->call_site, REC->ptr, REC->bytes_req, REC->bytes_alloc,
  1611. (REC->gfp_flags) ? __print_flags(REC->gfp_flags, "|", {(unsigned long)(((( gfp_t)0x10u) | (( gfp_t)0x40u) | (( gfp_t)0x80u) | ((
  1612. gfp_t)0x20000u) | (( gfp_t)0x02u) | (( gfp_t)0x08u)) | (( gfp_t)0x4000u) | (( gfp_t)0x10000u) | (( gfp_t)0x1000u) | (( gfp_t)0x200u) | ((
  1613. gfp_t)0x400000u)), "GFP_TRANSHUGE"}, {(unsigned long)((( gfp_t)0x10u) | (( gfp_t)0x40u) | (( gfp_t)0x80u) | (( gfp_t)0x20000u) | ((
  1614. gfp_t)0x02u) | (( gfp_t)0x08u)), "GFP_HIGHUSER_MOVABLE"}, {(unsigned long)((( gfp_t)0x10u) | (( gfp_t)0x40u) | (( gfp_t)0x80u) | ((
  1615. gfp_t)0x20000u) | (( gfp_t)0x02u)), "GFP_HIGHUSER"}, {(unsigned long)((( gfp_t)0x10u) | (( gfp_t)0x40u) | (( gfp_t)0x80u) | ((
  1616. gfp_t)0x20000u)), "GFP_USER"}, {(unsigned long)((( gfp_t)0x10u) | (( gfp_t)0x40u) | (( gfp_t)0x80u) | (( gfp_t)0x80000u)), GFP_TEMPORARY"},
  1617. {(unsigned long)((( gfp_t)0x10u) | (( gfp_t)0x40u) | (( gfp_t)0x80u)), "GFP_KERNEL"}, {(unsigned long)((( gfp_t)0x10u) | (( gfp_t)0x40u)),
  1618. "GFP_NOFS"}, {(unsigned long)((( gfp_t)0x20u)), "GFP_ATOMIC"}, {(unsigned long)((( gfp_t)0x10u)), "GFP_NOIO"}, {(unsigned long)((
  1619. gfp_t)0x20u), "GFP_HIGH"}, {(unsigned long)(( gfp_t)0x10u), "GFP_WAIT"}, {(unsigned long)(( gfp_t)0x40u), "GFP_IO"}, {(unsigned long)((
  1620. gfp_t)0x100u), "GFP_COLD"}, {(unsigned long)(( gfp_t)0x200u), "GFP_NOWARN"}, {(unsigned long)(( gfp_t)0x400u), "GFP_REPEAT"}, {(unsigned
  1621. long)(( gfp_t)0x800u), "GFP_NOFAIL"}, {(unsigned long)(( gfp_t)0x1000u), "GFP_NORETRY"}, {(unsigned long)(( gfp_t)0x4000u), "GFP_COMP"},
  1622. {(unsigned long)(( gfp_t)0x8000u), "GFP_ZERO"}, {(unsigned long)(( gfp_t)0x10000u), "GFP_NOMEMALLOC"}, {(unsigned long)(( gfp_t)0x20000u),
  1623. "GFP_HARDWALL"}, {(unsigned long)(( gfp_t)0x40000u), "GFP_THISNODE"}, {(unsigned long)(( gfp_t)0x80000u), "GFP_RECLAIMABLE"}, {(unsigned
  1624. long)(( gfp_t)0x08u), "GFP_MOVABLE"}, {(unsigned long)(( gfp_t)0), "GFP_NOTRACK"}, {(unsigned long)(( gfp_t)0x400000u), "GFP_NO_KSWAPD"},
  1625. {(unsigned long)(( gfp_t)0x800000u), "GFP_OTHER_NODE"} ) : "GFP_NOWAIT"
  1626. </literallayout>
  1627. The 'enable' file in the tracepoint directory is what allows
  1628. the user (or tools such as trace-cmd) to actually turn the
  1629. tracepoint on and off. When enabled, the corresponding
  1630. tracepoint will start appearing in the ftrace 'trace'
  1631. file described previously. For example, this turns on the
  1632. kmalloc tracepoint:
  1633. <literallayout class='monospaced'>
  1634. root@sugarbay:/sys/kernel/debug/tracing/events/kmem/kmalloc# echo 1 > enable
  1635. </literallayout>
  1636. At the moment, we're not interested in the function tracer or
  1637. some other tracer that might be in effect, so we first turn
  1638. it off, but if we do that, we still need to turn tracing on in
  1639. order to see the events in the output buffer:
  1640. <literallayout class='monospaced'>
  1641. root@sugarbay:/sys/kernel/debug/tracing# echo nop > current_tracer
  1642. root@sugarbay:/sys/kernel/debug/tracing# echo 1 > tracing_on
  1643. </literallayout>
  1644. Now, if we look at the the 'trace' file, we see nothing
  1645. but the kmalloc events we just turned on:
  1646. <literallayout class='monospaced'>
  1647. root@sugarbay:/sys/kernel/debug/tracing# cat trace | less
  1648. # tracer: nop
  1649. #
  1650. # entries-in-buffer/entries-written: 1897/1897 #P:8
  1651. #
  1652. # _-----=&gt; irqs-off
  1653. # / _----=&gt; need-resched
  1654. # | / _---=&gt; hardirq/softirq
  1655. # || / _--=&gt; preempt-depth
  1656. # ||| / delay
  1657. # TASK-PID CPU# |||| TIMESTAMP FUNCTION
  1658. # | | | |||| | |
  1659. dropbear-1465 [000] ...1 18154.620753: kmalloc: call_site=ffffffff816650d4 ptr=ffff8800729c3000 bytes_req=2048 bytes_alloc=2048 gfp_flags=GFP_KERNEL
  1660. &lt;idle&gt;-0 [000] ..s3 18154.621640: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d555800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1661. &lt;idle&gt;-0 [000] ..s3 18154.621656: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d555800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1662. matchbox-termin-1361 [001] ...1 18154.755472: kmalloc: call_site=ffffffff81614050 ptr=ffff88006d5f0e00 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_KERNEL|GFP_REPEAT
  1663. Xorg-1264 [002] ...1 18154.755581: kmalloc: call_site=ffffffff8141abe8 ptr=ffff8800734f4cc0 bytes_req=168 bytes_alloc=192 gfp_flags=GFP_KERNEL|GFP_NOWARN|GFP_NORETRY
  1664. Xorg-1264 [002] ...1 18154.755583: kmalloc: call_site=ffffffff814192a3 ptr=ffff88001f822520 bytes_req=24 bytes_alloc=32 gfp_flags=GFP_KERNEL|GFP_ZERO
  1665. Xorg-1264 [002] ...1 18154.755589: kmalloc: call_site=ffffffff81419edb ptr=ffff8800721a2f00 bytes_req=64 bytes_alloc=64 gfp_flags=GFP_KERNEL|GFP_ZERO
  1666. matchbox-termin-1361 [001] ...1 18155.354594: kmalloc: call_site=ffffffff81614050 ptr=ffff88006db35400 bytes_req=576 bytes_alloc=1024 gfp_flags=GFP_KERNEL|GFP_REPEAT
  1667. Xorg-1264 [002] ...1 18155.354703: kmalloc: call_site=ffffffff8141abe8 ptr=ffff8800734f4cc0 bytes_req=168 bytes_alloc=192 gfp_flags=GFP_KERNEL|GFP_NOWARN|GFP_NORETRY
  1668. Xorg-1264 [002] ...1 18155.354705: kmalloc: call_site=ffffffff814192a3 ptr=ffff88001f822520 bytes_req=24 bytes_alloc=32 gfp_flags=GFP_KERNEL|GFP_ZERO
  1669. Xorg-1264 [002] ...1 18155.354711: kmalloc: call_site=ffffffff81419edb ptr=ffff8800721a2f00 bytes_req=64 bytes_alloc=64 gfp_flags=GFP_KERNEL|GFP_ZERO
  1670. &lt;idle&gt;-0 [000] ..s3 18155.673319: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d555800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1671. dropbear-1465 [000] ...1 18155.673525: kmalloc: call_site=ffffffff816650d4 ptr=ffff8800729c3000 bytes_req=2048 bytes_alloc=2048 gfp_flags=GFP_KERNEL
  1672. &lt;idle&gt;-0 [000] ..s3 18155.674821: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d554800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1673. &lt;idle&gt;-0 [000] ..s3 18155.793014: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d554800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1674. dropbear-1465 [000] ...1 18155.793219: kmalloc: call_site=ffffffff816650d4 ptr=ffff8800729c3000 bytes_req=2048 bytes_alloc=2048 gfp_flags=GFP_KERNEL
  1675. &lt;idle&gt;-0 [000] ..s3 18155.794147: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d555800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1676. &lt;idle&gt;-0 [000] ..s3 18155.936705: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d555800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1677. dropbear-1465 [000] ...1 18155.936910: kmalloc: call_site=ffffffff816650d4 ptr=ffff8800729c3000 bytes_req=2048 bytes_alloc=2048 gfp_flags=GFP_KERNEL
  1678. &lt;idle&gt;-0 [000] ..s3 18155.937869: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d554800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1679. matchbox-termin-1361 [001] ...1 18155.953667: kmalloc: call_site=ffffffff81614050 ptr=ffff88006d5f2000 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_KERNEL|GFP_REPEAT
  1680. Xorg-1264 [002] ...1 18155.953775: kmalloc: call_site=ffffffff8141abe8 ptr=ffff8800734f4cc0 bytes_req=168 bytes_alloc=192 gfp_flags=GFP_KERNEL|GFP_NOWARN|GFP_NORETRY
  1681. Xorg-1264 [002] ...1 18155.953777: kmalloc: call_site=ffffffff814192a3 ptr=ffff88001f822520 bytes_req=24 bytes_alloc=32 gfp_flags=GFP_KERNEL|GFP_ZERO
  1682. Xorg-1264 [002] ...1 18155.953783: kmalloc: call_site=ffffffff81419edb ptr=ffff8800721a2f00 bytes_req=64 bytes_alloc=64 gfp_flags=GFP_KERNEL|GFP_ZERO
  1683. &lt;idle&gt;-0 [000] ..s3 18156.176053: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d554800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1684. dropbear-1465 [000] ...1 18156.176257: kmalloc: call_site=ffffffff816650d4 ptr=ffff8800729c3000 bytes_req=2048 bytes_alloc=2048 gfp_flags=GFP_KERNEL
  1685. &lt;idle&gt;-0 [000] ..s3 18156.177717: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d555800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1686. &lt;idle&gt;-0 [000] ..s3 18156.399229: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d555800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1687. dropbear-1465 [000] ...1 18156.399434: kmalloc: call_site=ffffffff816650d4 ptr=ffff8800729c3000 bytes_http://rostedt.homelinux.com/kernelshark/req=2048 bytes_alloc=2048 gfp_flags=GFP_KERNEL
  1688. &lt;idle&gt;-0 [000] ..s3 18156.400660: kmalloc: call_site=ffffffff81619b36 ptr=ffff88006d554800 bytes_req=512 bytes_alloc=512 gfp_flags=GFP_ATOMIC
  1689. matchbox-termin-1361 [001] ...1 18156.552800: kmalloc: call_site=ffffffff81614050 ptr=ffff88006db34800 bytes_req=576 bytes_alloc=1024 gfp_flags=GFP_KERNEL|GFP_REPEAT
  1690. </literallayout>
  1691. To again disable the kmalloc event, we need to send 0 to the
  1692. enable file:
  1693. <literallayout class='monospaced'>
  1694. root@sugarbay:/sys/kernel/debug/tracing/events/kmem/kmalloc# echo 0 > enable
  1695. </literallayout>
  1696. You can enable any number of events or complete subsystems
  1697. (by using the 'enable' file in the subsystem directory) and
  1698. get an arbitrarily fine-grained idea of what's going on in the
  1699. system by enabling as many of the appropriate tracepoints
  1700. as applicable.
  1701. </para>
  1702. <para>
  1703. A number of the tools described in this HOWTO do just that,
  1704. including trace-cmd and kernelshark in the next section.
  1705. </para>
  1706. <informalexample>
  1707. <emphasis>Tying it Together:</emphasis> These tracepoints and their representation
  1708. are used not only by ftrace, but by many of the other tools
  1709. covered in this document and they form a central point of
  1710. integration for the various tracers available in Linux.
  1711. They form a central part of the instrumentation for the
  1712. following tools: perf, lttng, ftrace, blktrace and SystemTap
  1713. </informalexample>
  1714. <informalexample>
  1715. <emphasis>Tying it Together:</emphasis> Eventually all the special-purpose tracers
  1716. currently available in /sys/kernel/debug/tracing will be
  1717. removed and replaced with equivalent tracers based on the
  1718. 'trace events' subsystem.
  1719. </informalexample>
  1720. </section>
  1721. <section id='trace-cmd-kernelshark'>
  1722. <title>trace-cmd/kernelshark</title>
  1723. <para>
  1724. trace-cmd is essentially an extensive command-line 'wrapper'
  1725. interface that hides the details of all the individual files
  1726. in /sys/kernel/debug/tracing, allowing users to specify
  1727. specific particular events within the
  1728. /sys/kernel/debug/tracing/events/ subdirectory and to collect
  1729. traces and avoid having to deal with those details directly.
  1730. </para>
  1731. <para>
  1732. As yet another layer on top of that, kernelshark provides a GUI
  1733. that allows users to start and stop traces and specify sets
  1734. of events using an intuitive interface, and view the
  1735. output as both trace events and as a per-CPU graphical
  1736. display. It directly uses 'trace-cmd' as the plumbing
  1737. that accomplishes all that underneath the covers (and
  1738. actually displays the trace-cmd command it uses, as we'll see).
  1739. </para>
  1740. <para>
  1741. To start a trace using kernelshark, first start kernelshark:
  1742. <literallayout class='monospaced'>
  1743. root@sugarbay:~# kernelshark
  1744. </literallayout>
  1745. Then bring up the 'Capture' dialog by choosing from the
  1746. kernelshark menu:
  1747. <literallayout class='monospaced'>
  1748. Capture | Record
  1749. </literallayout>
  1750. That will display the following dialog, which allows you to
  1751. choose one or more events (or even one or more complete
  1752. subsystems) to trace:
  1753. </para>
  1754. <para>
  1755. <imagedata fileref="figures/kernelshark-choose-events.png" width="6in" depth="6in" align="center" scalefit="1" />
  1756. </para>
  1757. <para>
  1758. Note that these are exactly the same sets of events described
  1759. in the previous trace events subsystem section, and in fact
  1760. is where trace-cmd gets them for kernelshark.
  1761. </para>
  1762. <para>
  1763. In the above screenshot, we've decided to explore the
  1764. graphics subsystem a bit and so have chosen to trace all
  1765. the tracepoints contained within the 'i915' and 'drm'
  1766. subsystems.
  1767. </para>
  1768. <para>
  1769. After doing that, we can start and stop the trace using
  1770. the 'Run' and 'Stop' button on the lower right corner of
  1771. the dialog (the same button will turn into the 'Stop'
  1772. button after the trace has started):
  1773. </para>
  1774. <para>
  1775. <imagedata fileref="figures/kernelshark-output-display.png" width="6in" depth="6in" align="center" scalefit="1" />
  1776. </para>
  1777. <para>
  1778. Notice that the right-hand pane shows the exact trace-cmd
  1779. command-line that's used to run the trace, along with the
  1780. results of the trace-cmd run.
  1781. </para>
  1782. <para>
  1783. Once the 'Stop' button is pressed, the graphical view magically
  1784. fills up with a colorful per-cpu display of the trace data,
  1785. along with the detailed event listing below that:
  1786. </para>
  1787. <para>
  1788. <imagedata fileref="figures/kernelshark-i915-display.png" width="6in" depth="7in" align="center" scalefit="1" />
  1789. </para>
  1790. <para>
  1791. Here's another example, this time a display resulting
  1792. from tracing 'all events':
  1793. </para>
  1794. <para>
  1795. <imagedata fileref="figures/kernelshark-all.png" width="6in" depth="7in" align="center" scalefit="1" />
  1796. </para>
  1797. <para>
  1798. The tool is pretty self-explanatory, but for more detailed
  1799. information on navigating through the data, see the
  1800. <ulink url='http://rostedt.homelinux.com/kernelshark/'>kernelshark website</ulink>.
  1801. </para>
  1802. </section>
  1803. <section id='ftrace-documentation'>
  1804. <title>Documentation</title>
  1805. <para>
  1806. The documentation for ftrace can be found in the kernel
  1807. Documentation directory:
  1808. <literallayout class='monospaced'>
  1809. Documentation/trace/ftrace.txt
  1810. </literallayout>
  1811. The documentation for the trace event subsystem can also
  1812. be found in the kernel Documentation directory:
  1813. <literallayout class='monospaced'>
  1814. Documentation/trace/events.txt
  1815. </literallayout>
  1816. There is a nice series of articles on using
  1817. ftrace and trace-cmd at LWN:
  1818. <itemizedlist>
  1819. <listitem><para><ulink url='http://lwn.net/Articles/365835/'>Debugging the kernel using Ftrace - part 1</ulink>
  1820. </para></listitem>
  1821. <listitem><para><ulink url='http://lwn.net/Articles/366796/'>Debugging the kernel using Ftrace - part 2</ulink>
  1822. </para></listitem>
  1823. <listitem><para><ulink url='http://lwn.net/Articles/370423/'>Secrets of the Ftrace function tracer</ulink>
  1824. </para></listitem>
  1825. <listitem><para><ulink url='https://lwn.net/Articles/410200/'>trace-cmd: A front-end for Ftrace</ulink>
  1826. </para></listitem>
  1827. </itemizedlist>
  1828. </para>
  1829. <para>
  1830. There's more detailed documentation kernelshark usage here:
  1831. <ulink url='http://rostedt.homelinux.com/kernelshark/'>KernelShark</ulink>
  1832. </para>
  1833. <para>
  1834. An amusing yet useful README (a tracing mini-HOWTO) can be
  1835. found in /sys/kernel/debug/tracing/README.
  1836. </para>
  1837. </section>
  1838. </section>
  1839. <section id='profile-manual-systemtap'>
  1840. <title>systemtap</title>
  1841. <para>
  1842. SystemTap is a system-wide script-based tracing and profiling tool.
  1843. </para>
  1844. <para>
  1845. SystemTap scripts are C-like programs that are executed in the
  1846. kernel to gather/print/aggregate data extracted from the context
  1847. they end up being invoked under.
  1848. </para>
  1849. <para>
  1850. For example, this probe from the
  1851. <ulink url='http://sourceware.org/systemtap/tutorial/'>SystemTap tutorial</ulink>
  1852. simply prints a line every time any process on the system open()s
  1853. a file. For each line, it prints the executable name of the
  1854. program that opened the file, along with its PID, and the name
  1855. of the file it opened (or tried to open), which it extracts
  1856. from the open syscall's argstr.
  1857. <literallayout class='monospaced'>
  1858. probe syscall.open
  1859. {
  1860. printf ("%s(%d) open (%s)\n", execname(), pid(), argstr)
  1861. }
  1862. probe timer.ms(4000) # after 4 seconds
  1863. {
  1864. exit ()
  1865. }
  1866. </literallayout>
  1867. Normally, to execute this probe, you'd simply install
  1868. systemtap on the system you want to probe, and directly run
  1869. the probe on that system e.g. assuming the name of the file
  1870. containing the above text is trace_open.stp:
  1871. <literallayout class='monospaced'>
  1872. # stap trace_open.stp
  1873. </literallayout>
  1874. What systemtap does under the covers to run this probe is 1)
  1875. parse and convert the probe to an equivalent 'C' form, 2)
  1876. compile the 'C' form into a kernel module, 3) insert the
  1877. module into the kernel, which arms it, and 4) collect the data
  1878. generated by the probe and display it to the user.
  1879. </para>
  1880. <para>
  1881. In order to accomplish steps 1 and 2, the 'stap' program needs
  1882. access to the kernel build system that produced the kernel
  1883. that the probed system is running. In the case of a typical
  1884. embedded system (the 'target'), the kernel build system
  1885. unfortunately isn't typically part of the image running on
  1886. the target. It is normally available on the 'host' system
  1887. that produced the target image however; in such cases,
  1888. steps 1 and 2 are executed on the host system, and steps
  1889. 3 and 4 are executed on the target system, using only the
  1890. systemtap 'runtime'.
  1891. </para>
  1892. <para>
  1893. The systemtap support in Yocto assumes that only steps
  1894. 3 and 4 are run on the target; it is possible to do
  1895. everything on the target, but this section assumes only
  1896. the typical embedded use-case.
  1897. </para>
  1898. <para>
  1899. So basically what you need to do in order to run a systemtap
  1900. script on the target is to 1) on the host system, compile the
  1901. probe into a kernel module that makes sense to the target, 2)
  1902. copy the module onto the target system and 3) insert the
  1903. module into the target kernel, which arms it, and 4) collect
  1904. the data generated by the probe and display it to the user.
  1905. </para>
  1906. <section id='systemtap-setup'>
  1907. <title>Setup</title>
  1908. <para>
  1909. Those are a lot of steps and a lot of details, but
  1910. fortunately Yocto includes a script called 'crosstap'
  1911. that will take care of those details, allowing you to
  1912. simply execute a systemtap script on the remote target,
  1913. with arguments if necessary.
  1914. </para>
  1915. <para>
  1916. In order to do this from a remote host, however, you
  1917. need to have access to the build for the image you
  1918. booted. The 'crosstap' script provides details on how
  1919. to do this if you run the script on the host without having
  1920. done a build:
  1921. <note>
  1922. SystemTap, which uses 'crosstap', assumes you can establish an
  1923. ssh connection to the remote target.
  1924. Please refer to the crosstap wiki page for details on verifying
  1925. ssh connections at
  1926. <ulink url='https://wiki.yoctoproject.org/wiki/Tracing_and_Profiling#systemtap'></ulink>.
  1927. Also, the ability to ssh into the target system is not enabled
  1928. by default in *-minimal images.
  1929. </note>
  1930. <literallayout class='monospaced'>
  1931. $ crosstap root@192.168.1.88 trace_open.stp
  1932. Error: No target kernel build found.
  1933. Did you forget to create a local build of your image?
  1934. 'crosstap' requires a local sdk build of the target system
  1935. (or a build that includes 'tools-profile') in order to build
  1936. kernel modules that can probe the target system.
  1937. Practically speaking, that means you need to do the following:
  1938. - If you're running a pre-built image, download the release
  1939. and/or BSP tarballs used to build the image.
  1940. - If you're working from git sources, just clone the metadata
  1941. and BSP layers needed to build the image you'll be booting.
  1942. - Make sure you're properly set up to build a new image (see
  1943. the BSP README and/or the widely available basic documentation
  1944. that discusses how to build images).
  1945. - Build an -sdk version of the image e.g.:
  1946. $ bitbake core-image-sato-sdk
  1947. OR
  1948. - Build a non-sdk image but include the profiling tools:
  1949. [ edit local.conf and add 'tools-profile' to the end of
  1950. the EXTRA_IMAGE_FEATURES variable ]
  1951. $ bitbake core-image-sato
  1952. Once you've build the image on the host system, you're ready to
  1953. boot it (or the equivalent pre-built image) and use 'crosstap'
  1954. to probe it (you need to source the environment as usual first):
  1955. $ source oe-init-build-env
  1956. $ cd ~/my/systemtap/scripts
  1957. $ crosstap root@192.168.1.xxx myscript.stp
  1958. </literallayout>
  1959. So essentially what you need to do is build an SDK image or
  1960. image with 'tools-profile' as detailed in the
  1961. "<link linkend='profile-manual-general-setup'>General Setup</link>"
  1962. section of this manual, and boot the resulting target image.
  1963. </para>
  1964. <note>
  1965. If you have a build directory containing multiple machines,
  1966. you need to have the MACHINE you're connecting to selected
  1967. in local.conf, and the kernel in that machine's build
  1968. directory must match the kernel on the booted system exactly,
  1969. or you'll get the above 'crosstap' message when you try to
  1970. invoke a script.
  1971. </note>
  1972. </section>
  1973. <section id='running-a-script-on-a-target'>
  1974. <title>Running a Script on a Target</title>
  1975. <para>
  1976. Once you've done that, you should be able to run a systemtap
  1977. script on the target:
  1978. <literallayout class='monospaced'>
  1979. $ cd /path/to/yocto
  1980. $ source oe-init-build-env
  1981. ### Shell environment set up for builds. ###
  1982. You can now run 'bitbake &lt;target&gt;'
  1983. Common targets are:
  1984. core-image-minimal
  1985. core-image-sato
  1986. meta-toolchain
  1987. meta-ide-support
  1988. You can also run generated qemu images with a command like 'runqemu qemux86'
  1989. </literallayout>
  1990. Once you've done that, you can cd to whatever directory
  1991. contains your scripts and use 'crosstap' to run the script:
  1992. <literallayout class='monospaced'>
  1993. $ cd /path/to/my/systemap/script
  1994. $ crosstap root@192.168.7.2 trace_open.stp
  1995. </literallayout>
  1996. If you get an error connecting to the target e.g.:
  1997. <literallayout class='monospaced'>
  1998. $ crosstap root@192.168.7.2 trace_open.stp
  1999. error establishing ssh connection on remote 'root@192.168.7.2'
  2000. </literallayout>
  2001. Try ssh'ing to the target and see what happens:
  2002. <literallayout class='monospaced'>
  2003. $ ssh root@192.168.7.2
  2004. </literallayout>
  2005. A lot of the time, connection problems are due specifying a
  2006. wrong IP address or having a 'host key verification error'.
  2007. </para>
  2008. <para>
  2009. If everything worked as planned, you should see something
  2010. like this (enter the password when prompted, or press enter
  2011. if it's set up to use no password):
  2012. <literallayout class='monospaced'>
  2013. $ crosstap root@192.168.7.2 trace_open.stp
  2014. root@192.168.7.2's password:
  2015. matchbox-termin(1036) open ("/tmp/vte3FS2LW", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600)
  2016. matchbox-termin(1036) open ("/tmp/vteJMC7LW", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600)
  2017. </literallayout>
  2018. </para>
  2019. </section>
  2020. <section id='systemtap-documentation'>
  2021. <title>Documentation</title>
  2022. <para>
  2023. The SystemTap language reference can be found here:
  2024. <ulink url='http://sourceware.org/systemtap/langref/'>SystemTap Language Reference</ulink>
  2025. </para>
  2026. <para>
  2027. Links to other SystemTap documents, tutorials, and examples can be
  2028. found here:
  2029. <ulink url='http://sourceware.org/systemtap/documentation.html'>SystemTap documentation page</ulink>
  2030. </para>
  2031. </section>
  2032. </section>
  2033. <section id='profile-manual-sysprof'>
  2034. <title>Sysprof</title>
  2035. <para>
  2036. Sysprof is a very easy to use system-wide profiler that consists
  2037. of a single window with three panes and a few buttons which allow
  2038. you to start, stop, and view the profile from one place.
  2039. </para>
  2040. <section id='sysprof-setup'>
  2041. <title>Setup</title>
  2042. <para>
  2043. For this section, we'll assume you've already performed the
  2044. basic setup outlined in the General Setup section.
  2045. </para>
  2046. <para>
  2047. Sysprof is a GUI-based application that runs on the target
  2048. system. For the rest of this document we assume you've
  2049. ssh'ed to the host and will be running Sysprof on the
  2050. target (you can use the '-X' option to ssh and have the
  2051. Sysprof GUI run on the target but display remotely on the
  2052. host if you want).
  2053. </para>
  2054. </section>
  2055. <section id='sysprof-basic-usage'>
  2056. <title>Basic Usage</title>
  2057. <para>
  2058. To start profiling the system, you simply press the 'Start'
  2059. button. To stop profiling and to start viewing the profile data
  2060. in one easy step, press the 'Profile' button.
  2061. </para>
  2062. <para>
  2063. Once you've pressed the profile button, the three panes will
  2064. fill up with profiling data:
  2065. </para>
  2066. <para>
  2067. <imagedata fileref="figures/sysprof-copy-to-user.png" width="6in" depth="4in" align="center" scalefit="1" />
  2068. </para>
  2069. <para>
  2070. The left pane shows a list of functions and processes.
  2071. Selecting one of those expands that function in the right
  2072. pane, showing all its callees. Note that this caller-oriented
  2073. display is essentially the inverse of perf's default
  2074. callee-oriented callchain display.
  2075. </para>
  2076. <para>
  2077. In the screenshot above, we're focusing on __copy_to_user_ll()
  2078. and looking up the callchain we can see that one of the callers
  2079. of __copy_to_user_ll is sys_read() and the complete callpath
  2080. between them. Notice that this is essentially a portion of the
  2081. same information we saw in the perf display shown in the perf
  2082. section of this page.
  2083. </para>
  2084. <para>
  2085. <imagedata fileref="figures/sysprof-copy-from-user.png" width="6in" depth="4in" align="center" scalefit="1" />
  2086. </para>
  2087. <para>
  2088. Similarly, the above is a snapshot of the Sysprof display of a
  2089. copy-from-user callchain.
  2090. </para>
  2091. <para>
  2092. Finally, looking at the third Sysprof pane in the lower left,
  2093. we can see a list of all the callers of a particular function
  2094. selected in the top left pane. In this case, the lower pane is
  2095. showing all the callers of __mark_inode_dirty:
  2096. </para>
  2097. <para>
  2098. <imagedata fileref="figures/sysprof-callers.png" width="6in" depth="4in" align="center" scalefit="1" />
  2099. </para>
  2100. <para>
  2101. Double-clicking on one of those functions will in turn change the
  2102. focus to the selected function, and so on.
  2103. </para>
  2104. <informalexample>
  2105. <emphasis>Tying it Together:</emphasis> If you like sysprof's 'caller-oriented'
  2106. display, you may be able to approximate it in other tools as
  2107. well. For example, 'perf report' has the -g (--call-graph)
  2108. option that you can experiment with; one of the options is
  2109. 'caller' for an inverted caller-based callgraph display.
  2110. </informalexample>
  2111. </section>
  2112. <section id='sysprof-documentation'>
  2113. <title>Documentation</title>
  2114. <para>
  2115. There doesn't seem to be any documentation for Sysprof, but
  2116. maybe that's because it's pretty self-explanatory.
  2117. The Sysprof website, however, is here:
  2118. <ulink url='http://sysprof.com/'>Sysprof, System-wide Performance Profiler for Linux</ulink>
  2119. </para>
  2120. </section>
  2121. </section>
  2122. <section id='lttng-linux-trace-toolkit-next-generation'>
  2123. <title>LTTng (Linux Trace Toolkit, next generation)</title>
  2124. <section id='lttng-setup'>
  2125. <title>Setup</title>
  2126. <para>
  2127. For this section, we'll assume you've already performed the
  2128. basic setup outlined in the General Setup section.
  2129. LTTng is run on the target system by ssh'ing to it.
  2130. </para>
  2131. </section>
  2132. <section id='collecting-and-viewing-traces'>
  2133. <title>Collecting and Viewing Traces</title>
  2134. <para>
  2135. Once you've applied the above commits and built and booted your
  2136. image (you need to build the core-image-sato-sdk image or use one of the
  2137. other methods described in the General Setup section), you're
  2138. ready to start tracing.
  2139. </para>
  2140. <section id='collecting-and-viewing-a-trace-on-the-target-inside-a-shell'>
  2141. <title>Collecting and viewing a trace on the target (inside a shell)</title>
  2142. <para>
  2143. First, from the host, ssh to the target:
  2144. <literallayout class='monospaced'>
  2145. $ ssh -l root 192.168.1.47
  2146. The authenticity of host '192.168.1.47 (192.168.1.47)' can't be established.
  2147. RSA key fingerprint is 23:bd:c8:b1:a8:71:52:00:ee:00:4f:64:9e:10:b9:7e.
  2148. Are you sure you want to continue connecting (yes/no)? yes
  2149. Warning: Permanently added '192.168.1.47' (RSA) to the list of known hosts.
  2150. root@192.168.1.47's password:
  2151. </literallayout>
  2152. Once on the target, use these steps to create a trace:
  2153. <literallayout class='monospaced'>
  2154. root@crownbay:~# lttng create
  2155. Spawning a session daemon
  2156. Session auto-20121015-232120 created.
  2157. Traces will be written in /home/root/lttng-traces/auto-20121015-232120
  2158. </literallayout>
  2159. Enable the events you want to trace (in this case all
  2160. kernel events):
  2161. <literallayout class='monospaced'>
  2162. root@crownbay:~# lttng enable-event --kernel --all
  2163. All kernel events are enabled in channel channel0
  2164. </literallayout>
  2165. Start the trace:
  2166. <literallayout class='monospaced'>
  2167. root@crownbay:~# lttng start
  2168. Tracing started for session auto-20121015-232120
  2169. </literallayout>
  2170. And then stop the trace after awhile or after running
  2171. a particular workload that you want to trace:
  2172. <literallayout class='monospaced'>
  2173. root@crownbay:~# lttng stop
  2174. Tracing stopped for session auto-20121015-232120
  2175. </literallayout>
  2176. You can now view the trace in text form on the target:
  2177. <literallayout class='monospaced'>
  2178. root@crownbay:~# lttng view
  2179. [23:21:56.989270399] (+?.?????????) sys_geteuid: { 1 }, { }
  2180. [23:21:56.989278081] (+0.000007682) exit_syscall: { 1 }, { ret = 0 }
  2181. [23:21:56.989286043] (+0.000007962) sys_pipe: { 1 }, { fildes = 0xB77B9E8C }
  2182. [23:21:56.989321802] (+0.000035759) exit_syscall: { 1 }, { ret = 0 }
  2183. [23:21:56.989329345] (+0.000007543) sys_mmap_pgoff: { 1 }, { addr = 0x0, len = 10485760, prot = 3, flags = 131362, fd = 4294967295, pgoff = 0 }
  2184. [23:21:56.989351694] (+0.000022349) exit_syscall: { 1 }, { ret = -1247805440 }
  2185. [23:21:56.989432989] (+0.000081295) sys_clone: { 1 }, { clone_flags = 0x411, newsp = 0xB5EFFFE4, parent_tid = 0xFFFFFFFF, child_tid = 0x0 }
  2186. [23:21:56.989477129] (+0.000044140) sched_stat_runtime: { 1 }, { comm = "lttng-consumerd", tid = 1193, runtime = 681660, vruntime = 43367983388 }
  2187. [23:21:56.989486697] (+0.000009568) sched_migrate_task: { 1 }, { comm = "lttng-consumerd", tid = 1193, prio = 20, orig_cpu = 1, dest_cpu = 1 }
  2188. [23:21:56.989508418] (+0.000021721) hrtimer_init: { 1 }, { hrtimer = 3970832076, clockid = 1, mode = 1 }
  2189. [23:21:56.989770462] (+0.000262044) hrtimer_cancel: { 1 }, { hrtimer = 3993865440 }
  2190. [23:21:56.989771580] (+0.000001118) hrtimer_cancel: { 0 }, { hrtimer = 3993812192 }
  2191. [23:21:56.989776957] (+0.000005377) hrtimer_expire_entry: { 1 }, { hrtimer = 3993865440, now = 79815980007057, function = 3238465232 }
  2192. [23:21:56.989778145] (+0.000001188) hrtimer_expire_entry: { 0 }, { hrtimer = 3993812192, now = 79815980008174, function = 3238465232 }
  2193. [23:21:56.989791695] (+0.000013550) softirq_raise: { 1 }, { vec = 1 }
  2194. [23:21:56.989795396] (+0.000003701) softirq_raise: { 0 }, { vec = 1 }
  2195. [23:21:56.989800635] (+0.000005239) softirq_raise: { 0 }, { vec = 9 }
  2196. [23:21:56.989807130] (+0.000006495) sched_stat_runtime: { 1 }, { comm = "lttng-consumerd", tid = 1193, runtime = 330710, vruntime = 43368314098 }
  2197. [23:21:56.989809993] (+0.000002863) sched_stat_runtime: { 0 }, { comm = "lttng-sessiond", tid = 1181, runtime = 1015313, vruntime = 36976733240 }
  2198. [23:21:56.989818514] (+0.000008521) hrtimer_expire_exit: { 0 }, { hrtimer = 3993812192 }
  2199. [23:21:56.989819631] (+0.000001117) hrtimer_expire_exit: { 1 }, { hrtimer = 3993865440 }
  2200. [23:21:56.989821866] (+0.000002235) hrtimer_start: { 0 }, { hrtimer = 3993812192, function = 3238465232, expires = 79815981000000, softexpires = 79815981000000 }
  2201. [23:21:56.989822984] (+0.000001118) hrtimer_start: { 1 }, { hrtimer = 3993865440, function = 3238465232, expires = 79815981000000, softexpires = 79815981000000 }
  2202. [23:21:56.989832762] (+0.000009778) softirq_entry: { 1 }, { vec = 1 }
  2203. [23:21:56.989833879] (+0.000001117) softirq_entry: { 0 }, { vec = 1 }
  2204. [23:21:56.989838069] (+0.000004190) timer_cancel: { 1 }, { timer = 3993871956 }
  2205. [23:21:56.989839187] (+0.000001118) timer_cancel: { 0 }, { timer = 3993818708 }
  2206. [23:21:56.989841492] (+0.000002305) timer_expire_entry: { 1 }, { timer = 3993871956, now = 79515980, function = 3238277552 }
  2207. [23:21:56.989842819] (+0.000001327) timer_expire_entry: { 0 }, { timer = 3993818708, now = 79515980, function = 3238277552 }
  2208. [23:21:56.989854831] (+0.000012012) sched_stat_runtime: { 1 }, { comm = "lttng-consumerd", tid = 1193, runtime = 49237, vruntime = 43368363335 }
  2209. [23:21:56.989855949] (+0.000001118) sched_stat_runtime: { 0 }, { comm = "lttng-sessiond", tid = 1181, runtime = 45121, vruntime = 36976778361 }
  2210. [23:21:56.989861257] (+0.000005308) sched_stat_sleep: { 1 }, { comm = "kworker/1:1", tid = 21, delay = 9451318 }
  2211. [23:21:56.989862374] (+0.000001117) sched_stat_sleep: { 0 }, { comm = "kworker/0:0", tid = 4, delay = 9958820 }
  2212. [23:21:56.989868241] (+0.000005867) sched_wakeup: { 0 }, { comm = "kworker/0:0", tid = 4, prio = 120, success = 1, target_cpu = 0 }
  2213. [23:21:56.989869358] (+0.000001117) sched_wakeup: { 1 }, { comm = "kworker/1:1", tid = 21, prio = 120, success = 1, target_cpu = 1 }
  2214. [23:21:56.989877460] (+0.000008102) timer_expire_exit: { 1 }, { timer = 3993871956 }
  2215. [23:21:56.989878577] (+0.000001117) timer_expire_exit: { 0 }, { timer = 3993818708 }
  2216. .
  2217. .
  2218. .
  2219. </literallayout>
  2220. You can now safely destroy the trace session (note that
  2221. this doesn't delete the trace - it's still there
  2222. in ~/lttng-traces):
  2223. <literallayout class='monospaced'>
  2224. root@crownbay:~# lttng destroy
  2225. Session auto-20121015-232120 destroyed at /home/root
  2226. </literallayout>
  2227. Note that the trace is saved in a directory of the same
  2228. name as returned by 'lttng create', under the ~/lttng-traces
  2229. directory (note that you can change this by supplying your
  2230. own name to 'lttng create'):
  2231. <literallayout class='monospaced'>
  2232. root@crownbay:~# ls -al ~/lttng-traces
  2233. drwxrwx--- 3 root root 1024 Oct 15 23:21 .
  2234. drwxr-xr-x 5 root root 1024 Oct 15 23:57 ..
  2235. drwxrwx--- 3 root root 1024 Oct 15 23:21 auto-20121015-232120
  2236. </literallayout>
  2237. </para>
  2238. </section>
  2239. <section id='collecting-and-viewing-a-userspace-trace-on-the-target-inside-a-shell'>
  2240. <title>Collecting and viewing a userspace trace on the target (inside a shell)</title>
  2241. <para>
  2242. For LTTng userspace tracing, you need to have a properly
  2243. instrumented userspace program. For this example, we'll use
  2244. the 'hello' test program generated by the lttng-ust build.
  2245. </para>
  2246. <para>
  2247. The 'hello' test program isn't installed on the rootfs by
  2248. the lttng-ust build, so we need to copy it over manually.
  2249. First cd into the build directory that contains the hello
  2250. executable:
  2251. <literallayout class='monospaced'>
  2252. $ cd build/tmp/work/core2_32-poky-linux/lttng-ust/2.0.5-r0/git/tests/hello/.libs
  2253. </literallayout>
  2254. Copy that over to the target machine:
  2255. <literallayout class='monospaced'>
  2256. $ scp hello root@192.168.1.20:
  2257. </literallayout>
  2258. You now have the instrumented lttng 'hello world' test
  2259. program on the target, ready to test.
  2260. </para>
  2261. <para>
  2262. First, from the host, ssh to the target:
  2263. <literallayout class='monospaced'>
  2264. $ ssh -l root 192.168.1.47
  2265. The authenticity of host '192.168.1.47 (192.168.1.47)' can't be established.
  2266. RSA key fingerprint is 23:bd:c8:b1:a8:71:52:00:ee:00:4f:64:9e:10:b9:7e.
  2267. Are you sure you want to continue connecting (yes/no)? yes
  2268. Warning: Permanently added '192.168.1.47' (RSA) to the list of known hosts.
  2269. root@192.168.1.47's password:
  2270. </literallayout>
  2271. Once on the target, use these steps to create a trace:
  2272. <literallayout class='monospaced'>
  2273. root@crownbay:~# lttng create
  2274. Session auto-20190303-021943 created.
  2275. Traces will be written in /home/root/lttng-traces/auto-20190303-021943
  2276. </literallayout>
  2277. Enable the events you want to trace (in this case all
  2278. userspace events):
  2279. <literallayout class='monospaced'>
  2280. root@crownbay:~# lttng enable-event --userspace --all
  2281. All UST events are enabled in channel channel0
  2282. </literallayout>
  2283. Start the trace:
  2284. <literallayout class='monospaced'>
  2285. root@crownbay:~# lttng start
  2286. Tracing started for session auto-20190303-021943
  2287. </literallayout>
  2288. Run the instrumented hello world program:
  2289. <literallayout class='monospaced'>
  2290. root@crownbay:~# ./hello
  2291. Hello, World!
  2292. Tracing... done.
  2293. </literallayout>
  2294. And then stop the trace after awhile or after running a
  2295. particular workload that you want to trace:
  2296. <literallayout class='monospaced'>
  2297. root@crownbay:~# lttng stop
  2298. Tracing stopped for session auto-20190303-021943
  2299. </literallayout>
  2300. You can now view the trace in text form on the target:
  2301. <literallayout class='monospaced'>
  2302. root@crownbay:~# lttng view
  2303. [02:31:14.906146544] (+?.?????????) hello:1424 ust_tests_hello:tptest: { cpu_id = 1 }, { intfield = 0, intfield2 = 0x0, longfield = 0, netintfield = 0, netintfieldhex = 0x0, arrfield1 = [ [0] = 1, [1] = 2, [2] = 3 ], arrfield2 = "test", _seqfield1_length = 4, seqfield1 = [ [0] = 116, [1] = 101, [2] = 115, [3] = 116 ], _seqfield2_length = 4, seqfield2 = "test", stringfield = "test", floatfield = 2222, doublefield = 2, boolfield = 1 }
  2304. [02:31:14.906170360] (+0.000023816) hello:1424 ust_tests_hello:tptest: { cpu_id = 1 }, { intfield = 1, intfield2 = 0x1, longfield = 1, netintfield = 1, netintfieldhex = 0x1, arrfield1 = [ [0] = 1, [1] = 2, [2] = 3 ], arrfield2 = "test", _seqfield1_length = 4, seqfield1 = [ [0] = 116, [1] = 101, [2] = 115, [3] = 116 ], _seqfield2_length = 4, seqfield2 = "test", stringfield = "test", floatfield = 2222, doublefield = 2, boolfield = 1 }
  2305. [02:31:14.906183140] (+0.000012780) hello:1424 ust_tests_hello:tptest: { cpu_id = 1 }, { intfield = 2, intfield2 = 0x2, longfield = 2, netintfield = 2, netintfieldhex = 0x2, arrfield1 = [ [0] = 1, [1] = 2, [2] = 3 ], arrfield2 = "test", _seqfield1_length = 4, seqfield1 = [ [0] = 116, [1] = 101, [2] = 115, [3] = 116 ], _seqfield2_length = 4, seqfield2 = "test", stringfield = "test", floatfield = 2222, doublefield = 2, boolfield = 1 }
  2306. [02:31:14.906194385] (+0.000011245) hello:1424 ust_tests_hello:tptest: { cpu_id = 1 }, { intfield = 3, intfield2 = 0x3, longfield = 3, netintfield = 3, netintfieldhex = 0x3, arrfield1 = [ [0] = 1, [1] = 2, [2] = 3 ], arrfield2 = "test", _seqfield1_length = 4, seqfield1 = [ [0] = 116, [1] = 101, [2] = 115, [3] = 116 ], _seqfield2_length = 4, seqfield2 = "test", stringfield = "test", floatfield = 2222, doublefield = 2, boolfield = 1 }
  2307. .
  2308. .
  2309. .
  2310. </literallayout>
  2311. You can now safely destroy the trace session (note that
  2312. this doesn't delete the trace - it's still
  2313. there in ~/lttng-traces):
  2314. <literallayout class='monospaced'>
  2315. root@crownbay:~# lttng destroy
  2316. Session auto-20190303-021943 destroyed at /home/root
  2317. </literallayout>
  2318. </para>
  2319. </section>
  2320. </section>
  2321. <section id='lltng-documentation'>
  2322. <title>Documentation</title>
  2323. <para>
  2324. You can find the primary LTTng Documentation on the
  2325. <ulink url='https://lttng.org/docs/'>LTTng Documentation</ulink>
  2326. site.
  2327. The documentation on this site is appropriate for intermediate to
  2328. advanced software developers who are working in a Linux environment
  2329. and are interested in efficient software tracing.
  2330. </para>
  2331. <para>
  2332. For information on LTTng in general, visit the
  2333. <ulink url='http://lttng.org/lttng2.0'>LTTng Project</ulink>
  2334. site.
  2335. You can find a "Getting Started" link on this site that takes
  2336. you to an LTTng Quick Start.
  2337. </para>
  2338. </section>
  2339. </section>
  2340. <section id='profile-manual-blktrace'>
  2341. <title>blktrace</title>
  2342. <para>
  2343. blktrace is a tool for tracing and reporting low-level disk I/O.
  2344. blktrace provides the tracing half of the equation; its output can
  2345. be piped into the blkparse program, which renders the data in a
  2346. human-readable form and does some basic analysis:
  2347. </para>
  2348. <section id='blktrace-setup'>
  2349. <title>Setup</title>
  2350. <para>
  2351. For this section, we'll assume you've already performed the
  2352. basic setup outlined in the
  2353. "<link linkend='profile-manual-general-setup'>General Setup</link>"
  2354. section.
  2355. </para>
  2356. <para>
  2357. blktrace is an application that runs on the target system.
  2358. You can run the entire blktrace and blkparse pipeline on the
  2359. target, or you can run blktrace in 'listen' mode on the target
  2360. and have blktrace and blkparse collect and analyze the data on
  2361. the host (see the
  2362. "<link linkend='using-blktrace-remotely'>Using blktrace Remotely</link>"
  2363. section below).
  2364. For the rest of this section we assume you've ssh'ed to the
  2365. host and will be running blkrace on the target.
  2366. </para>
  2367. </section>
  2368. <section id='blktrace-basic-usage'>
  2369. <title>Basic Usage</title>
  2370. <para>
  2371. To record a trace, simply run the 'blktrace' command, giving it
  2372. the name of the block device you want to trace activity on:
  2373. <literallayout class='monospaced'>
  2374. root@crownbay:~# blktrace /dev/sdc
  2375. </literallayout>
  2376. In another shell, execute a workload you want to trace.
  2377. <literallayout class='monospaced'>
  2378. root@crownbay:/media/sdc# rm linux-2.6.19.2.tar.bz2; wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>; sync
  2379. Connecting to downloads.yoctoproject.org (140.211.169.59:80)
  2380. linux-2.6.19.2.tar.b 100% |*******************************| 41727k 0:00:00 ETA
  2381. </literallayout>
  2382. Press Ctrl-C in the blktrace shell to stop the trace. It will
  2383. display how many events were logged, along with the per-cpu file
  2384. sizes (blktrace records traces in per-cpu kernel buffers and
  2385. simply dumps them to userspace for blkparse to merge and sort
  2386. later).
  2387. <literallayout class='monospaced'>
  2388. ^C=== sdc ===
  2389. CPU 0: 7082 events, 332 KiB data
  2390. CPU 1: 1578 events, 74 KiB data
  2391. Total: 8660 events (dropped 0), 406 KiB data
  2392. </literallayout>
  2393. If you examine the files saved to disk, you see multiple files,
  2394. one per CPU and with the device name as the first part of the
  2395. filename:
  2396. <literallayout class='monospaced'>
  2397. root@crownbay:~# ls -al
  2398. drwxr-xr-x 6 root root 1024 Oct 27 22:39 .
  2399. drwxr-sr-x 4 root root 1024 Oct 26 18:24 ..
  2400. -rw-r--r-- 1 root root 339938 Oct 27 22:40 sdc.blktrace.0
  2401. -rw-r--r-- 1 root root 75753 Oct 27 22:40 sdc.blktrace.1
  2402. </literallayout>
  2403. To view the trace events, simply invoke 'blkparse' in the
  2404. directory containing the trace files, giving it the device name
  2405. that forms the first part of the filenames:
  2406. <literallayout class='monospaced'>
  2407. root@crownbay:~# blkparse sdc
  2408. 8,32 1 1 0.000000000 1225 Q WS 3417048 + 8 [jbd2/sdc-8]
  2409. 8,32 1 2 0.000025213 1225 G WS 3417048 + 8 [jbd2/sdc-8]
  2410. 8,32 1 3 0.000033384 1225 P N [jbd2/sdc-8]
  2411. 8,32 1 4 0.000043301 1225 I WS 3417048 + 8 [jbd2/sdc-8]
  2412. 8,32 1 0 0.000057270 0 m N cfq1225 insert_request
  2413. 8,32 1 0 0.000064813 0 m N cfq1225 add_to_rr
  2414. 8,32 1 5 0.000076336 1225 U N [jbd2/sdc-8] 1
  2415. 8,32 1 0 0.000088559 0 m N cfq workload slice:150
  2416. 8,32 1 0 0.000097359 0 m N cfq1225 set_active wl_prio:0 wl_type:1
  2417. 8,32 1 0 0.000104063 0 m N cfq1225 Not idling. st->count:1
  2418. 8,32 1 0 0.000112584 0 m N cfq1225 fifo= (null)
  2419. 8,32 1 0 0.000118730 0 m N cfq1225 dispatch_insert
  2420. 8,32 1 0 0.000127390 0 m N cfq1225 dispatched a request
  2421. 8,32 1 0 0.000133536 0 m N cfq1225 activate rq, drv=1
  2422. 8,32 1 6 0.000136889 1225 D WS 3417048 + 8 [jbd2/sdc-8]
  2423. 8,32 1 7 0.000360381 1225 Q WS 3417056 + 8 [jbd2/sdc-8]
  2424. 8,32 1 8 0.000377422 1225 G WS 3417056 + 8 [jbd2/sdc-8]
  2425. 8,32 1 9 0.000388876 1225 P N [jbd2/sdc-8]
  2426. 8,32 1 10 0.000397886 1225 Q WS 3417064 + 8 [jbd2/sdc-8]
  2427. 8,32 1 11 0.000404800 1225 M WS 3417064 + 8 [jbd2/sdc-8]
  2428. 8,32 1 12 0.000412343 1225 Q WS 3417072 + 8 [jbd2/sdc-8]
  2429. 8,32 1 13 0.000416533 1225 M WS 3417072 + 8 [jbd2/sdc-8]
  2430. 8,32 1 14 0.000422121 1225 Q WS 3417080 + 8 [jbd2/sdc-8]
  2431. 8,32 1 15 0.000425194 1225 M WS 3417080 + 8 [jbd2/sdc-8]
  2432. 8,32 1 16 0.000431968 1225 Q WS 3417088 + 8 [jbd2/sdc-8]
  2433. 8,32 1 17 0.000435251 1225 M WS 3417088 + 8 [jbd2/sdc-8]
  2434. 8,32 1 18 0.000440279 1225 Q WS 3417096 + 8 [jbd2/sdc-8]
  2435. 8,32 1 19 0.000443911 1225 M WS 3417096 + 8 [jbd2/sdc-8]
  2436. 8,32 1 20 0.000450336 1225 Q WS 3417104 + 8 [jbd2/sdc-8]
  2437. 8,32 1 21 0.000454038 1225 M WS 3417104 + 8 [jbd2/sdc-8]
  2438. 8,32 1 22 0.000462070 1225 Q WS 3417112 + 8 [jbd2/sdc-8]
  2439. 8,32 1 23 0.000465422 1225 M WS 3417112 + 8 [jbd2/sdc-8]
  2440. 8,32 1 24 0.000474222 1225 I WS 3417056 + 64 [jbd2/sdc-8]
  2441. 8,32 1 0 0.000483022 0 m N cfq1225 insert_request
  2442. 8,32 1 25 0.000489727 1225 U N [jbd2/sdc-8] 1
  2443. 8,32 1 0 0.000498457 0 m N cfq1225 Not idling. st->count:1
  2444. 8,32 1 0 0.000503765 0 m N cfq1225 dispatch_insert
  2445. 8,32 1 0 0.000512914 0 m N cfq1225 dispatched a request
  2446. 8,32 1 0 0.000518851 0 m N cfq1225 activate rq, drv=2
  2447. .
  2448. .
  2449. .
  2450. 8,32 0 0 58.515006138 0 m N cfq3551 complete rqnoidle 1
  2451. 8,32 0 2024 58.516603269 3 C WS 3156992 + 16 [0]
  2452. 8,32 0 0 58.516626736 0 m N cfq3551 complete rqnoidle 1
  2453. 8,32 0 0 58.516634558 0 m N cfq3551 arm_idle: 8 group_idle: 0
  2454. 8,32 0 0 58.516636933 0 m N cfq schedule dispatch
  2455. 8,32 1 0 58.516971613 0 m N cfq3551 slice expired t=0
  2456. 8,32 1 0 58.516982089 0 m N cfq3551 sl_used=13 disp=6 charge=13 iops=0 sect=80
  2457. 8,32 1 0 58.516985511 0 m N cfq3551 del_from_rr
  2458. 8,32 1 0 58.516990819 0 m N cfq3551 put_queue
  2459. CPU0 (sdc):
  2460. Reads Queued: 0, 0KiB Writes Queued: 331, 26,284KiB
  2461. Read Dispatches: 0, 0KiB Write Dispatches: 485, 40,484KiB
  2462. Reads Requeued: 0 Writes Requeued: 0
  2463. Reads Completed: 0, 0KiB Writes Completed: 511, 41,000KiB
  2464. Read Merges: 0, 0KiB Write Merges: 13, 160KiB
  2465. Read depth: 0 Write depth: 2
  2466. IO unplugs: 23 Timer unplugs: 0
  2467. CPU1 (sdc):
  2468. Reads Queued: 0, 0KiB Writes Queued: 249, 15,800KiB
  2469. Read Dispatches: 0, 0KiB Write Dispatches: 42, 1,600KiB
  2470. Reads Requeued: 0 Writes Requeued: 0
  2471. Reads Completed: 0, 0KiB Writes Completed: 16, 1,084KiB
  2472. Read Merges: 0, 0KiB Write Merges: 40, 276KiB
  2473. Read depth: 0 Write depth: 2
  2474. IO unplugs: 30 Timer unplugs: 1
  2475. Total (sdc):
  2476. Reads Queued: 0, 0KiB Writes Queued: 580, 42,084KiB
  2477. Read Dispatches: 0, 0KiB Write Dispatches: 527, 42,084KiB
  2478. Reads Requeued: 0 Writes Requeued: 0
  2479. Reads Completed: 0, 0KiB Writes Completed: 527, 42,084KiB
  2480. Read Merges: 0, 0KiB Write Merges: 53, 436KiB
  2481. IO unplugs: 53 Timer unplugs: 1
  2482. Throughput (R/W): 0KiB/s / 719KiB/s
  2483. Events (sdc): 6,592 entries
  2484. Skips: 0 forward (0 - 0.0%)
  2485. Input file sdc.blktrace.0 added
  2486. Input file sdc.blktrace.1 added
  2487. </literallayout>
  2488. The report shows each event that was found in the blktrace data,
  2489. along with a summary of the overall block I/O traffic during
  2490. the run. You can look at the
  2491. <ulink url='http://linux.die.net/man/1/blkparse'>blkparse</ulink>
  2492. manpage to learn the
  2493. meaning of each field displayed in the trace listing.
  2494. </para>
  2495. <section id='blktrace-live-mode'>
  2496. <title>Live Mode</title>
  2497. <para>
  2498. blktrace and blkparse are designed from the ground up to
  2499. be able to operate together in a 'pipe mode' where the
  2500. stdout of blktrace can be fed directly into the stdin of
  2501. blkparse:
  2502. <literallayout class='monospaced'>
  2503. root@crownbay:~# blktrace /dev/sdc -o - | blkparse -i -
  2504. </literallayout>
  2505. This enables long-lived tracing sessions to run without
  2506. writing anything to disk, and allows the user to look for
  2507. certain conditions in the trace data in 'real-time' by
  2508. viewing the trace output as it scrolls by on the screen or
  2509. by passing it along to yet another program in the pipeline
  2510. such as grep which can be used to identify and capture
  2511. conditions of interest.
  2512. </para>
  2513. <para>
  2514. There's actually another blktrace command that implements
  2515. the above pipeline as a single command, so the user doesn't
  2516. have to bother typing in the above command sequence:
  2517. <literallayout class='monospaced'>
  2518. root@crownbay:~# btrace /dev/sdc
  2519. </literallayout>
  2520. </para>
  2521. </section>
  2522. <section id='using-blktrace-remotely'>
  2523. <title>Using blktrace Remotely</title>
  2524. <para>
  2525. Because blktrace traces block I/O and at the same time
  2526. normally writes its trace data to a block device, and
  2527. in general because it's not really a great idea to make
  2528. the device being traced the same as the device the tracer
  2529. writes to, blktrace provides a way to trace without
  2530. perturbing the traced device at all by providing native
  2531. support for sending all trace data over the network.
  2532. </para>
  2533. <para>
  2534. To have blktrace operate in this mode, start blktrace on
  2535. the target system being traced with the -l option, along with
  2536. the device to trace:
  2537. <literallayout class='monospaced'>
  2538. root@crownbay:~# blktrace -l /dev/sdc
  2539. server: waiting for connections...
  2540. </literallayout>
  2541. On the host system, use the -h option to connect to the
  2542. target system, also passing it the device to trace:
  2543. <literallayout class='monospaced'>
  2544. $ blktrace -d /dev/sdc -h 192.168.1.43
  2545. blktrace: connecting to 192.168.1.43
  2546. blktrace: connected!
  2547. </literallayout>
  2548. On the target system, you should see this:
  2549. <literallayout class='monospaced'>
  2550. server: connection from 192.168.1.43
  2551. </literallayout>
  2552. In another shell, execute a workload you want to trace.
  2553. <literallayout class='monospaced'>
  2554. root@crownbay:/media/sdc# rm linux-2.6.19.2.tar.bz2; wget <ulink url='http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2'>http://downloads.yoctoproject.org/mirror/sources/linux-2.6.19.2.tar.bz2</ulink>; sync
  2555. Connecting to downloads.yoctoproject.org (140.211.169.59:80)
  2556. linux-2.6.19.2.tar.b 100% |*******************************| 41727k 0:00:00 ETA
  2557. </literallayout>
  2558. When it's done, do a Ctrl-C on the host system to
  2559. stop the trace:
  2560. <literallayout class='monospaced'>
  2561. ^C=== sdc ===
  2562. CPU 0: 7691 events, 361 KiB data
  2563. CPU 1: 4109 events, 193 KiB data
  2564. Total: 11800 events (dropped 0), 554 KiB data
  2565. </literallayout>
  2566. On the target system, you should also see a trace
  2567. summary for the trace just ended:
  2568. <literallayout class='monospaced'>
  2569. server: end of run for 192.168.1.43:sdc
  2570. === sdc ===
  2571. CPU 0: 7691 events, 361 KiB data
  2572. CPU 1: 4109 events, 193 KiB data
  2573. Total: 11800 events (dropped 0), 554 KiB data
  2574. </literallayout>
  2575. The blktrace instance on the host will save the target
  2576. output inside a hostname-timestamp directory:
  2577. <literallayout class='monospaced'>
  2578. $ ls -al
  2579. drwxr-xr-x 10 root root 1024 Oct 28 02:40 .
  2580. drwxr-sr-x 4 root root 1024 Oct 26 18:24 ..
  2581. drwxr-xr-x 2 root root 1024 Oct 28 02:40 192.168.1.43-2012-10-28-02:40:56
  2582. </literallayout>
  2583. cd into that directory to see the output files:
  2584. <literallayout class='monospaced'>
  2585. $ ls -l
  2586. -rw-r--r-- 1 root root 369193 Oct 28 02:44 sdc.blktrace.0
  2587. -rw-r--r-- 1 root root 197278 Oct 28 02:44 sdc.blktrace.1
  2588. </literallayout>
  2589. And run blkparse on the host system using the device name:
  2590. <literallayout class='monospaced'>
  2591. $ blkparse sdc
  2592. 8,32 1 1 0.000000000 1263 Q RM 6016 + 8 [ls]
  2593. 8,32 1 0 0.000036038 0 m N cfq1263 alloced
  2594. 8,32 1 2 0.000039390 1263 G RM 6016 + 8 [ls]
  2595. 8,32 1 3 0.000049168 1263 I RM 6016 + 8 [ls]
  2596. 8,32 1 0 0.000056152 0 m N cfq1263 insert_request
  2597. 8,32 1 0 0.000061600 0 m N cfq1263 add_to_rr
  2598. 8,32 1 0 0.000075498 0 m N cfq workload slice:300
  2599. .
  2600. .
  2601. .
  2602. 8,32 0 0 177.266385696 0 m N cfq1267 arm_idle: 8 group_idle: 0
  2603. 8,32 0 0 177.266388140 0 m N cfq schedule dispatch
  2604. 8,32 1 0 177.266679239 0 m N cfq1267 slice expired t=0
  2605. 8,32 1 0 177.266689297 0 m N cfq1267 sl_used=9 disp=6 charge=9 iops=0 sect=56
  2606. 8,32 1 0 177.266692649 0 m N cfq1267 del_from_rr
  2607. 8,32 1 0 177.266696560 0 m N cfq1267 put_queue
  2608. CPU0 (sdc):
  2609. Reads Queued: 0, 0KiB Writes Queued: 270, 21,708KiB
  2610. Read Dispatches: 59, 2,628KiB Write Dispatches: 495, 39,964KiB
  2611. Reads Requeued: 0 Writes Requeued: 0
  2612. Reads Completed: 90, 2,752KiB Writes Completed: 543, 41,596KiB
  2613. Read Merges: 0, 0KiB Write Merges: 9, 344KiB
  2614. Read depth: 2 Write depth: 2
  2615. IO unplugs: 20 Timer unplugs: 1
  2616. CPU1 (sdc):
  2617. Reads Queued: 688, 2,752KiB Writes Queued: 381, 20,652KiB
  2618. Read Dispatches: 31, 124KiB Write Dispatches: 59, 2,396KiB
  2619. Reads Requeued: 0 Writes Requeued: 0
  2620. Reads Completed: 0, 0KiB Writes Completed: 11, 764KiB
  2621. Read Merges: 598, 2,392KiB Write Merges: 88, 448KiB
  2622. Read depth: 2 Write depth: 2
  2623. IO unplugs: 52 Timer unplugs: 0
  2624. Total (sdc):
  2625. Reads Queued: 688, 2,752KiB Writes Queued: 651, 42,360KiB
  2626. Read Dispatches: 90, 2,752KiB Write Dispatches: 554, 42,360KiB
  2627. Reads Requeued: 0 Writes Requeued: 0
  2628. Reads Completed: 90, 2,752KiB Writes Completed: 554, 42,360KiB
  2629. Read Merges: 598, 2,392KiB Write Merges: 97, 792KiB
  2630. IO unplugs: 72 Timer unplugs: 1
  2631. Throughput (R/W): 15KiB/s / 238KiB/s
  2632. Events (sdc): 9,301 entries
  2633. Skips: 0 forward (0 - 0.0%)
  2634. </literallayout>
  2635. You should see the trace events and summary just as
  2636. you would have if you'd run the same command on the target.
  2637. </para>
  2638. </section>
  2639. <section id='tracing-block-io-via-ftrace'>
  2640. <title>Tracing Block I/O via 'ftrace'</title>
  2641. <para>
  2642. It's also possible to trace block I/O using only
  2643. <link linkend='the-trace-events-subsystem'>trace events subsystem</link>,
  2644. which can be useful for casual tracing
  2645. if you don't want to bother dealing with the userspace tools.
  2646. </para>
  2647. <para>
  2648. To enable tracing for a given device, use
  2649. /sys/block/xxx/trace/enable, where xxx is the device name.
  2650. This for example enables tracing for /dev/sdc:
  2651. <literallayout class='monospaced'>
  2652. root@crownbay:/sys/kernel/debug/tracing# echo 1 > /sys/block/sdc/trace/enable
  2653. </literallayout>
  2654. Once you've selected the device(s) you want to trace,
  2655. selecting the 'blk' tracer will turn the blk tracer on:
  2656. <literallayout class='monospaced'>
  2657. root@crownbay:/sys/kernel/debug/tracing# cat available_tracers
  2658. blk function_graph function nop
  2659. root@crownbay:/sys/kernel/debug/tracing# echo blk > current_tracer
  2660. </literallayout>
  2661. Execute the workload you're interested in:
  2662. <literallayout class='monospaced'>
  2663. root@crownbay:/sys/kernel/debug/tracing# cat /media/sdc/testfile.txt
  2664. </literallayout>
  2665. And look at the output (note here that we're using
  2666. 'trace_pipe' instead of trace to capture this trace -
  2667. this allows us to wait around on the pipe for data to
  2668. appear):
  2669. <literallayout class='monospaced'>
  2670. root@crownbay:/sys/kernel/debug/tracing# cat trace_pipe
  2671. cat-3587 [001] d..1 3023.276361: 8,32 Q R 1699848 + 8 [cat]
  2672. cat-3587 [001] d..1 3023.276410: 8,32 m N cfq3587 alloced
  2673. cat-3587 [001] d..1 3023.276415: 8,32 G R 1699848 + 8 [cat]
  2674. cat-3587 [001] d..1 3023.276424: 8,32 P N [cat]
  2675. cat-3587 [001] d..2 3023.276432: 8,32 I R 1699848 + 8 [cat]
  2676. cat-3587 [001] d..1 3023.276439: 8,32 m N cfq3587 insert_request
  2677. cat-3587 [001] d..1 3023.276445: 8,32 m N cfq3587 add_to_rr
  2678. cat-3587 [001] d..2 3023.276454: 8,32 U N [cat] 1
  2679. cat-3587 [001] d..1 3023.276464: 8,32 m N cfq workload slice:150
  2680. cat-3587 [001] d..1 3023.276471: 8,32 m N cfq3587 set_active wl_prio:0 wl_type:2
  2681. cat-3587 [001] d..1 3023.276478: 8,32 m N cfq3587 fifo= (null)
  2682. cat-3587 [001] d..1 3023.276483: 8,32 m N cfq3587 dispatch_insert
  2683. cat-3587 [001] d..1 3023.276490: 8,32 m N cfq3587 dispatched a request
  2684. cat-3587 [001] d..1 3023.276497: 8,32 m N cfq3587 activate rq, drv=1
  2685. cat-3587 [001] d..2 3023.276500: 8,32 D R 1699848 + 8 [cat]
  2686. </literallayout>
  2687. And this turns off tracing for the specified device:
  2688. <literallayout class='monospaced'>
  2689. root@crownbay:/sys/kernel/debug/tracing# echo 0 > /sys/block/sdc/trace/enable
  2690. </literallayout>
  2691. </para>
  2692. </section>
  2693. </section>
  2694. <section id='blktrace-documentation'>
  2695. <title>Documentation</title>
  2696. <para>
  2697. Online versions of the man pages for the commands discussed
  2698. in this section can be found here:
  2699. <itemizedlist>
  2700. <listitem><para><ulink url='http://linux.die.net/man/8/blktrace'>http://linux.die.net/man/8/blktrace</ulink>
  2701. </para></listitem>
  2702. <listitem><para><ulink url='http://linux.die.net/man/1/blkparse'>http://linux.die.net/man/1/blkparse</ulink>
  2703. </para></listitem>
  2704. <listitem><para><ulink url='http://linux.die.net/man/8/btrace'>http://linux.die.net/man/8/btrace</ulink>
  2705. </para></listitem>
  2706. </itemizedlist>
  2707. </para>
  2708. <para>
  2709. The above manpages, along with manpages for the other
  2710. blktrace utilities (btt, blkiomon, etc) can be found in the
  2711. /doc directory of the blktrace tools git repo:
  2712. <literallayout class='monospaced'>
  2713. $ git clone git://git.kernel.dk/blktrace.git
  2714. </literallayout>
  2715. </para>
  2716. </section>
  2717. </section>
  2718. </chapter>
  2719. <!--
  2720. vim: expandtab tw=80 ts=4
  2721. -->