-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataToWriter.cs
More file actions
187 lines (165 loc) · 9.35 KB
/
DataToWriter.cs
File metadata and controls
187 lines (165 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace DecodeSample
{
using Microsoft.LinuxTracepoints.Decode;
using System;
using StringBuilder = System.Text.StringBuilder;
using TextWriter = System.IO.TextWriter;
/// <summary>
/// Simple class illustrating how to read a perf data file and write its
/// contents to a TextWriter.
/// </summary>
internal sealed class DataToWriter : IDisposable
{
private readonly PerfDataFileReader reader = new PerfDataFileReader();
private readonly EventHeaderEnumerator enumerator = new EventHeaderEnumerator();
private readonly StringBuilder scratch = new StringBuilder();
private readonly TextWriter writer;
private readonly bool leaveOpenWriter;
public DataToWriter(TextWriter writer, bool leaveOpen)
{
this.writer = writer;
this.leaveOpenWriter = leaveOpen;
}
public void Dispose()
{
this.reader.Dispose();
if (!this.leaveOpenWriter)
{
this.writer.Dispose();
}
}
public void WritePerfData(string perfDataFilePath)
{
// Open the file. Ask reader to sort the events by timestamp (sorted
// in chunks bounded by the FinishedRound events). The alternative is
// to read the events in the order they appear in the file (slightly
// less overhead in cases where the event order doesn't matter).
if (!this.reader.OpenFile(perfDataFilePath, PerfDataFileEventOrder.Time))
{
this.writer.WriteLine($"OpenFile error: Invalid data");
return;
}
while (true)
{
var result = this.reader.ReadEvent(out var eventBytes);
if (result != PerfDataFileResult.Ok)
{
if (result != PerfDataFileResult.EndOfFile)
{
// Unexpected. This usually means a corrupt file.
this.writer.WriteLine($"ReadEvent error: {result.AsString()}");
}
break; // No more events.
}
if (eventBytes.Header.Type != PerfEventHeaderType.Sample)
{
// Non-sample event, typically information about the system or information
// about the trace itself.
// Event info (timestamp, cpu, pid, etc.) may be available.
PerfNonSampleEventInfo nonSampleEventInfo;
result = this.reader.GetNonSampleEventInfo(eventBytes, out nonSampleEventInfo);
if (result != PerfDataFileResult.Ok && // Success getting event info.
result != PerfDataFileResult.IdNotFound) // Event info not available (common).
{
// Unexpected: error getting event info.
this.writer.WriteLine($"GetNonSampleEventInfo error: {result.AsString()}");
}
this.writer.WriteLine($"NonSample: {eventBytes.Header.Type.AsString()}");
this.writer.WriteLine($" size = {eventBytes.Header.Size}");
if (result == PerfDataFileResult.Ok)
{
// Event info was found. Include it in the output.
this.scratch.Clear();
nonSampleEventInfo.AppendJsonEventMetaTo(this.scratch);
this.writer.WriteLine($" info = {{ {this.scratch} }}");
}
}
else
{
// Sample event, e.g. tracepoint event.
// Event info (timestamp, cpu, pid, etc.) may be available.
PerfSampleEventInfo sampleEventInfo;
result = this.reader.GetSampleEventInfo(eventBytes, out sampleEventInfo);
if (result != PerfDataFileResult.Ok)
{
// Unexpected: error getting event info.
this.writer.WriteLine($"GetSampleEventInfo error: {result.AsString()}");
this.writer.WriteLine($" size = {eventBytes.Header.Size}");
continue; // Usually can't make use of the event without the metadata.
}
this.writer.WriteLine($"Sample: {sampleEventInfo.GetName()}");
this.writer.WriteLine($" size = {eventBytes.Header.Size}");
// Found event info (attributes). Include data from it in the output.
// Will be written to output later, after we know the format of the event.
this.scratch.Clear();
sampleEventInfo.AppendJsonEventMetaTo(this.scratch);
var eventFormat = sampleEventInfo.Format;
if (eventFormat.IsEmpty)
{
// Unexpected: Did not find TraceFS format metadata for this event.
this.writer.WriteLine($" info = {{ {this.scratch} }}");
this.writer.WriteLine($" no format");
}
else if (eventFormat.DecodingStyle != PerfEventDecodingStyle.EventHeader ||
!this.enumerator.StartEvent(sampleEventInfo))
{
// Decode using TraceFS format metadata.
this.writer.WriteLine($" info = {{ {this.scratch} }}");
// Typically the "common" fields are not interesting, so skip them.
var fieldsStart = eventFormat.CommonFieldCount;
var fieldsEnd = eventFormat.Fields.Count;
for (int i = fieldsStart; i < fieldsEnd; i += 1)
{
var fieldFormat = eventFormat.Fields[i];
var fieldValue = fieldFormat.GetFieldValue(sampleEventInfo);
// fieldValue has lots of properties and methods for accessing its data in different
// formats. TraceFS fields are always scalars or arrays of fixed-size elements, so
// the following will work to get the data as a JSON value.
this.scratch.Clear();
fieldValue.AppendJsonTo(this.scratch);
this.writer.WriteLine($" {fieldFormat.Name} = {this.scratch}");
}
}
else
{
// Decode using EventHeader metadata.
// eventInfo has a bunch of information about the event.
// We won't use it in this example, since we get the same information in JSON
// format from AppendJsonEventMetaTo.
var eventInfo = this.enumerator.GetEventInfo();
// Add the EventHeader-specific info.
eventInfo.AppendJsonEventMetaTo(this.scratch, this.scratch.Length != 0);
this.writer.WriteLine($" info = {{ {this.scratch} }}");
// Transition past the initial BeforeFirstItem state.
this.enumerator.MoveNext();
// This will loop once for each top-level item in the event.
while (this.enumerator.State >= EventHeaderEnumeratorState.BeforeFirstItem)
{
var itemInfo = this.enumerator.GetItemInfo(); // Information about the item.
// itemInfo.Value has lots of properties and methods for accessing its data in different
// formats, but they only work for simple values -- scalar, array element, or array of
// fixed-size elements. For complex values such as structs or arrays of variable-size
// elements, you need to use the enumerator to access the sub-items. In this example,
// we use the enumerator to convert the current item to a JSON-formatted string.
// In the case of a simple item, it will be the same as itemInfo.Value.AppendJsonScalarTo().
// In the case of a complex item, it will recursively format the item and its sub-items.
this.scratch.Clear();
this.enumerator.AppendJsonItemToAndMoveNextSibling(
this.scratch,
false,
PerfConvertOptions.Default & ~PerfConvertOptions.RootName); // We don't want a JSON "ItemName": prefix.
this.writer.WriteLine($" {itemInfo.GetNameAsString()} = {this.scratch}");
}
if (this.enumerator.State == EventHeaderEnumeratorState.Error)
{
// Unexpected: Error decoding event.
this.writer.WriteLine($" MoveNext error: {this.enumerator.LastError}");
}
}
}
}
}
}
}