Issam Posted November 14, 2014 Share Posted November 14, 2014 Hi to all; Is there any way, a function for example in php to add to my script in order to guess if the running script is facing a cpu overload or a memory overload or a slow processing speed ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/ Share on other sites More sharing options...
requinix Posted November 14, 2014 Share Posted November 14, 2014 With effort, yes. What exactly are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496618 Share on other sites More sharing options...
Issam Posted November 14, 2014 Author Share Posted November 14, 2014 Hi; I would like to sleep the script using sleep function or even usleep while the system is in overload condition. do { $is_memory_overload = check_memory_overload(); $is_cpu_overload = check_cpu_overload(); $is_database_overloaded = check_database_overload(); sleep(1); } while($is_memory_overload || $is_cpu_overload || $is_database_overloaded); The idea is fairly simple, do nothing while there is an overload which can be from cpu or memory or database. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496619 Share on other sites More sharing options...
requinix Posted November 15, 2014 Share Posted November 15, 2014 Okay, but is there an underlying problem you're trying to solve? Putting a script to sleep because of resource problems isn't always the right answer. Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496620 Share on other sites More sharing options...
NotionCommotion Posted November 15, 2014 Share Posted November 15, 2014 Issam, I tend to agree with Requinix's questions/recommendations that you might be chasing the wrong carrot. If you really wanted to do so, my first inclination was to use some sort of native operating system function (are you running Linux/Windows/etc?) along with PHP's exec(), and then parse if if necessary. On a whim, I Goggled "php find cpu load", and the first hit was http://php.net/manual/en/function.sys-getloadavg.php, so you might want to check out this as well. Before doing so, however, you should find out what is causing over usage and deal with it. Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496622 Share on other sites More sharing options...
Issam Posted November 15, 2014 Author Share Posted November 15, 2014 Okay, but is there an underlying problem you're trying to solve? Putting a script to sleep because of resource problems isn't always the right answer. Okay, i have a function that uses a garbage collector and may consume lot of resources, so i would like to run it and at same time catch the event of resources overload. More like that: try { use_my_garbage_func(); } catch($ServerOverload) { // 1. Terminate or free resources // 2. Die or kill the script } So what i need is how to figure if there is a server overload, it can be database, cpu or memory and the script should finish even if it is not itself which is causing the overload, because someone will ask me why don't you optimize your function so for that reason. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496625 Share on other sites More sharing options...
Issam Posted November 15, 2014 Author Share Posted November 15, 2014 (edited) Issam, I tend to agree with Requinix's questions/recommendations that you might be chasing the wrong carrot. If you really wanted to do so, my first inclination was to use some sort of native operating system function (are you running Linux/Windows/etc?) along with PHP's exec(), and then parse if if necessary. On a whim, I Goggled "php find cpu load", and the first hit was http://php.net/manual/en/function.sys-getloadavg.php, so you might want to check out this as well. Before doing so, however, you should find out what is causing over usage and deal with it. Hi, sys_getloadavg doesn't return any array when i run it from my shared hosting account, even if it is not disabled but works when test it on localhost. So how to get that info using a different approach ? Thanks. Edited November 15, 2014 by Issam Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496626 Share on other sites More sharing options...
Issam Posted November 15, 2014 Author Share Posted November 15, 2014 Hi again, the command is : $res = shell_exec('ps aux --sort -rss'); Now, how to extract total memory usage and total cpu usage from the result ? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496627 Share on other sites More sharing options...
requinix Posted November 15, 2014 Share Posted November 15, 2014 (edited) Ah. Shared hosting. That explains it. 1. You're using enough resources that there's a risk of the machine running out and your hosting provider isn't going to like that. There's probably wording in the ToS to that effect. 2. You can generally get information like CPU and memory usage on a machine, but it's only worth it if you can target a very specific setup - not just Windows vs Linux but which version running in what environment. And on shared hosting the tactics you can use are often unavailable as half the time they involve you running something through exec() or system(). 3. You're pushing the bounds of what shared hosting can provide. Consider upgrading to dedicated hosting. ps gives a full breakdown by process. You need overall statistics. Try uptime for load (a better indicator than CPU usage) and free for memory usage. Edited November 15, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496628 Share on other sites More sharing options...
Issam Posted November 15, 2014 Author Share Posted November 15, 2014 Hi, i know i need a vps for that, i have one but i would like to check if it work on shared or no. Thanks for giving me the right commands, just please explain to me what does uptime return ? for me this is the output : uptime command: 04:03:03 up 9 days, 5:28, 4 users, load average: 0.34, 0.44, 0.56 free command: Mem: 2941848 2811196 130652 172904 234948 1104000 -/+ buffers/cache: 1472248 1469600 Swap: 1028092 16668 1011424 How to extract the valuable information i need from the output using php ? i need to extract cpu load, current memory usage and total memory available. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496630 Share on other sites More sharing options...
Solution requinix Posted November 15, 2014 Solution Share Posted November 15, 2014 (edited) The three numbers from uptime are load, which is (more than) a measure of how active the CPUs are. Say, 0.01 per percent per CPU, so a 2-CPU machine at 100% CPU would show 2.00. But the numbers can go higher. The three correspond to the average load over the last 1, 5, and 15 minutes. if (preg_match_all('/\d+\.\d\d/', $output, $values)) { list($one, $five, $fifteen) = $values[0];With the headings, free shows total used free shared buffers cached Mem: 2040152 1926196 113956 248 26764 1724268 -/+ buffers/cache: 175164 1864988 Swap: 2093052 0 2093052The first three numbers give you everything you need (measured in KBs) and you can grab them easily with if (preg_match_all('/\d+/', $output, $values)) { list($total, $used, $free) = $values[0]; Edited November 15, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/292472-check-if-server-cpu-or-memory-is-overloaded/#findComment-1496698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.