Merge "Don't check namespace in SpecialWantedtemplates"
[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 https://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 global $wgUDPProfilerPort, $wgUDPProfilerHost, $wgUDPProfilerFormatString;
44
45 // Initialize port, host, and format from config, back-compat if available
46 if ( isset( $this->params['udpport'] ) ) {
47 $this->port = $this->params['udpport'];
48 } elseif ( $wgUDPProfilerPort ) {
49 $this->port = $wgUDPProfilerPort;
50 }
51
52 if ( isset( $this->params['udphost'] ) ) {
53 $this->host = $this->params['udphost'];
54 } elseif ( $wgUDPProfilerHost ) {
55 $this->host = $wgUDPProfilerHost;
56 }
57
58 if ( isset( $this->params['udpformat'] ) ) {
59 $this->format = $this->params['udpformat'];
60 } elseif ( $wgUDPProfilerFormatString ) {
61 $this->format = $wgUDPProfilerFormatString;
62 }
63 }
64
65 public function canUse() {
66 # Sockets are not enabled
67 return function_exists( 'socket_create' );
68 }
69
70 public function log( array $stats ) {
71 $sock = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
72 $plength = 0;
73 $packet = "";
74 foreach ( $stats as $pfdata ) {
75 $pfline = sprintf( $this->format,
76 $this->collector->getProfileID(),
77 $pfdata['calls'],
78 $pfdata['cpu'] / 1000, // ms => sec
79 0.0, // sum of CPU^2 for each invocation (unused)
80 $pfdata['real'] / 1000, // ms => sec
81 0.0, // sum of real^2 for each invocation (unused)
82 $pfdata['name'],
83 $pfdata['memory']
84 );
85 $length = strlen( $pfline );
86 if ( $length + $plength > 1400 ) {
87 socket_sendto( $sock, $packet, $plength, 0, $this->host, $this->port );
88 $packet = "";
89 $plength = 0;
90 }
91 $packet .= $pfline;
92 $plength += $length;
93 }
94 socket_sendto( $sock, $packet, $plength, 0x100, $this->host, $this->port );
95 }
96 }