Source File
proc_cgroup.go
Belonging Package
github.com/prometheus/procfs
// Copyright 2020 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 ()// Cgroup models one line from /proc/[pid]/cgroup. Each Cgroup struct describes the placement of a PID inside a// specific control hierarchy. The kernel has two cgroup APIs, v1 and v2. The v1 has one hierarchy per available resource// controller, while v2 has one unified hierarchy shared by all controllers. Regardless of v1 or v2, all hierarchies// contain all running processes, so the question answerable with a Cgroup struct is 'where is this process in// this hierarchy' (where==what path on the specific cgroupfs). By prefixing this path with the mount point of// *this specific* hierarchy, you can locate the relevant pseudo-files needed to read/set the data for this PID// in this hierarchy//// Also see http://man7.org/linux/man-pages/man7/cgroups.7.htmltype Cgroup struct {// HierarchyID that can be matched to a named hierarchy using /proc/cgroups. Cgroups V2 only has one// hierarchy, so HierarchyID is always 0. For cgroups v1 this is a unique ID numberHierarchyID int// Controllers using this hierarchy of processes. Controllers are also known as subsystems. For// Cgroups V2 this may be empty, as all active controllers use the same hierarchyControllers []string// Path of this control group, relative to the mount point of the cgroupfs representing this specific// hierarchyPath string}// parseCgroupString parses each line of the /proc/[pid]/cgroup file// Line format is hierarchyID:[controller1,controller2]:path.func parseCgroupString( string) (*Cgroup, error) {var error:= strings.SplitN(, ":", 3)if len() < 3 {return nil, fmt.Errorf("%w: 3+ fields required, found %d fields in cgroup string: %s", ErrFileParse, len(), )}:= &Cgroup{Path: [2],Controllers: nil,}.HierarchyID, = strconv.Atoi([0])if != nil {return nil, fmt.Errorf("%w: hierarchy ID: %q", ErrFileParse, .HierarchyID)}if [1] != "" {:= strings.Split([1], ",").Controllers = append(.Controllers, ...)}return , nil}// parseCgroups reads each line of the /proc/[pid]/cgroup file.func parseCgroups( []byte) ([]Cgroup, error) {var []Cgroup:= bufio.NewScanner(bytes.NewReader())for .Scan() {:= .Text(), := parseCgroupString()if != nil {return nil,}= append(, *)}:= .Err()return ,}// Cgroups reads from /proc/<pid>/cgroups and returns a []*Cgroup struct locating this PID in each process// control hierarchy running on this system. On every system (v1 and v2), all hierarchies contain all processes,// so the len of the returned struct is equal to the number of active hierarchies on this system.func ( Proc) () ([]Cgroup, error) {, := util.ReadFileNoStat(.path("cgroup"))if != nil {return nil,}return parseCgroups()}
![]() |
The pages are generated with Golds v0.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. |