Skip to content

Commit

Permalink
Merge pull request #689 from ningmingxiao/dev16
Browse files Browse the repository at this point in the history
add  nerdctl run  cpuset-mems,cpu-quota,cpu-period support
  • Loading branch information
AkihiroSuda authored Jan 11, 2022
2 parents 2b084fd + 57132f2 commit 4f91839
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,11 @@ Network flags:

Cgroup flags:
- :whale: `--cpus`: Number of CPUs
- :whale: `--cpu-quota`: Limit the CPU CFS (Completely Fair Scheduler) quota
- :whale: `--cpu-period`: Limit the CPU CFS (Completely Fair Scheduler) period
- :whale: `--cpu-shares`: CPU shares (relative weight)
- :whale: `--cpuset-cpus`: CPUs in which to allow execution (0-3, 0,1)
- :whale: `--cpuset-mems`: Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems
- :whale: `--memory`: Memory limit
- :whale: `--pids-limit`: Tune container pids limit
- :nerd_face: `--cgroup-conf`: Configure cgroup v2 (key=value)
Expand Down
3 changes: 3 additions & 0 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ func setCreateFlags(cmd *cobra.Command) {
return []string{"host", "private"}, cobra.ShellCompDirectiveNoFileComp
})
cmd.Flags().String("cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)")
cmd.Flags().String("cpuset-mems", "", "MEMs in which to allow execution (0-3, 0,1)")
cmd.Flags().Int("cpu-shares", 0, "CPU shares (relative weight)")
cmd.Flags().Int64("cpu-quota", -1, "Limit CPU CFS (Completely Fair Scheduler) quota")
cmd.Flags().Uint64("cpu-period", 0, "Limit CPU CFS (Completely Fair Scheduler) period")
// device is defined as StringSlice, not StringArray, to allow specifying "--device=DEV1,DEV2" (compatible with Podman)
cmd.Flags().StringSlice("device", nil, "Add a host device to the container")
// ulimit is defined as StringSlice, not StringArray, to allow specifying "--ulimit=ULIMIT1,ULIMIT2" (compatible with Podman)
Expand Down
22 changes: 21 additions & 1 deletion cmd/nerdctl/run_cgroup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,27 @@ func generateCgroupOpts(cmd *cobra.Command, id string) ([]oci.SpecOpts, error) {
if cpuset != "" {
opts = append(opts, oci.WithCPUs(cpuset))
}

cpuQuota, err := cmd.Flags().GetInt64("cpu-quota")
if err != nil {
return nil, err
}
cpuPeriod, err := cmd.Flags().GetUint64("cpu-period")
if err != nil {
return nil, err
}
if cpuQuota != -1 || cpuPeriod != 0 {
if cpus > 0.0 {
return nil, errors.New("cpus and quota/period should be used separately")
}
opts = append(opts, oci.WithCPUCFS(cpuQuota, cpuPeriod))
}
cpusetMems, err := cmd.Flags().GetString("cpuset-mems")
if err != nil {
return nil, err
}
if cpusetMems != "" {
opts = append(opts, oci.WithCPUsMems(cpusetMems))
}
if memStr != "" {
mem64, err := units.RAMInBytes(memStr)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions cmd/nerdctl/run_cgroup_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ func TestRunCgroupV2(t *testing.T) {
42
77
0-1
0
`
//In CgroupV2 CPUWeight replace CPUShares => weight := 1 + ((shares-2)*9999)/262142
base.Cmd("run", "--rm", "--cpus", "0.42", "--memory", "42m", "--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1", "-w", "/sys/fs/cgroup", testutil.AlpineImage,
"cat", "cpu.max", "memory.max", "pids.max", "cpu.weight", "cpuset.cpus").AssertOutExactly(expected)
base.Cmd("run", "--rm", "--cpus", "0.42", "--cpuset-mems", "0", "--memory", "42m", "--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1", "-w", "/sys/fs/cgroup", testutil.AlpineImage,
"cat", "cpu.max", "memory.max", "pids.max", "cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected)
base.Cmd("run", "--rm", "--cpu-quota", "42000", "--cpuset-mems", "0", "--cpu-period", "100000", "--memory", "42m", "--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1", "-w", "/sys/fs/cgroup", testutil.AlpineImage,
"cat", "cpu.max", "memory.max", "pids.max", "cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected)
}

func TestRunDevice(t *testing.T) {
Expand Down

0 comments on commit 4f91839

Please sign in to comment.