mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-15 19:41:02 +00:00
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.
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
package marshal
|
||||
|
||||
// Stub to make the compiler happy in darwin (i.e. avoid "no buildable Go source
|
||||
// files in" error)
|
||||
@@ -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])
|
||||
}
|
||||
@@ -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])
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user