Skip to content

Commit a5bdf5a

Browse files
SimonCqkHusterWan
authored andcommitted
refactor: use raw system call instead of clone-and-read-from-stdout to boost GetKernelVersion.
Signed-off-by: Simoncqk <[email protected]>
1 parent e7e89c8 commit a5bdf5a

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

pkg/kernel/kernel.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package kernel
33
import (
44
"fmt"
55

6-
"github.com/alibaba/pouch/pkg/exec"
7-
"github.com/pkg/errors"
6+
"golang.org/x/sys/unix"
87
)
98

109
// VersionInfo holds information about the kernel.
@@ -27,14 +26,15 @@ func GetKernelVersion() (*VersionInfo, error) {
2726
flavor string
2827
)
2928

30-
_, stdout, _, err := exec.Run(0, "uname", "-r")
29+
buf := unix.Utsname{}
30+
err := unix.Uname(&buf)
3131
if err != nil {
32-
return nil, errors.Wrap(err, "failed to run command uname -r")
32+
return nil, err
3333
}
34-
35-
parsed, _ := fmt.Sscanf(stdout, "%d.%d.%d-%s", &kernel, &major, &minor, &flavor)
34+
release := string(buf.Release[:])
35+
parsed, _ := fmt.Sscanf(release, "%d.%d.%d-%s", &kernel, &major, &minor, &flavor)
3636
if parsed < 3 {
37-
return nil, fmt.Errorf("Can't parse kernel version, release: %s" + stdout)
37+
return nil, fmt.Errorf("Can't parse kernel version, release: %s" + release)
3838
}
3939

4040
return &VersionInfo{

pkg/kernel/kernel_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package kernel
33
import (
44
"testing"
55

6+
"github.com/alibaba/pouch/pkg/exec"
7+
68
"github.com/stretchr/testify/assert"
9+
"golang.org/x/sys/unix"
710
)
811

912
func TestGetKernelVersion(t *testing.T) {
@@ -12,3 +15,23 @@ func TestGetKernelVersion(t *testing.T) {
1215

1316
println(version.String())
1417
}
18+
19+
// Benchmark result for below two methods to execute `uname` command in GetKernelVersion().
20+
21+
// BenchmarkGetUnameByUnix-4 200000 10584 ns/op
22+
// BenchmarkGetUnameByExecRun-4 200 6255530 ns/op
23+
24+
// Benchmark for executing `uname` by raw unix system call
25+
func BenchmarkGetUnameByUnix(b *testing.B) {
26+
for i := 0; i < b.N; i++ {
27+
buf := unix.Utsname{}
28+
unix.Uname(&buf)
29+
}
30+
}
31+
32+
// Benchmark for executing `uname` by original method -- clone and run the command.
33+
func BenchmarkGetUnameByExecRun(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
exec.Run(0, "uname", "-r")
36+
}
37+
}

0 commit comments

Comments
 (0)