-
Notifications
You must be signed in to change notification settings - Fork 449
/
sched_send.cpp
1760 lines (1624 loc) · 57.5 KB
/
sched_send.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2023 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/>.
// scheduler code related to sending jobs.
// NOTE: there should be nothing here specific to particular
// scheduling policies (array scan, score-based, locality)
#include "config.h"
#include <vector>
#include <list>
#include <string>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include "error_numbers.h"
#include "parse.h"
#include "util.h"
#include "str_util.h"
#include "synch.h"
#include "credit.h"
#include "hr.h"
#include "sched_array.h"
#include "sched_assign.h"
#include "sched_config.h"
#include "sched_customize.h"
#include "sched_hr.h"
#include "sched_locality.h"
#include "sched_main.h"
#include "sched_msgs.h"
#include "sched_nci.h"
#include "sched_shmem.h"
#include "sched_score.h"
#include "sched_timezone.h"
#include "sched_types.h"
#include "sched_util.h"
#include "sched_version.h"
#include "sched_send.h"
#include "boinc_stdio.h"
// if host sends us an impossible RAM size, use this instead
//
const double DEFAULT_RAM_SIZE = 64000000;
int selected_app_message_index=0;
static inline bool file_present_on_host(const char* name) {
for (unsigned i=0; i<g_request->file_infos.size(); i++) {
FILE_INFO& fi = g_request->file_infos[i];
if (!strstr(name, fi.name)) {
return true;
}
}
return false;
}
// return the number of sticky files present on host, used by job
//
int nfiles_on_host(WORKUNIT& wu) {
MIOFILE mf;
mf.init_buf_read(wu.xml_doc);
XML_PARSER xp(&mf);
int n=0;
while (!xp.get_tag()) {
if (xp.match_tag("file_info")) {
FILE_INFO fi;
int retval = fi.parse(xp);
if (retval) continue;
if (!fi.sticky) continue;
if (file_present_on_host(fi.name)) {
n++;
}
}
}
return n;
}
// we're going to send the client this job,
// and the app uses locality scheduling lite.
// Add the job's sticky files to the list of files present on host.
//
void add_job_files_to_host(WORKUNIT& wu) {
MIOFILE mf;
mf.init_buf_read(wu.xml_doc);
XML_PARSER xp(&mf);
while (!xp.get_tag()) {
if (xp.match_tag("file_info")) {
FILE_INFO fi;
int retval = fi.parse(xp);
if (retval) continue;
if (!fi.sticky) continue;
if (!file_present_on_host(fi.name)) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] Adding file %s to host file list\n", fi.name
);
}
g_request->file_infos.push_back(fi);
}
}
}
}
const double MIN_REQ_SECS = 0;
const double MAX_REQ_SECS = (28*SECONDS_IN_DAY);
// compute effective_ncpus;
// get limits on:
// # jobs per day
// # jobs per RPC
// # jobs in progress
//
void WORK_REQ::get_job_limits() {
int ninstances[NPROC_TYPES];
int i;
memset(ninstances, 0, sizeof(ninstances));
int n;
n = g_reply->host.p_ncpus;
if (g_request->global_prefs.max_ncpus_pct && g_request->global_prefs.max_ncpus_pct < 100) {
n = (int)((n*g_request->global_prefs.max_ncpus_pct)/100.);
}
if (n > config.max_ncpus) n = config.max_ncpus;
if (n < 1) n = 1;
if (n > MAX_CPUS) n = MAX_CPUS;
if (project_prefs.max_cpus) {
if (n > project_prefs.max_cpus) {
n = project_prefs.max_cpus;
}
}
ninstances[PROC_TYPE_CPU] = n;
effective_ncpus = n;
effective_ngpus = 0;
for (i=1; i<g_request->coprocs.n_rsc; i++) {
COPROC& cp = g_request->coprocs.coprocs[i];
int proc_type = coproc_type_name_to_num(cp.type);
if (proc_type < 0) continue;
n = cp.count;
if (n > MAX_GPUS) n = MAX_GPUS;
ninstances[proc_type] = n;
effective_ngpus += n;
}
int mult = effective_ncpus + config.gpu_multiplier * effective_ngpus;
if (config.non_cpu_intensive) {
mult = 1;
ninstances[0] = 1;
if (effective_ngpus) effective_ngpus = 1;
}
if (config.max_wus_to_send) {
g_wreq->max_jobs_per_rpc = mult * config.max_wus_to_send;
} else {
g_wreq->max_jobs_per_rpc = 999999;
}
if (config.debug_quota) {
log_messages.printf(MSG_NORMAL,
"[quota] effective ncpus %d ngpus %d\n",
effective_ncpus, effective_ngpus
);
}
config.max_jobs_in_progress.reset(ninstances);
}
const char* find_user_friendly_name(int appid) {
APP* app = ssp->lookup_app(appid);
if (app) return app->user_friendly_name;
return "deprecated application";
}
// Called at start of request handling.
// 1) if there's a global jobs/day limit, enforce it using HAV limit
// 2) if last RPC was yesterday or earlier, clear n_jobs_today for HAV
//
static void update_quota(DB_HOST_APP_VERSION& hav) {
if (config.daily_result_quota) {
if (hav.max_jobs_per_day == 0) {
hav.max_jobs_per_day = config.daily_result_quota;
if (config.debug_quota) {
log_messages.printf(MSG_NORMAL,
"[quota] [HAV#%lu] Initializing max_results_day to %d\n",
hav.app_version_id,
config.daily_result_quota
);
}
}
}
if (g_request->last_rpc_dayofyear != g_request->current_rpc_dayofyear) {
if (config.debug_quota) {
log_messages.printf(MSG_NORMAL,
"[quota] [HOST#%lu] [HAV#%lu] Resetting n_jobs_today\n",
g_reply->host.id, hav.app_version_id
);
}
hav.n_jobs_today = 0;
}
}
// see how much RAM we can use on this machine
//
static inline void get_mem_sizes() {
g_wreq->ram = g_reply->host.m_nbytes;
if (g_wreq->ram <= 0) g_wreq->ram = DEFAULT_RAM_SIZE;
g_wreq->usable_ram = g_wreq->ram;
double busy_frac = g_request->global_prefs.ram_max_used_busy_frac;
double idle_frac = g_request->global_prefs.ram_max_used_idle_frac;
double frac = 1;
if (busy_frac>0 && idle_frac>0) {
frac = std::max(busy_frac, idle_frac);
if (frac > 1) frac = 1;
g_wreq->usable_ram *= frac;
}
}
// Decide whether or not this app version is 'reliable'
// An app version is reliable if the following conditions are true
// (for those that are set in the config file)
// 1) The host average turnaround is less than a threshold
// 2) consecutive_valid is above a threshold
// 3) The host results per day is equal to the max value
//
void get_reliability_version(HOST_APP_VERSION& hav, double multiplier) {
if (hav.turnaround.n > MIN_HOST_SAMPLES && config.reliable_max_avg_turnaround) {
if (hav.turnaround.get_avg() > config.reliable_max_avg_turnaround*multiplier) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] [AV#%lu] not reliable; avg turnaround: %.3f > %.3f hrs\n",
hav.app_version_id,
hav.turnaround.get_avg()/3600,
config.reliable_max_avg_turnaround*multiplier/3600
);
}
hav.reliable = false;
return;
}
}
if (hav.consecutive_valid < CONS_VALID_RELIABLE) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] [AV#%lu] not reliable; cons valid %d < %d\n",
hav.app_version_id,
hav.consecutive_valid, CONS_VALID_RELIABLE
);
}
hav.reliable = false;
return;
}
if (config.daily_result_quota) {
if (hav.max_jobs_per_day < config.daily_result_quota) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] [AV#%lu] not reliable; max_jobs_per_day %d<%d\n",
hav.app_version_id,
hav.max_jobs_per_day,
config.daily_result_quota
);
}
hav.reliable = false;
return;
}
}
hav.reliable = true;
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] [HOST#%lu] app version %lu is reliable\n",
g_reply->host.id, hav.app_version_id
);
}
g_wreq->has_reliable_version = true;
}
// decide whether do unreplicated jobs with this app version
//
static void set_trust(DB_HOST_APP_VERSION& hav) {
hav.trusted = false;
if (hav.consecutive_valid < CONS_VALID_UNREPLICATED) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] set_trust: cons valid %d < %d, don't use single replication\n",
hav.consecutive_valid, CONS_VALID_UNREPLICATED
);
}
return;
}
double x = 1./hav.consecutive_valid;
if (drand() > x) hav.trusted = true;
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] set_trust: random choice for cons valid %d: %s\n",
hav.consecutive_valid, hav.trusted?"yes":"no"
);
}
}
static void get_reliability_and_trust() {
// Platforms other than Windows, Linux and Intel Macs need a
// larger set of computers to be marked reliable
//
double multiplier = 1.0;
if (strstr(g_reply->host.os_name,"Windows")
|| strstr(g_reply->host.os_name,"Linux")
|| (strstr(g_reply->host.os_name,"Darwin")
&& !(strstr(g_reply->host.p_vendor,"Power Macintosh"))
)) {
multiplier = 1.0;
} else {
multiplier = 1.8;
}
for (unsigned int i=0; i<g_wreq->host_app_versions.size(); i++) {
DB_HOST_APP_VERSION& hav = g_wreq->host_app_versions[i];
get_reliability_version(hav, multiplier);
set_trust(hav);
}
}
// Compute the max additional disk usage we can impose on the host.
// Depending on the client version, it can either send us
// - d_total and d_free (pre 4 oct 2005)
// - the above plus d_boinc_used_total and d_boinc_used_project
//
double max_allowable_disk() {
HOST host = g_request->host;
GLOBAL_PREFS prefs = g_request->global_prefs;
double x, x1=0, x2=0, x3;
// defaults are from config.xml
// if not there these are used:
// default_disk_min_free_gb = 1
//
if (prefs.disk_min_free_gb < config.default_disk_min_free_gb) {
prefs.disk_min_free_gb = config.default_disk_min_free_gb;
}
// no defaults for total/free disk space (host.d_total, d_free)
// if they're zero, client will get no work.
//
if (host.d_boinc_used_total) {
// The post 4 oct 2005 case.
// Compute the max allowable additional disk usage based on prefs
//
x3 = host.d_free - prefs.disk_min_free_gb*GIGA;
// may be negative
x = x3;
int which = 3;
if (prefs.disk_max_used_pct > 0) {
x2 = host.d_total * prefs.disk_max_used_pct / 100.0
- host.d_boinc_used_total;
if (x2 < x) {
x = x2;
which = 2;
}
}
if (prefs.disk_max_used_gb > 0) {
x1 = prefs.disk_max_used_gb*GIGA - host.d_boinc_used_total;
if (x1 < x) {
x = x1;
which = 1;
}
}
// see which bound is the most stringent
// (for client notification in sched_locality.cpp)
//
if (which == 1) {
g_reply->disk_limits.max_used = x;
} else if (which == 2) {
g_reply->disk_limits.max_frac = x;
} else {
g_reply->disk_limits.min_free = x;
}
} else {
// here we don't know how much space BOINC is using.
// so we're kinda screwed.
// All we can do is assume that BOINC is using zero space.
// We can't honor the max_used for max_used_pct preferences.
// We can only honor the min_free pref.
//
x = host.d_free - prefs.disk_min_free_gb*GIGA; // may be negative
g_reply->disk_limits.min_free = x;
x1 = x2 = x3 = 0;
}
if (x < 0) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] No disk space available: disk_max_used_gb %.2fGB disk_max_used_pct %.2f disk_min_free_gb %.2fGB\n",
prefs.disk_max_used_gb,
prefs.disk_max_used_pct,
prefs.disk_min_free_gb
);
log_messages.printf(MSG_NORMAL,
"[send] No disk space available: host.d_total %.2fGB host.d_free %.2fGB host.d_boinc_used_total %.2fGB\n",
host.d_total/GIGA,
host.d_free/GIGA,
host.d_boinc_used_total/GIGA
);
log_messages.printf(MSG_NORMAL,
"[send] No disk space available: x1 %.2fGB x2 %.2fGB x3 %.2fGB x %.2fGB\n",
x1/GIGA, x2/GIGA, x3/GIGA, x/GIGA
);
}
g_wreq->disk.set_insufficient(-x);
x = 0;
}
return x;
}
static double estimate_duration_unscaled(WORKUNIT& wu, BEST_APP_VERSION& bav) {
double rsc_fpops_est = wu.rsc_fpops_est;
if (rsc_fpops_est <= 0) rsc_fpops_est = 1e12;
return rsc_fpops_est/bav.host_usage.projected_flops;
}
// Compute cpu_available_frac and gpu_available_frac.
// These are based on client-supplied data, so do sanity checks
//
#define FRAC_MIN 0.1
static inline void clamp_frac(double& frac, const char* name) {
if (frac > 1) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] %s=%f; setting to 1\n", name, frac
);
}
frac = 1;
} else if (frac < FRAC_MIN) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] %s=%f; setting to %f\n", name, frac, FRAC_MIN
);
}
frac = FRAC_MIN;
}
}
static inline void get_available_fracs() {
if (g_request->core_client_version<=41900) {
g_wreq->cpu_available_frac = g_reply->host.on_frac;
g_wreq->gpu_available_frac = g_reply->host.on_frac; // irrelevant
} else {
g_wreq->cpu_available_frac = g_reply->host.active_frac * g_reply->host.on_frac;
g_wreq->gpu_available_frac = g_reply->host.gpu_active_frac * g_reply->host.on_frac;
}
clamp_frac(g_wreq->cpu_available_frac, "CPU available fraction");
clamp_frac(g_wreq->gpu_available_frac, "GPU available fraction");
}
double available_frac(BEST_APP_VERSION& bav) {
if (bav.host_usage.uses_gpu()) {
return g_wreq->gpu_available_frac;
} else {
return g_wreq->cpu_available_frac;
}
}
// estimate the amount of real time to complete this WU,
// taking into account active_frac etc.
// Note: don't factor in resource_share_fraction.
// The core client doesn't necessarily round-robin across all projects.
//
double estimate_duration(WORKUNIT& wu, BEST_APP_VERSION& bav) {
double edu = estimate_duration_unscaled(wu, bav);
double ed = edu/available_frac(bav);
if (config.debug_send_job) {
log_messages.printf(MSG_NORMAL,
"[send_job] est. duration for WU %lu: unscaled %.2f scaled %.2f\n",
wu.id, edu, ed
);
}
return ed;
}
void update_n_jobs_today() {
for (unsigned int i=0; i<g_wreq->host_app_versions.size(); i++) {
DB_HOST_APP_VERSION& hav = g_wreq->host_app_versions[i];
update_quota(hav);
}
}
static inline void update_estimated_delay(BEST_APP_VERSION& bav, double dt) {
int pt = bav.host_usage.proc_type;
if (pt == PROC_TYPE_CPU) {
g_request->cpu_estimated_delay += dt*bav.host_usage.avg_ncpus/g_request->host.p_ncpus;
} else {
COPROC* cp = g_request->coprocs.proc_type_to_coproc(pt);
cp->estimated_delay += dt*bav.host_usage.gpu_usage/cp->count;
}
}
// insert "text" right after "after" in the given buffer
//
static int insert_after(char* buffer, const char* after, const char* text) {
char* p;
char temp[BLOB_SIZE];
if (strlen(buffer) + strlen(text) >= BLOB_SIZE-1) {
log_messages.printf(MSG_CRITICAL,
"insert_after: overflow: %d %d\n",
(int)strlen(buffer),
(int)strlen(text)
);
return ERR_BUFFER_OVERFLOW;
}
p = strstr(buffer, after);
if (!p) {
log_messages.printf(MSG_CRITICAL,
"insert_after: %s not found in %s\n", after, buffer
);
return ERR_XML_PARSE;
}
p += strlen(after);
// coverity[fixed_size_dest]
strcpy(temp, p);
strcpy(p, text);
strcat(p, temp);
return 0;
}
// add elements to WU's xml_doc,
// in preparation for sending it to a client
//
static int insert_wu_tags(WORKUNIT& wu, APP& app) {
char buf[BLOB_SIZE];
sprintf(buf,
" <rsc_fpops_est>%f</rsc_fpops_est>\n"
" <rsc_fpops_bound>%f</rsc_fpops_bound>\n"
" <rsc_memory_bound>%f</rsc_memory_bound>\n"
" <rsc_disk_bound>%f</rsc_disk_bound>\n"
" <name>%s</name>\n"
" <app_name>%s</app_name>\n",
wu.rsc_fpops_est,
wu.rsc_fpops_bound,
wu.rsc_memory_bound,
wu.rsc_disk_bound,
wu.name,
app.name
);
if (!empty(wu.keywords)) {
char buf2[1024];
sprintf(buf2,
" <job_keyword_ids>%s</job_keyword_ids>\n",
wu.keywords
);
strcat(buf, buf2);
}
return insert_after(wu.xml_doc, "<workunit>\n", buf);
}
// Add the given workunit, app, and app version to a reply.
//
static int add_wu_to_reply(
WORKUNIT& wu, SCHEDULER_REPLY&, APP* app, BEST_APP_VERSION* bavp
) {
int retval;
WORKUNIT wu2, wu3;
APP_VERSION* avp = bavp->avp;
// add the app, app_version, and workunit to the reply,
// but only if they aren't already there
//
if (avp) {
APP_VERSION av2=*avp, *avp2=&av2;
if (strlen(config.replace_download_url_by_timezone)) {
process_av_timezone(avp, av2);
}
g_reply->insert_app_unique(*app);
av2.bavp = bavp;
g_reply->insert_app_version_unique(*avp2);
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] Sending app_version %s %lu %d %s; projected %.2f GFLOPS\n",
app->name,
avp2->platformid, avp2->version_num, avp2->plan_class,
bavp->host_usage.projected_flops/1e9
);
}
}
// modify the WU's xml_doc; add <name>, <rsc_*> etc.
//
wu2 = wu; // make copy since we're going to modify its XML field
// check if plan class specified memory usage
//
if (bavp->host_usage.mem_usage) {
wu2.rsc_memory_bound = bavp->host_usage.mem_usage;
}
// adjust FPOPS figures for anonymous platform
//
if (bavp->cavp) {
wu2.rsc_fpops_est *= bavp->cavp->rsc_fpops_scale;
wu2.rsc_fpops_bound *= bavp->cavp->rsc_fpops_scale;
}
retval = insert_wu_tags(wu2, *app);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"insert_wu_tags failed: %s\n", boincerror(retval)
);
return retval;
}
wu3 = wu2;
if (strlen(config.replace_download_url_by_timezone)) {
process_wu_timezone(wu2, wu3);
}
g_reply->insert_workunit_unique(wu3);
// switch to tighter policy for estimating delay
//
return 0;
}
// add <name> tags to result's xml_doc_in
//
static int insert_name_tags(RESULT& result, WORKUNIT const& wu) {
char buf[256];
int retval;
sprintf(buf, "<name>%s</name>\n", result.name);
retval = insert_after(result.xml_doc_in, "<result>\n", buf);
if (retval) return retval;
sprintf(buf, "<wu_name>%s</wu_name>\n", wu.name);
retval = insert_after(result.xml_doc_in, "<result>\n", buf);
if (retval) return retval;
return 0;
}
static int insert_deadline_tag(RESULT& result) {
char buf[256];
sprintf(buf, "<report_deadline>%d</report_deadline>\n", result.report_deadline);
int retval = insert_after(result.xml_doc_in, "<result>\n", buf);
if (retval) return retval;
return 0;
}
// update workunit fields when send an instance of it:
// - transition time
// - app_version_id, if app uses homogeneous app version
// - hr_class, if we're using HR
//
// In the latter two cases, the update is conditional on the field
// fields either being zero or the desired value.
// Some other scheduler instance might have updated it since we read the WU,
// and the transitioner might have set it to zero.
//
int update_wu_on_send(WORKUNIT wu, time_t x, APP& app, BEST_APP_VERSION& bav) {
DB_WORKUNIT dbwu;
char buf[256], buf2[256], where_clause[256];
int retval;
dbwu.id = wu.id;
// SQL note: can't use min() here
//
sprintf(buf,
"transition_time=if(transition_time<%d, transition_time, %d)",
(int)x, (int)x
);
strcpy(where_clause, "");
if (app.homogeneous_app_version) {
sprintf(buf2, ", app_version_id=%lu", bav.avp->id);
strcat(buf, buf2);
sprintf(where_clause,
"(app_version_id=0 or app_version_id=%lu)", bav.avp->id
);
}
if (app_hr_type(app)) {
int host_hr_class = hr_class(g_request->host, app_hr_type(app));
sprintf(buf2, ", hr_class=%d", host_hr_class);
strcat(buf, buf2);
if (strlen(where_clause)) {
strcat(where_clause, " and ");
}
sprintf(buf2, "(hr_class=0 or hr_class=%d)", host_hr_class);
strcat(where_clause, buf2);
}
retval = dbwu.update_field(buf, strlen(where_clause)?where_clause:NULL);
if (retval) return retval;
if (boinc_db.affected_rows() != 1) {
return ERR_DB_NOT_FOUND;
}
return 0;
}
// return true iff a result for same WU is already being sent
//
bool wu_already_in_reply(WORKUNIT& wu) {
unsigned int i;
for (i=0; i<g_reply->results.size(); i++) {
if (wu.id == g_reply->results[i].workunitid) {
return true;
}
}
return false;
}
void lock_sema() {
lock_semaphore(sema_key);
}
void unlock_sema() {
unlock_semaphore(sema_key);
}
static inline bool have_apps(int pt) {
if (g_wreq->anonymous_platform) {
return g_wreq->client_has_apps_for_proc_type[pt];
} else {
return ssp->have_apps_for_proc_type[pt];
}
}
// return true if additional work is needed,
// and there's disk space left,
// and we haven't exceeded result per RPC limit,
// and we haven't exceeded results per day limit
//
bool work_needed(bool locality_sched) {
if (locality_sched) {
// if we've failed to send a result because of a transient condition,
// return false to preserve invariant
//
if (g_wreq->disk.insufficient) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] stopping work search - insufficient disk space\n"
);
}
return false;
}
if (g_wreq->speed.insufficient) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] stopping work search - host too slow\n"
);
}
return false;
}
if (g_wreq->mem.insufficient) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] stopping work search - insufficient memory\n"
);
}
return false;
}
if (g_wreq->no_allowed_apps_available) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] stopping work search - no locality app selected\n"
);
}
return false;
}
}
// check user-specified project prefs limit on # of jobs in progress
//
int mj = g_wreq->project_prefs.max_jobs_in_progress;
if (mj && config.max_jobs_in_progress.project_limits.total.njobs >= mj) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] user project preferences job limit exceeded\n"
);
}
g_wreq->max_jobs_on_host_exceeded = true;
return false;
}
// check config.xml limits on in-progress jobs
//
bool some_type_allowed = false;
for (int i=0; i<NPROC_TYPES; i++) {
if (!have_apps(i)) continue;
if (config.max_jobs_in_progress.exceeded(NULL, i)) {
if (config.debug_quota) {
log_messages.printf(MSG_NORMAL,
"[quota] reached limit on %s jobs in progress\n",
proc_type_name(i)
);
config.max_jobs_in_progress.print_log();
}
g_wreq->clear_req(i);
g_wreq->max_jobs_on_host_proc_type_exceeded[i] = true;
} else {
some_type_allowed = true;
}
}
if (!some_type_allowed) {
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] config.xml max_jobs_in_progress limit exceeded\n"
);
}
g_wreq->max_jobs_on_host_exceeded = true;
return false;
}
// see if we've reached max jobs per RPC
//
if (g_wreq->njobs_sent >= g_wreq->max_jobs_per_rpc) {
if (config.debug_quota) {
log_messages.printf(MSG_NORMAL,
"[quota] stopping work search - njobs %d >= max_jobs_per_rpc %d\n",
g_wreq->njobs_sent, g_wreq->max_jobs_per_rpc
);
}
return false;
}
#if 0
if (config.debug_send) {
char buf[256], buf2[256];
strcpy(buf, "");
for (int i=0; i<NPROC_TYPES; i++) {
sprintf(buf2, " %s (%.2f, %.2f)",
proc_type_name(i),
g_wreq->req_secs[i],
g_wreq->req_instances[i]
);
strcat(buf, buf2);
}
log_messages.printf(MSG_NORMAL,
"[send] work_needed: spec req %d sec to fill %.2f; %s\n",
g_wreq->rsc_spec_request,
g_wreq->seconds_to_fill,
buf
);
}
#endif
if (g_wreq->rsc_spec_request) {
for (int i=0; i<NPROC_TYPES; i++) {
if (g_wreq->need_proc_type(i) && have_apps(i)) {
return true;
}
}
} else {
if (g_wreq->seconds_to_fill > 0) {
return true;
}
}
if (config.debug_send) {
log_messages.printf(MSG_NORMAL, "[send] don't need more work\n");
}
return false;
}
// return the app version ID, or -2/-3/-4 if anonymous platform
//
inline static DB_ID_TYPE get_app_version_id(BEST_APP_VERSION* bavp) {
if (bavp->avp) {
return bavp->avp->id;
} else {
return bavp->cavp->host_usage.resource_type();
}
}
int add_result_to_reply(
SCHED_DB_RESULT& result,
WORKUNIT& wu,
BEST_APP_VERSION* bavp,
bool locality_scheduling
) {
int retval;
bool resent_result = false;
APP* app = ssp->lookup_app(wu.appid);
result.hostid = g_reply->host.id;
result.userid = g_reply->user.id;
result.sent_time = time(0);
result.report_deadline = result.sent_time + wu.delay_bound;
result.flops_estimate = bavp->host_usage.peak_flops;
result.app_version_id = get_app_version_id(bavp);
// update WU DB record.
// This can fail in normal operation
// (other scheduler already updated hr_class or app_version_id)
// so do it before updating the result.
//
retval = update_wu_on_send(
wu, result.report_deadline + config.report_grace_period, *app, *bavp
);
if (retval == ERR_DB_NOT_FOUND) {
log_messages.printf(MSG_NORMAL,
"add_result_to_reply: WU already sent to other HR class or app version\n"
);
return retval;
} else if (retval) {
log_messages.printf(MSG_CRITICAL,
"add_result_to_reply: WU update failed: %d\n",
retval
);
return retval;
}
// update result DB record.
// This can also fail in normal operation.
// In this case, in principle we should undo
// the changes we just made to the WU (or use a transaction)
// but I don't think it actually matters.
//
int old_server_state = result.server_state;
if (result.server_state != RESULT_SERVER_STATE_IN_PROGRESS) {
// We're sending this result for the first time
//
result.server_state = RESULT_SERVER_STATE_IN_PROGRESS;
} else {
// Result was already sent to this host but was lost,
// so we're resending it.
//
resent_result = true;
if (config.debug_send) {
log_messages.printf(MSG_NORMAL,
"[send] [RESULT#%lu] [HOST#%lu] (resend lost work)\n",
result.id, g_reply->host.id
);
}
}
retval = result.mark_as_sent(old_server_state, config.report_grace_period);
if (retval == ERR_DB_NOT_FOUND) {
log_messages.printf(MSG_CRITICAL,
"[RESULT#%lu] [HOST#%lu]: CAN'T SEND, already sent to another host\n",
result.id, g_reply->host.id
);
} else if (retval) {
log_messages.printf(MSG_CRITICAL,
"add_result_to_reply: can't update result: %s\n", boincerror(retval)
);
}
if (retval) return retval;
// done with DB updates.
//
retval = add_wu_to_reply(wu, *g_reply, app, bavp);
if (retval) return retval;
// Adjust available disk space.
// In the locality scheduling locality case,
// reduce the available space by less than the workunit rsc_disk_bound,
// if the host already has the file or the file was not already sent.
//
if (!locality_scheduling || decrement_disk_space_locality(wu)) {
g_wreq->disk_available -= wu.rsc_disk_bound;
}
double est_dur = estimate_duration(wu, *bavp);
if (config.debug_send) {
double max_time = wu.rsc_fpops_bound / bavp->host_usage.projected_flops;
char buf1[64],buf2[64];
secs_to_hmsf(est_dur, buf1);
secs_to_hmsf(max_time, buf2);
log_messages.printf(MSG_NORMAL,
"[send] [HOST#%lu] sending [RESULT#%lu %s] (est. dur. %.2fs (%s)) (max time %.2fs (%s))\n",
g_reply->host.id, result.id, result.name, est_dur, buf1, max_time, buf2
);
}
// The following overwrites the result's xml_doc field.
// But that's OK cuz we're done with DB updates
//
retval = insert_name_tags(result, wu);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"add_result_to_reply: can't insert name tags: %d\n",
retval
);
return retval;