home *** CD-ROM | disk | FTP | other *** search
- // timer.cpp
- //
- // Created 01/19/99
- //
- // (C) Copyright 1995 - 1999 Microsoft Corporation. All rights reserved.
- //
-
- #include "project.hpp"
- #pragma hdrstop
-
- #include "timer.hpp"
-
-
- enum TimerTypes
- {
- TIMER_DETECT,
-
- TIMER_TICKS,
- TIMER_COUNTER,
- TIMER_CYCLES,
- };
-
-
- TimerTypes g_timer = TIMER_DETECT;
- float g_countspersec;
- GetProfilerTimeProc *g_pfnGetProfilerTime;
-
- #ifdef _X86_
-
- __declspec(naked)
- __int64 GetCycleCount ()
- {
- __asm
- {
- __emit 0x0F
- __emit 0x31
- retn
- }
- };
-
- #endif
-
-
- PROFTIME GetProfilerTime_Ticks ()
- {
- return GetTickCount();
- }
-
- PROFTIME GetProfilerTime_Counter ()
- {
- LARGE_INTEGER counter;
- QueryPerformanceCounter(&counter);
- return counter.QuadPart;
- }
-
- PROFTIME GetProfilerTime_Cycles ()
- {
- return GetCycleCount();
- }
-
-
- DWORD WINAPI TimeCPUThread (LPVOID lpThreadParameter)
- {
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
- Sleep(0);
-
- DWORD sampleduration = 100;
-
- __int64 start = GetCycleCount();
- Sleep(sampleduration);
- __int64 end = GetCycleCount();
-
- g_countspersec = ((float)(end-start)/sampleduration)*1000;
-
- return 0;
- }
-
-
- BOOL InitTimer ()
- {
- switch (g_timer)
- {
- LARGE_INTEGER counterfreq;
-
- case TIMER_COUNTER:
-
- init_counter_timer:
-
- if (QueryPerformanceFrequency(&counterfreq))
- {
- g_countspersec = (float)counterfreq.QuadPart;
- g_pfnGetProfilerTime = &GetProfilerTime_Counter;
- return TRUE;
- }
- break;
-
- case TIMER_CYCLES:
-
- init_cycle_timer:
-
- DWORD tid;
- HANDLE hthd;
- hthd = CreateThread(NULL, 0, &TimeCPUThread, NULL, 0, &tid);
- if (hthd != INVALID_HANDLE_VALUE)
- {
- if (WaitForSingleObject(hthd, 1000) == WAIT_OBJECT_0)
- {
- g_timer = TIMER_CYCLES;
- g_pfnGetProfilerTime = &GetProfilerTime_Cycles;
- return TRUE;
- }
- }
- break;
-
-
- case TIMER_DETECT:
-
- if (QueryPerformanceFrequency(&counterfreq))
- {
- g_timer = TIMER_COUNTER;
- goto init_counter_timer;
- }
-
- #ifdef _X86_
- SYSTEM_INFO sysinfo;
- GetSystemInfo(&sysinfo);
-
- if ( sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL
- && sysinfo.wProcessorLevel >= 5)
- {
- DWORD features;
-
- __asm
- {
- mov eax, 0
- __emit 0x0F
- __emit 0xA2
- mov [features], edx
- }
-
- if (features & 0x10)
- {
- g_timer = TIMER_CYCLES;
- goto init_cycle_timer;
- }
- }
- #endif
-
- g_timer = TIMER_TICKS;
-
- // fall through
-
-
- case TIMER_TICKS:
-
- OSVERSIONINFO verinfo;
- ZeroMemory(&verinfo, sizeof(verinfo));
- verinfo.dwOSVersionInfoSize = sizeof(verinfo);
- if (GetVersionEx(&verinfo))
- {
- if (verinfo.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)
- g_countspersec = (float)1000/(float)55;
- else
- g_countspersec = 100;
-
- g_pfnGetProfilerTime = &GetProfilerTime_Ticks;
- return TRUE;
- }
- }
-
- return FALSE;
- }
-
-
- float ConvertProfilerTimeToMS (PROFTIME time)
- {
- return (float)time / (g_countspersec / (float)1000);
- }
-
- float ConvertProfilerTimeToUS (PROFTIME time)
- {
- return (float)time / (g_countspersec / (float)1000000);
- }
-
-