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// PerfCounters.h
6//
7// Internal Interface for CLR to use Performance counters
8//-----------------------------------------------------------------------------
9
10
11#ifndef _PerfCounters_h_
12#define _PerfCounters_h_
13
14#include "perfcounterdefs.h"
15
16#ifdef ENABLE_PERF_COUNTERS
17//-----------------------------------------------------------------------------
18//-----------------------------------------------------------------------------
19// This code section active iff we're using Perf Counters
20//-----------------------------------------------------------------------------
21//-----------------------------------------------------------------------------
22
23//-----------------------------------------------------------------------------
24// PerfCounter class serves as namespace with data protection.
25// Enforce this by making constructor private
26//-----------------------------------------------------------------------------
27class PerfCounters
28{
29private:
30 PerfCounters();
31
32public:
33 static HRESULT Init();
34 static void Terminate();
35
36 static PerfCounterIPCControlBlock * GetPrivatePerfCounterPtr();
37
38private:
39 static HANDLE m_hPrivateMapPerf;
40
41 static PerfCounterIPCControlBlock * m_pPrivatePerf;
42
43 static BOOL m_fInit;
44
45// Set pointers to garbage so they're never null.
46 static PerfCounterIPCControlBlock m_garbage;
47
48 friend PerfCounterIPCControlBlock & GetPerfCounters();
49};
50
51//-----------------------------------------------------------------------------
52// Utility functions
53//-----------------------------------------------------------------------------
54
55//-----------------------------------------------------------------------------
56// Get the perf counters specific to our process
57//-----------------------------------------------------------------------------
58inline PerfCounterIPCControlBlock & GetPerfCounters()
59{
60 LIMITED_METHOD_CONTRACT;
61
62 return *PerfCounters::m_pPrivatePerf;
63}
64
65inline PerfCounterIPCControlBlock *PerfCounters::GetPrivatePerfCounterPtr()
66{
67 LIMITED_METHOD_CONTRACT;
68
69 return m_pPrivatePerf;
70};
71
72#define COUNTER_ONLY(x) x
73
74#define PERF_COUNTER_NUM_OF_ITERATIONS 10
75
76#if defined(_X86_) && defined(_MSC_VER)
77
78inline UINT64 GetCycleCount_UINT64()
79{
80 LIMITED_METHOD_CONTRACT;
81 return __rdtsc();
82}
83
84#else // defined(_X86_) && defined(_MSC_VER)
85inline UINT64 GetCycleCount_UINT64()
86{
87 LIMITED_METHOD_CONTRACT;
88
89 LARGE_INTEGER qwTmp;
90 QueryPerformanceCounter(&qwTmp);
91 return qwTmp.QuadPart;
92}
93#endif // defined(_X86_) && defined(_MSC_VER)
94
95#define PERF_COUNTER_TIMER_PRECISION UINT64
96#define GET_CYCLE_COUNT GetCycleCount_UINT64
97
98#define PERF_COUNTER_TIMER_START() \
99PERF_COUNTER_TIMER_PRECISION _startPerfCounterTimer = GET_CYCLE_COUNT();
100
101#define PERF_COUNTER_TIMER_STOP(global) \
102global = (GET_CYCLE_COUNT() - _startPerfCounterTimer);
103
104
105
106
107#else // ENABLE_PERF_COUNTERS
108//-----------------------------------------------------------------------------
109//-----------------------------------------------------------------------------
110// This code section active iff we're NOT using Perf Counters
111// Note, not even a class definition, so all usages of PerfCounters in client
112// should be in #ifdef or COUNTER_ONLY().
113//-----------------------------------------------------------------------------
114//-----------------------------------------------------------------------------
115
116#define COUNTER_ONLY(x)
117
118
119#endif // ENABLE_PERF_COUNTERS
120
121
122#endif // _PerfCounters_h_
123