Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use raw system call instead of clone-and-read-from-stdout to boost GetKernelVersion. #2463

Merged
merged 1 commit into from
Nov 11, 2018
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"
HusterWan marked this conversation as resolved.
Show resolved Hide resolved

"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)
HusterWan marked this conversation as resolved.
Show resolved Hide resolved
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"
HusterWan marked this conversation as resolved.
Show resolved Hide resolved

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