Jayvnl Posted August 6, 2010 Share Posted August 6, 2010 Hi, My hosting disabled my account because my site used too many system resources (CPU). There is no way for me to know how much CPU my site is using since i am not a psychic or god so no clue how they expect me to monitor this or fix this (the site has been running just fine for 2 years with the same code). Is there any code that can tell me how much CPU my site is using on the server? Thanks in advance, Jay Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/ Share on other sites More sharing options...
sspoke Posted August 6, 2010 Share Posted August 6, 2010 http://phpsysinfo.sourceforge.net/ you can't actually check how much CPU your script is running although you can add a timestamp to start at top of page and a subtract it from a new timestamp reading to get a value how long it took your site to load but it doesn't reflect the cpu usage i guess.. and you can tell that you site loads under a second etc.. blah completely off topic tbh nothing can really do this unless you have remote access of the machines desktop/terminal Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095854 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 Thank you for the reply I know they are being completely unprofessional about this and i don't know what i can do to make it better so that they will lift the block on my account... Is there a way to 'slip' php code or something to make it run smoother/faster/better ? (i know that's off topic). Anyway thanks Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095855 Share on other sites More sharing options...
PFMaBiSmAd Posted August 6, 2010 Share Posted August 6, 2010 It's somewhat your web host's responsibility to provide you with as much information that they know about the problem, not just to say your site consumed too many resources. You would need to know which pages where being requested and how frequently they were being requested at the time the over-usages occurred (perhaps your site just got popular enough that you need to move it to a plan that provides more resources.) Someone could also have hijacked part of your site and is using it to host their media files or as a phishing site. Have you checked if your files are the only ones present? For any particular page (hopefully the one(s) that are causing the problem), you can look at ways to reduce the amount of processing. We would only be able to help if you posted actual code for a page. You might need to make use of some caching to prevent processing the same information over and over for each page request. Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095858 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 Thank you I will look into this. Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095859 Share on other sites More sharing options...
sspoke Posted August 6, 2010 Share Posted August 6, 2010 yah what hosting is this anyways? Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095860 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 JustHost Worst choice of my life..... Lots of downtime, horrific support and as i found out today..... no warning or heads up but a suspended account without clarifying or trying to suggest fixes. The fix they did offer was to move to a dedicated server (135 bucks a month), wish i had the money for that! lol Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095864 Share on other sites More sharing options...
PFMaBiSmAd Posted August 6, 2010 Share Posted August 6, 2010 If you have access to the access log, you can look for any large groups of requests, pick the most frequently requested file, and attempt to optimize its cpu usage. You can also look at the access log for anything that is being requested that should not be (perhaps someone is probing your site either to break in or to trigger errors to shut your site down.) There are probably 50 different things that your code could be doing that are wasteful of processing time, such as including a file using a URL instead of a file system path, performing the same query more than once on a page, putting queries inside of loops ... Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095866 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 Monthly stats show i have between 50 and 60GB bandwidth use and between 6 and 7 million hits. While i have no clue how the bandwidth could be that high the hits are pretty much the same as they have always been. I will check logs, dig through my FTP and such to make sure everything is in order. Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095867 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 It looks like caching is the answer to my issues from what i read after googling it. Does anyone have a good noobie site/tutorial that will tell me how i can start implementing this? (i know 000.1% PHP, a buddy did all the coding and he won't be available til December... so i'm on my own :/) Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095870 Share on other sites More sharing options...
xcoderx Posted August 6, 2010 Share Posted August 6, 2010 one of the worst webhost that is, Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095885 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 Any suggestions for a better one that has no issues with such (and higher) traffic are most welcome Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095886 Share on other sites More sharing options...
joel24 Posted August 6, 2010 Share Posted August 6, 2010 there is a PHP function to check the system load sys_getloadavg(); as for a good host, I've always found dreamhost great. Reasonably priced, good uptime, easy to upgrade to a virtual private server and increase/decrease the power of that VPS. Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095897 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 Thanks for the reply. How do i implement this sys_getloadavg(); just <?php sys_getloadavg(); ?> ? (i really don't know jack about PHP....) Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095899 Share on other sites More sharing options...
joel24 Posted August 6, 2010 Share Posted August 6, 2010 you "don't know jack about PHP" and yet you have a site getting 6 or 7million hits per month? details for implementations and what values the function returns are here http://php.net/manual/en/function.sys-getloadavg.php Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095904 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 The bible is just paper yet half the world reads it, it's contents that matter not the presentation Thanks for the link. Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095906 Share on other sites More sharing options...
Jayvnl Posted August 6, 2010 Author Share Posted August 6, 2010 I put the code in a new php page, uploaded it and when i load it i just see a blank page. This is the code i put in: <?php $load = sys_getloadavg(); if ($load[0] > 80) { header('HTTP/1.1 503 Too busy, try again later'); die('Server too busy. Please try again later.'); } ?> What does the 80 stand for in this case? Percent cpu load or... ? What kind of tweaks do i have to make to get this to work, it looks like this script will automatically deny webpage access if the load becomes too high, just what i need! Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095909 Share on other sites More sharing options...
joel24 Posted August 6, 2010 Share Posted August 6, 2010 the function sys_getloadavg() returns an array of the system load average 1 minute ago, 5minutes ago and 15minutes ago. You're code is not returning anything because it is not greater than 80, it returns the number of processes queued up for execution. You can do print_r(sys_getloadavg()); and it will return the array with those three values. You're code if if($load[0] > 80) will work, but only if the load avg 1 minute ago was > 80. You'll need to change 80 to a value which corresponds to a high system load on your server, and what that value is I don't know, you'll have to play around with it and find out. Quote Link to comment https://forums.phpfreaks.com/topic/209956-cpu-usage/#findComment-1095939 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.