source: phptop/phptop_hook.php @ 264

Last change on this file since 264 was 264, checked in by zerodeux, 11 years ago

Releasing 0.3.1 - \n in error.log bugfix + more robust parsing

File size: 2.0 KB
Line 
1<?php
2/*
3 * phptop - Analyse quickly system ressource usage across many PHP queries
4 * Copyright (C) 2009 Bearstech - http://bearstech.com/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20function _phptop_init() {
21  global $_phptop_t0;
22  $t0 = microtime(TRUE);
23  $ru = getrusage();
24  $_phptop_t0 = array(
25    'time' => $t0,
26    'tusr' => $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6,
27    'tsys' => $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6
28  );
29  register_shutdown_function('_phptop_fini');
30}
31
32function _phptop_fini() {
33  global $_phptop_disable;
34  if ($_phptop_disable) return;
35
36  global $_phptop_t0;
37  $t1   = microtime(TRUE);
38  $time = $t1 - $_phptop_t0['time'];
39
40  $ru = getrusage();
41  $tusr = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6 - $_phptop_t0['tusr'];
42  $tsys = $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6 - $_phptop_t0['tsys'];
43
44  $mem   = memory_get_peak_usage(TRUE);
45
46  $inc   = count(get_included_files());
47
48  $proto = isset($_SERVER['HTTPS']) ? 'https' : 'http';
49  $vhost = $_SERVER['SERVER_NAME'];
50  $uri   = $_SERVER['REQUEST_URI'];
51  $self  = $vhost != '' ? "$proto://$vhost$uri" : $_SERVER['SCRIPT_FILENAME'];
52
53  $msg = sprintf("phptop %s time:%.6F user:%.6F sys:%.6F mem:%g inc:%g", $self, $time, $tusr, $tsys, $mem, $inc);
54  error_log($msg);
55}
56
57/* Don't run in CLI, it pollutes stderr and makes cronjob un-needingly noisy */
58if (php_sapi_name() != 'cli') _phptop_init();
59?>
Note: See TracBrowser for help on using the repository browser.