← Index
NYTProf Performance Profile   « block view • line view • sub view »
For ddd2.pl
  Run on Tue May 25 16:52:24 2010
Reported on Tue May 25 16:57:00 2010

File /usr/local/lib/perl5/5.8.8/IO/Socket/INET.pm
Statements Executed 37827
Statement Execution Time 819ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
46111170ms48.9sIO::Socket::INET::::configureIO::Socket::INET::configure
92221101ms178msIO::Socket::INET::::_sock_infoIO::Socket::INET::_sock_info
4611256.8ms56.8msIO::Socket::INET::::CORE:gpbynameIO::Socket::INET::CORE:gpbyname (opcode)
9222249.0ms124msIO::Socket::INET::::peerportIO::Socket::INET::peerport
4611140.9ms277msIO::Socket::INET::::_get_addrIO::Socket::INET::_get_addr
4611238.8ms38.8msIO::Socket::INET::::CORE:gpbynumberIO::Socket::INET::CORE:gpbynumber (opcode)
4611124.6ms67.4msIO::Socket::INET::::peeraddrIO::Socket::INET::peeraddr
4611123.7ms48.2sIO::Socket::INET::::connectIO::Socket::INET::connect
4611122.5ms49.5sIO::Socket::INET::::newIO::Socket::INET::new
4611121.8ms93.5msIO::Socket::INET::::peerhostIO::Socket::INET::peerhost
18443214.6ms14.6msIO::Socket::INET::::CORE:matchIO::Socket::INET::CORE:match (opcode)
922225.59ms5.59msIO::Socket::INET::::CORE:substIO::Socket::INET::CORE:subst (opcode)
0000s0sIO::Socket::INET::::BEGINIO::Socket::INET::BEGIN
0000s0sIO::Socket::INET::::_errorIO::Socket::INET::_error
0000s0sIO::Socket::INET::::bindIO::Socket::INET::bind
0000s0sIO::Socket::INET::::sockaddrIO::Socket::INET::sockaddr
0000s0sIO::Socket::INET::::sockhostIO::Socket::INET::sockhost
0000s0sIO::Socket::INET::::sockportIO::Socket::INET::sockport
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
93128µs125µsuse strict;
# spent 25µs making 1 call to strict::import
1015µsour(@ISA, $VERSION);
113336µs14.72msuse IO::Socket;
# spent 4.72ms making 1 call to IO::Socket::import
123109µs14.03msuse Socket;
# spent 4.03ms making 1 call to Exporter::import
13391µs1234µsuse Carp;
# spent 234µs making 1 call to Exporter::import
14398µs1136µsuse Exporter;
# spent 136µs making 1 call to Exporter::import
1532.94ms1118µsuse Errno;
# spent 118µs making 1 call to Exporter::import
16
1719µs@ISA = qw(IO::Socket);
1816µs$VERSION = "1.29";
19
2017µsmy $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1;
21
22152µs2138µsIO::Socket::INET->register_domain( AF_INET );
# spent 116µs making 1 call to Socket::AUTOLOAD # spent 22µs making 1 call to IO::Socket::register_domain
23
24171µs3255µsmy %socket_type = ( tcp => SOCK_STREAM,
# spent 255µs making 3 calls to Socket::AUTOLOAD, avg 85µs/call
25 udp => SOCK_DGRAM,
26 icmp => SOCK_RAW
27 );
28
29
# spent 49.5s (22.5ms+49.5) within IO::Socket::INET::new which was called 461 times, avg 107ms/call: # 461 times (22.5ms+49.5s) by LWP::Protocol::http::_new_socket at line 35 of LWP/Protocol/http.pm, avg 107ms/call
sub new {
304612.40ms my $class = shift;
314612.46ms unshift(@_, "PeerAddr") if @_ == 1;
3246117.6ms46149.5s return $class->SUPER::new(@_);
# spent 49.5s making 461 calls to IO::Socket::new, avg 107ms/call
33}
34
35
# spent 178ms (101+77.0) within IO::Socket::INET::_sock_info which was called 922 times, avg 193µs/call: # 461 times (44.0ms+61.3ms) by IO::Socket::INET::configure at line 111, avg 228µs/call # 461 times (56.7ms+15.7ms) by IO::Socket::INET::configure at line 126, avg 157µs/call
sub _sock_info {
369226.87ms my($addr,$port,$proto) = @_;
379224.35ms my $origport = $port;
389224.33ms my @proto = ();
399224.10ms my @serv = ();
40
4192212.2ms4613.41ms $port = $1
# spent 3.41ms making 461 calls to IO::Socket::INET::CORE:subst, avg 7µs/call
42 if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,);
43
4492223.9ms9227.78ms if(defined $proto && $proto =~ /\D/) {
# spent 7.78ms making 922 calls to IO::Socket::INET::CORE:match, avg 8µs/call
4546168.8ms46156.8ms if(@proto = getprotobyname($proto)) {
# spent 56.8ms making 461 calls to IO::Socket::INET::CORE:gpbyname, avg 123µs/call
46 $proto = $proto[2] || undef;
47 }
48 else {
49 $@ = "Bad protocol '$proto'";
50 return;
51 }
52 }
53
549226.36ms if(defined $port) {
554617.78ms4612.18ms my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef;
# spent 2.18ms making 461 calls to IO::Socket::INET::CORE:subst, avg 5µs/call
5646110.9ms4614.66ms my $pnum = ($port =~ m,^(\d+)$,)[0];
# spent 4.66ms making 461 calls to IO::Socket::INET::CORE:match, avg 10µs/call
57
584617.39ms4612.12ms @serv = getservbyname($port, $proto[0] || "")
# spent 2.12ms making 461 calls to IO::Socket::INET::CORE:match, avg 5µs/call
59 if ($port =~ m,\D,);
60
614612.45ms $port = $serv[2] || $defport || $pnum;
624612.01ms unless (defined $port) {
63 $@ = "Bad service '$origport'";
64 return;
65 }
66
674612.71ms $proto = (getprotobyname($serv[3]))[2] || undef
68 if @serv && !$proto;
69 }
70
7192215.4ms return ($addr || undef,
72 $port || undef,
73 $proto || undef
74 );
75}
76
77sub _error {
78 my $sock = shift;
79 my $err = shift;
80 {
81 local($!);
82 my $title = ref($sock).": ";
83 $@ = join("", $_[0] =~ /^$title/ ? "" : $title, @_);
84 close($sock)
85 if(defined fileno($sock));
86 }
87 $! = $err;
88 return undef;
89}
90
91
# spent 277ms (40.9+237) within IO::Socket::INET::_get_addr which was called 461 times, avg 602µs/call: # 461 times (40.9ms+237ms) by IO::Socket::INET::configure at line 140, avg 602µs/call
sub _get_addr {
924613.52ms my($sock,$addr_str, $multi) = @_;
934617.41ms my @addr;
944614.30ms if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
95 (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
96 } else {
97461250ms461237ms my $h = inet_aton($addr_str);
# spent 237ms making 461 calls to Socket::inet_aton, avg 513µs/call
984614.38ms push(@addr, $h) if defined $h;
99 }
1004618.18ms @addr;
101}
102
103
# spent 48.9s (170ms+48.8) within IO::Socket::INET::configure which was called 461 times, avg 106ms/call: # 461 times (170ms+48.8s) by Net::HTTP::http_connect at line 21 of Net/HTTP.pm, avg 106ms/call
sub configure {
1044612.29ms my($sock,$arg) = @_;
1054612.13ms my($lport,$rport,$laddr,$raddr,$proto,$type);
106
107
1084612.49ms $arg->{LocalAddr} = $arg->{LocalHost}
109 if exists $arg->{LocalHost} && !exists $arg->{LocalAddr};
110
11146112.0ms461105ms ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr},
# spent 105ms making 461 calls to IO::Socket::INET::_sock_info, avg 228µs/call
112 $arg->{LocalPort},
113 $arg->{Proto})
114 or return _error($sock, $!, $@);
115
11646110.2ms4618.03ms $laddr = defined $laddr ? inet_aton($laddr)
# spent 7.93ms making 460 calls to Socket::__ANON__[Socket.pm:395], avg 17µs/call # spent 109µs making 1 call to Socket::AUTOLOAD
117 : INADDR_ANY;
118
1194612.16ms return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'")
120 unless(defined $laddr);
121
1224612.80ms $arg->{PeerAddr} = $arg->{PeerHost}
123 if exists $arg->{PeerHost} && !exists $arg->{PeerAddr};
124
1254614.65ms unless(exists $arg->{Listen}) {
1264619.88ms46172.3ms ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr},
# spent 72.3ms making 461 calls to IO::Socket::INET::_sock_info, avg 157µs/call
127 $arg->{PeerPort},
128 $proto)
129 or return _error($sock, $!, $@);
130 }
131
1324612.09ms $proto ||= (getprotobyname('tcp'))[2];
133
13446148.2ms46138.8ms my $pname = (getprotobynumber($proto))[0];
# spent 38.8ms making 461 calls to IO::Socket::INET::CORE:gpbynumber, avg 84µs/call
1354614.41ms $type = $arg->{Type} || $socket_type{lc $pname};
136
1374612.64ms my @raddr = ();
138
1394614.47ms if(defined $raddr) {
14046113.2ms461277ms @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed});
# spent 277ms making 461 calls to IO::Socket::INET::_get_addr, avg 602µs/call
1414612.36ms return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
142 unless @raddr;
143 }
144
1454612.39ms while(1) {
146
14746118.2ms92257.2ms $sock->socket(AF_INET, $type, $proto) or
# spent 49.2ms making 461 calls to IO::Socket::socket, avg 107µs/call # spent 8.06ms making 461 calls to Socket::__ANON__[Socket.pm:395], avg 17µs/call
148 return _error($sock, $!, "$!");
149
1504613.07ms if (defined $arg->{Blocking}) {
151 defined $sock->blocking($arg->{Blocking})
152 or return _error($sock, $!, "$!");
153 }
154
1554612.66ms if ($arg->{Reuse} || $arg->{ReuseAddr}) {
156 $sock->sockopt(SO_REUSEADDR,1) or
157 return _error($sock, $!, "$!");
158 }
159
1604612.18ms if ($arg->{ReusePort}) {
161 $sock->sockopt(SO_REUSEPORT,1) or
162 return _error($sock, $!, "$!");
163 }
164
1654612.11ms if ($arg->{Broadcast}) {
166 $sock->sockopt(SO_BROADCAST,1) or
167 return _error($sock, $!, "$!");
168 }
169
17046110.3ms4617.05ms if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) {
# spent 7.05ms making 461 calls to Socket::__ANON__[Socket.pm:395], avg 15µs/call
171 $sock->bind($lport || 0, $laddr) or
172 return _error($sock, $!, "$!");
173 }
174
1754612.29ms if(exists $arg->{Listen}) {
176 $sock->listen($arg->{Listen} || 5) or
177 return _error($sock, $!, "$!");
178 last;
179 }
180
181 # don't try to connect unless we're given a PeerAddr
1824612.63ms last unless exists($arg->{PeerAddr});
183
1844613.14ms $raddr = shift @raddr;
185
1864612.20ms return _error($sock, $EINVAL, 'Cannot determine remote port')
187 unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW);
188
189 last
1904618.16ms4616.05ms unless($type == SOCK_STREAM || defined $raddr);
# spent 6.05ms making 461 calls to Socket::__ANON__[Socket.pm:395], avg 13µs/call
191
1924612.13ms return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'")
193 unless defined $raddr;
194
195# my $timeout = ${*$sock}{'io_socket_timeout'};
196# my $before = time() if $timeout;
197
1984612.59ms undef $@;
19946122.7ms92248.2s if ($sock->connect(pack_sockaddr_in($rport, $raddr))) {
# spent 48.2s making 461 calls to IO::Socket::INET::connect, avg 105ms/call # spent 4.60ms making 461 calls to Socket::pack_sockaddr_in, avg 10µs/call
200# ${*$sock}{'io_socket_timeout'} = $timeout;
201 return $sock;
202 }
203
204 return _error($sock, $!, $@ || "Timeout")
205 unless @raddr;
206
207# if ($timeout) {
208# my $new_timeout = $timeout - (time() - $before);
209# return _error($sock,
210# (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
211# "Timeout") if $new_timeout <= 0;
212# ${*$sock}{'io_socket_timeout'} = $new_timeout;
213# }
214
215 }
216
217 $sock;
218}
219
220
# spent 48.2s (23.7ms+48.2) within IO::Socket::INET::connect which was called 461 times, avg 105ms/call: # 461 times (23.7ms+48.2s) by IO::Socket::INET::configure at line 199, avg 105ms/call
sub connect {
2214612.47ms @_ == 2 || @_ == 3 or
222 croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)';
2234612.39ms my $sock = shift;
22446119.0ms46148.2s return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_));
# spent 48.2s making 461 calls to IO::Socket::connect, avg 104ms/call
225}
226
227sub bind {
228 @_ == 2 || @_ == 3 or
229 croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)';
230 my $sock = shift;
231 return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_))
232}
233
234sub sockaddr {
235 @_ == 1 or croak 'usage: $sock->sockaddr()';
236 my($sock) = @_;
237 my $name = $sock->sockname;
238 $name ? (sockaddr_in($name))[1] : undef;
239}
240
241sub sockport {
242 @_ == 1 or croak 'usage: $sock->sockport()';
243 my($sock) = @_;
244 my $name = $sock->sockname;
245 $name ? (sockaddr_in($name))[0] : undef;
246}
247
248sub sockhost {
249 @_ == 1 or croak 'usage: $sock->sockhost()';
250 my($sock) = @_;
251 my $addr = $sock->sockaddr;
252 $addr ? inet_ntoa($addr) : undef;
253}
254
255
# spent 67.4ms (24.6+42.7) within IO::Socket::INET::peeraddr which was called 461 times, avg 146µs/call: # 461 times (24.6ms+42.7ms) by IO::Socket::INET::peerhost at line 272, avg 146µs/call
sub peeraddr {
2564612.03ms @_ == 1 or croak 'usage: $sock->peeraddr()';
2574612.04ms my($sock) = @_;
2584618.23ms46121.4ms my $name = $sock->peername;
# spent 21.4ms making 461 calls to IO::Socket::peername, avg 46µs/call
25946112.1ms46121.3ms $name ? (sockaddr_in($name))[1] : undef;
# spent 21.3ms making 461 calls to Socket::sockaddr_in, avg 46µs/call
260}
261
262
# spent 124ms (49.0+75.4) within IO::Socket::INET::peerport which was called 922 times, avg 135µs/call: # 461 times (25.6ms+39.7ms) by Net::HTTP::Methods::http_configure at line 52 of Net/HTTP/Methods.pm, avg 142µs/call # 461 times (23.4ms+35.7ms) by LWP::Protocol::http::_get_sock_info at line 75 of LWP/Protocol/http.pm, avg 128µs/call
sub peerport {
2639224.77ms @_ == 1 or croak 'usage: $sock->peerport()';
2649224.68ms my($sock) = @_;
26592215.9ms92236.2ms my $name = $sock->peername;
# spent 36.2ms making 922 calls to IO::Socket::peername, avg 39µs/call
26692222.9ms92239.2ms $name ? (sockaddr_in($name))[0] : undef;
# spent 39.2ms making 922 calls to Socket::sockaddr_in, avg 42µs/call
267}
268
269
# spent 93.5ms (21.8+71.6) within IO::Socket::INET::peerhost which was called 461 times, avg 203µs/call: # 461 times (21.8ms+71.6ms) by LWP::Protocol::http::_get_sock_info at line 75 of LWP/Protocol/http.pm, avg 203µs/call
sub peerhost {
2704612.49ms @_ == 1 or croak 'usage: $sock->peerhost()';
2714612.19ms my($sock) = @_;
2724617.92ms46167.4ms my $addr = $sock->peeraddr;
# spent 67.4ms making 461 calls to IO::Socket::INET::peeraddr, avg 146µs/call
27346113.8ms4614.28ms $addr ? inet_ntoa($addr) : undef;
# spent 4.28ms making 461 calls to Socket::inet_ntoa, avg 9µs/call
274}
275
276130µs1;
277
278__END__
279
280=head1 NAME
281
282IO::Socket::INET - Object interface for AF_INET domain sockets
283
284=head1 SYNOPSIS
285
286 use IO::Socket::INET;
287
288=head1 DESCRIPTION
289
290C<IO::Socket::INET> provides an object interface to creating and using sockets
291in the AF_INET domain. It is built upon the L<IO::Socket> interface and
292inherits all the methods defined by L<IO::Socket>.
293
294=head1 CONSTRUCTOR
295
296=over 4
297
298=item new ( [ARGS] )
299
300Creates an C<IO::Socket::INET> object, which is a reference to a
301newly created symbol (see the C<Symbol> package). C<new>
302optionally takes arguments, these arguments are in key-value pairs.
303
304In addition to the key-value pairs accepted by L<IO::Socket>,
305C<IO::Socket::INET> provides.
306
307
308 PeerAddr Remote host address <hostname>[:<port>]
309 PeerHost Synonym for PeerAddr
310 PeerPort Remote port or service <service>[(<no>)] | <no>
311 LocalAddr Local host bind address hostname[:port]
312 LocalHost Synonym for LocalAddr
313 LocalPort Local host bind port <service>[(<no>)] | <no>
314 Proto Protocol name (or number) "tcp" | "udp" | ...
315 Type Socket type SOCK_STREAM | SOCK_DGRAM | ...
316 Listen Queue size for listen
317 ReuseAddr Set SO_REUSEADDR before binding
318 Reuse Set SO_REUSEADDR before binding (deprecated, prefer ReuseAddr)
319 ReusePort Set SO_REUSEPORT before binding
320 Broadcast Set SO_BROADCAST before binding
321 Timeout Timeout value for various operations
322 MultiHomed Try all addresses for multi-homed hosts
323 Blocking Determine if connection will be blocking mode
324
325If C<Listen> is defined then a listen socket is created, else if the
326socket type, which is derived from the protocol, is SOCK_STREAM then
327connect() is called.
328
329Although it is not illegal, the use of C<MultiHomed> on a socket
330which is in non-blocking mode is of little use. This is because the
331first connect will never fail with a timeout as the connect call
332will not block.
333
334The C<PeerAddr> can be a hostname or the IP-address on the
335"xx.xx.xx.xx" form. The C<PeerPort> can be a number or a symbolic
336service name. The service name might be followed by a number in
337parenthesis which is used if the service is not known by the system.
338The C<PeerPort> specification can also be embedded in the C<PeerAddr>
339by preceding it with a ":".
340
341If C<Proto> is not given and you specify a symbolic C<PeerPort> port,
342then the constructor will try to derive C<Proto> from the service
343name. As a last resort C<Proto> "tcp" is assumed. The C<Type>
344parameter will be deduced from C<Proto> if not specified.
345
346If the constructor is only passed a single argument, it is assumed to
347be a C<PeerAddr> specification.
348
349If C<Blocking> is set to 0, the connection will be in nonblocking mode.
350If not specified it defaults to 1 (blocking mode).
351
352Examples:
353
354 $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
355 PeerPort => 'http(80)',
356 Proto => 'tcp');
357
358 $sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
359
360 $sock = IO::Socket::INET->new(Listen => 5,
361 LocalAddr => 'localhost',
362 LocalPort => 9000,
363 Proto => 'tcp');
364
365 $sock = IO::Socket::INET->new('127.0.0.1:25');
366
367 $sock = IO::Socket::INET->new(PeerPort => 9999,
368 PeerAddr => inet_ntoa(INADDR_BROADCAST),
369 Proto => udp,
370 LocalAddr => 'localhost',
371 Broadcast => 1 )
372 or die "Can't bind : $@\n";
373
374 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
375
376As of VERSION 1.18 all IO::Socket objects have autoflush turned on
377by default. This was not the case with earlier releases.
378
379 NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
380
381=back
382
383=head2 METHODS
384
385=over 4
386
387=item sockaddr ()
388
389Return the address part of the sockaddr structure for the socket
390
391=item sockport ()
392
393Return the port number that the socket is using on the local host
394
395=item sockhost ()
396
397Return the address part of the sockaddr structure for the socket in a
398text form xx.xx.xx.xx
399
400=item peeraddr ()
401
402Return the address part of the sockaddr structure for the socket on
403the peer host
404
405=item peerport ()
406
407Return the port number for the socket on the peer host.
408
409=item peerhost ()
410
411Return the address part of the sockaddr structure for the socket on the
412peer host in a text form xx.xx.xx.xx
413
414=back
415
416=head1 SEE ALSO
417
418L<Socket>, L<IO::Socket>
419
420=head1 AUTHOR
421
422Graham Barr. Currently maintained by the Perl Porters. Please report all
423bugs to <perl5-porters@perl.org>.
424
425=head1 COPYRIGHT
426
427Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
428This program is free software; you can redistribute it and/or
429modify it under the same terms as Perl itself.
430
431=cut
# spent 56.8ms within IO::Socket::INET::CORE:gpbyname which was called 461 times, avg 123µs/call: # 461 times (56.8ms+0s) by IO::Socket::INET::_sock_info at line 45 of IO/Socket/INET.pm, avg 123µs/call
sub IO::Socket::INET::CORE:gpbyname; # xsub
# spent 38.8ms within IO::Socket::INET::CORE:gpbynumber which was called 461 times, avg 84µs/call: # 461 times (38.8ms+0s) by IO::Socket::INET::configure at line 134 of IO/Socket/INET.pm, avg 84µs/call
sub IO::Socket::INET::CORE:gpbynumber; # xsub
# spent 14.6ms within IO::Socket::INET::CORE:match which was called 1844 times, avg 8µs/call: # 922 times (7.78ms+0s) by IO::Socket::INET::_sock_info at line 44 of IO/Socket/INET.pm, avg 8µs/call # 461 times (4.66ms+0s) by IO::Socket::INET::_sock_info at line 56 of IO/Socket/INET.pm, avg 10µs/call # 461 times (2.12ms+0s) by IO::Socket::INET::_sock_info at line 58 of IO/Socket/INET.pm, avg 5µs/call
sub IO::Socket::INET::CORE:match; # xsub
# spent 5.59ms within IO::Socket::INET::CORE:subst which was called 922 times, avg 6µs/call: # 461 times (3.41ms+0s) by IO::Socket::INET::_sock_info at line 41 of IO/Socket/INET.pm, avg 7µs/call # 461 times (2.18ms+0s) by IO::Socket::INET::_sock_info at line 55 of IO/Socket/INET.pm, avg 5µs/call
sub IO::Socket::INET::CORE:subst; # xsub