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_BLOCK_H__
6#define __EVENTPIPE_BLOCK_H__
7
8#ifdef FEATURE_PERFTRACING
9
10#include "eventpipeeventinstance.h"
11#include "fastserializableobject.h"
12#include "fastserializer.h"
13
14class EventPipeBlock : public FastSerializableObject
15{
16 public:
17 EventPipeBlock(unsigned int maxBlockSize);
18
19 ~EventPipeBlock();
20
21 // Write an event to the block.
22 // Returns:
23 // - true: The write succeeded.
24 // - false: The write failed. In this case, the block should be considered full.
25 bool WriteEvent(EventPipeEventInstance &instance);
26
27 void Clear();
28
29 const char* GetTypeName()
30 {
31 LIMITED_METHOD_CONTRACT;
32 return "EventBlock";
33 }
34
35 void FastSerialize(FastSerializer *pSerializer)
36 {
37 CONTRACTL
38 {
39 NOTHROW;
40 GC_NOTRIGGER;
41 MODE_PREEMPTIVE;
42 PRECONDITION(pSerializer != NULL);
43 }
44 CONTRACTL_END;
45
46 if (m_pBlock == NULL)
47 {
48 return;
49 }
50
51 unsigned int eventsSize = (unsigned int)(m_pWritePointer - m_pBlock);
52 pSerializer->WriteBuffer((BYTE*)&eventsSize, sizeof(eventsSize));
53
54 if (eventsSize == 0)
55 {
56 return;
57 }
58
59 size_t currentPosition = pSerializer->GetCurrentPosition();
60 if (currentPosition % ALIGNMENT_SIZE != 0)
61 {
62 BYTE maxPadding[ALIGNMENT_SIZE - 1] = {}; // it's longest possible padding, we are going to use only part of it
63 unsigned int paddingLength = ALIGNMENT_SIZE - (currentPosition % ALIGNMENT_SIZE);
64 pSerializer->WriteBuffer(maxPadding, paddingLength); // we write zeros here, the reader is going to always read from the first aligned address of the serialized content
65
66 _ASSERTE(pSerializer->GetCurrentPosition() % ALIGNMENT_SIZE == 0);
67 }
68
69 pSerializer->WriteBuffer(m_pBlock, eventsSize);
70 }
71
72 private:
73 BYTE *m_pBlock;
74 BYTE *m_pWritePointer;
75 BYTE *m_pEndOfTheBuffer;
76
77 unsigned int GetSize() const
78 {
79 LIMITED_METHOD_CONTRACT;
80
81 if (m_pBlock == NULL)
82 {
83 return 0;
84 }
85
86 return (unsigned int)(m_pEndOfTheBuffer - m_pBlock);
87 }
88};
89
90#endif // FEATURE_PERFTRACING
91
92#endif // __EVENTPIPE_BLOCK_H__
93