From 89f3ce2e64cd7eef4e65a14dfbecc9cdb23c0567 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 1 Nov 2017 09:54:58 +0100 Subject: [PATCH] Simplify Utsname string conversion Use Utsname from golang.org/x/sys/unix which contains byte array instead of int8/uint8 array members. This allows to simplify the string conversions of these members and the marshal.FromUtsname functions are no longer needed. --- common/marshal/marshal_darwin.go | 4 ---- common/marshal/marshal_linux.go | 17 ----------------- common/marshal/marshal_linux_arm.go | 15 --------------- probe/endpoint/procspy/proc_linux.go | 9 +++++---- probe/host/system_linux.go | 15 ++++++++------- probe/host/system_linux_test.go | 17 +++++------------ 6 files changed, 18 insertions(+), 59 deletions(-) delete mode 100644 common/marshal/marshal_darwin.go delete mode 100644 common/marshal/marshal_linux.go delete mode 100644 common/marshal/marshal_linux_arm.go diff --git a/common/marshal/marshal_darwin.go b/common/marshal/marshal_darwin.go deleted file mode 100644 index 04becb198..000000000 --- a/common/marshal/marshal_darwin.go +++ /dev/null @@ -1,4 +0,0 @@ -package marshal - -// Stub to make the compiler happy in darwin (i.e. avoid "no buildable Go source -// files in" error) diff --git a/common/marshal/marshal_linux.go b/common/marshal/marshal_linux.go deleted file mode 100644 index 00a360207..000000000 --- a/common/marshal/marshal_linux.go +++ /dev/null @@ -1,17 +0,0 @@ -// +build !arm - -package marshal - -// FromUtsname reads the C-strings from syscall.Utsname and transforms them to Go -// strings -func FromUtsname(ca [65]int8) string { - s := make([]byte, len(ca)) - var lens int - for ; lens < len(ca); lens++ { - if ca[lens] == 0 { - break - } - s[lens] = uint8(ca[lens]) - } - return string(s[0:lens]) -} diff --git a/common/marshal/marshal_linux_arm.go b/common/marshal/marshal_linux_arm.go deleted file mode 100644 index 85b373178..000000000 --- a/common/marshal/marshal_linux_arm.go +++ /dev/null @@ -1,15 +0,0 @@ -package marshal - -// FromUtsname reads the C-strings from syscall.Utsname and transforms them to Go -// strings -func FromUtsname(ca [65]uint8) string { - s := make([]byte, len(ca)) - var lens int - for ; lens < len(ca); lens++ { - if ca[lens] == 0 { - break - } - s[lens] = uint8(ca[lens]) - } - return string(s[0:lens]) -} diff --git a/probe/endpoint/procspy/proc_linux.go b/probe/endpoint/procspy/proc_linux.go index ec676072c..5dc0953fa 100644 --- a/probe/endpoint/procspy/proc_linux.go +++ b/probe/endpoint/procspy/proc_linux.go @@ -14,8 +14,9 @@ import ( "github.com/armon/go-metrics" "github.com/weaveworks/common/fs" - "github.com/weaveworks/scope/common/marshal" "github.com/weaveworks/scope/probe/process" + + "golang.org/x/sys/unix" ) var ( @@ -53,13 +54,13 @@ func newPidWalker(walker process.Walker, tickc <-chan time.Time, fdBlockSize uin } func getKernelVersion() (major, minor int, err error) { - var u syscall.Utsname - if err = syscall.Uname(&u); err != nil { + var u unix.Utsname + if err = unix.Uname(&u); err != nil { return } // Kernel versions are not always a semver, so we have to do minimal parsing. - release := marshal.FromUtsname(u.Release) + release := string(u.Release[:bytes.IndexByte(u.Release[:], 0)]) if n, err := fmt.Sscanf(release, "%d.%d", &major, &minor); err != nil || n != 2 { return 0, 0, fmt.Errorf("Malformed version: %s", release) } diff --git a/probe/host/system_linux.go b/probe/host/system_linux.go index d089fa139..c9c5fcee1 100644 --- a/probe/host/system_linux.go +++ b/probe/host/system_linux.go @@ -1,33 +1,34 @@ package host import ( + "bytes" "fmt" "io/ioutil" "strconv" "strings" - "syscall" "time" linuxproc "github.com/c9s/goprocinfo/linux" - "github.com/weaveworks/scope/common/marshal" "github.com/weaveworks/scope/report" + + "golang.org/x/sys/unix" ) const kb = 1024 // Uname is swappable for mocking in tests. -var Uname = syscall.Uname +var Uname = unix.Uname // GetKernelReleaseAndVersion returns the kernel version as reported by uname. var GetKernelReleaseAndVersion = func() (string, string, error) { - var utsname syscall.Utsname + var utsname unix.Utsname if err := Uname(&utsname); err != nil { return "unknown", "unknown", err } - release := marshal.FromUtsname(utsname.Release) - version := marshal.FromUtsname(utsname.Version) - return release, version, nil + release := utsname.Release[:bytes.IndexByte(utsname.Release[:], 0)] + version := utsname.Version[:bytes.IndexByte(utsname.Version[:], 0)] + return string(release), string(version), nil } // GetLoad returns the current load averages as metrics. diff --git a/probe/host/system_linux_test.go b/probe/host/system_linux_test.go index 8420e703a..99ee4ae50 100644 --- a/probe/host/system_linux_test.go +++ b/probe/host/system_linux_test.go @@ -2,10 +2,11 @@ package host_test import ( "fmt" - "syscall" "testing" "github.com/weaveworks/scope/probe/host" + + "golang.org/x/sys/unix" ) func TestUname(t *testing.T) { @@ -16,9 +17,9 @@ func TestUname(t *testing.T) { release = "rls" version = "ver" ) - host.Uname = func(uts *syscall.Utsname) error { - uts.Release = string2c(release) - uts.Version = string2c(version) + host.Uname = func(uts *unix.Utsname) error { + copy(uts.Release[:], []byte(release)) + copy(uts.Version[:], []byte(version)) return nil } @@ -31,11 +32,3 @@ func TestUname(t *testing.T) { t.Errorf("want %q, have %q", want, have) } } - -func string2c(s string) [65]int8 { - var result [65]int8 - for i, c := range s { - result[i] = int8(c) - } - return result -}