Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions cmd/cgroups-playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package main

import (
"os"

"github.com/containerd/cgroups/v2"
stats2 "github.com/containerd/cgroups/v2/stats"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"os"
)

func main() {
Expand Down Expand Up @@ -54,5 +55,29 @@ func xmain() error {
for i, s := range subsystems {
logrus.Infof("Subsystem %d: %q", i, s.Name())
}

cpuCgroup, err := v2.NewCpu(unifiedMountpoint)
if err != nil {
return err
}
var period, shares uint64 = 1000, 5000
resources := specs.LinuxResources{
CPU: &specs.LinuxCPU{Period: &period, Shares: &shares},
}
err = cpuCgroup.Create(g, &resources)
if err != nil {
return err
}
stats := stats2.Metrics{
CPU: &stats2.CPUStat{
Usage: &stats2.CPUUsage{},
},
}
err = cpuCgroup.Stat(g, &stats)
if err != nil {
return err
}
logrus.Infof("CPU usage stats: usage in kernel mode - %d", stats.CPU.Usage.Kernel)
Comment thread
Zyqsempai marked this conversation as resolved.

return nil
}
123 changes: 123 additions & 0 deletions v2/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v2

import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"strconv"

statsv2 "github.com/containerd/cgroups/v2/stats"
specs "github.com/opencontainers/runtime-spec/specs-go"
)

func NewCpu(unifiedMountpoint string) (*cpuController, error) {
c := &cpuController{
unifiedMountpoint: unifiedMountpoint,
}

ok, err := c.Available("/")
if err != nil {
return nil, err
}
if !ok {
return nil, ErrCPUNotSupported
}
return c, nil
}

type cpuController struct {
unifiedMountpoint string
}

func (c *cpuController) Name() Name {
return Cpu
}

func (c *cpuController) path(g GroupPath) string {
return filepath.Join(c.unifiedMountpoint, string(g))
}

func (c *cpuController) Create(g GroupPath, resources *specs.LinuxResources) error {
if err := os.MkdirAll(c.path(g), defaultDirPerm); err != nil {
return err
}
if cpuShares := resources.CPU.Shares; cpuShares != nil {
// Converting cgroups configuration from v1 to v2
// more here https://github.com/containers/crun/blob/master/crun.1.md#cgroup-v2
convertedWeight := (1 + ((*cpuShares-2)*9999)/262142)
Comment thread
Zyqsempai marked this conversation as resolved.
weight := []byte(strconv.FormatUint(convertedWeight, 10))
if err := ioutil.WriteFile(
filepath.Join(c.path(g), "cpu.weight"),
weight,
defaultFilePerm,
); err != nil {
return err
}
}

if cpuPeriod := resources.CPU.Period; cpuPeriod != nil {
max := []byte(strconv.FormatUint(*cpuPeriod, 10))
if err := ioutil.WriteFile(
filepath.Join(c.path(g), "cpu.max"),
max,
defaultFilePerm,
); err != nil {
return err
}
}

return nil
}

func (c *cpuController) Update(g GroupPath, resources *specs.LinuxResources) error {
return c.Create(g, resources)
}

func (c *cpuController) Stat(g GroupPath, stats *statsv2.Metrics) error {
f, err := os.Open(filepath.Join(c.path(g), "cpu.stat"))
if err != nil {
return err
}
defer f.Close()
// get or create the cpu field because cpuacct can also set values on this struct
sc := bufio.NewScanner(f)
for sc.Scan() {
if err := sc.Err(); err != nil {
return err
}
key, v, err := parseKV(sc.Text())
if err != nil {
return err
}
switch key {
case "usage_usec":
stats.CPU.Usage.Total = v
case "user_usec":
stats.CPU.Usage.User = v
case "system_usec":
stats.CPU.Usage.Kernel = v
}
}
return nil
}

func (c *cpuController) Available(g GroupPath) (bool, error) {
return available(c.unifiedMountpoint, g, Pids)
}
1 change: 1 addition & 0 deletions v2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
ErrFreezerNotSupported = errors.New("cgroups: freezer cgroup (v2) not supported on this system")
ErrMemoryNotSupported = errors.New("cgroups: memory cgroup (v2) not supported on this system")
ErrPidsNotSupported = errors.New("cgroups: pids cgroup (v2) not supported on this system")
ErrCPUNotSupported = errors.New("cgroups: cpu cgroup (v2) not supported on this system")
ErrCgroupDeleted = errors.New("cgroups: cgroup deleted")
ErrNoCgroupMountDestination = errors.New("cgroups: cannot find cgroup mount destination")

Expand Down
Loading