Read /proc/net files with a single read syscall.

The /proc/net files are not guaranteed to be consistent, they are only
consitent on the row level. This is probably one of the reasons why
consequent read calls might return duplicate entries - the kernel is
changing the file as it is being read. In certain situations this might
lead to loop like situations - the same net entry is being returned when
reading the file as new connections are added to the kernel tcp table, i.e
there can be a lot of duplications.

This commit is trying to reduce the duplications, by fetching the contents
of the net files with a single read syscall.
pull/361/head
Alexander Blagoev 8 years ago
parent 119305b4ce
commit 531f9507d2

@ -3,6 +3,7 @@
package net
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
@ -613,14 +614,18 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
// IPv6 not supported, return empty.
return []connTmp{}, nil
}
lines, err := common.ReadLines(file)
contents, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
lines := bytes.Split(contents, []byte("\n"))
var ret []connTmp
// skip first line
for _, line := range lines[1:] {
l := strings.Fields(line)
l := strings.Fields(string(line))
if len(l) < 10 {
continue
}
@ -667,15 +672,17 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
}
func processUnix(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
lines, err := common.ReadLines(file)
contents, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
lines := bytes.Split(contents, []byte("\n"))
var ret []connTmp
// skip first line
for _, line := range lines[1:] {
tokens := strings.Fields(line)
tokens := strings.Fields(string(line))
if len(tokens) < 6 {
continue
}

Loading…
Cancel
Save