xref: /aosp_15_r20/external/OpenCSD/decoder/tests/auto-fdo/autofdo.md (revision 02ca8ccacfba7e0df68f3332a95f3180334d6649)
1*02ca8ccaSAndroid Build Coastguard WorkerAutoFDO and ARM Trace   {#AutoFDO}
2*02ca8ccaSAndroid Build Coastguard Worker=====================
3*02ca8ccaSAndroid Build Coastguard Worker
4*02ca8ccaSAndroid Build Coastguard Worker@brief Using CoreSight trace and perf with OpenCSD for AutoFDO.
5*02ca8ccaSAndroid Build Coastguard Worker
6*02ca8ccaSAndroid Build Coastguard Worker## Introduction
7*02ca8ccaSAndroid Build Coastguard Worker
8*02ca8ccaSAndroid Build Coastguard WorkerFeedback directed optimization (FDO, also know as profile guided
9*02ca8ccaSAndroid Build Coastguard Workeroptimization - PGO) uses a profile of a program's execution to guide the
10*02ca8ccaSAndroid Build Coastguard Workeroptmizations performed by the compiler.  Traditionally, this involves
11*02ca8ccaSAndroid Build Coastguard Workerbuilding an instrumented version of the program, which records a profile of
12*02ca8ccaSAndroid Build Coastguard Workerexecution as it runs.  The instrumentation adds significant runtime
13*02ca8ccaSAndroid Build Coastguard Workeroverhead, possibly changing the behaviour of the program and it may not be
14*02ca8ccaSAndroid Build Coastguard Workerpossible to run the instrumented program in a production environment
15*02ca8ccaSAndroid Build Coastguard Worker(e.g. where performance criteria must be met).
16*02ca8ccaSAndroid Build Coastguard Worker
17*02ca8ccaSAndroid Build Coastguard WorkerAutoFDO uses facilities in the hardware to sample the behaviour of the
18*02ca8ccaSAndroid Build Coastguard Workerprogram in the production environment and generate the execution profile.
19*02ca8ccaSAndroid Build Coastguard WorkerAn improved profile can be obtained by including the branch history
20*02ca8ccaSAndroid Build Coastguard Worker(i.e. a record of the last branches taken) when generating an instruction
21*02ca8ccaSAndroid Build Coastguard Workersamples.  On Arm systems, the ETM can be used to generate such records.
22*02ca8ccaSAndroid Build Coastguard Worker
23*02ca8ccaSAndroid Build Coastguard WorkerThe process can be broken down into the following steps:
24*02ca8ccaSAndroid Build Coastguard Worker
25*02ca8ccaSAndroid Build Coastguard Worker* Record execution trace of the program
26*02ca8ccaSAndroid Build Coastguard Worker* Convert the execution trace to instruction samples with branch histories
27*02ca8ccaSAndroid Build Coastguard Worker* Convert the instruction samples to source level profiles
28*02ca8ccaSAndroid Build Coastguard Worker* Use the source level profile with the compiler
29*02ca8ccaSAndroid Build Coastguard Worker
30*02ca8ccaSAndroid Build Coastguard WorkerThis article describes how to enable ETM trace on Arm targets running Linux
31*02ca8ccaSAndroid Build Coastguard Workerand use the ETM trace to generate AutoFDO profiles and compile an optimized
32*02ca8ccaSAndroid Build Coastguard Workerprogram.
33*02ca8ccaSAndroid Build Coastguard Worker
34*02ca8ccaSAndroid Build Coastguard Worker
35*02ca8ccaSAndroid Build Coastguard Worker## Execution trace on Arm targets
36*02ca8ccaSAndroid Build Coastguard Worker
37*02ca8ccaSAndroid Build Coastguard WorkerDebug and trace of Arm targets is provided by CoreSight.  This consists of
38*02ca8ccaSAndroid Build Coastguard Workera set of components that allow access to debug logic, record (trace) the
39*02ca8ccaSAndroid Build Coastguard Workerexecution of a processor and route this data through the system, collecting
40*02ca8ccaSAndroid Build Coastguard Workerit into a store.
41*02ca8ccaSAndroid Build Coastguard Worker
42*02ca8ccaSAndroid Build Coastguard WorkerTo record the execution of a processor, we require the following
43*02ca8ccaSAndroid Build Coastguard Workercomponents:
44*02ca8ccaSAndroid Build Coastguard Worker
45*02ca8ccaSAndroid Build Coastguard Worker* A trace source.  The core contains a trace unit, called an ETM that emits
46*02ca8ccaSAndroid Build Coastguard Worker  data describing the instructions executed by the core.
47*02ca8ccaSAndroid Build Coastguard Worker* Trace links.  The trace data generated by the ETM must be moved through
48*02ca8ccaSAndroid Build Coastguard Worker  the system to the component that collects the data (sink).  Links
49*02ca8ccaSAndroid Build Coastguard Worker  include:
50*02ca8ccaSAndroid Build Coastguard Worker    * Funnels: merge multiple streams of data
51*02ca8ccaSAndroid Build Coastguard Worker    * FIFOs: buffer data to smooth out bursts
52*02ca8ccaSAndroid Build Coastguard Worker    * Replicators: send a stream of data to multiple components
53*02ca8ccaSAndroid Build Coastguard Worker* Sinks.  These receive the trace data and store it or send it to an
54*02ca8ccaSAndroid Build Coastguard Worker  external device:
55*02ca8ccaSAndroid Build Coastguard Worker    * ETB: A small circular buffer (64-128 kilobytes) that stores the most
56*02ca8ccaSAndroid Build Coastguard Worker      recent data
57*02ca8ccaSAndroid Build Coastguard Worker    * ETR: A larger (several megabytes) buffer that uses system RAM to
58*02ca8ccaSAndroid Build Coastguard Worker      store data
59*02ca8ccaSAndroid Build Coastguard Worker    * TPIU: Sends data to an off-chip capture device (e.g. Arm DSTREAM)
60*02ca8ccaSAndroid Build Coastguard Worker
61*02ca8ccaSAndroid Build Coastguard WorkerEach Arm SoC design may have a different layout (topology) of components.
62*02ca8ccaSAndroid Build Coastguard WorkerThis topology is described to the OS drivers by the platform's devicetree
63*02ca8ccaSAndroid Build Coastguard Workeror (in future) ACPI firmware.
64*02ca8ccaSAndroid Build Coastguard Worker
65*02ca8ccaSAndroid Build Coastguard WorkerFor application profiling, we need to store several megabytes of data
66*02ca8ccaSAndroid Build Coastguard Workerwithin the system, so will use ETR with the capture tool (perf)
67*02ca8ccaSAndroid Build Coastguard Workerperiodically draining the buffer to a file.
68*02ca8ccaSAndroid Build Coastguard Worker
69*02ca8ccaSAndroid Build Coastguard WorkerEven though we have a large capture buffer, the ETM can still generate a
70*02ca8ccaSAndroid Build Coastguard Workerlot of data very quickly - typically an ETM will generate ~1 bit of data
71*02ca8ccaSAndroid Build Coastguard Workerper instruction (depending on the workload), which results in 256Mbytes per
72*02ca8ccaSAndroid Build Coastguard Workersecond for a core running at 2GHz.  This leads to problems storing and
73*02ca8ccaSAndroid Build Coastguard Workerdecoding such large volumes of data.  AutoFDO uses samples of program
74*02ca8ccaSAndroid Build Coastguard Workerexecution, so we can avoid this problem by using the ETM's features to
75*02ca8ccaSAndroid Build Coastguard Workeronly record small slices of execution - e.g. collect ~5000 cycles of data
76*02ca8ccaSAndroid Build Coastguard Workerevery 50M cycles.  This reduces the data rate to a manageable level - a few
77*02ca8ccaSAndroid Build Coastguard Workermegabytes per minute.  This technique is known as 'strobing'.
78*02ca8ccaSAndroid Build Coastguard Worker
79*02ca8ccaSAndroid Build Coastguard Worker
80*02ca8ccaSAndroid Build Coastguard Worker## Enabling trace
81*02ca8ccaSAndroid Build Coastguard Worker
82*02ca8ccaSAndroid Build Coastguard Worker### Driver support
83*02ca8ccaSAndroid Build Coastguard Worker
84*02ca8ccaSAndroid Build Coastguard WorkerTo collect ETM trace, the CoreSight drivers must be included in the
85*02ca8ccaSAndroid Build Coastguard Workerkernel.  Some of the driver support is not yet included in the mainline
86*02ca8ccaSAndroid Build Coastguard Workerkernel and many targets are using older kernels.  To enable CoreSight trace
87*02ca8ccaSAndroid Build Coastguard Workeron these targets, Arm have provided backports of the latest CoreSight
88*02ca8ccaSAndroid Build Coastguard Workerdrivers and ETM strobing patch at:
89*02ca8ccaSAndroid Build Coastguard Worker
90*02ca8ccaSAndroid Build Coastguard Worker  <https://gitlab.arm.com/linux-arm/linux-coresight-backports>
91*02ca8ccaSAndroid Build Coastguard Worker
92*02ca8ccaSAndroid Build Coastguard WorkerThis repository can be cloned with:
93*02ca8ccaSAndroid Build Coastguard Worker
94*02ca8ccaSAndroid Build Coastguard Worker```
95*02ca8ccaSAndroid Build Coastguard Workergit clone https://git.gitlab.arm.com/linux-arm/linux-coresight-backports.git
96*02ca8ccaSAndroid Build Coastguard Worker```
97*02ca8ccaSAndroid Build Coastguard Worker
98*02ca8ccaSAndroid Build Coastguard WorkerYou can include these backports in your kernel by either merging the
99*02ca8ccaSAndroid Build Coastguard Workerappropriate branch using git or generating patches (using `git
100*02ca8ccaSAndroid Build Coastguard Workerformat-patch`).
101*02ca8ccaSAndroid Build Coastguard Worker
102*02ca8ccaSAndroid Build Coastguard WorkerFor 5.x based kernel onwards, the only patch which needs to be applied is the one enabling strobing - etm4x: `Enable strobing of ETM`.
103*02ca8ccaSAndroid Build Coastguard Worker
104*02ca8ccaSAndroid Build Coastguard WorkerFor 4.9 based kernels, use the `coresight-4.9-etr-etm_strobe` branch:
105*02ca8ccaSAndroid Build Coastguard Worker
106*02ca8ccaSAndroid Build Coastguard Worker```
107*02ca8ccaSAndroid Build Coastguard Workergit merge coresight-4.9-etr-etm_strobe
108*02ca8ccaSAndroid Build Coastguard Worker```
109*02ca8ccaSAndroid Build Coastguard Worker
110*02ca8ccaSAndroid Build Coastguard Workeror
111*02ca8ccaSAndroid Build Coastguard Worker
112*02ca8ccaSAndroid Build Coastguard Worker```
113*02ca8ccaSAndroid Build Coastguard Workergit format-patch --output-directory /output/dir v4.9..coresight-4.9-etr-etm_strobe
114*02ca8ccaSAndroid Build Coastguard Workercd my_kernel
115*02ca8ccaSAndroid Build Coastguard Workergit am /output/dir/*.patch # or patch -p1 /output/dir/*.patch if not using git
116*02ca8ccaSAndroid Build Coastguard Worker```
117*02ca8ccaSAndroid Build Coastguard Worker
118*02ca8ccaSAndroid Build Coastguard WorkerFor 4.14 based kernels, use the `coresight-4.14-etm_strobe` branch:
119*02ca8ccaSAndroid Build Coastguard Worker
120*02ca8ccaSAndroid Build Coastguard Worker```
121*02ca8ccaSAndroid Build Coastguard Workergit merge coresight-4.14-etm_strobe
122*02ca8ccaSAndroid Build Coastguard Worker```
123*02ca8ccaSAndroid Build Coastguard Worker
124*02ca8ccaSAndroid Build Coastguard Workeror
125*02ca8ccaSAndroid Build Coastguard Worker
126*02ca8ccaSAndroid Build Coastguard Worker```
127*02ca8ccaSAndroid Build Coastguard Workergit format-patch --output-directory /output/dir v4.14..coresight-4.14-etm_strobe
128*02ca8ccaSAndroid Build Coastguard Workercd my_kernel
129*02ca8ccaSAndroid Build Coastguard Workergit am /output/dir/*.patch # or patch -p1 /output/dir/*.patch if not using git
130*02ca8ccaSAndroid Build Coastguard Worker```
131*02ca8ccaSAndroid Build Coastguard Worker
132*02ca8ccaSAndroid Build Coastguard WorkerThe CoreSight trace drivers must also be enabled in the kernel
133*02ca8ccaSAndroid Build Coastguard Workerconfiguration.  This can be done using the configuration menu (`make
134*02ca8ccaSAndroid Build Coastguard Workermenuconfig`), selecting `Kernel hacking` / `arm64 Debugging`  /`CoreSight Tracing Support` and
135*02ca8ccaSAndroid Build Coastguard Workerenabling all options, or by setting the following in the configuration
136*02ca8ccaSAndroid Build Coastguard Workerfile:
137*02ca8ccaSAndroid Build Coastguard Worker
138*02ca8ccaSAndroid Build Coastguard Worker```
139*02ca8ccaSAndroid Build Coastguard WorkerCONFIG_CORESIGHT=y
140*02ca8ccaSAndroid Build Coastguard WorkerCONFIG_CORESIGHT_LINK_AND_SINK_TMC=y
141*02ca8ccaSAndroid Build Coastguard WorkerCONFIG_CORESIGHT_SINK_TPIU=y
142*02ca8ccaSAndroid Build Coastguard WorkerCONFIG_CORESIGHT_SOURCE_ETM4X=y
143*02ca8ccaSAndroid Build Coastguard WorkerCONFIG_CORESIGHT_DYNAMIC_REPLICATOR=y
144*02ca8ccaSAndroid Build Coastguard WorkerCONFIG_CORESIGHT_STM=y
145*02ca8ccaSAndroid Build Coastguard WorkerCONFIG_CORESIGHT_CATU=y
146*02ca8ccaSAndroid Build Coastguard Worker```
147*02ca8ccaSAndroid Build Coastguard Worker
148*02ca8ccaSAndroid Build Coastguard WorkerCompile the kernel for your target in the usual way, e.g.
149*02ca8ccaSAndroid Build Coastguard Worker
150*02ca8ccaSAndroid Build Coastguard Worker```
151*02ca8ccaSAndroid Build Coastguard Workermake ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
152*02ca8ccaSAndroid Build Coastguard Worker```
153*02ca8ccaSAndroid Build Coastguard Worker
154*02ca8ccaSAndroid Build Coastguard WorkerEach target may have a different layout of CoreSight components.  To
155*02ca8ccaSAndroid Build Coastguard Workercollect trace into a sink, the kernel drivers need to know which other
156*02ca8ccaSAndroid Build Coastguard Workerdevices need to be configured to route data from the source to the sink.
157*02ca8ccaSAndroid Build Coastguard WorkerThis is described in the devicetree (and in future, the ACPI tables).  The
158*02ca8ccaSAndroid Build Coastguard Workerdevice tree will define which CoreSight devices are present in the system,
159*02ca8ccaSAndroid Build Coastguard Workerwhere they are located and how they are connected together.  The devicetree
160*02ca8ccaSAndroid Build Coastguard Workerfor some platforms includes a description of the platform's CoreSight
161*02ca8ccaSAndroid Build Coastguard Workercomponents, but in other cases you may have to ask the platform/SoC vendor
162*02ca8ccaSAndroid Build Coastguard Workerto supply it or create it yourself (see Appendix: Describing CoreSight in
163*02ca8ccaSAndroid Build Coastguard WorkerDevicetree).
164*02ca8ccaSAndroid Build Coastguard Worker
165*02ca8ccaSAndroid Build Coastguard WorkerOnce the target has been booted with the devicetree describing the
166*02ca8ccaSAndroid Build Coastguard WorkerCoreSight devices, you should find the devices in sysfs:
167*02ca8ccaSAndroid Build Coastguard Worker
168*02ca8ccaSAndroid Build Coastguard Worker```
169*02ca8ccaSAndroid Build Coastguard Worker# ls /sys/bus/coresight/devices/
170*02ca8ccaSAndroid Build Coastguard Workeretm0  etm2  etm4  etm6  funnel0  funnel2  funnel4      stm0      tmc_etr0
171*02ca8ccaSAndroid Build Coastguard Workeretm1  etm3  etm5  etm7  funnel1  funnel3  replicator0  tmc_etf0
172*02ca8ccaSAndroid Build Coastguard Worker```
173*02ca8ccaSAndroid Build Coastguard Worker
174*02ca8ccaSAndroid Build Coastguard WorkerThe naming convention for etm devices can be different according to the kernel version you're using.
175*02ca8ccaSAndroid Build Coastguard WorkerFor more information about the naming scheme, please check out the [Linux Kernel Documentation](https://www.kernel.org/doc/html/latest/trace/coresight/coresight.html#device-naming-scheme)
176*02ca8ccaSAndroid Build Coastguard Worker
177*02ca8ccaSAndroid Build Coastguard WorkerIf `/sys/bus/coresight/devices/` is empty, you may want to check out your Kernel configuration to make sure your .config file is including CoreSight dependencies, such as the clock.
178*02ca8ccaSAndroid Build Coastguard Worker
179*02ca8ccaSAndroid Build Coastguard Worker### Perf tools
180*02ca8ccaSAndroid Build Coastguard Worker
181*02ca8ccaSAndroid Build Coastguard WorkerThe perf tool is used to capture execution trace, configuring the trace
182*02ca8ccaSAndroid Build Coastguard Workersources to generate trace, routing the data to the sink and collecting the
183*02ca8ccaSAndroid Build Coastguard Workerdata from the sink.
184*02ca8ccaSAndroid Build Coastguard Worker
185*02ca8ccaSAndroid Build Coastguard WorkerArm recommends to use the perf version corresponding to the kernel running
186*02ca8ccaSAndroid Build Coastguard Workeron the target.  This can be built from the same kernel sources with
187*02ca8ccaSAndroid Build Coastguard Worker
188*02ca8ccaSAndroid Build Coastguard Worker```
189*02ca8ccaSAndroid Build Coastguard Workermake -C tools/perf CORESIGHT=1 VF=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
190*02ca8ccaSAndroid Build Coastguard Worker```
191*02ca8ccaSAndroid Build Coastguard Worker
192*02ca8ccaSAndroid Build Coastguard WorkerWhen specifying CORESIGHT=1, perf will be built using the installed OpenCSD library.
193*02ca8ccaSAndroid Build Coastguard WorkerIf you are cross compiling, then additional setup is required to ensure the build process links against the correct version of the library.
194*02ca8ccaSAndroid Build Coastguard Worker
195*02ca8ccaSAndroid Build Coastguard WorkerIf the post-processing (`perf inject`) of the captured data is not being
196*02ca8ccaSAndroid Build Coastguard Workerdone on the target, then the OpenCSD library is not required for this build
197*02ca8ccaSAndroid Build Coastguard Workerof perf.
198*02ca8ccaSAndroid Build Coastguard Worker
199*02ca8ccaSAndroid Build Coastguard WorkerTrace is captured by collecting the `cs_etm` event from perf.  The sink
200*02ca8ccaSAndroid Build Coastguard Workerto collect data into is specified as a parameter of this event.  Trace can
201*02ca8ccaSAndroid Build Coastguard Workeralso be restricted to user space or kernel space with 'u' or 'k'
202*02ca8ccaSAndroid Build Coastguard Workerparameters.  For example:
203*02ca8ccaSAndroid Build Coastguard Worker
204*02ca8ccaSAndroid Build Coastguard Worker```
205*02ca8ccaSAndroid Build Coastguard Workerperf record -e cs_etm/@tmc_etr0/u --per-thread -- /bin/ls
206*02ca8ccaSAndroid Build Coastguard Worker```
207*02ca8ccaSAndroid Build Coastguard Worker
208*02ca8ccaSAndroid Build Coastguard WorkerWill record the userspace execution of '/bin/ls' using tmc_etr0 as sink.
209*02ca8ccaSAndroid Build Coastguard Worker
210*02ca8ccaSAndroid Build Coastguard Worker## Capturing modes
211*02ca8ccaSAndroid Build Coastguard Worker
212*02ca8ccaSAndroid Build Coastguard WorkerYou can trace a single-threaded program in two different ways:
213*02ca8ccaSAndroid Build Coastguard Worker
214*02ca8ccaSAndroid Build Coastguard Worker1. By specifying `--per-thread`, and in this case the CoreSight subsystem will
215*02ca8ccaSAndroid Build Coastguard Workerrecord only a trace relative to the given program.
216*02ca8ccaSAndroid Build Coastguard Worker
217*02ca8ccaSAndroid Build Coastguard Worker2. By NOT specifying `--per-thread`, and in this case CPU-wide tracing will
218*02ca8ccaSAndroid Build Coastguard Workerbe enabled. In this scenario the trace will contain both the target program trace
219*02ca8ccaSAndroid Build Coastguard Workerand other workloads that were executing on the same CPU
220*02ca8ccaSAndroid Build Coastguard Worker
221*02ca8ccaSAndroid Build Coastguard Worker
222*02ca8ccaSAndroid Build Coastguard Worker
223*02ca8ccaSAndroid Build Coastguard Worker## Processing trace and profiles
224*02ca8ccaSAndroid Build Coastguard Worker
225*02ca8ccaSAndroid Build Coastguard Workerperf is also used to convert the execution trace an instruction profile.
226*02ca8ccaSAndroid Build Coastguard WorkerThis requires a different build of perf, using the version of perf from
227*02ca8ccaSAndroid Build Coastguard WorkerLinux v4.17 or later, as the trace processing code isn't included in the
228*02ca8ccaSAndroid Build Coastguard Workerdriver backports.  Trace decode is provided by the OpenCSD library
229*02ca8ccaSAndroid Build Coastguard Worker(<https://github.com/Linaro/OpenCSD>), v0.9.1 or later.  This is packaged
230*02ca8ccaSAndroid Build Coastguard Workerfor debian testing (install the libopencsd0, libopencsd-dev packages) or
231*02ca8ccaSAndroid Build Coastguard Workercan be compiled from source and installed.
232*02ca8ccaSAndroid Build Coastguard Worker
233*02ca8ccaSAndroid Build Coastguard WorkerThe autoFDO tool <https://github.com/google/autofdo> is used to convert the
234*02ca8ccaSAndroid Build Coastguard Workerinstruction profiles to source profiles for the GCC and clang/llvm
235*02ca8ccaSAndroid Build Coastguard Workercompilers.
236*02ca8ccaSAndroid Build Coastguard Worker
237*02ca8ccaSAndroid Build Coastguard Worker
238*02ca8ccaSAndroid Build Coastguard Worker## Recording and profiling
239*02ca8ccaSAndroid Build Coastguard Worker
240*02ca8ccaSAndroid Build Coastguard WorkerOnce trace collection using perf is working, we can now use it to profile
241*02ca8ccaSAndroid Build Coastguard Workeran application.
242*02ca8ccaSAndroid Build Coastguard Worker
243*02ca8ccaSAndroid Build Coastguard WorkerThe application must be compiled to include sufficient debug information to
244*02ca8ccaSAndroid Build Coastguard Workermap instructions back to source lines.  For GCC, use the `-g1` or `-gmlt`
245*02ca8ccaSAndroid Build Coastguard Workeroptions.  For clang/llvm, also add the `-fdebug-info-for-profiling` option.
246*02ca8ccaSAndroid Build Coastguard Worker
247*02ca8ccaSAndroid Build Coastguard Workerperf identifies the active program or library using the build identifier
248*02ca8ccaSAndroid Build Coastguard Workerstored in the elf file.  This should be added at link time with the compiler
249*02ca8ccaSAndroid Build Coastguard Workerflag `-Wl,--build-id=sha1`.
250*02ca8ccaSAndroid Build Coastguard Worker
251*02ca8ccaSAndroid Build Coastguard WorkerThe next step is to record the execution trace of the application using the
252*02ca8ccaSAndroid Build Coastguard Workerperf tool.  The ETM strobing should be configured before running the perf
253*02ca8ccaSAndroid Build Coastguard Workertool.  There are two parameters:
254*02ca8ccaSAndroid Build Coastguard Worker
255*02ca8ccaSAndroid Build Coastguard Worker  * window size: A number of CPU cycles (W)
256*02ca8ccaSAndroid Build Coastguard Worker  * period: Trace is enabled for W cycle every _period_ * W cycles.
257*02ca8ccaSAndroid Build Coastguard Worker
258*02ca8ccaSAndroid Build Coastguard WorkerFor example, a typical configuration is to use a window size of 5000 cycles
259*02ca8ccaSAndroid Build Coastguard Workerand a period of 10000 - this will collect 5000 cycles of trace every 50M
260*02ca8ccaSAndroid Build Coastguard Workercycles.  With these proof-of-concept patches, the strobe parameters are
261*02ca8ccaSAndroid Build Coastguard Workerconfigured via sysfs - each ETM will have `strobe_window` and
262*02ca8ccaSAndroid Build Coastguard Worker`strobe_period` parameters in `/sys/bus/coresight/devices/<sink>` and
263*02ca8ccaSAndroid Build Coastguard Workerthese values will have to be written to each (In a future version, this
264*02ca8ccaSAndroid Build Coastguard Workerwill be integrated into the drivers and perf tool).
265*02ca8ccaSAndroid Build Coastguard WorkerThe `set_strobing.sh` script in this directory [`<opencsd>/decoder/tests/auto-fdo`] automates this process.
266*02ca8ccaSAndroid Build Coastguard Worker
267*02ca8ccaSAndroid Build Coastguard WorkerTo collect trace from an application using ETM strobing, run:
268*02ca8ccaSAndroid Build Coastguard Worker
269*02ca8ccaSAndroid Build Coastguard Worker```
270*02ca8ccaSAndroid Build Coastguard Workersudo ./set_strobing.sh 5000 10000
271*02ca8ccaSAndroid Build Coastguard Workerperf record -e cs_etm/@tmc_etr0/u --per-thread -- <your app>"
272*02ca8ccaSAndroid Build Coastguard Worker```
273*02ca8ccaSAndroid Build Coastguard Worker
274*02ca8ccaSAndroid Build Coastguard WorkerThe raw trace can be examined using the `perf report` command:
275*02ca8ccaSAndroid Build Coastguard Worker
276*02ca8ccaSAndroid Build Coastguard Worker```
277*02ca8ccaSAndroid Build Coastguard Workerperf report -D -i perf.data --stdio
278*02ca8ccaSAndroid Build Coastguard Worker```
279*02ca8ccaSAndroid Build Coastguard Worker
280*02ca8ccaSAndroid Build Coastguard WorkerPerf needs to be built from your linux kernel version souce code repository against the OpenCSD library in order to be able to properly read ETM-gathered samples and post-process them.
281*02ca8ccaSAndroid Build Coastguard WorkerIf running `perf report` produces an error like:
282*02ca8ccaSAndroid Build Coastguard Worker
283*02ca8ccaSAndroid Build Coastguard Worker```
284*02ca8ccaSAndroid Build Coastguard Worker0x1f8 [0x268]: failed to process type: 70 [Operation not permitted]
285*02ca8ccaSAndroid Build Coastguard WorkerError:
286*02ca8ccaSAndroid Build Coastguard Workerfailed to process sample
287*02ca8ccaSAndroid Build Coastguard Worker```
288*02ca8ccaSAndroid Build Coastguard Workeror
289*02ca8ccaSAndroid Build Coastguard Worker
290*02ca8ccaSAndroid Build Coastguard Worker```
291*02ca8ccaSAndroid Build Coastguard Worker"file uses a more recent and unsupported ABI (8 bytes extra). incompatible file format".
292*02ca8ccaSAndroid Build Coastguard Worker```
293*02ca8ccaSAndroid Build Coastguard Worker
294*02ca8ccaSAndroid Build Coastguard WorkerYou are probably using a perf version which is not using this library: please make sure to install this project in your system by either compiling it from [Source Code]( <https://github.com/Linaro/OpenCSD>) from v0.9.1 or later and compile perf using this library.
295*02ca8ccaSAndroid Build Coastguard WorkerOtherwise, this project is packaged for debian (install the libopencsd0, libopencsd-dev packages).
296*02ca8ccaSAndroid Build Coastguard Worker
297*02ca8ccaSAndroid Build Coastguard Worker
298*02ca8ccaSAndroid Build Coastguard WorkerFor example:
299*02ca8ccaSAndroid Build Coastguard Worker
300*02ca8ccaSAndroid Build Coastguard Worker```
301*02ca8ccaSAndroid Build Coastguard Worker0x1d370 [0x30]: PERF_RECORD_AUXTRACE size: 0x2003c0  offset: 0  ref: 0x39ba881d145f8639  idx: 0  tid: 4551  cpu: -1
302*02ca8ccaSAndroid Build Coastguard Worker
303*02ca8ccaSAndroid Build Coastguard Worker. ... CoreSight ETM Trace data: size 2098112 bytes
304*02ca8ccaSAndroid Build Coastguard Worker        Idx:0; ID:12;   I_ASYNC : Alignment Synchronisation.
305*02ca8ccaSAndroid Build Coastguard Worker        Idx:12; ID:12;  I_TRACE_INFO : Trace Info.; INFO=0x0
306*02ca8ccaSAndroid Build Coastguard Worker        Idx:17; ID:12;  I_ADDR_L_64IS0 : Address, Long, 64 bit, IS0.; Addr=0xFFFF000008A4991C;
307*02ca8ccaSAndroid Build Coastguard Worker        Idx:48; ID:14;  I_ASYNC : Alignment Synchronisation.
308*02ca8ccaSAndroid Build Coastguard Worker        Idx:60; ID:14;  I_TRACE_INFO : Trace Info.; INFO=0x0
309*02ca8ccaSAndroid Build Coastguard Worker        Idx:65; ID:14;  I_ADDR_L_64IS0 : Address, Long, 64 bit, IS0.; Addr=0xFFFF000008A4991C;
310*02ca8ccaSAndroid Build Coastguard Worker        Idx:96; ID:14;  I_ASYNC : Alignment Synchronisation.
311*02ca8ccaSAndroid Build Coastguard Worker        Idx:108; ID:14; I_TRACE_INFO : Trace Info.; INFO=0x0
312*02ca8ccaSAndroid Build Coastguard Worker        Idx:113; ID:14; I_ADDR_L_64IS0 : Address, Long, 64 bit, IS0.; Addr=0xFFFF000008A4991C;
313*02ca8ccaSAndroid Build Coastguard Worker        Idx:122; ID:14; I_TRACE_ON : Trace On.
314*02ca8ccaSAndroid Build Coastguard Worker        Idx:123; ID:14; I_ADDR_CTXT_L_64IS0 : Address & Context, Long, 64 bit, IS0.; Addr=0x0000000000407B00; Ctxt: AArch64,EL0, NS;
315*02ca8ccaSAndroid Build Coastguard Worker        Idx:134; ID:14; I_ATOM_F3 : Atom format 3.; ENN
316*02ca8ccaSAndroid Build Coastguard Worker        Idx:135; ID:14; I_ATOM_F5 : Atom format 5.; NENEN
317*02ca8ccaSAndroid Build Coastguard Worker        Idx:136; ID:14; I_ATOM_F5 : Atom format 5.; ENENE
318*02ca8ccaSAndroid Build Coastguard Worker        Idx:137; ID:14; I_ATOM_F5 : Atom format 5.; NENEN
319*02ca8ccaSAndroid Build Coastguard Worker        Idx:138; ID:14; I_ATOM_F3 : Atom format 3.; ENN
320*02ca8ccaSAndroid Build Coastguard Worker        Idx:139; ID:14; I_ATOM_F3 : Atom format 3.; NNE
321*02ca8ccaSAndroid Build Coastguard Worker        Idx:140; ID:14; I_ATOM_F1 : Atom format 1.; E
322*02ca8ccaSAndroid Build Coastguard Worker.....
323*02ca8ccaSAndroid Build Coastguard Worker```
324*02ca8ccaSAndroid Build Coastguard Worker
325*02ca8ccaSAndroid Build Coastguard WorkerThe execution trace is then converted to an instruction profile using
326*02ca8ccaSAndroid Build Coastguard Workerthe perf build with trace decode support.  This may be done on a different
327*02ca8ccaSAndroid Build Coastguard Workermachine than that which collected the trace (e.g. when cross compiling for
328*02ca8ccaSAndroid Build Coastguard Workeran embedded target).  The `perf inject` command
329*02ca8ccaSAndroid Build Coastguard Workerdecodes the execution trace and generates periodic instruction samples,
330*02ca8ccaSAndroid Build Coastguard Workerwith branch histories:
331*02ca8ccaSAndroid Build Coastguard Worker
332*02ca8ccaSAndroid Build Coastguard Worker!! Careful: if you are using a device different than the one used to collect the profiling data,
333*02ca8ccaSAndroid Build Coastguard Workeryou'll need to run `perf buildid-cache` as described below.
334*02ca8ccaSAndroid Build Coastguard Worker```
335*02ca8ccaSAndroid Build Coastguard Workerperf inject -i perf.data -o inj.data --itrace=i100000il
336*02ca8ccaSAndroid Build Coastguard Worker```
337*02ca8ccaSAndroid Build Coastguard Worker
338*02ca8ccaSAndroid Build Coastguard WorkerThe `--itrace` option configures the instruction sample behaviour:
339*02ca8ccaSAndroid Build Coastguard Worker
340*02ca8ccaSAndroid Build Coastguard Worker* `i100000i` generates an instruction sample every 100000 instructions
341*02ca8ccaSAndroid Build Coastguard Worker  (only instruction count periods are currently supported, future versions
342*02ca8ccaSAndroid Build Coastguard Worker  may support time or cycle count periods)
343*02ca8ccaSAndroid Build Coastguard Worker* `l` includes the branch histories on each sample
344*02ca8ccaSAndroid Build Coastguard Worker* `b` generates a sample on each branch (not used here)
345*02ca8ccaSAndroid Build Coastguard Worker
346*02ca8ccaSAndroid Build Coastguard WorkerPerf requires the original program binaries to decode the execution trace.
347*02ca8ccaSAndroid Build Coastguard WorkerIf running the `inject` command on a different system than the trace was
348*02ca8ccaSAndroid Build Coastguard Workercaptured on, then the binary and any shared libraries must be added to
349*02ca8ccaSAndroid Build Coastguard Workerperf's cache with:
350*02ca8ccaSAndroid Build Coastguard Worker
351*02ca8ccaSAndroid Build Coastguard Worker```
352*02ca8ccaSAndroid Build Coastguard Workerperf buildid-cache -a /path/to/binary_or_library
353*02ca8ccaSAndroid Build Coastguard Worker```
354*02ca8ccaSAndroid Build Coastguard Worker
355*02ca8ccaSAndroid Build Coastguard Worker`perf report` can also be used to show the instruction samples:
356*02ca8ccaSAndroid Build Coastguard Worker
357*02ca8ccaSAndroid Build Coastguard Worker```
358*02ca8ccaSAndroid Build Coastguard Workerperf report -D -i inj.data --stdio
359*02ca8ccaSAndroid Build Coastguard Worker.......
360*02ca8ccaSAndroid Build Coastguard Worker0x1528 [0x630]: PERF_RECORD_SAMPLE(IP, 0x2): 4551/4551: 0x434b98 period: 3093 addr: 0
361*02ca8ccaSAndroid Build Coastguard Worker... branch stack: nr:64
362*02ca8ccaSAndroid Build Coastguard Worker.....  0: 0000000000434b58 -> 0000000000434b68 0 cycles  P   0
363*02ca8ccaSAndroid Build Coastguard Worker.....  1: 0000000000436a88 -> 0000000000434b4c 0 cycles  P   0
364*02ca8ccaSAndroid Build Coastguard Worker.....  2: 0000000000436a64 -> 0000000000436a78 0 cycles  P   0
365*02ca8ccaSAndroid Build Coastguard Worker.....  3: 00000000004369d0 -> 0000000000436a60 0 cycles  P   0
366*02ca8ccaSAndroid Build Coastguard Worker.....  4: 000000000043693c -> 00000000004369cc 0 cycles  P   0
367*02ca8ccaSAndroid Build Coastguard Worker.....  5: 00000000004368a8 -> 0000000000436928 0 cycles  P   0
368*02ca8ccaSAndroid Build Coastguard Worker.....  6: 000000000042d070 -> 00000000004368a8 0 cycles  P   0
369*02ca8ccaSAndroid Build Coastguard Worker.....  7: 000000000042d108 -> 000000000042d070 0 cycles  P   0
370*02ca8ccaSAndroid Build Coastguard Worker.......
371*02ca8ccaSAndroid Build Coastguard Worker..... 57: 0000000000448ee0 -> 0000000000448f24 0 cycles  P   0
372*02ca8ccaSAndroid Build Coastguard Worker..... 58: 0000000000448ea4 -> 0000000000448ebc 0 cycles  P   0
373*02ca8ccaSAndroid Build Coastguard Worker..... 59: 0000000000448e20 -> 0000000000448e94 0 cycles  P   0
374*02ca8ccaSAndroid Build Coastguard Worker..... 60: 0000000000448da8 -> 0000000000448ddc 0 cycles  P   0
375*02ca8ccaSAndroid Build Coastguard Worker..... 61: 00000000004486f4 -> 0000000000448da8 0 cycles  P   0
376*02ca8ccaSAndroid Build Coastguard Worker..... 62: 00000000004480fc -> 00000000004486d4 0 cycles  P   0
377*02ca8ccaSAndroid Build Coastguard Worker..... 63: 0000000000448658 -> 00000000004480ec 0 cycles  P   0
378*02ca8ccaSAndroid Build Coastguard Worker ... thread: program1:4551
379*02ca8ccaSAndroid Build Coastguard Worker ...... dso: /home/root/program1
380*02ca8ccaSAndroid Build Coastguard Worker.......
381*02ca8ccaSAndroid Build Coastguard Worker```
382*02ca8ccaSAndroid Build Coastguard Worker
383*02ca8ccaSAndroid Build Coastguard WorkerThe instruction samples produced by `perf inject` is then passed to the
384*02ca8ccaSAndroid Build Coastguard Workerautofdo tool to generate source level profiles for the compiler.  For
385*02ca8ccaSAndroid Build Coastguard Workerclang/LLVM:
386*02ca8ccaSAndroid Build Coastguard Worker
387*02ca8ccaSAndroid Build Coastguard Worker```
388*02ca8ccaSAndroid Build Coastguard Workercreate_llvm_prof -binary=/path/to/binary -profile=inj.data -out=program.llvmprof
389*02ca8ccaSAndroid Build Coastguard Worker```
390*02ca8ccaSAndroid Build Coastguard Worker
391*02ca8ccaSAndroid Build Coastguard WorkerAnd for GCC:
392*02ca8ccaSAndroid Build Coastguard Worker
393*02ca8ccaSAndroid Build Coastguard Worker```
394*02ca8ccaSAndroid Build Coastguard Workercreate_gcov -binary=/path/to/binary -profile=inj.data -gcov_version=1 -gcov=program.gcov
395*02ca8ccaSAndroid Build Coastguard Worker```
396*02ca8ccaSAndroid Build Coastguard Worker
397*02ca8ccaSAndroid Build Coastguard WorkerThe profiles can be viewed with:
398*02ca8ccaSAndroid Build Coastguard Worker
399*02ca8ccaSAndroid Build Coastguard Worker```
400*02ca8ccaSAndroid Build Coastguard Workerllvm-profdata show -sample program.llvmprof
401*02ca8ccaSAndroid Build Coastguard Worker```
402*02ca8ccaSAndroid Build Coastguard Worker
403*02ca8ccaSAndroid Build Coastguard WorkerOr, for GCC:
404*02ca8ccaSAndroid Build Coastguard Worker
405*02ca8ccaSAndroid Build Coastguard Worker```
406*02ca8ccaSAndroid Build Coastguard Workerdump_gcov -gcov_version=1 program.gcov
407*02ca8ccaSAndroid Build Coastguard Worker```
408*02ca8ccaSAndroid Build Coastguard Worker
409*02ca8ccaSAndroid Build Coastguard Worker## Using profile in the compiler
410*02ca8ccaSAndroid Build Coastguard Worker
411*02ca8ccaSAndroid Build Coastguard WorkerThe profile produced by the above steps can then be passed to the compiler
412*02ca8ccaSAndroid Build Coastguard Workerto optimize the next build of the program.
413*02ca8ccaSAndroid Build Coastguard Worker
414*02ca8ccaSAndroid Build Coastguard WorkerFor GCC, use the `-fauto-profile` option:
415*02ca8ccaSAndroid Build Coastguard Worker
416*02ca8ccaSAndroid Build Coastguard Worker```
417*02ca8ccaSAndroid Build Coastguard Workergcc -O2 -fauto-profile=program.gcov -o program program.c
418*02ca8ccaSAndroid Build Coastguard Worker```
419*02ca8ccaSAndroid Build Coastguard Worker
420*02ca8ccaSAndroid Build Coastguard WorkerFor Clang, use the `-fprofile-sample-use` option:
421*02ca8ccaSAndroid Build Coastguard Worker
422*02ca8ccaSAndroid Build Coastguard Worker```
423*02ca8ccaSAndroid Build Coastguard Workerclang -O2 -fprofile-sample-use=program.llvmprof -o program program.c
424*02ca8ccaSAndroid Build Coastguard Worker```
425*02ca8ccaSAndroid Build Coastguard Worker
426*02ca8ccaSAndroid Build Coastguard Worker
427*02ca8ccaSAndroid Build Coastguard Worker## Summary
428*02ca8ccaSAndroid Build Coastguard Worker
429*02ca8ccaSAndroid Build Coastguard WorkerThe basic commands to run an application and create a compiler profile are:
430*02ca8ccaSAndroid Build Coastguard Worker
431*02ca8ccaSAndroid Build Coastguard Worker```
432*02ca8ccaSAndroid Build Coastguard Workersudo ./set_strobing.sh 5000 10000
433*02ca8ccaSAndroid Build Coastguard Workerperf record -e cs_etm/@tmc_etr0/u --per-thread -- <your app>"
434*02ca8ccaSAndroid Build Coastguard Workerperf inject -i perf.data -o inj.data --itrace=i100000il
435*02ca8ccaSAndroid Build Coastguard Workercreate_llvm_prof -binary=/path/to/binary -profile=inj.data -out=program.llvmprof
436*02ca8ccaSAndroid Build Coastguard Workerclang -O2 -fprofile-sample-use=program.llvmprof -o program program.c
437*02ca8ccaSAndroid Build Coastguard Worker```
438*02ca8ccaSAndroid Build Coastguard Worker
439*02ca8ccaSAndroid Build Coastguard WorkerUse `create_gcov` for gcc.
440*02ca8ccaSAndroid Build Coastguard Worker
441*02ca8ccaSAndroid Build Coastguard Worker## High Level Summary for recoding on Arm board and decoding on different host
442*02ca8ccaSAndroid Build Coastguard Worker
443*02ca8ccaSAndroid Build Coastguard Worker1. (on Arm board)
444*02ca8ccaSAndroid Build Coastguard Worker
445*02ca8ccaSAndroid Build Coastguard Worker        sudo ./set_strobing.sh 5000 10000
446*02ca8ccaSAndroid Build Coastguard Worker        perf record -e cs_etm/@tmc_etr0/u --per-thread -- <your app>.
447*02ca8ccaSAndroid Build Coastguard Worker	If you specify `-N, --no-buildid-cache`, perf will just take care of recording the target binary and nothing will be copied.<br>  If you don't specify it, any recorded dynamic library will be copied to ~/.debug in the board.
448*02ca8ccaSAndroid Build Coastguard Worker
449*02ca8ccaSAndroid Build Coastguard Worker2. (on Arm board) `perf archive` which saves all the found libraries in a tar (internally, it looks into perf.data file and performs a lookup using perf-buildid-list --with-hits)
450*02ca8ccaSAndroid Build Coastguard Worker3. (on host) `scp` to copy perf.data and the .tar file generated from `perf archive`.
451*02ca8ccaSAndroid Build Coastguard Worker4. (on host) Run `tar xvf perf_data.tar.bz2 -C ~/.debug` to populate the buildid-cache
452*02ca8ccaSAndroid Build Coastguard Worker5. (on host) Double check the setup is correct:
453*02ca8ccaSAndroid Build Coastguard Worker
454*02ca8ccaSAndroid Build Coastguard Worker       a. `perf buildid-list -i perf.data` gives you the list of dynamic libraries buildids whose trace has been recorded and saved in perf.data.
455*02ca8ccaSAndroid Build Coastguard Worker       b. `perf buildid-cache --list` lists the dynamic libraries in the buildid cache that will be used by `perf inject`.
456*02ca8ccaSAndroid Build Coastguard Worker	Make sure the output of (a) and (b) overlaps as in buildid value for those binaries you are interested into optimizing with afdo.
457*02ca8ccaSAndroid Build Coastguard Worker
458*02ca8ccaSAndroid Build Coastguard Worker6. (on host) `perf inject -i perf.data -o inj.data --itrace=i100000il` will check for the dynamic libraries using the buildid inside the buildid-cache and post-process the trace.<br>  buildids have to be the same, otherwise it won't be possible to post-process the trace.
459*02ca8ccaSAndroid Build Coastguard Worker
460*02ca8ccaSAndroid Build Coastguard Worker7. (on host) `create_llvm_prof -binary=/path/to/binary -profile=inj.data -out=program.llvmprof` takes the output from perf-inject and tranforms it into a format that the compiler can read.
461*02ca8ccaSAndroid Build Coastguard Worker8. (on host) `clang -O2 -fprofile-sample-use=program.llvmprof -o program program.c` to make clang use the produced profile.<br>
462*02ca8ccaSAndroid Build Coastguard Worker	If you are confident enough that your profile is accurate, you can add the `-fprofile-sample-accurate` flag, which will penalize all the callsites without corresponding profile, marking them as cold.
463*02ca8ccaSAndroid Build Coastguard Worker
464*02ca8ccaSAndroid Build Coastguard WorkerIf you are using the same host for both building the binary to be traced and re-building it with afdo:
465*02ca8ccaSAndroid Build Coastguard Worker
466*02ca8ccaSAndroid Build Coastguard Worker1. You won't need to copy back any dynamic libraries from the board (since you already have them), and can use `--no-buildid-cache` when recording
467*02ca8ccaSAndroid Build Coastguard Worker2. You have to make sure the relevant dynamic libraries to be optimized are present in the buildid-cache.
468*02ca8ccaSAndroid Build Coastguard Worker
469*02ca8ccaSAndroid Build Coastguard WorkerYou can easily add a dynamic library manually into the build-id cache by running:
470*02ca8ccaSAndroid Build Coastguard Worker
471*02ca8ccaSAndroid Build Coastguard Worker`perf buildid-cache --add <path/to/library/or/binary> -vvv`
472*02ca8ccaSAndroid Build Coastguard Worker
473*02ca8ccaSAndroid Build Coastguard WorkerYou can easily check what is currently contained in you buildid-cache by running:
474*02ca8ccaSAndroid Build Coastguard Worker
475*02ca8ccaSAndroid Build Coastguard Worker`perf buildid-cache --list`
476*02ca8ccaSAndroid Build Coastguard Worker
477*02ca8ccaSAndroid Build Coastguard WorkerYou can check the buildid of a given binary/dynamic library:
478*02ca8ccaSAndroid Build Coastguard Worker
479*02ca8ccaSAndroid Build Coastguard Worker`file <path/to/dynamic/library>`
480*02ca8ccaSAndroid Build Coastguard Worker
481*02ca8ccaSAndroid Build Coastguard Worker## References
482*02ca8ccaSAndroid Build Coastguard Worker
483*02ca8ccaSAndroid Build Coastguard Worker* AutoFDO tool: <https://github.com/google/autofdo>
484*02ca8ccaSAndroid Build Coastguard Worker* GCC's wiki on autofdo: <https://gcc.gnu.org/wiki/AutoFDO>, <https://gcc.gnu.org/wiki/AutoFDO/Tutorial>
485*02ca8ccaSAndroid Build Coastguard Worker* Google paper: <https://ai.google/research/pubs/pub45290>
486*02ca8ccaSAndroid Build Coastguard Worker* CoreSight kernel docs: Documentation/trace/coresight.txt
487*02ca8ccaSAndroid Build Coastguard Worker
488*02ca8ccaSAndroid Build Coastguard Worker
489*02ca8ccaSAndroid Build Coastguard Worker## Appendix: Describing CoreSight in Devicetree
490*02ca8ccaSAndroid Build Coastguard Worker
491*02ca8ccaSAndroid Build Coastguard Worker
492*02ca8ccaSAndroid Build Coastguard WorkerEach component has an entry in the device tree that describes its:
493*02ca8ccaSAndroid Build Coastguard Worker
494*02ca8ccaSAndroid Build Coastguard Worker* type: The `compatible` field defines which driver to use
495*02ca8ccaSAndroid Build Coastguard Worker* location: A `reg` defines the component's address and size on the bus
496*02ca8ccaSAndroid Build Coastguard Worker* clocks: The `clocks` and `clock-names` fields state which clock provides
497*02ca8ccaSAndroid Build Coastguard Worker  the `apb_pclk` clock.
498*02ca8ccaSAndroid Build Coastguard Worker* connections to other components: `port` and `ports` field link the
499*02ca8ccaSAndroid Build Coastguard Worker  component to ports of other components
500*02ca8ccaSAndroid Build Coastguard Worker
501*02ca8ccaSAndroid Build Coastguard WorkerTo create the device tree, some information about the platform is required:
502*02ca8ccaSAndroid Build Coastguard Worker
503*02ca8ccaSAndroid Build Coastguard Worker* The memory address of the CoreSight components.  This is the address in
504*02ca8ccaSAndroid Build Coastguard Worker  the CPU's address space where the CPU can access each CoreSight
505*02ca8ccaSAndroid Build Coastguard Worker  component.
506*02ca8ccaSAndroid Build Coastguard Worker* The connections between the components.
507*02ca8ccaSAndroid Build Coastguard Worker
508*02ca8ccaSAndroid Build Coastguard WorkerThis information can be found in the SoC's reference manual or you may need
509*02ca8ccaSAndroid Build Coastguard Workerto ask the platform/SoC vendor to supply it.
510*02ca8ccaSAndroid Build Coastguard Worker
511*02ca8ccaSAndroid Build Coastguard WorkerAn ETMv4 source is declared with a section like this:
512*02ca8ccaSAndroid Build Coastguard Worker
513*02ca8ccaSAndroid Build Coastguard Worker```
514*02ca8ccaSAndroid Build Coastguard Worker	etm0: etm@22040000 {
515*02ca8ccaSAndroid Build Coastguard Worker		compatible = "arm,coresight-etm4x", "arm,primecell";
516*02ca8ccaSAndroid Build Coastguard Worker		reg = <0 0x22040000 0 0x1000>;
517*02ca8ccaSAndroid Build Coastguard Worker
518*02ca8ccaSAndroid Build Coastguard Worker		cpu = <&A72_0>;
519*02ca8ccaSAndroid Build Coastguard Worker		clocks = <&soc_smc50mhz>;
520*02ca8ccaSAndroid Build Coastguard Worker		clock-names = "apb_pclk";
521*02ca8ccaSAndroid Build Coastguard Worker		port {
522*02ca8ccaSAndroid Build Coastguard Worker			cluster0_etm0_out_port: endpoint {
523*02ca8ccaSAndroid Build Coastguard Worker				remote-endpoint = <&cluster0_funnel_in_port0>;
524*02ca8ccaSAndroid Build Coastguard Worker			};
525*02ca8ccaSAndroid Build Coastguard Worker		};
526*02ca8ccaSAndroid Build Coastguard Worker	};
527*02ca8ccaSAndroid Build Coastguard Worker```
528*02ca8ccaSAndroid Build Coastguard Worker
529*02ca8ccaSAndroid Build Coastguard WorkerThis describes an ETMv4 attached to core A72_0, located at 0x22040000, with
530*02ca8ccaSAndroid Build Coastguard Workerits output linked to port 0 of a funnel.  The funnel is described with:
531*02ca8ccaSAndroid Build Coastguard Worker
532*02ca8ccaSAndroid Build Coastguard Worker```
533*02ca8ccaSAndroid Build Coastguard Worker	funnel@220c0000 { /* cluster0 funnel */
534*02ca8ccaSAndroid Build Coastguard Worker		compatible = "arm,coresight-funnel", "arm,primecell";
535*02ca8ccaSAndroid Build Coastguard Worker		reg = <0 0x220c0000 0 0x1000>;
536*02ca8ccaSAndroid Build Coastguard Worker
537*02ca8ccaSAndroid Build Coastguard Worker		clocks = <&soc_smc50mhz>;
538*02ca8ccaSAndroid Build Coastguard Worker		clock-names = "apb_pclk";
539*02ca8ccaSAndroid Build Coastguard Worker		power-domains = <&scpi_devpd 0>;
540*02ca8ccaSAndroid Build Coastguard Worker		ports {
541*02ca8ccaSAndroid Build Coastguard Worker			#address-cells = <1>;
542*02ca8ccaSAndroid Build Coastguard Worker			#size-cells = <0>;
543*02ca8ccaSAndroid Build Coastguard Worker
544*02ca8ccaSAndroid Build Coastguard Worker			port@0 {
545*02ca8ccaSAndroid Build Coastguard Worker				reg = <0>;
546*02ca8ccaSAndroid Build Coastguard Worker				cluster0_funnel_out_port: endpoint {
547*02ca8ccaSAndroid Build Coastguard Worker					remote-endpoint = <&main_funnel_in_port0>;
548*02ca8ccaSAndroid Build Coastguard Worker				};
549*02ca8ccaSAndroid Build Coastguard Worker			};
550*02ca8ccaSAndroid Build Coastguard Worker
551*02ca8ccaSAndroid Build Coastguard Worker			port@1 {
552*02ca8ccaSAndroid Build Coastguard Worker				reg = <0>;
553*02ca8ccaSAndroid Build Coastguard Worker				cluster0_funnel_in_port0: endpoint {
554*02ca8ccaSAndroid Build Coastguard Worker					slave-mode;
555*02ca8ccaSAndroid Build Coastguard Worker					remote-endpoint = <&cluster0_etm0_out_port>;
556*02ca8ccaSAndroid Build Coastguard Worker				};
557*02ca8ccaSAndroid Build Coastguard Worker			};
558*02ca8ccaSAndroid Build Coastguard Worker
559*02ca8ccaSAndroid Build Coastguard Worker			port@2 {
560*02ca8ccaSAndroid Build Coastguard Worker				reg = <1>;
561*02ca8ccaSAndroid Build Coastguard Worker				cluster0_funnel_in_port1: endpoint {
562*02ca8ccaSAndroid Build Coastguard Worker					slave-mode;
563*02ca8ccaSAndroid Build Coastguard Worker					remote-endpoint = <&cluster0_etm1_out_port>;
564*02ca8ccaSAndroid Build Coastguard Worker				};
565*02ca8ccaSAndroid Build Coastguard Worker			};
566*02ca8ccaSAndroid Build Coastguard Worker		};
567*02ca8ccaSAndroid Build Coastguard Worker	};
568*02ca8ccaSAndroid Build Coastguard Worker```
569*02ca8ccaSAndroid Build Coastguard Worker
570*02ca8ccaSAndroid Build Coastguard WorkerThis describes a funnel located at 0x220c0000, receiving data from 2 ETMs
571*02ca8ccaSAndroid Build Coastguard Workerand sending the merged data to another funnel.  We continue describing
572*02ca8ccaSAndroid Build Coastguard Workercomponents with similar blocks until we reach the sink (an ETR):
573*02ca8ccaSAndroid Build Coastguard Worker
574*02ca8ccaSAndroid Build Coastguard Worker```
575*02ca8ccaSAndroid Build Coastguard Worker	etr@20070000 {
576*02ca8ccaSAndroid Build Coastguard Worker		compatible = "arm,coresight-tmc", "arm,primecell";
577*02ca8ccaSAndroid Build Coastguard Worker		reg = <0 0x20070000 0 0x1000>;
578*02ca8ccaSAndroid Build Coastguard Worker		iommus = <&smmu_etr 0>;
579*02ca8ccaSAndroid Build Coastguard Worker
580*02ca8ccaSAndroid Build Coastguard Worker		clocks = <&soc_smc50mhz>;
581*02ca8ccaSAndroid Build Coastguard Worker		clock-names = "apb_pclk";
582*02ca8ccaSAndroid Build Coastguard Worker		power-domains = <&scpi_devpd 0>;
583*02ca8ccaSAndroid Build Coastguard Worker		port {
584*02ca8ccaSAndroid Build Coastguard Worker			etr_in_port: endpoint {
585*02ca8ccaSAndroid Build Coastguard Worker				slave-mode;
586*02ca8ccaSAndroid Build Coastguard Worker				remote-endpoint = <&replicator_out_port1>;
587*02ca8ccaSAndroid Build Coastguard Worker			};
588*02ca8ccaSAndroid Build Coastguard Worker		};
589*02ca8ccaSAndroid Build Coastguard Worker	};
590*02ca8ccaSAndroid Build Coastguard Worker```
591*02ca8ccaSAndroid Build Coastguard Worker
592*02ca8ccaSAndroid Build Coastguard WorkerFull descriptions of the properties of each component can be found in the
593*02ca8ccaSAndroid Build Coastguard WorkerLinux source at Documentation/devicetree/bindings/arm/coresight.txt.
594*02ca8ccaSAndroid Build Coastguard WorkerThe Arm Juno platform's devicetree (arch/arm64/boot/dts/arm) provides an example
595*02ca8ccaSAndroid Build Coastguard Workerdescription of CoreSight description.
596*02ca8ccaSAndroid Build Coastguard Worker
597*02ca8ccaSAndroid Build Coastguard WorkerMany systems include a TPIU for off-chip trace.  While this isn't required
598*02ca8ccaSAndroid Build Coastguard Workerfor self-hosted trace, it should still be included in the devicetree.  This
599*02ca8ccaSAndroid Build Coastguard Workerallows the drivers to access it to ensure it is put into a disabled state,
600*02ca8ccaSAndroid Build Coastguard Workerotherwise it may limit the trace bandwidth causing data loss.
601