Source file
src/runtime/pprof/uname_linux_test.go
1
2
3
4
5
6
7 package pprof
8
9 import (
10 "fmt"
11 "regexp"
12 "strconv"
13 "syscall"
14 )
15
16 var versionRe = regexp.MustCompile(`^(\d+)(?:\.(\d+)(?:\.(\d+))).*$`)
17
18 func linuxKernelVersion() (major, minor, patch int, err error) {
19 var uname syscall.Utsname
20 if err := syscall.Uname(&uname); err != nil {
21 return 0, 0, 0, err
22 }
23
24 buf := make([]byte, 0, len(uname.Release))
25 for _, b := range uname.Release {
26 if b == 0 {
27 break
28 }
29 buf = append(buf, byte(b))
30 }
31 rl := string(buf)
32
33 m := versionRe.FindStringSubmatch(rl)
34 if m == nil {
35 return 0, 0, 0, fmt.Errorf("error matching version number in %q", rl)
36 }
37
38 v, err := strconv.ParseInt(m[1], 10, 64)
39 if err != nil {
40 return 0, 0, 0, fmt.Errorf("error parsing major version %q in %s: %w", m[1], rl, err)
41 }
42 major = int(v)
43
44 if len(m) >= 3 {
45 v, err := strconv.ParseInt(m[2], 10, 64)
46 if err != nil {
47 return 0, 0, 0, fmt.Errorf("error parsing minor version %q in %s: %w", m[2], rl, err)
48 }
49 minor = int(v)
50 }
51
52 if len(m) >= 4 {
53 v, err := strconv.ParseInt(m[3], 10, 64)
54 if err != nil {
55 return 0, 0, 0, fmt.Errorf("error parsing patch version %q in %s: %w", m[3], rl, err)
56 }
57 patch = int(v)
58 }
59
60 return
61 }
62
View as plain text