• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Perfetto <-> Ftrace interoperability
2
3*** note
4**This doc is WIP**, stay tuned.
5<!-- TODO(primiano): write ftrace doc. -->
6***
7
8This doc should:
9- Describe the ftrace trace_pipe_raw -> protobuf translation.
10- Describe how we deal with kernel ABI (in)stability and ftrace fields changing
11  over kernel versions (we process `event/**/format files on-device`).
12- Describe how to generate ftrace protos (`tools/pull_ftrace_format_files.py`,
13  `tools/udate_protos.py`)
14- Describe how session multiplexing works.
15- Describe the page-by-page scheduling algorithm that uses vmsplice()
16
17Code lives in [/src/ftrace_reader](/src/ftrace_reader/).
18
19From https://android-review.googlesource.com/c/platform/external/perfetto/+/603793/
20```
21
22  main thread                           [drain] [unblock]
23                                        /:              |
24                            post .-----' :              |
25                                /        :              v
26  worker #0  [splice ...] [wakeup] [block ............] [splice]
27                                         :
28  worker #1  [splice ...]     [wakeup] [block ........] [splice]
29                                         :
30  worker #2  [splice ..........................................]
31                                         :
32                                         :
33                                    drain period (100ms)
34
35In other words, the splice(2) system call is used to move data from
36the raw kernel ftrace pipe into an intermediate pipe at a page
37granularity. This call allows every per-cpu worker to sleep until there
38is at least one page of data available.
39
40When a worker wakes up, it will attempt to move as many pages as
41possible to its staging pipe (up to 64K, depending on the
42system's pipe buffer size) in a non-blocking way. After this, it
43will notify the main thread that data is available. This notification
44will block the calling worker until the main thread has drained the
45data.
46
47When at least one worker has woken up, we schedule a drain operation
48on the main thread for the next drain period (every 100ms by default).
49The drain operation parses ftrace data from the staging pipes of
50every worker having pending data. After this, each waiting worker is
51allowed to issue another call to splice(), restarting the cycle.
52```
53