aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/hodgesds/perf-utils/fs_utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hodgesds/perf-utils/fs_utils.go')
-rw-r--r--vendor/github.com/hodgesds/perf-utils/fs_utils.go102
1 files changed, 102 insertions, 0 deletions
diff --git a/vendor/github.com/hodgesds/perf-utils/fs_utils.go b/vendor/github.com/hodgesds/perf-utils/fs_utils.go
new file mode 100644
index 0000000..1145c9e
--- /dev/null
+++ b/vendor/github.com/hodgesds/perf-utils/fs_utils.go
@@ -0,0 +1,102 @@
1// +build linux
2
3package perf
4
5import (
6 "bufio"
7 "fmt"
8 "os"
9 "strings"
10)
11
12const (
13 // DebugFS is the filesystem type for debugfs.
14 DebugFS = "debugfs"
15
16 // TraceFS is the filesystem type for tracefs.
17 TraceFS = "tracefs"
18
19 // ProcMounts is the mount point for file systems in procfs.
20 ProcMounts = "/proc/mounts"
21
22 // PerfMaxStack is the mount point for the max perf event size.
23 PerfMaxStack = "/proc/sys/kernel/perf_event_max_stack"
24
25 // PerfMaxContexts is a sysfs mount that contains the max perf contexts.
26 PerfMaxContexts = "/proc/sys/kernel/perf_event_max_contexts_per_stack"
27
28 // SyscallsDir is a constant of the default tracing event syscalls directory.
29 SyscallsDir = "/sys/kernel/debug/tracing/events/syscalls/"
30
31 // TracingDir is a constant of the default tracing directory.
32 TracingDir = "/sys/kernel/debug/tracing"
33)
34
35var (
36 // ErrNoMount is when there is no such mount.
37 ErrNoMount = fmt.Errorf("no such mount")
38)
39
40// TraceFSMount returns the first found mount point of a tracefs file system.
41func TraceFSMount() (string, error) {
42 mounts, err := GetFSMount(TraceFS)
43 if err != nil {
44 return "", err
45 }
46 if len(mounts) == 0 {
47 return "", ErrNoMount
48 }
49 return mounts[0], nil
50}
51
52// DebugFSMount returns the first found mount point of a debugfs file system.
53func DebugFSMount() (string, error) {
54 mounts, err := GetFSMount(DebugFS)
55 if err != nil {
56 return "", err
57 }
58 if len(mounts) == 0 {
59 return "", ErrNoMount
60 }
61 return mounts[0], nil
62}
63
64// GetFSMount is a helper function to get a mount file system type.
65func GetFSMount(mountType string) ([]string, error) {
66 mounts := []string{}
67 file, err := os.Open(ProcMounts)
68 if err != nil {
69 return mounts, err
70 }
71 scanner := bufio.NewScanner(file)
72 for scanner.Scan() {
73 mountInfo := strings.Split(scanner.Text(), " ")
74 if len(mountInfo) > 3 && mountInfo[2] == mountType {
75 mounts = append(mounts, mountInfo[1])
76 }
77 }
78 if err := scanner.Err(); err != nil {
79 return mounts, err
80 }
81
82 return mounts, file.Close()
83}
84
85// fileToStrings is a helper method that reads a line line by line and returns
86// a slice of strings.
87func fileToStrings(path string) ([]string, error) {
88 res := []string{}
89 f, err := os.Open(path)
90 if err != nil {
91 return res, err
92 }
93 scanner := bufio.NewScanner(f)
94 for scanner.Scan() {
95 res = append(res, scanner.Text())
96 }
97 if err := scanner.Err(); err != nil {
98 return res, err
99 }
100
101 return res, nil
102}