Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.0 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright � Microsoft Corporation. All rights reserved.
  4. //
  5. // StopWatch.h
  6. //
  7. // Purpose: Timing functions
  8. //
  9. //***************************************************************************
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif
  13. #ifndef _STOPWATCH_COMPILED_ALREADY_
  14. #define _STOPWATCH_COMPILED_ALREADY_
  15. #include <wchar.h>
  16. #include <stdio.h>
  17. #ifdef PROVIDER_INSTRUMENTATION
  18. #define PROVIDER_INSTRUMENTATION_START(pmc, timer) \
  19. if ( pmc && pmc->pStopWatch) \
  20. pmc->pStopWatch->Start(timer);
  21. #define PROVIDER_INSTRUMENTATION_START2(pStopWatch, timer) \
  22. if (pStopWatch) \
  23. pStopWatch->Start(timer);
  24. class POLARITY StopWatch
  25. {
  26. public:
  27. // those types of timers we have.
  28. // note that any new timers must be added before NTimers
  29. enum Timers {NullTimer = -1, FrameworkTimer =0, ProviderTimer, AtomicTimer, WinMgmtTimer, NTimers};
  30. StopWatch(const CHString& reason);
  31. // start a particular timer, stopping the previous one
  32. void Start(Timers timer);
  33. // call this only at the very end
  34. void Stop();
  35. __int64 GetTime(Timers timer);
  36. void LogResults();
  37. private:
  38. // something to spit to the log to identify this run
  39. CHString m_reason;
  40. // track the times we're timing
  41. // elapsed times in array
  42. __int64 m_times[NTimers];
  43. // the one we're currently tracking
  44. Timers m_currentTimer;
  45. // the start time for the one we're currently tracking
  46. LARGE_INTEGER m_startTime;
  47. };
  48. inline StopWatch::StopWatch(const CHString& reason)
  49. {
  50. m_reason = reason;
  51. m_currentTimer = NullTimer;
  52. ZeroMemory(m_times, sizeof(m_times));
  53. }
  54. inline void StopWatch::Start(Timers timer)
  55. {
  56. LARGE_INTEGER count;
  57. QueryPerformanceCounter(&count);
  58. if (m_currentTimer != NullTimer)
  59. m_times[m_currentTimer] += count.QuadPart - m_startTime.QuadPart;
  60. m_currentTimer = timer;
  61. m_startTime = count;
  62. }
  63. inline void StopWatch::Stop()
  64. {
  65. Start(NullTimer);
  66. }
  67. inline __int64 StopWatch::GetTime(Timers timer)
  68. {
  69. return m_times[timer];
  70. }
  71. inline void StopWatch::LogResults()
  72. {
  73. FILE *fpLogFile;
  74. fpLogFile = _wfopen( L"C:\\StopWatch.log", L"a+" );
  75. if(fpLogFile)
  76. {
  77. WCHAR datebuffer [9];
  78. WCHAR timebuffer [9];
  79. _wstrdate( datebuffer );
  80. _wstrtime( timebuffer );
  81. LARGE_INTEGER omega;
  82. QueryPerformanceFrequency(&omega);
  83. // _ftprintf(fpLogFile, L"%s\n\t%-8s %-8s\n", m_reason, datebuffer, timebuffer);
  84. fwprintf(fpLogFile, L"%s\n ", m_reason);
  85. fwprintf(fpLogFile, L"Framework\tProvider\tWinmgmt \tAtomic\n %I64u\t%I64u\t%I64u\t%I64u\n",
  86. GetTime(FrameworkTimer), GetTime(ProviderTimer), GetTime(WinMgmtTimer), omega);
  87. fclose(fpLogFile);
  88. }
  89. }
  90. #else
  91. #define PROVIDER_INSTRUMENTATION_START(pmc, timer)
  92. #define PROVIDER_INSTRUMENTATION_START2(pStopWatch, timer)
  93. #endif
  94. #endif