forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_win32_perfctr_provider.cc
314 lines (254 loc) · 10.5 KB
/
node_win32_perfctr_provider.cc
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#define __INIT_node_perfctr_provider_IMP
#include "node_counters.h"
#include "node_win32_perfctr_provider.h"
#include <perflib.h>
#include "node_perfctr_provider.h"
typedef ULONG (WINAPI *PerfStartProviderExFunc)(
__in LPGUID ProviderGuid,
__in_opt PPERF_PROVIDER_CONTEXT ProviderContext,
__out PHANDLE Provider);
typedef ULONG (WINAPI *PerfStopProviderFunc)(
__in HANDLE ProviderHandle);
typedef ULONG (WINAPI *PerfSetCounterSetInfoFunc)(
__in HANDLE ProviderHandle,
__inout_bcount(TemplateSize) PPERF_COUNTERSET_INFO Template,
__in ULONG TemplateSize);
typedef PPERF_COUNTERSET_INSTANCE (WINAPI *PerfCreateInstanceFunc)(
__in HANDLE ProviderHandle,
__in LPCGUID CounterSetGuid,
__in PCWSTR Name,
__in ULONG Id);
typedef ULONG (WINAPI *PerfDeleteInstanceFunc)(
__in HANDLE Provider,
__in PPERF_COUNTERSET_INSTANCE InstanceBlock);
typedef ULONG (WINAPI *PerfSetULongCounterValueFunc)(
__in HANDLE Provider,
__inout PPERF_COUNTERSET_INSTANCE Instance,
__in ULONG CounterId,
__in ULONG Value);
typedef ULONG (WINAPI *PerfSetULongLongCounterValueFunc)(
__in HANDLE Provider,
__inout PPERF_COUNTERSET_INSTANCE Instance,
__in ULONG CounterId,
__in ULONGLONG Value);
typedef ULONG (WINAPI *PerfIncrementULongCounterValueFunc)(
__in HANDLE Provider,
__inout PPERF_COUNTERSET_INSTANCE Instance,
__in ULONG CounterId,
__in ULONG Value);
typedef ULONG (WINAPI *PerfIncrementULongLongCounterValueFunc)(
__in HANDLE Provider,
__inout PPERF_COUNTERSET_INSTANCE Instance,
__in ULONG CounterId,
__in ULONGLONG Value);
typedef ULONG (WINAPI *PerfDecrementULongCounterValueFunc)(
__in HANDLE Provider,
__inout PPERF_COUNTERSET_INSTANCE Instance,
__in ULONG CounterId,
__in ULONG Value);
typedef ULONG (WINAPI *PerfDecrementULongLongCounterValueFunc)(
__in HANDLE Provider,
__inout PPERF_COUNTERSET_INSTANCE Instance,
__in ULONG CounterId,
__in ULONGLONG Value);
HMODULE advapimod;
PerfStartProviderExFunc perfctr_startProvider;
PerfStopProviderFunc perfctr_stopProvider;
PerfSetCounterSetInfoFunc perfctr_setCounterSetInfo;
PerfCreateInstanceFunc perfctr_createInstance;
PerfDeleteInstanceFunc perfctr_deleteInstance;
PerfSetULongCounterValueFunc perfctr_setULongValue;
PerfSetULongLongCounterValueFunc perfctr_setULongLongValue;
PerfIncrementULongCounterValueFunc perfctr_incrementULongValue;
PerfIncrementULongLongCounterValueFunc perfctr_incrementULongLongValue;
PerfDecrementULongCounterValueFunc perfctr_decrementULongValue;
PerfDecrementULongLongCounterValueFunc perfctr_decrementULongLongValue;
PPERF_COUNTERSET_INSTANCE perfctr_instance;
#define NODE_COUNTER_HTTP_SERVER_REQUEST 1
#define NODE_COUNTER_HTTP_SERVER_RESPONSE 2
#define NODE_COUNTER_HTTP_CLIENT_REQUEST 3
#define NODE_COUNTER_HTTP_CLIENT_RESPONSE 4
#define NODE_COUNTER_SERVER_CONNS 5
#define NODE_COUNTER_NET_BYTES_SENT 6
#define NODE_COUNTER_NET_BYTES_RECV 7
#define NODE_COUNTER_GC_PERCENTTIME 8
#define NODE_COUNTER_PIPE_BYTES_SENT 9
#define NODE_COUNTER_PIPE_BYTES_RECV 10
namespace node {
EXTERN_C DECLSPEC_SELECTANY HANDLE NodeCounterProvider = nullptr;
void InitPerfCountersWin32() {
ULONG status;
PERF_PROVIDER_CONTEXT providerContext;
// create instance name using pid
#define INST_MAX_LEN 32
#define INST_PREFIX_LEN 5
#define INST_PREFIX L"node_"
wchar_t Inst[INST_MAX_LEN];
DWORD pid = GetCurrentProcessId();
wcscpy_s(Inst, INST_MAX_LEN, INST_PREFIX);
_itow_s(pid, Inst + INST_PREFIX_LEN, INST_MAX_LEN - INST_PREFIX_LEN, 10);
advapimod = LoadLibraryW(L"advapi32.dll");
if (advapimod) {
perfctr_startProvider = (PerfStartProviderExFunc)
GetProcAddress(advapimod, "PerfStartProviderEx");
perfctr_stopProvider = (PerfStopProviderFunc)
GetProcAddress(advapimod, "PerfStopProvider");
perfctr_setCounterSetInfo = (PerfSetCounterSetInfoFunc)
GetProcAddress(advapimod, "PerfSetCounterSetInfo");
perfctr_createInstance = (PerfCreateInstanceFunc)
GetProcAddress(advapimod, "PerfCreateInstance");
perfctr_deleteInstance = (PerfDeleteInstanceFunc)
GetProcAddress(advapimod, "PerfDeleteInstance");
perfctr_setULongValue = (PerfSetULongCounterValueFunc)
GetProcAddress(advapimod, "PerfSetULongCounterValue");
perfctr_setULongLongValue = (PerfSetULongLongCounterValueFunc)
GetProcAddress(advapimod, "PerfSetULongLongCounterValue");
perfctr_incrementULongValue = (PerfIncrementULongCounterValueFunc)
GetProcAddress(advapimod, "PerfIncrementULongCounterValue");
perfctr_incrementULongLongValue = (PerfIncrementULongLongCounterValueFunc)
GetProcAddress(advapimod, "PerfIncrementULongLongCounterValue");
perfctr_decrementULongValue = (PerfDecrementULongCounterValueFunc)
GetProcAddress(advapimod, "PerfDecrementULongCounterValue");
perfctr_decrementULongLongValue = (PerfDecrementULongLongCounterValueFunc)
GetProcAddress(advapimod, "PerfDecrementULongLongCounterValue");
ZeroMemory(&providerContext, sizeof(providerContext));
providerContext.ContextSize = sizeof(providerContext);
if (!perfctr_startProvider ||
!perfctr_setCounterSetInfo ||
!perfctr_createInstance) {
NodeCounterProvider = nullptr;
return;
}
status = perfctr_startProvider(&NodeCounterSetGuid,
&providerContext,
&NodeCounterProvider);
if (status != ERROR_SUCCESS) {
NodeCounterProvider = nullptr;
return;
}
status = perfctr_setCounterSetInfo(NodeCounterProvider,
&NodeCounterSetInfo.CounterSet,
sizeof(NodeCounterSetInfo));
if (status != ERROR_SUCCESS) {
perfctr_stopProvider(NodeCounterProvider);
NodeCounterProvider = nullptr;
return;
}
perfctr_instance = perfctr_createInstance(NodeCounterProvider,
&NodeCounterSetGuid,
Inst,
1);
if (perfctr_instance == nullptr) {
perfctr_stopProvider(NodeCounterProvider);
NodeCounterProvider = nullptr;
}
}
}
void TermPerfCountersWin32() {
if (NodeCounterProvider != nullptr &&
perfctr_stopProvider != nullptr) {
perfctr_stopProvider(NodeCounterProvider);
NodeCounterProvider = nullptr;
}
if (advapimod) {
FreeLibrary(advapimod);
advapimod = nullptr;
}
}
void NODE_COUNT_HTTP_SERVER_REQUEST() {
if (NodeCounterProvider != nullptr && perfctr_incrementULongValue != nullptr) {
perfctr_incrementULongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_HTTP_SERVER_REQUEST,
1);
}
}
void NODE_COUNT_HTTP_SERVER_RESPONSE() {
if (NodeCounterProvider != nullptr && perfctr_incrementULongValue != nullptr) {
perfctr_incrementULongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_HTTP_SERVER_RESPONSE,
1);
}
}
void NODE_COUNT_HTTP_CLIENT_REQUEST() {
if (NodeCounterProvider != nullptr && perfctr_incrementULongValue != nullptr) {
perfctr_incrementULongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_HTTP_CLIENT_REQUEST,
1);
}
}
void NODE_COUNT_HTTP_CLIENT_RESPONSE() {
if (NodeCounterProvider != nullptr && perfctr_incrementULongValue != nullptr) {
perfctr_incrementULongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_HTTP_CLIENT_RESPONSE,
1);
}
}
void NODE_COUNT_SERVER_CONN_OPEN() {
if (NodeCounterProvider != nullptr && perfctr_incrementULongValue != nullptr) {
perfctr_incrementULongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_SERVER_CONNS,
1);
}
}
void NODE_COUNT_SERVER_CONN_CLOSE() {
if (NodeCounterProvider != nullptr && perfctr_decrementULongValue != nullptr) {
perfctr_decrementULongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_SERVER_CONNS,
1);
}
}
void NODE_COUNT_NET_BYTES_SENT(int bytes) {
if (NodeCounterProvider != nullptr && perfctr_incrementULongLongValue != nullptr) {
perfctr_incrementULongLongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_NET_BYTES_SENT,
static_cast<ULONGLONG>(bytes));
}
}
void NODE_COUNT_NET_BYTES_RECV(int bytes) {
if (NodeCounterProvider != nullptr && perfctr_incrementULongLongValue != nullptr) {
perfctr_incrementULongLongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_NET_BYTES_RECV,
static_cast<ULONGLONG>(bytes));
}
}
uint64_t NODE_COUNT_GET_GC_RAWTIME() {
LARGE_INTEGER timegc;
if (QueryPerformanceCounter(&timegc)) {
return timegc.QuadPart;
} else {
return static_cast<uint64_t>(GetTickCount());
}
}
void NODE_COUNT_GC_PERCENTTIME(unsigned int percent) {
if (NodeCounterProvider != nullptr && perfctr_setULongValue != nullptr) {
perfctr_setULongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_GC_PERCENTTIME,
percent);
}
}
void NODE_COUNT_PIPE_BYTES_SENT(int bytes) {
if (NodeCounterProvider != nullptr && perfctr_incrementULongLongValue != nullptr) {
perfctr_incrementULongLongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_PIPE_BYTES_SENT,
static_cast<ULONGLONG>(bytes));
}
}
void NODE_COUNT_PIPE_BYTES_RECV(int bytes) {
if (NodeCounterProvider != nullptr && perfctr_incrementULongLongValue != nullptr) {
perfctr_incrementULongLongValue(NodeCounterProvider,
perfctr_instance,
NODE_COUNTER_PIPE_BYTES_RECV,
static_cast<ULONGLONG>(bytes));
}
}
} // namespace node