Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read /proc/net files with a single read syscall #1380

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions collector/tcpstat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package collector

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -108,23 +108,21 @@ func getTCPStats(statsFile string) (map[tcpConnectionState]float64, error) {
}

func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
var (
tcpStats = map[tcpConnectionState]float64{}
scanner = bufio.NewScanner(r)
)
tcpStats := map[tcpConnectionState]float64{}
contents, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

for scanner.Scan() {
parts := strings.Fields(scanner.Text())
for _, line := range strings.Split(string(contents), "\n")[1:] {
parts := strings.Fields(line)
if len(parts) == 0 {
continue
}
if len(parts) < 4 {
return nil, fmt.Errorf("invalid TCP stats line: %q", scanner.Text())
return nil, fmt.Errorf("invalid TCP stats line: %q", line)
}

if strings.HasPrefix(parts[0], "sl") {
continue
}
st, err := strconv.ParseInt(parts[3], 16, 8)
if err != nil {
return nil, err
Expand All @@ -133,7 +131,7 @@ func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
tcpStats[tcpConnectionState(st)]++
}

return tcpStats, scanner.Err()
return tcpStats, nil
}

func (st tcpConnectionState) String() string {
Expand Down
2 changes: 1 addition & 1 deletion collector/tcpstat_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_parseTCPStatsError(t *testing.T) {
}{
{
name: "too few fields",
in: "hello world",
in: "sl local_address\n 0: 00000000:0016",
},
}

Expand Down