7

I have a website with a custom script running on a VPS hosting service. All scripts go out through index.php using mod_rewrite and .htaccess for friendly URLs

Something in my script is generating high CPU Usage, as show: CPU usage

When I go and strace a given process I get this that I don't understand:

    setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={90, 0}}, NULL) = 0
    rt_sigaction(SIGPROF, {0x7a6b8f, [PROF], SA_RESTORER|SA_RESTART, 0x2af8ae8742f0}, {0x7a6b8f, [PROF],    SA_RESTORER|SA_RESTART, 0x2af8ae8742f0}, 8) = 0
    rt_sigprocmask(SIG_UNBLOCK, [PROF], NULL, 8) = 0

This is repeated continuously in an infinite loop.

What I need to know is how to track down the exact PHP script causing this problem. Any suggestions?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    Enable error logging. Set max execution time. Take a look into the error log which script misbehaves. Probably create your own error handler that is able to dump request details in case that timeout error is triggered, so you can start to reproduce this with a remote debugger. – hakre Jun 28 '12 at 20:49
  • And which API are you using? CGI? – hakre Jun 28 '12 at 20:59

2 Answers2

6

If you can get the PECL proctitle package into your PHP install, you can use that to make index.php set its process title to the name of the script it handed off to. (The altered process title may or may not show up in particular process table visualizers; use ps if all else fails.)

chaos
  • 122,029
  • 33
  • 303
  • 309
  • @chaos how do i know if that is available on my host? if not, how do i install it, and if it is installed.. how do i use it? – Vinicio Barrantes Jun 29 '12 at 02:22
  • 1
    `` should display all installed PHP modules. `pecl install proctitle` should install the package chaos mentioned. [See the PECL docs for more info](http://www.php.net/manual/en/install.pecl.downloads.php) – Josh Jun 29 '12 at 12:15
2

Use the getmypid() function in your script. Just output this to a log file or something to see which script is which.

EDIT: Use the auto_prepend_file config setting to automatically include this snippet in all your files:

php_value auto_prepend_file append.php
davidethell
  • 11,708
  • 6
  • 43
  • 63
  • Of course he doesn't. I'm assuming he is trying to figure this out by debugging his scripts. He didn't say he can't edit his scripts. I am suggesting the OP edit his scripts to note what PID each script is using to help in his tracing efforts. – davidethell Jun 28 '12 at 20:49
  • 1
    So you're suggesting he add that to every PHP file? OK. I think I see what you mean. If he makes each script log `__FILE__` *and* `getmypid()` then this is a valid answer. – Josh Jun 28 '12 at 20:50
  • Correct. And he can use the auto_prepend_file setting to include it in all his files in one move. – davidethell Jun 28 '12 at 21:01
  • I'm with you now. Sorry for misunderstanding at first! – Josh Jun 28 '12 at 21:02
  • @davidethell at first i thought that was nonsense, but with your edit it made perfect sense. Althought i still only get index.php in my log since all php files are being called by require_once or include throuth index.php I´m using getmypid() for loggin the pid and $_SERVER['SCRIPT_FILENAME'] for the php file. Do you suggest any other function to get the exact script name, beacuase $_SERVER['SCRIPT_FILENAME'] = index.php all the time – Vinicio Barrantes Jun 28 '12 at 23:20
  • @Josh swithing $_SERVER['SCRIPT_FILENAME'] for `__FILE__` returns only the name of the preppended file... – Vinicio Barrantes Jun 28 '12 at 23:27
  • I managed to create this log --> http://grupozermat.com/pid_log.txt using the auto_append_file config and then a foreach for get_included_files() Although, none of the pids on the log matches the ones on the top -c report on SSH When i run lsof - p {pid} i get files outside my project http://www.dropmocks.com/mBjoTl is there a way to get related processes on SSH or Php? – Vinicio Barrantes Jun 29 '12 at 01:25
  • You're right about __FILE__. I'll have to think about how to get the real name. – davidethell Jun 29 '12 at 02:04
  • Vincinio, if you logged the URL ($_SERVER['REQUEST_URI']) used to access the script would that tell you which PHP file was used? – davidethell Jun 29 '12 at 02:09
  • @davidethell no, $_SERVER['REQUEST_URI'] returns the script running for a GET or POST request, which always will be index.php – Vinicio Barrantes Jun 29 '12 at 03:33
  • Can you update your question to include an example of how your files are structured? Maybe if we understand how your scripts are feeding through index.php it would help. – davidethell Jun 29 '12 at 07:22