• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Running perfetto in detached mode
2
3This document describes the `--detach` and `--attach` advanced operating modes
4of the `perfetto` cmdline client.
5
6WARNING: The use of `--detach` and `--attach` is highly discouraged because of
7the risk of leaking tracing sessions and accidentally leaving tracing on for
8arbitrarily long periods of time.
9
10TIP: If what you are looking for is just a way to grab a trace in background
11(e.g., while the USB cable / adb is disconnected) from the adb shell simply
12use `--background`.
13
14## Use case
15
16By default the tracing service `traced` keeps the lifetime of a tracing session
17attached to the lifetime of the `perfetto` cmdline client that started it.
18This means that a `killall perfetto` or `kill $PID_OF_PERFETTO` is sufficient
19to guarantee that the tracing session is stopped.
20
21There are rare occasions when this is undesirable; for example, this mode of
22operation was designed for the Traceur app (on-device tracing UI for Android).
23
24When required by the user, Traceur needs to enable tracing in the background,
25possibly for very long periods of time. Because Traceur is not a persistent service (and even if it was, it could be
26still low-memory-killed), it cannot just use `--background`; this is
27because the Android framework kills any other process in the same process group
28when tearing down an app/service, and this would including killing forked
29`perfetto` client obtained via `--background`.
30
31## Operation
32
33`--detach=key` decouples the lifetime of the cmdline client from the lifetime
34of the tracing session.
35
36The `key` argument is an arbitrary string passed by the client to later
37re-identify the session using `--attach=key`.
38
39Once detached, the cmdline client will exit (without forking any bg process) and
40the `traced` service will keep the tracing session alive. Because of the exit,
41a client that wants to use `--detach` needs to set the
42[`write_into_file`](config.md#long-traces) option in the trace config, which
43transfers the responsibility of writing the output trace file to the
44service (see the [examples](#examples) section).
45
46A detached session will run until either:
47
48- The session is later re-attached and stopped.
49- The time limit specified by the `duration_ms` argument in the trace config
50  is reached.
51
52`--attach=key` re-couples the lifetime of a new cmdline client invocation with
53an existing tracing session identified by `key`.
54For security reasons the service allows a client to re-attach to a tracing
55session only if the Unix UID of the re-attaching client matches the UID of the
56client that originally started the session and detached.
57
58Overall `--attach=key` makes the `perfetto` cmdline client behave as if it was
59never detached. This means that:
60
61- sending a `SIGKILL` (or Ctrl-C) to the client will gracefully stop the tracing
62  session.
63- If the `duration_ms` time limit is hit, the client will be informed by the
64  service and exit soon after.
65
66When re-attaching it is possible to also specify a further `--stop` argument.
67`--stop` will gracefully terminate the tracing session immediately after
68re-attaching (This is to avoid a race where SIGKILL is sent too early, before
69the client gets a chance to attach or even register the signal handler).
70
71No other cmdline argument other than `--stop` can be passed when using
72`--attach`.
73
74`--is_detached=key` can be used to check whether a detached session is running.
75The cmdline client will return quickly after the invocation with the following
76exit code:
77
78- 0 if the session identified by `key` exists and can be re-attached.
79- 1 in case of a general error (e.g. wrong cmdline, cannot reach the service).
80- 2 if no detached session with the given `key` is found.
81
82## Examples
83
84### Capturing a long trace in detached mode
85
86```bash
87echo '
88write_into_file: true
89# Long tracing mode, periodically flush the trace buffer into the trace file.
90file_write_period_ms: 5000
91
92buffers {
93  # This buffer needs to be big enough just to hold data between two consecutive
94  # |file_write_period|s (5s in this examples).
95  size_kb: 16384
96}
97
98data_sources {
99  config {
100    name: "linux.ftrace"
101    ftrace_config {
102      ftrace_events: "sched_switch"
103    }
104  }
105}
106' | perfetto -c - --txt --detach=session1 -o /data/misc/perfetto-traces/trace
107
108sleep 60
109
110perfetto --attach=session1 --stop
111# At this point the trace file is fully flushed into
112# /data/misc/perfetto-traces/trace.
113```
114
115### Start in detached ring-buffer mode. Later stop and save the ring buffer
116
117```bash
118echo '
119write_into_file: true
120
121# Specify an arbitrarily long flush period. Practically this means: never flush
122# unless trace is stopped.
123# TODO(primiano): an explicit no_periodic_flushes argument would be nicer. Maybe
124# we could repurpose the 0 value?
125file_write_period_ms: 1000000000
126
127buffers {
128  # This will be the size of the final trace.
129  size_kb: 16384
130}
131
132data_sources {
133  config {
134    name: "linux.ftrace"
135    ftrace_config {
136      ftrace_events: "sched_switch"
137    }
138  }
139}
140' | perfetto -c - --txt --detach=session2 -o /data/misc/perfetto-traces/trace
141
142# Wait for user input, or some critical event to happen.
143
144perfetto --attach=session2 --stop
145
146# At this point the trace file is saved into
147# /data/misc/perfetto-traces/trace.
148```
149
150### Start tracing with a time limit. Later re-attach and wait for the end
151
152```bash
153echo '
154duration_ms: 10000
155write_into_file: true
156
157buffers {
158  size_kb: 16384
159}
160
161data_sources {
162  config {
163    name: "linux.ftrace"
164    ftrace_config {
165      ftrace_events: "sched_switch"
166    }
167  }
168}
169' | perfetto -c - --txt --detach=session3 -o /data/misc/perfetto-traces/trace
170
171sleep 3
172perfetto --attach=session3
173# The cmdline client will stay up for 7 more seconds and then terminate.
174```
175