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

ws-daemon: Fix the problem with nice value not being applied #14111

Merged
merged 3 commits into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 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
NumberOfProcessesToStopApplying = 5
)

type ProcessPriorityV2 struct {
Expand All @@ -51,21 +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)

_, err := os.Stat(fullCgroupPath)
if errors.Is(err, fs.ErrNotExist) {
return xerrors.Errorf("cannot read cgroup directory %s: %w", fullCgroupPath, err)
}
t := time.NewTicker(10 * time.Second)
defer t.Stop()

go func() {
time.Sleep(10 * time.Second)
for {
select {
case <-ctx.Done():
return nil
case <-t.C:
}

data, err := ioutil.ReadFile(filepath.Join(fullCgroupPath, "workspace", "user", "cgroup.procs"))
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
// 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 @@ -97,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 >= NumberOfProcessesToStopApplying {
return nil
}
}
}

var (
Expand Down