gopsutil icon indicating copy to clipboard operation
gopsutil copied to clipboard

WIP: [net][linux]: add netlink implementation for Linux connection stat

Open shirou opened this issue 5 years ago • 4 comments

This PR introduces netlink support for Linux Connections. To use netlink, users should add build tag -tags=netlink

Thanks for sample code of @Brian-Williams in #695.

I choose build tag to change implementation. Because current implementation is enough for most of users, I think. Some of users want to use netlink to work with their big system. They must be their environment and they can compile binary by their own. so adding build tag is not so big problem, I think.

This is what only I think. There must be other objection. Comments are welcome as always.

Limitation for using netlink

  • fd is always 0
    • to get fd becomes slow
  • support Linux 3.3 or later only (inet_diag_req_v2 is required)
    • until that, inet_diag has not Protocol field

TODO

  • benchmark

shirou avatar Dec 26 '19 14:12 shirou

Note: this version of netlink does NOT return FD and ProcessID from inode

func BenchmarkGetConnectionsInet(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Connections("inet")
	}
}
func BenchmarkGetConnectionsAll(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Connections("all")
	}
}
goos: linux
goarch: amd64
pkg: github.com/shirou/gopsutil/net
netlink
BenchmarkGetConnectionsInet-2               1162            882153 ns/op           84048 B/op       1792 allocs/op
BenchmarkGetConnectionsAll-2                 572           1958050 ns/op          458220 B/op      12660 allocs/op
original
BenchmarkGetConnectionsInet-2                 63          21234560 ns/op         3640324 B/op      40404 allocs/op
BenchmarkGetConnectionsAll-2                  57          23370421 ns/op         4523979 B/op      47422 allocs/op

shirou avatar Dec 31 '19 06:12 shirou

So getting Pid and FD from inodes IS a bottleneck.

goos: linux
goarch: amd64
pkg: github.com/shirou/gopsutil/net
netlink:
BenchmarkGetConnectionsInet-2                 66          20531232 ns/op         3297398 B/op      39004 allocs/op
BenchmarkGetConnectionsAll-2                  57          19901569 ns/op         3714031 B/op      50611 allocs/op
original:
BenchmarkGetConnectionsInet-2                 52          22912904 ns/op         3950026 B/op      44519 allocs/op
BenchmarkGetConnectionsAll-2                  43          24103518 ns/op         4856759 B/op      51875 allocs/op

shirou avatar Dec 31 '19 14:12 shirou

So getting Pid and FD from inodes IS a bottleneck.

Yes, #784 was opened regarding this.

Awesome work you've done here btw, using a build tag is an original solution but is it optimal?

Lomanic avatar Jan 06 '20 18:01 Lomanic

Hi! I followed this PR from #695. Thanks for working on adding this awesome feature! I just wanted to check and see if there is still plan to work on and merge this PR.

Hugozys avatar Jul 17 '23 20:07 Hugozys