summaryrefslogtreecommitdiff
path: root/src/vm/eventpipefile.cpp
blob: 621a4cd1b878dd2149b51d7d7782fb0725b08085 (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "common.h"
#include "eventpipeblock.h"
#include "eventpipeeventinstance.h"
#include "eventpipefile.h"
#include "sampleprofiler.h"

#ifdef FEATURE_PERFTRACING


StackHashEntry* StackHashEntry::CreateNew(StackContents* pStack, ULONG id, ULONG hash)
{
    LIMITED_METHOD_CONTRACT;

    StackHashEntry* pEntry = (StackHashEntry*) new (nothrow) BYTE[offsetof(StackHashEntry, StackBytes) + pStack->GetSize()];
    if (pEntry == NULL)
    {
        return NULL;
    }
    pEntry->Id = id;
    pEntry->Hash = hash;
    pEntry->StackSizeInBytes = pStack->GetSize();
    memcpy_s(pEntry->StackBytes, pStack->GetSize(), pStack->GetPointer(), pStack->GetSize());
    return pEntry;
}

StackHashKey StackHashEntry::GetKey() const
{
    LIMITED_METHOD_CONTRACT;
    StackHashKey key((BYTE*)StackBytes, StackSizeInBytes, Hash);
    return key;
}

StackHashKey::StackHashKey(StackContents* pStack) :
    pStackBytes(pStack->GetPointer()),
    Hash(HashBytes(pStack->GetPointer(), pStack->GetSize())),
    StackSizeInBytes(pStack->GetSize())
{}

StackHashKey::StackHashKey(BYTE* pStackBytes, ULONG stackSizeInBytes, ULONG hash) :
    pStackBytes(pStackBytes),
    Hash(hash),
    StackSizeInBytes(stackSizeInBytes)
{}

DWORD GetFileVersion(EventPipeSerializationFormat format)
{
    LIMITED_METHOD_CONTRACT;
    switch(format)
    {
    case EventPipeSerializationFormat::NetPerfV3:
        return 3;
    case EventPipeSerializationFormat::NetTraceV4:
        return 4;
    default:
        _ASSERTE(!"Unrecognized EventPipeSerializationFormat");
        return 0;
    }
}

DWORD GetFileMinVersion(EventPipeSerializationFormat format)
{
    LIMITED_METHOD_CONTRACT;
    switch (format)
    {
    case EventPipeSerializationFormat::NetPerfV3:
        return 0;
    case EventPipeSerializationFormat::NetTraceV4:
        return 4;
    default:
        _ASSERTE(!"Unrecognized EventPipeSerializationFormat");
        return 0;
    }
}

EventPipeFile::EventPipeFile(StreamWriter *pStreamWriter, EventPipeSerializationFormat format) :
    FastSerializableObject(GetFileVersion(format), GetFileMinVersion(format), format >= EventPipeSerializationFormat::NetTraceV4)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
    }
    CONTRACTL_END;

    m_format = format;
    m_pBlock = new EventPipeEventBlock(100 * 1024, format);
    m_pMetadataBlock = new EventPipeMetadataBlock(100 * 1024);
    m_pStackBlock = new EventPipeStackBlock(100 * 1024);

    // File start time information.
    GetSystemTime(&m_fileOpenSystemTime);
    QueryPerformanceCounter(&m_fileOpenTimeStamp);
    QueryPerformanceFrequency(&m_timeStampFrequency);

    m_pointerSize = TARGET_POINTER_SIZE;

    m_currentProcessId = GetCurrentProcessId();

    SYSTEM_INFO sysinfo = {};
    GetSystemInfo(&sysinfo);
    m_numberOfProcessors = sysinfo.dwNumberOfProcessors;

    m_samplingRateInNs = SampleProfiler::GetSamplingRate();

    bool fSuccess = true;
    if (m_format >= EventPipeSerializationFormat::NetTraceV4)
    {
        const char* pHeader = "Nettrace";
        uint32_t bytesWritten = 0;
        fSuccess = pStreamWriter->Write(pHeader, 8, bytesWritten) && bytesWritten == 8;
    }
    if (fSuccess)
    {
        // Create the file stream and write the FastSerialization header.
        m_pSerializer = new FastSerializer(pStreamWriter);
    }
    else
    {
        m_pSerializer = nullptr;
    }

    m_serializationLock.Init(LOCK_TYPE_DEFAULT);

    m_pMetadataIds = new MapSHashWithRemove<EventPipeEvent*, unsigned int>();

    // Start at 0 - The value is always incremented prior to use, so the first ID will be 1.
    m_metadataIdCounter = 0;

    // Start at 0 - The value is always incremented prior to use, so the first ID will be 1.
    m_stackIdCounter = 0;

