Merge pull request #4371 from thaJeztah/bump_cgroups

vendor: containerd/cgroups 318312a373405e5e91134d8063d04d59768a1bff
This commit is contained in:
Akihiro Suda 2020-07-13 23:12:52 +09:00 committed by GitHub
commit 9cd02b5953
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 85 additions and 106 deletions

View File

@ -2,7 +2,7 @@ github.com/beorn7/perks v1.0.1
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/cespare/xxhash/v2 v2.1.1 github.com/cespare/xxhash/v2 v2.1.1
github.com/containerd/btrfs 153935315f4ab9be5bf03650a1341454b05efa5d github.com/containerd/btrfs 153935315f4ab9be5bf03650a1341454b05efa5d
github.com/containerd/cgroups 0dbf7f05ba59274095946e2c0c89540726e8a8aa github.com/containerd/cgroups 318312a373405e5e91134d8063d04d59768a1bff
github.com/containerd/console v1.0.0 github.com/containerd/console v1.0.0
github.com/containerd/continuity d3ef23f19fbb106bb73ffde425d07a9187e30745 github.com/containerd/continuity d3ef23f19fbb106bb73ffde425d07a9187e30745
github.com/containerd/fifo f15a3290365b9d2627d189e619ab4008e0069caf github.com/containerd/fifo f15a3290365b9d2627d189e619ab4008e0069caf

View File

@ -1,6 +1,6 @@
# cgroups # cgroups
[![Build Status](https://travis-ci.org/containerd/cgroups.svg?branch=master)](https://travis-ci.org/containerd/cgroups) [![Build Status](https://github.com/containerd/cgroups/workflows/CI/badge.svg)](https://github.com/containerd/cgroups/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/containerd/cgroups/branch/master/graph/badge.svg)](https://codecov.io/gh/containerd/cgroups) [![codecov](https://codecov.io/gh/containerd/cgroups/branch/master/graph/badge.svg)](https://codecov.io/gh/containerd/cgroups)
[![GoDoc](https://godoc.org/github.com/containerd/cgroups?status.svg)](https://godoc.org/github.com/containerd/cgroups) [![GoDoc](https://godoc.org/github.com/containerd/cgroups?status.svg)](https://godoc.org/github.com/containerd/cgroups)
[![Go Report Card](https://goreportcard.com/badge/github.com/containerd/cgroups)](https://goreportcard.com/report/github.com/containerd/cgroups) [![Go Report Card](https://goreportcard.com/badge/github.com/containerd/cgroups)](https://goreportcard.com/report/github.com/containerd/cgroups)
@ -65,7 +65,7 @@ To update the resources applied in the cgroup
```go ```go
shares = uint64(200) shares = uint64(200)
if err := control.Update(&specs.LinuxResources{ if err := control.Update(&specs.LinuxResources{
CPU: &specs.CPU{ CPU: &specs.LinuxCPU{
Shares: &shares, Shares: &shares,
}, },
}); err != nil { }); err != nil {

View File

@ -20,7 +20,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -72,8 +71,8 @@ func (b *blkioController) Create(path string, resources *specs.LinuxResources) e
} }
for _, t := range createBlkioSettings(resources.BlockIO) { for _, t := range createBlkioSettings(resources.BlockIO) {
if t.value != nil { if t.value != nil {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(b.Path(path), fmt.Sprintf("blkio.%s", t.name)), filepath.Join(b.Path(path), "blkio."+t.name),
t.format(t.value), t.format(t.value),
defaultFilePerm, defaultFilePerm,
); err != nil { ); err != nil {
@ -94,7 +93,7 @@ func (b *blkioController) Stat(path string, stats *v1.Metrics) error {
var settings []blkioStatSettings var settings []blkioStatSettings
// Try to read CFQ stats available on all CFQ enabled kernels first // Try to read CFQ stats available on all CFQ enabled kernels first
if _, err := os.Lstat(filepath.Join(b.Path(path), fmt.Sprintf("blkio.io_serviced_recursive"))); err == nil { if _, err := os.Lstat(filepath.Join(b.Path(path), "blkio.io_serviced_recursive")); err == nil {
settings = []blkioStatSettings{ settings = []blkioStatSettings{
{ {
name: "sectors_recursive", name: "sectors_recursive",
@ -174,7 +173,7 @@ func (b *blkioController) Stat(path string, stats *v1.Metrics) error {
} }
func (b *blkioController) readEntry(devices map[deviceKey]string, path, name string, entry *[]*v1.BlkIOEntry) error { func (b *blkioController) readEntry(devices map[deviceKey]string, path, name string, entry *[]*v1.BlkIOEntry) error {
f, err := os.Open(filepath.Join(b.Path(path), fmt.Sprintf("blkio.%s", name))) f, err := os.Open(filepath.Join(b.Path(path), "blkio."+name))
if err != nil { if err != nil {
return err return err
} }
@ -188,7 +187,7 @@ func (b *blkioController) readEntry(devices map[deviceKey]string, path, name str
// skip total line // skip total line
continue continue
} else { } else {
return fmt.Errorf("Invalid line found while parsing %s: %s", path, sc.Text()) return fmt.Errorf("invalid line found while parsing %s: %s", path, sc.Text())
} }
} }
major, err := strconv.ParseUint(fields[0], 10, 64) major, err := strconv.ParseUint(fields[0], 10, 64)
@ -357,11 +356,3 @@ func getDevices(r io.Reader) (map[deviceKey]string, error) {
} }
return devices, s.Err() return devices, s.Err()
} }
func major(devNumber uint64) uint64 {
return (devNumber >> 8) & 0xfff
}
func minor(devNumber uint64) uint64 {
return (devNumber & 0xff) | ((devNumber >> 12) & 0xfff00)
}

View File

@ -18,7 +18,6 @@ package cgroups
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -169,7 +168,7 @@ func (c *cgroup) add(process Process) error {
if err != nil { if err != nil {
return err return err
} }
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(s.Path(p), cgroupProcs), filepath.Join(s.Path(p), cgroupProcs),
[]byte(strconv.Itoa(process.Pid)), []byte(strconv.Itoa(process.Pid)),
defaultFilePerm, defaultFilePerm,
@ -199,7 +198,7 @@ func (c *cgroup) addTask(process Process) error {
if err != nil { if err != nil {
return err return err
} }
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(s.Path(p), cgroupTasks), filepath.Join(s.Path(p), cgroupTasks),
[]byte(strconv.Itoa(process.Pid)), []byte(strconv.Itoa(process.Pid)),
defaultFilePerm, defaultFilePerm,
@ -217,7 +216,7 @@ func (c *cgroup) Delete() error {
if c.err != nil { if c.err != nil {
return c.err return c.err
} }
var errors []string var errs []string
for _, s := range c.subsystems { for _, s := range c.subsystems {
if d, ok := s.(deleter); ok { if d, ok := s.(deleter); ok {
sp, err := c.path(s.Name()) sp, err := c.path(s.Name())
@ -225,7 +224,7 @@ func (c *cgroup) Delete() error {
return err return err
} }
if err := d.Delete(sp); err != nil { if err := d.Delete(sp); err != nil {
errors = append(errors, string(s.Name())) errs = append(errs, string(s.Name()))
} }
continue continue
} }
@ -236,12 +235,12 @@ func (c *cgroup) Delete() error {
} }
path := p.Path(sp) path := p.Path(sp)
if err := remove(path); err != nil { if err := remove(path); err != nil {
errors = append(errors, path) errs = append(errs, path)
} }
} }
} }
if len(errors) > 0 { if len(errs) > 0 {
return fmt.Errorf("cgroups: unable to remove paths %s", strings.Join(errors, ", ")) return fmt.Errorf("cgroups: unable to remove paths %s", strings.Join(errs, ", "))
} }
c.err = ErrCgroupDeleted c.err = ErrCgroupDeleted
return nil return nil

View File

@ -18,8 +18,6 @@ package cgroups
import ( import (
"bufio" "bufio"
"fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -84,8 +82,8 @@ func (c *cpuController) Create(path string, resources *specs.LinuxResources) err
value = []byte(strconv.FormatInt(*t.ivalue, 10)) value = []byte(strconv.FormatInt(*t.ivalue, 10))
} }
if value != nil { if value != nil {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(c.Path(path), fmt.Sprintf("cpu.%s", t.name)), filepath.Join(c.Path(path), "cpu."+t.name),
value, value,
defaultFilePerm, defaultFilePerm,
); err != nil { ); err != nil {

View File

@ -26,7 +26,7 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
) )
func NewCputset(root string) *cpusetController { func NewCpuset(root string) *cpusetController {
return &cpusetController{ return &cpusetController{
root: filepath.Join(root, string(Cpuset)), root: filepath.Join(root, string(Cpuset)),
} }
@ -69,8 +69,8 @@ func (c *cpusetController) Create(path string, resources *specs.LinuxResources)
}, },
} { } {
if t.value != "" { if t.value != "" {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(c.Path(path), fmt.Sprintf("cpuset.%s", t.name)), filepath.Join(c.Path(path), "cpuset."+t.name),
[]byte(t.value), []byte(t.value),
defaultFilePerm, defaultFilePerm,
); err != nil { ); err != nil {
@ -134,7 +134,7 @@ func (c *cpusetController) copyIfNeeded(current, parent string) error {
return err return err
} }
if isEmpty(currentCpus) { if isEmpty(currentCpus) {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(current, "cpuset.cpus"), filepath.Join(current, "cpuset.cpus"),
parentCpus, parentCpus,
defaultFilePerm, defaultFilePerm,
@ -143,7 +143,7 @@ func (c *cpusetController) copyIfNeeded(current, parent string) error {
} }
} }
if isEmpty(currentMems) { if isEmpty(currentMems) {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(current, "cpuset.mems"), filepath.Join(current, "cpuset.mems"),
parentMems, parentMems,
defaultFilePerm, defaultFilePerm,

View File

@ -18,7 +18,6 @@ package cgroups
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -61,7 +60,7 @@ func (d *devicesController) Create(path string, resources *specs.LinuxResources)
if device.Type == "" { if device.Type == "" {
device.Type = "a" device.Type = "a"
} }
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(d.Path(path), file), filepath.Join(d.Path(path), file),
[]byte(deviceString(device)), []byte(deviceString(device)),
defaultFilePerm, defaultFilePerm,

View File

@ -50,7 +50,7 @@ func (f *freezerController) Thaw(path string) error {
} }
func (f *freezerController) changeState(path string, state State) error { func (f *freezerController) changeState(path string, state State) error {
return ioutil.WriteFile( return retryingWriteFile(
filepath.Join(f.root, path, "freezer.state"), filepath.Join(f.root, path, "freezer.state"),
[]byte(strings.ToUpper(string(state))), []byte(strings.ToUpper(string(state))),
defaultFilePerm, defaultFilePerm,

View File

@ -3,17 +3,16 @@ module github.com/containerd/cgroups
go 1.13 go 1.13
require ( require (
github.com/cilium/ebpf v0.0.0-20200110133405-4032b1d8aae3 github.com/cilium/ebpf v0.0.0-20200702112145-1c8d4c9ef775
github.com/coreos/go-systemd/v22 v22.0.0 github.com/coreos/go-systemd/v22 v22.0.0
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/docker/go-units v0.4.0 github.com/docker/go-units v0.4.0
github.com/godbus/dbus/v5 v5.0.3 github.com/godbus/dbus/v5 v5.0.3
github.com/gogo/protobuf v1.3.1 github.com/gogo/protobuf v1.3.1
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/opencontainers/runtime-spec v1.0.2 github.com/opencontainers/runtime-spec v1.0.2
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.2.2 github.com/stretchr/testify v1.2.2
github.com/urfave/cli v1.22.2 github.com/urfave/cli v1.22.2
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9
) )

View File

@ -17,7 +17,6 @@
package cgroups package cgroups
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -57,7 +56,7 @@ func (h *hugetlbController) Create(path string, resources *specs.LinuxResources)
return err return err
} }
for _, limit := range resources.HugepageLimits { for _, limit := range resources.HugepageLimits {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(h.Path(path), strings.Join([]string{"hugetlb", limit.Pagesize, "limit_in_bytes"}, ".")), filepath.Join(h.Path(path), strings.Join([]string{"hugetlb", limit.Pagesize, "limit_in_bytes"}, ".")),
[]byte(strconv.FormatUint(limit.Limit, 10)), []byte(strconv.FormatUint(limit.Limit, 10)),
defaultFilePerm, defaultFilePerm,

View File

@ -20,7 +20,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -214,7 +213,7 @@ func (m *memoryController) Create(path string, resources *specs.LinuxResources)
// until a limit is set on the cgroup and limit cannot be set once the // until a limit is set on the cgroup and limit cannot be set once the
// cgroup has children, or if there are already tasks in the cgroup. // cgroup has children, or if there are already tasks in the cgroup.
for _, i := range []int64{1, -1} { for _, i := range []int64{1, -1} {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(m.Path(path), "memory.kmem.limit_in_bytes"), filepath.Join(m.Path(path), "memory.kmem.limit_in_bytes"),
[]byte(strconv.FormatInt(i, 10)), []byte(strconv.FormatInt(i, 10)),
defaultFilePerm, defaultFilePerm,
@ -378,8 +377,8 @@ func (m *memoryController) parseStats(r io.Reader, stat *v1.MemoryStat) error {
func (m *memoryController) set(path string, settings []memorySettings) error { func (m *memoryController) set(path string, settings []memorySettings) error {
for _, t := range settings { for _, t := range settings {
if t.value != nil { if t.value != nil {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(m.Path(path), fmt.Sprintf("memory.%s", t.name)), filepath.Join(m.Path(path), "memory."+t.name),
[]byte(strconv.FormatInt(*t.value, 10)), []byte(strconv.FormatInt(*t.value, 10)),
defaultFilePerm, defaultFilePerm,
); err != nil { ); err != nil {
@ -468,7 +467,7 @@ func (m *memoryController) memoryEvent(path string, event MemoryEvent) (uintptr,
defer evtFile.Close() defer evtFile.Close()
data := fmt.Sprintf("%d %d %s", efd, evtFile.Fd(), event.Arg()) data := fmt.Sprintf("%d %d %s", efd, evtFile.Fd(), event.Arg())
evctlPath := filepath.Join(root, "cgroup.event_control") evctlPath := filepath.Join(root, "cgroup.event_control")
if err := ioutil.WriteFile(evctlPath, []byte(data), 0700); err != nil { if err := retryingWriteFile(evctlPath, []byte(data), 0700); err != nil {
unix.Close(efd) unix.Close(efd)
return 0, err return 0, err
} }

View File

@ -17,7 +17,6 @@
package cgroups package cgroups
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -48,7 +47,7 @@ func (n *netclsController) Create(path string, resources *specs.LinuxResources)
return err return err
} }
if resources.Network != nil && resources.Network.ClassID != nil && *resources.Network.ClassID > 0 { if resources.Network != nil && resources.Network.ClassID != nil && *resources.Network.ClassID > 0 {
return ioutil.WriteFile( return retryingWriteFile(
filepath.Join(n.Path(path), "net_cls.classid"), filepath.Join(n.Path(path), "net_cls.classid"),
[]byte(strconv.FormatUint(uint64(*resources.Network.ClassID), 10)), []byte(strconv.FormatUint(uint64(*resources.Network.ClassID), 10)),
defaultFilePerm, defaultFilePerm,

View File

@ -18,7 +18,6 @@ package cgroups
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -49,7 +48,7 @@ func (n *netprioController) Create(path string, resources *specs.LinuxResources)
} }
if resources.Network != nil { if resources.Network != nil {
for _, prio := range resources.Network.Priorities { for _, prio := range resources.Network.Priorities {
if err := ioutil.WriteFile( if err := retryingWriteFile(
filepath.Join(n.Path(path), "net_prio.ifpriomap"), filepath.Join(n.Path(path), "net_prio.ifpriomap"),
formatPrio(prio.Name, prio.Priority), formatPrio(prio.Name, prio.Priority),
defaultFilePerm, defaultFilePerm,

View File

@ -48,12 +48,12 @@ func newInitConfig() *InitConfig {
type InitCheck func(Subsystem, Path, error) error type InitCheck func(Subsystem, Path, error) error
// AllowAny allows any subsystem errors to be skipped // AllowAny allows any subsystem errors to be skipped
func AllowAny(s Subsystem, p Path, err error) error { func AllowAny(_ Subsystem, _ Path, _ error) error {
return ErrIgnoreSubsystem return ErrIgnoreSubsystem
} }
// RequireDevices requires the device subsystem but no others // RequireDevices requires the device subsystem but no others
func RequireDevices(s Subsystem, p Path, err error) error { func RequireDevices(s Subsystem, _ Path, _ error) error {
if s.Name() == Devices { if s.Name() == Devices {
return ErrDevicesRequired return ErrDevicesRequired
} }

View File

@ -25,7 +25,7 @@ import (
type Path func(subsystem Name) (string, error) type Path func(subsystem Name) (string, error)
func RootPath(subsysem Name) (string, error) { func RootPath(subsystem Name) (string, error) {
return "/", nil return "/", nil
} }
@ -63,7 +63,7 @@ var ErrControllerNotActive = errors.New("controller is not supported")
func existingPath(paths map[string]string, suffix string) Path { func existingPath(paths map[string]string, suffix string) Path {
// localize the paths based on the root mount dest for nested cgroups // localize the paths based on the root mount dest for nested cgroups
for n, p := range paths { for n, p := range paths {
dest, err := getCgroupDestination(string(n)) dest, err := getCgroupDestination(n)
if err != nil { if err != nil {
return errorPath(err) return errorPath(err)
} }
@ -79,7 +79,7 @@ func existingPath(paths map[string]string, suffix string) Path {
return func(name Name) (string, error) { return func(name Name) (string, error) {
root, ok := paths[string(name)] root, ok := paths[string(name)]
if !ok { if !ok {
if root, ok = paths[fmt.Sprintf("name=%s", name)]; !ok { if root, ok = paths["name="+string(name)]; !ok {
return "", ErrControllerNotActive return "", ErrControllerNotActive
} }
} }

View File

@ -50,7 +50,7 @@ func (p *pidsController) Create(path string, resources *specs.LinuxResources) er
return err return err
} }
if resources.Pids != nil && resources.Pids.Limit > 0 { if resources.Pids != nil && resources.Pids.Limit > 0 {
return ioutil.WriteFile( return retryingWriteFile(
filepath.Join(p.Path(path), "pids.max"), filepath.Join(p.Path(path), "pids.max"),
[]byte(strconv.FormatInt(resources.Pids.Limit, 10)), []byte(strconv.FormatInt(resources.Pids.Limit, 10)),
defaultFilePerm, defaultFilePerm,

View File

@ -67,7 +67,7 @@ func (p *rdmaController) Create(path string, resources *specs.LinuxResources) er
for device, limit := range resources.Rdma { for device, limit := range resources.Rdma {
if device != "" && (limit.HcaHandles != nil || limit.HcaObjects != nil) { if device != "" && (limit.HcaHandles != nil || limit.HcaObjects != nil) {
return ioutil.WriteFile( return retryingWriteFile(
filepath.Join(p.Path(path), "rdma.max"), filepath.Join(p.Path(path), "rdma.max"),
[]byte(createCmdString(device, &limit)), []byte(createCmdString(device, &limit)),
defaultFilePerm, defaultFilePerm,

View File

@ -17,7 +17,6 @@
package cgroups package cgroups
import ( import (
"fmt"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -78,7 +77,7 @@ func (s *SystemdController) Name() Name {
return SystemdDbus return SystemdDbus
} }
func (s *SystemdController) Create(path string, resources *specs.LinuxResources) error { func (s *SystemdController) Create(path string, _ *specs.LinuxResources) error {
conn, err := systemdDbus.New() conn, err := systemdDbus.New()
if err != nil { if err != nil {
return err return err
@ -105,7 +104,7 @@ func (s *SystemdController) Create(path string, resources *specs.LinuxResources)
} }
once.Do(checkDelegate) once.Do(checkDelegate)
properties := []systemdDbus.Property{ properties := []systemdDbus.Property{
systemdDbus.PropDescription(fmt.Sprintf("cgroup %s", name)), systemdDbus.PropDescription("cgroup " + name),
systemdDbus.PropWants(slice), systemdDbus.PropWants(slice),
newProperty("DefaultDependencies", false), newProperty("DefaultDependencies", false),
newProperty("MemoryAccounting", true), newProperty("MemoryAccounting", true),
@ -150,10 +149,6 @@ func newProperty(name string, units interface{}) systemdDbus.Property {
} }
} }
func unitName(name string) string {
return fmt.Sprintf("%s.slice", name)
}
func splitName(path string) (slice string, unit string) { func splitName(path string) (slice string, unit string) {
slice, unit = filepath.Split(path) slice, unit = filepath.Split(path)
return strings.TrimSuffix(slice, "/"), unit return strings.TrimSuffix(slice, "/"), unit

View File

@ -18,6 +18,7 @@ package cgroups
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -26,6 +27,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
units "github.com/docker/go-units" units "github.com/docker/go-units"
@ -121,7 +123,7 @@ func defaults(root string) ([]Subsystem, error) {
NewNetCls(root), NewNetCls(root),
NewNetPrio(root), NewNetPrio(root),
NewPerfEvent(root), NewPerfEvent(root),
NewCputset(root), NewCpuset(root),
NewCpu(root), NewCpu(root),
NewCpuacct(root), NewCpuacct(root),
NewMemory(root), NewMemory(root),
@ -380,5 +382,18 @@ func cleanPath(path string) string {
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path, _ = filepath.Rel(string(os.PathSeparator), filepath.Clean(string(os.PathSeparator)+path)) path, _ = filepath.Rel(string(os.PathSeparator), filepath.Clean(string(os.PathSeparator)+path))
} }
return filepath.Clean(path) return path
}
func retryingWriteFile(path string, data []byte, mode os.FileMode) error {
// Retry writes on EINTR; see:
// https://github.com/golang/go/issues/38033
for {
err := ioutil.WriteFile(path, data, mode)
if err == nil {
return nil
} else if !errors.Is(err, syscall.EINTR) {
return err
}
}
} }

View File

@ -44,7 +44,3 @@ func IgnoreNotExist(err error) error {
} }
return err return err
} }
func errPassthrough(err error) error {
return err
}

View File

@ -18,26 +18,22 @@ package v2
import ( import (
"bufio" "bufio"
"fmt"
"io/ioutil" "io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"syscall" "syscall"
"time" "time"
"golang.org/x/sys/unix"
"github.com/containerd/cgroups/v2/stats" "github.com/containerd/cgroups/v2/stats"
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/godbus/dbus/v5" "github.com/godbus/dbus/v5"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
) )
const ( const (
@ -49,13 +45,8 @@ const (
var ( var (
canDelegate bool canDelegate bool
once sync.Once
) )
type cgValuer interface {
Values() []Value
}
type Event struct { type Event struct {
Low uint64 Low uint64
High uint64 High uint64
@ -149,11 +140,21 @@ func (c *Value) write(path string, perm os.FileMode) error {
default: default:
return ErrInvalidFormat return ErrInvalidFormat
} }
return ioutil.WriteFile(
filepath.Join(path, c.filename), // Retry writes on EINTR; see:
data, // https://github.com/golang/go/issues/38033
perm, for {
) err := ioutil.WriteFile(
filepath.Join(path, c.filename),
data,
perm,
)
if err == nil {
return nil
} else if !errors.Is(err, syscall.EINTR) {
return err
}
}
} }
func writeValues(path string, values []Value) error { func writeValues(path string, values []Value) error {
@ -259,7 +260,7 @@ func (c *Manager) ToggleControllers(controllers []string, t ControllerToggle) er
// Note that /sys/fs/cgroup/foo/bar/baz/cgroup.subtree_control does not need to be written. // Note that /sys/fs/cgroup/foo/bar/baz/cgroup.subtree_control does not need to be written.
split := strings.Split(c.path, "/") split := strings.Split(c.path, "/")
var lastErr error var lastErr error
for i, _ := range split { for i := range split {
f := strings.Join(split[:i], "/") f := strings.Join(split[:i], "/")
if !strings.HasPrefix(f, c.unifiedMountpoint) || f == c.path { if !strings.HasPrefix(f, c.unifiedMountpoint) || f == c.path {
continue continue
@ -362,8 +363,7 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
for _, controller := range controllers { for _, controller := range controllers {
switch controller { switch controller {
case "cpu", "memory": case "cpu", "memory":
filename := fmt.Sprintf("%s.stat", controller) if err := readKVStatsFile(c.path, controller+".stat", out); err != nil {
if err := readKVStatsFile(c.path, filename, out); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
continue continue
} }
@ -670,7 +670,7 @@ func NewSystemd(slice, group string, pid int, resources *Resources) (*Manager, e
defer conn.Close() defer conn.Close()
properties := []systemdDbus.Property{ properties := []systemdDbus.Property{
systemdDbus.PropDescription(fmt.Sprintf("cgroup %s", group)), systemdDbus.PropDescription("cgroup " + group),
newSystemdProperty("DefaultDependencies", false), newSystemdProperty("DefaultDependencies", false),
newSystemdProperty("MemoryAccounting", true), newSystemdProperty("MemoryAccounting", true),
newSystemdProperty("CPUAccounting", true), newSystemdProperty("CPUAccounting", true),

View File

@ -29,7 +29,7 @@ func NestedGroupPath(suffix string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return filepath.Join(string(path), suffix), nil return filepath.Join(path, suffix), nil
} }
// PidGroupPath will return the correct cgroup paths for an existing process running inside a cgroup // PidGroupPath will return the correct cgroup paths for an existing process running inside a cgroup

View File

@ -28,9 +28,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/godbus/dbus/v5"
"github.com/containerd/cgroups/v2/stats" "github.com/containerd/cgroups/v2/stats"
"github.com/godbus/dbus/v5"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -106,14 +105,6 @@ func parseKV(raw string) (string, interface{}, error) {
} }
} }
func readUint(path string) (uint64, error) {
v, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
}
return parseUint(strings.TrimSpace(string(v)), 10, 64)
}
func parseUint(s string, base, bitSize int) (uint64, error) { func parseUint(s string, base, bitSize int) (uint64, error) {
v, err := strconv.ParseUint(s, base, bitSize) v, err := strconv.ParseUint(s, base, bitSize)
if err != nil { if err != nil {
@ -178,7 +169,7 @@ func ToResources(spec *specs.LinuxResources) *Resources {
Mems: cpu.Mems, Mems: cpu.Mems,
} }
if shares := cpu.Shares; shares != nil { if shares := cpu.Shares; shares != nil {
convertedWeight := (1 + ((*shares-2)*9999)/262142) convertedWeight := 1 + ((*shares-2)*9999)/262142
resources.CPU.Weight = &convertedWeight resources.CPU.Weight = &convertedWeight
} }
if period := cpu.Period; period != nil { if period := cpu.Period; period != nil {
@ -301,8 +292,8 @@ func readIoStats(path string) []*stats.IOEntry {
Major: major, Major: major,
Minor: minor, Minor: minor,
} }
for _, stats := range parts { for _, s := range parts {
keyPairValue := strings.Split(stats, "=") keyPairValue := strings.Split(s, "=")
if len(keyPairValue) != 2 { if len(keyPairValue) != 2 {
continue continue
} }