home *** CD-ROM | disk | FTP | other *** search
/ Enter 1999 November / ENTER11_1.bin / WARSZTAT / SDKJava32.exe / data1.cab / fg_Samples / Samples / Profiler / jviewprf / timer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-17  |  3.5 KB  |  180 lines

  1. // timer.cpp
  2. //
  3. // Created 01/19/99
  4. //
  5. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  6. //
  7.  
  8. #include "project.hpp"
  9. #pragma hdrstop
  10.  
  11. #include "timer.hpp"
  12.  
  13.  
  14. enum TimerTypes
  15. {
  16.     TIMER_DETECT,
  17.  
  18.     TIMER_TICKS,
  19.     TIMER_COUNTER,
  20.     TIMER_CYCLES,
  21. };
  22.  
  23.  
  24. TimerTypes g_timer = TIMER_DETECT;
  25. double g_countspersec;
  26. GetProfilerTimeProc *g_pfnGetProfilerTime;
  27.  
  28. #ifdef _X86_
  29.  
  30. __int64 GetCycleCount ()
  31. {
  32. __asm   _emit   0x0F
  33. __asm   _emit   0x31
  34. };
  35.  
  36. #endif
  37.  
  38.  
  39. PROFTIME GetProfilerTime_Ticks ()
  40. {
  41.     return GetTickCount();
  42. }
  43.  
  44. PROFTIME GetProfilerTime_Counter ()
  45. {
  46.     LARGE_INTEGER counter;
  47.     QueryPerformanceCounter(&counter);
  48.     return counter.QuadPart;
  49. }
  50.  
  51. PROFTIME GetProfilerTime_Cycles ()
  52. {
  53.     return GetCycleCount();
  54. }
  55.  
  56.  
  57. DWORD WINAPI TimeCPUThread (LPVOID lpThreadParameter)
  58. {
  59.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  60.     Sleep(0);
  61.  
  62.     DWORD sampleduration = 100;
  63.  
  64.     __int64 start = GetCycleCount();
  65.     Sleep(sampleduration);
  66.     __int64 end = GetCycleCount();
  67.  
  68.     g_countspersec = ((end-start)/sampleduration)*1000;
  69.  
  70.     return 0;
  71. }
  72.  
  73.  
  74. BOOL InitTimer ()
  75. {
  76.     switch (g_timer)
  77.     {
  78.         LARGE_INTEGER counterfreq;
  79.  
  80.     case TIMER_COUNTER:
  81.  
  82. init_counter_timer:
  83.  
  84.         if (QueryPerformanceFrequency(&counterfreq))
  85.         {
  86.             g_countspersec = counterfreq.QuadPart;
  87.             g_pfnGetProfilerTime = &GetProfilerTime_Counter;
  88.             return TRUE;
  89.         }
  90.         break;
  91.  
  92.     case TIMER_CYCLES:
  93.  
  94. init_cycle_timer:
  95.  
  96.         DWORD tid;
  97.         HANDLE hthd;
  98.         hthd = CreateThread(NULL, 0, &TimeCPUThread, NULL, 0, &tid);
  99.         if (hthd != INVALID_HANDLE_VALUE)
  100.         {
  101.             if (WaitForSingleObject(hthd, 1000) == WAIT_OBJECT_0)
  102.             {
  103.                 g_timer = TIMER_CYCLES;
  104.                 g_pfnGetProfilerTime = &GetProfilerTime_Cycles;
  105.                 return TRUE;
  106.             }
  107.         }
  108.         break;
  109.  
  110.  
  111.     case TIMER_DETECT:
  112.  
  113.         if (QueryPerformanceFrequency(&counterfreq))
  114.         {
  115.             g_timer = TIMER_COUNTER;
  116.             goto init_counter_timer;
  117.         }
  118.  
  119. #ifdef _X86_
  120.         SYSTEM_INFO sysinfo;
  121.         GetSystemInfo(&sysinfo);
  122.  
  123.         if (   sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL
  124.             && sysinfo.wProcessorLevel >= 5)
  125.         {
  126.             DWORD features;
  127.         
  128.             __asm
  129.             {
  130.                 mov     eax, 0
  131.                 __emit 0x0F
  132.                 __emit 0xA2
  133.                 mov     [features], edx
  134.             }
  135.  
  136.             if (features & 0x10)
  137.             {
  138.                 g_timer = TIMER_CYCLES;
  139.                 goto init_cycle_timer;
  140.             }
  141.         }
  142. #endif
  143.  
  144.         g_timer = TIMER_TICKS;
  145.  
  146.         // fall through
  147.  
  148.  
  149.     case TIMER_TICKS:
  150.  
  151.         OSVERSIONINFO verinfo;
  152.         ZeroMemory(&verinfo, sizeof(verinfo));
  153.         verinfo.dwOSVersionInfoSize = sizeof(verinfo);
  154.         if (GetVersionEx(&verinfo))
  155.         {
  156.             if (verinfo.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)
  157.                 g_countspersec = (double)1000/(double)55;
  158.             else
  159.                 g_countspersec = 100;
  160.  
  161.             g_pfnGetProfilerTime = &GetProfilerTime_Ticks;
  162.             return TRUE;
  163.         }
  164.     }
  165.  
  166.     return FALSE;
  167. }
  168.  
  169.  
  170. float ConvertProfilerTimeToMS (PROFTIME time)
  171. {
  172.     return (double)time / (g_countspersec / (double)1000);
  173. }
  174.  
  175. float ConvertProfilerTimeToUS (PROFTIME time)
  176. {
  177.     return (double)time / (g_countspersec / (double)1000000);
  178. }
  179.  
  180.