Showing PHP resource usage

I turned on resource logging for my site a while ago, but the reports have not been very useful so far, since all my php scripts simply show up as “php.cgi”. So, I followed these Dreamhost-supplied instructions in the hopes of fixing the problem.

Unfortunately, I ran into the following two problems:

  1. Trying to access any page with a .php ending now results in the following error message:

and a link to this page of the PHP manual.

Curiously, this only happens if the .php ending is visible in the URL - for example, accessing /folder/index.php does not work, but /folder/ does, even though they are the same file.

  1. Even after this change, the resource logs don’t provide any more insight than before - they now simply list “php” instead of “php.cgi”, but no script name or anything.

I’ve already contacted support about the first issue, but the only suggestion I received was to “reverse the directions so they’re running PHP as CGI again, and try to find a different way to see why is your usage so high. We just give pointers to find out about your script, but we are not able to figure it out for you.” A bit disappointing since I did not ask for Dreamhost to figure anything out except why the instructions they supply do not work :frowning:

So, I’m turning here in the hopes that another customer has gotten this to work and can maybe help me :slight_smile: Thanks!

Did you solve the first problem? I have the same on my site…

No, unfortunately not :frowning: I just switched back to running PHP as CGI. I’d love to see an exact resource report, but it’s not worth it if it breaks my site.

I guess it’s not just me then - if anyone has figured out a way to make it work right, I’d love to hear it :slight_smile:

The instructions also did not work for me. My support request is now at 31 hours without a reply… My sites get 82,000 php requests per day so tracking down which of those pages are resource intensive is pretty much impossible without the resources logs being more specific…

I’m really disappointed to see DH move to such a rediculously low CPU limit. I don’t have a problem optimizing my pages since that will also speed them up for the user, but at least make it a reasonable limit in-line with the bandwidth/disk usage limits.

This solution didn’t work for me, so I wrote this function. On my site I include a PrintHeader() and PrintFooter() function on every page, so it’s a convenient place to put it. Otherwise you’ll need this code on every page.

  1. Put this at the very top of the script (in the header): global $start_time; $start_time = microtime();
  2. At the very end (in the footer) put this:
    global $start_time;
    $ra_start = explode(’ ‘, $start_time);
    $ra_end = explode(’ ‘, microtime());
    $cpu_time = ($ra_end[1]+$ra_end[0]) - ($ra_start[1]+$ra_start[0]);
    $f = fopen(’/home/kallahar/cpu_usage.log’, ‘a’, 1);
    // time seconds request by_ip
    fwrite($f, date(‘m-d-Y H:m:s’)."\t".$cpu_time."\t".$_SERVER[‘SERVER_NAME’]."\t".$_SERVER[‘PHP_SELF’]."\t".$_SERVER[‘REMOTE_ADDR’]."\n");
  3. create a file called cpu_usage.log in the root directory (the one in the fopen command)

This creates a tab delimited log for every page you include this on with microsecond timing. You can then sort by domain, usage, etc.