// Copyright 2021 The Prometheus Authors// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.package procfsimport ()// CgroupSummary models one line from /proc/cgroups.// This file contains information about the controllers that are compiled into the kernel.//// Also see http://man7.org/linux/man-pages/man7/cgroups.7.htmltypeCgroupSummarystruct {// The name of the controller. controller is also known as subsystem. SubsysName string// The unique ID of the cgroup hierarchy on which this controller is mounted. Hierarchy int// The number of control groups in this hierarchy using this controller. Cgroups int// This field contains the value 1 if this controller is enabled, or 0 if it has been disabled Enabled int}// parseCgroupSummary parses each line of the /proc/cgroup file// Line format is `subsys_name hierarchy num_cgroups enabled`.func parseCgroupSummaryString( string) (*CgroupSummary, error) {varerror := strings.Fields()// require at least 4 fieldsiflen() < 4 {returnnil, fmt.Errorf("%w: 4+ fields required, found %d fields in cgroup info string: %s", ErrFileParse, len(), ) } := &CgroupSummary{SubsysName: [0], } .Hierarchy, = strconv.Atoi([1])if != nil {returnnil, fmt.Errorf("%w: Unable to parse hierarchy ID from %q", ErrFileParse, [1]) } .Cgroups, = strconv.Atoi([2])if != nil {returnnil, fmt.Errorf("%w: Unable to parse Cgroup Num from %q", ErrFileParse, [2]) } .Enabled, = strconv.Atoi([3])if != nil {returnnil, fmt.Errorf("%w: Unable to parse Enabled from %q", ErrFileParse, [3]) }return , nil}// parseCgroupSummary reads each line of the /proc/cgroup file.func parseCgroupSummary( []byte) ([]CgroupSummary, error) {var []CgroupSummary := bufio.NewScanner(bytes.NewReader())for .Scan() { := .Text()// ignore comment linesifstrings.HasPrefix(, "#") {continue } , := parseCgroupSummaryString()if != nil {returnnil, } = append(, *) } := .Err()return , }// CgroupSummarys returns information about current /proc/cgroups.func ( FS) () ([]CgroupSummary, error) { , := util.ReadFileNoStat(.proc.Path("cgroups"))if != nil {returnnil, }returnparseCgroupSummary()}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.