Skip to content

Commit

Permalink
ws-daemon: Fix the problem with nice value not being applied
Browse files Browse the repository at this point in the history
Signed-off-by: utam0k <[email protected]>
  • Loading branch information
utam0k authored and roboquat committed Oct 24, 2022
1 parent 9bdc0a5 commit 708fc53
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions components/ws-daemon/pkg/cgroup/plugin_process_priority_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
Expand All @@ -18,7 +17,6 @@ import (
"time"

"github.com/shirou/gopsutil/process"
"golang.org/x/xerrors"

"github.com/gitpod-io/gitpod/common-go/log"
)
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 708fc53

Please sign in to comment.