← Documents Documentation/trace/coresight/coresight.rst GitHub 원문 ↗

Linux 6.18.37 · Tracing

CoreSight - HW Assisted Tracing on ARM

ARM CoreSight의 source·link·sink와 APB·ATB·CTM 구조, framework 등록·naming·connections topology, sysfs/Perf trace 수집, PID·AutoFDO, STM·CTI와 system configuration을 설명합니다.

Source pathDocumentation/trace/coresight/coresight.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

coresight.rst:1-686

ARM CoreSight의 source·link·sink와 APB·ATB·CTM 구조, framework 등록·naming·connections topology, sysfs/Perf trace 수집, PID·AutoFDO, STM·CTI와 system configuration을 설명합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ======================================
2 Coresight - HW Assisted Tracing on ARM
3 ======================================
4
5 :Author: Mathieu Poirier <[email protected]>
6 :Date: September 11th, 2014
7
8 Introduction
9 ------------
10
11 Coresight is an umbrella of technologies allowing for the debugging of ARM
12 based SoC. It includes solutions for JTAG and HW assisted tracing. This
13 document is concerned with the latter.
14
15 HW assisted tracing is becoming increasingly useful when dealing with systems
16 that have many SoCs and other components like GPU and DMA engines. ARM has
17 developed a HW assisted tracing solution by means of different components, each
18 being added to a design at synthesis time to cater to specific tracing needs.
19 Components are generally categorised as source, link and sinks and are
20 (usually) discovered using the AMBA bus.
21
22 "Sources" generate a compressed stream representing the processor instruction
23 path based on tracing scenarios as configured by users. From there the stream
24 flows through the coresight system (via ATB bus) using links that are connecting
25 the emanating source to a sink(s). Sinks serve as endpoints to the coresight
26 implementation, either storing the compressed stream in a memory buffer or
27 creating an interface to the outside world where data can be transferred to a
28 host without fear of filling up the onboard coresight memory buffer.
29
30 At typical coresight system would look like this::
31
32 *****************************************************************
33 **************************** AMBA AXI ****************************===||
34 ***************************************************************** ||
35 ^ ^ | ||
36 | | * **
37 0000000 ::::: 0000000 ::::: ::::: @@@@@@@ ||||||||||||
38 0 CPU 0<-->: C : 0 CPU 0<-->: C : : C : @ STM @ || System ||
39 |->0000000 : T : |->0000000 : T : : T :<--->@@@@@ || Memory ||
40 | #######<-->: I : | #######<-->: I : : I : @@@<-| ||||||||||||
41 | # ETM # ::::: | # PTM # ::::: ::::: @ |
42 | ##### ^ ^ | ##### ^ ! ^ ! . | |||||||||
43 | |->### | ! | |->### | ! | ! . | || DAP ||
44 | | # | ! | | # | ! | ! . | |||||||||
45 | | . | ! | | . | ! | ! . | | |
46 | | . | ! | | . | ! | ! . | | *
47 | | . | ! | | . | ! | ! . | | SWD/
48 | | . | ! | | . | ! | ! . | | JTAG
49 *****************************************************************<-|
50 *************************** AMBA Debug APB ************************
51 *****************************************************************
52 | . ! . ! ! . |
53 | . * . * * . |
54 *****************************************************************
55 ******************** Cross Trigger Matrix (CTM) *******************
56 *****************************************************************
57 | . ^ . . |
58 | * ! * * |
59 *****************************************************************
60 ****************** AMBA Advanced Trace Bus (ATB) ******************
61 *****************************************************************
62 | ! =============== |
63 | * ===== F =====<---------|
64 | ::::::::: ==== U ====
65 |-->:: CTI ::<!! === N ===
66 | ::::::::: ! == N ==
67 | ^ * == E ==
68 | ! &&&&&&&&& IIIIIII == L ==
69 |------>&& ETB &&<......II I =======
70 | ! &&&&&&&&& II I .
71 | ! I I .
72 | ! I REP I<..........
73 | ! I I
74 | !!>&&&&&&&&& II I *Source: ARM ltd.
75 |------>& TPIU &<......II I DAP = Debug Access Port
76 &&&&&&&&& IIIIIII ETM = Embedded Trace Macrocell
77 ; PTM = Program Trace Macrocell
78 ; CTI = Cross Trigger Interface
79 * ETB = Embedded Trace Buffer
80 To trace port TPIU= Trace Port Interface Unit
81 SWD = Serial Wire Debug
82
83 While on target configuration of the components is done via the APB bus,
84 all trace data are carried out-of-band on the ATB bus. The CTM provides
85 a way to aggregate and distribute signals between CoreSight components.
86
87 The coresight framework provides a central point to represent, configure and
88 manage coresight devices on a platform. This first implementation centers on
89 the basic tracing functionality, enabling components such ETM/PTM, funnel,
90 replicator, TMC, TPIU and ETB. Future work will enable more
91 intricate IP blocks such as STM and CTI.
92
93
94 Acronyms and Classification
95 ---------------------------
96
97 Acronyms:
98
99 PTM:
100 Program Trace Macrocell
101 ETM:
102 Embedded Trace Macrocell
103 STM:
104 System trace Macrocell
105 ETB:
106 Embedded Trace Buffer
107 ITM:
108 Instrumentation Trace Macrocell
109 TPIU:
110 Trace Port Interface Unit
111 TMC-ETR:
112 Trace Memory Controller, configured as Embedded Trace Router
113 TMC-ETF:
114 Trace Memory Controller, configured as Embedded Trace FIFO
115 CTI:
116 Cross Trigger Interface
117
118 Classification:
119
120 Source:
121 ETMv3.x ETMv4, PTMv1.0, PTMv1.1, STM, STM500, ITM
122 Link:
123 Funnel, replicator (intelligent or not), TMC-ETR
124 Sinks:
125 ETBv1.0, ETB1.1, TPIU, TMC-ETF
126 Misc:
127 CTI
128
129
130 Device Tree Bindings
131 --------------------
132
133 See ``Documentation/devicetree/bindings/arm/arm,coresight-*.yaml`` for details.
134
135 As of this writing drivers for ITM, STMs and CTIs are not provided but are
136 expected to be added as the solution matures.
137
138
139 Framework and implementation
140 ----------------------------
141
142 The coresight framework provides a central point to represent, configure and
143 manage coresight devices on a platform. Any coresight compliant device can
144 register with the framework for as long as they use the right APIs:
145
146 .. c:function:: struct coresight_device *coresight_register(struct coresight_desc *desc);
147 .. c:function:: void coresight_unregister(struct coresight_device *csdev);
148
149 The registering function is taking a ``struct coresight_desc *desc`` and
150 register the device with the core framework. The unregister function takes
151 a reference to a ``struct coresight_device *csdev`` obtained at registration time.
152
153 If everything goes well during the registration process the new devices will
154 show up under /sys/bus/coresight/devices, as showns here for a TC2 platform::
155
156 root:~# ls /sys/bus/coresight/devices/
157 replicator 20030000.tpiu 2201c000.ptm 2203c000.etm 2203e000.etm
158 20010000.etb 20040000.funnel 2201d000.ptm 2203d000.etm
159 root:~#
160
161 The functions take a ``struct coresight_device``, which looks like this::
162
163 struct coresight_desc {
164 enum coresight_dev_type type;
165 struct coresight_dev_subtype subtype;
166 const struct coresight_ops *ops;
167 struct coresight_platform_data *pdata;
168 struct device *dev;
169 const struct attribute_group **groups;
170 };
171
172
173 The "coresight_dev_type" identifies what the device is, i.e, source link or
174 sink while the "coresight_dev_subtype" will characterise that type further.
175
176 The ``struct coresight_ops`` is mandatory and will tell the framework how to
177 perform base operations related to the components, each component having
178 a different set of requirement. For that ``struct coresight_ops_sink``,
179 ``struct coresight_ops_link`` and ``struct coresight_ops_source`` have been
180 provided.
181
182 The next field ``struct coresight_platform_data *pdata`` is acquired by calling
183 ``of_get_coresight_platform_data()``, as part of the driver's _probe routine and
184 ``struct device *dev`` gets the device reference embedded in the ``amba_device``::
185
186 static int etm_probe(struct amba_device *adev, const struct amba_id *id)
187 {
188 ...
189 ...
190 drvdata->dev = &adev->dev;
191 ...
192 }
193
194 Specific class of device (source, link, or sink) have generic operations
195 that can be performed on them (see ``struct coresight_ops``). The ``**groups``
196 is a list of sysfs entries pertaining to operations
197 specific to that component only. "Implementation defined" customisations are
198 expected to be accessed and controlled using those entries.
199
200 Device Naming scheme
201 --------------------
202
203 The devices that appear on the "coresight" bus were named the same as their
204 parent devices, i.e, the real devices that appears on AMBA bus or the platform bus.
205 Thus the names were based on the Linux Open Firmware layer naming convention,
206 which follows the base physical address of the device followed by the device
207 type. e.g::
208
209 root:~# ls /sys/bus/coresight/devices/
210 20010000.etf 20040000.funnel 20100000.stm 22040000.etm
211 22140000.etm 230c0000.funnel 23240000.etm 20030000.tpiu
212 20070000.etr 20120000.replicator 220c0000.funnel
213 23040000.etm 23140000.etm 23340000.etm
214
215 However, with the introduction of ACPI support, the names of the real
216 devices are a bit cryptic and non-obvious. Thus, a new naming scheme was
217 introduced to use more generic names based on the type of the device. The
218 following rules apply::
219
220 1) Devices that are bound to CPUs, are named based on the CPU logical
221 number.
222
223 e.g, ETM bound to CPU0 is named "etm0"
224
225 2) All other devices follow a pattern, "<device_type_prefix>N", where :
226
227 <device_type_prefix> - A prefix specific to the type of the device
228 N - a sequential number assigned based on the order
229 of probing.
230
231 e.g, tmc_etf0, tmc_etr0, funnel0, funnel1
232
233 Thus, with the new scheme the devices could appear as ::
234
235 root:~# ls /sys/bus/coresight/devices/
236 etm0 etm1 etm2 etm3 etm4 etm5 funnel0
237 funnel1 funnel2 replicator0 stm0 tmc_etf0 tmc_etr0 tpiu0
238
239 Some of the examples below might refer to old naming scheme and some
240 to the newer scheme, to give a confirmation that what you see on your
241 system is not unexpected. One must use the "names" as they appear on
242 the system under specified locations.
243
244 Topology Representation
245 -----------------------
246
247 Each CoreSight component has a ``connections`` directory which will contain
248 links to other CoreSight components. This allows the user to explore the trace
249 topology and for larger systems, determine the most appropriate sink for a
250 given source. The connection information can also be used to establish
251 which CTI devices are connected to a given component. This directory contains a
252 ``nr_links`` attribute detailing the number of links in the directory.
253
254 For an ETM source, in this case ``etm0`` on a Juno platform, a typical
255 arrangement will be::
256
257 linaro-developer:~# ls - l /sys/bus/coresight/devices/etm0/connections
258 <file details> cti_cpu0 -> ../../../23020000.cti/cti_cpu0
259 <file details> nr_links
260 <file details> out:0 -> ../../../230c0000.funnel/funnel2
261
262 Following the out port to ``funnel2``::
263
264 linaro-developer:~# ls -l /sys/bus/coresight/devices/funnel2/connections
265 <file details> in:0 -> ../../../23040000.etm/etm0
266 <file details> in:1 -> ../../../23140000.etm/etm3
267 <file details> in:2 -> ../../../23240000.etm/etm4
268 <file details> in:3 -> ../../../23340000.etm/etm5
269 <file details> nr_links
270 <file details> out:0 -> ../../../20040000.funnel/funnel0
271
272 And again to ``funnel0``::
273
274 linaro-developer:~# ls -l /sys/bus/coresight/devices/funnel0/connections
275 <file details> in:0 -> ../../../220c0000.funnel/funnel1
276 <file details> in:1 -> ../../../230c0000.funnel/funnel2
277 <file details> nr_links
278 <file details> out:0 -> ../../../20010000.etf/tmc_etf0
279
280 Finding the first sink ``tmc_etf0``. This can be used to collect data
281 as a sink, or as a link to propagate further along the chain::
282
283 linaro-developer:~# ls -l /sys/bus/coresight/devices/tmc_etf0/connections
284 <file details> cti_sys0 -> ../../../20020000.cti/cti_sys0
285 <file details> in:0 -> ../../../20040000.funnel/funnel0
286 <file details> nr_links
287 <file details> out:0 -> ../../../20150000.funnel/funnel4
288
289 via ``funnel4``::
290
291 linaro-developer:~# ls -l /sys/bus/coresight/devices/funnel4/connections
292 <file details> in:0 -> ../../../20010000.etf/tmc_etf0
293 <file details> in:1 -> ../../../20140000.etf/tmc_etf1
294 <file details> nr_links
295 <file details> out:0 -> ../../../20120000.replicator/replicator0
296
297 and a ``replicator0``::
298
299 linaro-developer:~# ls -l /sys/bus/coresight/devices/replicator0/connections
300 <file details> in:0 -> ../../../20150000.funnel/funnel4
301 <file details> nr_links
302 <file details> out:0 -> ../../../20030000.tpiu/tpiu0
303 <file details> out:1 -> ../../../20070000.etr/tmc_etr0
304
305 Arriving at the final sink in the chain, ``tmc_etr0``::
306
307 linaro-developer:~# ls -l /sys/bus/coresight/devices/tmc_etr0/connections
308 <file details> cti_sys0 -> ../../../20020000.cti/cti_sys0
309 <file details> in:0 -> ../../../20120000.replicator/replicator0
310 <file details> nr_links
311
312 As described below, when using sysfs it is sufficient to enable a sink and
313 a source for successful trace. The framework will correctly enable all
314 intermediate links as required.
315
316 Note: ``cti_sys0`` appears in two of the connections lists above.
317 CTIs can connect to multiple devices and are arranged in a star topology
318 via the CTM. See (Documentation/trace/coresight/coresight-ect.rst)
319 [#fourth]_ for further details.
320 Looking at this device we see 4 connections::
321
322 linaro-developer:~# ls -l /sys/bus/coresight/devices/cti_sys0/connections
323 <file details> nr_links
324 <file details> stm0 -> ../../../20100000.stm/stm0
325 <file details> tmc_etf0 -> ../../../20010000.etf/tmc_etf0
326 <file details> tmc_etr0 -> ../../../20070000.etr/tmc_etr0
327 <file details> tpiu0 -> ../../../20030000.tpiu/tpiu0
328
329
330 How to use the tracer modules
331 -----------------------------
332
333 There are two ways to use the Coresight framework:
334
335 1. using the perf cmd line tools.
336 2. interacting directly with the Coresight devices using the sysFS interface.
337
338 Preference is given to the former as using the sysFS interface
339 requires a deep understanding of the Coresight HW. The following sections
340 provide details on using both methods.
341
342 Using the sysFS interface
343 ~~~~~~~~~~~~~~~~~~~~~~~~~
344
345 Before trace collection can start, a coresight sink needs to be identified.
346 There is no limit on the amount of sinks (nor sources) that can be enabled at
347 any given moment. As a generic operation, all device pertaining to the sink
348 class will have an "active" entry in sysfs::
349
350 root:/sys/bus/coresight/devices# ls
351 replicator 20030000.tpiu 2201c000.ptm 2203c000.etm 2203e000.etm
352 20010000.etb 20040000.funnel 2201d000.ptm 2203d000.etm
353 root:/sys/bus/coresight/devices# ls 20010000.etb
354 enable_sink status trigger_cntr
355 root:/sys/bus/coresight/devices# echo 1 > 20010000.etb/enable_sink
356 root:/sys/bus/coresight/devices# cat 20010000.etb/enable_sink
357 1
358 root:/sys/bus/coresight/devices#
359
360 At boot time the current etm3x driver will configure the first address
361 comparator with "_stext" and "_etext", essentially tracing any instruction
362 that falls within that range. As such "enabling" a source will immediately
363 trigger a trace capture::
364
365 root:/sys/bus/coresight/devices# echo 1 > 2201c000.ptm/enable_source
366 root:/sys/bus/coresight/devices# cat 2201c000.ptm/enable_source
367 1
368 root:/sys/bus/coresight/devices# cat 20010000.etb/status
369 Depth: 0x2000
370 Status: 0x1
371 RAM read ptr: 0x0
372 RAM wrt ptr: 0x19d3 <----- The write pointer is moving
373 Trigger cnt: 0x0
374 Control: 0x1
375 Flush status: 0x0
376 Flush ctrl: 0x2001
377 root:/sys/bus/coresight/devices#
378
379 Trace collection is stopped the same way::
380
381 root:/sys/bus/coresight/devices# echo 0 > 2201c000.ptm/enable_source
382 root:/sys/bus/coresight/devices#
383
384 The content of the ETB buffer can be harvested directly from /dev::
385
386 root:/sys/bus/coresight/devices# dd if=/dev/20010000.etb \
387 of=~/cstrace.bin
388 64+0 records in
389 64+0 records out
390 32768 bytes (33 kB) copied, 0.00125258 s, 26.2 MB/s
391 root:/sys/bus/coresight/devices#
392
393 The file cstrace.bin can be decompressed using "ptm2human", DS-5 or Trace32.
394
395 Following is a DS-5 output of an experimental loop that increments a variable up
396 to a certain value. The example is simple and yet provides a glimpse of the
397 wealth of possibilities that coresight provides.
398 ::
399
400 Info Tracing enabled
401 Instruction 106378866 0x8026B53C E52DE004 false PUSH {lr}
402 Instruction 0 0x8026B540 E24DD00C false SUB sp,sp,#0xc
403 Instruction 0 0x8026B544 E3A03000 false MOV r3,#0
404 Instruction 0 0x8026B548 E58D3004 false STR r3,[sp,#4]
405 Instruction 0 0x8026B54C E59D3004 false LDR r3,[sp,#4]
406 Instruction 0 0x8026B550 E3530004 false CMP r3,#4
407 Instruction 0 0x8026B554 E2833001 false ADD r3,r3,#1
408 Instruction 0 0x8026B558 E58D3004 false STR r3,[sp,#4]
409 Instruction 0 0x8026B55C DAFFFFFA true BLE {pc}-0x10 ; 0x8026b54c
410 Timestamp Timestamp: 17106715833
411 Instruction 319 0x8026B54C E59D3004 false LDR r3,[sp,#4]
412 Instruction 0 0x8026B550 E3530004 false CMP r3,#4
413 Instruction 0 0x8026B554 E2833001 false ADD r3,r3,#1
414 Instruction 0 0x8026B558 E58D3004 false STR r3,[sp,#4]
415 Instruction 0 0x8026B55C DAFFFFFA true BLE {pc}-0x10 ; 0x8026b54c
416 Instruction 9 0x8026B54C E59D3004 false LDR r3,[sp,#4]
417 Instruction 0 0x8026B550 E3530004 false CMP r3,#4
418 Instruction 0 0x8026B554 E2833001 false ADD r3,r3,#1
419 Instruction 0 0x8026B558 E58D3004 false STR r3,[sp,#4]
420 Instruction 0 0x8026B55C DAFFFFFA true BLE {pc}-0x10 ; 0x8026b54c
421 Instruction 7 0x8026B54C E59D3004 false LDR r3,[sp,#4]
422 Instruction 0 0x8026B550 E3530004 false CMP r3,#4
423 Instruction 0 0x8026B554 E2833001 false ADD r3,r3,#1
424 Instruction 0 0x8026B558 E58D3004 false STR r3,[sp,#4]
425 Instruction 0 0x8026B55C DAFFFFFA true BLE {pc}-0x10 ; 0x8026b54c
426 Instruction 7 0x8026B54C E59D3004 false LDR r3,[sp,#4]
427 Instruction 0 0x8026B550 E3530004 false CMP r3,#4
428 Instruction 0 0x8026B554 E2833001 false ADD r3,r3,#1
429 Instruction 0 0x8026B558 E58D3004 false STR r3,[sp,#4]
430 Instruction 0 0x8026B55C DAFFFFFA true BLE {pc}-0x10 ; 0x8026b54c
431 Instruction 10 0x8026B54C E59D3004 false LDR r3,[sp,#4]
432 Instruction 0 0x8026B550 E3530004 false CMP r3,#4
433 Instruction 0 0x8026B554 E2833001 false ADD r3,r3,#1
434 Instruction 0 0x8026B558 E58D3004 false STR r3,[sp,#4]
435 Instruction 0 0x8026B55C DAFFFFFA true BLE {pc}-0x10 ; 0x8026b54c
436 Instruction 6 0x8026B560 EE1D3F30 false MRC p15,#0x0,r3,c13,c0,#1
437 Instruction 0 0x8026B564 E1A0100D false MOV r1,sp
438 Instruction 0 0x8026B568 E3C12D7F false BIC r2,r1,#0x1fc0
439 Instruction 0 0x8026B56C E3C2203F false BIC r2,r2,#0x3f
440 Instruction 0 0x8026B570 E59D1004 false LDR r1,[sp,#4]
441 Instruction 0 0x8026B574 E59F0010 false LDR r0,[pc,#16] ; [0x8026B58C] = 0x80550368
442 Instruction 0 0x8026B578 E592200C false LDR r2,[r2,#0xc]
443 Instruction 0 0x8026B57C E59221D0 false LDR r2,[r2,#0x1d0]
444 Instruction 0 0x8026B580 EB07A4CF true BL {pc}+0x1e9344 ; 0x804548c4
445 Info Tracing enabled
446 Instruction 13570831 0x8026B584 E28DD00C false ADD sp,sp,#0xc
447 Instruction 0 0x8026B588 E8BD8000 true LDM sp!,{pc}
448 Timestamp Timestamp: 17107041535
449
450 Using perf framework
451 ~~~~~~~~~~~~~~~~~~~~
452
453 Coresight tracers are represented using the Perf framework's Performance
454 Monitoring Unit (PMU) abstraction. As such the perf framework takes charge of
455 controlling when tracing gets enabled based on when the process of interest is
456 scheduled. When configured in a system, Coresight PMUs will be listed when
457 queried by the perf command line tool:
458
459 linaro@linaro-nano:~$ ./perf list pmu
460
461 List of pre-defined events (to be used in -e):
462
463 cs_etm// [Kernel PMU event]
464
465 Regardless of the number of tracers available in a system (usually equal to the
466 amount of processor cores), the "cs_etm" PMU will be listed only once.
467
468 A Coresight PMU works the same way as any other PMU, i.e the name of the PMU is
469 provided along with configuration options within forward slashes '/' (see
470 `Config option formats`_).
471
472 Advanced Perf framework usage
473 -----------------------------
474
475 Sink selection
476 ~~~~~~~~~~~~~~
477
478 An appropriate sink will be selected automatically for use with Perf, but since
479 there will typically be more than one sink, the name of the sink to use may be
480 specified as a special config option prefixed with '@'.
481
482 The available sinks are listed in sysFS under
483 ($SYSFS)/bus/event_source/devices/cs_etm/sinks/::
484
485 root@localhost:/sys/bus/event_source/devices/cs_etm/sinks# ls
486 tmc_etf0 tmc_etr0 tpiu0
487
488 root@linaro-nano:~# perf record -e cs_etm/@tmc_etr0/u --per-thread program
489
490 More information on the above and other example on how to use Coresight with
491 the perf tools can be found in the "HOWTO.md" file of the openCSD gitHub
492 repository [#third]_.
493
494 AutoFDO analysis using the perf tools
495 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
496
497 perf can be used to record and analyze trace of programs.
498
499 Execution can be recorded using 'perf record' with the cs_etm event,
500 specifying the name of the sink to record to, e.g::
501
502 perf record -e cs_etm//u --per-thread
503
504 The 'perf report' and 'perf script' commands can be used to analyze execution,
505 synthesizing instruction and branch events from the instruction trace.
506 'perf inject' can be used to replace the trace data with the synthesized events.
507 The --itrace option controls the type and frequency of synthesized events
508 (see perf documentation).
509
510 Note that only 64-bit programs are currently supported - further work is
511 required to support instruction decode of 32-bit Arm programs.
512
513 Tracing PID
514 ~~~~~~~~~~~
515
516 The kernel can be built to write the PID value into the PE ContextID registers.
517 For a kernel running at EL1, the PID is stored in CONTEXTIDR_EL1. A PE may
518 implement Arm Virtualization Host Extensions (VHE), which the kernel can
519 run at EL2 as a virtualisation host; in this case, the PID value is stored in
520 CONTEXTIDR_EL2.
521
522 perf provides PMU formats that program the ETM to insert these values into the
523 trace data; the PMU formats are defined as below:
524
525 "contextid1": Available on both EL1 kernel and EL2 kernel. When the
526 kernel is running at EL1, "contextid1" enables the PID
527 tracing; when the kernel is running at EL2, this enables
528 tracing the PID of guest applications.
529
530 "contextid2": Only usable when the kernel is running at EL2. When
531 selected, enables PID tracing on EL2 kernel.
532
533 "contextid": Will be an alias for the option that enables PID
534 tracing. I.e,
535 contextid == contextid1, on EL1 kernel.
536 contextid == contextid2, on EL2 kernel.
537
538 perf will always enable PID tracing at the relevant EL, this is accomplished by
539 automatically enable the "contextid" config - but for EL2 it is possible to make
540 specific adjustments using configs "contextid1" and "contextid2", E.g. if a user
541 wants to trace PIDs for both host and guest, the two configs "contextid1" and
542 "contextid2" can be set at the same time:
543
544 perf record -e cs_etm/contextid1,contextid2/u -- vm
545
546
547 Generating coverage files for Feedback Directed Optimization: AutoFDO
548 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
549
550 'perf inject' accepts the --itrace option in which case tracing data is
551 removed and replaced with the synthesized events. e.g.
552 ::
553
554 perf inject --itrace --strip -i perf.data -o perf.data.new
555
556 Below is an example of using ARM ETM for autoFDO. It requires autofdo
557 (https://github.com/google/autofdo) and gcc version 5. The bubble
558 sort example is from the AutoFDO tutorial (https://gcc.gnu.org/wiki/AutoFDO/Tutorial).
559 ::
560
561 $ gcc-5 -O3 sort.c -o sort
562 $ taskset -c 2 ./sort
563 Bubble sorting array of 30000 elements
564 5910 ms
565
566 $ perf record -e cs_etm//u --per-thread taskset -c 2 ./sort
567 Bubble sorting array of 30000 elements
568 12543 ms
569 [ perf record: Woken up 35 times to write data ]
570 [ perf record: Captured and wrote 69.640 MB perf.data ]
571
572 $ perf inject -i perf.data -o inj.data --itrace=il64 --strip
573 $ create_gcov --binary=./sort --profile=inj.data --gcov=sort.gcov -gcov_version=1
574 $ gcc-5 -O3 -fauto-profile=sort.gcov sort.c -o sort_autofdo
575 $ taskset -c 2 ./sort_autofdo
576 Bubble sorting array of 30000 elements
577 5806 ms
578
579 Config option formats
580 ~~~~~~~~~~~~~~~~~~~~~
581
582 The following strings can be provided between // on the perf command line to enable various options.
583 They are also listed in the folder /sys/bus/event_source/devices/cs_etm/format/
584
585 .. list-table::
586 :header-rows: 1
587
588 * - Option
589 - Description
590 * - branch_broadcast
591 - Session local version of the system wide setting:
592 :ref:`ETM_MODE_BB <coresight-branch-broadcast>`
593 * - contextid
594 - See `Tracing PID`_
595 * - contextid1
596 - See `Tracing PID`_
597 * - contextid2
598 - See `Tracing PID`_
599 * - configid
600 - Selection for a custom configuration. This is an implementation detail and not used directly,
601 see :ref:`trace/coresight/coresight-config:Using Configurations in perf`
602 * - preset
603 - Override for parameters in a custom configuration, see
604 :ref:`trace/coresight/coresight-config:Using Configurations in perf`
605 * - sinkid
606 - Hashed version of the string to select a sink, automatically set when using the @ notation.
607 This is an internal implementation detail and is not used directly, see `Using perf
608 framework`_.
609 * - cycacc
610 - Session local version of the system wide setting: :ref:`ETMv4_MODE_CYCACC
611 <coresight-cycle-accurate>`
612 * - retstack
613 - Session local version of the system wide setting: :ref:`ETM_MODE_RETURNSTACK
614 <coresight-return-stack>`
615 * - timestamp
616 - Session local version of the system wide setting: :ref:`ETMv4_MODE_TIMESTAMP
617 <coresight-timestamp>`
618 * - cc_threshold
619 - Cycle count threshold value. If nothing is provided here or the provided value is 0, then the
620 default value i.e 0x100 will be used. If provided value is less than minimum cycles threshold
621 value, as indicated via TRCIDR3.CCITMIN, then the minimum value will be used instead.
622
623 How to use the STM module
624 -------------------------
625
626 Using the System Trace Macrocell module is the same as the tracers - the only
627 difference is that clients are driving the trace capture rather
628 than the program flow through the code.
629
630 As with any other CoreSight component, specifics about the STM tracer can be
631 found in sysfs with more information on each entry being found in [#first]_::
632
633 root@genericarmv8:~# ls /sys/bus/coresight/devices/stm0
634 enable_source hwevent_select port_enable subsystem uevent
635 hwevent_enable mgmt port_select traceid
636 root@genericarmv8:~#
637
638 Like any other source a sink needs to be identified and the STM enabled before
639 being used::
640
641 root@genericarmv8:~# echo 1 > /sys/bus/coresight/devices/tmc_etf0/enable_sink
642 root@genericarmv8:~# echo 1 > /sys/bus/coresight/devices/stm0/enable_source
643
644 From there user space applications can request and use channels using the devfs
645 interface provided for that purpose by the generic STM API::
646
647 root@genericarmv8:~# ls -l /dev/stm0
648 crw------- 1 root root 10, 61 Jan 3 18:11 /dev/stm0
649 root@genericarmv8:~#
650
651 Details on how to use the generic STM API can be found here:
652 - Documentation/trace/stm.rst [#second]_.
653
654 The CTI & CTM Modules
655 ---------------------
656
657 The CTI (Cross Trigger Interface) provides a set of trigger signals between
658 individual CTIs and components, and can propagate these between all CTIs via
659 channels on the CTM (Cross Trigger Matrix).
660
661 A separate documentation file is provided to explain the use of these devices.
662 (Documentation/trace/coresight/coresight-ect.rst) [#fourth]_.
663
664 CoreSight System Configuration
665 ------------------------------
666
667 CoreSight components can be complex devices with many programming options.
668 Furthermore, components can be programmed to interact with each other across the
669 complete system.
670
671 A CoreSight System Configuration manager is provided to allow these complex programming
672 configurations to be selected and used easily from perf and sysfs.
673
674 See the separate document for further information.
675 (Documentation/trace/coresight/coresight-config.rst) [#fifth]_.
676
677
678 .. [#first] Documentation/ABI/testing/sysfs-bus-coresight-devices-stm
679
680 .. [#second] Documentation/trace/stm.rst
681
682 .. [#third] https://github.com/Linaro/perf-opencsd
683
684 .. [#fourth] Documentation/trace/coresight/coresight-ect.rst
685
686 .. [#fifth] Documentation/trace/coresight/coresight-config.rst
687

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Source·link·sink와 APB·ATB·CTM 구조

1-93

이 문서는 Mathieu Poirier가 2014년 9월 11일에 작성했다. CoreSight는 ARM 기반 SoC debugging을 위한 기술군으로 JTAG와 hardware-assisted tracing을 포함하며, 이 문서는 후자를 다룬다.

여러 SoC와 GPU, DMA engine 같은 구성요소가 있는 system에서는 hardware-assisted tracing의 가치가 커진다. ARM은 synthesis 단계에서 특정 tracing 요구에 맞는 구성요소를 design에 추가하는 방식으로 이 기능을 제공한다. 구성요소는 보통 source, link, sink로 분류되고 AMBA bus를 통해 발견된다.

source는 사용자가 설정한 tracing scenario에 따라 processor instruction path를 나타내는 compressed stream을 만든다. stream은 ATB bus를 통해 source와 sink를 잇는 link를 지나간다.

sink는 CoreSight 구현의 endpoint다. compressed stream을 on-board memory buffer에 저장하거나 외부 interface를 만들어 data를 host로 보낸다. 외부 전송 sink를 사용하면 on-board CoreSight memory buffer가 가득 찰 위험 없이 장시간 수집할 수 있다.

원문 시스템 그림에는 CPU별 ETM/PTM source, STM source, CTI, funnel, replicator, ETB, TPIU, system memory, DAP와 SWD/JTAG가 함께 나타난다. AMBA AXI는 system memory 쪽 data path를, Debug APB는 target 내부 programming을, ATB는 trace stream을 운반한다.

target에서 구성요소 설정은 APB bus로 수행하지만 trace data는 별도의 ATB bus로 전달된다. CTM은 CoreSight 구성요소 사이의 trigger signal을 모으고 분배한다.

CoreSight framework는 platform의 CoreSight device를 표현·설정·관리하는 중앙 지점이다. 초기 구현은 ETM/PTM, funnel, replicator, TMC, TPIU, ETB의 기본 tracing 기능에 초점을 맞췄으며 문서 작성 당시 STM과 CTI 같은 복잡한 IP block은 후속 작업으로 설명됐다.

원문 CoreSight trace data 경로
CPU instructionETM / PTM source
STM instrumentationSTM source
ETM / PTM sourceATB
STM sourceATB
ATBFunnel
FunnelReplicator
ReplicatorETB memory buffer
ReplicatorTPIU trace port
Trace portExternal host

대형 ASCII 그림의 source, link, sink 연결을 기능별로 재구성했다.

설정과 trigger 경로
SWD / JTAG via DAPAMBA Debug APB
AMBA Debug APBConfigure CoreSight components
CTI trigger signalsCross Trigger Matrix
Cross Trigger MatrixDistribute triggers

trace payload와 별개로 APB와 CTM이 control 및 trigger를 전달한다.

CoreSight bus 역할
경로역할
AMBA Debug APBtarget 내부 CoreSight register 설정
AMBA ATBout-of-band trace data stream
AMBA AXIsystem memory와 연결
CTM구성요소 간 trigger 집계·분배

payload, programming, system memory access와 trigger network를 분리한다.

구성요소 class
Class책임
Sourcecompressed trace stream 생성
Linksource에서 sink까지 stream 연결·병합·복제
Sinkmemory 저장 또는 외부 host 전송

trace stream lifecycle을 source, link와 sink로 나눈다.

======================================
Coresight - HW Assisted Tracing on ARM
======================================

   :Author:   Mathieu Poirier <[email protected]>
   :Date:     September 11th, 2014

Introduction
------------

Coresight is an umbrella of technologies allowing for the debugging of ARM
based SoC.  It includes solutions for JTAG and HW assisted tracing.  This
document is concerned with the latter.

HW assisted tracing is becoming increasingly useful when dealing with systems
that have many SoCs and other components like GPU and DMA engines.  ARM has
developed a HW assisted tracing solution by means of different components, each
being added to a design at synthesis time to cater to specific tracing needs.
Components are generally categorised as source, link and sinks and are
(usually) discovered using the AMBA bus.

"Sources" generate a compressed stream representing the processor instruction
path based on tracing scenarios as configured by users.  From there the stream
flows through the coresight system (via ATB bus) using links that are connecting
the emanating source to a sink(s).  Sinks serve as endpoints to the coresight
implementation, either storing the compressed stream in a memory buffer or
creating an interface to the outside world where data can be transferred to a
host without fear of filling up the onboard coresight memory buffer.

At typical coresight system would look like this::

  *****************************************************************
 **************************** AMBA AXI  ****************************===||
  *****************************************************************    ||
        ^                    ^                            |            ||
        |                    |                            *            **
     0000000    :::::     0000000    :::::    :::::    @@@@@@@    ||||||||||||
     0 CPU 0<-->: C :     0 CPU 0<-->: C :    : C :    @ STM @    || System ||
  |->0000000    : T :  |->0000000    : T :    : T :<--->@@@@@     || Memory ||
  |  #######<-->: I :  |  #######<-->: I :    : I :      @@@<-|   ||||||||||||
  |  # ETM #    :::::  |  # PTM #    :::::    :::::       @   |
  |   #####      ^ ^   |   #####      ^ !      ^ !        .   |   |||||||||
  | |->###       | !   | |->###       | !      | !        .   |   || DAP ||
  | |   #        | !   | |   #        | !      | !        .   |   |||||||||
  | |   .        | !   | |   .        | !      | !        .   |      |  |
  | |   .        | !   | |   .        | !      | !        .   |      |  *
  | |   .        | !   | |   .        | !      | !        .   |      | SWD/
  | |   .        | !   | |   .        | !      | !        .   |      | JTAG
  *****************************************************************<-|
 *************************** AMBA Debug APB ************************
  *****************************************************************
   |    .          !         .          !        !        .    |
   |    .          *         .          *        *        .    |
  *****************************************************************
 ******************** Cross Trigger Matrix (CTM) *******************
  *****************************************************************
   |    .     ^              .                            .    |
   |    *     !              *                            *    |
  *****************************************************************
 ****************** AMBA Advanced Trace Bus (ATB) ******************
  *****************************************************************
   |          !                        ===============         |
   |          *                         ===== F =====<---------|
   |   :::::::::                         ==== U ====
   |-->:: CTI ::<!!                       === N ===
   |   :::::::::  !                        == N ==
   |    ^         *                        == E ==
   |    !  &&&&&&&&&       IIIIIII         == L ==
   |------>&& ETB &&<......II     I        =======
   |    !  &&&&&&&&&       II     I           .
   |    !                    I     I          .
   |    !                    I REP I<..........
   |    !                    I     I
   |    !!>&&&&&&&&&       II     I           *Source: ARM ltd.
   |------>& TPIU  &<......II    I            DAP = Debug Access Port
           &&&&&&&&&       IIIIIII            ETM = Embedded Trace Macrocell
               ;                              PTM = Program Trace Macrocell
               ;                              CTI = Cross Trigger Interface
               *                              ETB = Embedded Trace Buffer
          To trace port                       TPIU= Trace Port Interface Unit
                                              SWD = Serial Wire Debug

While on target configuration of the components is done via the APB bus,
all trace data are carried out-of-band on the ATB bus.  The CTM provides
a way to aggregate and distribute signals between CoreSight components.

The coresight framework provides a central point to represent, configure and
manage coresight devices on a platform.  This first implementation centers on
the basic tracing functionality, enabling components such ETM/PTM, funnel,
replicator, TMC, TPIU and ETB.  Future work will enable more
intricate IP blocks such as STM and CTI.

약어·분류와 Device Tree binding

94-138

PTM은 Program Trace Macrocell, ETM은 Embedded Trace Macrocell, STM은 System Trace Macrocell, ETB는 Embedded Trace Buffer, ITM은 Instrumentation Trace Macrocell이다.

TPIU는 Trace Port Interface Unit이다. TMC-ETR은 Embedded Trace Router로 설정된 Trace Memory Controller이고, TMC-ETF는 Embedded Trace FIFO로 설정된 Trace Memory Controller다. CTI는 Cross Trigger Interface다.

source class에는 ETMv3.x, ETMv4, PTMv1.0, PTMv1.1, STM, STM500, ITM이 있다. link에는 intelligent 여부와 관계없는 funnel, replicator와 TMC-ETR이 들어간다. sink에는 ETBv1.0, ETB1.1, TPIU, TMC-ETF가 있고, CTI는 misc로 분류된다.

Device Tree binding 세부 사항은 `Documentation/devicetree/bindings/arm/arm,coresight-*.yaml`을 참조한다.

문서 작성 당시 ITM, STM, CTI driver는 제공되지 않았고 solution이 성숙하면서 추가될 것으로 예상했다. 이는 문서의 작성 시점 설명이며 현재 source와 page의 원문은 그대로 보존한다.

CoreSight 약어
약어이름
PTMProgram Trace Macrocell
ETMEmbedded Trace Macrocell
STMSystem Trace Macrocell
ETBEmbedded Trace Buffer
ITMInstrumentation Trace Macrocell
TPIUTrace Port Interface Unit
TMC-ETRTrace Memory Controller as Embedded Trace Router
TMC-ETFTrace Memory Controller as Embedded Trace FIFO
CTICross Trigger Interface

문서에서 사용하는 주요 IP 이름을 풀어 쓴다.

IP class 예
Class구성요소
SourceETMv3.x/v4, PTMv1.0/v1.1, STM/STM500, ITM
Linkfunnel, replicator, TMC-ETR
SinkETBv1.0/v1.1, TPIU, TMC-ETF
MiscCTI

source, link, sink와 misc 분류를 원문대로 보존한다.

Acronyms and Classification
---------------------------

Acronyms:

PTM:
    Program Trace Macrocell
ETM:
    Embedded Trace Macrocell
STM:
    System trace Macrocell
ETB:
    Embedded Trace Buffer
ITM:
    Instrumentation Trace Macrocell
TPIU:
     Trace Port Interface Unit
TMC-ETR:
        Trace Memory Controller, configured as Embedded Trace Router
TMC-ETF:
        Trace Memory Controller, configured as Embedded Trace FIFO
CTI:
    Cross Trigger Interface

Classification:

Source:
   ETMv3.x ETMv4, PTMv1.0, PTMv1.1, STM, STM500, ITM
Link:
   Funnel, replicator (intelligent or not), TMC-ETR
Sinks:
   ETBv1.0, ETB1.1, TPIU, TMC-ETF
Misc:
   CTI


Device Tree Bindings
--------------------

See ``Documentation/devicetree/bindings/arm/arm,coresight-*.yaml`` for details.

As of this writing drivers for ITM, STMs and CTIs are not provided but are
expected to be added as the solution matures.

Framework API·descriptor와 device naming

139-243

CoreSight compliant device는 올바른 API를 사용하면 framework에 등록할 수 있다. `coresight_register(struct coresight_desc *desc)`는 descriptor를 받아 device를 core framework에 등록하고 `struct coresight_device *`를 반환한다. `coresight_unregister(struct coresight_device *csdev)`는 등록 때 얻은 device reference로 등록을 해제한다.

등록이 성공하면 device는 `/sys/bus/coresight/devices` 아래에 나타난다. TC2 예제에는 replicator, TPIU, ETB, funnel, PTM과 ETM device가 보인다.

`struct coresight_desc`에는 `enum coresight_dev_type type`, `struct coresight_dev_subtype subtype`, `const struct coresight_ops *ops`, `struct coresight_platform_data *pdata`, `struct device *dev`, `const struct attribute_group **groups`가 들어간다.

`coresight_dev_type`은 source, link, sink 중 device의 기본 class를 식별하고 `coresight_dev_subtype`은 그 type을 더 세분한다.

`struct coresight_ops`는 필수이며 framework가 구성요소의 기본 operation을 수행하는 방법을 제공한다. class마다 요구가 달라 `struct coresight_ops_sink`, `struct coresight_ops_link`, `struct coresight_ops_source`가 별도로 제공된다.

driver probe에서 `of_get_coresight_platform_data()`로 `pdata`를 얻는다. `dev`에는 `amba_device`에 포함된 device reference를 넣으며 예제의 `etm_probe()`는 `drvdata->dev = &adev->dev`로 설정한다.

source, link, sink class에는 `struct coresight_ops`가 정의한 generic operation이 있다. `groups`는 해당 구성요소에만 고유한 operation을 위한 sysfs entry 목록이며 implementation-defined customization은 이 entry로 접근하고 제어한다.

초기 device 이름은 AMBA/platform bus의 실제 parent device 이름과 같았다. Linux Open Firmware naming에 따라 base physical address 뒤에 device type을 붙여 `20010000.etf`, `20040000.funnel`, `22040000.etm`처럼 만들었다.

ACPI 지원이 도입되면서 실제 device 이름이 난해해져 type 기반 generic naming scheme이 추가됐다. CPU에 binding된 device는 CPU logical number를 사용하므로 CPU0 ETM은 `etm0`이다.

그 밖의 device는 `<device_type_prefix>N` 형식이다. prefix는 device type에 고유하고 N은 probe 순서에 따라 부여되는 연속 번호다. 예는 `tmc_etf0`, `tmc_etr0`, `funnel0`, `funnel1`이다.

이후 예제에는 old naming과 new naming이 모두 나타날 수 있다. 실제 작업에서는 지정된 system location 아래에 표시되는 이름을 그대로 사용해야 한다.

coresight_desc field
Field의미
typesource/link/sink 기본 class
subtypeclass 세부 유형
ops필수 generic class operation
pdataDevice Tree에서 얻은 platform data
devAMBA/platform device reference
groups구성요소 고유 sysfs attribute group

framework 등록 descriptor의 책임을 정리한다.

Device naming 규칙
대상규칙
Old schemebase physical address + type20010000.etf
CPU-bound devicetype + CPU logical numberetm0
Other devicetype prefix + probe sequence Nfunnel0, tmc_etr0

physical-address 기반 old scheme과 type 기반 new scheme을 비교한다.

Device 등록
Driver probeBuild coresight_desc
of_get_coresight_platform_datapdata
coresight_registercoresight_device
coresight_device/sys/bus/coresight/devices

probe에서 descriptor를 채우고 framework 등록 후 sysfs에 노출한다.

Framework and implementation
----------------------------

The coresight framework provides a central point to represent, configure and
manage coresight devices on a platform.  Any coresight compliant device can
register with the framework for as long as they use the right APIs:

.. c:function:: struct coresight_device *coresight_register(struct coresight_desc *desc);
.. c:function:: void coresight_unregister(struct coresight_device *csdev);

The registering function is taking a ``struct coresight_desc *desc`` and
register the device with the core framework. The unregister function takes
a reference to a ``struct coresight_device *csdev`` obtained at registration time.

If everything goes well during the registration process the new devices will
show up under /sys/bus/coresight/devices, as showns here for a TC2 platform::

    root:~# ls /sys/bus/coresight/devices/
    replicator  20030000.tpiu    2201c000.ptm  2203c000.etm  2203e000.etm
    20010000.etb         20040000.funnel  2201d000.ptm  2203d000.etm
    root:~#

The functions take a ``struct coresight_device``, which looks like this::

    struct coresight_desc {
            enum coresight_dev_type type;
            struct coresight_dev_subtype subtype;
            const struct coresight_ops *ops;
            struct coresight_platform_data *pdata;
            struct device *dev;
            const struct attribute_group **groups;
    };


The "coresight_dev_type" identifies what the device is, i.e, source link or
sink while the "coresight_dev_subtype" will characterise that type further.

The ``struct coresight_ops`` is mandatory and will tell the framework how to
perform base operations related to the components, each component having
a different set of requirement. For that ``struct coresight_ops_sink``,
``struct coresight_ops_link`` and ``struct coresight_ops_source`` have been
provided.

The next field ``struct coresight_platform_data *pdata`` is acquired by calling
``of_get_coresight_platform_data()``, as part of the driver's _probe routine and
``struct device *dev`` gets the device reference embedded in the ``amba_device``::

    static int etm_probe(struct amba_device *adev, const struct amba_id *id)
    {
     ...
     ...
     drvdata->dev = &adev->dev;
     ...
    }

Specific class of device (source, link, or sink) have generic operations
that can be performed on them (see ``struct coresight_ops``). The ``**groups``
is a list of sysfs entries pertaining to operations
specific to that component only.  "Implementation defined" customisations are
expected to be accessed and controlled using those entries.

Device Naming scheme
--------------------

The devices that appear on the "coresight" bus were named the same as their
parent devices, i.e, the real devices that appears on AMBA bus or the platform bus.
Thus the names were based on the Linux Open Firmware layer naming convention,
which follows the base physical address of the device followed by the device
type. e.g::

    root:~# ls /sys/bus/coresight/devices/
     20010000.etf  20040000.funnel      20100000.stm     22040000.etm
     22140000.etm  230c0000.funnel      23240000.etm     20030000.tpiu
     20070000.etr  20120000.replicator  220c0000.funnel
     23040000.etm  23140000.etm         23340000.etm

However, with the introduction of ACPI support, the names of the real
devices are a bit cryptic and non-obvious. Thus, a new naming scheme was
introduced to use more generic names based on the type of the device. The
following rules apply::

  1) Devices that are bound to CPUs, are named based on the CPU logical
     number.

     e.g, ETM bound to CPU0 is named "etm0"

  2) All other devices follow a pattern, "<device_type_prefix>N", where :

	<device_type_prefix> 	- A prefix specific to the type of the device
	N			- a sequential number assigned based on the order
				  of probing.

	e.g, tmc_etf0, tmc_etr0, funnel0, funnel1

Thus, with the new scheme the devices could appear as ::

    root:~# ls /sys/bus/coresight/devices/
     etm0     etm1     etm2         etm3  etm4      etm5      funnel0
     funnel1  funnel2  replicator0  stm0  tmc_etf0  tmc_etr0  tpiu0

Some of the examples below might refer to old naming scheme and some
to the newer scheme, to give a confirmation that what you see on your
system is not unexpected. One must use the "names" as they appear on
the system under specified locations.

connections directory로 trace topology 탐색

244-329

각 CoreSight component의 `connections` directory에는 다른 CoreSight component로 가는 link가 있다. 이를 통해 trace topology를 탐색하고 큰 system에서 특정 source에 가장 적절한 sink를 정할 수 있다. CTI가 어느 component에 연결됐는지도 확인할 수 있으며 `nr_links`는 directory의 link 수를 나타낸다.

Juno platform의 `etm0` 예제에서 `connections`는 `cti_cpu0` 연결과 output port 0에서 `funnel2`로 가는 link를 보여 준다.

`funnel2`는 `etm0`, `etm3`, `etm4`, `etm5`를 input 0-3으로 받아 output 0으로 `funnel0`에 보낸다. `funnel0`은 `funnel1`과 `funnel2`를 받아 `tmc_etf0`으로 보낸다.

첫 sink인 `tmc_etf0`은 data를 직접 수집하는 sink로도, 뒤쪽 chain으로 전달하는 link로도 사용할 수 있다. 예제에서는 `cti_sys0`과 연결되고 `funnel0`에서 입력받아 `funnel4`로 출력한다.

`funnel4`는 `tmc_etf0`과 `tmc_etf1`을 합쳐 `replicator0`으로 보낸다. `replicator0`은 stream을 output 0의 `tpiu0`과 output 1의 `tmc_etr0`으로 복제한다. 최종 sink `tmc_etr0`은 `cti_sys0`과 연결되고 replicator input을 받는다.

sysfs를 사용할 때는 sink와 source만 enable하면 된다. framework가 필요한 모든 intermediate link를 올바르게 enable한다.

`cti_sys0`은 여러 connections 목록에 나타난다. CTI는 여러 device에 연결할 수 있고 CTM을 중심으로 star topology를 이룬다. 예제의 `cti_sys0`은 STM0, TMC-ETF0, TMC-ETR0, TPIU0 네 component와 연결된다. 자세한 내용은 `Documentation/trace/coresight/coresight-ect.rst`를 참조한다.

Juno etm0 trace path
etm0funnel2 in:0
etm3/4/5funnel2 in:1-3
funnel2funnel0
funnel1funnel0
funnel0tmc_etf0
tmc_etf0funnel4
tmc_etf1funnel4
funnel4replicator0
replicator0 out:0tpiu0
replicator0 out:1tmc_etr0

connections link를 따라 source에서 두 최종 sink까지 이동한다.

CTI star topology
cti_sys0stm0
cti_sys0tmc_etf0
cti_sys0tmc_etr0
cti_sys0tpiu0

cti_sys0가 CTM trigger network를 통해 여러 component와 연결된다.

Topology directory entry
Entry의미
in:Ncomponent input port N
out:Ncomponent output port N
cti_* 또는 device 이름비-data-path component association
nr_linksdirectory link 총수

port link와 CTI association을 같은 connections directory에서 탐색한다.

Topology Representation
-----------------------

Each CoreSight component has a ``connections`` directory which will contain
links to other CoreSight components. This allows the user to explore the trace
topology and for larger systems, determine the most appropriate sink for a
given source. The connection information can also be used to establish
which CTI devices are connected to a given component. This directory contains a
``nr_links`` attribute detailing the number of links in the directory.

For an ETM source, in this case ``etm0`` on a Juno platform, a typical
arrangement will be::

  linaro-developer:~# ls - l /sys/bus/coresight/devices/etm0/connections
  <file details>  cti_cpu0 -> ../../../23020000.cti/cti_cpu0
  <file details>  nr_links
  <file details>  out:0 -> ../../../230c0000.funnel/funnel2

Following the out port to ``funnel2``::

  linaro-developer:~# ls -l /sys/bus/coresight/devices/funnel2/connections
  <file details> in:0 -> ../../../23040000.etm/etm0
  <file details> in:1 -> ../../../23140000.etm/etm3
  <file details> in:2 -> ../../../23240000.etm/etm4
  <file details> in:3 -> ../../../23340000.etm/etm5
  <file details> nr_links
  <file details> out:0 -> ../../../20040000.funnel/funnel0

And again to ``funnel0``::

  linaro-developer:~# ls -l /sys/bus/coresight/devices/funnel0/connections
  <file details> in:0 -> ../../../220c0000.funnel/funnel1
  <file details> in:1 -> ../../../230c0000.funnel/funnel2
  <file details> nr_links
  <file details> out:0 -> ../../../20010000.etf/tmc_etf0

Finding the first sink ``tmc_etf0``. This can be used to collect data
as a sink, or as a link to propagate further along the chain::

  linaro-developer:~# ls -l /sys/bus/coresight/devices/tmc_etf0/connections
  <file details> cti_sys0 -> ../../../20020000.cti/cti_sys0
  <file details> in:0 -> ../../../20040000.funnel/funnel0
  <file details> nr_links
  <file details> out:0 -> ../../../20150000.funnel/funnel4

via ``funnel4``::

  linaro-developer:~# ls -l /sys/bus/coresight/devices/funnel4/connections
  <file details> in:0 -> ../../../20010000.etf/tmc_etf0
  <file details> in:1 -> ../../../20140000.etf/tmc_etf1
  <file details> nr_links
  <file details> out:0 -> ../../../20120000.replicator/replicator0

and a ``replicator0``::

  linaro-developer:~# ls -l /sys/bus/coresight/devices/replicator0/connections
  <file details> in:0 -> ../../../20150000.funnel/funnel4
  <file details> nr_links
  <file details> out:0 -> ../../../20030000.tpiu/tpiu0
  <file details> out:1 -> ../../../20070000.etr/tmc_etr0

Arriving at the final sink in the chain, ``tmc_etr0``::

  linaro-developer:~# ls -l /sys/bus/coresight/devices/tmc_etr0/connections
  <file details> cti_sys0 -> ../../../20020000.cti/cti_sys0
  <file details> in:0 -> ../../../20120000.replicator/replicator0
  <file details> nr_links

As described below, when using sysfs it is sufficient to enable a sink and
a source for successful trace. The framework will correctly enable all
intermediate links as required.

Note: ``cti_sys0`` appears in two of the connections lists above.
CTIs can connect to multiple devices and are arranged in a star topology
via the CTM. See (Documentation/trace/coresight/coresight-ect.rst)
[#fourth]_ for further details.
Looking at this device we see 4 connections::

  linaro-developer:~# ls -l /sys/bus/coresight/devices/cti_sys0/connections
  <file details> nr_links
  <file details> stm0 -> ../../../20100000.stm/stm0
  <file details> tmc_etf0 -> ../../../20010000.etf/tmc_etf0
  <file details> tmc_etr0 -> ../../../20070000.etr/tmc_etr0
  <file details> tpiu0 -> ../../../20030000.tpiu/tpiu0

sysfs로 sink·source enable과 ETB 수집

330-449

CoreSight framework는 Perf command-line tool 또는 CoreSight device sysfs interface 두 방식으로 사용할 수 있다. sysfs 방식은 CoreSight hardware에 대한 깊은 이해가 필요하므로 Perf 방식이 권장된다.

trace 수집 전에 sink를 선택해야 한다. 동시에 enable할 수 있는 sink나 source 수에는 제한이 없다. sink class device에는 공통 sysfs enable entry가 있으며 예제에서는 `20010000.etb/enable_sink`에 1을 써 ETB sink를 enable하고 읽어서 상태 1을 확인한다.

boot 시점의 etm3x driver는 첫 address comparator에 `_stext`와 `_etext`를 설정해 그 범위의 instruction을 trace한다. 따라서 source를 enable하면 즉시 trace capture가 시작된다.

예제는 `2201c000.ptm/enable_source`에 1을 쓴 뒤 ETB status를 읽는다. depth는 `0x2000`, control은 `0x1`이고 RAM write pointer `0x19d3`가 움직이는 것이 실제 data 수집의 표식이다.

trace 수집을 멈출 때는 같은 source `enable_source`에 0을 쓴다.

ETB buffer 내용은 `/dev/20010000.etb`에서 `dd`로 직접 읽어 `cstrace.bin`에 저장할 수 있다. 예제는 32768 byte를 복사한다. 이 file은 `ptm2human`, DS-5 또는 Trace32로 decompress할 수 있다.

DS-5 예제는 변수를 특정 값까지 증가시키는 loop의 instruction trace다. `PUSH`, stack 조정, 변수 load/store, compare, increment, 조건 branch가 여러 차례 반복되고 중간 timestamp가 기록된다. 끝에는 context 관련 register load와 function call, stack 복구, return과 마지막 timestamp가 보인다.

단순한 loop지만 각 instruction address, opcode, branch 실행 여부와 timestamp를 복원할 수 있어 CoreSight가 제공하는 분석 가능성을 보여 준다.

sysfs trace 수집
Select ETB/TMC sinkecho 1 > enable_sink
echo 1 > source/enable_sourceTrace capture
RAM write pointer movesecho 0 > enable_source
dd from /dev sinkcstrace.bin
ptm2human / DS-5 / Trace32Decoded instructions

sink를 먼저 enable한 뒤 source를 시작하고 buffer를 file로 회수한다.

ETB status 예제
Field예제해석
Depth0x2000buffer depth
Status0x1sink status
RAM read ptr0x0현재 read pointer
RAM write ptr0x19d3움직이면 trace가 유입 중
Control0x1capture enabled

source enable 뒤 sink 상태에서 수집 진행을 확인한다.

두 사용 방식
방식장점요구 지식
Perfprocess scheduling과 PMU abstraction 연동일반 profiling 수준
sysfscomponent를 직접 세밀하게 제어CoreSight hardware topology 이해

일반 사용자는 Perf를 우선하고 hardware 실험은 sysfs를 사용한다.

How to use the tracer modules
-----------------------------

There are two ways to use the Coresight framework:

1. using the perf cmd line tools.
2. interacting directly with the Coresight devices using the sysFS interface.

Preference is given to the former as using the sysFS interface
requires a deep understanding of the Coresight HW.  The following sections
provide details on using both methods.

Using the sysFS interface
~~~~~~~~~~~~~~~~~~~~~~~~~

Before trace collection can start, a coresight sink needs to be identified.
There is no limit on the amount of sinks (nor sources) that can be enabled at
any given moment.  As a generic operation, all device pertaining to the sink
class will have an "active" entry in sysfs::

    root:/sys/bus/coresight/devices# ls
    replicator  20030000.tpiu    2201c000.ptm  2203c000.etm  2203e000.etm
    20010000.etb         20040000.funnel  2201d000.ptm  2203d000.etm
    root:/sys/bus/coresight/devices# ls 20010000.etb
    enable_sink  status  trigger_cntr
    root:/sys/bus/coresight/devices# echo 1 > 20010000.etb/enable_sink
    root:/sys/bus/coresight/devices# cat 20010000.etb/enable_sink
    1
    root:/sys/bus/coresight/devices#

At boot time the current etm3x driver will configure the first address
comparator with "_stext" and "_etext", essentially tracing any instruction
that falls within that range.  As such "enabling" a source will immediately
trigger a trace capture::

    root:/sys/bus/coresight/devices# echo 1 > 2201c000.ptm/enable_source
    root:/sys/bus/coresight/devices# cat 2201c000.ptm/enable_source
    1
    root:/sys/bus/coresight/devices# cat 20010000.etb/status
    Depth:          0x2000
    Status:         0x1
    RAM read ptr:   0x0
    RAM wrt ptr:    0x19d3   <----- The write pointer is moving
    Trigger cnt:    0x0
    Control:        0x1
    Flush status:   0x0
    Flush ctrl:     0x2001
    root:/sys/bus/coresight/devices#

Trace collection is stopped the same way::

    root:/sys/bus/coresight/devices# echo 0 > 2201c000.ptm/enable_source
    root:/sys/bus/coresight/devices#

The content of the ETB buffer can be harvested directly from /dev::

    root:/sys/bus/coresight/devices# dd if=/dev/20010000.etb \
    of=~/cstrace.bin
    64+0 records in
    64+0 records out
    32768 bytes (33 kB) copied, 0.00125258 s, 26.2 MB/s
    root:/sys/bus/coresight/devices#

The file cstrace.bin can be decompressed using "ptm2human", DS-5 or Trace32.

Following is a DS-5 output of an experimental loop that increments a variable up
to a certain value.  The example is simple and yet provides a glimpse of the
wealth of possibilities that coresight provides.
::

    Info                                    Tracing enabled
    Instruction     106378866       0x8026B53C      E52DE004        false   PUSH     {lr}
    Instruction     0       0x8026B540      E24DD00C        false   SUB      sp,sp,#0xc
    Instruction     0       0x8026B544      E3A03000        false   MOV      r3,#0
    Instruction     0       0x8026B548      E58D3004        false   STR      r3,[sp,#4]
    Instruction     0       0x8026B54C      E59D3004        false   LDR      r3,[sp,#4]
    Instruction     0       0x8026B550      E3530004        false   CMP      r3,#4
    Instruction     0       0x8026B554      E2833001        false   ADD      r3,r3,#1
    Instruction     0       0x8026B558      E58D3004        false   STR      r3,[sp,#4]
    Instruction     0       0x8026B55C      DAFFFFFA        true    BLE      {pc}-0x10 ; 0x8026b54c
    Timestamp                                       Timestamp: 17106715833
    Instruction     319     0x8026B54C      E59D3004        false   LDR      r3,[sp,#4]
    Instruction     0       0x8026B550      E3530004        false   CMP      r3,#4
    Instruction     0       0x8026B554      E2833001        false   ADD      r3,r3,#1
    Instruction     0       0x8026B558      E58D3004        false   STR      r3,[sp,#4]
    Instruction     0       0x8026B55C      DAFFFFFA        true    BLE      {pc}-0x10 ; 0x8026b54c
    Instruction     9       0x8026B54C      E59D3004        false   LDR      r3,[sp,#4]
    Instruction     0       0x8026B550      E3530004        false   CMP      r3,#4
    Instruction     0       0x8026B554      E2833001        false   ADD      r3,r3,#1
    Instruction     0       0x8026B558      E58D3004        false   STR      r3,[sp,#4]
    Instruction     0       0x8026B55C      DAFFFFFA        true    BLE      {pc}-0x10 ; 0x8026b54c
    Instruction     7       0x8026B54C      E59D3004        false   LDR      r3,[sp,#4]
    Instruction     0       0x8026B550      E3530004        false   CMP      r3,#4
    Instruction     0       0x8026B554      E2833001        false   ADD      r3,r3,#1
    Instruction     0       0x8026B558      E58D3004        false   STR      r3,[sp,#4]
    Instruction     0       0x8026B55C      DAFFFFFA        true    BLE      {pc}-0x10 ; 0x8026b54c
    Instruction     7       0x8026B54C      E59D3004        false   LDR      r3,[sp,#4]
    Instruction     0       0x8026B550      E3530004        false   CMP      r3,#4
    Instruction     0       0x8026B554      E2833001        false   ADD      r3,r3,#1
    Instruction     0       0x8026B558      E58D3004        false   STR      r3,[sp,#4]
    Instruction     0       0x8026B55C      DAFFFFFA        true    BLE      {pc}-0x10 ; 0x8026b54c
    Instruction     10      0x8026B54C      E59D3004        false   LDR      r3,[sp,#4]
    Instruction     0       0x8026B550      E3530004        false   CMP      r3,#4
    Instruction     0       0x8026B554      E2833001        false   ADD      r3,r3,#1
    Instruction     0       0x8026B558      E58D3004        false   STR      r3,[sp,#4]
    Instruction     0       0x8026B55C      DAFFFFFA        true    BLE      {pc}-0x10 ; 0x8026b54c
    Instruction     6       0x8026B560      EE1D3F30        false   MRC      p15,#0x0,r3,c13,c0,#1
    Instruction     0       0x8026B564      E1A0100D        false   MOV      r1,sp
    Instruction     0       0x8026B568      E3C12D7F        false   BIC      r2,r1,#0x1fc0
    Instruction     0       0x8026B56C      E3C2203F        false   BIC      r2,r2,#0x3f
    Instruction     0       0x8026B570      E59D1004        false   LDR      r1,[sp,#4]
    Instruction     0       0x8026B574      E59F0010        false   LDR      r0,[pc,#16] ; [0x8026B58C] = 0x80550368
    Instruction     0       0x8026B578      E592200C        false   LDR      r2,[r2,#0xc]
    Instruction     0       0x8026B57C      E59221D0        false   LDR      r2,[r2,#0x1d0]
    Instruction     0       0x8026B580      EB07A4CF        true    BL       {pc}+0x1e9344 ; 0x804548c4
    Info                                    Tracing enabled
    Instruction     13570831        0x8026B584      E28DD00C        false   ADD      sp,sp,#0xc
    Instruction     0       0x8026B588      E8BD8000        true    LDM      sp!,{pc}
    Timestamp                                       Timestamp: 17107041535

Perf PMU abstraction과 sink 선택

450-493

CoreSight tracer는 Perf framework에서 Performance Monitoring Unit(PMU) abstraction으로 표현된다. Perf는 관심 process가 schedule되는 시점에 맞춰 tracing enable 시점을 제어한다.

system에 CoreSight PMU가 구성되면 `perf list pmu` 결과에 `cs_etm//` kernel PMU event가 나타난다. tracer 수가 보통 processor core 수만큼 있더라도 `cs_etm` PMU는 한 번만 표시된다.

CoreSight PMU는 다른 PMU와 같은 형식을 사용한다. PMU 이름 뒤 slash `/.../` 안에 configuration option을 넣는다.

Perf는 적절한 sink를 자동 선택하지만 보통 sink가 여러 개이므로 `@` prefix의 특별 configuration으로 이름을 지정할 수 있다.

사용 가능한 sink는 `$SYSFS/bus/event_source/devices/cs_etm/sinks/`에 나열된다. 예제는 `tmc_etf0`, `tmc_etr0`, `tpiu0`을 보여 주며 `perf record -e cs_etm/@tmc_etr0/u --per-thread program`으로 TMC-ETR0을 명시한다.

추가 Perf CoreSight 예제는 OpenCSD GitHub repository의 `HOWTO.md`를 참조한다.

Perf 자동 제어
Process scheduledPerf cs_etm PMU
Perf cs_etm PMUEnable relevant CPU tracer
Trace streamAuto-selected or @named sink

process scheduling에 맞춰 하나의 cs_etm PMU가 실제 CPU tracer를 제어한다.

Sink selection
항목
Sink directory/sys/bus/event_source/devices/cs_etm/sinks
자동 선택cs_etm//
명시 선택cs_etm/@tmc_etr0/

sysfs 목록과 Perf event syntax를 연결한다.

Using perf framework
~~~~~~~~~~~~~~~~~~~~

Coresight tracers are represented using the Perf framework's Performance
Monitoring Unit (PMU) abstraction.  As such the perf framework takes charge of
controlling when tracing gets enabled based on when the process of interest is
scheduled.  When configured in a system, Coresight PMUs will be listed when
queried by the perf command line tool:

	linaro@linaro-nano:~$ ./perf list pmu

		List of pre-defined events (to be used in -e):

		cs_etm//                                    [Kernel PMU event]

Regardless of the number of tracers available in a system (usually equal to the
amount of processor cores), the "cs_etm" PMU will be listed only once.

A Coresight PMU works the same way as any other PMU, i.e the name of the PMU is
provided along with configuration options within forward slashes '/' (see
`Config option formats`_).

Advanced Perf framework usage
-----------------------------

Sink selection
~~~~~~~~~~~~~~

An appropriate sink will be selected automatically for use with Perf, but since
there will typically be more than one sink, the name of the sink to use may be
specified as a special config option prefixed with '@'.

The available sinks are listed in sysFS under
($SYSFS)/bus/event_source/devices/cs_etm/sinks/::

	root@localhost:/sys/bus/event_source/devices/cs_etm/sinks# ls
	tmc_etf0  tmc_etr0  tpiu0

	root@linaro-nano:~# perf record -e cs_etm/@tmc_etr0/u --per-thread program

More information on the above and other example on how to use Coresight with
the perf tools can be found in the "HOWTO.md" file of the openCSD gitHub
repository [#third]_.

Instruction 합성·PID trace와 AutoFDO

494-578

Perf는 program trace를 기록하고 분석할 수 있다. `perf record`에서 `cs_etm` event와 sink를 지정해 실행을 기록한다. `perf report`와 `perf script`는 instruction trace에서 instruction 및 branch event를 합성해 실행을 분석한다.

`perf inject`는 trace data를 합성 event로 바꿀 수 있고 `--itrace`가 event type과 frequency를 제어한다. 이 문서 시점에는 64-bit program만 지원하며 32-bit Arm instruction decode에는 추가 작업이 필요하다.

kernel은 PID를 PE ContextID register에 쓸 수 있다. EL1에서 실행하는 kernel은 `CONTEXTIDR_EL1`에 PID를 저장한다. Arm VHE를 구현해 kernel이 virtualization host로 EL2에서 실행하면 `CONTEXTIDR_EL2`에 저장한다.

Perf PMU format은 ETM이 이 값을 trace data에 넣도록 programming한다. `contextid1`은 EL1과 EL2 kernel 모두에서 사용할 수 있다. EL1에서는 kernel PID trace를 enable하고 EL2에서는 guest application PID를 trace한다.

`contextid2`는 kernel이 EL2에서 실행할 때만 쓸 수 있고 EL2 kernel PID tracing을 enable한다. `contextid`는 현재 kernel EL의 PID trace option alias로 EL1에서는 `contextid1`, EL2에서는 `contextid2`와 같다.

Perf는 관련 EL의 PID tracing을 위해 `contextid`를 자동 enable한다. EL2에서는 host와 guest PID를 모두 trace하려면 `contextid1`과 `contextid2`를 동시에 설정할 수 있으며 예제는 `perf record -e cs_etm/contextid1,contextid2/u -- vm`이다.

Feedback Directed Optimization용 coverage를 만들 때 `perf inject --itrace --strip`은 원 trace data를 제거하고 합성 event로 대체한다.

AutoFDO 예제는 `autofdo`와 GCC 5가 필요하다. 먼저 bubble sort를 `-O3`로 build해 CPU 2에서 실행하면 5910 ms다. CoreSight recording을 추가한 실행은 12543 ms이고 69.640 MB `perf.data`를 기록한다.

이어서 `perf inject --itrace=il64 --strip`으로 `inj.data`를 만들고 `create_gcov`로 `sort.gcov` profile을 생성한다. `-fauto-profile=sort.gcov`로 다시 build한 `sort_autofdo` 실행은 예제에서 5806 ms다.

PID PMU format
OptionEL1 kernelEL2 kernel
contextid1kernel PIDguest application PID
contextid2사용 불가EL2 host kernel PID
contextidcontextid1 aliascontextid2 alias

kernel 실행 EL에 따라 host와 guest ContextID source를 선택한다.

AutoFDO workflow
perf record cs_etmperf.data
perf inject --itrace --stripinj.data synthesized events
create_gcovsort.gcov
gcc -fauto-profileOptimized binary

ETM trace를 합성 event와 gcov profile로 변환해 feedback build에 사용한다.

AutoFDO 예제 시간
실행시간
기본 -O35910 ms
CoreSight recording 포함12543 ms
AutoFDO 적용5806 ms

원문 bubble sort 예제의 측정값을 보존한다.

AutoFDO analysis using the perf tools
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

perf can be used to record and analyze trace of programs.

Execution can be recorded using 'perf record' with the cs_etm event,
specifying the name of the sink to record to, e.g::

    perf record -e cs_etm//u --per-thread

The 'perf report' and 'perf script' commands can be used to analyze execution,
synthesizing instruction and branch events from the instruction trace.
'perf inject' can be used to replace the trace data with the synthesized events.
The --itrace option controls the type and frequency of synthesized events
(see perf documentation).

Note that only 64-bit programs are currently supported - further work is
required to support instruction decode of 32-bit Arm programs.

Tracing PID
~~~~~~~~~~~

The kernel can be built to write the PID value into the PE ContextID registers.
For a kernel running at EL1, the PID is stored in CONTEXTIDR_EL1.  A PE may
implement Arm Virtualization Host Extensions (VHE), which the kernel can
run at EL2 as a virtualisation host; in this case, the PID value is stored in
CONTEXTIDR_EL2.

perf provides PMU formats that program the ETM to insert these values into the
trace data; the PMU formats are defined as below:

  "contextid1": Available on both EL1 kernel and EL2 kernel.  When the
                kernel is running at EL1, "contextid1" enables the PID
                tracing; when the kernel is running at EL2, this enables
                tracing the PID of guest applications.

  "contextid2": Only usable when the kernel is running at EL2.  When
                selected, enables PID tracing on EL2 kernel.

  "contextid":  Will be an alias for the option that enables PID
                tracing.  I.e,
                contextid == contextid1, on EL1 kernel.
                contextid == contextid2, on EL2 kernel.

perf will always enable PID tracing at the relevant EL, this is accomplished by
automatically enable the "contextid" config - but for EL2 it is possible to make
specific adjustments using configs "contextid1" and "contextid2", E.g. if a user
wants to trace PIDs for both host and guest, the two configs "contextid1" and
"contextid2" can be set at the same time:

  perf record -e cs_etm/contextid1,contextid2/u -- vm


Generating coverage files for Feedback Directed Optimization: AutoFDO
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'perf inject' accepts the --itrace option in which case tracing data is
removed and replaced with the synthesized events. e.g.
::

	perf inject --itrace --strip -i perf.data -o perf.data.new

Below is an example of using ARM ETM for autoFDO.  It requires autofdo
(https://github.com/google/autofdo) and gcc version 5.  The bubble
sort example is from the AutoFDO tutorial (https://gcc.gnu.org/wiki/AutoFDO/Tutorial).
::

	$ gcc-5 -O3 sort.c -o sort
	$ taskset -c 2 ./sort
	Bubble sorting array of 30000 elements
	5910 ms

	$ perf record -e cs_etm//u --per-thread taskset -c 2 ./sort
	Bubble sorting array of 30000 elements
	12543 ms
	[ perf record: Woken up 35 times to write data ]
	[ perf record: Captured and wrote 69.640 MB perf.data ]

	$ perf inject -i perf.data -o inj.data --itrace=il64 --strip
	$ create_gcov --binary=./sort --profile=inj.data --gcov=sort.gcov -gcov_version=1
	$ gcc-5 -O3 -fauto-profile=sort.gcov sort.c -o sort_autofdo
	$ taskset -c 2 ./sort_autofdo
	Bubble sorting array of 30000 elements
	5806 ms

cs_etm configuration option format

579-622

다음 option은 Perf command line의 `cs_etm/.../` slash 사이에 넣어 기능을 enable한다. 같은 목록은 `/sys/bus/event_source/devices/cs_etm/format/`에도 있다.

`branch_broadcast`는 system-wide `ETM_MODE_BB`의 session-local version이다. `cycacc`, `retstack`, `timestamp`도 각각 `ETMv4_MODE_CYCACC`, `ETM_MODE_RETURNSTACK`, `ETMv4_MODE_TIMESTAMP`의 session-local version이다.

`contextid`, `contextid1`, `contextid2`는 PID tracing option이다.

`configid`는 custom configuration을 선택하는 implementation detail이며 직접 쓰지 않는다. `preset`은 custom configuration parameter를 override한다. 두 option은 `coresight-config.rst`의 Perf configuration 사용법을 참조한다.

`sinkid`는 sink 선택 문자열의 hash이며 `@` notation을 쓰면 자동 설정된다. 내부 implementation detail이므로 직접 사용하지 않는다.

`cc_threshold`는 cycle count threshold다. 값이 없거나 0이면 기본 `0x100`을 사용한다. 입력값이 `TRCIDR3.CCITMIN`이 나타내는 최소 cycle threshold보다 작으면 최소값을 대신 사용한다.

cs_etm format option
Option기능
branch_broadcastsession-local branch broadcast
contextid/contextid1/contextid2PID ContextID trace
configidcustom configuration 선택, 내부용
presetcustom configuration parameter override
sinkid@ sink 이름의 hash, 자동 설정
cycaccsession-local cycle accurate trace
retstacksession-local return stack
timestampsession-local timestamp
cc_thresholdcycle count threshold

session-local trace feature와 내부 selection 값을 구분한다.

cc_threshold 결정
Missing or 0Use 0x100 default
Below TRCIDR3.CCITMINUse hardware minimum
Valid valueUse requested threshold

사용자 값이 없거나 hardware minimum보다 작을 때 안전한 값으로 보정한다.

Config option formats
~~~~~~~~~~~~~~~~~~~~~

The following strings can be provided between // on the perf command line to enable various options.
They are also listed in the folder /sys/bus/event_source/devices/cs_etm/format/

.. list-table::
   :header-rows: 1

   * - Option
     - Description
   * - branch_broadcast
     - Session local version of the system wide setting:
       :ref:`ETM_MODE_BB <coresight-branch-broadcast>`
   * - contextid
     - See `Tracing PID`_
   * - contextid1
     - See `Tracing PID`_
   * - contextid2
     - See `Tracing PID`_
   * - configid
     - Selection for a custom configuration. This is an implementation detail and not used directly,
       see :ref:`trace/coresight/coresight-config:Using Configurations in perf`
   * - preset
     - Override for parameters in a custom configuration, see
       :ref:`trace/coresight/coresight-config:Using Configurations in perf`
   * - sinkid
     - Hashed version of the string to select a sink, automatically set when using the @ notation.
       This is an internal implementation detail and is not used directly, see `Using perf
       framework`_.
   * - cycacc
     - Session local version of the system wide setting: :ref:`ETMv4_MODE_CYCACC
       <coresight-cycle-accurate>`
   * - retstack
     - Session local version of the system wide setting: :ref:`ETM_MODE_RETURNSTACK
       <coresight-return-stack>`
   * - timestamp
     - Session local version of the system wide setting: :ref:`ETMv4_MODE_TIMESTAMP
       <coresight-timestamp>`
   * - cc_threshold
     - Cycle count threshold value. If nothing is provided here or the provided value is 0, then the
       default value i.e 0x100 will be used. If provided value is less than minimum cycles threshold
       value, as indicated via TRCIDR3.CCITMIN, then the minimum value will be used instead.

STM·CTI/CTM과 system configuration manager

623-686

System Trace Macrocell(STM) module 사용법은 다른 tracer와 같다. 차이는 program control flow가 아니라 client가 trace capture를 구동한다는 점이다.

STM tracer의 세부 항목은 sysfs에서 확인하고 각 entry 설명은 `Documentation/ABI/testing/sysfs-bus-coresight-devices-stm`을 참조한다. 예제 `stm0` directory에는 `enable_source`, hardware event와 port select/enable, management, trace ID 등이 있다.

다른 source와 마찬가지로 STM 사용 전에 sink를 선택해 enable하고 STM source를 enable해야 한다. 예제는 `tmc_etf0/enable_sink`과 `stm0/enable_source`에 각각 1을 쓴다.

이후 user-space application은 generic STM API가 제공하는 devfs interface `/dev/stm0`에서 channel을 요청하고 사용할 수 있다. generic API 세부 사항은 `Documentation/trace/stm.rst`에 있다.

CTI는 개별 CTI와 component 사이에 trigger signal 집합을 제공하고 CTM channel을 통해 모든 CTI 사이에 이를 전파한다. 사용법은 `Documentation/trace/coresight/coresight-ect.rst`에 별도로 설명한다.

CoreSight component는 programming option이 많고 system 전체에서 서로 상호작용하도록 설정할 수 있는 복잡한 device다.

CoreSight System Configuration manager는 이러한 복잡한 programming configuration을 선택해 Perf와 sysfs에서 쉽게 사용하도록 한다. 자세한 내용은 `Documentation/trace/coresight/coresight-config.rst`를 참조한다.

문서 footnote는 STM sysfs ABI, generic STM API, Linaro perf-opencsd repository, CTI/CTM 문서, CoreSight configuration 문서의 source path와 URL을 제공한다.

STM trace 경로
User client/dev/stm0 channel
Generic STM APIstm0 source
stm0 sourceCoreSight links
CoreSight linkstmc_etf0 sink

client가 STM channel에 data를 쓰고 CoreSight sink가 stream을 수집한다.

System configuration
Complex component settingsSystem Configuration manager
System Configuration managerPerf configuration
System Configuration managersysfs configuration

복수 component programming을 configuration manager가 Perf와 sysfs에 제공한다.

후속 문서
주제문서
STM sysfs ABIDocumentation/ABI/testing/sysfs-bus-coresight-devices-stm
Generic STM APIDocumentation/trace/stm.rst
Perf OpenCSDgithub.com/Linaro/perf-opencsd
CTI & CTMDocumentation/trace/coresight/coresight-ect.rst
System configurationDocumentation/trace/coresight/coresight-config.rst

각 하위 subsystem의 상세 source path다.

How to use the STM module
-------------------------

Using the System Trace Macrocell module is the same as the tracers - the only
difference is that clients are driving the trace capture rather
than the program flow through the code.

As with any other CoreSight component, specifics about the STM tracer can be
found in sysfs with more information on each entry being found in [#first]_::

    root@genericarmv8:~# ls /sys/bus/coresight/devices/stm0
    enable_source   hwevent_select  port_enable     subsystem       uevent
    hwevent_enable  mgmt            port_select     traceid
    root@genericarmv8:~#

Like any other source a sink needs to be identified and the STM enabled before
being used::

    root@genericarmv8:~# echo 1 > /sys/bus/coresight/devices/tmc_etf0/enable_sink
    root@genericarmv8:~# echo 1 > /sys/bus/coresight/devices/stm0/enable_source

From there user space applications can request and use channels using the devfs
interface provided for that purpose by the generic STM API::

    root@genericarmv8:~# ls -l /dev/stm0
    crw-------    1 root     root       10,  61 Jan  3 18:11 /dev/stm0
    root@genericarmv8:~#

Details on how to use the generic STM API can be found here:
- Documentation/trace/stm.rst [#second]_.

The CTI & CTM Modules
---------------------

The CTI (Cross Trigger Interface) provides a set of trigger signals between
individual CTIs and components, and can propagate these between all CTIs via
channels on the CTM (Cross Trigger Matrix).

A separate documentation file is provided to explain the use of these devices.
(Documentation/trace/coresight/coresight-ect.rst) [#fourth]_.

CoreSight System Configuration
------------------------------

CoreSight components can be complex devices with many programming options.
Furthermore, components can be programmed to interact with each other across the
complete system.

A CoreSight System Configuration manager is provided to allow these complex programming
configurations to be selected and used easily from perf and sysfs.

See the separate document for further information.
(Documentation/trace/coresight/coresight-config.rst) [#fifth]_.


.. [#first] Documentation/ABI/testing/sysfs-bus-coresight-devices-stm

.. [#second] Documentation/trace/stm.rst

.. [#third] https://github.com/Linaro/perf-opencsd

.. [#fourth] Documentation/trace/coresight/coresight-ect.rst

.. [#fifth] Documentation/trace/coresight/coresight-config.rst