blob: 46f12c61d3e962fa8f22725e2b916a7a6677d63c [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001// Copyright 2019 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package procfs
15
16import (
17 "bufio"
18 "bytes"
19 "fmt"
20 "io"
21 "strconv"
22 "strings"
23
24 "github.com/prometheus/procfs/internal/util"
25)
26
27// For the proc file format details,
28// See:
29// * Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2343
30// * Linux 4.17 https://elixir.bootlin.com/linux/v4.17/source/net/core/net-procfs.c#L162
31// and https://elixir.bootlin.com/linux/v4.17/source/include/linux/netdevice.h#L2810.
32
33// SoftnetStat contains a single row of data from /proc/net/softnet_stat
34type SoftnetStat struct {
35 // Number of processed packets
36 Processed uint32
37 // Number of dropped packets
38 Dropped uint32
39 // Number of times processing packets ran out of quota
40 TimeSqueezed uint32
41}
42
43var softNetProcFile = "net/softnet_stat"
44
45// NetSoftnetStat reads data from /proc/net/softnet_stat.
46func (fs FS) NetSoftnetStat() ([]SoftnetStat, error) {
47 b, err := util.ReadFileNoStat(fs.proc.Path(softNetProcFile))
48 if err != nil {
49 return nil, err
50 }
51
52 entries, err := parseSoftnet(bytes.NewReader(b))
53 if err != nil {
54 return nil, fmt.Errorf("failed to parse /proc/net/softnet_stat: %w", err)
55 }
56
57 return entries, nil
58}
59
60func parseSoftnet(r io.Reader) ([]SoftnetStat, error) {
61 const minColumns = 9
62
63 s := bufio.NewScanner(r)
64
65 var stats []SoftnetStat
66 for s.Scan() {
67 columns := strings.Fields(s.Text())
68 width := len(columns)
69
70 if width < minColumns {
71 return nil, fmt.Errorf("%d columns were detected, but at least %d were expected", width, minColumns)
72 }
73
74 // We only parse the first three columns at the moment.
75 us, err := parseHexUint32s(columns[0:3])
76 if err != nil {
77 return nil, err
78 }
79
80 stats = append(stats, SoftnetStat{
81 Processed: us[0],
82 Dropped: us[1],
83 TimeSqueezed: us[2],
84 })
85 }
86
87 return stats, nil
88}
89
90func parseHexUint32s(ss []string) ([]uint32, error) {
91 us := make([]uint32, 0, len(ss))
92 for _, s := range ss {
93 u, err := strconv.ParseUint(s, 16, 32)
94 if err != nil {
95 return nil, err
96 }
97
98 us = append(us, uint32(u))
99 }
100
101 return us, nil
102}