Excessive Resource usage by PHP : How to find the exact cause?

martin MHC

Well-Known Member
Sep 14, 2016
154
25
28
UK
cPanel Access Level
Root Administrator
I have an account on the server that's been merrily normal forever. Yesterday I adjusted an included PHP file (included into index.php):



And since this change started getting persitent server notifications of:

Time: Thu Oct 5 13:18:27 2017 +0100
Account: accountname
Resource: Process Time
Exceeded: 62126 > 7500 (seconds) Executable: /cdf/cpanel/ea-php56/.../php-cgi
Command Line: /cdf/cpanel/.../php-cgi> /home3/accountname/public_html/index.php
PID: 3668 (Parent PID: 2416)
Killed: No​

Now, I can clearly see that the issue is somehow the /home3/accountname/public_html/index.php file, but I have no Apache error logs for this account and no PHP errors (even with error_reporting(E_ALL)).

I have also explored the daily process log for CPU usage and this is negligable, so it's definitely a PHP / Memory issue.

The issue does not appear on local-machine testing of the page.

These error logs are comming in at ~20 an hour, for the last 6 hours and I can't find a way of establishing which part of the PHP file is causing the exceedage?

While other posts on here states the issue may be with FastCGI (which is used here, see above), but

i) This has not been an issue previously. and

ii) Only this account (of many) is effected.

iii) Only the index.php file is effected

How do I find which part of the PHP file is draining the server memory?

NOTE:
I don't want to ignore this issue; as all the other posts seem to suggest, I want to be able to view the cause of the issue to establish for myself if I should ignore it or fix some memory gobbling code (a while statement maybe).
 

martin MHC

Well-Known Member
Sep 14, 2016
154
25
28
UK
cPanel Access Level
Root Administrator
Out of curiousity, if you reverse these changes does the issue go away?
No. reverting the changes does not cease the notifications. Even if it did, I'd still like to know how the best way of finding which part of a file causes this issue, if it's logged and if so where to find this log. Cheers
 

martin MHC

Well-Known Member
Sep 14, 2016
154
25
28
UK
cPanel Access Level
Root Administrator
Yes, there are no error logs that I've found, as I said.

By your line of questions, I should maybe clarify that while this is an issue, my motive to post here is how to debug this and similar issues in the future; rather than simply getting a specific answer for this specific case (although that would also be good!).

If the generated server error log is able to tell me which part of the file is causing the excessive usage?

Thank you for your help though :)
 

martin MHC

Well-Known Member
Sep 14, 2016
154
25
28
UK
cPanel Access Level
Root Administrator
No clue.


What did you change exactly?
Hello.
I setup using mb_ functions for string work turning strlen into mb_strlen

It is not something I would expect to use excessive resources and the publicfacing side of the pages works fine, such as there's no delay in page output etc. indicative of a code issue (such as infinite looping).
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,912
2,241
363
Hello,

Is PHP-FPM enabled for this domain name? If so, you may also want to review the PHP-FPM error log at:

Code:
/opt/cpanel/ea-php$$/root/usr/var/log/php-fpm/error.log
Replace "ea-php$$" with the version of PHP assigned to the account.

Thank you.
 

martin MHC

Well-Known Member
Sep 14, 2016
154
25
28
UK
cPanel Access Level
Root Administrator
Hi Michael,
Thanks for suggestion. PHP FPM is not installed in these accounts. PHP version is 5.6.

I have resolved the issue itself by rebooting the server. But my query still remains in principle how to investigate exact causes in PHP scripts of excessive resource usage notifications.

Cheers
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,912
2,241
363
But my query still remains in principle how to investigate exact causes in PHP scripts of excessive resource usage notifications.
Hello,

There's no set guidelines on how to determine the cause of a PHP script using excessive resources, but you may find the following StackOverflow discussions helpful:

PHP and MySQL: Find out which file or query is causing heavy CPU load
How to find which PHP script is leaking memory?
Determining a PHP script's execution time
find which function(file) causes server overload

Additionally, the following thread is useful for troubleshooting overall load issues:

Troubleshooting high server loads on Linux servers

Thank you.