cazz has asked for the wisdom of the Perl Monks concerning the following question:
A bit of background, I ran some simple IO::Socket code in an NIS/NFS env and noticed that ypserv was leaking memory. While the ypserv env was getting fixed, an strace of the code showed that getpeername was being called over 30k times in a 2 minute window. In an NIS env, getpeername does a yp call. The leak in ypserv was exposed thanks to the 30k yp calls.
While waiting for the IT team to fix ypserv, I took a look at IO::Socket to figure out why it is calling getpeername so often.
On every send, it can call getpeername twice. Once if the peer wasn't passed in as an arguement, and once to see which mode to call the Socket's send. Of course, the getpeername response is cached in the socket if the send was successful.
My thinking is that both peername and send should get "optomized" to use the cached data in the socket object.
The peername sub uses its cache if getpeername fails, but shouldn't it use the cache first, and only look up the name if the cache doesn't exist?
The send sub should be modified to use the peername function, which should have the caching mechanism.sub peername { @_ == 1 or croak 'usage: $sock->peername()'; my($sock) = @_; getpeername($sock) || ${*$sock}{'io_socket_peername'} || undef; }
While changing the code to use the getpeername cache works for me, before I submit a patch, I wanted to get a few more eyes on the idea, hense this post.sub send { @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]] +)'; my $sock = $_[0]; my $flags = $_[2] || 0; my $peer = $_[3] || $sock->peername; croak 'send: Cannot determine peer address' unless($peer); my $r = defined(getpeername($sock)) ? send($sock, $_[1], $flags) : send($sock, $_[1], $flags, $peer); # remember who we send to, if it was sucessful ${*$sock}{'io_socket_peername'} = $peer if(@_ == 4 && defined $r); $r; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IO::Socket doing getpeername twice on every send?
by Zaxo (Archbishop) on Feb 23, 2005 at 05:44 UTC | |
by cazz (Pilgrim) on Feb 23, 2005 at 15:42 UTC | |
|
Re: IO::Socket doing getpeername twice on every send?
by cbrandtbuffalo (Deacon) on Feb 24, 2005 at 13:13 UTC |