Skip to content

Commit 708fc53

Browse files
utam0kroboquat
authored andcommitted
ws-daemon: Fix the problem with nice value not being applied
Signed-off-by: utam0k <[email protected]>
1 parent 9bdc0a5 commit 708fc53

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"errors"
1010
"io/fs"
1111
"io/ioutil"
12-
"os"
1312
"path/filepath"
1413
"regexp"
1514
"strconv"
@@ -18,7 +17,6 @@ import (
1817
"time"
1918

2019
"github.com/shirou/gopsutil/process"
21-
"golang.org/x/xerrors"
2220

2321
"github.com/gitpod-io/gitpod/common-go/log"
2422
)
@@ -39,6 +37,9 @@ const (
3937
ProcessCodeServerHelper ProcessType = "vscode-server-helper"
4038
// ProcessDefault referes to any process that is not one of the above
4139
ProcessDefault ProcessType = "default"
40+
41+
// Repeat applying until this number of processes is reached
42+
NumberOfProcessesToStopApllying = 5
4243
)
4344

4445
type ProcessPriorityV2 struct {
@@ -51,19 +52,28 @@ func (c *ProcessPriorityV2) Type() Version { return Version2 }
5152
func (c *ProcessPriorityV2) Apply(ctx context.Context, opts *PluginOptions) error {
5253
fullCgroupPath := filepath.Join(opts.BasePath, opts.CgroupPath)
5354

54-
go func() {
55-
time.Sleep(10 * time.Second)
55+
t := time.NewTicker(10 * time.Second)
56+
defer t.Stop()
57+
58+
for {
59+
select {
60+
case <-ctx.Done():
61+
return nil
62+
case <-t.C:
63+
}
5664

5765
data, err := ioutil.ReadFile(filepath.Join(fullCgroupPath, "workspace", "user", "cgroup.procs"))
5866
if errors.Is(err, fs.ErrNotExist) {
59-
log.WithField("path", fullCgroupPath).WithError(err).Warn("the target cgroup has gone")
60-
return
67+
// the target cgroup/workspace has gone
68+
return nil
6169
} else if err != nil {
6270
log.WithField("path", fullCgroupPath).WithError(err).Errorf("cannot read cgroup.procs file")
63-
return
71+
return err
6472
}
6573

66-
for _, line := range strings.Split(string(data), "\n") {
74+
var countRunningProcess int
75+
lines := strings.Split(string(data), "\n")
76+
for _, line := range lines {
6777
line = strings.TrimSpace(line)
6878
if len(line) == 0 {
6979
continue
@@ -95,14 +105,17 @@ func (c *ProcessPriorityV2) Apply(ctx context.Context, opts *PluginOptions) erro
95105
continue
96106
}
97107

108+
countRunningProcess += 1
98109
err = syscall.Setpriority(syscall.PRIO_PROCESS, int(pid), priority)
99110
if err != nil {
100111
log.WithError(err).WithField("pid", pid).WithField("priority", priority).Warn("cannot set process priority")
101112
}
102113
}
103-
}()
104114

105-
return nil
115+
if countRunningProcess >= NumberOfProcessesToStopApllying {
116+
return nil
117+
}
118+
}
106119
}
107120

108121
var (

0 commit comments

Comments
 (0)