From daaadd4f1e60173a7e6481900d7d9a8bcd67b768 Mon Sep 17 00:00:00 2001 From: Alexander Blagoev Date: Fri, 28 Apr 2017 19:00:28 +0300 Subject: [PATCH] Optimize memory usage for net.Connections on Linux --- net/net_linux.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/net/net_linux.go b/net/net_linux.go index 58c0729..5e0fc21 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -346,12 +346,13 @@ func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error } func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap) ([]ConnectionStat, error) { - dupCheckMap := make(map[connTmp]struct{}) + dupCheckMap := make(map[string]struct{}) var ret []ConnectionStat var err error for _, t := range tmap { var path string + var connKey string var ls []connTmp path = fmt.Sprintf("%s/net/%s", root, t.filename) switch t.family { @@ -366,7 +367,11 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode return nil, err } for _, c := range ls { - if _, ok := dupCheckMap[c]; ok { + // Build TCP key to id the connection uniquely + // socket type, src ip, src port, dst ip dst port should be enough + // to prevent duplications. + connKey = strconv.Itoa(int(c.sockType)) + "-" + c.laddr.IP + ":" + strconv.Itoa(int(c.laddr.Port)) + "-" + c.raddr.IP + ":" + strconv.Itoa(int(c.raddr.Port)) + if _, ok := dupCheckMap[connKey]; ok { continue } @@ -390,7 +395,7 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode conn.Uids, _ = proc.getUids() ret = append(ret, conn) - dupCheckMap[c] = struct{}{} + dupCheckMap[connKey] = struct{}{} } }