-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathVirtualProc.java
More file actions
290 lines (246 loc) · 9.89 KB
/
Copy pathVirtualProc.java
File metadata and controls
290 lines (246 loc) · 9.89 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.imageworks.spcue;
import com.imageworks.spcue.dispatcher.Dispatcher;
import com.imageworks.spcue.grpc.host.ThreadMode;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class VirtualProc extends FrameEntity implements ProcInterface {
private static final Logger logger = LogManager.getLogger(VirtualProc.class);
public String hostId;
public String allocationId;
public String frameId;
public String hostName;
public String os;
public byte[] childProcesses;
public boolean canHandleNegativeCoresRequest;
public int coresReserved;
public long memoryReserved;
public long memoryUsed;
public long memoryMax;
public long virtualMemoryUsed;
public long virtualMemoryMax;
public int gpusReserved;
public long gpuMemoryReserved;
public long gpuMemoryUsed;
public long gpuMemoryMax;
public boolean unbooked;
public boolean usageRecorded = false;
public boolean isLocalDispatch = false;
public String getProcId() {
return id;
}
public String getHostId() {
return hostId;
}
public String getAllocationId() {
return allocationId;
}
public String getFrameId() {
return frameId;
}
public String getName() {
return hostName;
}
/**
* Build and return a proc in either fast or efficient mode.
*
* Efficient mode tries to assign one core per frame, but may upgrade the number of cores based
* on memory usage.
*
* Fast mode books all the idle cores on the the host at one time.
*/
public static final VirtualProc build(DispatchHost host, DispatchFrame frame,
String... selfishServices) {
VirtualProc proc = seedProc(host, frame);
// Give away all existing stranded cores to drift host back to a balanced state
// Read "docs > User Guides > Stranded Cores in Cuebot" for more info
if (host.strandedCores > 0) {
proc.coresReserved += host.strandedCores;
}
proc.canHandleNegativeCoresRequest = host.canHandleNegativeCoresRequest(proc.coresReserved);
int requested = proc.coresReserved;
if (requested == 0) {
logger.debug("Reserving all cores");
proc.coresReserved = host.cores;
} else if (requested < 0) {
logger.debug("Reserving all cores minus " + requested);
proc.coresReserved = host.cores + requested;
} else if (requested >= 100) {
proc.coresReserved =
computeMultiCoreReservation(host, frame, requested, selfishServices);
}
if (!frame.threadable && proc.coresReserved > 100) {
proc.coresReserved = 100;
}
return proc;
}
private static VirtualProc seedProc(DispatchHost host, DispatchFrame frame) {
VirtualProc proc = new VirtualProc();
proc.allocationId = host.getAllocationId();
proc.hostId = host.getHostId();
proc.frameId = null;
proc.layerId = frame.getLayerId();
proc.jobId = frame.getJobId();
proc.showId = frame.getShowId();
proc.facilityId = frame.getFacilityId();
proc.os = frame.os;
proc.hostName = host.getName();
proc.unbooked = false;
proc.isLocalDispatch = host.isLocalDispatch;
proc.coresReserved = frame.minCores;
proc.memoryReserved = frame.getMinMemory();
proc.gpusReserved = frame.minGpus;
proc.gpuMemoryReserved = frame.minGpuMemory;
return proc;
}
private static int computeMultiCoreReservation(DispatchHost host, DispatchFrame frame,
int requested, String[] selfishServices) {
int wholeCores = wholeIdleCores(host);
requireWholeCore(wholeCores, frame);
int cores = baseReservationByMode(host, frame, wholeCores, requested, selfishServices);
cores = enforceVariableFloor(host, frame, cores);
cores = applySanityBounds(cores, requested, frame.maxCores);
cores = clampToIdle(host, frame, cores, wholeCores);
return cores;
}
private static int wholeIdleCores(DispatchHost host) {
return (int) Math.floor(host.idleCores / 100.0);
}
private static void requireWholeCore(int wholeCores, DispatchFrame frame) {
if (wholeCores == 0) {
throw new EntityException("The host had only a fraction of a core remaining "
+ "but the frame required " + frame.minCores);
}
}
private static int baseReservationByMode(DispatchHost host, DispatchFrame frame, int wholeCores,
int requested, String[] selfishServices) {
if (host.threadMode == ThreadMode.ALL_VALUE) {
return wholeCores * 100;
}
if (!frame.threadable) {
return requested;
}
if (isSelfishService(frame, selfishServices)) {
return wholeCores * 100;
}
if (host.idleMemory - frame.getMinMemory() <= Dispatcher.MEM_STRANDED_THRESHHOLD) {
return wholeCores * 100;
}
return getCoreSpan(host, frame.getMinMemory(), frame.maxCores);
}
private static int enforceVariableFloor(DispatchHost host, DispatchFrame frame, int cores) {
if (host.threadMode != ThreadMode.VARIABLE_VALUE || !frame.threadable || cores > 200) {
return cores;
}
if (200 > host.idleCores) {
// Do not allow threadable frame running on 1 core.
throw new JobDispatchException(
"Do not allow threadable frame running one core on a ThreadMode.Variable host.");
}
return 200;
}
private static int applySanityBounds(int cores, int originalCores, int maxCores) {
if (cores < 100) {
cores = 100;
}
if (cores < originalCores) {
cores = originalCores;
}
if (maxCores > 0 && cores > maxCores) {
cores = maxCores;
}
return cores;
}
private static int clampToIdle(DispatchHost host, DispatchFrame frame, int cores,
int wholeCores) {
if (cores <= host.idleCores) {
return cores;
}
if (host.threadMode == ThreadMode.VARIABLE_VALUE && frame.threadable && wholeCores == 1) {
throw new JobDispatchException(
"Do not allow threadable frame running one core on a ThreadMode.Variable host.");
}
return wholeCores * 100;
}
private static boolean isSelfishService(DispatchFrame frame, String[] selfishServices) {
return selfishServices != null && frame.services != null
&& containsSelfishService(frame.services.split(","), selfishServices);
}
private static final boolean containsSelfishService(String[] frameServices,
String[] selfishServices) {
for (String frameService : frameServices) {
for (String selfishService : selfishServices) {
if (frameService.equals(selfishService)) {
return true;
}
}
}
return false;
}
public static final VirtualProc build(DispatchHost host, DispatchFrame frame,
LocalHostAssignment lja) {
VirtualProc proc = new VirtualProc();
proc.allocationId = host.getAllocationId();
proc.hostId = host.getHostId();
proc.frameId = null;
proc.layerId = frame.getLayerId();
proc.jobId = frame.getJobId();
proc.showId = frame.getShowId();
proc.facilityId = frame.getFacilityId();
proc.os = frame.os;
proc.hostName = host.getName();
proc.unbooked = false;
proc.isLocalDispatch = host.isLocalDispatch;
proc.coresReserved = lja.getThreads() * 100;
proc.memoryReserved = frame.getMinMemory();
proc.gpusReserved = frame.minGpus;
proc.gpuMemoryReserved = frame.minGpuMemory;
int wholeCores = (int) (Math.floor(host.idleCores / 100.0));
if (wholeCores == 0) {
throw new EntityException("The host had only a fraction of a core remaining "
+ "but the frame required " + frame.minCores);
}
if (proc.coresReserved > host.idleCores) {
proc.coresReserved = wholeCores * 100;
}
return proc;
}
/**
* Allocates additional cores when the frame is using more than a single core's worth of memory,
* bounded by maxCores when set.
*
* @param host
* @param minMemory
* @param maxCores maximum cores allowed in core units (using core_multiplier: 100 = 1, 0 = no
* limit)
* @return
*/
public static int getCoreSpan(DispatchHost host, long minMemory, int maxCores) {
int totalCores = (int) (Math.floor(host.cores / 100.0));
int idleCores = (int) (Math.floor(host.idleCores / 100.0));
if (idleCores < 1) {
return 100;
}
long memPerCore = host.idleMemory / totalCores;
double procs = minMemory / (double) memPerCore;
int reserveCores = (int) (Math.round(procs)) * 100;
// Respect max cores even when the memory-per-core guideline
// would allocate more cores than the frame is allowed to use.
if (maxCores > 0 && reserveCores > maxCores) {
reserveCores = maxCores;
}
return reserveCores;
}
}