diff --git a/pkg/kernel/kernel.go b/pkg/kernel/kernel.go index 3ceaa7ad5..f05ee25f6 100644 --- a/pkg/kernel/kernel.go +++ b/pkg/kernel/kernel.go @@ -3,8 +3,7 @@ package kernel import ( "fmt" - "github.com/alibaba/pouch/pkg/exec" - "github.com/pkg/errors" + "golang.org/x/sys/unix" ) // VersionInfo holds information about the kernel. @@ -27,14 +26,15 @@ func GetKernelVersion() (*VersionInfo, error) { flavor string ) - _, stdout, _, err := exec.Run(0, "uname", "-r") + buf := unix.Utsname{} + err := unix.Uname(&buf) if err != nil { - return nil, errors.Wrap(err, "failed to run command uname -r") + return nil, err } - - parsed, _ := fmt.Sscanf(stdout, "%d.%d.%d-%s", &kernel, &major, &minor, &flavor) + release := string(buf.Release[:]) + parsed, _ := fmt.Sscanf(release, "%d.%d.%d-%s", &kernel, &major, &minor, &flavor) if parsed < 3 { - return nil, fmt.Errorf("Can't parse kernel version, release: %s" + stdout) + return nil, fmt.Errorf("Can't parse kernel version, release: %s" + release) } return &VersionInfo{ diff --git a/pkg/kernel/kernel_test.go b/pkg/kernel/kernel_test.go index 7b052e269..6bd13ba2e 100644 --- a/pkg/kernel/kernel_test.go +++ b/pkg/kernel/kernel_test.go @@ -3,7 +3,10 @@ package kernel import ( "testing" + "github.com/alibaba/pouch/pkg/exec" + "github.com/stretchr/testify/assert" + "golang.org/x/sys/unix" ) func TestGetKernelVersion(t *testing.T) { @@ -12,3 +15,23 @@ func TestGetKernelVersion(t *testing.T) { println(version.String()) } + +// Benchmark result for below two methods to execute `uname` command in GetKernelVersion(). + +// BenchmarkGetUnameByUnix-4 200000 10584 ns/op +// BenchmarkGetUnameByExecRun-4 200 6255530 ns/op + +// Benchmark for executing `uname` by raw unix system call +func BenchmarkGetUnameByUnix(b *testing.B) { + for i := 0; i < b.N; i++ { + buf := unix.Utsname{} + unix.Uname(&buf) + } +} + +// Benchmark for executing `uname` by original method -- clone and run the command. +func BenchmarkGetUnameByExecRun(b *testing.B) { + for i := 0; i < b.N; i++ { + exec.Run(0, "uname", "-r") + } +}