← Index
NYTProf Performance Profile   « block view • line view • sub view »
For mojo-test.pl
  Run on Sat Feb 5 19:54:22 2011
Reported on Sat Feb 5 19:54:57 2011

Filename/Users/marcus/perl5/perlbrew/perls/perl-5.10.1/lib/5.10.1/darwin-2level/IO/Socket/INET.pm
StatementsExecuted 27123 statements in 61.8ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
10023211.6ms102msIO::Socket::INET::::newIO::Socket::INET::new
1000119.20ms30.0msIO::Socket::INET::::sockaddrIO::Socket::INET::sockaddr
1000119.14ms42.6msIO::Socket::INET::::sockhostIO::Socket::INET::sockhost
1000118.47ms23.0msIO::Socket::INET::::peeraddrIO::Socket::INET::peeraddr
1000118.26ms32.7msIO::Socket::INET::::peerhostIO::Socket::INET::peerhost
1000118.09ms13.2msIO::Socket::INET::::peerportIO::Socket::INET::peerport
1000117.79ms16.0msIO::Socket::INET::::sockportIO::Socket::INET::sockport
211104µs373µsIO::Socket::INET::::configureIO::Socket::INET::configure
21138µs50µsIO::Socket::INET::::_sock_infoIO::Socket::INET::_sock_info
21129µs64µsIO::Socket::INET::::bindIO::Socket::INET::bind
11122µs822µsIO::Socket::INET::::BEGIN@12IO::Socket::INET::BEGIN@12
11116µs19µsIO::Socket::INET::::BEGIN@9IO::Socket::INET::BEGIN@9
22111µs11µsIO::Socket::INET::::_get_proto_numberIO::Socket::INET::_get_proto_number
1118µs39µsIO::Socket::INET::::BEGIN@13IO::Socket::INET::BEGIN@13
1117µs20µsIO::Socket::INET::::BEGIN@14IO::Socket::INET::BEGIN@14
1117µs2.16msIO::Socket::INET::::BEGIN@11IO::Socket::INET::BEGIN@11
1117µs24µsIO::Socket::INET::::BEGIN@15IO::Socket::INET::BEGIN@15
1115µs5µsIO::Socket::INET::::_get_proto_nameIO::Socket::INET::_get_proto_name
3315µs5µsIO::Socket::INET::::CORE:matchIO::Socket::INET::CORE:match (opcode)
2211µs1µsIO::Socket::INET::::CORE:substIO::Socket::INET::CORE:subst (opcode)
0000s0sIO::Socket::INET::::_cache_protoIO::Socket::INET::_cache_proto
0000s0sIO::Socket::INET::::_errorIO::Socket::INET::_error
0000s0sIO::Socket::INET::::_get_addrIO::Socket::INET::_get_addr
0000s0sIO::Socket::INET::::connectIO::Socket::INET::connect
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1# IO::Socket::INET.pm
2#
3# Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4# This program is free software; you can redistribute it and/or
5# modify it under the same terms as Perl itself.
6
7package IO::Socket::INET;
8
9333µs222µs
# spent 19µs (16+3) within IO::Socket::INET::BEGIN@9 which was called: # once (16µs+3µs) by Mojo::IOLoop::BEGIN@9 at line 9
use strict;
# spent 19µs making 1 call to IO::Socket::INET::BEGIN@9 # spent 3µs making 1 call to strict::import
101300nsour(@ISA, $VERSION);
11327µs24.32ms
# spent 2.16ms (7µs+2.16) within IO::Socket::INET::BEGIN@11 which was called: # once (7µs+2.16ms) by Mojo::IOLoop::BEGIN@9 at line 11
use IO::Socket;
# spent 2.16ms making 1 call to IO::Socket::INET::BEGIN@11 # spent 2.16ms making 1 call to IO::Socket::import
12324µs21.62ms
# spent 822µs (22+800) within IO::Socket::INET::BEGIN@12 which was called: # once (22µs+800µs) by Mojo::IOLoop::BEGIN@9 at line 12
use Socket;
# spent 822µs making 1 call to IO::Socket::INET::BEGIN@12 # spent 800µs making 1 call to Exporter::import
13322µs270µs
# spent 39µs (8+31) within IO::Socket::INET::BEGIN@13 which was called: # once (8µs+31µs) by Mojo::IOLoop::BEGIN@9 at line 13
use Carp;
# spent 39µs making 1 call to IO::Socket::INET::BEGIN@13 # spent 31µs making 1 call to Exporter::import
14319µs232µs
# spent 20µs (7+12) within IO::Socket::INET::BEGIN@14 which was called: # once (7µs+12µs) by Mojo::IOLoop::BEGIN@9 at line 14
use Exporter;
# spent 20µs making 1 call to IO::Socket::INET::BEGIN@14 # spent 12µs making 1 call to Exporter::import
1531.37ms242µs
# spent 24µs (7+17) within IO::Socket::INET::BEGIN@15 which was called: # once (7µs+17µs) by Mojo::IOLoop::BEGIN@9 at line 15
use Errno;
# spent 24µs making 1 call to IO::Socket::INET::BEGIN@15 # spent 17µs making 1 call to Exporter::import
16
1718µs@ISA = qw(IO::Socket);
181300ns$VERSION = "1.31";
19
201600nsmy $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1;
21
2214µs13µsIO::Socket::INET->register_domain( AF_INET );
# spent 3µs making 1 call to IO::Socket::register_domain
23
2411µsmy %socket_type = ( tcp => SOCK_STREAM,
25 udp => SOCK_DGRAM,
26 icmp => SOCK_RAW
27 );
281100nsmy %proto_number;
291600ns$proto_number{tcp} = Socket::IPPROTO_TCP() if defined &Socket::IPPROTO_TCP;
301200ns$proto_number{udp} = Socket::IPPROTO_UDP() if defined &Socket::IPPROTO_UDP;
311200ns$proto_number{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP;
3214µsmy %proto_name = reverse %proto_number;
33
34
# spent 102ms (11.6+90.3) within IO::Socket::INET::new which was called 1002 times, avg 102µs/call: # 1000 times (11.6ms+89.7ms) by IO::Socket::accept at line 222 of IO/Socket.pm, avg 101µs/call # once (25µs+382µs) by Mojo::IOLoop::listen at line 338 of Mojo/IOLoop.pm # once (10µs+201µs) by Mojo::IOLoop::_prepare_loop at line 1358 of Mojo/IOLoop.pm
sub new {
35300610.5ms my $class = shift;
36 unshift(@_, "PeerAddr") if @_ == 1;
37100290.3ms return $class->SUPER::new(@_);
# spent 90.3ms making 1002 calls to IO::Socket::new, avg 90µs/call
38}
39
40sub _cache_proto {
41 my @proto = @_;
42 for (map lc($_), $proto[0], split(' ', $proto[1])) {
43 $proto_number{$_} = $proto[2];
44 }
45 $proto_name{$proto[2]} = $proto[0];
46}
47
48
# spent 11µs within IO::Socket::INET::_get_proto_number which was called 2 times, avg 5µs/call: # once (7µs+0s) by IO::Socket::INET::_sock_info at line 81 # once (4µs+0s) by IO::Socket::INET::configure at line 166
sub _get_proto_number {
49613µs my $name = lc(shift);
50 return undef unless defined $name;
51 return $proto_number{$name} if exists $proto_number{$name};
52
53 my @proto = getprotobyname($name);
54 return undef unless @proto;
55 _cache_proto(@proto);
56
57 return $proto[2];
58}
59
60
# spent 5µs within IO::Socket::INET::_get_proto_name which was called: # once (5µs+0s) by IO::Socket::INET::configure at line 168
sub _get_proto_name {
6136µs my $num = shift;
62 return undef unless defined $num;
63 return $proto_name{$num} if exists $proto_name{$num};
64
65 my @proto = getprotobynumber($num);
66 return undef unless @proto;
67 _cache_proto(@proto);
68
69 return $proto[0];
70}
71
72
# spent 50µs (38+13) within IO::Socket::INET::_sock_info which was called 2 times, avg 25µs/call: # 2 times (38µs+13µs) by IO::Socket::INET::configure at line 145, avg 25µs/call
sub _sock_info {
732342µs my($addr,$port,$proto) = @_;
74 my $origport = $port;
75 my @serv = ();
76
771700ns $port = $1
# spent 700ns making 1 call to IO::Socket::INET::CORE:subst
78 if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,);
79
8012µs if(defined $proto && $proto =~ /\D/) {
# spent 2µs making 1 call to IO::Socket::INET::CORE:match
8117µs my $num = _get_proto_number($proto);
# spent 7µs making 1 call to IO::Socket::INET::_get_proto_number
82 unless (defined $num) {
83 $@ = "Bad protocol '$proto'";
84 return;
85 }
86 $proto = $num;
87 }
88
89 if(defined $port) {
901600ns my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef;
# spent 600ns making 1 call to IO::Socket::INET::CORE:subst
9112µs my $pnum = ($port =~ m,^(\d+)$,)[0];
# spent 2µs making 1 call to IO::Socket::INET::CORE:match
92
931500ns @serv = getservbyname($port, _get_proto_name($proto) || "")
# spent 500ns making 1 call to IO::Socket::INET::CORE:match
94 if ($port =~ m,\D,);
95
96 $port = $serv[2] || $defport || $pnum;
97 unless (defined $port) {
98 $@ = "Bad service '$origport'";
99 return;
100 }
101
102 $proto = _get_proto_number($serv[3]) if @serv && !$proto;
103 }
104
105 return ($addr || undef,
106 $port || undef,
107 $proto || undef
108 );
109}
110
111sub _error {
112 my $sock = shift;
113 my $err = shift;
114 {
115 local($!);
116 my $title = ref($sock).": ";
117 $@ = join("", $_[0] =~ /^$title/ ? "" : $title, @_);
118 $sock->close()
119 if(defined fileno($sock));
120 }
121 $! = $err;
122 return undef;
123}
124
125sub _get_addr {
126 my($sock,$addr_str, $multi) = @_;
127 my @addr;
128 if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
129 (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
130 } else {
131 my $h = inet_aton($addr_str);
132 push(@addr, $h) if defined $h;
133 }
134 @addr;
135}
136
137
# spent 373µs (104+269) within IO::Socket::INET::configure which was called 2 times, avg 187µs/call: # 2 times (104µs+269µs) by IO::Socket::new at line 48 of IO/Socket.pm, avg 187µs/call
sub configure {
1384995µs my($sock,$arg) = @_;
139 my($lport,$rport,$laddr,$raddr,$proto,$type);
140
141
142 $arg->{LocalAddr} = $arg->{LocalHost}
143 if exists $arg->{LocalHost} && !exists $arg->{LocalAddr};
144
145250µs ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr},
# spent 50µs making 2 calls to IO::Socket::INET::_sock_info, avg 25µs/call
146 $arg->{LocalPort},
147 $arg->{Proto})
148 or return _error($sock, $!, $@);
149
15018µs $laddr = defined $laddr ? inet_aton($laddr)
# spent 8µs making 1 call to Socket::inet_aton
151 : INADDR_ANY;
152
153 return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'")
154 unless(defined $laddr);
155
156 $arg->{PeerAddr} = $arg->{PeerHost}
157 if exists $arg->{PeerHost} && !exists $arg->{PeerAddr};
158
159 unless(exists $arg->{Listen}) {
160 ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr},
161 $arg->{PeerPort},
162 $proto)
163 or return _error($sock, $!, $@);
164 }
165
16614µs $proto ||= _get_proto_number('tcp');
# spent 4µs making 1 call to IO::Socket::INET::_get_proto_number
167
16815µs $type = $arg->{Type} || $socket_type{lc _get_proto_name($proto)};
# spent 5µs making 1 call to IO::Socket::INET::_get_proto_name
169
170 my @raddr = ();
171
172 if(defined $raddr) {
173 @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed});
174 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
175 unless @raddr;
176 }
177
178 while(1) {
179
180283µs $sock->socket(AF_INET, $type, $proto) or
# spent 83µs making 2 calls to IO::Socket::socket, avg 42µs/call
181 return _error($sock, $!, "$!");
182
183 if (defined $arg->{Blocking}) {
184 defined $sock->blocking($arg->{Blocking})
185 or return _error($sock, $!, "$!");
186 }
187
188 if ($arg->{Reuse} || $arg->{ReuseAddr}) {
189130µs $sock->sockopt(SO_REUSEADDR,1) or
# spent 30µs making 1 call to IO::Socket::sockopt
190 return _error($sock, $!, "$!");
191 }
192
193 if ($arg->{ReusePort}) {
194 $sock->sockopt(SO_REUSEPORT,1) or
195 return _error($sock, $!, "$!");
196 }
197
198 if ($arg->{Broadcast}) {
199 $sock->sockopt(SO_BROADCAST,1) or
200 return _error($sock, $!, "$!");
201 }
202
203 if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) {
204264µs $sock->bind($lport || 0, $laddr) or
# spent 64µs making 2 calls to IO::Socket::INET::bind, avg 32µs/call
205 return _error($sock, $!, "$!");
206 }
207
208 if(exists $arg->{Listen}) {
209225µs $sock->listen($arg->{Listen} || 5) or
# spent 25µs making 2 calls to IO::Socket::listen, avg 12µs/call
210 return _error($sock, $!, "$!");
211 last;
212 }
213
214 # don't try to connect unless we're given a PeerAddr
215 last unless exists($arg->{PeerAddr});
216
217 $raddr = shift @raddr;
218
219 return _error($sock, $EINVAL, 'Cannot determine remote port')
220 unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW);
221
222 last
223 unless($type == SOCK_STREAM || defined $raddr);
224
225 return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
226 unless defined $raddr;
227
228# my $timeout = ${*$sock}{'io_socket_timeout'};
229# my $before = time() if $timeout;
230
231 undef $@;
232 if ($sock->connect(pack_sockaddr_in($rport, $raddr))) {
233# ${*$sock}{'io_socket_timeout'} = $timeout;
234 return $sock;
235 }
236
237 return _error($sock, $!, $@ || "Timeout")
238 unless @raddr;
239
240# if ($timeout) {
241# my $new_timeout = $timeout - (time() - $before);
242# return _error($sock,
243# (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
244# "Timeout") if $new_timeout <= 0;
245# ${*$sock}{'io_socket_timeout'} = $new_timeout;
246# }
247
248 }
249
250 $sock;
251}
252
253sub connect {
254 @_ == 2 || @_ == 3 or
255 croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)';
256 my $sock = shift;
257 return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_));
258}
259
260
# spent 64µs (29+35) within IO::Socket::INET::bind which was called 2 times, avg 32µs/call: # 2 times (29µs+35µs) by IO::Socket::INET::configure at line 204, avg 32µs/call
sub bind {
261635µs @_ == 2 || @_ == 3 or
262 croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)';
263 my $sock = shift;
264435µs return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_))
# spent 28µs making 2 calls to IO::Socket::bind, avg 14µs/call # spent 7µs making 2 calls to Socket::pack_sockaddr_in, avg 4µs/call
265}
266
267
# spent 30.0ms (9.20+20.8) within IO::Socket::INET::sockaddr which was called 1000 times, avg 30µs/call: # 1000 times (9.20ms+20.8ms) by IO::Socket::INET::sockhost at line 284, avg 30µs/call
sub sockaddr {
26840009.54ms @_ == 1 or croak 'usage: $sock->sockaddr()';
269 my($sock) = @_;
270100010.0ms my $name = $sock->sockname;
# spent 10.0ms making 1000 calls to IO::Socket::sockname, avg 10µs/call
271100010.8ms $name ? (sockaddr_in($name))[1] : undef;
# spent 10.8ms making 1000 calls to Socket::sockaddr_in, avg 11µs/call
272}
273
274
# spent 16.0ms (7.79+8.22) within IO::Socket::INET::sockport which was called 1000 times, avg 16µs/call: # 1000 times (7.79ms+8.22ms) by Mojo::IOLoop::local_info at line 402 of Mojo/IOLoop.pm, avg 16µs/call
sub sockport {
27540006.06ms @_ == 1 or croak 'usage: $sock->sockport()';
276 my($sock) = @_;
27710004.34ms my $name = $sock->sockname;
# spent 4.34ms making 1000 calls to IO::Socket::sockname, avg 4µs/call
27810003.87ms $name ? (sockaddr_in($name))[0] : undef;
# spent 3.87ms making 1000 calls to Socket::sockaddr_in, avg 4µs/call
279}
280
281
# spent 42.6ms (9.14+33.4) within IO::Socket::INET::sockhost which was called 1000 times, avg 43µs/call: # 1000 times (9.14ms+33.4ms) by Mojo::IOLoop::local_info at line 402 of Mojo/IOLoop.pm, avg 43µs/call
sub sockhost {
282400011.8ms @_ == 1 or croak 'usage: $sock->sockhost()';
283 my($sock) = @_;
284100030.0ms my $addr = $sock->sockaddr;
# spent 30.0ms making 1000 calls to IO::Socket::INET::sockaddr, avg 30µs/call
28510003.39ms $addr ? inet_ntoa($addr) : undef;
# spent 3.39ms making 1000 calls to Socket::inet_ntoa, avg 3µs/call
286}
287
288
# spent 23.0ms (8.47+14.5) within IO::Socket::INET::peeraddr which was called 1000 times, avg 23µs/call: # 1000 times (8.47ms+14.5ms) by IO::Socket::INET::peerhost at line 305, avg 23µs/call
sub peeraddr {
28940007.45ms @_ == 1 or croak 'usage: $sock->peeraddr()';
290 my($sock) = @_;
291100010.4ms my $name = $sock->peername;
# spent 10.4ms making 1000 calls to IO::Socket::peername, avg 10µs/call
29210004.06ms $name ? (sockaddr_in($name))[1] : undef;
# spent 4.06ms making 1000 calls to Socket::sockaddr_in, avg 4µs/call
293}
294
295
# spent 13.2ms (8.09+5.08) within IO::Socket::INET::peerport which was called 1000 times, avg 13µs/call: # 1000 times (8.09ms+5.08ms) by Mojo::IOLoop::remote_info at line 577 of Mojo/IOLoop.pm, avg 13µs/call
sub peerport {
29640006.15ms @_ == 1 or croak 'usage: $sock->peerport()';
297 my($sock) = @_;
29810001.52ms my $name = $sock->peername;
# spent 1.52ms making 1000 calls to IO::Socket::peername, avg 2µs/call
29910003.56ms $name ? (sockaddr_in($name))[0] : undef;
# spent 3.56ms making 1000 calls to Socket::sockaddr_in, avg 4µs/call
300}
301
302
# spent 32.7ms (8.26+24.4) within IO::Socket::INET::peerhost which was called 1000 times, avg 33µs/call: # 1000 times (8.26ms+24.4ms) by Mojo::IOLoop::remote_info at line 577 of Mojo/IOLoop.pm, avg 33µs/call
sub peerhost {
30340008.69ms @_ == 1 or croak 'usage: $sock->peerhost()';
304 my($sock) = @_;
305100023.0ms my $addr = $sock->peeraddr;
# spent 23.0ms making 1000 calls to IO::Socket::INET::peeraddr, avg 23µs/call
30610001.47ms $addr ? inet_ntoa($addr) : undef;
# spent 1.47ms making 1000 calls to Socket::inet_ntoa, avg 1µs/call
307}
308
309116µs1;
310
311__END__
 
# spent 5µs within IO::Socket::INET::CORE:match which was called 3 times, avg 2µs/call: # once (2µs+0s) by IO::Socket::INET::_sock_info at line 91 # once (2µs+0s) by IO::Socket::INET::_sock_info at line 80 # once (500ns+0s) by IO::Socket::INET::_sock_info at line 93
sub IO::Socket::INET::CORE:match; # opcode
# spent 1µs within IO::Socket::INET::CORE:subst which was called 2 times, avg 650ns/call: # once (700ns+0s) by IO::Socket::INET::_sock_info at line 77 # once (600ns+0s) by IO::Socket::INET::_sock_info at line 90
sub IO::Socket::INET::CORE:subst; # opcode