Merge pull request #5525 from pgavlin/gh5484
[platform/upstream/coreclr.git] / Documentation / project-docs / linux-performance-tracing.md
1 Performance Tracing on Linux
2 ============================
3
4 When a performance problem is encountered on Linux, these instructions can be used to gather detailed information about what was happening on the machine at the time of the performance problem.
5
6 #Required Tools#
7 - **perfcollect**: Bash script that automates data collection.
8         - Available at <http://aka.ms/perfcollect>.
9 - **PerfView**: Windows-based performance tool that can also analyze trace files collected with Perfcollect.
10         - Available at <http://aka.ms/perfview>.
11
12 #Preparing Your Machine#
13 Follow these steps to prepare your machine to collect a performance trace.
14
15 1. Download Perfcollect.
16
17         > ```bash
18         > curl -OL http://aka.ms/perfcollect
19         > ```
20
21 2. Make the script executable.
22
23         > ```bash
24         > chmod +x perfcollect
25         > ```
26
27 3. Install tracing prerequisites - these are the actual tracing libraries.  For details on prerequisites, see [below](#prerequisites).
28
29         > ```bash
30         > sudo ./perfcollect install
31         > ```
32
33 #Collecting a Trace#
34 1. Have two shell windows available - one for controlling tracing, referred to as **[Trace]**, and one for running the application, referred to as **[App]**.
35 2. **[App]** Setup the application shell - this enables tracing configuration inside of CoreCLR.
36
37         > ```bash 
38         > export COMPlus_PerfMapEnabled=1
39         > export COMPlus_EnableEventLog=1
40         > ```
41
42 3. **[Trace]** Start collection. 
43
44         > ```bash
45         > sudo ./perfcollect collect sampleTrace
46         > ```
47
48         Expected Output:
49
50         > ```bash
51         > Collection started.  Press CTRL+C to stop.
52         > ```
53
54 4. **[App]** Run the app - let it run as long as you need to in order to capture the performance problem.  Generally, you don't need very long.  As an example, for a CPU investigation, 5-10 seconds of the high CPU situation is usually enough.
55
56         > ```bash
57         > dotnet run
58         > ```
59
60 5. **[Trace]** Stop collection - hit CTRL+C.
61
62         > ```bash
63         > ^C
64         > ...STOPPED.
65         >
66         > Starting post-processing. This may take some time.
67         >
68         > Generating native image symbol files
69         > ...SKIPPED
70         > Saving native symbols
71         > ...FINISHED
72         > Exporting perf.data file
73         > ...FINISHED
74         > Compressing trace files
75         > ...FINISHED
76         > Cleaning up artifacts
77         > ...FINISHED
78         >
79         > Trace saved to sampleTrace.trace.zip
80         > ```
81
82         The compressed trace file is now stored in the current working directory.
83
84 #Viewing a Trace#
85 Traces are best viewed using PerfView on Windows.  Note that we're currently looking into porting the analysis pieces of PerfView to Linux so that the entire investigation can occur on Linux.
86
87 ##Open the Trace File##
88 1. Copy the trace.zip file from Linux to a Windows machine.
89 2. Download PerfView from <http://aka.ms/perfview>.
90 3. Run PerfView.exe
91
92         > ```bash
93         > PerfView.exe <path to trace.zip file>
94         > ```
95
96 ##Select a View##
97 PerfView will display the list of views that are supported based on the data contained in the trace file.
98
99 - For CPU investigations, choose **CPU stacks**.
100 - For very detailed GC information, choose **GCStats**.
101 - For per-process/module/method JIT information, choose **JITStats**.
102 - If there is not a view for the information you need, you can try looking for the events in the raw events view.  Choose **Events**. 
103
104 For more details on how to interpret views in PerfView, see help links in the view itself, or from the main window in PerfView choose **Help->Users Guide**.
105
106 #Extra Information#
107 This information is not strictly required to collect and analyze traces, but is provided for those who are interested.
108
109 ##Prerequisites##
110 Perfcollect will alert users to any prerequisites that are not installed and offer to install them.  Prerequisites can be installed automatically by running:
111
112 >```bash
113 >sudo ./perfcollect install
114 >```
115
116 The current prerequisites are:
117
118 1. perf: Also known as perf_event, the Linux Performance Events sub-system and companion user-mode collection/viewer application.  perf is part of the Linux kernel source, but is not usually installed by default.
119 2. LTTng: Stands for "Linux Tracing Toolkit Next Generation", and is used to capture event data emitted at runtime by CoreCLR.  This data is then used to analyze the behavior of various runtime components such as the GC, JIT and thread pool.