File: cgroup.go

package info (click to toggle)
golang-github-containerd-nydus-snapshotter 0.13.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,824 kB
  • sloc: sh: 470; makefile: 129
file content (61 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Copyright (c) 2023. Nydus Developers. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package cgroup

import (
	"errors"

	"github.com/containerd/cgroups"
	v1 "github.com/containerd/nydus-snapshotter/pkg/cgroup/v1"
	v2 "github.com/containerd/nydus-snapshotter/pkg/cgroup/v2"
)

const (
	defaultSlice = "system.slice"
)

var (
	ErrCgroupNotSupported = errors.New("cgroups: cgroup not supported")
)

type Config struct {
	MemoryLimitInBytes int64
}

type DaemonCgroup interface {
	// Delete the current cgroup.
	Delete() error
	// Add a process to current cgroup.
	AddProc(pid int) error
}

func createCgroup(name string, config Config) (DaemonCgroup, error) {
	if cgroups.Mode() == cgroups.Unified {
		return v2.NewCgroup(defaultSlice, name, config.MemoryLimitInBytes)
	}

	return v1.NewCgroup(defaultSlice, name, config.MemoryLimitInBytes)
}

func supported() bool {
	return cgroups.Mode() != cgroups.Unavailable
}

func displayMode() string {
	switch cgroups.Mode() {
	case cgroups.Legacy:
		return "legacy"
	case cgroups.Hybrid:
		return "hybrid"
	case cgroups.Unified:
		return "unified"
	case cgroups.Unavailable:
		return "unavailable"
	default:
		return "unknown"
	}
}