Remove deprecated profiling config parameters, clarify docs
[lhc/web/wiklou.git] / includes / profiler / output / ProfilerOutputUdp.php
1 <?php
2 /**
3 * Profiler sending messages over UDP.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 */
23
24 /**
25 * ProfilerSimpleUDP class, that sends out messages for 'udpprofile' daemon
26 * (see http://git.wikimedia.org/tree/operations%2Fsoftware.git/master/udpprofile)
27 *
28 * @ingroup Profiler
29 * @since 1.25
30 */
31 class ProfilerOutputUdp extends ProfilerOutput {
32 /** @var int port to send profiling data to */
33 private $port = 3811;
34
35 /** @var string host to send profiling data to */
36 private $host = '127.0.0.1';
37
38 /** @var string format string for profiling data */
39 private $format = "%s - %d %f %f %f %f %s\n";
40
41 public function __construct( Profiler $collector, array $params ) {
42 parent::__construct( $collector, $params );
43
44 // Initialize port, host, and format from config, back-compat if available
45 if ( isset( $this->params['udpport'] ) ) {
46 $this->port = $this->params['udpport'];
47 }
48
49 if ( isset( $this->params['udphost'] ) ) {
50 $this->host = $this->params['udphost'];
51 }
52
53 if ( isset( $this->params['udpformat'] ) ) {
54 $this->format = $this->params['udpformat'];
55 }
56 }
57
58 public function canUse() {
59 # Sockets are not enabled
60 return function_exists( 'socket_create' );
61 }
62
63 public function log( array $stats ) {
64 $sock = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
65 $plength = 0;
66 $packet = "";
67 foreach ( $stats as $pfdata ) {
68 $pfline = sprintf( $this->format,
69 $this->collector->getProfileID(),
70 $pfdata['calls'],
71 $pfdata['cpu'] / 1000, // ms => sec
72 0.0, // sum of CPU^2 for each invocation (unused)
73 $pfdata['real'] / 1000, // ms => sec
74 0.0, // sum of real^2 for each invocation (unused)
75 $pfdata['name'],
76 $pfdata['memory']
77 );
78 $length = strlen( $pfline );
79 if ( $length + $plength > 1400 ) {
80 socket_sendto( $sock, $packet, $plength, 0, $this->host, $this->port );
81 $packet = "";
82 $plength = 0;
83 }
84 $packet .= $pfline;
85 $plength += $length;
86 }
87 socket_sendto( $sock, $packet, $plength, 0x100, $this->host, $this->port );
88 }
89 }