Move raw cgroups into fs package (filesystem)

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-04-18 21:34:26 -07:00
parent 9f508e4b3e
commit 8cc8aca9cc
3 changed files with 26 additions and 24 deletions

View file

@ -1,10 +1,13 @@
package cgroups package fs
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/dotcloud/docker/pkg/cgroups"
) )
type rawCgroup struct { type rawCgroup struct {
@ -12,7 +15,7 @@ type rawCgroup struct {
cgroup string cgroup string
} }
func rawApply(c *Cgroup, pid int) (ActiveCgroup, error) { func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
// We have two implementation of cgroups support, one is based on // We have two implementation of cgroups support, one is based on
// systemd and the dbus api, and one is based on raw cgroup fs operations // systemd and the dbus api, and one is based on raw cgroup fs operations
// following the pre-single-writer model docs at: // following the pre-single-writer model docs at:
@ -20,7 +23,7 @@ func rawApply(c *Cgroup, pid int) (ActiveCgroup, error) {
// //
// we can pick any subsystem to find the root // we can pick any subsystem to find the root
cgroupRoot, err := FindCgroupMountpoint("cpu") cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -39,7 +42,7 @@ func rawApply(c *Cgroup, pid int) (ActiveCgroup, error) {
root: cgroupRoot, root: cgroupRoot,
cgroup: cgroup, cgroup: cgroup,
} }
for _, g := range []func(*Cgroup, int) error{ for _, g := range []func(*cgroups.Cgroup, int) error{
raw.setupDevices, raw.setupDevices,
raw.setupMemory, raw.setupMemory,
raw.setupCpu, raw.setupCpu,
@ -58,7 +61,7 @@ func rawApply(c *Cgroup, pid int) (ActiveCgroup, error) {
} }
func (raw *rawCgroup) path(subsystem string) (string, error) { func (raw *rawCgroup) path(subsystem string) (string, error) {
initPath, err := GetInitCgroupDir(subsystem) initPath, err := cgroups.GetInitCgroupDir(subsystem)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -79,7 +82,7 @@ func (raw *rawCgroup) join(subsystem string, pid int) (string, error) {
return path, nil return path, nil
} }
func (raw *rawCgroup) setupDevices(c *Cgroup, pid int) (err error) { func (raw *rawCgroup) setupDevices(c *cgroups.Cgroup, pid int) (err error) {
dir, err := raw.join("devices", pid) dir, err := raw.join("devices", pid)
if err != nil { if err != nil {
return err return err
@ -133,7 +136,7 @@ func (raw *rawCgroup) setupDevices(c *Cgroup, pid int) (err error) {
return nil return nil
} }
func (raw *rawCgroup) setupMemory(c *Cgroup, pid int) (err error) { func (raw *rawCgroup) setupMemory(c *cgroups.Cgroup, pid int) (err error) {
dir, err := raw.join("memory", pid) dir, err := raw.join("memory", pid)
// only return an error for memory if it was not specified // only return an error for memory if it was not specified
if err != nil && (c.Memory != 0 || c.MemorySwap != 0) { if err != nil && (c.Memory != 0 || c.MemorySwap != 0) {
@ -165,7 +168,7 @@ func (raw *rawCgroup) setupMemory(c *Cgroup, pid int) (err error) {
return nil return nil
} }
func (raw *rawCgroup) setupCpu(c *Cgroup, pid int) (err error) { func (raw *rawCgroup) setupCpu(c *cgroups.Cgroup, pid int) (err error) {
// We always want to join the cpu group, to allow fair cpu scheduling // We always want to join the cpu group, to allow fair cpu scheduling
// on a container basis // on a container basis
dir, err := raw.join("cpu", pid) dir, err := raw.join("cpu", pid)
@ -180,7 +183,7 @@ func (raw *rawCgroup) setupCpu(c *Cgroup, pid int) (err error) {
return nil return nil
} }
func (raw *rawCgroup) setupCpuset(c *Cgroup, pid int) (err error) { func (raw *rawCgroup) setupCpuset(c *cgroups.Cgroup, pid int) (err error) {
// we don't want to join this cgroup unless it is specified // we don't want to join this cgroup unless it is specified
if c.CpusetCpus != "" { if c.CpusetCpus != "" {
dir, err := raw.join("cpuset", pid) dir, err := raw.join("cpuset", pid)
@ -200,33 +203,33 @@ func (raw *rawCgroup) setupCpuset(c *Cgroup, pid int) (err error) {
return nil return nil
} }
func (raw *rawCgroup) setupCpuacct(c *Cgroup, pid int) error { func (raw *rawCgroup) setupCpuacct(c *cgroups.Cgroup, pid int) error {
// we just want to join this group even though we don't set anything // we just want to join this group even though we don't set anything
if _, err := raw.join("cpuacct", pid); err != nil && err != ErrNotFound { if _, err := raw.join("cpuacct", pid); err != nil && err != cgroups.ErrNotFound {
return err return err
} }
return nil return nil
} }
func (raw *rawCgroup) setupBlkio(c *Cgroup, pid int) error { func (raw *rawCgroup) setupBlkio(c *cgroups.Cgroup, pid int) error {
// we just want to join this group even though we don't set anything // we just want to join this group even though we don't set anything
if _, err := raw.join("blkio", pid); err != nil && err != ErrNotFound { if _, err := raw.join("blkio", pid); err != nil && err != cgroups.ErrNotFound {
return err return err
} }
return nil return nil
} }
func (raw *rawCgroup) setupPerfevent(c *Cgroup, pid int) error { func (raw *rawCgroup) setupPerfevent(c *cgroups.Cgroup, pid int) error {
// we just want to join this group even though we don't set anything // we just want to join this group even though we don't set anything
if _, err := raw.join("perf_event", pid); err != nil && err != ErrNotFound { if _, err := raw.join("perf_event", pid); err != nil && err != cgroups.ErrNotFound {
return err return err
} }
return nil return nil
} }
func (raw *rawCgroup) setupFreezer(c *Cgroup, pid int) error { func (raw *rawCgroup) setupFreezer(c *cgroups.Cgroup, pid int) error {
// we just want to join this group even though we don't set anything // we just want to join this group even though we don't set anything
if _, err := raw.join("freezer", pid); err != nil && err != ErrNotFound { if _, err := raw.join("freezer", pid); err != nil && err != cgroups.ErrNotFound {
return err return err
} }
return nil return nil
@ -254,3 +257,7 @@ func (raw *rawCgroup) Cleanup() error {
} }
return nil return nil
} }
func writeFile(dir, file, data string) error {
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
}

View file

@ -3,9 +3,7 @@ package cgroups
import ( import (
"bufio" "bufio"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/dotcloud/docker/pkg/mount" "github.com/dotcloud/docker/pkg/mount"
@ -67,7 +65,3 @@ func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
} }
return "", ErrNotFound return "", ErrNotFound
} }
func writeFile(dir, file, data string) error {
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
}

View file

@ -8,6 +8,7 @@ import (
"syscall" "syscall"
"github.com/dotcloud/docker/pkg/cgroups" "github.com/dotcloud/docker/pkg/cgroups"
"github.com/dotcloud/docker/pkg/cgroups/fs"
"github.com/dotcloud/docker/pkg/cgroups/systemd" "github.com/dotcloud/docker/pkg/cgroups/systemd"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/network" "github.com/dotcloud/docker/pkg/libcontainer/network"
@ -104,7 +105,7 @@ func (ns *linuxNs) SetupCgroups(container *libcontainer.Container, nspid int) (c
if systemd.UseSystemd() { if systemd.UseSystemd() {
return systemd.Apply(c, nspid) return systemd.Apply(c, nspid)
} }
return rawApply(c, nspid) return fs.Apply(c, nspid)
} }
return nil, nil return nil, nil
} }