Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions pkg/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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{
Expand Down
23 changes: 23 additions & 0 deletions pkg/kernel/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking, how long this two Benchmark case execute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
the result shows 4.135s

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")
}
}