From 03935fcf8cb2049a2788df4c82b4bc3cb7f33ddf Mon Sep 17 00:00:00 2001 From: utam0k Date: Mon, 24 Oct 2022 11:57:17 +0900 Subject: [PATCH 1/3] ws-daemon: Add the disposal in the priority plugin when the workspace has gone --- .../ws-daemon/pkg/cgroup/plugin_process_priority_v2.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go b/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go index d6a2178b1a6652..7b29dba79440e9 100644 --- a/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go +++ b/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go @@ -51,16 +51,14 @@ func (c *ProcessPriorityV2) Type() Version { return Version2 } func (c *ProcessPriorityV2) Apply(ctx context.Context, opts *PluginOptions) error { fullCgroupPath := filepath.Join(opts.BasePath, opts.CgroupPath) - _, err := os.Stat(fullCgroupPath) - if errors.Is(err, fs.ErrNotExist) { - return xerrors.Errorf("cannot read cgroup directory %s: %w", fullCgroupPath, err) - } - go func() { time.Sleep(10 * time.Second) data, err := ioutil.ReadFile(filepath.Join(fullCgroupPath, "workspace", "user", "cgroup.procs")) - if err != nil { + if errors.Is(err, fs.ErrNotExist) { + log.WithField("path", fullCgroupPath).WithError(err).Warn("the target cgroup has gone") + return + } else if err != nil { log.WithField("path", fullCgroupPath).WithError(err).Errorf("cannot read cgroup.procs file") return } From 93f4e7f1b38350e4a606f4f6060cbd700b657503 Mon Sep 17 00:00:00 2001 From: utam0k Date: Mon, 24 Oct 2022 11:59:53 +0900 Subject: [PATCH 2/3] ws-daemon: Fix the problem with nice value not being applied Signed-off-by: utam0k --- .../pkg/cgroup/plugin_process_priority_v2.go | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go b/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go index 7b29dba79440e9..24ba4dabdb41aa 100644 --- a/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go +++ b/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go @@ -9,7 +9,6 @@ import ( "errors" "io/fs" "io/ioutil" - "os" "path/filepath" "regexp" "strconv" @@ -18,7 +17,6 @@ import ( "time" "github.com/shirou/gopsutil/process" - "golang.org/x/xerrors" "github.com/gitpod-io/gitpod/common-go/log" ) @@ -39,6 +37,9 @@ const ( ProcessCodeServerHelper ProcessType = "vscode-server-helper" // ProcessDefault referes to any process that is not one of the above ProcessDefault ProcessType = "default" + + // Repeat applying until this number of processes is reached + NumberOfProcessesToStopApllying = 5 ) type ProcessPriorityV2 struct { @@ -51,19 +52,28 @@ func (c *ProcessPriorityV2) Type() Version { return Version2 } func (c *ProcessPriorityV2) Apply(ctx context.Context, opts *PluginOptions) error { fullCgroupPath := filepath.Join(opts.BasePath, opts.CgroupPath) - go func() { - time.Sleep(10 * time.Second) + t := time.NewTicker(10 * time.Second) + defer t.Stop() + + for { + select { + case <-ctx.Done(): + return nil + case <-t.C: + } data, err := ioutil.ReadFile(filepath.Join(fullCgroupPath, "workspace", "user", "cgroup.procs")) if errors.Is(err, fs.ErrNotExist) { - log.WithField("path", fullCgroupPath).WithError(err).Warn("the target cgroup has gone") - return + // the target cgroup/workspace has gone + return nil } else if err != nil { log.WithField("path", fullCgroupPath).WithError(err).Errorf("cannot read cgroup.procs file") - return + return err } - for _, line := range strings.Split(string(data), "\n") { + var countRunningProcess int + lines := strings.Split(string(data), "\n") + for _, line := range lines { line = strings.TrimSpace(line) if len(line) == 0 { continue @@ -95,14 +105,17 @@ func (c *ProcessPriorityV2) Apply(ctx context.Context, opts *PluginOptions) erro continue } + countRunningProcess += 1 err = syscall.Setpriority(syscall.PRIO_PROCESS, int(pid), priority) if err != nil { log.WithError(err).WithField("pid", pid).WithField("priority", priority).Warn("cannot set process priority") } } - }() - return nil + if countRunningProcess >= NumberOfProcessesToStopApllying { + return nil + } + } } var ( From 3e4f0002fdc215cfc3cef34eea83631ffdc7c66c Mon Sep 17 00:00:00 2001 From: Pavel Tumik <18602811+sagor999@users.noreply.github.com> Date: Mon, 24 Oct 2022 14:21:05 +0000 Subject: [PATCH 3/3] fix nit --- components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go b/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go index 24ba4dabdb41aa..f0922a659b7782 100644 --- a/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go +++ b/components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go @@ -39,7 +39,7 @@ const ( ProcessDefault ProcessType = "default" // Repeat applying until this number of processes is reached - NumberOfProcessesToStopApllying = 5 + NumberOfProcessesToStopApplying = 5 ) type ProcessPriorityV2 struct { @@ -112,7 +112,7 @@ func (c *ProcessPriorityV2) Apply(ctx context.Context, opts *PluginOptions) erro } } - if countRunningProcess >= NumberOfProcessesToStopApllying { + if countRunningProcess >= NumberOfProcessesToStopApplying { return nil } }