summaryrefslogtreecommitdiffstats
path: root/src/runtime/pprof/uname_linux_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/runtime/pprof/uname_linux_test.go')
-rw-r--r--src/runtime/pprof/uname_linux_test.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/runtime/pprof/uname_linux_test.go b/src/runtime/pprof/uname_linux_test.go
new file mode 100644
index 0000000..8374c83
--- /dev/null
+++ b/src/runtime/pprof/uname_linux_test.go
@@ -0,0 +1,61 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build linux
+
+package pprof
+
+import (
+ "fmt"
+ "regexp"
+ "strconv"
+ "syscall"
+)
+
+var versionRe = regexp.MustCompile(`^(\d+)(?:\.(\d+)(?:\.(\d+))).*$`)
+
+func linuxKernelVersion() (major, minor, patch int, err error) {
+ var uname syscall.Utsname
+ if err := syscall.Uname(&uname); err != nil {
+ return 0, 0, 0, err
+ }
+
+ buf := make([]byte, 0, len(uname.Release))
+ for _, b := range uname.Release {
+ if b == 0 {
+ break
+ }
+ buf = append(buf, byte(b))
+ }
+ rl := string(buf)
+
+ m := versionRe.FindStringSubmatch(rl)
+ if m == nil {
+ return 0, 0, 0, fmt.Errorf("error matching version number in %q", rl)
+ }
+
+ v, err := strconv.ParseInt(m[1], 10, 64)
+ if err != nil {
+ return 0, 0, 0, fmt.Errorf("error parsing major version %q in %s: %w", m[1], rl, err)
+ }
+ major = int(v)
+
+ if len(m) >= 3 {
+ v, err := strconv.ParseInt(m[2], 10, 64)
+ if err != nil {
+ return 0, 0, 0, fmt.Errorf("error parsing minor version %q in %s: %w", m[2], rl, err)
+ }
+ minor = int(v)
+ }
+
+ if len(m) >= 4 {
+ v, err := strconv.ParseInt(m[3], 10, 64)
+ if err != nil {
+ return 0, 0, 0, fmt.Errorf("error parsing patch version %q in %s: %w", m[3], rl, err)
+ }
+ patch = int(v)
+ }
+
+ return
+}