summaryrefslogtreecommitdiff
path: root/src/inc/perfcounterdefs.h
blob: 83fe7b1d5b5c11cba597ed1bf5dd455a10e0b798 (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//-----------------------------------------------------------------------------
// PerfCounterDefs.h
//
// Internal Interface for CLR to use Performance counters
//-----------------------------------------------------------------------------


#ifndef _PerfCounterDefs_h_
#define _PerfCounterDefs_h_

#include "contract.h"

//-----------------------------------------------------------------------------
// PerfCounters are only enabled if ENABLE_PERF_COUNTERS is defined.
// If we know we want them (such as in the impl or perfmon, then define this
// in before we include this header, else define this from the sources file.
//
// Note that some platforms don't use perfcounters, so to avoid a build
// break, you must wrap PerfCounter code (such as instrumenting in either 
// #ifdef or use the COUNTER_ONLY(x) macro (defined below)
// 

//-----------------------------------------------------------------------------
// Name of global IPC block
#define SHARED_PERF_IPC_NAME W("SharedPerfIPCBlock")


//-----------------------------------------------------------------------------
// Attributes for the IPC block
//-----------------------------------------------------------------------------
const int PERF_ATTR_ON      = 0x0001;   // Are we even updating any counters?
const int PERF_ATTR_GLOBAL  = 0x0002;   // Is this a global or private block?





//.............................................................................
// Tri Counter. Support for the common trio of counters (Total, Current, and 
// Instantaneous). This compiles to the same thing if we had them all separate,
// but it's a lot cleaner this way.
//.............................................................................
struct TRICOUNT
{
    DWORD Cur;                              // Current, has +, -
    DWORD Total;                            // Total, has  only +
    inline void operator++(int) {
        LIMITED_METHOD_CONTRACT;
        Cur ++; Total ++;
    }
    inline void operator--(int) {
        LIMITED_METHOD_CONTRACT;
        Cur --;
    }
    inline void operator+=(int delta) {
        LIMITED_METHOD_CONTRACT;
        Cur += delta; Total += delta;
    }
    inline void operator-=(int delta) {
        LIMITED_METHOD_CONTRACT;
        Cur -= delta;
    }
    inline void operator=(int delta) {
        LIMITED_METHOD_CONTRACT;
        Cur = delta;
        Total = delta;
    }
    inline void operator+=(TRICOUNT delta) {
        LIMITED_METHOD_CONTRACT;
        Cur += delta.Cur; Total += delta.Total;
    }

};

//.............................................................................
// Interlocked Tri Counter. Support for the common trio of counters (Total, Current,
// and Instantaneous). This compiles to the same thing if we had them all separate,
// but it's a lot cleaner this way.
//.............................................................................
struct TRICOUNT_IL
{
    DWORD Cur;                              // Current, has +, -
    DWORD Total;                            // Total, has  only +
    inline void operator++(int) {
        LIMITED_METHOD_CONTRACT;
        InterlockedIncrement((LPLONG)&Cur); InterlockedIncrement((LPLONG)&Total);
    }
    inline void operator--(int) {
        LIMITED_METHOD_CONTRACT;
        InterlockedDecrement((LPLONG)&Cur);
    }
    inline void operator+=(int delta) {
        LIMITED_METHOD_CONTRACT;
        while (TRUE)
        {
            LONG old_Cur = Cur;
            if (InterlockedCompareExchange((LPLONG)&Cur, Cur+delta, old_Cur) == old_Cur)
                break;
        }
        while (TRUE)
        {
            LONG old_Total = Total;
            if (InterlockedCompareExchange((LPLONG)&Total, Total+delta, old_Total) == old_Total)
                break;
        }
    }
    inline void operator-=(int delta) {
        LIMITED_METHOD_CONTRACT;
        while (TRUE)
        {
            LONG old_Cur = Cur;
            if (InterlockedCompareExchange((LPLONG)&Cur, Cur-delta, old_Cur) == old_Cur)
                break;
        }
    }
    inline void operator=(int delta) {
        LIMITED_METHOD_CONTRACT;
        while (TRUE)
        {
            LONG old_Cur = Cur;
            if (InterlockedCompareExchange((LPLONG)&Cur, delta, old_Cur) == old_Cur)
                break;
        }

        while (TRUE)
        {
            LONG old_Total = Total;
            if (InterlockedCompareExchange((LPLONG)&Total, delta, old_Total) == old_Total)
                break;
        }
    }
    inline void operator+=(TRICOUNT_IL delta) {
        LIMITED_METHOD_CONTRACT;
        while (TRUE)
        {
            LONG old_Cur = Cur;
            if (InterlockedCompareExchange((LPLONG)&Cur, Cur+delta.Cur, old_Cur) == old_Cur)
                break;
        }
        while (TRUE)
        {
            LONG old_Total = Total;
            if (InterlockedCompareExchange((LPLONG)&Total, Total+delta.Total, old_Total) == old_Total)
                break;
        }
    }

};


//.............................................................................
// Dual Counter. Support for the (Total and Instantaneous (rate)). Helpful in cases
// where the current value is always same as the total value. ie. the counter is never
// decremented.
// This compiles to the same thing if we had them separate, but it's a lot cleaner 
// this way.
//.............................................................................
struct DUALCOUNT
{
    DWORD Total;                            
    inline void operator++(int) {
        LIMITED_METHOD_CONTRACT;
        Total ++;
    }

    inline void operator+=(int delta) {
        LIMITED_METHOD_CONTRACT;
        Total += delta;
    }

    inline void operator+=(DUALCOUNT delta) {
        LIMITED_METHOD_CONTRACT;
        Total += delta.Total;
    }


};

//-----------------------------------------------------------------------------
// Format for the Perf Counter IPC Block
// IPC block is broken up into sections. This marks it easier to marshall
// into different perfmon objects
//
//.............................................................................
// Naming convention (by prefix):
// c - Raw count of something.
// cb- count of bytes
// time - time value.
// depth - stack depth
//-----------------------------------------------------------------------------

const int MAX_TRACKED_GENS = 3; // number of generations we track

//
// Perf_GC_Wow64 mimics in a 64 bit process, the layout of Perf_GC in a 32 bit process
// It does this by replacing all size_t by DWORD
//
// *** Keep contents of Perf_GC_Wow64 and Perf_GC in sync ***

struct Perf_GC_Wow64
{
public:
    DWORD cGenCollections[MAX_TRACKED_GENS];// count of collects per gen
    DWORD cbPromotedMem[MAX_TRACKED_GENS-1]; // count of promoted memory
    DWORD cbPromotedFinalizationMem;       // count of memory promoted due to finalization
    DWORD cProcessID;                      // process ID
    DWORD cGenHeapSize[MAX_TRACKED_GENS];  // size of heaps per gen
    DWORD cTotalCommittedBytes;            // total number of committed bytes.
    DWORD cTotalReservedBytes;             // bytes reserved via VirtualAlloc
    DWORD cLrgObjSize;                     // size of Large Object Heap
    DWORD cSurviveFinalize;                // count of instances surviving from finalizing
    DWORD cHandles;                        // count of GC handles
    DWORD cbAlloc;                         // bytes allocated
    DWORD cbLargeAlloc;                    // bytes allocated for Large Objects
    DWORD cInducedGCs;                     // number of explicit GCs
    DWORD timeInGC;                        // Time in GC
    DWORD timeInGCBase;                    // must follow time in GC counter
    
    DWORD cPinnedObj;                      // # of Pinned Objects
    DWORD cSinkBlocks;                     // # of sink blocks
};

// *** Keep contents of Perf_GC_Wow64 and Perf_GC in sync ***
#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_GC
{
public:
    size_t cGenCollections[MAX_TRACKED_GENS];// count of collects per gen
    size_t cbPromotedMem[MAX_TRACKED_GENS-1]; // count of promoted memory
    size_t cbPromotedFinalizationMem;       // count of memory promoted due to finalization
    size_t cProcessID;                      // process ID
    size_t cGenHeapSize[MAX_TRACKED_GENS];  // size of heaps per gen
    size_t cTotalCommittedBytes;            // total number of committed bytes.
    size_t cTotalReservedBytes;             // bytes reserved via VirtualAlloc
    size_t cLrgObjSize;                     // size of Large Object Heap
    size_t cSurviveFinalize;                // count of instances surviving from finalizing
    size_t cHandles;                        // count of GC handles
    size_t cbAlloc;                         // bytes allocated
    size_t cbLargeAlloc;                    // bytes allocated for Large Objects
    size_t cInducedGCs;                     // number of explicit GCs
    DWORD  timeInGC;                        // Time in GC
    DWORD  timeInGCBase;                    // must follow time in GC counter
    
    size_t cPinnedObj;                      // # of Pinned Objects
    size_t cSinkBlocks;                     // # of sink blocks

    Perf_GC();
    Perf_GC(Perf_GC_Wow64& copyFrom);
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64

//
// Perf_Loading_Wow64 mimics in a 64 bit process, the layout of Perf_Loading 
// in a 32 bit process. It does this by replacing all size_t by DWORD
//
// *** Keep contents of Perf_Loading_Wow64 and Perf_Loading in sync ***
struct Perf_Loading_Wow64
{
// Loading
public:
    TRICOUNT cClassesLoaded;
    TRICOUNT_IL cAppDomains;                   // Current # of AppDomains
    TRICOUNT cAssemblies;                   // Current # of Assemblies.
    UNALIGNED LONGLONG timeLoading;         // % time loading
    DWORD cAsmSearchLen;                    // Avg search length for assemblies
    DUALCOUNT cLoadFailures;                // Classes Failed to load
    DWORD cbLoaderHeapSize;                 // Total size of heap used by the loader
    DUALCOUNT cAppDomainsUnloaded;          // Rate at which app domains are unloaded
};

// *** Keep contents of Perf_Loading_Wow64 and Perf_Loading in sync ***
#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_Loading
{
// Loading
public:
    TRICOUNT cClassesLoaded;
    TRICOUNT_IL cAppDomains;                   // Current # of AppDomains
    TRICOUNT cAssemblies;                   // Current # of Assemblies.
    UNALIGNED LONGLONG timeLoading;                   // % time loading
    DWORD cAsmSearchLen;                    // Avg search length for assemblies
    DUALCOUNT cLoadFailures;                // Classes Failed to load
    size_t cbLoaderHeapSize;                 // Total size of heap used by the loader
    DUALCOUNT cAppDomainsUnloaded;          // Rate at which app domains are unloaded

    Perf_Loading();
    Perf_Loading(Perf_Loading_Wow64& copyFrom);
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64

#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_Jit
{
// Jitting
    DWORD cMethodsJitted;                   // number of methods jitted
    TRICOUNT cbILJitted;                    // IL jitted stats
//    DUALCOUNT cbPitched;                    // Total bytes pitched
    DWORD cJitFailures;                     // # of standard Jit failures
    DWORD timeInJit;                        // Time in JIT since last sample
    DWORD timeInJitBase;                    // Time in JIT base counter
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64

#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_Excep
{
// Exceptions
    DUALCOUNT cThrown;                          // Number of Exceptions thrown
    DWORD cFiltersExecuted;                 // Number of Filters executed
    DWORD cFinallysExecuted;                // Number of Finallys executed
    DWORD cThrowToCatchStackDepth;          // Delta from throw to catch site on stack
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64

#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_Interop
{
// Interop
    DWORD cCCW;                             // Number of CCWs
    DWORD cStubs;                           // Number of stubs
    DWORD cMarshalling;                      // # of time marshalling args and return values.
    DWORD cTLBImports;                      // Number of tlbs we import
    DWORD cTLBExports;                      // Number of tlbs we export
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64

#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_LocksAndThreads
{
// Locks
    DUALCOUNT cContention;                      // # of times in AwareLock::EnterEpilogue()
    TRICOUNT cQueueLength;                      // Lenght of queue
// Threads
    DWORD cCurrentThreadsLogical;           // Number (created - destroyed) of logical threads 
    DWORD cCurrentThreadsPhysical;          // Number (created - destroyed) of OS threads 
    TRICOUNT cRecognizedThreads;            // # of Threads execute in runtime's control
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64


// IMPORTANT!!!!!!!: The first two fields in the struct have to be together
// and be the first two fields in the struct. The managed code in ChannelServices.cs
// depends on this.
#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_Contexts
{
// Contexts & Remoting
    DUALCOUNT cRemoteCalls;                 // # of remote calls    
    DWORD cChannels;                        // Number of current channels
    DWORD cProxies;                         // Number of context proxies. 
    DWORD cClasses;                         // # of Context-bound classes
    DWORD cObjAlloc;                        // # of context bound objects allocated
    DWORD cContexts;                        // The current number of contexts.
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64

//
// Perf_Security_Wow64 mimics in a 64 bit process, the layout of Perf_Security 
// in a 32 bit process. It does this by packing all members on 4 byte boundary
// ("timeAuthorize" field which is 8 bytes in size, will get 8 byte aligned 
// on 64 bit by default)
//
// *** Keep contents of Perf_Security_Wow64 and Perf_Security in sync ***
#include <pshpack4.h>
struct Perf_Security_Wow64
{
// Security
public:
    DWORD cTotalRTChecks;                   // Total runtime checks
    UNALIGNED LONGLONG timeAuthorize;       // % time authenticating
    DWORD cLinkChecks;                      // link time checks
    DWORD timeRTchecks;                     // % time in Runtime checks
    DWORD timeRTchecksBase;                 // % time in Runtime checks base counter
    DWORD stackWalkDepth;                   // depth of stack for security checks
};
#include <poppack.h>

#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct Perf_Security
{
// Security
public:
    DWORD cTotalRTChecks;                   // Total runtime checks
    UNALIGNED LONGLONG timeAuthorize;       // % time authenticating
    DWORD cLinkChecks;                      // link time checks
    DWORD timeRTchecks;                     // % time in Runtime checks
    DWORD timeRTchecksBase;                 // % time in Runtime checks base counter
    DWORD stackWalkDepth;                   // depth of stack for security checks

    Perf_Security();
    Perf_Security(Perf_Security_Wow64& copyFrom);
};
#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64


//
// PerfCounterWow64IPCControlBlock mimics in a 64 bit process, the layout of 
// PerfCounterIPCControlBlock in a 32 bit process.
//
// *** Keep contents of PerfCounterWow64IPCControlBlock and PerfCounterIPCControlBlock in sync ***
#include <pshpack4.h>
struct PerfCounterWow64IPCControlBlock
{   
public:
// Versioning info
    WORD m_cBytes;      // size of this entire block
    WORD m_wAttrs;      // attributes for this block

// Counter Sections
    Perf_GC_Wow64       m_GC;
    Perf_Contexts       m_Context;
    Perf_Interop        m_Interop;
    Perf_Loading_Wow64  m_Loading;
    Perf_Excep          m_Excep;
    Perf_LocksAndThreads      m_LocksAndThreads;
    Perf_Jit            m_Jit;
    Perf_Security_Wow64 m_Security;
};
#include <poppack.h>

// Note: PerfMonDll marshalls data out of here by copying a continous block of memory.
// We can still add new members to the subsections above, but if we change their
// placement in the structure below, we may break PerfMon's marshalling

// *** Keep contents of PerfCounterWow64IPCControlBlock and PerfCounterIPCControlBlock in sync ***
#ifndef _WIN64
#include <pshpack4.h>
#endif //#ifndef _WIN64
struct PerfCounterIPCControlBlock
{   
public:
// Versioning info
    WORD m_cBytes;      // size of this entire block
    WORD m_wAttrs;      // attributes for this block

// Counter Sections
    Perf_GC         m_GC;
    Perf_Contexts   m_Context;
    Perf_Interop    m_Interop;
    Perf_Loading    m_Loading;
    Perf_Excep      m_Excep;
    Perf_LocksAndThreads      m_LocksAndThreads;
    Perf_Jit        m_Jit;
    Perf_Security   m_Security;

    PerfCounterIPCControlBlock();
    PerfCounterIPCControlBlock(PerfCounterWow64IPCControlBlock& copyFrom);
};

#ifndef _WIN64
#include <poppack.h>
#endif //#ifndef _WIN64

//
// Inline definitions
//

#include "perfcounterdefs.inl"

#endif // _PerfCounterDefs_h_