#ifdef DEBUG
    QueryPerformanceCounter(&m_lastSortedTimestamp);
#endif

    // Write the first object to the file.
    m_pSerializer->WriteObject(this);
}

EventPipeFile::~EventPipeFile()
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    if (m_pBlock != NULL && m_pSerializer != NULL)
        WriteEnd();

    for (EventPipeStackHash::Iterator pCur = m_stackHash.Begin(); pCur != m_stackHash.End(); pCur++)
    {
        delete *pCur;
    }

    delete m_pBlock;
    delete m_pMetadataBlock;
    delete m_pStackBlock;
    delete m_pSerializer;
    delete m_pMetadataIds;
}

EventPipeSerializationFormat EventPipeFile::GetSerializationFormat() const
{
    LIMITED_METHOD_CONTRACT;
    return m_format;
}

bool EventPipeFile::HasErrors() const
{
    LIMITED_METHOD_CONTRACT;
    return (m_pSerializer == nullptr) || m_pSerializer->HasWriteErrors();
}

void EventPipeFile::WriteEvent(EventPipeEventInstance &instance, ULONGLONG captureThreadId, unsigned int sequenceNumber, BOOL isSortedEvent)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

#ifdef DEBUG
    _ASSERTE(instance.GetTimeStamp()->QuadPart >= m_lastSortedTimestamp.QuadPart);
    if (isSortedEvent)
    {
        m_lastSortedTimestamp = *(instance.GetTimeStamp());
    }
#endif

    unsigned int stackId = 0;
    if (m_format >= EventPipeSerializationFormat::NetTraceV4)
    {
        stackId = GetStackId(instance);
    }

    // Check to see if we've seen this event type before.
    // If not, then write the event metadata to the event stream first.
    unsigned int metadataId = GetMetadataId(*instance.GetEvent());
    if(metadataId == 0)
    {
        metadataId = GenerateMetadataId();

        EventPipeEventInstance* pMetadataInstance = EventPipe::BuildEventMetadataEvent(instance, metadataId);

        WriteEventToBlock(*pMetadataInstance, 0); // metadataId=0 breaks recursion and represents the metadata event.

        SaveMetadataId(*instance.GetEvent(), metadataId);

        delete[] pMetadataInstance->GetData();
        delete pMetadataInstance;
    }

    WriteEventToBlock(instance, metadataId, captureThreadId, sequenceNumber, stackId, isSortedEvent);
}

void EventPipeFile::WriteSequencePoint(EventPipeSequencePoint* pSequencePoint)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(pSequencePoint != nullptr);
    }
    CONTRACTL_END;

    if (m_format < EventPipeSerializationFormat::NetTraceV4)
    {
        // sequence points aren't used in NetPerf format
        return;
    }

    Flush(FlushAllBlocks);
    EventPipeSequencePointBlock sequencePointBlock(pSequencePoint);
    m_pSerializer->WriteObject(&sequencePointBlock);

    // stack cache resets on sequence points
    m_stackIdCounter = 0;
    for (EventPipeStackHash::Iterator pCur = m_stackHash.Begin(); pCur != m_stackHash.End(); pCur++)
    {
        delete *pCur;
    }
    m_stackHash.RemoveAll();
}

void EventPipeFile::Flush(FlushFlags flags)
{
    // Write existing buffer to the stream/file regardless of whether it is full or not.
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;
    // we write current blocks to the disk, whether they are full or not
    if ((m_pMetadataBlock->GetBytesWritten() != 0) && ((flags & FlushMetadataBlock) != 0))
    {
        _ASSERTE(m_format >= EventPipeSerializationFormat::NetTraceV4);
        m_pSerializer->WriteObject(m_pMetadataBlock);
        m_pMetadataBlock->Clear();
    }
    if ((m_pStackBlock->GetBytesWritten() != 0) && ((flags & FlushStackBlock) != 0))
    {
        _ASSERTE(m_format >= EventPipeSerializationFormat::NetTraceV4);
        m_pSerializer->WriteObject(m_pStackBlock);
        m_pStackBlock->Clear();
    }
    if ((m_pBlock->GetBytesWritten() != 0) && ((flags & FlushEventBlock) != 0))
    {
        m_pSerializer->WriteObject(m_pBlock);
        m_pBlock->Clear();
    }
}

