aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorKir Kolyshkin <kolyshkin@gmail.com>2022-08-18 13:43:47 -0700
committerGopher Robot <gobot@golang.org>2022-09-03 18:39:45 +0000
commita73506cff560c4d95ac604f4f76d58386ca29ed9 (patch)
tree3a923b1fcbd3c3f24ccb495bb434c20b3b312024 /src/runtime
parent2392b7061cfc71dcdaefeb027dcce0951f697658 (diff)
downloadgo-a73506cff560c4d95ac604f4f76d58386ca29ed9.tar.xz
internal/syscall/unix: consolidate kernelVersion implementations
Currently, there are 3 functions returning Linux kernel version numbers. Two of them are identical: - in net, initially added by commit 0a9dd47dd817904e; - in internal/poll, initially added by commit 1c7650aa93bd53; (both were later fixed by commit 66c02645062561a). The third one is a more complex, regexp-based implementation in runtime/pprof, which is only used for a test. Instead of adding one more, let's consolidate existing ones. Remove the complex implementation, and move the simple one into internal/syscall/unix. Use it from all the three places mentioned above. Change-Id: I4a34d9ca47257743c16def30e4dd634e36056091 Reviewed-on: https://go-review.googlesource.com/c/go/+/424896 Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/pprof/pprof_test.go8
-rw-r--r--src/runtime/pprof/uname_linux_test.go61
-rw-r--r--src/runtime/pprof/uname_other_test.go15
3 files changed, 3 insertions, 81 deletions
diff --git a/src/runtime/pprof/pprof_test.go b/src/runtime/pprof/pprof_test.go
index 28b8f4319e..31a4024be8 100644
--- a/src/runtime/pprof/pprof_test.go
+++ b/src/runtime/pprof/pprof_test.go
@@ -12,6 +12,7 @@ import (
"fmt"
"internal/abi"
"internal/profile"
+ "internal/syscall/unix"
"internal/testenv"
"io"
"math"
@@ -116,11 +117,8 @@ func TestCPUProfileMultithreadMagnitude(t *testing.T) {
// Linux [5.9,5.16) has a kernel bug that can break CPU timers on newly
// created threads, breaking our CPU accounting.
- major, minor, patch, err := linuxKernelVersion()
- if err != nil {
- t.Errorf("Error determining kernel version: %v", err)
- }
- t.Logf("Running on Linux %d.%d.%d", major, minor, patch)
+ major, minor := unix.KernelVersion()
+ t.Logf("Running on Linux %d.%d", major, minor)
defer func() {
if t.Failed() {
t.Logf("Failure of this test may indicate that your system suffers from a known Linux kernel bug fixed on newer kernels. See https://golang.org/issue/49065.")
diff --git a/src/runtime/pprof/uname_linux_test.go b/src/runtime/pprof/uname_linux_test.go
deleted file mode 100644
index 8374c83f74..0000000000
--- a/src/runtime/pprof/uname_linux_test.go
+++ /dev/null
@@ -1,61 +0,0 @@
-// 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
-}
diff --git a/src/runtime/pprof/uname_other_test.go b/src/runtime/pprof/uname_other_test.go
deleted file mode 100644
index 327640755b..0000000000
--- a/src/runtime/pprof/uname_other_test.go
+++ /dev/null
@@ -1,15 +0,0 @@
-// 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 (
- "errors"
-)
-
-func linuxKernelVersion() (major, minor, patch int, err error) {
- return 0, 0, 0, errors.New("not running on linux")
-}