php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70130 usleep(1) CPU usage
Submitted: 2015-07-24 16:37 UTC Modified: 2018-06-16 17:19 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
From: saniafd93 at gmail dot com Assigned:
Status: Re-Opened Package: Unknown/Other Function
PHP Version: 5.5.27 OS: Ubuntu 14.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
34 + 33 = ?
Subscribe to this entry?

 
 [2015-07-24 16:37 UTC] saniafd93 at gmail dot com
Description:
------------
*PHP version 5.5.19

Short time ago I wrote an app, that uses usleep(1) for reducing CPU usage. I wrote it on my Windows 8.1 machine, and it's worked as suspected: CPU usage was reduced to 0%-1%. After that, I runned it on Ubuntu 14.04, and there was same behavior: 0%-1.5% CPU usage.

All worked nice untill March 2015, when on Ubuntu CPU usage was increased up to 48% (2-core server). I tested that app again, and found that on Windows CPU usage was still 0%-1%.

I tested it in several environments: 2core Windows 8.1, 4core Windows 8.1, 2core Ubuntu 14.04, 4core Ubuntu 14.04.

Test script:
---------------
<?php
// usage below is right for Ubuntu 14.04 machines
// Windows 8.1 machines all the time have 0%-1.5%

while (true) {
	usleep(1);     // CPU usage: ~48% 2core machine, ~25% 4core machine
	usleep(10000); // CPU usage:  ~5% 2core machine,  ~1% 4core machine
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-24 17:11 UTC] saniafd93 at gmail dot com
I was wrong about number of cores. Here is new summary with processor models:

 - 2core Windows 8.1  (Intel Pentium P6100 2.0 GHz)
	usleep(1);     // CPU usage: 0%-1.5%
	usleep(10000); // CPU usage: 0%-1%
 - 4core Windows 8.1  (Intel Core i3 4130 3.4 GHz)
	usleep(1);     // CPU usage: 0%-1%
	usleep(10000); // CPU usage: 0%-1%
 - 4core Ubuntu 14.04 (Intel Celeron J1900 2.0-2.42GHz)
	usleep(1);     // CPU usage: ~25%
	usleep(10000); // CPU usage:  ~5%
 - 8core Ubuntu 14.04 (Intel Xeon E5506 2.13GHz)
	usleep(1);     // CPU usage: ~48%
	usleep(10000); // CPU usage:  ~1%
 [2015-07-28 23:46 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2015-07-28 23:46 UTC] cmb@php.net
> All worked nice untill March 2015, [...]

What do you mean by that? Did you update to a newer PHP version?
If so, which version did you use before, and which version do you
use now.
 [2015-07-28 23:46 UTC] cmb@php.net
-Status: Assigned +Status: Feedback
 [2015-07-29 05:28 UTC] saniafd93 at gmail dot com
-Status: Feedback +Status: Assigned
 [2015-07-29 05:28 UTC] saniafd93 at gmail dot com
I didn't update to a new version, because the latest PHP version on production environment (Ubuntu 14.04) was PHP 5.5.19 all the time. I was badly surprised by this bug in a LTS version. Anyway, this is a strange behavior, because as I wrote, untill March all worked fine. CPU usage on production Ubuntu 14.04 was ~1% with usleep(1); Same behavior now requires at least usleep(10000);
 [2015-07-29 06:19 UTC] saniafd93 at gmail dot com
I've already reviewed the PHP versions on my machines:

 - 2core Windows 8.1  - PHP 5.5.13
 - 4core Windows 8.1  - PHP 5.5.19 (also tested PHP 5.6)
 - 4core Ubuntu 14.04 - PHP 5.5.9
 - 8core Ubuntu 14.04 - PHP 5.5.9

But the fact remains: 5.5.9 before March worked fine
 [2015-08-10 22:37 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2015-08-10 22:37 UTC] cmb@php.net
> I didn't update to a new version, because the latest PHP version
> on production environment (Ubuntu 14.04) was PHP 5.5.19 all the
> time.

Hard to tell what's causing the problem then. It might not be
a PHP issue at all.
 [2015-12-09 01:15 UTC] yohgaki@php.net
-Status: Open +Status: Feedback
 [2015-12-09 01:15 UTC] yohgaki@php.net
I tested on my Fedora 22 64bit with PHP 5.5/5.6/7.0 (today's git) and didn't have this issue.

@saniafd93 do you still have this problem?
 [2015-12-20 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2015-12-20 11:16 UTC] saniafd93 at gmail dot com
@yohgaki yes, bug still present on Ubuntu 14.04 (kernel version 3.13.0-65, php version 5.5.9-1). Sorry for my late answer.
 [2015-12-20 11:39 UTC] requinix@php.net
-Status: No Feedback +Status: Re-Opened
 [2015-12-29 04:46 UTC] cyril at grazetech dot com
PHP 5.6.11-1ubuntu3.1 
Ubuntu 15.10
Intel Core i5-5257U CPU @ 2.70GHz


I am seeing 4% - 6% CPU usage with usleep(1)
 [2018-06-16 17:19 UTC] cmb@php.net
-Package: SPL related +Package: Unknown/Other Function
 [2021-04-21 13:02 UTC] gerdchristian dot kunze at googlemail dot com
Same behavior on Ubuntu 20.04 Php 7.4

Hardware:
 - Synology DS920+
Virtual Machine:
 - 4 Core 2 GB RAM Default install in VirtualMachineManager (from Synology)
OS:
 - Ubuntu 20.04 (latest updates at comment)
 - Installed from LegacyServerISO (not LiveISO) as minimum virtual machine
Webserver:
 - Apache 2.4.41 Default install
PHP:
 - 7.4.3 FPM Default install

Test:
 - while(true){usleep(1);}
Result:
 - Cpu @ ~25%

Test:
 - while(true){usleep(1000);}
Result:
 - Cpu @ ~2%

Test:
 - while(true){sleep(1);}
Result:
 - Cpu @ ~1%
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 20:01:45 2024 UTC