forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfCounterImpl.cpp
More file actions
286 lines (251 loc) · 9.46 KB
/
PerfCounterImpl.cpp
File metadata and controls
286 lines (251 loc) · 9.46 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#ifdef PERF_COUNTERS
// Initialization order
// AB AutoSystemInfo
// AD PerfCounter
// AE PerfCounterSet
// AM Output/Configuration
// AN MemProtectHeap
// AP DbgHelpSymbolManager
// AQ CFGLogger
// AR LeakReport
// AS JavascriptDispatch/RecyclerObjectDumper
// AT HeapAllocator/RecyclerHeuristic
// AU RecyclerWriteBarrierManager
#pragma warning(disable:4075) // initializers put in unrecognized initialization area on purpose
#pragma init_seg(".CRT$XCAD")
#include "Microsoft-Scripting-Jscript9.InternalCounters.h"
namespace PerfCounter
{
class Provider
{
public:
static Provider InternalCounter;
#ifdef ENABLE_COUNTER_NOTIFICATION_CALLBACK
void SetNotificationCallBack(PERFLIBREQUEST pfn) { pfnNotificationCallBack = pfn; }
#endif
private:
Provider(HANDLE& handle);
~Provider();
bool IsInitialized() const { return isInitialized; }
HANDLE GetHandler() { return handle; }
#ifdef ENABLE_COUNTER_NOTIFICATION_CALLBACK
PERFLIBREQUEST pfnNotificationCallBack;
static ULONG WINAPI NotificationCallBack(ULONG RequestCode, PVOID Buffer, ULONG BufferSize);
#endif
HANDLE& handle;
bool isInitialized;
friend class InstanceBase;
friend class Counter;
};
Provider Provider::InternalCounter(JS9InternalCounterProvider);
#ifdef ENABLE_COUNTER_NOTIFICATION_CALLBACK
ULONG WINAPI
Provider::NotificationCallBack(ULONG RequestCode, PVOID Buffer, ULONG BufferSize)
{
if (Provider::InternalCounter.pfnNotificationCallBack != NULL)
{
return Provider::InternalCounter.pfnNotificationCallBack(RequestCode, Buffer, BufferSize);
}
return ERROR_SUCCESS;
}
#endif
Provider::Provider(HANDLE& handle) :
handle(handle), isInitialized(false)
{
PERFLIBREQUEST callback = NULL;
#ifdef ENABLE_COUNTER_NOTIFICATION_CALLBACK
callback = &NotificationCallBack;
#endif
if (ERROR_SUCCESS == CounterInitialize(callback, NULL, NULL, NULL))
{
isInitialized = true;
}
}
Provider::~Provider()
{
if (IsInitialized())
{
CounterCleanup();
}
}
InstanceBase::InstanceBase(Provider& provider, GUID const& guid) : provider(provider), guid(guid), instanceData(NULL)
{
}
InstanceBase::~InstanceBase()
{
if (IsEnabled())
{
::PerfDeleteInstance(provider.GetHandler(), instanceData);
}
}
bool
InstanceBase::IsProviderInitialized() const
{
return provider.IsInitialized();
}
bool
InstanceBase::IsEnabled() const
{
return instanceData != NULL;
}
static const size_t GUID_LEN = 37; // includes null
static const wchar_t s_wszObjectNamePrefix[] = L"jscript9_perf_counter_";
static const size_t OBJECT_NAME_LEN = GUID_LEN + _countof(s_wszObjectNamePrefix) + 11;
static
void GetSharedMemoryObjectName(__inout_ecount(OBJECT_NAME_LEN) wchar_t wszObjectName[OBJECT_NAME_LEN], DWORD pid, GUID const& guid)
{
swprintf_s(wszObjectName, OBJECT_NAME_LEN, L"%s%d_%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
s_wszObjectNamePrefix, pid,
guid.Data1,
guid.Data2,
guid.Data3,
guid.Data4[0], guid.Data4[1],
guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
}
bool
InstanceBase::Initialize(wchar_t const * wszInstanceName, DWORD processId)
{
if (provider.IsInitialized())
{
instanceData = PerfCreateInstance(provider.GetHandler(), &guid,
wszInstanceName, processId);
return instanceData != NULL;
}
return false;
}
DWORD *
InstanceBase::InitializeSharedMemory(DWORD numCounter, HANDLE& handle)
{
Assert(!IsEnabled());
DWORD size = numCounter * sizeof(DWORD);
wchar_t wszObjectName[OBJECT_NAME_LEN];
GetSharedMemoryObjectName(wszObjectName, GetCurrentProcessId(), guid);
handle = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, wszObjectName);
if (handle == NULL)
{
return NULL;
}
DWORD * data = (DWORD *)MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, size);
if (data == NULL)
{
CloseHandle(handle);
handle = NULL;
}
return data;
}
DWORD *
InstanceBase::OpenSharedMemory(__in_ecount(MAX_OBJECT_NAME_PREFIX) wchar_t const wszObjectNamePrefix[MAX_OBJECT_NAME_PREFIX],
DWORD pid, DWORD numCounter, HANDLE& handle)
{
DWORD size = numCounter * sizeof(DWORD);
wchar_t wszObjectName[OBJECT_NAME_LEN];
GetSharedMemoryObjectName(wszObjectName, pid, guid);
wchar_t wszObjectNameFull[MAX_OBJECT_NAME_PREFIX + OBJECT_NAME_LEN];
swprintf_s(wszObjectNameFull, L"%s\\%s", wszObjectNamePrefix, wszObjectName);
handle = ::OpenFileMapping(FILE_MAP_READ, FALSE, wszObjectNameFull);
if (handle == NULL)
{
return NULL;
}
DWORD * data = (DWORD *)MapViewOfFile(handle, FILE_MAP_READ, 0, 0, size);
if (data == NULL)
{
CloseHandle(handle);
handle = NULL;
}
return data;
}
void
InstanceBase::UninitializeSharedMemory(DWORD * data, HANDLE handle)
{
UnmapViewOfFile(data);
CloseHandle(handle);
}
void
Counter::Initialize(InstanceBase& instance, DWORD id, DWORD * count)
{
this->count = count;
if (instance.IsEnabled())
{
::PerfSetCounterRefValue(instance.GetProvider().GetHandler(), instance.GetData(), id, count);
}
}
void
Counter::Uninitialize(InstanceBase& instance, DWORD id)
{
if (instance.IsEnabled())
{
::PerfSetCounterRefValue(instance.GetProvider().GetHandler(), instance.GetData(), id, NULL);
}
}
#define DEFINE_PAGE_ALLOCATOR_COUNTER_ID(type) JS9InternalCounter_PageAllocCounterSet_##type##ReservedSize,
static uint ReservedCounterId[PageAllocatorType_Max + 1] =
{
PAGE_ALLOCATOR_TYPE(DEFINE_PAGE_ALLOCATOR_COUNTER_ID)
JS9InternalCounter_PageAllocCounterSet_TotalReservedSize
};
#undef DEFINE_PAGE_ALLOCATOR_COUNTER_ID
#define DEFINE_PAGE_ALLOCATOR_COUNTER_ID(type) JS9InternalCounter_PageAllocCounterSet_##type##CommittedSize,
static uint CommittedCounterId[PageAllocatorType_Max + 1] =
{
PAGE_ALLOCATOR_TYPE(DEFINE_PAGE_ALLOCATOR_COUNTER_ID)
JS9InternalCounter_PageAllocCounterSet_TotalCommittedSize
};
#undef DEFINE_PAGE_ALLOCATOR_COUNTER_ID
#define DEFINE_PAGE_ALLOCATOR_COUNTER_ID(type) JS9InternalCounter_PageAllocCounterSet_##type##UsedSize,
static uint UsedCounterId[PageAllocatorType_Max + 1] =
{
PAGE_ALLOCATOR_TYPE(DEFINE_PAGE_ALLOCATOR_COUNTER_ID)
JS9InternalCounter_PageAllocCounterSet_TotalUsedSize
};
#undef DEFINE_PAGE_ALLOCATOR_COUNTER_ID
uint
PageAllocatorCounterSetDefinition::GetReservedCounterId(PageAllocatorType type)
{
return ReservedCounterId[type];
}
uint
PageAllocatorCounterSetDefinition::GetCommittedCounterId(PageAllocatorType type)
{
return CommittedCounterId[type];
}
uint
PageAllocatorCounterSetDefinition::GetUsedCounterId(PageAllocatorType type)
{
return UsedCounterId[type];
}
GUID const& PageAllocatorCounterSetDefinition::GetGuid() { return JS9InternalCounter_PageAllocCounterSetGuid; }
Provider& PageAllocatorCounterSetDefinition::GetProvider() { return Provider::InternalCounter; }
GUID const& BasicCounterSetDefinition::GetGuid() { return JS9InternalCounter_BasicCounterSetGuid; }
Provider& BasicCounterSetDefinition::GetProvider() { return Provider::InternalCounter; }
GUID const& CodeCounterSetDefinition::GetGuid() { return JS9InternalCounter_CodeCounterSetGuid; }
Provider& CodeCounterSetDefinition::GetProvider() { return Provider::InternalCounter; }
#ifdef HEAP_PERF_COUNTERS
GUID const& HeapCounterSetDefinition::GetGuid() { return JS9InternalCounter_HeapCounterSetGuid; }
Provider& HeapCounterSetDefinition::GetProvider() { return Provider::InternalCounter; }
#endif
#ifdef RECYCLER_PERF_COUNTERS
GUID const& RecyclerCounterSetDefinition::GetGuid() { return JS9InternalCounter_RecyclerCounterSetGuid; }
Provider& RecyclerCounterSetDefinition::GetProvider() { return Provider::InternalCounter; }
#endif
#ifdef PROFILE_RECYCLER_ALLOC
GUID const& RecyclerTrackerCounterSetDefinition::GetGuid() { return JS9InternalCounter_RecyclerTrackerCounterSetGuid; }
Provider& RecyclerTrackerCounterSetDefinition::GetProvider() { return Provider::InternalCounter; }
#define DEFINE_RECYCLER_TRACKER_PERF_COUNTER_INDEX(type) \
uint const RecyclerTrackerCounterSetDefinition::##type##CounterIndex = JS9InternalCounter_RecyclerTrackerCounterSet_##type##Count; \
uint const RecyclerTrackerCounterSetDefinition::##type##SizeCounterIndex = JS9InternalCounter_RecyclerTrackerCounterSet_##type##Size;
#define DEFINE_RECYCLER_TRACKER_ARRAY_PERF_COUNTER_INDEX(type) \
uint const RecyclerTrackerCounterSetDefinition::##type##ArrayCounterIndex = JS9InternalCounter_RecyclerTrackerCounterSet_##type##ArrayCount; \
uint const RecyclerTrackerCounterSetDefinition::##type##ArraySizeCounterIndex = JS9InternalCounter_RecyclerTrackerCounterSet_##type##ArraySize;
#define DEFINE_RECYCLER_TRACKER_WEAKREF_PERF_COUNTER_INDEX(type) \
uint const RecyclerTrackerCounterSetDefinition::##type##WeakRefCounterIndex = JS9InternalCounter_RecyclerTrackerCounterSet_##type##WeakRefCount;
RECYCLER_TRACKER_PERF_COUNTER_TYPE(DEFINE_RECYCLER_TRACKER_PERF_COUNTER_INDEX);
RECYCLER_TRACKER_ARRAY_PERF_COUNTER_TYPE(DEFINE_RECYCLER_TRACKER_ARRAY_PERF_COUNTER_INDEX);
RECYCLER_TRACKER_WEAKREF_PERF_COUNTER_TYPE(DEFINE_RECYCLER_TRACKER_WEAKREF_PERF_COUNTER_INDEX);
#endif
};
#endif