Filename | /usr/lib/perl/5.18/IO/Socket/INET.pm |
Statements | Executed 163 statements in 2.34ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
2 | 1 | 1 | 98µs | 690µs | configure | IO::Socket::INET::
4 | 2 | 1 | 66µs | 91µs | _sock_info | IO::Socket::INET::
2 | 2 | 2 | 22µs | 908µs | new | IO::Socket::INET::
1 | 1 | 1 | 18µs | 893µs | BEGIN@12 | IO::Socket::INET::
2 | 1 | 1 | 18µs | 33µs | _get_addr | IO::Socket::INET::
2 | 1 | 1 | 16µs | 394µs | connect | IO::Socket::INET::
1 | 1 | 1 | 16µs | 30µs | BEGIN@9 | IO::Socket::INET::
8 | 3 | 1 | 12µs | 12µs | CORE:match (opcode) | IO::Socket::INET::
2 | 1 | 1 | 10µs | 10µs | _get_proto_number | IO::Socket::INET::
1 | 1 | 1 | 10µs | 24µs | BEGIN@14 | IO::Socket::INET::
1 | 1 | 1 | 9µs | 2.52ms | BEGIN@11 | IO::Socket::INET::
1 | 1 | 1 | 9µs | 45µs | BEGIN@13 | IO::Socket::INET::
1 | 1 | 1 | 8µs | 26µs | BEGIN@15 | IO::Socket::INET::
4 | 2 | 1 | 4µs | 4µs | CORE:subst (opcode) | IO::Socket::INET::
1 | 1 | 1 | 3µs | 3µs | _get_proto_name | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _cache_proto | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | _error | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | bind | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | peeraddr | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | peerhost | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | peerport | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | sockaddr | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | sockhost | IO::Socket::INET::
0 | 0 | 0 | 0s | 0s | sockport | IO::Socket::INET::
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 | |||||
7 | package IO::Socket::INET; | ||||
8 | |||||
9 | 2 | 45µs | 2 | 45µs | # spent 30µs (16+15) within IO::Socket::INET::BEGIN@9 which was called:
# once (16µs+15µs) by main::BEGIN@7 at line 9 # spent 30µs making 1 call to IO::Socket::INET::BEGIN@9
# spent 15µs making 1 call to strict::import |
10 | 1 | 400ns | our(@ISA, $VERSION); | ||
11 | 2 | 39µs | 2 | 5.03ms | # spent 2.52ms (9µs+2.51) within IO::Socket::INET::BEGIN@11 which was called:
# once (9µs+2.51ms) by main::BEGIN@7 at line 11 # spent 2.52ms making 1 call to IO::Socket::INET::BEGIN@11
# spent 2.51ms making 1 call to IO::Socket::import |
12 | 2 | 32µs | 2 | 1.77ms | # spent 893µs (18+874) within IO::Socket::INET::BEGIN@12 which was called:
# once (18µs+874µs) by main::BEGIN@7 at line 12 # spent 893µs making 1 call to IO::Socket::INET::BEGIN@12
# spent 874µs making 1 call to Exporter::import |
13 | 2 | 29µs | 2 | 81µs | # spent 45µs (9+36) within IO::Socket::INET::BEGIN@13 which was called:
# once (9µs+36µs) by main::BEGIN@7 at line 13 # spent 45µs making 1 call to IO::Socket::INET::BEGIN@13
# spent 36µs making 1 call to Exporter::import |
14 | 2 | 27µs | 2 | 38µs | # spent 24µs (10+14) within IO::Socket::INET::BEGIN@14 which was called:
# once (10µs+14µs) by main::BEGIN@7 at line 14 # spent 24µs making 1 call to IO::Socket::INET::BEGIN@14
# spent 14µs making 1 call to Exporter::import |
15 | 2 | 1.87ms | 2 | 43µs | # spent 26µs (8+17) within IO::Socket::INET::BEGIN@15 which was called:
# once (8µs+17µs) by main::BEGIN@7 at line 15 # spent 26µs making 1 call to IO::Socket::INET::BEGIN@15
# spent 17µs making 1 call to Exporter::import |
16 | |||||
17 | 1 | 9µs | @ISA = qw(IO::Socket); | ||
18 | 1 | 400ns | $VERSION = "1.33"; | ||
19 | |||||
20 | 1 | 600ns | my $EINVAL = exists(&Errno::EINVAL) ? Errno::EINVAL() : 1; | ||
21 | |||||
22 | 1 | 5µs | 1 | 3µs | IO::Socket::INET->register_domain( AF_INET ); # spent 3µs making 1 call to IO::Socket::register_domain |
23 | |||||
24 | 1 | 2µs | my %socket_type = ( tcp => SOCK_STREAM, | ||
25 | udp => SOCK_DGRAM, | ||||
26 | icmp => SOCK_RAW | ||||
27 | ); | ||||
28 | 1 | 200ns | my %proto_number; | ||
29 | 1 | 1µs | $proto_number{tcp} = Socket::IPPROTO_TCP() if defined &Socket::IPPROTO_TCP; | ||
30 | 1 | 300ns | $proto_number{udp} = Socket::IPPROTO_UDP() if defined &Socket::IPPROTO_UDP; | ||
31 | 1 | 200ns | $proto_number{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP; | ||
32 | 1 | 3µs | my %proto_name = reverse %proto_number; | ||
33 | |||||
34 | # spent 908µs (22+885) within IO::Socket::INET::new which was called 2 times, avg 454µs/call:
# once (10µs+600µs) by HTTP::Tiny::Handle::connect at line 1041 of /opt/flows/lib/lib/perl5/HTTP/Tiny.pm
# once (12µs+285µs) by main::RUNTIME at line 39 of flows_to_es.pl | ||||
35 | 2 | 2µs | my $class = shift; | ||
36 | 2 | 1µs | unshift(@_, "PeerAddr") if @_ == 1; | ||
37 | 2 | 18µs | 2 | 885µs | return $class->SUPER::new(@_); # spent 885µs making 2 calls to IO::Socket::new, avg 443µs/call |
38 | } | ||||
39 | |||||
40 | sub _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 10µs within IO::Socket::INET::_get_proto_number which was called 2 times, avg 5µs/call:
# 2 times (10µs+0s) by IO::Socket::INET::_sock_info at line 81, avg 5µs/call | ||||
49 | 2 | 3µs | my $name = lc(shift); | ||
50 | 2 | 600ns | return undef unless defined $name; | ||
51 | 2 | 8µs | 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 3µs within IO::Socket::INET::_get_proto_name which was called:
# once (3µs+0s) by IO::Socket::INET::configure at line 167 | ||||
61 | 1 | 500ns | my $num = shift; | ||
62 | 1 | 300ns | return undef unless defined $num; | ||
63 | 1 | 4µs | 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 | sub _sock_info { | ||||
73 | 4 | 4µs | my($addr,$port,$proto) = @_; | ||
74 | 4 | 1µs | my $origport = $port; | ||
75 | 4 | 2µs | my @serv = (); | ||
76 | |||||
77 | 4 | 10µs | 2 | 2µs | $port = $1 # spent 2µs making 2 calls to IO::Socket::INET::CORE:subst, avg 1µs/call |
78 | if(defined $addr && $addr =~ s,:([\w\(\)/]+)$,,); | ||||
79 | |||||
80 | 4 | 19µs | 4 | 6µs | if(defined $proto && $proto =~ /\D/) { # spent 6µs making 4 calls to IO::Socket::INET::CORE:match, avg 1µs/call |
81 | 2 | 5µs | 2 | 10µs | my $num = _get_proto_number($proto); # spent 10µs making 2 calls to IO::Socket::INET::_get_proto_number, avg 5µs/call |
82 | 2 | 600ns | unless (defined $num) { | ||
83 | $@ = "Bad protocol '$proto'"; | ||||
84 | return; | ||||
85 | } | ||||
86 | 2 | 1µs | $proto = $num; | ||
87 | } | ||||
88 | |||||
89 | 4 | 1µs | if(defined $port) { | ||
90 | 2 | 6µs | 2 | 2µs | my $defport = ($port =~ s,\((\d+)\)$,,) ? $1 : undef; # spent 2µs making 2 calls to IO::Socket::INET::CORE:subst, avg 800ns/call |
91 | 2 | 12µs | 2 | 5µs | my $pnum = ($port =~ m,^(\d+)$,)[0]; # spent 5µs making 2 calls to IO::Socket::INET::CORE:match, avg 2µs/call |
92 | |||||
93 | 2 | 5µs | 2 | 900ns | @serv = getservbyname($port, _get_proto_name($proto) || "") # spent 900ns making 2 calls to IO::Socket::INET::CORE:match, avg 450ns/call |
94 | if ($port =~ m,\D,); | ||||
95 | |||||
96 | 2 | 2µs | $port = $serv[2] || $defport || $pnum; | ||
97 | 2 | 400ns | unless (defined $port) { | ||
98 | $@ = "Bad service '$origport'"; | ||||
99 | return; | ||||
100 | } | ||||
101 | |||||
102 | 2 | 2µs | $proto = _get_proto_number($serv[3]) if @serv && !$proto; | ||
103 | } | ||||
104 | |||||
105 | 4 | 13µs | return ($addr || undef, | ||
106 | $port || undef, | ||||
107 | $proto || undef | ||||
108 | ); | ||||
109 | } | ||||
110 | |||||
111 | sub _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 | |||||
125 | # spent 33µs (18+16) within IO::Socket::INET::_get_addr which was called 2 times, avg 17µs/call:
# 2 times (18µs+16µs) by IO::Socket::INET::configure at line 172, avg 17µs/call | ||||
126 | 2 | 2µs | my($sock,$addr_str, $multi) = @_; | ||
127 | 2 | 400ns | my @addr; | ||
128 | 2 | 1µs | if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) { | ||
129 | (undef, undef, undef, undef, @addr) = gethostbyname($addr_str); | ||||
130 | } else { | ||||
131 | 2 | 24µs | 2 | 16µs | my $h = inet_aton($addr_str); # spent 16µs making 2 calls to Socket::inet_aton, avg 8µs/call |
132 | 2 | 2µs | push(@addr, $h) if defined $h; | ||
133 | } | ||||
134 | 2 | 6µs | @addr; | ||
135 | } | ||||
136 | |||||
137 | # spent 690µs (98+592) within IO::Socket::INET::configure which was called 2 times, avg 345µs/call:
# 2 times (98µs+592µs) by IO::Socket::new at line 48 of IO/Socket.pm, avg 345µs/call | ||||
138 | 2 | 1µs | my($sock,$arg) = @_; | ||
139 | 2 | 600ns | my($lport,$rport,$laddr,$raddr,$proto,$type); | ||
140 | |||||
141 | 2 | 2µs | $arg->{LocalAddr} = $arg->{LocalHost} | ||
142 | if exists $arg->{LocalHost} && !exists $arg->{LocalAddr}; | ||||
143 | |||||
144 | 2 | 11µs | 2 | 45µs | ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr}, # spent 45µs making 2 calls to IO::Socket::INET::_sock_info, avg 23µs/call |
145 | $arg->{LocalPort}, | ||||
146 | $arg->{Proto}) | ||||
147 | or return _error($sock, $!, $@); | ||||
148 | |||||
149 | 2 | 2µs | $laddr = defined $laddr ? inet_aton($laddr) | ||
150 | : INADDR_ANY; | ||||
151 | |||||
152 | 2 | 500ns | return _error($sock, $EINVAL, "Bad hostname '",$arg->{LocalAddr},"'") | ||
153 | unless(defined $laddr); | ||||
154 | |||||
155 | 2 | 2µs | $arg->{PeerAddr} = $arg->{PeerHost} | ||
156 | if exists $arg->{PeerHost} && !exists $arg->{PeerAddr}; | ||||
157 | |||||
158 | 2 | 2µs | unless(exists $arg->{Listen}) { | ||
159 | 2 | 6µs | 2 | 46µs | ($raddr,$rport,$proto) = _sock_info($arg->{PeerAddr}, # spent 46µs making 2 calls to IO::Socket::INET::_sock_info, avg 23µs/call |
160 | $arg->{PeerPort}, | ||||
161 | $proto) | ||||
162 | or return _error($sock, $!, $@); | ||||
163 | } | ||||
164 | |||||
165 | 2 | 500ns | $proto ||= _get_proto_number('tcp'); | ||
166 | |||||
167 | 2 | 4µs | 1 | 3µs | $type = $arg->{Type} || $socket_type{lc _get_proto_name($proto)}; # spent 3µs making 1 call to IO::Socket::INET::_get_proto_name |
168 | |||||
169 | 2 | 900ns | my @raddr = (); | ||
170 | |||||
171 | 2 | 1µs | if(defined $raddr) { | ||
172 | 2 | 7µs | 2 | 33µs | @raddr = $sock->_get_addr($raddr, $arg->{MultiHomed}); # spent 33µs making 2 calls to IO::Socket::INET::_get_addr, avg 17µs/call |
173 | 2 | 900ns | return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") | ||
174 | unless @raddr; | ||||
175 | } | ||||
176 | |||||
177 | 2 | 600ns | while(1) { | ||
178 | |||||
179 | 2 | 11µs | 2 | 64µs | $sock->socket(AF_INET, $type, $proto) or # spent 64µs making 2 calls to IO::Socket::socket, avg 32µs/call |
180 | return _error($sock, $!, "$!"); | ||||
181 | |||||
182 | 2 | 2µs | if (defined $arg->{Blocking}) { | ||
183 | defined $sock->blocking($arg->{Blocking}) | ||||
184 | or return _error($sock, $!, "$!"); | ||||
185 | } | ||||
186 | |||||
187 | 2 | 2µs | if ($arg->{Reuse} || $arg->{ReuseAddr}) { | ||
188 | $sock->sockopt(SO_REUSEADDR,1) or | ||||
189 | return _error($sock, $!, "$!"); | ||||
190 | } | ||||
191 | |||||
192 | 2 | 900ns | if ($arg->{ReusePort}) { | ||
193 | $sock->sockopt(SO_REUSEPORT,1) or | ||||
194 | return _error($sock, $!, "$!"); | ||||
195 | } | ||||
196 | |||||
197 | 2 | 1µs | if ($arg->{Broadcast}) { | ||
198 | $sock->sockopt(SO_BROADCAST,1) or | ||||
199 | return _error($sock, $!, "$!"); | ||||
200 | } | ||||
201 | |||||
202 | 2 | 3µs | if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) { | ||
203 | $sock->bind($lport || 0, $laddr) or | ||||
204 | return _error($sock, $!, "$!"); | ||||
205 | } | ||||
206 | |||||
207 | 2 | 900ns | if(exists $arg->{Listen}) { | ||
208 | $sock->listen($arg->{Listen} || 5) or | ||||
209 | return _error($sock, $!, "$!"); | ||||
210 | last; | ||||
211 | } | ||||
212 | |||||
213 | # don't try to connect unless we're given a PeerAddr | ||||
214 | 2 | 1µs | last unless exists($arg->{PeerAddr}); | ||
215 | |||||
216 | 2 | 1µs | $raddr = shift @raddr; | ||
217 | |||||
218 | 2 | 400ns | return _error($sock, $EINVAL, 'Cannot determine remote port') | ||
219 | unless($rport || $type == SOCK_DGRAM || $type == SOCK_RAW); | ||||
220 | |||||
221 | last | ||||
222 | 2 | 700ns | unless($type == SOCK_STREAM || defined $raddr); | ||
223 | |||||
224 | 2 | 400ns | return _error($sock, $EINVAL, "Bad hostname '",$arg->{PeerAddr},"'") | ||
225 | unless defined $raddr; | ||||
226 | |||||
227 | # my $timeout = ${*$sock}{'io_socket_timeout'}; | ||||
228 | # my $before = time() if $timeout; | ||||
229 | |||||
230 | 2 | 2µs | undef $@; | ||
231 | 2 | 24µs | 4 | 400µs | if ($sock->connect(pack_sockaddr_in($rport, $raddr))) { # spent 394µs making 2 calls to IO::Socket::INET::connect, avg 197µs/call
# spent 6µs making 2 calls to Socket::pack_sockaddr_in, avg 3µs/call |
232 | # ${*$sock}{'io_socket_timeout'} = $timeout; | ||||
233 | return $sock; | ||||
234 | } | ||||
235 | |||||
236 | return _error($sock, $!, $@ || "Timeout") | ||||
237 | unless @raddr; | ||||
238 | |||||
239 | # if ($timeout) { | ||||
240 | # my $new_timeout = $timeout - (time() - $before); | ||||
241 | # return _error($sock, | ||||
242 | # (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL), | ||||
243 | # "Timeout") if $new_timeout <= 0; | ||||
244 | # ${*$sock}{'io_socket_timeout'} = $new_timeout; | ||||
245 | # } | ||||
246 | |||||
247 | } | ||||
248 | |||||
249 | $sock; | ||||
250 | } | ||||
251 | |||||
252 | # spent 394µs (16+378) within IO::Socket::INET::connect which was called 2 times, avg 197µs/call:
# 2 times (16µs+378µs) by IO::Socket::INET::configure at line 231, avg 197µs/call | ||||
253 | 2 | 1µs | @_ == 2 || @_ == 3 or | ||
254 | croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADDR)'; | ||||
255 | 2 | 900ns | my $sock = shift; | ||
256 | 2 | 14µs | 2 | 378µs | return $sock->SUPER::connect(@_ == 1 ? shift : pack_sockaddr_in(@_)); # spent 378µs making 2 calls to IO::Socket::connect, avg 189µs/call |
257 | } | ||||
258 | |||||
259 | sub bind { | ||||
260 | @_ == 2 || @_ == 3 or | ||||
261 | croak 'usage: $sock->bind(NAME) or $sock->bind(PORT, ADDR)'; | ||||
262 | my $sock = shift; | ||||
263 | return $sock->SUPER::bind(@_ == 1 ? shift : pack_sockaddr_in(@_)) | ||||
264 | } | ||||
265 | |||||
266 | sub sockaddr { | ||||
267 | @_ == 1 or croak 'usage: $sock->sockaddr()'; | ||||
268 | my($sock) = @_; | ||||
269 | my $name = $sock->sockname; | ||||
270 | $name ? (sockaddr_in($name))[1] : undef; | ||||
271 | } | ||||
272 | |||||
273 | sub sockport { | ||||
274 | @_ == 1 or croak 'usage: $sock->sockport()'; | ||||
275 | my($sock) = @_; | ||||
276 | my $name = $sock->sockname; | ||||
277 | $name ? (sockaddr_in($name))[0] : undef; | ||||
278 | } | ||||
279 | |||||
280 | sub sockhost { | ||||
281 | @_ == 1 or croak 'usage: $sock->sockhost()'; | ||||
282 | my($sock) = @_; | ||||
283 | my $addr = $sock->sockaddr; | ||||
284 | $addr ? inet_ntoa($addr) : undef; | ||||
285 | } | ||||
286 | |||||
287 | sub peeraddr { | ||||
288 | @_ == 1 or croak 'usage: $sock->peeraddr()'; | ||||
289 | my($sock) = @_; | ||||
290 | my $name = $sock->peername; | ||||
291 | $name ? (sockaddr_in($name))[1] : undef; | ||||
292 | } | ||||
293 | |||||
294 | sub peerport { | ||||
295 | @_ == 1 or croak 'usage: $sock->peerport()'; | ||||
296 | my($sock) = @_; | ||||
297 | my $name = $sock->peername; | ||||
298 | $name ? (sockaddr_in($name))[0] : undef; | ||||
299 | } | ||||
300 | |||||
301 | sub peerhost { | ||||
302 | @_ == 1 or croak 'usage: $sock->peerhost()'; | ||||
303 | my($sock) = @_; | ||||
304 | my $addr = $sock->peeraddr; | ||||
305 | $addr ? inet_ntoa($addr) : undef; | ||||
306 | } | ||||
307 | |||||
308 | 1 | 10µs | 1; | ||
309 | |||||
310 | __END__ | ||||
# spent 12µs within IO::Socket::INET::CORE:match which was called 8 times, avg 1µs/call:
# 4 times (6µs+0s) by IO::Socket::INET::_sock_info at line 80, avg 1µs/call
# 2 times (5µs+0s) by IO::Socket::INET::_sock_info at line 91, avg 2µs/call
# 2 times (900ns+0s) by IO::Socket::INET::_sock_info at line 93, avg 450ns/call | |||||
sub IO::Socket::INET::CORE:subst; # opcode |