cazz has asked for the wisdom of the Perl Monks concerning the following question:

IO::Socket seems to do getpeername far too often on a socket and can be "optomized".

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?

sub peername { @_ == 1 or croak 'usage: $sock->peername()'; my($sock) = @_; getpeername($sock) || ${*$sock}{'io_socket_peername'} || undef; }
The send sub should be modified to use the peername function, which should have the caching mechanism.
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; }
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.

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

    I don't know the internals of IO::Socket, but it seems like this might cause trouble with a persistent LISTEN socket. Have you tested that case?

    After Compline,
    Zaxo

      Internally, IO::Socket passes around a seperate $sock for each connection. Well, I tested my change by taking the nonblock example from the Perl Cookbook, making it use persistent LISTEN sockets, and then adding the following code in the request handling sub.
      print "GOT LEN " . length($client->peername) . "\n"; my($port, $ipaddr) = sockaddr_in($client->peername); print "GOT PORT $port\n";
      I connected from multiple hosts, getting the right port each time, then restarted the listener, connected again from multiple hosts and still got the expected results from my inserted block of code.
Re: IO::Socket doing getpeername twice on every send?
by cbrandtbuffalo (Deacon) on Feb 24, 2005 at 13:13 UTC
    Your post and your follow-up regarding your testing methodology suggest IO::Socket may be a bit lacking in tests of its own. In a perfect world, it would be loaded with tests that would tell you right away if your proposed change broke things.

    The IO modules are on the Phalanx 100, so some additional tests might be welcome. Now I'm sure this would be biting off much more than you bargained for, but what about adopting the module and becoming a hoplite? Even if you're not up for that, it would be helpful to convert the test cases you came up with into module tests and submit them with your patch.