2

Hi guys I have a weird question,

I have a cli php script runs on Centos 5.x which uses usleep (somtimes 1sec, sometimes 2sec, somtimes 100ms it depends) if there is some wait required, but what I have noticed its that once on usleep() it seems to use about 40% of idle CPU:

Cpu(s):  5.3%us, 21.3%sy,  0.0%ni, 57.2%id,  0.0%wa,  0.0%hi,  0.0%si, 16.1%st

any ideas ?
cheers

Marcin
  • 5,469
  • 15
  • 55
  • 69
  • Most of the time is spent in kernel mode, try strace on the process to see what syscalls is it invoking. – jpalecek Mar 25 '10 at 12:23

2 Answers2

4

This doesn't happen for me with a very simple testcase. Try the following on your system to see if you still get the excessive CPU time.

Script test.php:

<?php
for ($n=0;$n<1000;$n++)
{
  usleep(10);
}
?>

Then at the command line run: time php test.php

My results are as follows:

[ar@arctic ~]$ cat /etc/redhat-release 
CentOS release 5.2 (Final)
[ar@arctic ~]$ time php test.php 

real    0m1.020s
user    0m0.013s
sys     0m0.006s

You can see that the user and sys time are very very small in comparison to the real (or elapsed) time. i.e. CPU utilisation was very low.

a'r
  • 35,921
  • 7
  • 66
  • 67
  • here you go my times= real: 0m4.941s, user: 0m0.036s, sys: 0m0.036s. but how this one is realeted to the CPU usage anyway ? – Marcin Mar 25 '10 at 13:07
  • 1
    From your results, I would conclude that `usleep` is not your problem. Can you show us what else is in your code around usleep - I presume you are in some sort of loop. – a'r Mar 25 '10 at 13:51
3

Under Windows systems, if you do not set the time limit of the execution of your script to 0 (set_time_limit(0);), the php executable will eat as much as 50% CPU power when using sleep or usleep functions.

pars
  • 3,700
  • 7
  • 38
  • 57