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. #361

Merged
merged 2 commits into from
May 4, 2017
Merged
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
23 changes: 19 additions & 4 deletions net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package net

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -613,14 +614,22 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
// IPv6 not supported, return empty.
return []connTmp{}, nil
}
lines, err := common.ReadLines(file)

// Read the contents of the /proc file with a single read sys call.
// This minimizes duplicates in the returned connections
// For more info:
// https://github.com/shirou/gopsutil/pull/361
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
}
Expand Down Expand Up @@ -667,15 +676,21 @@ 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)
// Read the contents of the /proc file with a single read sys call.
// This minimizes duplicates in the returned connections
// For more info:
// https://github.com/shirou/gopsutil/pull/361
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
}
Expand Down