-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (212 loc) · 7.3 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// easy is a port of ionice to Go
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"syscall"
"github.com/jessevdk/go-flags"
"github.com/xyproto/gionice"
)
const versionString = "easy 1.3.0"
const usageString = `Usage:
easy [options] -p <pid>...
easy [options] -P <pgid>...
easy [options] -u <uid>...
easy [options] <command>
Set or get the I/O-scheduling class and priority of a process.
Options:
-c, --class <class> name or number of scheduling class (I/O),
0: none, 1: realtime, 2: best-effort, 3: idle
-n, --classdata <num> priority (0..7) in the specified scheduling class,
only for the realtime and best-effort classes (I/O)
-p, --pid <pid>... act on these already running processes
-P, --pgid <pgrp>... act on already running processes in these groups
-t, --ignore ignore failures
-N, --nice set the niceness to 10 (CPU)
-a, --adjustment <x> adjust the nice priority with the given number
-u, --uid <uid>... act on already running processes owned by these users
-s, --setnice <x> set the process niceness
-b, --both set the CPU niceness to 10 and the I/O class to "idle"
-h, --help display this help
-V, --version display version
For more details see easy(1).`
// Options is a struct containing information about all flags used by easy
type Options struct {
Class string `short:"c" long:"class" description:"name or number of scheduling class, 0: none, 1: realtime, 2: best-effort, 3: idle" choice:"0" choice:"1" choice:"2" choice:"3" choice:"none" choice:"realtime" choice:"best-effort" choice:"idle"`
ClassData int `short:"n" long:"classdata" description:"priority (0..7) in the specified scheduling class, only for the realtime and best-effort classes" choice:"0" choice:"1" choice:"2" choice:"3" choice:"4" choice:"5" choice:"6" choice:"7" choice:"8" choice:"9"`
PID int `short:"p" long:"pid" description:"act on these already running processes" value-name:"PID"`
PGID int `short:"P" long:"pgid" description:"act on already running processes in these groups" value-name:"PGID"`
Ignore bool `short:"t" long:"ignore" description:"ignore failures"`
Nice bool `short:"N" long:"nice" description:"also set niceness to 10"`
UID int `short:"u" long:"uid" description:"act on already running processes owned by these users" value-name:"UID"`
Help bool `short:"h" long:"help" description:"display this help"`
Version bool `short:"V" long:"version" description:"display version"`
Adjustment int `short:"a" long:"adjustment" description:"niceness priority adjustment"`
SetNice int `short:"s" long:"setnice" description:"set the process niceness"`
Both bool `short:"b" long:"both" description:"set the CPU niceness to 10 and the I/O class to idle"`
Args struct {
Command []string
}
}
func main() {
opts := &Options{}
parser := flags.NewParser(opts, flags.PassAfterNonOption|flags.PassDoubleDash)
args, err := parser.Parse()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var (
hasClass = parser.FindOptionByLongName("class").IsSet()
hasClassData = parser.FindOptionByLongName("classdata").IsSet()
hasPID = parser.FindOptionByLongName("pid").IsSet()
hasPGID = parser.FindOptionByLongName("pgid").IsSet()
hasUID = parser.FindOptionByLongName("uid").IsSet()
hasAdjustment = parser.FindOptionByLongName("adjustment").IsSet()
hasSetNice = parser.FindOptionByLongName("setnice").IsSet()
hasSetBoth = parser.FindOptionByLongName("both").IsSet()
data = 4
set, which, who int
ioclass = gionice.IOPRIO_CLASS_BE
tolerant bool
)
if opts.Help {
fmt.Println(usageString)
os.Exit(0)
}
if opts.Version {
fmt.Println(versionString)
os.Exit(0)
}
if opts.Ignore {
tolerant = true
}
if hasClassData {
set |= 1
}
if hasClass {
ioclass, err = gionice.Parse(opts.Class)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if ioclass < 0 {
fmt.Fprintf(os.Stderr, "uknown scheduling class: '%s'\n", opts.Class)
}
set |= 2
}
if hasPID {
if who != 0 {
fmt.Fprintln(os.Stderr, "can handle only one of pid, pgid or uid at once")
os.Exit(1)
}
which = opts.PID
who = gionice.IOPRIO_WHO_PROCESS
}
if hasPGID {
if who != 0 {
fmt.Fprintln(os.Stderr, "can handle only one of pid, pgid or uid at once")
os.Exit(1)
}
which = opts.PGID
who = gionice.IOPRIO_WHO_PGRP
}
if hasUID {
if who != 0 {
fmt.Fprintln(os.Stderr, "can handle only one of pid, pgid or uid at once")
os.Exit(1)
}
which = opts.UID
who = gionice.IOPRIO_WHO_USER
}
// The functionality of "nice"
if hasSetNice {
gionice.SetNicePri(which, who, opts.SetNice)
} else if hasSetBoth {
gionice.SetNicePri(which, who, 10)
ioclass = gionice.IOPRIO_CLASS_IDLE
} else if hasAdjustment {
currentPri, err := gionice.NicePri(which, who)
if err != nil {
// warning, can not get niceness priority
fmt.Fprintf(os.Stderr, "can not get niceness: %v\n", err)
os.Exit(1)
}
currentPri += opts.Adjustment
gionice.SetNicePri(which, who, currentPri)
} else if opts.Nice {
gionice.SetNicePri(which, who, 10)
}
switch ioclass {
case gionice.IOPRIO_CLASS_NONE:
if (set&1) != 0 && !tolerant {
// warning
fmt.Fprintln(os.Stderr, "ignoring given cass data for none class")
}
data = 0
case gionice.IOPRIO_CLASS_RT, gionice.IOPRIO_CLASS_BE:
break
case gionice.IOPRIO_CLASS_IDLE:
if (set&1) != 0 && !tolerant {
// just a warning, no exit
fmt.Fprintln(os.Stderr, "ignoring given class data for idle class")
}
data = 7
default:
if !tolerant {
// just a warning, no exit
fmt.Fprintf(os.Stderr, "unknown prio class %d\n", ioclass)
}
}
if set == 0 && which == 0 && len(args) == 0 {
// easy without options, print the current ioprio
gionice.Print(0, gionice.IOPRIO_WHO_PROCESS)
} else if set == 0 && who != 0 {
// easy -p|-P|-u ID [ID ...]
gionice.Print(which, who)
for _, id := range args {
if n, err := strconv.Atoi(id); err == nil { // success, arg is a number
which = n
gionice.Print(which, who)
}
}
} else if set != 0 && who != 0 {
// easy -c CLASS -p|-P|-u ID [ID ...]
if err := gionice.SetIDPri(which, ioclass, data, who); err != nil && !tolerant {
fmt.Fprintln(os.Stderr, "ioprio_set failed", err)
os.Exit(1)
}
for _, id := range args {
if n, err := strconv.Atoi(id); err == nil { // success, arg is a number
which = n
if err := gionice.SetIDPri(which, ioclass, data, who); err != nil && !tolerant {
fmt.Fprintln(os.Stderr, "ioprio_set failed", err)
os.Exit(1)
}
}
}
} else if len(args) > 0 {
// easy [-c CLASS] COMMAND
if err := gionice.SetIDPri(0, ioclass, data, gionice.IOPRIO_WHO_PROCESS); err != nil && !tolerant {
fmt.Fprintln(os.Stderr, "ioprio_set failed", err)
os.Exit(1)
}
var argv0 string = args[0] // got to find the path first?
argv0, err := exec.LookPath(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "could not find %s in PATH\n", args[0])
os.Exit(1)
}
err = syscall.Exec(argv0, args, os.Environ())
if err != nil {
fmt.Fprintf(os.Stderr, "failed to execute %s\n", argv0)
os.Exit(1)
}
os.Exit(1)
} else {
fmt.Fprintln(os.Stderr, "bad usage\nTry 'easy --help' for more information.")
os.Exit(1)
}
}