-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
plugin_process_priority_v2.go
163 lines (130 loc) · 3.82 KB
/
plugin_process_priority_v2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package cgroup
import (
"context"
"errors"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/shirou/gopsutil/process"
"golang.org/x/xerrors"
"github.com/gitpod-io/gitpod/common-go/log"
)
// ProcessType referes to the kinds of prioritisable processes in the cgroup
type ProcessType string
const (
// ProcessSupervisor referes to a supervisor process
ProcessSupervisor ProcessType = "supervisor"
// ProcessIDE refers to node.js IDE process
ProcessIDE ProcessType = "ide"
// ProcessWebIDEHelper refers to VS Code Browser process
ProcessWebIDEHelper ProcessType = "ide-helper"
// ProcessCodeServer refers to VS Code Desktop IDE process
ProcessCodeServer ProcessType = "vscode-server"
// ProcessCodeServerHelper refers to VS Code Desktop child process
ProcessCodeServerHelper ProcessType = "vscode-server-helper"
// ProcessDefault referes to any process that is not one of the above
ProcessDefault ProcessType = "default"
)
type ProcessPriorityV2 struct {
ProcessPriorities map[ProcessType]int
}
func (c *ProcessPriorityV2) Name() string { return "process-priority-v2" }
func (c *ProcessPriorityV2) Type() Version { return Version2 }
func (c *ProcessPriorityV2) Apply(ctx context.Context, basePath, cgroupPath string) error {
fullCgroupPath := filepath.Join(basePath, cgroupPath)
_, err := os.Stat(fullCgroupPath)
if os.IsNotExist(err) {
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 {
log.WithField("path", fullCgroupPath).WithError(err).Errorf("cannot read cgroup.procs file")
return
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
pid, err := strconv.ParseInt(line, 10, 64)
if err != nil {
log.WithError(err).WithField("line", line).Warn("cannot parse pid")
continue
}
proc, err := process.NewProcess(int32(pid))
if err != nil {
if errors.Is(err, process.ErrorProcessNotRunning) {
continue
}
log.WithError(err).WithField("pid", pid).Warn("cannot get process")
continue
}
procType := determineProcessType(proc)
log.Warnf("%s: %s", extractCommand(proc), procType)
if procType == ProcessDefault {
continue
}
priority, ok := c.ProcessPriorities[procType]
if !ok {
continue
}
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
}
var (
vsCodeNodeRegex = regexp.MustCompile("/home/gitpod/.vscode-server/bin/.*/node")
)
func determineProcessType(p *process.Process) ProcessType {
cmd := extractCommand(p)
if len(cmd) == 0 {
return ProcessDefault
}
if strings.HasSuffix(cmd[0], "supervisor") {
return ProcessSupervisor
}
if strings.HasSuffix(cmd[0], "/bin/code-server") {
return ProcessCodeServer
}
if vsCodeNodeRegex.MatchString(cmd[0]) {
return ProcessCodeServerHelper
}
if strings.HasSuffix(cmd[0], "/ide/bin/gitpod-code") {
return ProcessIDE
}
if strings.HasSuffix(cmd[0], "/ide/node") {
return ProcessWebIDEHelper
}
return ProcessDefault
}
func extractCommand(p *process.Process) []string {
if p == nil {
return []string{}
}
cmdLine, err := p.CmdlineSlice()
if err != nil {
return []string{}
}
if len(cmdLine) == 0 {
return []string{}
}
cmd := cmdLine[0]
if cmd == "/bin/bash" || cmd == "sh" {
return cmdLine[1:]
}
return cmdLine
}