Skip to content

Commit

Permalink
Merge pull request #50 from jingxiaolu/master
Browse files Browse the repository at this point in the history
Add interface AddTask to control groups.
  • Loading branch information
estesp committed May 15, 2018
2 parents 07683a6 + 0d1587c commit 5e61083
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
30 changes: 30 additions & 0 deletions cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,36 @@ func (c *cgroup) add(process Process) error {
return nil
}

// AddTask moves the provided tasks (threads) into the new cgroup
func (c *cgroup) AddTask(process Process) error {
if process.Pid <= 0 {
return ErrInvalidPid
}
c.mu.Lock()
defer c.mu.Unlock()
if c.err != nil {
return c.err
}
return c.addTask(process)
}

func (c *cgroup) addTask(process Process) error {
for _, s := range pathers(c.subsystems) {
p, err := c.path(s.Name())
if err != nil {
return err
}
if err := ioutil.WriteFile(
filepath.Join(s.Path(p), cgroupTasks),
[]byte(strconv.Itoa(process.Pid)),
defaultFilePerm,
); err != nil {
return err
}
}
return nil
}

// Delete will remove the control group from each of the subsystems registered
func (c *cgroup) Delete() error {
c.mu.Lock()
Expand Down
48 changes: 48 additions & 0 deletions cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ func TestAdd(t *testing.T) {
}
}

func TestAddTask(t *testing.T) {
mock, err := newMock()
if err != nil {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
if err != nil {
t.Error(err)
return
}
if err := control.AddTask(Process{Pid: 1234}); err != nil {
t.Error(err)
return
}
for _, s := range Subsystems() {
if err := checkTaskid(mock, filepath.Join(string(s), "test"), 1234); err != nil {
t.Error(err)
return
}
}
}

func TestListPids(t *testing.T) {
mock, err := newMock()
if err != nil {
Expand Down Expand Up @@ -159,6 +182,21 @@ func checkPid(mock *mockCgroup, path string, expected int) error {
return nil
}

func checkTaskid(mock *mockCgroup, path string, expected int) error {
data, err := readValue(mock, filepath.Join(path, cgroupTasks))
if err != nil {
return err
}
v, err := strconv.Atoi(string(data))
if err != nil {
return err
}
if v != expected {
return fmt.Errorf("expectd task id %d but received %d", expected, v)
}
return nil
}

func TestLoad(t *testing.T) {
mock, err := newMock()
if err != nil {
Expand Down Expand Up @@ -222,6 +260,16 @@ func TestCreateSubCgroup(t *testing.T) {
return
}
}
if err := sub.AddTask(Process{Pid: 5678}); err != nil {
t.Error(err)
return
}
for _, s := range Subsystems() {
if err := checkTaskid(mock, filepath.Join(string(s), "test", "child"), 5678); err != nil {
t.Error(err)
return
}
}
}

func TestFreezeThaw(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion control.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

const (
cgroupProcs = "cgroup.procs"
cgroupTasks = "tasks"
defaultDirPerm = 0755
)

Expand All @@ -48,8 +49,10 @@ type Process struct {
type Cgroup interface {
// New creates a new cgroup under the calling cgroup
New(string, *specs.LinuxResources) (Cgroup, error)
// Add adds a process to the cgroup
// Add adds a process to the cgroup (cgroup.procs)
Add(Process) error
// AddTask adds a process to the cgroup (tasks)
AddTask(Process) error
// Delete removes the cgroup as a whole
Delete() error
// MoveTo moves all the processes under the calling cgroup to the provided one
Expand Down

0 comments on commit 5e61083

Please sign in to comment.