📄 ztimer.c
字号:
/****************************************************************************** SciTech OS Portability Manager Library** ========================================================================** The contents of this file are subject to the SciTech MGL Public* License Version 1.0 (the "License"); you may not use this file* except in compliance with the License. You may obtain a copy of* the License at http://www.scitechsoft.com/mgl-license.txt** Software distributed under the License is distributed on an* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or* implied. See the License for the specific language governing* rights and limitations under the License.** The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.** The Initial Developer of the Original Code is SciTech Software, Inc.* All Rights Reserved.** ========================================================================** Language: ANSI C* Environment: Any** Description: Module to implement high precision timing on each OS.*****************************************************************************/#include "ztimer.h"#include "pmapi.h"#include "oshdr.h"/*---------------------------- Global variables ---------------------------*/static LZTimerObject LZTimer;static ulong start,finish;#ifdef __INTEL__static long cpuSpeed = -1;static ibool haveRDTSC = false;#endif/*----------------------------- Implementation ----------------------------*//* External Intel assembler functions */#ifdef __INTEL__/* {secret} */void _ASMAPI _CPU_readTimeStamp(CPU_largeInteger *time);/* {secret} */ulong _ASMAPI _CPU_diffTime64(CPU_largeInteger *t1,CPU_largeInteger *t2,CPU_largeInteger *t);/* {secret} */ulong _ASMAPI _CPU_calcMicroSec(CPU_largeInteger *count,ulong freq);#endif#if defined(__SMX32__)#include "smx/ztimer.c"#elif defined(__RTTARGET__)#include "rttarget/ztimer.c"#elif defined(__REALDOS__)#include "dos/ztimer.c"#elif defined(__NT_DRIVER__)#include "ntdrv/ztimer.c"#elif defined(__WIN32_VXD__)#include "vxd/ztimer.c"#elif defined(__WINDOWS32__)#include "win32/ztimer.c"#elif defined(__OS2_VDD__)#include "vdd/ztimer.c"#elif defined(__OS2__)#include "os2/ztimer.c"#elif defined(__LINUX__)#include "linux/ztimer.c"#elif defined(__QNX__)#include "qnx/ztimer.c"#elif defined(__BEOS__)#include "beos/ztimer.c"#else#error Timer library not ported to this platform yet!#endif/*------------------------ Public interface routines ----------------------*//****************************************************************************DESCRIPTION:Initializes the Zen Timer library (extended)PARAMETERS:accurate - True of the speed should be measured accuratelyHEADER:ztimer.hREMARKS:This function initializes the Zen Timer library, and /must/ be called beforeany of the remaining Zen Timer library functions are called. The accurateparameter is used to determine whether highly accurate timing should beused or not. If high accuracy is needed, more time is spent profiling theactual speed of the CPU so that we can obtain highly accurate timingresults, but the time spent in the initialisation routine will besignificantly longer (on the order of 5 seconds).****************************************************************************/void ZAPI ZTimerInitExt( ibool accurate){ if (cpuSpeed == -1) { __ZTimerInit();#ifdef __INTEL__ cpuSpeed = CPU_getProcessorSpeedInHZ(accurate); haveRDTSC = CPU_haveRDTSC() && (cpuSpeed > 0);#endif }}/****************************************************************************DESCRIPTION:Initializes the Zen Timer library.HEADER:ztimer.hREMARKS:This function initializes the Zen Timer library, and /must/ be called beforeany of the remaining Zen Timer library functions are called.****************************************************************************/void ZAPI ZTimerInit(void){ ZTimerInitExt(false);}/****************************************************************************DESCRIPTION:Starts the Long Period Zen Timer counting.HEADER:ztimer.hPARAMETERS:tm - Timer object to start timing withREMARKS:Starts the Long Period Zen Timer counting. Once you have started the timer,you can stop it with LZTimerOff or you can latch the current count withLZTimerLap.The Long Period Zen Timer uses a number of different high precision timingmechanisms to obtain microsecond accurate timings results whenever possible.The following different techniques are used depending on the operatingsystem, runtime environment and CPU on the target machine. If the targetsystem has a Pentium CPU installed which supports the Read Time StampCounter instruction (RDTSC), the Zen Timer library will use this toobtain the maximum timing precision available.Under 32-bit Windows, if the Pentium RDTSC instruction is not available, wefirst try to use the Win32 QueryPerformanceCounter API, and if that is notavailable we fall back on the timeGetTime API which is always supported.Under 32-bit DOS, if the Pentium RDTSC instruction is not available, wethen do all timing using the old style 8253 timer chip. The 8253 timerroutines provide highly accurate timings results in pure DOS mode, howeverin a DOS box under Windows or other Operating Systems the virtualizationof the timer can produce inaccurate results.Note: Because the Long Period Zen Timer stores the results in a 32-bit unsigned integer, you can only time periods of up to 2^32 microseconds, or about 1hr 20mins. For timing longer periods use the Ultra Long Period Zen Timer.SEE ALSO:LZTimerOff, LZTimerLap, LZTimerCount****************************************************************************/void ZAPI LZTimerOnExt( LZTimerObject *tm){#ifdef __INTEL__ if (haveRDTSC) { _CPU_readTimeStamp(&tm->start); } else#endif __LZTimerOn(tm);}/****************************************************************************DESCRIPTION:Returns the current count for the Long Period Zen Timer and keeps itrunning.HEADER:ztimer.hPARAMETERS:tm - Timer object to do lap timing withRETURNS:Count that has elapsed in microseconds.REMARKS:Returns the current count that has elapsed since the last call toLZTimerOn in microseconds. The time continues to run after this function iscalled so you can call this function repeatedly.SEE ALSO:LZTimerOn, LZTimerOff, LZTimerCount****************************************************************************/ulong ZAPI LZTimerLapExt( LZTimerObject *tm){#ifdef __INTEL__ CPU_largeInteger tmLap,tmCount; if (haveRDTSC) { _CPU_readTimeStamp(&tmLap); _CPU_diffTime64(&tm->start,&tmLap,&tmCount); return _CPU_calcMicroSec(&tmCount,cpuSpeed); } else#endif return __LZTimerLap(tm);}/****************************************************************************DESCRIPTION:Stops the Long Period Zen Timer counting.HEADER:ztimer.hPARAMETERS:tm - Timer object to stop timing withREMARKS:Stops the Long Period Zen Timer counting and latches the count. Once youhave stopped the timer you can read the count with LZTimerCount. If you needhighly accurate timing, you should use the on and off functions rather thanthe lap function since the lap function does not subtract the overhead ofthe function calls from the timed count.SEE ALSO:LZTimerOn, LZTimerLap, LZTimerCount****************************************************************************/void ZAPI LZTimerOffExt( LZTimerObject *tm){#ifdef __INTEL__ if (haveRDTSC) { _CPU_readTimeStamp(&tm->end); } else#endif __LZTimerOff(tm);}/****************************************************************************DESCRIPTION:Returns the current count for the Long Period Zen Timer.HEADER:ztimer.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -