-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[DO NOT MERGE] cgroup v1: optimize code to avoid parsing mountinfo if possible #2438
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
Changes from 3 commits
38ab3b2
89bd4c1
6f6c72d
55c1ce2
db45ce7
c7147ed
7ce1857
576d617
0bb5f5c
cffb553
44a02d2
f78b570
5aef675
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,16 +116,14 @@ func (m *legacyManager) Apply(pid int) error { | |
| defer m.mu.Unlock() | ||
| if c.Paths != nil { | ||
| paths := make(map[string]string) | ||
| cgMap, err := cgroups.ParseCgroupFile("/proc/self/cgroup") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for name, path := range c.Paths { | ||
| _, err := getSubsystemPath(m.cgroups, name) | ||
| if err != nil { | ||
| // Don't fail if a cgroup hierarchy was not found, just skip this subsystem | ||
| if cgroups.IsNotFound(err) { | ||
| continue | ||
| } | ||
| return err | ||
| if _, ok := cgMap[name]; ok { | ||
| paths[name] = path | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
| } | ||
| paths[name] = path | ||
| } | ||
| m.paths = paths | ||
| return cgroups.EnterPid(m.paths, pid) | ||
|
|
@@ -190,14 +188,16 @@ func (m *legacyManager) Apply(pid int) error { | |
| return err | ||
| } | ||
|
|
||
| if err := joinCgroups(c, pid); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| paths := make(map[string]string) | ||
| for _, s := range legacySubsystems { | ||
| subsystemPath, err := getSubsystemPath(m.cgroups, s.Name()) | ||
| if err != nil { | ||
| // Even if it's `not found` error, we'll return err | ||
| // because devices cgroup is hard requirement for | ||
| // container security. | ||
| if s.Name() == "devices" { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was moved here from |
||
| return err | ||
| } | ||
| // Don't fail if a cgroup hierarchy was not found, just skip this subsystem | ||
| if cgroups.IsNotFound(err) { | ||
| continue | ||
|
|
@@ -207,6 +207,11 @@ func (m *legacyManager) Apply(pid int) error { | |
| paths[s.Name()] = subsystemPath | ||
| } | ||
| m.paths = paths | ||
|
|
||
| if err := m.joinCgroups(pid); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -235,48 +240,25 @@ func (m *legacyManager) Path(subsys string) string { | |
| return m.paths[subsys] | ||
| } | ||
|
|
||
| func join(c *configs.Cgroup, subsystem string, pid int) (string, error) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the body of this function (modulo |
||
| path, err := getSubsystemPath(c, subsystem) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if err := os.MkdirAll(path, 0755); err != nil { | ||
| return "", err | ||
| } | ||
| if err := cgroups.WriteCgroupProc(path, pid); err != nil { | ||
| return "", err | ||
| } | ||
| return path, nil | ||
| } | ||
|
|
||
| func joinCgroups(c *configs.Cgroup, pid int) error { | ||
| func (m *legacyManager) joinCgroups(pid int) error { | ||
| for _, sys := range legacySubsystems { | ||
| name := sys.Name() | ||
| switch name { | ||
| case "name=systemd": | ||
| // let systemd handle this | ||
| case "cpuset": | ||
| path, err := getSubsystemPath(c, name) | ||
| if err != nil && !cgroups.IsNotFound(err) { | ||
| return err | ||
| } | ||
| s := &fs.CpusetGroup{} | ||
| if err := s.ApplyDir(path, c, pid); err != nil { | ||
| return err | ||
| if path, ok := m.paths[name]; ok { | ||
| s := &fs.CpusetGroup{} | ||
| if err := s.ApplyDir(path, m.cgroups, pid); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| default: | ||
| _, err := join(c, name, pid) | ||
| if err != nil { | ||
| // Even if it's `not found` error, we'll return err | ||
| // because devices cgroup is hard requirement for | ||
| // container security. | ||
| if name == "devices" { | ||
| if path, ok := m.paths[name]; ok { | ||
| if err := os.MkdirAll(path, 0755); err != nil { | ||
| return err | ||
| } | ||
| // For other subsystems, omit the `not found` error | ||
| // because they are optional. | ||
| if !cgroups.IsNotFound(err) { | ||
| if err := cgroups.WriteCgroupProc(path, pid); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a check to see whether the subsystem is available. I am not quite sure in which scenario such a check is required. Maybe we need to just drop it.