Skip to content

Commit

Permalink
update to *uint64 by following the spec
Browse files Browse the repository at this point in the history
Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
  • Loading branch information
chanwit committed Dec 19, 2015
1 parent bc46574 commit b2d3d2a
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 68 deletions.
6 changes: 3 additions & 3 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (m *Manager) Apply(pid int) (err error) {
m.Paths = paths

if paths["cpu"] != "" {
if err := CheckCpushares(paths["cpu"], c.Resources.CpuShares); err != nil {
if err := CheckCpushares(paths["cpu"], *c.Resources.CpuShares); err != nil {
return err
}
}
Expand Down Expand Up @@ -336,8 +336,8 @@ func removePath(p string, err error) error {
return nil
}

func CheckCpushares(path string, c int64) error {
var cpuShares int64
func CheckCpushares(path string, c uint64) error {
var cpuShares uint64

if c == 0 {
return nil
Expand Down
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func (s *BlkioGroup) Apply(d *cgroupData) error {
}

func (s *BlkioGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.BlkioWeight != 0 {
if err := writeFile(path, "blkio.weight", strconv.FormatUint(uint64(cgroup.Resources.BlkioWeight), 10)); err != nil {
if *cgroup.Resources.BlkioWeight != 0 {
if err := writeFile(path, "blkio.weight", strconv.FormatUint(uint64(*cgroup.Resources.BlkioWeight), 10)); err != nil {
return err
}
}

if cgroup.Resources.BlkioLeafWeight != 0 {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(cgroup.Resources.BlkioLeafWeight), 10)); err != nil {
if *cgroup.Resources.BlkioLeafWeight != 0 {
if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(*cgroup.Resources.BlkioLeafWeight), 10)); err != nil {
return err
}
}
Expand Down
20 changes: 10 additions & 10 deletions libcontainer/cgroups/fs/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ func (s *CpuGroup) Apply(d *cgroupData) error {
}

func (s *CpuGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.CpuShares != 0 {
if err := writeFile(path, "cpu.shares", strconv.FormatInt(cgroup.Resources.CpuShares, 10)); err != nil {
if *cgroup.Resources.CpuShares != 0 {
if err := writeFile(path, "cpu.shares", strconv.FormatUint(*cgroup.Resources.CpuShares, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuPeriod != 0 {
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(cgroup.Resources.CpuPeriod, 10)); err != nil {
if *cgroup.Resources.CpuPeriod != 0 {
if err := writeFile(path, "cpu.cfs_period_us", strconv.FormatUint(*cgroup.Resources.CpuPeriod, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuQuota != 0 {
if err := writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(cgroup.Resources.CpuQuota, 10)); err != nil {
if *cgroup.Resources.CpuQuota != 0 {
if err := writeFile(path, "cpu.cfs_quota_us", strconv.FormatUint(*cgroup.Resources.CpuQuota, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuRtPeriod != 0 {
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatInt(cgroup.Resources.CpuRtPeriod, 10)); err != nil {
if *cgroup.Resources.CpuRtPeriod != 0 {
if err := writeFile(path, "cpu.rt_period_us", strconv.FormatUint(*cgroup.Resources.CpuRtPeriod, 10)); err != nil {
return err
}
}
if cgroup.Resources.CpuRtRuntime != 0 {
if err := writeFile(path, "cpu.rt_runtime_us", strconv.FormatInt(cgroup.Resources.CpuRtRuntime, 10)); err != nil {
if *cgroup.Resources.CpuRtRuntime != 0 {
if err := writeFile(path, "cpu.rt_runtime_us", strconv.FormatUint(*cgroup.Resources.CpuRtRuntime, 10)); err != nil {
return err
}
}
Expand Down
8 changes: 4 additions & 4 deletions libcontainer/cgroups/fs/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func (s *CpusetGroup) Apply(d *cgroupData) error {
}

func (s *CpusetGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.CpusetCpus != "" {
if err := writeFile(path, "cpuset.cpus", cgroup.Resources.CpusetCpus); err != nil {
if *cgroup.Resources.CpusetCpus != "" {
if err := writeFile(path, "cpuset.cpus", *cgroup.Resources.CpusetCpus); err != nil {
return err
}
}
if cgroup.Resources.CpusetMems != "" {
if err := writeFile(path, "cpuset.mems", cgroup.Resources.CpusetMems); err != nil {
if *cgroup.Resources.CpusetMems != "" {
if err := writeFile(path, "cpuset.mems", *cgroup.Resources.CpusetMems); err != nil {
return err
}
}
Expand Down
37 changes: 17 additions & 20 deletions libcontainer/cgroups/fs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,38 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
}

func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.Memory != 0 {
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
if *cgroup.Resources.Memory != 0 {
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatUint(*cgroup.Resources.Memory, 10)); err != nil {
return err
}
}
if cgroup.Resources.MemoryReservation != 0 {
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
if *cgroup.Resources.MemoryReservation != 0 {
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(*cgroup.Resources.MemoryReservation, 10)); err != nil {
return err
}
}
if cgroup.Resources.MemorySwap > 0 {
if err := writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
if *cgroup.Resources.MemorySwap > 0 {
if err := writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatUint(*cgroup.Resources.MemorySwap, 10)); err != nil {
return err
}
}
if cgroup.Resources.KernelMemory > 0 {
if err := writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemory, 10)); err != nil {
if *cgroup.Resources.KernelMemory > 0 {
if err := writeFile(path, "memory.kmem.limit_in_bytes", strconv.FormatUint(*cgroup.Resources.KernelMemory, 10)); err != nil {
return err
}
}

if cgroup.Resources.OomKillDisable {
if *cgroup.Resources.OomKillDisable {
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
return err
}
}
if cgroup.Resources.MemorySwappiness >= 0 && cgroup.Resources.MemorySwappiness <= 100 {
if err := writeFile(path, "memory.swappiness", strconv.FormatInt(cgroup.Resources.MemorySwappiness, 10)); err != nil {
if *cgroup.Resources.MemorySwappiness >= 0 && *cgroup.Resources.MemorySwappiness <= 100 {
if err := writeFile(path, "memory.swappiness", strconv.FormatUint(*cgroup.Resources.MemorySwappiness, 10)); err != nil {
return err
}
} else if cgroup.Resources.MemorySwappiness == -1 {
return nil
} else {
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", cgroup.Resources.MemorySwappiness)
return fmt.Errorf("invalid value:%d. valid memory swappiness range is 0-100", *cgroup.Resources.MemorySwappiness)
}

return nil
Expand Down Expand Up @@ -138,12 +136,11 @@ func (s *MemoryGroup) GetStats(path string, stats *cgroups.Stats) error {
}

func memoryAssigned(cgroup *configs.Cgroup) bool {
return cgroup.Resources.Memory != 0 ||
cgroup.Resources.MemoryReservation != 0 ||
cgroup.Resources.MemorySwap > 0 ||
cgroup.Resources.KernelMemory > 0 ||
cgroup.Resources.OomKillDisable ||
cgroup.Resources.MemorySwappiness != -1
return *cgroup.Resources.Memory != 0 ||
*cgroup.Resources.MemoryReservation != 0 ||
*cgroup.Resources.MemorySwap > 0 ||
*cgroup.Resources.KernelMemory > 0 ||
*cgroup.Resources.OomKillDisable
}

func getMemoryData(path, name string) (cgroups.MemoryData, error) {
Expand Down
6 changes: 3 additions & 3 deletions libcontainer/cgroups/fs/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func (s *PidsGroup) Apply(d *cgroupData) error {
}

func (s *PidsGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.PidsLimit != 0 {
if *cgroup.Resources.PidsLimit != 0 {
// "max" is the fallback value.
limit := "max"

if cgroup.Resources.PidsLimit > 0 {
limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10)
if *cgroup.Resources.PidsLimit > 0 {
limit = strconv.FormatUint(*cgroup.Resources.PidsLimit, 10)
}

if err := writeFile(path, "pids.max", limit); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions libcontainer/cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,26 @@ func (m *Manager) Apply(pid int) error {
newProp("DefaultDependencies", false))
}

if c.Resources.Memory != 0 {
if *c.Resources.Memory != 0 {
properties = append(properties,
newProp("MemoryLimit", uint64(c.Resources.Memory)))
newProp("MemoryLimit", *c.Resources.Memory))
}

if c.Resources.CpuShares != 0 {
if *c.Resources.CpuShares != 0 {
properties = append(properties,
newProp("CPUShares", uint64(c.Resources.CpuShares)))
newProp("CPUShares", *c.Resources.CpuShares))
}

if c.Resources.BlkioWeight != 0 {
if *c.Resources.BlkioWeight != 0 {
properties = append(properties,
newProp("BlockIOWeight", uint64(c.Resources.BlkioWeight)))
newProp("BlockIOWeight", *c.Resources.BlkioWeight))
}

// We need to set kernel memory before processes join cgroup because
// kmem.limit_in_bytes can only be set when the cgroup is empty.
// And swap memory limit needs to be set after memory limit, only
// memory limit is handled by systemd, so it's kind of ugly here.
if c.Resources.KernelMemory > 0 {
if *c.Resources.KernelMemory > 0 {
if err := setKernelMemory(c); err != nil {
return err
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func (m *Manager) Apply(pid int) error {
m.Paths = paths

if paths["cpu"] != "" {
if err := fs.CheckCpushares(paths["cpu"], c.Resources.CpuShares); err != nil {
if err := fs.CheckCpushares(paths["cpu"], *c.Resources.CpuShares); err != nil {
return err
}
}
Expand Down
32 changes: 16 additions & 16 deletions libcontainer/configs/cgroup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,46 @@ type Resources struct {
DeniedDevices []*Device `json:"denied_devices"`

// Memory limit (in bytes)
Memory int64 `json:"memory"`
Memory *uint64 `json:"memory"`

// Memory reservation or soft_limit (in bytes)
MemoryReservation int64 `json:"memory_reservation"`
MemoryReservation *uint64 `json:"memory_reservation"`

// Total memory usage (memory + swap); set `-1' to disable swap
MemorySwap int64 `json:"memory_swap"`
MemorySwap *uint64 `json:"memory_swap"`

// Kernel memory limit (in bytes)
KernelMemory int64 `json:"kernel_memory"`
KernelMemory *uint64 `json:"kernel_memory"`

// CPU shares (relative weight vs. other containers)
CpuShares int64 `json:"cpu_shares"`
CpuShares *uint64 `json:"cpu_shares"`

// CPU hardcap limit (in usecs). Allowed cpu time in a given period.
CpuQuota int64 `json:"cpu_quota"`
CpuQuota *uint64 `json:"cpu_quota"`

// CPU period to be used for hardcapping (in usecs). 0 to use system default.
CpuPeriod int64 `json:"cpu_period"`
CpuPeriod *uint64 `json:"cpu_period"`

// How many time CPU will use in realtime scheduling (in usecs).
CpuRtRuntime int64 `json:"cpu_quota"`
CpuRtRuntime *uint64 `json:"cpu_quota"`

// CPU period to be used for realtime scheduling (in usecs).
CpuRtPeriod int64 `json:"cpu_period"`
CpuRtPeriod *uint64 `json:"cpu_period"`

// CPU to use
CpusetCpus string `json:"cpuset_cpus"`
CpusetCpus *string `json:"cpuset_cpus"`

// MEM to use
CpusetMems string `json:"cpuset_mems"`
CpusetMems *string `json:"cpuset_mems"`

// Process limit; set <= `0' to disable limit.
PidsLimit int64 `json:"pids_limit"`
PidsLimit *uint64 `json:"pids_limit"`

// Specifies per cgroup weight, range is from 10 to 1000.
BlkioWeight uint16 `json:"blkio_weight"`
BlkioWeight *uint16 `json:"blkio_weight"`

// Specifies tasks' weight in the given cgroup while competing with the cgroup's child cgroups, range is from 10 to 1000, cfq scheduler only
BlkioLeafWeight uint16 `json:"blkio_leaf_weight"`
BlkioLeafWeight *uint16 `json:"blkio_leaf_weight"`

// Weight per cgroup per device, can override BlkioWeight.
BlkioWeightDevice []*WeightDevice `json:"blkio_weight_device"`
Expand All @@ -95,10 +95,10 @@ type Resources struct {
HugetlbLimit []*HugepageLimit `json:"hugetlb_limit"`

// Whether to disable OOM Killer
OomKillDisable bool `json:"oom_kill_disable"`
OomKillDisable *bool `json:"oom_kill_disable"`

// Tuning swappiness behaviour per cgroup
MemorySwappiness int64 `json:"memory_swappiness"`
MemorySwappiness *uint64 `json:"memory_swappiness"`

// Set priority of network traffic for container
NetPrioIfpriomap []*IfPrioMap `json:"net_prio_ifpriomap"`
Expand Down

0 comments on commit b2d3d2a

Please sign in to comment.