skip to Main Content

By default php terminates a script after 30 seconds of CPU time, that is sleeps and time waiting for network responses are mostly not counted.

Also on each call of set_time_limit this internal time keeping is reset.

PHP must internally keep track of how much CPU time a process has used, is there a reliable way to get this information?

If possible I would like a function that tells me how much time left I have.

2

Answers


  1. Try this,you can get the CPU time of execution.

    // Record the start time
    $startUsage = getrusage();
    
    // Your PHP code here
    
    // Record the end time
    $endUsage = getrusage();
    
    // Calculate CPU time
    $cpuTime = ($endUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_sec']) +
           ($endUsage['ru_utime.tv_usec'] - $startUsage['ru_utime.tv_usec']) / 1000000;
    
    echo "CPU time used: {$cpuTime} seconds";
    
    Login or Signup to reply.
  2. For what it’s worth, I tried to create some portable functions to do this. Here is the code I wrote. On my machine, my Apache server is set to timeout after 30 seconds, and it does when I set the for loop to 6 billion, but it runs with a few seconds to spare at 5 billion. The sleep() doesn’t count towards the usage, but I do have prefork mpm module. When I get some time to look again at this, I’ll see what it does on other mpm modules.

    If the getrusage() function is not available, then it just resorts to using elapsed time which should be safely greater than or equal to cpu time I think. I wanted to make it also use elapsed time if it was not on mpm prefork, but I don’t think there’s a way to determine this in PHP without exec’ing httpd which seemed like overkill. Please someone let me know if I’m wrong about that. Anyway, feel free to comment or make suggestions on how to improve it.

    It returns seconds, which I think is sufficient if the make purpose of the function is just to ensure that a script finishes in an orderly way rather than timing out. I think you could just have a line like

    if(!Is_Bool($remainingtime) && $remainingtime < 5) saveandquit();
    

    to give the script enough time to do what it needs to do to finish off safely. Anyway, here is the code…

    <?php
    
    function is_cli() {
      if (function_exists('http_response_code')) {
        return (http_response_code() === false);
      }
      return !isset($_SERVER['REMOTE_ADDR']);
    }
    
    function getstarttime() {
      if (function_exists('getrusage')) {
        $rusage = getrusage();
        if (isset($rusage['ru_utime.tv_sec'])) {
          return $rusage['ru_utime.tv_sec'];
        }
      }
      return time();
    }
    
    function gettimeremaining($starttime) {
      // NOTE: May not work as intended if not on mpm prefork
      // Function returns estimate of the number of seconds remaining before script timeout.
      $met = ini_get('max_execution_time');
      if ($met == '0') return false;
      if (function_exists('getrusage')) {
        $rusage = getrusage();
        if (isset($rusage['ru_utime.tv_sec'])) {
          return $met - ($rusage['ru_utime.tv_sec'] - $starttime);
        }
      }
      return $met - (time() - $starttime);
    }
    
    $starttime = getstarttime();
    $br = "";
    if (is_cli()) {
      echo("This is cli.n");
    } else {
      $br = "<br />";
      echo("This is not cli.$brn");
    }
    for($i=0;$i<5000000000;$i++); 
    sleep(10);
    $remainingtime = gettimeremaining($starttime);
    if (Is_Bool($remainingtime)) {
      echo("There is no time limit.$brn");
    } else {
      echo "There are $remainingtime seconds remaining before timeout.$brn";
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search