source: phptop/phptop_hook.php

Last change on this file was 580, checked in by zerodeux, 10 years ago

phptop 0.5.3 release

File size: 2.0 KB
Line 
1<?php
2/*
3 * phptop - Analyse quickly system ressource usage across many PHP queries
4 * Copyright (C) 2009,2010,2011 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  $proto = isset($_SERVER['HTTPS']) ? 'https' : 'http';
47  $vhost = $_SERVER['SERVER_NAME'];
48  $uri   = $_SERVER['REQUEST_URI'];
49  $self  = $vhost != '' ? "$proto://$vhost$uri" : $_SERVER['SCRIPT_FILENAME'];
50
51  $msg = sprintf("phptop %s time:%.6F user:%.6F sys:%.6F mem:%d", $self, $time, $tusr, $tsys, $mem);
52  error_log($msg);
53}
54
55/* Don't run in CLI, it pollutes stderr and makes cronjob un-needingly noisy */
56if (php_sapi_name() != 'cli') _phptop_init();
57?>
Note: See TracBrowser for help on using the repository browser.