110 likes | 248 Views
OS Timing Services. Typical PC Clock System. RTC (Real Time Clock) Low freq oscillator (Eg. 32 kHz) Minimises Battery Consumption Second-level accuracy Software Clock (System Clock) Counter Interrupt driven: ticks are added to counter value
E N D
OS Timing Services Dr. Hugh Melvin, Dept. of IT, NUI,G
Typical PC Clock System • RTC (Real Time Clock) • Low freq oscillator (Eg. 32 kHz) • Minimises Battery Consumption • Second-level accuracy • Software Clock (System Clock) • Counter • Interrupt driven: ticks are added to counter value • Counter value translated into time standard eg. UTC • At system startup, counter value taken from RTC • Interrupts generated by separate oscillator • Eg. Intel 8254 1.193182 MHz Clock is commonly used Interrupt rate determines granularity or resolution Dr. Hugh Melvin, Dept. of IT, NUI,G
System Clock • Interrupt rate tick size system clock resolution • Note: Little point in implementing NTP for msec accuracy if timing services have poor resolution • Unix/Linux platforms • Interrupt rate upgraded to 1000 Hz (1msec) from 100Hz (10 msec) in recent releases • Windows Platforms • 10 15 msec ticksize still common • Eg. GetSystemTimeAsFileTime() Dr. Hugh Melvin, Dept. of IT, NUI,G
Granularity/Resolution • System Clock Granularity • Significant restriction for precision timing requirements of many RTS applications • Process timeslice also typically set by timer interrupts • Further restriction on Real Time applications • Limits responsiveness • Windows • Not yet implemented • Manual workarounds available • See notes • NTP workaround based on interpolation eg. using 8254 clock counter Dr. Hugh Melvin, Dept. of IT, NUI,G
Granularity/Resolution • Unix/Linux • gettimeofday() circumvents clock granularity (100/1000Hz) by interpolating between ticks using main CPU processor counter • gettimeofday() • Stores time in timeval structure (sec + usec) • struct timeval{ time_t tv_sec; time_t tv_usec; } • time_t is long integer Dr. Hugh Melvin, Dept. of IT, NUI,G
Windows Resolution //File handler ofstream outFile( argv[1],ios::out); SYSTEMTIME st; int i=0; while (i<5000) { ::GetSystemTime(&st); outFile<< st.wHour << ':'<< st.wMinute << ':'<< st.wSecond << '.' << st.wMilliseconds << '\n'; i++; } • Indicates granularity See http://msdn.microsoft.com/msdnmag/issues/04/03/HighResolutionTimer/ Dr. Hugh Melvin, Dept. of IT, NUI,G
Windows Resolution • See Code Extract • Store 5000 intervals • Examine distribution • Not all equal to fundamental resolution! • OS issue process preempted time has advanced by > 1 tick 5000 samples. Smallest delta: 15.625ms; Largest delta: 46.875ms; Sum of Delta: 78234.4ms; Average: 15.6469ms Distribution: 156250: 4993 312500: 4 468750: 2 Total Runtime was : 78.2656 seconds Exercise: Load up OS .. Examine distribution Dr. Hugh Melvin, Dept. of IT, NUI,G
Windows Resolution: Workaround • Improve resolution via separate High Performance/ Resolution Counter (HPRC) • ::QueryPerformanceCounter(&li); • Can access its frequency • ::QueryPerformanceFrequency(&frequency); • 3.57 MHz Clock • Interpolate between ticks of system clock • Need to establish baseline ‘synchronise’ with system clock • Baseline or reference has • System clock at instant of change • Corresponding HPRC value • Use baseline for interpolation Dr. Hugh Melvin, Dept. of IT, NUI,G
void simplistic_synchronize(reference_point& ref_point) { FILETIME ft0 = { 0, 0 }, ft1 = { 0, 0 }; LARGE_INTEGER li; // // Spin waiting for a change in system time. Get the matching // performance counter value for that time. // ::GetSystemTimeAsFileTime(&ft0); do { ::GetSystemTimeAsFileTime(&ft1); ::QueryPerformanceCounter(&li); } while ((ft0.dwHighDateTime == ft1.dwHighDateTime) && (ft0.dwLowDateTime == ft1.dwLowDateTime)); ref_point.file_time = ft1; ref_point.counter = li; } Dr. Hugh Melvin, Dept. of IT, NUI,G
Skew System Clock : Ref Sys Time + ΔHPRC Actual System Clock Reference Point (Ref Sys Time, Ref HPRC) Dr. Hugh Melvin, Dept. of IT, NUI,G
Unix/Linux Timing • gettimeofday() • Microsec granularity • Process timeslice can however impact on timing services • Eg. usleep( ) .. microsec level argument • In practice, limited by timeslice Dr. Hugh Melvin, Dept. of IT, NUI,G