9441ec4b621019492e1eb1b4e28567f1e22328fb
[platform/upstream/coreclr.git] / src / vm / eventpipefile.h
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 #ifndef __EVENTPIPE_FILE_H__
6 #define __EVENTPIPE_FILE_H__
7
8 #ifdef FEATURE_PERFTRACING
9
10 #include "eventpipe.h"
11 #include "eventpipeblock.h"
12 #include "eventpipeeventinstance.h"
13 #include "fastserializableobject.h"
14
15 class FastSerializer;
16
17 class EventPipeFile final : public FastSerializableObject
18 {
19 public:
20     EventPipeFile(StreamWriter *pStreamWriter);
21     ~EventPipeFile();
22
23     void WriteEvent(EventPipeEventInstance &instance);
24     void Flush();
25
26     const char *GetTypeName() override
27     {
28         LIMITED_METHOD_CONTRACT;
29         return "Trace";
30     }
31
32     void FastSerialize(FastSerializer *pSerializer) override
33     {
34         CONTRACTL
35         {
36             NOTHROW;
37             GC_NOTRIGGER;
38             MODE_PREEMPTIVE;
39             PRECONDITION(pSerializer != NULL);
40         }
41         CONTRACTL_END;
42
43         pSerializer->WriteBuffer((BYTE *)&m_fileOpenSystemTime, sizeof(m_fileOpenSystemTime));
44         pSerializer->WriteBuffer((BYTE *)&m_fileOpenTimeStamp, sizeof(m_fileOpenTimeStamp));
45         pSerializer->WriteBuffer((BYTE *)&m_timeStampFrequency, sizeof(m_timeStampFrequency));
46
47         // the beginning of V3
48         pSerializer->WriteBuffer((BYTE *)&m_pointerSize, sizeof(m_pointerSize));
49         pSerializer->WriteBuffer((BYTE *)&m_currentProcessId, sizeof(m_currentProcessId));
50         pSerializer->WriteBuffer((BYTE *)&m_numberOfProcessors, sizeof(m_numberOfProcessors));
51         pSerializer->WriteBuffer((BYTE *)&m_samplingRateInNs, sizeof(m_samplingRateInNs));
52     }
53
54 private:
55     void WriteEnd();
56
57     unsigned int GenerateMetadataId();
58
59     unsigned int GetMetadataId(EventPipeEvent &event);
60
61     void SaveMetadataId(EventPipeEvent &event, unsigned int metadataId);
62
63     void WriteToBlock(EventPipeEventInstance &instance, unsigned int metadataId);
64
65     // The object responsible for serialization.
66     FastSerializer *m_pSerializer;
67
68     EventPipeBlock *m_pBlock;
69
70     // The system time when the file was opened.
71     SYSTEMTIME m_fileOpenSystemTime;
72
73     // The timestamp when the file was opened.  Used for calculating file-relative timestamps.
74     LARGE_INTEGER m_fileOpenTimeStamp;
75
76     // The frequency of the timestamps used for this file.
77     LARGE_INTEGER m_timeStampFrequency;
78
79     unsigned int m_pointerSize;
80
81     unsigned int m_currentProcessId;
82
83     unsigned int m_numberOfProcessors;
84
85     unsigned int m_samplingRateInNs;
86
87     // The serialization which is responsible for making sure only a single event
88     // or block of events gets written to the file at once.
89     SpinLock m_serializationLock;
90
91     // Hashtable of metadata labels.
92     MapSHashWithRemove<EventPipeEvent *, unsigned int> *m_pMetadataIds;
93
94     Volatile<LONG> m_metadataIdCounter;
95 };
96
97 #endif // FEATURE_PERFTRACING
98
99 #endif // __EVENTPIPE_FILE_H__