forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sched_check.cpp
528 lines (488 loc) · 17.6 KB
/
sched_check.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2013 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// Check whether a job can be sent to a host
#include "credit.h"
#include "sched_config.h"
#include "sched_hr.h"
#include "sched_main.h"
#include "sched_msgs.h"
#include "sched_send.h"
#include "sched_check.h"
#ifdef _USING_FCGI_
#include "boinc_fcgi.h"
#endif
const char* infeasible_string(int code) {
switch (code) {
case INFEASIBLE_MEM: return "Not enough memory";
case INFEASIBLE_DISK: return "Not enough disk";
case INFEASIBLE_CPU: return "CPU too slow";
case INFEASIBLE_APP_SETTING: return "App not selected";
case INFEASIBLE_WORKLOAD: return "Existing workload";
case INFEASIBLE_DUP: return "Already in reply";
case INFEASIBLE_HR: return "Homogeneous redundancy";
case INFEASIBLE_BANDWIDTH: return "Download bandwidth too low";
}
return "Unknown";
}
// Return true if the user has set application preferences,
// and excluded this app
//
bool app_not_selected(int appid) {
unsigned int i;
if (g_wreq->preferred_apps.size() == 0) return false;
for (i=0; i<g_wreq->preferred_apps.size(); i++) {
if (appid == g_wreq->preferred_apps[i].appid) {
g_wreq->preferred_apps[i].work_available = true;
return false;
}
}
return true;
}
static inline int check_memory(WORKUNIT& wu) {
double diff = wu.rsc_memory_bound - g_wreq->usable_ram;
if (diff > 0) {
char message[256];
sprintf(message,
"%s needs %0.2f MB RAM but only %0.2f MB is available for use.",
find_user_friendly_name(wu.appid),
wu.rsc_memory_bound/MEGA, g_wreq->usable_ram/MEGA
);
add_no_work_message(message);
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [WU#%lu %s] needs %0.2fMB RAM; [HOST#%lu] has %0.2fMB, %0.2fMB usable\n",
wu.id, wu.name, wu.rsc_memory_bound/MEGA,
g_reply->host.id, g_wreq->ram/MEGA, g_wreq->usable_ram/MEGA
);
}
g_wreq->mem.set_insufficient(wu.rsc_memory_bound);
g_reply->set_delay(DELAY_NO_WORK_TEMP);
return INFEASIBLE_MEM;
}
return 0;
}
static inline int check_disk(WORKUNIT& wu) {
double diff = wu.rsc_disk_bound - g_wreq->disk_available;
if (diff > 0) {
char message[256];
sprintf(message,
"%s needs %0.2fMB more disk space. You currently have %0.2f MB available and it needs %0.2f MB.",
find_user_friendly_name(wu.appid),
diff/MEGA, g_wreq->disk_available/MEGA, wu.rsc_disk_bound/MEGA
);
add_no_work_message(message);
g_wreq->disk.set_insufficient(diff);
return INFEASIBLE_DISK;
}
return 0;
}
static inline int check_bandwidth(WORKUNIT& wu) {
if (wu.rsc_bandwidth_bound == 0) return 0;
// if n_bwdown is zero, the host has never downloaded anything,
// so skip this check
//
if (g_reply->host.n_bwdown == 0) return 0;
double diff = wu.rsc_bandwidth_bound - g_reply->host.n_bwdown;
if (diff > 0) {
char message[256];
sprintf(message,
"%s requires %0.2f KB/sec download bandwidth. Your computer has been measured at %0.2f KB/sec.",
find_user_friendly_name(wu.appid),
wu.rsc_bandwidth_bound/KILO, g_reply->host.n_bwdown/KILO
);
add_no_work_message(message);
g_wreq->bandwidth.set_insufficient(diff);
return INFEASIBLE_BANDWIDTH;
}
return 0;
}
// Determine if the app is "hard",
// and we should send it only to high-end hosts.
// Currently this is specified by setting weight=-1;
// this is a kludge for SETI@home/Astropulse.
//
static inline bool hard_app(APP& app) {
return (app.weight == -1);
}
static inline double get_estimated_delay(BEST_APP_VERSION& bav) {
int pt = bav.host_usage.proc_type;
if (pt == PROC_TYPE_CPU) {
return g_request->cpu_estimated_delay;
}
COPROC* cp = g_request->coprocs.proc_type_to_coproc(pt);
return cp->estimated_delay;
}
// return the delay bound to use for this job/host.
// Actually, return two: optimistic (lower) and pessimistic (higher).
// If the deadline check with the optimistic bound fails,
// try the pessimistic bound.
// TODO: clean up this mess
//
static void get_delay_bound_range(
WORKUNIT& wu,
int res_server_state, int res_priority, double res_report_deadline,
BEST_APP_VERSION& bav,
double& opt, double& pess
) {
if (res_server_state == RESULT_SERVER_STATE_IN_PROGRESS) {
double now = dtime();
if (res_report_deadline < now) {
// if original deadline has passed, return zeros
// This will skip deadline check.
opt = pess = 0;
return;
}
opt = res_report_deadline - now;
pess = wu.delay_bound;
} else {
opt = pess = wu.delay_bound;
// If the workunit needs reliable and is being sent to a reliable host,
// then shorten the delay bound by the percent specified
//
if (config.reliable_on_priority && res_priority >= config.reliable_on_priority && config.reliable_reduced_delay_bound > 0.01
) {
opt = wu.delay_bound*config.reliable_reduced_delay_bound;
double est_wallclock_duration = estimate_duration(wu, bav);
// Check to see how reasonable this reduced time is.
// Increase it to twice the estimated delay bound
// if all the following apply:
//
// 1) Twice the estimate is longer then the reduced delay bound
// 2) Twice the estimate is less then the original delay bound
// 3) Twice the estimate is less then the twice the reduced delay bound
if (est_wallclock_duration*2 > opt
&& est_wallclock_duration*2 < wu.delay_bound
&& est_wallclock_duration*2 < wu.delay_bound*config.reliable_reduced_delay_bound*2
) {
opt = est_wallclock_duration*2;
}
}
}
}
// return 0 if the job, with the given delay bound,
// will complete by its deadline, and won't cause other jobs to miss deadlines.
//
static inline int check_deadline(
WORKUNIT& wu, APP& app, BEST_APP_VERSION& bav
) {
if (config.ignore_delay_bound) return 0;
// skip delay check if host currently doesn't have any work
// and it's not a hard app.
// (i.e. everyone gets one result, no matter how slow they are)
//
if (get_estimated_delay(bav) == 0 && !hard_app(app)) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [WU#%lu] est delay 0, skipping deadline check\n",
wu.id
);
}
return 0;
}
// if it's a hard app, don't send it to a host with no credit
//
if (hard_app(app) && g_reply->host.total_credit == 0) {
return INFEASIBLE_CPU;
}
// do EDF simulation if possible; else use cruder approximation
//
if (config.workload_sim && g_request->have_other_results_list) {
double est_dur = estimate_duration(wu, bav);
if (g_reply->wreq.edf_reject_test(est_dur, wu.delay_bound)) {
return INFEASIBLE_WORKLOAD;
}
IP_RESULT candidate("", wu.delay_bound, est_dur);
safe_strcpy(candidate.name, wu.name);
if (check_candidate(candidate, g_wreq->effective_ncpus, g_request->ip_results)) {
// it passed the feasibility test,
// but don't add it to the workload yet;
// wait until we commit to sending it
} else {
g_reply->wreq.edf_reject(est_dur, wu.delay_bound);
g_reply->wreq.speed.set_insufficient(0);
return INFEASIBLE_WORKLOAD;
}
} else {
double ewd = estimate_duration(wu, bav);
if (hard_app(app)) ewd *= 1.3;
double est_report_delay = get_estimated_delay(bav) + ewd;
double diff = est_report_delay - wu.delay_bound;
if (diff > 0) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [WU#%lu] deadline miss %d > %d\n",
wu.id, (int)est_report_delay, wu.delay_bound
);
}
g_reply->wreq.speed.set_insufficient(diff);
return INFEASIBLE_CPU;
} else {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [WU#%lu] meets deadline: %.2f + %.2f < %d\n",
wu.id, get_estimated_delay(bav), ewd, wu.delay_bound
);
}
}
}
return 0;
}
// Fast checks (no DB access) to see if the job can be sent to the host.
// Reasons why not include:
// 1) the host doesn't have enough memory;
// 2) the host doesn't have enough disk space;
// 3) based on CPU speed, resource share and estimated delay,
// the host probably won't get the result done within the delay bound
// 4) app isn't in user's "approved apps" list
//
// If the job is feasible, return 0 and fill in wu.delay_bound
// with the delay bound we've decided to use.
//
int wu_is_infeasible_fast(
WORKUNIT& wu,
int res_server_state, int res_priority, double res_report_deadline,
APP& app, BEST_APP_VERSION& bav
) {
int retval;
// project-specific check
//
if (wu_is_infeasible_custom(wu, app, bav)) {
return INFEASIBLE_CUSTOM;
}
if (config.user_filter) {
if (wu.batch && wu.batch != g_reply->user.id) {
return INFEASIBLE_USER_FILTER;
}
}
// homogeneous redundancy: can't send if app uses HR and
// 1) host is of unknown HR class, or
// 2) WU is already committed to different HR class
//
if (app_hr_type(app)) {
if (hr_unknown_class(g_reply->host, app_hr_type(app))) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [HOST#%lu] [WU#%lu %s] host is of unknown class in HR type %d\n",
g_reply->host.id, wu.id, wu.name, app_hr_type(app)
);
}
return INFEASIBLE_HR;
}
if (already_sent_to_different_hr_class(wu, app)) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [HOST#%lu] [WU#%lu %s] failed quick HR check: WU is class %d, host is class %d\n",
g_reply->host.id, wu.id, wu.name, wu.hr_class, hr_class(g_request->host, app_hr_type(app))
);
}
return INFEASIBLE_HR;
}
}
// homogeneous app version
//
if (app.homogeneous_app_version) {
DB_ID_TYPE avid = wu.app_version_id;
if (avid && bav.avp->id != avid) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [HOST#%lu] [WU#%lu %s] failed homogeneous app version check: %lu %lu\n",
g_reply->host.id, wu.id, wu.name, avid, bav.avp->id
);
}
return INFEASIBLE_HAV;
}
}
if (config.one_result_per_user_per_wu || config.one_result_per_host_per_wu) {
if (wu_already_in_reply(wu)) {
return INFEASIBLE_DUP;
}
}
retval = check_memory(wu);
if (retval) return retval;
retval = check_disk(wu);
if (retval) return retval;
retval = check_bandwidth(wu);
if (retval) return retval;
if (app.non_cpu_intensive) {
return 0;
}
// do deadline check last because EDF sim uses some CPU
//
double opt, pess;
get_delay_bound_range(
wu, res_server_state, res_priority, res_report_deadline, bav, opt, pess
);
wu.delay_bound = (int)opt;
if (opt == 0) {
// this is a resend; skip deadline check
return 0;
}
retval = check_deadline(wu, app, bav);
if (retval && (opt != pess)) {
wu.delay_bound = (int)pess;
retval = check_deadline(wu, app, bav);
}
return retval;
}
// Do checks that require DB access for whether we can send this job,
// and return:
// 0 if OK to send
// 1 if can't send to this host
// 2 if can't send to ANY host
//
int slow_check(
WU_RESULT& wu_result, // the job cache entry.
// We may refresh its hr_class and app_version_id fields.
APP* app,
BEST_APP_VERSION* bavp // the app version to be used
) {
int retval;
long n;
DB_RESULT result;
char buf[256];
WORKUNIT& wu = wu_result.workunit;
// Don't send if we've already sent a result of this WU to this user.
//
if (config.one_result_per_user_per_wu) {
sprintf(buf,
"where workunitid=%lu and userid=%lu", wu.id, g_reply->user.id
);
retval = result.count(n, buf);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"send_work: can't get result count (%s)\n", boincerror(retval)
);
return 1;
} else {
if (n>0) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [USER#%lu] already has %ld result(s) for [WU#%lu]\n",
g_reply->user.id, n, wu.id
);
}
return 1;
}
}
} else if (config.one_result_per_host_per_wu) {
// Don't send if we've already sent a result of this WU to this host.
// We only have to check this if we don't send one result per user.
//
sprintf(buf,
"where workunitid=%lu and hostid=%lu", wu.id, g_reply->host.id
);
retval = result.count(n, buf);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"send_work: can't get result count (%s)\n", boincerror(retval)
);
return 1;
} else {
if (n>0) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [HOST#%lu] already has %ld result(s) for [WU#%lu]\n",
g_reply->host.id, n, wu.id
);
}
return 1;
}
}
}
// Checks that require looking up the WU.
// Lump these together so we only do 1 lookup
//
if (app_hr_type(*app) || app->homogeneous_app_version) {
DB_WORKUNIT db_wu;
db_wu.id = wu.id;
int vals[3];
retval = db_wu.get_field_ints(
"hr_class, app_version_id, error_mask", 3, vals
);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"can't get fields for [WU#%lu]: %s\n", db_wu.id, boincerror(retval)
);
return 1;
}
// check wu.error_mask
//
if (vals[2] != 0) {
return 2;
}
if (app_hr_type(*app)) {
wu.hr_class = vals[0];
if (already_sent_to_different_hr_class(wu, *app)) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [HOST#%lu] [WU#%lu %s] is assigned to different HR class\n",
g_reply->host.id, wu.id, wu.name
);
}
// Mark the workunit as infeasible.
// This ensures that jobs already assigned to an HR class
// are processed first.
//
wu_result.infeasible_count++;
return 1;
}
}
if (app->homogeneous_app_version) {
int wu_avid = vals[1];
wu.app_version_id = wu_avid;
if (wu_avid && wu_avid != bavp->avp->id) {
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] [HOST#%lu] [WU#%lu %s] is assigned to different app version\n",
g_reply->host.id, wu.id, wu.name
);
}
wu_result.infeasible_count++;
return 1;
}
}
}
return 0;
}
// Check for pathological conditions that mean
// result is not sendable at all.
//
bool result_still_sendable(DB_RESULT& result, WORKUNIT& wu) {
int retval = result.lookup_id(result.id);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"[RESULT#%lu] result.lookup_id() failed: %s\n",
result.id, boincerror(retval)
);
return false;
}
if (result.server_state != RESULT_SERVER_STATE_UNSENT) {
log_messages.printf(MSG_NORMAL,
"[RESULT#%lu] expected to be unsent; instead, state is %d\n",
result.id, result.server_state
);
return false;
}
if (result.workunitid != wu.id) {
log_messages.printf(MSG_CRITICAL,
"[RESULT#%lu] wrong WU ID: wanted %lu, got %lu\n",
result.id, wu.id, result.workunitid
);
return false;
}
return true;
}