...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package procfs
15
16 import (
17 "bufio"
18 "bytes"
19 "errors"
20 "fmt"
21 "io"
22 "strconv"
23 "strings"
24
25 "github.com/prometheus/procfs/internal/util"
26 )
27
28
29 type Interrupt struct {
30
31 Info string
32
33 Devices string
34
35 Values []string
36 }
37
38
39
40
41 type Interrupts map[string]Interrupt
42
43
44 func (p Proc) Interrupts() (Interrupts, error) {
45 data, err := util.ReadFileNoStat(p.path("interrupts"))
46 if err != nil {
47 return nil, err
48 }
49 return parseInterrupts(bytes.NewReader(data))
50 }
51
52 func parseInterrupts(r io.Reader) (Interrupts, error) {
53 var (
54 interrupts = Interrupts{}
55 scanner = bufio.NewScanner(r)
56 )
57
58 if !scanner.Scan() {
59 return nil, errors.New("interrupts empty")
60 }
61 cpuNum := len(strings.Fields(scanner.Text()))
62
63 for scanner.Scan() {
64 parts := strings.Fields(scanner.Text())
65 if len(parts) == 0 {
66 continue
67 }
68 if len(parts) < 2 {
69 return nil, fmt.Errorf("%w: Not enough fields in interrupts (expected 2+ fields but got %d): %s", ErrFileParse, len(parts), parts)
70 }
71 intName := parts[0][:len(parts[0])-1]
72
73 if len(parts) == 2 {
74 interrupts[intName] = Interrupt{
75 Info: "",
76 Devices: "",
77 Values: []string{
78 parts[1],
79 },
80 }
81 continue
82 }
83
84 intr := Interrupt{
85 Values: parts[1 : cpuNum+1],
86 }
87
88 if _, err := strconv.Atoi(intName); err == nil {
89 intr.Info = parts[cpuNum+1]
90 intr.Devices = strings.Join(parts[cpuNum+2:], " ")
91 } else {
92 intr.Info = strings.Join(parts[cpuNum+1:], " ")
93 }
94 interrupts[intName] = intr
95 }
96
97 return interrupts, scanner.Err()
98 }
99
View as plain text