source: code/trunk/vendor/github.com/prometheus/procfs/proc_cgroups.go@ 822

Last change on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 3.0 KB
Line 
1// Copyright 2021 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 "strconv"
21 "strings"
22
23 "github.com/prometheus/procfs/internal/util"
24)
25
26// CgroupSummary models one line from /proc/cgroups.
27// This file contains information about the controllers that are compiled into the kernel.
28//
29// Also see http://man7.org/linux/man-pages/man7/cgroups.7.html
30type CgroupSummary struct {
31 // The name of the controller. controller is also known as subsystem.
32 SubsysName string
33 // The unique ID of the cgroup hierarchy on which this controller is mounted.
34 Hierarchy int
35 // The number of control groups in this hierarchy using this controller.
36 Cgroups int
37 // This field contains the value 1 if this controller is enabled, or 0 if it has been disabled
38 Enabled int
39}
40
41// parseCgroupSummary parses each line of the /proc/cgroup file
42// Line format is `subsys_name hierarchy num_cgroups enabled`.
43func parseCgroupSummaryString(CgroupSummaryStr string) (*CgroupSummary, error) {
44 var err error
45
46 fields := strings.Fields(CgroupSummaryStr)
47 // require at least 4 fields
48 if len(fields) < 4 {
49 return nil, fmt.Errorf("at least 4 fields required, found %d fields in cgroup info string: %s", len(fields), CgroupSummaryStr)
50 }
51
52 CgroupSummary := &CgroupSummary{
53 SubsysName: fields[0],
54 }
55 CgroupSummary.Hierarchy, err = strconv.Atoi(fields[1])
56 if err != nil {
57 return nil, fmt.Errorf("failed to parse hierarchy ID")
58 }
59 CgroupSummary.Cgroups, err = strconv.Atoi(fields[2])
60 if err != nil {
61 return nil, fmt.Errorf("failed to parse Cgroup Num")
62 }
63 CgroupSummary.Enabled, err = strconv.Atoi(fields[3])
64 if err != nil {
65 return nil, fmt.Errorf("failed to parse Enabled")
66 }
67 return CgroupSummary, nil
68}
69
70// parseCgroupSummary reads each line of the /proc/cgroup file.
71func parseCgroupSummary(data []byte) ([]CgroupSummary, error) {
72 var CgroupSummarys []CgroupSummary
73 scanner := bufio.NewScanner(bytes.NewReader(data))
74 for scanner.Scan() {
75 CgroupSummaryString := scanner.Text()
76 // ignore comment lines
77 if strings.HasPrefix(CgroupSummaryString, "#") {
78 continue
79 }
80 CgroupSummary, err := parseCgroupSummaryString(CgroupSummaryString)
81 if err != nil {
82 return nil, err
83 }
84 CgroupSummarys = append(CgroupSummarys, *CgroupSummary)
85 }
86
87 err := scanner.Err()
88 return CgroupSummarys, err
89}
90
91// CgroupSummarys returns information about current /proc/cgroups.
92func (fs FS) CgroupSummarys() ([]CgroupSummary, error) {
93 data, err := util.ReadFileNoStat(fs.proc.Path("cgroups"))
94 if err != nil {
95 return nil, err
96 }
97 return parseCgroupSummary(data)
98}
Note: See TracBrowser for help on using the repository browser.