home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Profiler / jviewprf / timer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  3.6 KB  |  185 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. float g_countspersec;
  26. GetProfilerTimeProc *g_pfnGetProfilerTime;
  27.  
  28. #ifdef _X86_
  29.  
  30. __declspec(naked)
  31. __int64 GetCycleCount ()
  32. {
  33.     __asm
  34.     {
  35.         __emit  0x0F
  36.         __emit  0x31
  37.         retn
  38.     }
  39. };
  40.  
  41. #endif
  42.  
  43.  
  44. PROFTIME GetProfilerTime_Ticks ()
  45. {
  46.     return GetTickCount();
  47. }
  48.  
  49. PROFTIME GetProfilerTime_Counter ()
  50. {
  51.     LARGE_INTEGER counter;
  52.     QueryPerformanceCounter(&counter);
  53.     return counter.QuadPart;
  54. }
  55.  
  56. PROFTIME GetProfilerTime_Cycles ()
  57. {
  58.     return GetCycleCount();
  59. }
  60.  
  61.  
  62. DWORD WINAPI TimeCPUThread (LPVOID lpThreadParameter)
  63. {
  64.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  65.     Sleep(0);
  66.  
  67.     DWORD sampleduration = 100;
  68.  
  69.     __int64 start = GetCycleCount();
  70.     Sleep(sampleduration);
  71.     __int64 end = GetCycleCount();
  72.  
  73.     g_countspersec = ((float)(end-start)/sampleduration)*1000;
  74.  
  75.     return 0;
  76. }
  77.  
  78.  
  79. BOOL InitTimer ()
  80. {
  81.     switch (g_timer)
  82.     {
  83.         LARGE_INTEGER counterfreq;
  84.  
  85.     case TIMER_COUNTER:
  86.  
  87. init_counter_timer:
  88.  
  89.         if (QueryPerformanceFrequency(&counterfreq))
  90.         {
  91.             g_countspersec = (float)counterfreq.QuadPart;
  92.             g_pfnGetProfilerTime = &GetProfilerTime_Counter;
  93.             return TRUE;
  94.         }
  95.         break;
  96.  
  97.     case TIMER_CYCLES:
  98.  
  99. init_cycle_timer:
  100.  
  101.         DWORD tid;
  102.         HANDLE hthd;
  103.         hthd = CreateThread(NULL, 0, &TimeCPUThread, NULL, 0, &tid);
  104.         if (hthd != INVALID_HANDLE_VALUE)
  105.         {
  106.             if (WaitForSingleObject(hthd, 1000) == WAIT_OBJECT_0)
  107.             {
  108.                 g_timer = TIMER_CYCLES;
  109.                 g_pfnGetProfilerTime = &GetProfilerTime_Cycles;
  110.                 return TRUE;
  111.             }
  112.         }
  113.         break;
  114.  
  115.  
  116.     case TIMER_DETECT:
  117.  
  118.         if (QueryPerformanceFrequency(&counterfreq))
  119.         {
  120.             g_timer = TIMER_COUNTER;
  121.             goto init_counter_timer;
  122.         }
  123.  
  124. #ifdef _X86_
  125.         SYSTEM_INFO sysinfo;
  126.         GetSystemInfo(&sysinfo);
  127.  
  128.         if (   sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL
  129.             && sysinfo.wProcessorLevel >= 5)
  130.         {
  131.             DWORD features;
  132.         
  133.             __asm
  134.             {
  135.                 mov     eax, 0
  136.                 __emit 0x0F
  137.                 __emit 0xA2
  138.                 mov     [features], edx
  139.             }
  140.  
  141.             if (features & 0x10)
  142.             {
  143.                 g_timer = TIMER_CYCLES;
  144.                 goto init_cycle_timer;
  145.             }
  146.         }
  147. #endif
  148.  
  149.         g_timer = TIMER_TICKS;
  150.  
  151.         // fall through
  152.  
  153.  
  154.     case TIMER_TICKS:
  155.  
  156.         OSVERSIONINFO verinfo;
  157.         ZeroMemory(&verinfo, sizeof(verinfo));
  158.         verinfo.dwOSVersionInfoSize = sizeof(verinfo);
  159.         if (GetVersionEx(&verinfo))
  160.         {
  161.             if (verinfo.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)
  162.                 g_countspersec = (float)1000/(float)55;
  163.             else
  164.                 g_countspersec = 100;
  165.  
  166.             g_pfnGetProfilerTime = &GetProfilerTime_Ticks;
  167.             return TRUE;
  168.         }
  169.     }
  170.  
  171.     return FALSE;
  172. }
  173.  
  174.  
  175. float ConvertProfilerTimeToMS (PROFTIME time)
  176. {
  177.     return (float)time / (g_countspersec / (float)1000);
  178. }
  179.  
  180. float ConvertProfilerTimeToUS (PROFTIME time)
  181. {
  182.     return (float)time / (g_countspersec / (float)1000000);
  183. }
  184.  
  185.