From:  php_lists@realplain.com ("Matt Wilmas")
Date:  02 Sep 2014 02:36:30 Hong Kong Time
Newsgroup:  news.php.net/php.internals.win
Subject:  

[PATCH] Microsecond resolution and accuracy on Windows

NNTP-Posting-Host:  null

Hi all!

I'm back after several years, and will have a few more changes for Windows, 
at least.  (It was CVS back then, so I still have to figure some things 
out...  Just had to edit files on Github site. :-/)

Anyway, this patch is for microtime, etc. on Windows XP-7.  In March 2013, 
the fix for bug #64370 reduced the resolution to 1/64 or 1/100 of a second 
or so -- hardly "micro" time. :*(  See also bugs #64633 and #65626.  The 
highest resolution time function available before Windows 8 has simply been 
used by itself since then.  High-resolution Performance Counters were always 
used until then, but in a way that may become inaccurate and error-prone on 
some Windows+(virtual) hardware combinations.

One problem with the long-standing previous Performance Counter 
implementation is that it tried to be monotonic (always increasing).  But 
gettimeofday() IS supposed to be affected by changes in system time. 
Another problem was assuming that Performance Counters would track at a 
nearly-perfect rate with the real clock even without system time changes.

The solution to have high-resolution like the past, and accuracy like now 
(after #64370 fix) is fairly simple and straightforward: Use Performance 
Counters, but check that the value is within "range" of the real, current 
time!

With this implementation, you get high-resolution at least over short 
periods (e.g. when it matters) AND accuracy over a longer time between calls 
(e.g. won't notice few milliseconds of lost resolution), with no 
buggy/incorrect times.  (It seems there *could* be a Counter synchronization 
issue between multiple cores/processors on *some* hardware on *XP*.  I don't 
think this is a concern, nor related to previous bugs? 
http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx#qpc_support_in_windows_versions )

It's much more optimized than what's there now, and slightly over the old 
implementation.  Not sure if I should give the saved patch link, or the 
"live compare" (?) on Github, so I'll do both for now:
http://realplain.com/php/microtime_5_4.diff
https://github.com/matt-moo/php-src/compare/PHP-5.4.diff

Against 5.4 since that's what I quickly worked on so it's ready for the next 
5.4 release (Stas?). (Although I guess we're supposed to change the oldest 
branch usually?)

What do you think?  Questions or comments?


Thanks,
Matt