php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79871 On Windows, php can sleep only in 1ms steps
Submitted: 2020-07-18 12:52 UTC Modified: 2020-07-20 09:12 UTC
From: michael dot vorisek at email dot cz Assigned: cmb (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: 7.4.8 OS: Windows
Private report: No CVE-ID: None
 [2020-07-18 12:52 UTC] michael dot vorisek at email dot cz
Description:
------------
On Windows, php can sleep only in 1ms steps.

Not sure if it is related with gettimeofday function usage, as

microtime() seems to return "at least microseconds steps"

but "gettimeofday in reality returns only 1ms steps on Windows"

Test script:
---------------
$times = [];
$last = microtime(true);
for ($i = 0; $i < 10000; $i++) {
    $t = microtime(true);
    $times[] = ($t - $last) * 1000 * 1000;
    $last = $t;
    usleep(2);
    // same issue... time_nanosleep(0, 2000);
}

print_r($times);

Expected result:
----------------
numbers lower than 1000 / 1 ms

Actual result:
--------------
Array
(
    [0] => 0.95367431640625
    [1] => 282.04917907715
    [2] => 1029.9682617188
    [3] => 964.87998962402
    [4] => 999.21226501465
    [5] => 991.8212890625
    [6] => 1002.0732879639
    [7] => 1002.0732879639
    [8] => 996.82807922363
    [9] => 994.20547485352
    [10] => 998.97384643555
    [11] => 998.97384643555
    [12] => 993.96705627441
...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-20 08:45 UTC] cmb@php.net
-Type: Feature/Change Request +Type: Documentation Problem -Package: *General Issues +Package: Unknown/Other Function
 [2020-07-20 08:45 UTC] cmb@php.net
> Not sure if it is related with gettimeofday function usage, […]

No it is not, since usleep() doesn't do a *busy* wait.  Instead
the implementation uses SetWaitableTimer()[1] whose documentation
states:

| The actual timer accuracy depends on the capability of your
| hardware.

I suggest using the following as test script:

    <?php
    for ($i = 0; $i < 100; $i++) {
        $t0 = hrtime(true);
        usleep(2);
        $t1 = hrtime(true);
        printf("%d\n", $t1-$t0);
    }
    ?>

On one machine this gives something like:

    951400
    961400
    938200

On another machine I get:

    15920800
    13570300
    14755100

You may get yet different results.

Anyhow, I don't think there is anything we can do about that.
Note that the current MinGW CRT just uses Sleep() to implement
usleep()[2]. 

[1] <https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer>
[2] <https://github.com/mirror/mingw-w64/blob/16151c441e89081fd398270bb888511ebef6fb35/mingw-w64-crt/misc/mingw_usleep.c#L11-L17>
 [2020-07-20 09:12 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=be1cedff74c70588ca4d2da2e41179eadf50fa43
Log: Fix #79871: On Windows, php can sleep only in 1ms steps
 [2020-07-20 09:12 UTC] phpdocbot@php.net
-Status: Open +Status: Closed
 [2020-07-20 09:12 UTC] cmb@php.net
-Status: Closed +Status: Open -Assigned To: +Assigned To: cmb
 [2020-07-20 09:12 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2020-07-20 09:38 UTC] michael dot vorisek at email dot cz
I tested your script also on Linux (but virtualized):
62133
61154
60665
60609
60563

So we should probably update the docs more like:

Depending on hardware, the system may sleep up too several milliseconds longer.
 [2020-07-21 01:35 UTC] phpdocbot@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=bde8a46a21e1ffe64b2925afaa7f7a5df006a5fd
Log: Fix #79871: On Windows, php can sleep only in 1ms steps
 [2020-12-30 11:59 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=5ec03b874c988595c5b6b149a3d42f44081108a8
Log: Fix #79871: On Windows, php can sleep only in 1ms steps
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC