forked from apptainer/singularity
/
cgroups.go
154 lines (129 loc) · 3.34 KB
/
cgroups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cgroups
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"github.com/containerd/cgroups"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
// Manager manage container cgroup resources restriction
type Manager struct {
Path string
Pid int
cgroup cgroups.Cgroup
}
func readSpecFromFile(path string) (spec specs.LinuxResources, err error) {
conf, err := LoadConfig(path)
if err != nil {
return
}
// convert TOML structures to OCI JSON structures
data, err := json.Marshal(conf)
if err != nil {
return
}
if err = json.Unmarshal(data, &spec); err != nil {
return
}
return
}
// GetCgroupRootPath returns cgroup root path
func (m *Manager) GetCgroupRootPath() string {
if m.cgroup == nil {
return ""
}
for _, sub := range m.cgroup.Subsystems() {
processes, err := m.cgroup.Processes(sub.Name(), false)
if len(processes) == 0 || err != nil {
continue
}
process := processes[0]
cgroupPath := strings.Split(process.Path, string(sub.Name()))[0]
return filepath.Clean(cgroupPath)
}
return ""
}
// ApplyFromSpec applies cgroups ressources restriction from OCI specification
func (m *Manager) ApplyFromSpec(spec *specs.LinuxResources) (err error) {
var path cgroups.Path
if !filepath.IsAbs(m.Path) {
return fmt.Errorf("cgroup path must be an absolute path")
}
path = cgroups.StaticPath(m.Path)
s := spec
if s == nil {
s = &specs.LinuxResources{}
}
// creates cgroup
m.cgroup, err = cgroups.New(cgroups.V1, path, s)
if err != nil {
return err
}
if err := m.cgroup.Add(cgroups.Process{Pid: m.Pid}); err != nil {
return err
}
return
}
// ApplyFromFile applies cgroups resources restriction from TOML configuration
// file
func (m *Manager) ApplyFromFile(path string) error {
spec, err := readSpecFromFile(path)
if err != nil {
return err
}
return m.ApplyFromSpec(&spec)
}
func (m *Manager) loadFromPid() (err error) {
if m.Pid == 0 {
return fmt.Errorf("no process ID specified")
}
path := cgroups.PidPath(m.Pid)
m.cgroup, err = cgroups.Load(cgroups.V1, path)
return
}
// UpdateFromSpec updates cgroups resources restriction from OCI specification
func (m *Manager) UpdateFromSpec(spec *specs.LinuxResources) (err error) {
if m.cgroup == nil {
if err = m.loadFromPid(); err != nil {
return
}
}
err = m.cgroup.Update(spec)
return
}
// UpdateFromFile updates cgroups resources restriction from TOML configuration
func (m *Manager) UpdateFromFile(path string) error {
spec, err := readSpecFromFile(path)
if err != nil {
return err
}
return m.UpdateFromSpec(&spec)
}
// Remove removes ressources restriction for current managed process
func (m *Manager) Remove() error {
// deletes subgroup
return m.cgroup.Delete()
}
// Pause suspends all processes inside the container
func (m *Manager) Pause() error {
if m.cgroup == nil {
if err := m.loadFromPid(); err != nil {
return err
}
}
return m.cgroup.Freeze()
}
// Resume resumes all processes that have been previously paused
func (m *Manager) Resume() error {
if m.cgroup == nil {
if err := m.loadFromPid(); err != nil {
return err
}
}
return m.cgroup.Thaw()
}