Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 29363e2

Browse files
committed
Merge pull request #143 from vishh/enter_cgroup
Enter cgroups as part of NsEnter
2 parents 6940d0e + 86ba98b commit 29363e2

File tree

10 files changed

+200
-215
lines changed

10 files changed

+200
-215
lines changed

cgroups/cgroups.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ type Cgroup struct {
3737

3838
type ActiveCgroup interface {
3939
Cleanup() error
40+
Paths() (map[string]string, error)
4041
}

cgroups/fs/apply_raw.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ var (
2121
"perf_event": &PerfEventGroup{},
2222
"freezer": &FreezerGroup{},
2323
}
24+
CgroupProcesses = "cgroup.procs"
2425
)
2526

2627
type subsystem interface {
27-
Set(*data) error
28+
// Returns the stats, as 'stats', corresponding to the cgroup under 'path'.
29+
GetStats(path string, stats *cgroups.Stats) error
30+
// Removes the cgroup represented by 'data'.
2831
Remove(*data) error
29-
GetStats(string, *cgroups.Stats) error
32+
// Creates and joins the cgroup represented by data.
33+
Set(*data) error
3034
}
3135

3236
type data struct {
@@ -149,6 +153,18 @@ func (raw *data) parent(subsystem string) (string, error) {
149153
return filepath.Join(raw.root, subsystem, initPath), nil
150154
}
151155

156+
func (raw *data) Paths() (map[string]string, error) {
157+
paths := make(map[string]string)
158+
for sysname := range subsystems {
159+
path, err := raw.path(sysname)
160+
if err != nil {
161+
return nil, err
162+
}
163+
paths[sysname] = path
164+
}
165+
return paths, nil
166+
}
167+
152168
func (raw *data) path(subsystem string) (string, error) {
153169
// If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
154170
if filepath.IsAbs(raw.cgroup) {
@@ -169,7 +185,7 @@ func (raw *data) join(subsystem string) (string, error) {
169185
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
170186
return "", err
171187
}
172-
if err := writeFile(path, "cgroup.procs", strconv.Itoa(raw.pid)); err != nil {
188+
if err := writeFile(path, CgroupProcesses, strconv.Itoa(raw.pid)); err != nil {
173189
return "", err
174190
}
175191
return path, nil

cgroups/fs/cpuset.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,10 @@ func (s *CpusetGroup) Set(d *data) error {
2020
if err != nil {
2121
return err
2222
}
23-
if err := s.ensureParent(dir); err != nil {
24-
return err
25-
}
2623

27-
// because we are not using d.join we need to place the pid into the procs file
28-
// unlike the other subsystems
29-
if err := writeFile(dir, "cgroup.procs", strconv.Itoa(d.pid)); err != nil {
30-
return err
31-
}
32-
if err := writeFile(dir, "cpuset.cpus", d.c.CpusetCpus); err != nil {
33-
return err
34-
}
24+
return s.SetDir(dir, d.c.CpusetCpus, d.pid)
3525
}
26+
3627
return nil
3728
}
3829

@@ -44,6 +35,24 @@ func (s *CpusetGroup) GetStats(path string, stats *cgroups.Stats) error {
4435
return nil
4536
}
4637

38+
func (s *CpusetGroup) SetDir(dir, value string, pid int) error {
39+
if err := s.ensureParent(dir); err != nil {
40+
return err
41+
}
42+
43+
// because we are not using d.join we need to place the pid into the procs file
44+
// unlike the other subsystems
45+
if err := writeFile(dir, "cgroup.procs", strconv.Itoa(pid)); err != nil {
46+
return err
47+
}
48+
49+
if err := writeFile(dir, "cpuset.cpus", value); err != nil {
50+
return err
51+
}
52+
53+
return nil
54+
}
55+
4756
func (s *CpusetGroup) getSubsystemSettings(parent string) (cpus []byte, mems []byte, err error) {
4857
if cpus, err = ioutil.ReadFile(filepath.Join(parent, "cpuset.cpus")); err != nil {
4958
return

cgroups/fs/memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type MemoryGroup struct {
1414

1515
func (s *MemoryGroup) Set(d *data) error {
1616
dir, err := d.join("memory")
17-
// only return an error for memory if it was not specified
17+
// only return an error for memory if it was specified
1818
if err != nil && (d.c.Memory != 0 || d.c.MemoryReservation != 0 || d.c.MemorySwap != 0) {
1919
return err
2020
}

0 commit comments

Comments
 (0)