portlist: refactor, introduce OS-specific types

Add an osImpl interface that can be stateful and thus more efficient
between calls. It will later be implemented by all OSes but for now
this change only adds a Linux implementation.

Remove Port.inode. It was only used by Linux and moves into its osImpl.

Don't reopen /proc/net/* files on each run. Turns out you can just
keep then open and seek to the beginning and reread and the contents
are fresh.

    name                    old time/op    new time/op    delta
    GetListIncremental-8    7.29ms ± 2%    6.53ms ± 1%  -10.50%  (p=0.000 n=9+9)

    name                   old alloc/op   new alloc/op   delta
    GetListIncremental-8    1.30kB ±13%    0.70kB ± 5%  -46.38%  (p=0.000 n=9+10)

    name                  old allocs/op  new allocs/op  delta
    GetListIncremental-8      33.2 ±11%      18.0 ± 0%  -45.82%  (p=0.000 n=9+10)

Updates #5958

Change-Id: I4be83463cbd23c2e2fa5d0bdf38560004f53401b
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-10-23 18:02:02 -07:00
committed by Brad Fitzpatrick
parent 4597ec1037
commit 036f70b7b4
6 changed files with 282 additions and 248 deletions

View File

@@ -71,19 +71,19 @@ udp4 0 0 *.5553 *.*
func TestParsePortsNetstat(t *testing.T) {
want := List{
Port{"tcp", 22, "", ""},
Port{"tcp", 23, "", ""},
Port{"tcp", 24, "", ""},
Port{"tcp", 32, "sshd", ""},
Port{"udp", 53, "chrome", ""},
Port{"udp", 53, "funball", ""},
Port{"udp", 5050, "CDPSvc", ""},
Port{"udp", 5353, "", ""},
Port{"udp", 5354, "", ""},
Port{"udp", 5453, "", ""},
Port{"udp", 5553, "", ""},
Port{"tcp", 8185, "", ""}, // but not 8186, 8187, 8188 on localhost
Port{"udp", 9353, "iTunes", ""},
Port{"tcp", 22, ""},
Port{"tcp", 23, ""},
Port{"tcp", 24, ""},
Port{"tcp", 32, "sshd"},
Port{"udp", 53, "chrome"},
Port{"udp", 53, "funball"},
Port{"udp", 5050, "CDPSvc"},
Port{"udp", 5353, ""},
Port{"udp", 5354, ""},
Port{"udp", 5453, ""},
Port{"udp", 5553, ""},
Port{"tcp", 8185, ""}, // but not 8186, 8187, 8188 on localhost
Port{"udp", 9353, "iTunes"},
}
pl := appendParsePortsNetstat(nil, netstatOutput)