-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventHeaderEventInfo.cs
357 lines (322 loc) · 13.2 KB
/
EventHeaderEventInfo.cs
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.LinuxTracepoints.Decode
{
using System;
using Debug = System.Diagnostics.Debug;
using Encoding = System.Text.Encoding;
using StringBuilder = System.Text.StringBuilder;
/// <summary>
/// Event attributes returned by the GetEventInfo() method of EventHeaderEnumerator.
/// </summary>
public readonly ref struct EventHeaderEventInfo
{
/// <summary>
/// Initializes a new instance of the EventHeaderEventInfo struct.
/// </summary>
internal EventHeaderEventInfo(
ReadOnlySpan<byte> eventData,
int nameStart,
int nameLength,
int activityIdStart,
int activityIdLength,
string tracepointName,
EventHeader header,
ulong keyword)
{
this.EventData = eventData;
this.NameStart = nameStart;
this.NameLength = nameLength;
this.ActivityIdStart = activityIdStart;
this.ActivityIdLength = activityIdLength;
this.TracepointName = tracepointName;
this.Header = header;
this.Keyword = keyword;
}
/// <summary>
/// The Span corresponding to the EventData parameter passed to
/// EventHeaderEnumerator.StartEvent(). For example, if you called
/// enumerator.StartEvent(name, myData), this will be the same as myData.Span.
/// The NameStart and ActivityIdStart fields are relative to this span.
/// </summary>
public ReadOnlySpan<byte> EventData { get; }
/// <summary>
/// Offset into EventData where NameBytes begins.
/// </summary>
public int NameStart { get; }
/// <summary>
/// Length of NameBytes.
/// </summary>
public int NameLength { get; }
/// <summary>
/// Offset into EventData where ActivityIdBytes begins.
/// </summary>
public int ActivityIdStart { get; }
/// <summary>
/// Length of ActivityIdBytes (may be 0, 16, or 32).
/// </summary>
public int ActivityIdLength { get; }
/// <summary>
/// TracepointName, e.g. "ProviderName_LnKnnnOptions".
/// </summary>
public string TracepointName { get; }
/// <summary>
/// Flags, Version, Id, Tag, Opcode, Level.
/// </summary>
public EventHeader Header { get; }
/// <summary>
/// Event category bits.
/// </summary>
public ulong Keyword { get; }
/// <summary>
/// UTF-8 encoded "EventName" followed by 0 or more field attributes.
/// Each attribute is ";AttribName=AttribValue".
/// EventName should not contain ';'.
/// AttribName should not contain ';' or '='.
/// AttribValue may contain ";;" which should be unescaped to ";".
/// </summary>
public ReadOnlySpan<byte> NameBytes =>
this.EventData.Slice(this.NameStart, this.NameLength);
/// <summary>
/// Gets a new string (decoded from NameBytes) containing
/// "EventName" followed by 0 or more field attributes.
/// Each attribute is ";AttribName=AttribValue".
/// EventName should not contain ';'.
/// AttribName should not contain ';' or '='.
/// AttribValue may contain ";;" which should be unescaped to ";".
/// </summary>
public string NameAsString =>
Encoding.UTF8.GetString(this.EventData.Slice(this.NameStart, this.NameLength));
/// <summary>
/// Gets the chars of ProviderName, i.e. the part of TracepointName
/// before level and keyword, e.g. if TracepointName is
/// "ProviderName_LnKnnnOptions", returns "ProviderName".
/// </summary>
public ReadOnlySpan<char> ProviderName =>
this.TracepointName.AsSpan(0, this.TracepointName.LastIndexOf('_'));
/// <summary>
/// Gets the chars of Options, i.e. the part of TracepointName after
/// level and keyword, e.g. if TracepointName is "ProviderName_LnKnnnOptions",
/// returns "Options".
/// </summary>
public ReadOnlySpan<char> Options
{
get
{
var n = this.TracepointName;
for (var i = n.LastIndexOf('_') + 1; i < n.Length; i += 1)
{
char ch = n[i];
if ('A' <= ch && ch <= 'Z' && ch != 'L' && ch != 'K')
{
return n.AsSpan(i);
}
}
return default;
}
}
/// <summary>
/// Big-endian activity id bytes. 0 bytes for none,
/// 16 bytes for activity id only, 32 bytes for activity id and related id.
/// </summary>
public ReadOnlySpan<byte> ActivityIdBytes =>
this.EventData.Slice(this.ActivityIdStart, this.ActivityIdLength);
/// <summary>
/// 128-bit activity id decoded from ActivityIdBytes, or NULL if no activity id.
/// </summary>
public Guid? ActivityId
{
get
{
Debug.Assert((this.ActivityIdLength & 0xF) == 0);
return this.ActivityIdLength < 16
? new Guid?()
: PerfConvert.ReadGuidBigEndian(this.EventData.Slice(this.ActivityIdStart));
}
}
/// <summary>
/// 128-bit related id decoded from ActivityIdBytes, or NULL if no related id.
/// </summary>
public Guid? RelatedActivityId
{
get
{
Debug.Assert((this.ActivityIdLength & 0xF) == 0);
return this.ActivityIdLength < 32
? new Guid?()
: PerfConvert.ReadGuidBigEndian(this.EventData.Slice(this.ActivityIdStart + 16));
}
}
/// <summary>
/// Returns TracepointName, or "" if none.
/// </summary>
public override string ToString()
{
return this.TracepointName ?? "";
}
/// <summary>
/// <para>
/// Appends the current event identity to the provided StringBuilder as a JSON string,
/// e.g. <c>"MyProvider:MyEvent"</c> (including the quotation marks).
/// </para><para>
/// The event identity includes the provider name and event name, e.g. "MyProvider:MyEvent".
/// This is commonly used as the value of the "n" property in the JSON rendering of the event.
/// </para>
/// </summary>
public void AppendJsonEventIdentityTo(StringBuilder sb)
{
sb.Append('"');
PerfConvert.StringAppendWithControlCharsJsonEscape(sb, this.ProviderName);
sb.Append(':');
PerfConvert.StringAppendWithControlCharsJsonEscape(sb, this.NameBytes, Encoding.UTF8);
sb.Append('"');
}
/// <summary>
/// <para>
/// Appends event metadata to the provided StringBuilder as a comma-separated list
/// of 0 or more JSON name-value pairs, e.g. <c>"level": 5, "keyword": 3</c>
/// (including the quotation marks).
/// </para><para>
/// One name-value pair is appended for each metadata item that is both requested
/// by metaOptions and has a meaningful value available in the event. For example,
/// the "id" metadata item is only appended if the event has a non-zero Id value,
/// even if the metaOptions parameter includes PerfMetaOptions.Id.
/// </para><para>
/// The following metadata items are supported:
/// <list type="bullet"><item>
/// "provider": "MyProviderName"
/// </item><item>
/// "event": "MyEventName"
/// </item><item>
/// "id": 123 (omitted if zero)
/// </item><item>
/// "version": 1 (omitted if zero)
/// </item><item>
/// "level": 5 (omitted if zero)
/// </item><item>
/// "keyword": "0x1" (omitted if zero)
/// </item><item>
/// "opcode": 1 (omitted if zero)
/// </item><item>
/// "tag": "0x123" (omitted if zero)
/// </item><item>
/// "activity": "12345678-1234-1234-1234-1234567890AB" (omitted if not present)
/// </item><item>
/// "relatedActivity": "12345678-1234-1234-1234-1234567890AB" (omitted if not present)
/// </item><item>
/// "options": "Gmygroup" (omitted if not present)
/// </item><item>
/// "flags": "0x7" (omitted if zero)
/// </item></list>
/// </para>
/// </summary>
/// <returns>
/// Returns true if a comma would be needed before subsequent JSON output, i.e. if
/// addCommaBeforeNextItem was true OR if any metadata items were appended.
/// </returns>
public bool AppendJsonEventMetaTo(
StringBuilder sb,
bool addCommaBeforeNextItem,
PerfMetaOptions metaOptions = PerfMetaOptions.Default,
PerfConvertOptions convertOptions = PerfConvertOptions.Default)
{
var w = new JsonWriter(sb, convertOptions, addCommaBeforeNextItem);
int providerNameEnd =
0 != (metaOptions & (PerfMetaOptions.Provider | PerfMetaOptions.Options))
? this.TracepointName.LastIndexOf('_')
: 0;
if (metaOptions.HasFlag(PerfMetaOptions.Provider))
{
PerfConvert.StringAppendJson(
w.WriteValueNoEscapeName("provider"),
this.TracepointName.AsSpan().Slice(0, providerNameEnd));
}
if (metaOptions.HasFlag(PerfMetaOptions.Event))
{
PerfConvert.StringAppendJson(
w.WriteValueNoEscapeName("event"),
this.NameBytes,
Encoding.UTF8);
}
if (metaOptions.HasFlag(PerfMetaOptions.Id) && this.Header.Id != 0)
{
PerfConvert.UInt32DecimalAppend(
w.WriteValueNoEscapeName("id"),
this.Header.Id);
}
if (metaOptions.HasFlag(PerfMetaOptions.Version) && this.Header.Version != 0)
{
PerfConvert.UInt32DecimalAppend(
w.WriteValueNoEscapeName("version"),
this.Header.Version);
}
if (metaOptions.HasFlag(PerfMetaOptions.Level) && this.Header.Level != 0)
{
PerfConvert.UInt32DecimalAppend(
w.WriteValueNoEscapeName("level"),
(byte)this.Header.Level);
}
if (metaOptions.HasFlag(PerfMetaOptions.Keyword) && this.Keyword != 0)
{
PerfConvert.UInt64HexAppendJson(
w.WriteValueNoEscapeName("keyword"),
this.Keyword,
convertOptions);
}
if (metaOptions.HasFlag(PerfMetaOptions.Opcode) && this.Header.Opcode != 0)
{
PerfConvert.UInt32DecimalAppend(
w.WriteValueNoEscapeName("opcode"),
(byte)this.Header.Opcode);
}
if (metaOptions.HasFlag(PerfMetaOptions.Tag) && this.Header.Tag != 0)
{
PerfConvert.UInt32HexAppendJson(
w.WriteValueNoEscapeName("tag"),
this.Header.Tag,
convertOptions);
}
if (metaOptions.HasFlag(PerfMetaOptions.Activity) && this.ActivityIdLength >= 16)
{
w.WriteValueNoEscapeName("activity");
sb.Append('"');
PerfConvert.GuidAppend(
sb,
PerfConvert.ReadGuidBigEndian(this.EventData.Slice(this.ActivityIdStart)));
sb.Append('"');
}
if (metaOptions.HasFlag(PerfMetaOptions.RelatedActivity) && this.ActivityIdLength >= 32)
{
w.WriteValueNoEscapeName("relatedActivity");
sb.Append('"');
PerfConvert.GuidAppend(
sb,
PerfConvert.ReadGuidBigEndian(this.EventData.Slice(this.ActivityIdStart + 16)));
sb.Append('"');
}
if (metaOptions.HasFlag(PerfMetaOptions.Options))
{
var n = this.TracepointName;
for (int i = providerNameEnd + 1; i < n.Length; i += 1)
{
var ch = n[i];
if ('A' <= ch && ch <= 'Z' && ch != 'L' && ch != 'K')
{
PerfConvert.StringAppendJson(
w.WriteValueNoEscapeName("options"),
n.AsSpan(i));
break;
}
}
}
if (metaOptions.HasFlag(PerfMetaOptions.Flags))
{
PerfConvert.UInt32HexAppendJson(
w.WriteValueNoEscapeName("flags"),
(byte)this.Header.Flags,
convertOptions);
}
return w.Comma;
}
}
}