viewing-and-analyzing-your-traces: minox fixes
[lttng-docs.git] / contents / getting-started / viewing-and-analyzing.md
CommitLineData
5e0cbfb0
PP
1---
2id: viewing-and-analyzing-your-traces
3---
4
5This section describes how to visualize the data gathered after tracing
6the Linux kernel or a user space application.
7
1c3359df 8Many ways exist to read LTTng traces:
5e0cbfb0
PP
9
10 * **`babeltrace`** is a command line utility which converts trace formats;
11 it supports the format used by LTTng,
12 <abbr title="Common Trace Format">CTF</abbr>, as well as a basic
13 text output which may be `grep`ed. The `babeltrace` command is
14 part of the
1c3359df
PP
15 <a href="http://diamon.org/babeltrace" class="ext">Babeltrace</a> project.
16 * Babeltrace also includes **Python bindings** so that you may
5e0cbfb0
PP
17 easily open and read an LTTng trace with your own script, benefiting
18 from the power of Python.
1c3359df 19 * **<a href="http://tracecompass.org/" class="ext">Trace Compass</a>**
b8734c94 20 is an Eclipse plugin used to visualize and analyze various types of
1c3359df 21 traces, including LTTng's. It also comes as a standalone application.
5e0cbfb0 22
1c3359df 23LTTng trace files are recorded in the `~/lttng-traces` directory by default.
5e0cbfb0
PP
24Let's now view the trace and perform a basic analysis using
25`babeltrace`.
26
27The simplest way to list all the recorded events of a trace is to pass its
28path to `babeltrace` with no options:
29
30<pre class="term">
31babeltrace ~/lttng-traces/my-session
32</pre>
33
1c3359df 34`babeltrace` finds all traces recursively within the given path and
47bfcb75 35prints all their events, merging them in order of time.
5e0cbfb0
PP
36
37Listing all the system calls of a Linux kernel trace with their arguments is
38easy with `babeltrace` and `grep`:
39
40<pre class="term">
41babeltrace ~/lttng-traces/my-kernel-session | grep sys_
42</pre>
43
44Counting events is also straightforward:
45
46<pre class="term">
b80ba306 47babeltrace ~/lttng-traces/my-kernel-session | grep sys_read | wc --lines
5e0cbfb0
PP
48</pre>
49
50The text output of `babeltrace` is useful for isolating events by simple
51matching using `grep` and similar utilities. However, more elaborate filters
52such as keeping only events with a field value falling within a specific range
53are not trivial to write using a shell. Moreover, reductions and even the
54most basic computations involving multiple events are virtually impossible
55to implement.
56
1c3359df 57Fortunately, Babeltrace ships with Python 3 bindings which makes it
5e0cbfb0
PP
58really easy to read the events of an LTTng trace sequentially and compute
59the desired information.
60
1c3359df 61Here's a simple example using the Babeltrace Python bindings. The following
5e0cbfb0 62script accepts an LTTng Linux kernel trace path as its first argument and
1c3359df 63prints the short names of the top 5 running processes on CPU 0 during the
5e0cbfb0
PP
64whole trace:
65
66~~~ python
67import sys
68from collections import Counter
69import babeltrace
70
71
72def top5proc():
73 if len(sys.argv) != 2:
74 msg = 'Usage: python {} TRACEPATH'.format(sys.argv[0])
75 raise ValueError(msg)
76
77 # a trace collection holds one to many traces
78 col = babeltrace.TraceCollection()
79
80 # add the trace provided by the user
81 # (LTTng traces always have the 'ctf' format)
82 if col.add_trace(sys.argv[1], 'ctf') is None:
83 raise RuntimeError('Cannot add trace')
84
85 # this counter dict will hold execution times:
86 #
87 # task command name -> total execution time (ns)
88 exec_times = Counter()
89
90 # this holds the last `sched_switch` timestamp
91 last_ts = None
92
93 # iterate events
94 for event in col.events:
95 # keep only `sched_switch` events
96 if event.name != 'sched_switch':
97 continue
98
99 # keep only events which happened on CPU 0
100 if event['cpu_id'] != 0:
101 continue
102
103 # event timestamp
104 cur_ts = event.timestamp
105
106 if last_ts is None:
107 # we start here
108 last_ts = cur_ts
109
110 # previous task command (short) name
111 prev_comm = event['prev_comm']
112
113 # initialize entry in our dict if not yet done
114 if prev_comm not in exec_times:
115 exec_times[prev_comm] = 0
116
117 # compute previous command execution time
118 diff = cur_ts - last_ts
119
120 # update execution time of this command
121 exec_times[prev_comm] += diff
122
123 # update last timestamp
124 last_ts = cur_ts
125
126 # display top 10
a345f8e0 127 for name, ns in exec_times.most_common(5):
5e0cbfb0
PP
128 s = ns / 1000000000
129 print('{:20}{} s'.format(name, s))
130
131
132if __name__ == '__main__':
133 top5proc()
134~~~
135
136Save this script as `top5proc.py` and run it with Python 3, providing the
137path to an LTTng Linux kernel trace as the first argument:
138
139<pre class="term">
140python3 top5proc.py ~/lttng-sessions/my-session-.../kernel
141</pre>
142
143Make sure the path you provide is the directory containing actual trace
ba3b1994 144files (`channel0_0`, `metadata`, and the rest): the `babeltrace` utility
1c3359df 145recurses directories, but the Python bindings do not.
5e0cbfb0
PP
146
147Here's an example of output:
148
149~~~ text
150swapper/0 48.607245889 s
151chromium 7.192738188 s
152pavucontrol 0.709894415 s
153Compositor 0.660867933 s
154Xorg.bin 0.616753786 s
155~~~
156
157Note that `swapper/0` is the "idle" process of CPU 0 on Linux; since we
158weren't using the CPU that much when tracing, its first position in the list
159makes sense.
This page took 0.028863 seconds and 4 git commands to generate.