void EventPipeFile::WriteEnd()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    Flush();

    // "After the last EventBlock is emitted, the stream is ended by emitting a NullReference Tag which indicates that there are no more objects in the stream to read."
    // see https://github.com/Microsoft/perfview/blob/master/src/TraceEvent/EventPipe/EventPipeFormat.md for more
    m_pSerializer->WriteTag(FastSerializerTags::NullReference);
}

void EventPipeFile::WriteEventToBlock(EventPipeEventInstance &instance, 
                                      unsigned int metadataId,
                                      ULONGLONG captureThreadId,
                                      unsigned int sequenceNumber,
                                      unsigned int stackId,
                                      BOOL isSortedEvent)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    instance.SetMetadataId(metadataId);

    // If we are flushing events we need to flush metadata and stacks as well
    // to ensure referenced metadata/stacks were written to the file before the
    // event which referenced them.
    FlushFlags flags = FlushAllBlocks;
    EventPipeEventBlockBase* pBlock = m_pBlock;
    if(metadataId == 0 && m_format >= EventPipeSerializationFormat::NetTraceV4)
    {
        flags = FlushMetadataBlock;
        pBlock = m_pMetadataBlock;
    }

    if (pBlock->WriteEvent(instance, captureThreadId, sequenceNumber, stackId, isSortedEvent))
        return; // the block is not full, we added the event and continue

    // we can't write this event to the current block (it's full)
    // so we write what we have in the block to the serializer
    Flush(flags);

    bool result = pBlock->WriteEvent(instance, captureThreadId, sequenceNumber, stackId, isSortedEvent);

    _ASSERTE(result == true); // we should never fail to add event to a clear block (if we do the max size is too small)
}

unsigned int EventPipeFile::GenerateMetadataId()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    // PAL does not support 32 bit InterlockedIncrement, so we are using the LONG version and cast to int
    // https://github.com/dotnet/coreclr/blob/master/src/pal/inc/pal.h#L4159
    // it's ok because the metadataId will never be bigger than 32 bit
    return (unsigned int)InterlockedIncrement(&m_metadataIdCounter);
}

unsigned int EventPipeFile::GetMetadataId(EventPipeEvent &event)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    unsigned int metadataId;
    if(m_pMetadataIds->Lookup(&event, &metadataId))
    {
        _ASSERTE(metadataId != 0);
        return metadataId;
    }

    return 0;
}

void EventPipeFile::SaveMetadataId(EventPipeEvent &event, unsigned int metadataId)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(metadataId > 0);
    }
    CONTRACTL_END;

    // If a pre-existing metadata label exists, remove it.
    unsigned int oldId;
    if(m_pMetadataIds->Lookup(&event, &oldId))
        m_pMetadataIds->Remove(&event);

    // Add the metadata label.
    m_pMetadataIds->Add(&event, metadataId);
}

unsigned int EventPipeFile::GetStackId(EventPipeEventInstance &instance)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(m_format >= EventPipeSerializationFormat::NetTraceV4);
    }
    CONTRACTL_END;

    unsigned int stackId = 0;
    StackHashEntry* pEntry = NULL;
    StackHashKey key(instance.GetStack());
    if (NULL == (pEntry = m_stackHash.Lookup(key)))
    {
        stackId = ++m_stackIdCounter;

        pEntry = StackHashEntry::CreateNew(instance.GetStack(), stackId, key.Hash);
        if (pEntry != NULL)
        {
            EX_TRY
            {
                m_stackHash.Add(pEntry);
            }
            EX_CATCH
            {
            }
            EX_END_CATCH(SwallowAllExceptions);
        }

        if (m_pStackBlock->WriteStack(stackId, instance.GetStack()))
            return stackId;

        // we can't write this stack to the current block (it's full)
        // so we write what we have in the block to the serializer
        Flush(FlushStackBlock);

        bool result = m_pStackBlock->WriteStack(stackId, instance.GetStack());
        _ASSERTE(result == true); // we should never fail to add event to a clear block (if we do the max size is too small)
    }
    else
    {
        stackId = pEntry->Id;
    }

    return stackId;
}

#endif // FEATURE_PERFTRACING