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.
6 #include "eventpipeeventinstance.h"
7 #include "eventpipejsonfile.h"
8 #include "fastserializer.h"
9 #include "sampleprofiler.h"
11 #ifdef FEATURE_PERFTRACING
13 EventPipeEventInstance::EventPipeEventInstance(
14 EventPipeEvent &event,
28 m_debugEventStart = 0xDEADBEEF;
29 m_debugEventEnd = 0xCAFEBABE;
32 m_threadID = threadID;
34 m_dataLength = length;
35 QueryPerformanceCounter(&m_timeStamp);
39 EventPipe::WalkManagedStackForCurrentThread(m_stackContents);
47 StackContents* EventPipeEventInstance::GetStack()
49 LIMITED_METHOD_CONTRACT;
51 return &m_stackContents;
54 EventPipeEvent* EventPipeEventInstance::GetEvent() const
56 LIMITED_METHOD_CONTRACT;
61 LARGE_INTEGER EventPipeEventInstance::GetTimeStamp() const
63 LIMITED_METHOD_CONTRACT;
68 BYTE* EventPipeEventInstance::GetData() const
70 LIMITED_METHOD_CONTRACT;
75 unsigned int EventPipeEventInstance::GetLength() const
77 LIMITED_METHOD_CONTRACT;
82 void EventPipeEventInstance::FastSerialize(FastSerializer *pSerializer, StreamLabel metadataLabel)
93 // Useful for diagnosing serialization bugs.
94 const unsigned int value = 0xDEADBEEF;
95 pSerializer->WriteBuffer((BYTE*)&value, sizeof(value));
98 // Calculate the size of the total payload so that it can be written to the file.
99 unsigned int payloadLength =
100 sizeof(metadataLabel) +
101 sizeof(m_threadID) + // Thread ID
102 sizeof(m_timeStamp) + // TimeStamp
103 m_dataLength + // Event payload data length
104 m_stackContents.GetSize(); // Stack payload size
106 // Write the size of the event to the file.
107 pSerializer->WriteBuffer((BYTE*)&payloadLength, sizeof(payloadLength));
109 // Write the metadata label.
110 pSerializer->WriteBuffer((BYTE*)&metadataLabel, sizeof(metadataLabel));
112 // Write the thread ID.
113 pSerializer->WriteBuffer((BYTE*)&m_threadID, sizeof(m_threadID));
115 // Write the timestamp.
116 pSerializer->WriteBuffer((BYTE*)&m_timeStamp, sizeof(m_timeStamp));
118 // Write the event data payload.
121 pSerializer->WriteBuffer(m_pData, m_dataLength);
124 // Write the stack if present.
125 if(m_stackContents.GetSize() > 0)
127 pSerializer->WriteBuffer(m_stackContents.GetPointer(), m_stackContents.GetSize());
132 void EventPipeEventInstance::SerializeToJsonFile(EventPipeJsonFile *pFile)
149 const unsigned int guidSize = 39;
150 WCHAR wszProviderID[guidSize];
151 if(!StringFromGUID2(m_pEvent->GetProvider()->GetProviderID(), wszProviderID, guidSize))
153 wszProviderID[0] = '\0';
157 StackScratchBuffer scratch;
158 SString guidStr(&wszProviderID[1], guidSize-3);
161 message.Printf("Provider=%s/EventID=%d/Version=%d", guidStr.GetANSI(scratch), m_pEvent->GetEventID(), m_pEvent->GetEventVersion());
162 pFile->WriteEvent(m_timeStamp, m_threadID, message, m_stackContents);
164 EX_CATCH{} EX_END_CATCH(SwallowAllExceptions);
168 void EventPipeEventInstance::SetTimeStamp(LARGE_INTEGER timeStamp)
170 LIMITED_METHOD_CONTRACT;
172 m_timeStamp = timeStamp;
176 bool EventPipeEventInstance::EnsureConsistency()
186 // Validate event start.
187 _ASSERTE(m_debugEventStart == 0xDEADBEEF);
189 // Validate event end.
190 _ASSERTE(m_debugEventEnd == 0xCAFEBABE);
196 SampleProfilerEventInstance::SampleProfilerEventInstance(Thread *pThread)
197 :EventPipeEventInstance(*SampleProfiler::s_pThreadTimeEvent, pThread->GetOSThreadId(), NULL, 0)
199 LIMITED_METHOD_CONTRACT;
202 #endif // FEATURE_PERFTRACING