Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit memory resource usage for nydus daemon via cgroup #415

Merged
merged 15 commits into from
Jun 16, 2023

Conversation

sctb512
Copy link
Member

@sctb512 sctb512 commented Mar 16, 2023

Nydus-snapshotter does not have a maximum memory limit by default. This can lead to host invalidity. Now, we put snapshotter and daemon in different cgroup to limit the maximum available memory.

This PR adds two entries to the configuration file.

[cgroup]
# Whether to use separate cgroup for nydusd.
enable = true
# The memory limit for nydusd.slice, which contains all nydusd processes.
# Percentage is supported as well, please ensure it is end with "%".
# The default unit is bytes. Acceptable values include "209715200", "200MiB", "200Mi" and "10%".
memory_limit = ""

@@ -559,6 +562,38 @@ func (d *Daemon) ClearVestige() {
d.ResetClient()
}

func (d *Daemon) AddCgroup(pid int) error {
Copy link
Collaborator

@imeoer imeoer Mar 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should implement a cgroup manager and add the daemon to it, instead add the method AddCgroup, DeleteCgroup on *Daemon. One reason is that maybe we need to support containerized snapshotter/nydusd to limit resource usage and process management in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will refactor this later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. We should support resources limitation based on both raw cgroup controller and containers

Copy link
Member

@changweige changweige left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it based on cgroup v1 or v2?

@sctb512
Copy link
Member Author

sctb512 commented Mar 16, 2023

Is it based on cgroup v1 or v2?

It is based on cgroup v1.

@changweige
Copy link
Member

Is it based on cgroup v1 or v2?

It is based on cgroup v1.

Is it possible to depend cgroup v2?

@sctb512
Copy link
Member Author

sctb512 commented Mar 17, 2023

Is it possible to depend cgroup v2?

May we implement both v1 and v2? Group v2 depends on the Linux kernel. On my machine, it mounts the cgroup v1 file system by default.

@sctb512 sctb512 changed the title Limit nydus daemon memory via cgroup [WIP] Limit nydus daemon memory via cgroup Mar 29, 2023
@sctb512 sctb512 closed this Jun 1, 2023
@sctb512 sctb512 force-pushed the limit-daemon-memory-via-cgroup branch from 2cb65c5 to d0d9f04 Compare June 1, 2023 08:57
@sctb512 sctb512 reopened this Jun 1, 2023
@codecov
Copy link

codecov bot commented Jun 1, 2023

Codecov Report

Merging #415 (5d9bdba) into main (88ccb52) will increase coverage by 0.17%.
The diff coverage is 56.92%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #415      +/-   ##
==========================================
+ Coverage   37.70%   37.88%   +0.17%     
==========================================
  Files          59       60       +1     
  Lines        6996     7061      +65     
==========================================
+ Hits         2638     2675      +37     
- Misses       4047     4073      +26     
- Partials      311      313       +2     
Impacted Files Coverage Δ
config/config.go 31.38% <0.00%> (-3.02%) ⬇️
pkg/manager/daemon_adaptor.go 0.00% <0.00%> (ø)
pkg/manager/manager.go 14.60% <0.00%> (-0.21%) ⬇️
pkg/utils/parser/parser.go 86.04% <86.04%> (ø)

@sctb512 sctb512 force-pushed the limit-daemon-memory-via-cgroup branch 3 times, most recently from caab0fb to f20f46e Compare June 2, 2023 03:47
@sctb512 sctb512 changed the title [WIP] Limit nydus daemon memory via cgroup Limit nydus daemon memory via cgroup Jun 2, 2023
@sctb512 sctb512 force-pushed the limit-daemon-memory-via-cgroup branch 15 times, most recently from d3f42df to 66d32b1 Compare June 3, 2023 14:17
@sctb512 sctb512 force-pushed the limit-daemon-memory-via-cgroup branch 5 times, most recently from 5f00416 to 83d2797 Compare June 14, 2023 10:07
This PR introduce a cgroup interface for nydusd daemon

Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
@sctb512 sctb512 force-pushed the limit-daemon-memory-via-cgroup branch from 83d2797 to b81c6d7 Compare June 14, 2023 10:08
@sctb512 sctb512 requested a review from changweige June 14, 2023 11:17
Name: "nydusd",
Config: cgroupConfig,
})
if err != nil && (err != cgroup.ErrCgroupNotSupported || err != v2.ErrRootMemorySubtreeControllerNotEnableed) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo “Enableed” or this can be named to xxxDisabled

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Fixed.


var (
sysinfo *syscall.Sysinfo_t
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try sync.Once to initialize the global sysinfo?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It somewhat not threaded safe

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
}
func (cg Cgroup) AddProc(pid int) error {
if cg.manager != nil {
err := cg.manager.AddProc(uint64(pid))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need move the process out from controller if nydusd dies or exits normally?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The process will be automatically removed if nydusd dies. The cgroups library in containerd does not have this method either.

@sctb512 sctb512 force-pushed the limit-daemon-memory-via-cgroup branch 2 times, most recently from 1ec7551 to 438d963 Compare June 15, 2023 12:29
The parser package aims to parse configuration data to expected units

Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
The cgroup manager aims to manager the lifetime of daemon's cgroup
and add nydusd to this cgroup for memory resource limitation and reclaim.

Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
refer to moby/moby#43093

Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
Signed-off-by: Bin Tang <tangbin.bin@bytedance.com>
@sctb512 sctb512 force-pushed the limit-daemon-memory-via-cgroup branch from 438d963 to 5d9bdba Compare June 15, 2023 12:37
@changweige changweige merged commit 0a9b4ad into containerd:main Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants