Hi everyone,

I would like to submit an RFC (and related PR) to deprecate posix_times for PHP 8.4, removing it for PHP 9.

There are multiple reasons to deprecate this function:

1)

The values which times(2) returns are expressed as the number of clock ticks elapsed since the process was started: the frequency of the aforementioned clock varies from platform to platform, and PHP does not expose the sysconf(2) function, which is what should be used to fetch the clock frequency, invoking `sysconf(_SC_CLK_TCK)`.

Thus, users have to deal with time values which **cannot be portably converted to microseconds**: this has actually bitten us @ work, where I recently found an ancient piece of code which incorrectly hardcoded the (undocumented) clock frequency of 100hz used on (most) Linux platforms: running the code on a Linux platform like ia64 [1], or any other POSIX OS which uses a different clock frequency would have silently reported an incorrect value, with no way to obtain the correct clock frequency from PHP.

2)

Equivalent functionality is already provided by getrusage(), with a *much higher resolution*:

*a.php:*

<?php

$t = microtime(true);

function slow2() {}
function slow() {
    slow2();
}

for ($x = 0; $x < 9000000; $x++) {
    slow();
}

$r = getrusage();
$stat = posix_times();

$res = [
    'utime_times' => $stat['utime'] * 10_000, // Assuming 100 hertz clock on x86 linux     'stime_times' => $stat['stime'] * 10_000, // Assuming 100 hertz clock on x86 linux

    'utime_rusage' => $r['ru_utime.tv_usec'] + ($r['ru_utime.tv_sec'] * 1000_000),     'stime_rusage' => $r['ru_stime.tv_usec'] + ($r['ru_stime.tv_sec'] * 1000_000),
];

var_dump($res);


*Result:*

array(4) {
  ["utime_times"]=>
  int(160000)
  ["stime_times"]=>
  int(0)
  ["utime_rusage"]=>
  int(163672)
  ["stime_rusage"]=>
  int(4994)
}


As you can see, with a system time of ~5 milliseconds, posix_times() even returns 0, since the minimum duration that can be represented with a 100 hertz clock (the default on most Linux platforms) is 10 milliseconds.


Waiting for feedback, kind regards,

Daniil Gentili.


[1]: https://github.com/bminor/glibc/blob/69239bd7a216007692470aa9d5f3658024638742/sysdeps/unix/sysv/linux/ia64/getclktck.c

Reply via email to