| 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | //go:build linux |
| 22 | // +build linux |
| 23 | |
| 24 | package cgroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/ |
| 25 | |
| 26 | import ( |
| 27 | "bufioscip-go gomod github.com/golang/go/src go1.18 bufio/" |
| 28 | "osscip-go gomod github.com/golang/go/src go1.18 os/" |
| 29 | "strconvscip-go gomod github.com/golang/go/src go1.18 strconv/" |
| 30 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 31 | ) |
| 32 | |
| 33 | const ( |
| 34 | _cgroupSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSep. = ":" |
| 35 | _cgroupSubsysSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSubsysSep. = "," |
| 36 | ) |
| 37 | |
| 38 | const ( |
| 39 | _csFieldIDIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldIDID. = iota |
| 40 | _csFieldIDSubsystemsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldIDSubsystems. |
| 41 | _csFieldIDNamescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldIDName. |
| 42 | _csFieldCountscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldCount. |
| 43 | ) |
| 44 | |
| 45 | // CGroupSubsys represents the data structure for entities in |
| 46 | // `/proc/$PID/cgroup`. See also proc(5) for more information. |
| 47 | type CGroupSubsysscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys# struct { |
| 48 | IDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#ID. int |
| 49 | Subsystemsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#Subsystems. []string |
| 50 | Namescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#Name. string |
| 51 | } |
| 52 | |
| 53 | // NewCGroupSubsysFromLine returns a new *CGroupSubsys by parsing a string in |
| 54 | // the format of `/proc/$PID/cgroup` |
| 55 | func NewCGroupSubsysFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroupSubsysFromLine().(line string) (*CGroupSubsysscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#, error) { |
| 56 | fields := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.SplitNscip-go gomod github.com/golang/go/src go1.18 strings/SplitN().(line, _cgroupSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSep., _csFieldCountscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldCount.) |
| 57 | |
| 58 | if len(fields) != _csFieldCountscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldCount. { |
| 59 | return nil, cgroupSubsysFormatInvalidErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/cgroupSubsysFormatInvalidError#{line} |
| 60 | } |
| 61 | |
| 62 | id, err := strconvscip-go gomod github.com/golang/go/src go1.18 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.18 strconv/Atoi().(fields[_csFieldIDIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldIDID.]) |
| 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | |
| 67 | cgroup := &CGroupSubsysscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#{ |
| 68 | IDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#ID.: id, |
| 69 | Subsystemsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#Subsystems.: stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(fields[_csFieldIDSubsystemsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldIDSubsystems.], _cgroupSubsysSepscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSubsysSep.), |
| 70 | Namescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#Name.: fields[_csFieldIDNamescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_csFieldIDName.], |
| 71 | } |
| 72 | |
| 73 | return cgroup, nil |
| 74 | } |
| 75 | |
| 76 | // parseCGroupSubsystems parses procPathCGroup (usually at `/proc/$PID/cgroup`) |
| 77 | // and returns a new map[string]*CGroupSubsys. |
| 78 | func parseCGroupSubsystemsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/parseCGroupSubsystems().(procPathCGroup string) (map[string]*CGroupSubsysscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#, error) { |
| 79 | cgroupFile, err := osscip-go gomod github.com/golang/go/src go1.18 os/.Openscip-go gomod github.com/golang/go/src go1.18 os/Open().(procPathCGroup) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | defer cgroupFile.Closescip-go gomod github.com/golang/go/src go1.18 os/File#Close().() |
| 84 | |
| 85 | scanner := bufioscip-go gomod github.com/golang/go/src go1.18 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.18 bufio/NewScanner().(cgroupFile) |
| 86 | subsystems := make(map[string]*CGroupSubsysscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#) |
| 87 | |
| 88 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.18 bufio/Scanner#Scan().() { |
| 89 | cgroup, err := NewCGroupSubsysFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroupSubsysFromLine().(scanner.Textscip-go gomod github.com/golang/go/src go1.18 bufio/Scanner#Text().()) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | for _, subsys := range cgroup.Subsystemsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroupSubsys#Subsystems. { |
| 94 | subsystems[subsys] = cgroup |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if err := scanner.Errscip-go gomod github.com/golang/go/src go1.18 bufio/Scanner#Err().(); err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | return subsystems, nil |
| 103 | } |