| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | |
| 6 | // Exit macros |
| 7 | #define PerfExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_PERFUTIL, x, s, __VA_ARGS__) |
| 8 | #define PerfExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_PERFUTIL, x, s, __VA_ARGS__) |
| 9 | #define PerfExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PERFUTIL, x, s, __VA_ARGS__) |
| 10 | #define PerfExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PERFUTIL, x, s, __VA_ARGS__) |
| 11 | #define PerfExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PERFUTIL, x, s, __VA_ARGS__) |
| 12 | #define PerfExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PERFUTIL, x, s, __VA_ARGS__) |
| 13 | #define PerfExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PERFUTIL, p, x, e, s, __VA_ARGS__) |
| 14 | #define PerfExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PERFUTIL, p, x, s, __VA_ARGS__) |
| 15 | #define PerfExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_PERFUTIL, p, x, e, s, __VA_ARGS__) |
| 16 | #define PerfExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_PERFUTIL, p, x, s, __VA_ARGS__) |
| 17 | #define PerfExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_PERFUTIL, e, x, s, __VA_ARGS__) |
| 18 | #define PerfExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_PERFUTIL, g, x, s, __VA_ARGS__) |
| 19 | |
| 20 | static BOOL vfHighPerformanceCounter = TRUE; // assume the system has a high performance counter |
| 21 | static double vdFrequency = 1; |
| 22 | |
| 23 | |
| 24 | /******************************************************************** |
| 25 | PerfInitialize - initializes internal static variables |
| 26 | |
| 27 | ********************************************************************/ |
| 28 | extern "C" void DAPI PerfInitialize( |
| 29 | ) |
| 30 | { |
| 31 | LARGE_INTEGER liFrequency = { }; |
| 32 | |
| 33 | // |
| 34 | // check for high perf counter |
| 35 | // |
| 36 | if (!::QueryPerformanceFrequency(&liFrequency)) |
| 37 | { |
| 38 | vfHighPerformanceCounter = FALSE; |
| 39 | vdFrequency = 1000; // ticks are measured in milliseconds |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | vdFrequency = static_cast<double>(liFrequency.QuadPart); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /******************************************************************** |
| 49 | PerfClickTime - resets the clicker, or returns elapsed time since last call |
| 50 | |
| 51 | NOTE: if pliElapsed is NULL, resets the elapsed time |
| 52 | if pliElapsed is not NULL, returns perf number since last call to PerfClickTime() |
| 53 | ********************************************************************/ |
| 54 | extern "C" void DAPI PerfClickTime( |
| 55 | __out_opt LARGE_INTEGER* pliElapsed |
| 56 | ) |
| 57 | { |
| 58 | static LARGE_INTEGER liStart = { }; |
| 59 | LARGE_INTEGER* pli = pliElapsed; |
| 60 | |
| 61 | if (!pli) // if elapsed time time was not requested, reset the start time |
| 62 | { |
| 63 | pli = &liStart; |
| 64 | } |
| 65 | |
| 66 | if (vfHighPerformanceCounter) |
| 67 | { |
| 68 | ::QueryPerformanceCounter(pli); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | pli->QuadPart = ::GetTickCount(); |
| 73 | } |
| 74 | |
| 75 | if (pliElapsed) |
| 76 | { |
| 77 | pliElapsed->QuadPart -= liStart.QuadPart; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /******************************************************************** |
| 83 | PerfConvertToSeconds - converts perf number to seconds |
| 84 | |
| 85 | ********************************************************************/ |
| 86 | extern "C" double DAPI PerfConvertToSeconds( |
| 87 | __in const LARGE_INTEGER* pli |
| 88 | ) |
| 89 | { |
| 90 | Assert(0 < vdFrequency); |
| 91 | return pli->QuadPart / vdFrequency; |
| 92 | } |