Repository navigation
Expand file tree
/
Copy pathweights.py
More file actions
187 lines (159 loc) · 5.19 KB
/
Copy pathweights.py
File metadata and controls
187 lines (159 loc) · 5.19 KB
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
'''
Memory a run is expected to need, in MB, by the program it starts.
The scheduler admits a run only while its weight fits into the free memory, so many
light runs (assembler, C) or a few heavy ones (dotnet, kotlinc) run at once. A weight is
only the estimate for the start: the server measures a running program and replaces the
estimate after a few seconds.
WEIGHTS is measured, not guessed: tools/measure_weights.py runs every example of
README.md in a container with the memory of the production server and prints this
table. Run it again after changing the image.
'''
import re
import shlex
# a program that is not listed, e.g. from a course with its own @LIA.eval command
DEFAULT = 64
# a compiled program of the run itself: ./a.out, ./main
NATIVE = 24
# shell helpers inside of a command, which do not count
IGNORE = {"cat", "cd", "cp", "echo", "exit", "grep", "head", "mv", "rm", "sort", "tail", "test", "["}
# programs that only start the one after them
WRAPPERS = {"env", "exec", "nice", "sh", "bash", "timeout"}
# measured on Ubuntu 26.04.1 LTS, 512 MB of memory, 8 CPUs
WEIGHTS = {
"a68g": 36,
"awk": 8,
"bwbasic": 4,
"clojure": 100,
"cobc": 36,
"coqc": 184,
"coqtop": 184,
"dfrotz": 4,
"dotnet": 376,
"dotnet *.dll": 36,
"dyalog": 16,
"elixir": 104,
"erl": 80,
"erlc": 92,
"forge": 212,
"fpc": 32,
"g++": 68,
"gcc": 36,
"gdc": 100,
"gforth": 8,
"gfortran": 36,
"ghc": 244,
"ghci": 204,
"ghdl": 12,
"gleam": 40,
"gm2": 40,
"gnatmake": 48,
"go": 124,
"gobjc": 52,
"groovy": 96,
"gs": 40,
"gst": 12,
"guile": 16,
"haxe": 52,
"iex": 108,
"inform": 4,
"io": 12,
"irb": 36,
"iverilog": 8,
"java": 56,
"javac": 84,
"julia": 312,
"kotlinc": 240,
"ld": 8,
"lua": 4,
"mcs": 72,
"mono": 32,
"nasm": 8,
"nim": 84,
"node": 72,
"ocaml": 20,
"octave": 72,
"perl": 16,
"php": 28,
"python2.7": 12,
"python3": 200,
"qsharp": 128,
"R": 68,
"racket": 172,
"rexx": 4,
"Rscript": 204,
"ruby": 24,
"rustc": 196,
"S2c": 136,
"scala": 96,
"scalac": 176,
"sml": 20,
"solcjs": 272,
"swipl": 12,
"tclsh": 8,
"v": 208,
"valac": 56,
"vvp": 12,
"zig": 120,
}
# installed in the image, but not an example of README.md
WEIGHTS["manim"] = 200
# Two programs need much less for the common case than for their heaviest example, which
# WEIGHTS holds. They get the weight in LIGHT, unless the files of the run (names and
# contents) match the pattern in HEAVY. The values are those of the lighter example in
# the output of tools/measure_weights.py.
# - Python on its own, not with its scientific libraries
# - dotnet for C#, not for F#
LIGHT = {"python3": 12, "dotnet": 300}
HEAVY = {
"python3": re.compile(
r"^\s*(?:import|from)\s+(?:numpy|scipy|pandas|matplotlib|cv2|manim|sympy|pyarrow|qdk)\b", re.M),
"dotnet": re.compile(r"\.fs(?:proj|x)?\b"),
}
def programs(command: str) -> list:
'''
The programs a shell command starts: the first word of every part between
`&&`, `||`, `|` and `;`, also inside of `sh -c "..."`.
'''
try:
lexer = shlex.shlex(command, posix=True, punctuation_chars=True)
lexer.whitespace_split = True
tokens = list(lexer)
except ValueError: # unbalanced quotes
tokens = command.split()
found, first = [], True
skip = False
for i, token in enumerate(tokens):
if skip:
skip = False # the file of a redirection
elif token and all(char in "<>&" for char in token) and token.strip("&"):
skip = True
elif token in ("{", "}", "!") or (token and all(char in "&|;()" for char in token)):
first = True
elif first:
if "=" in token and not token.startswith(("./", "/")):
continue # VARIABLE=value in front of a program
name = "./" if token.startswith("./") else token.rsplit("/", 1)[-1]
if name in WRAPPERS:
if name in ("sh", "bash") and tokens[i + 1:i + 2] == ["-c"] and len(tokens) > i + 2:
found += programs(tokens[i + 2])
first = False
continue # the program follows, after the options of the wrapper
if token.startswith("-") or token.replace(".", "").isdigit():
continue # option or number of a wrapper: timeout -k 5 300 program
if name == "dotnet" and tokens[i + 1:i + 2] and tokens[i + 1].endswith(".dll"):
name = "dotnet *.dll" # a built program, not the SDK
found.append(name)
first = False
return [name for name in found if name not in IGNORE]
def estimate(command: str, sources: str = "") -> int:
'''
Weight of a command in MB: that of its heaviest program. `sources` are the names
and contents of the files of the run.
'''
def weight(name):
if name == "./":
return NATIVE
if name in LIGHT and not HEAVY[name].search(sources):
return LIGHT[name]
return WEIGHTS.get(name, DEFAULT)
return max((weight(name) for name in programs(command)), default=DEFAULT)