forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_state.cpp
2136 lines (1919 loc) · 62.2 KB
/
client_state.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) 2008 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/>.
#ifdef _WIN32
#include "boinc_win.h"
#else
#include "config.h"
#include <unistd.h>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstdarg>
#include <cstring>
#include <math.h>
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#endif
#ifdef __EMX__
#define INCL_DOS
#include <os2.h>
#endif
#include "cpp.h"
#include "error_numbers.h"
#include "filesys.h"
#include "parse.h"
#include "str_replace.h"
#include "str_util.h"
#include "util.h"
#ifdef _WIN32
#include "run_app_windows.h"
#endif
#include "app_config.h"
#include "async_file.h"
#include "client_msgs.h"
#include "cs_notice.h"
#include "cs_proxy.h"
#include "cs_trickle.h"
#include "file_names.h"
#include "hostinfo.h"
#include "hostinfo_network.h"
#include "http_curl.h"
#include "network.h"
#include "project.h"
#include "result.h"
#include "sandbox.h"
#include "shmem.h"
#include "client_state.h"
using std::max;
CLIENT_STATE gstate;
COPROCS coprocs;
CLIENT_STATE::CLIENT_STATE()
: lookup_website_op(&gui_http),
get_current_version_op(&gui_http),
get_project_list_op(&gui_http),
acct_mgr_op(&gui_http)
{
http_ops = new HTTP_OP_SET();
file_xfers = new FILE_XFER_SET(http_ops);
pers_file_xfers = new PERS_FILE_XFER_SET(file_xfers);
#ifndef SIM
scheduler_op = new SCHEDULER_OP(http_ops);
#endif
client_state_dirty = false;
clock_change = false;
check_all_logins = false;
cmdline_gui_rpc_port = 0;
run_cpu_benchmarks = false;
file_xfer_giveup_period = PERS_GIVEUP;
had_or_requested_work = false;
tasks_suspended = false;
network_suspended = false;
suspend_reason = 0;
network_suspend_reason = 0;
core_client_version.major = BOINC_MAJOR_VERSION;
core_client_version.minor = BOINC_MINOR_VERSION;
core_client_version.release = BOINC_RELEASE;
#ifdef BOINC_PRERELEASE
core_client_version.prerelease = true;
#else
core_client_version.prerelease = false;
#endif
exit_after_app_start_secs = 0;
app_started = 0;
exit_before_upload = false;
show_projects = false;
strcpy(detach_project_url, "");
strcpy(main_host_venue, "");
strcpy(attach_project_url, "");
strcpy(attach_project_auth, "");
cpu_run_mode.set(RUN_MODE_AUTO, 0);
gpu_run_mode.set(RUN_MODE_AUTO, 0);
network_run_mode.set(RUN_MODE_AUTO, 0);
started_by_screensaver = false;
requested_exit = false;
os_requested_suspend = false;
os_requested_suspend_time = 0;
cleanup_completed = false;
in_abort_sequence = false;
master_fetch_period = MASTER_FETCH_PERIOD;
retry_cap = RETRY_CAP;
master_fetch_retry_cap = MASTER_FETCH_RETRY_CAP;
master_fetch_interval = MASTER_FETCH_INTERVAL;
sched_retry_delay_min = SCHED_RETRY_DELAY_MIN;
sched_retry_delay_max = SCHED_RETRY_DELAY_MAX;
pers_retry_delay_min = PERS_RETRY_DELAY_MIN;
pers_retry_delay_max = PERS_RETRY_DELAY_MAX;
pers_giveup = PERS_GIVEUP;
executing_as_daemon = false;
redirect_io = false;
disable_graphics = false;
cant_write_state_file = false;
benchmarks_running = false;
rec_interval_start = 0;
retry_shmem_time = 0;
must_schedule_cpus = true;
no_gui_rpc = false;
new_version_check_time = 0;
all_projects_list_check_time = 0;
detach_console = false;
#ifdef SANDBOX
g_use_sandbox = true; // User can override with -insecure command-line arg
#endif
launched_by_manager = false;
initialized = false;
last_wakeup_time = dtime();
#ifdef _WIN32
have_sysmon_msg = false;
#endif
}
void CLIENT_STATE::show_host_info() {
char buf[256], buf2[256];
nbytes_to_string(host_info.m_cache, 0, buf, sizeof(buf));
msg_printf(NULL, MSG_INFO,
"Processor: %d %s %s",
host_info.p_ncpus, host_info.p_vendor, host_info.p_model
);
if (ncpus != host_info.p_ncpus) {
msg_printf(NULL, MSG_INFO, "Using %d CPUs", ncpus);
}
#if 0
if (host_info.m_cache > 0) {
msg_printf(NULL, MSG_INFO,
"Processor: %s cache",
buf
);
}
#endif
msg_printf(NULL, MSG_INFO,
"Processor features: %s", host_info.p_features
);
#ifdef __APPLE__
int major, minor, rev;
sscanf(host_info.os_version, "%d.%d.%d", &major, &minor, &rev);
msg_printf(NULL, MSG_INFO,
"OS: Mac OS X 10.%d.%d (%s %s)", major-4, minor,
host_info.os_name, host_info.os_version
);
#else
msg_printf(NULL, MSG_INFO,
"OS: %s: %s", host_info.os_name, host_info.os_version
);
#endif
nbytes_to_string(host_info.m_nbytes, 0, buf, sizeof(buf));
nbytes_to_string(host_info.m_swap, 0, buf2, sizeof(buf2));
msg_printf(NULL, MSG_INFO,
"Memory: %s physical, %s virtual",
buf, buf2
);
nbytes_to_string(host_info.d_total, 0, buf, sizeof(buf));
nbytes_to_string(host_info.d_free, 0, buf2, sizeof(buf2));
msg_printf(NULL, MSG_INFO, "Disk: %s total, %s free", buf, buf2);
int tz = host_info.timezone/3600;
msg_printf(0, MSG_INFO, "Local time is UTC %s%d hours",
tz<0?"":"+", tz
);
if (strlen(host_info.virtualbox_version)) {
msg_printf(NULL, MSG_INFO,
"VirtualBox version: %s",
host_info.virtualbox_version
);
}
}
int rsc_index(const char* name) {
const char* nm = strcmp(name, "CUDA")?name:GPU_TYPE_NVIDIA;
// handle old state files
for (int i=0; i<coprocs.n_rsc; i++) {
if (!strcmp(nm, coprocs.coprocs[i].type)) {
return i;
}
}
return -1;
}
const char* rsc_name(int i) {
return coprocs.coprocs[i].type;
}
// set no_X_apps for anonymous platform project
//
static void check_no_apps(PROJECT* p) {
for (int i=0; i<coprocs.n_rsc; i++) {
p->no_rsc_apps[i] = true;
}
for (unsigned int i=0; i<gstate.app_versions.size(); i++) {
APP_VERSION* avp = gstate.app_versions[i];
if (avp->project != p) continue;
p->no_rsc_apps[avp->gpu_usage.rsc_type] = false;
}
}
// alert user if any jobs need more RAM than available
//
static void check_too_large_jobs() {
unsigned int i, j;
double m = gstate.max_available_ram();
for (i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
bool found = false;
for (j=0; j<gstate.results.size(); j++) {
RESULT* rp = gstate.results[j];
if (rp->project == p && rp->wup->rsc_memory_bound > m) {
found = true;
break;
}
}
if (found) {
msg_printf(p, MSG_USER_ALERT,
_("Some tasks need more memory than allowed by your preferences. Please check the preferences.")
);
}
}
}
// Something has failed N times.
// Calculate an exponential backoff between MIN and MAX
//
double calculate_exponential_backoff(int n, double MIN, double MAX) {
double x = pow(2, (double)n);
x *= MIN;
if (x > MAX) x = MAX;
x *= (.5 + .5*drand());
return x;
}
#ifndef SIM
void CLIENT_STATE::set_now() {
double x = dtime();
// if time went backward significantly, clear delays
//
clock_change = false;
if (x < (now-60)) {
clock_change = true;
clear_absolute_times();
}
#ifdef _WIN32
// On Win, check for evidence that we're awake after a suspension
// (in case we missed the event announcing this)
//
if (os_requested_suspend) {
if (x > now+10) {
msg_printf(0, MSG_INFO, "Resuming after OS suspension");
os_requested_suspend = false;
} else if (x > os_requested_suspend_time + 300) {
msg_printf(0, MSG_INFO, "Resuming after OS suspension");
os_requested_suspend = false;
}
}
#endif
now = x;
}
int CLIENT_STATE::init() {
int retval;
unsigned int i;
char buf[256];
PROJECT* p;
srand((unsigned int)time(0));
now = dtime();
scheduler_op->url_random = drand();
notices.init();
daily_xfer_history.init();
time_stats.init();
detect_platforms();
time_stats.start();
msg_printf(
NULL, MSG_INFO, "Starting BOINC client version %d.%d.%d for %s%s",
core_client_version.major,
core_client_version.minor,
core_client_version.release,
get_primary_platform(),
#ifdef _DEBUG
" (DEBUG)"
#else
""
#endif
);
if (core_client_version.prerelease) {
msg_printf(NULL, MSG_INFO,
"This a development version of BOINC and may not function properly"
);
}
log_flags.show();
msg_printf(NULL, MSG_INFO, "Libraries: %s", curl_version());
if (executing_as_daemon) {
msg_printf(NULL, MSG_INFO, "Running as a daemon");
}
relative_to_absolute("", buf);
msg_printf(NULL, MSG_INFO, "Data directory: %s", buf);
#ifdef _WIN32
DWORD buf_size = sizeof(buf);
LPTSTR pbuf = buf;
GetUserName(pbuf, &buf_size);
msg_printf(NULL, MSG_INFO, "Running under account %s", pbuf);
#endif
parse_account_files();
parse_statistics_files();
host_info.get_host_info();
set_ncpus();
show_host_info();
// check for GPUs.
//
for (int j=1; j<coprocs.n_rsc; j++) {
msg_printf(NULL, MSG_INFO, "GPU specified in cc_config.xml: %d %s",
coprocs.coprocs[j].count,
coprocs.coprocs[j].type
);
}
if (!config.no_gpus
#ifdef _WIN32
&& !executing_as_daemon
#endif
) {
vector<string> descs;
vector<string> warnings;
coprocs.get(
config.use_all_gpus, descs, warnings, config.ignore_gpu_instance
);
for (i=0; i<descs.size(); i++) {
msg_printf(NULL, MSG_INFO, "%s", descs[i].c_str());
}
if (log_flags.coproc_debug) {
for (i=0; i<warnings.size(); i++) {
msg_printf(NULL, MSG_INFO, "%s", warnings[i].c_str());
}
}
#if 0
msg_printf(NULL, MSG_INFO, "Faking an NVIDIA GPU");
coprocs.nvidia.fake(18000, 512*MEGA, 490*MEGA, 1);
#endif
#if 0
msg_printf(NULL, MSG_INFO, "Faking an ATI GPU");
coprocs.ati.fake(512*MEGA, 256*MEGA, 2);
#endif
#if 0
msg_printf(NULL, MSG_INFO, "Faking an Intel GPU");
coprocs.intel_gpu.fake(512*MEGA, 256*MEGA, 2);
#endif
}
if (coprocs.have_nvidia()) {
if (rsc_index(GPU_TYPE_NVIDIA)>0) {
msg_printf(NULL, MSG_INFO, "NVIDIA GPU info taken from cc_config.xml");
} else {
coprocs.add(coprocs.nvidia);
}
}
if (coprocs.have_ati()) {
if (rsc_index(GPU_TYPE_ATI)>0) {
msg_printf(NULL, MSG_INFO, "ATI GPU info taken from cc_config.xml");
} else {
coprocs.add(coprocs.ati);
}
}
if (coprocs.have_intel_gpu()) {
if (rsc_index(GPU_TYPE_INTEL)>0) {
msg_printf(NULL, MSG_INFO, "INTEL GPU info taken from cc_config.xml");
} else {
coprocs.add(coprocs.intel_gpu);
}
}
host_info.coprocs = coprocs;
if (coprocs.none() ) {
msg_printf(NULL, MSG_INFO, "No usable GPUs found");
}
set_no_rsc_config();
// check for app_info.xml file in project dirs.
// If find, read app info from there, set project.anonymous_platform
// - this must follow coproc.get() (need to know if GPUs are present)
// - this is being done before CPU speed has been read,
// so we'll need to patch up avp->flops later;
//
check_anonymous();
// first time, set p_fpops nonzero to avoid div by zero
//
cpu_benchmarks_set_defaults();
// Parse the client state file,
// ignoring any <project> tags (and associated stuff)
// for projects with no account file
//
parse_state_file();
// check for app_config.xml files in project dirs
//
check_app_config();
// this needs to go after parse_state_file because
// GPU exclusions refer to projects
//
config.show();
// inform the user if there's a newer version of client
//
newer_version_startup_check();
// parse account files again,
// now that we know the host's venue on each project
//
parse_account_files_venue();
// fill in p->no_X_apps for anon platform projects
//
for (i=0; i<projects.size(); i++) {
p = projects[i];
if (p->anonymous_platform) {
check_no_apps(p);
}
}
// fill in avp->flops for anonymous platform projects
//
for (i=0; i<app_versions.size(); i++) {
APP_VERSION* avp = app_versions[i];
if (!avp->flops) {
if (!avp->avg_ncpus) {
avp->avg_ncpus = 1;
}
avp->flops = avp->avg_ncpus * host_info.p_fpops;
// for GPU apps, use conservative estimate:
// assume GPU runs at 10X peak CPU speed
//
if (avp->gpu_usage.rsc_type) {
avp->flops += avp->gpu_usage.usage * 10 * host_info.p_fpops;
}
}
}
process_gpu_exclusions();
check_clock_reset();
// Check to see if we can write the state file.
//
retval = write_state_file();
if (retval) {
msg_printf_notice(NULL, false,
"http://boinc.berkeley.edu/manager_links.php?target=notice&controlid=statefile",
_("Couldn't write state file; check directory permissions")
);
cant_write_state_file = true;
}
// scan user prefs; create file records
//
parse_preferences_for_user_files();
if (log_flags.state_debug) {
print_summary();
}
do_cmdline_actions();
// check if version or platform has changed.
// Either of these is evidence that we're running a different
// client than previously.
//
bool new_client = false;
if ((core_client_version.major != old_major_version)
|| (core_client_version.minor != old_minor_version)
|| (core_client_version.release != old_release)
) {
msg_printf(NULL, MSG_INFO,
"Version change (%d.%d.%d -> %d.%d.%d)",
old_major_version, old_minor_version, old_release,
core_client_version.major,
core_client_version.minor,
core_client_version.release
);
new_client = true;
}
if (statefile_platform_name.size() && strcmp(get_primary_platform(), statefile_platform_name.c_str())) {
msg_printf(NULL, MSG_INFO,
"Platform changed from %s to %s",
statefile_platform_name.c_str(), get_primary_platform()
);
new_client = true;
}
// if new version of client,
// - run CPU benchmarks
// - get new project list
// - contact reference site (or some project) to trigger firewall alert
//
if (new_client) {
run_cpu_benchmarks = true;
all_projects_list_check_time = 0;
if (config.dont_contact_ref_site) {
if (projects.size() > 0) {
projects[0]->master_url_fetch_pending = true;
}
} else {
net_status.need_to_contact_reference_site = true;
}
}
log_show_projects();
read_global_prefs();
// do CPU scheduler and work fetch
//
request_schedule_cpus("Startup");
request_work_fetch("Startup");
work_fetch.init();
rec_interval_start = now;
// set up the project and slot directories
//
delete_old_slot_dirs();
retval = make_project_dirs();
if (retval) return retval;
active_tasks.init();
active_tasks.report_overdue();
active_tasks.handle_upload_files();
had_or_requested_work = (active_tasks.active_tasks.size() > 0);
// Just to be on the safe side; something may have been modified
//
set_client_state_dirty("init");
// check for initialization files
//
acct_mgr_info.init();
project_init.init();
// set up for handling GUI RPCs
//
if (!no_gui_rpc) {
// When we're running at boot time,
// it may be a few seconds before we can socket/bind/listen.
// So retry a few times.
//
for (i=0; i<30; i++) {
bool last_time = (i==29);
retval = gui_rpcs.init(last_time);
if (!retval) break;
boinc_sleep(1.0);
}
if (retval) return retval;
}
#ifdef SANDBOX
get_project_gid();
#endif
#ifdef _WIN32
get_sandbox_account_service_token();
if (sandbox_account_service_token != NULL) g_use_sandbox = true;
#endif
check_file_existence();
if (!boinc_file_exists(ALL_PROJECTS_LIST_FILENAME)) {
all_projects_list_check_time = 0;
}
#ifdef ENABLE_AUTO_UPDATE
auto_update.init();
#endif
http_ops->cleanup_temp_files();
// must parse env vars after parsing state file
// otherwise items will get overwritten with state file info
//
parse_env_vars();
// do this after parsing env vars
//
proxy_info_startup();
if (gstate.projects.size() == 0) {
msg_printf(NULL, MSG_INFO,
"This computer is not attached to any projects"
);
msg_printf(NULL, MSG_INFO,
"Visit http://boinc.berkeley.edu for instructions"
);
}
// get list of BOINC projects occasionally,
// and initialize notice RSS feeds
//
if (!config.no_info_fetch) {
all_projects_list_check();
notices.init_rss();
}
// warn user if some jobs need more memory than available
//
check_too_large_jobs();
// initialize project priorities (for the GUI, in case we're suspended)
//
project_priority_init(false);
initialized = true;
return 0;
}
static void double_to_timeval(double x, timeval& t) {
t.tv_sec = (int)x;
t.tv_usec = (int)(1000000*(x - (int)x));
}
FDSET_GROUP curl_fds;
FDSET_GROUP gui_rpc_fds;
FDSET_GROUP all_fds;
// Spend x seconds either doing I/O (if possible) or sleeping.
//
void CLIENT_STATE::do_io_or_sleep(double x) {
int n;
struct timeval tv;
set_now();
double end_time = now + x;
//int loops = 0;
while (1) {
bool action = do_async_file_ops();
curl_fds.zero();
gui_rpc_fds.zero();
http_ops->get_fdset(curl_fds);
all_fds = curl_fds;
gui_rpcs.get_fdset(gui_rpc_fds, all_fds);
double_to_timeval(action?0:x, tv);
n = select(
all_fds.max_fd+1,
&all_fds.read_fds, &all_fds.write_fds, &all_fds.exc_fds,
&tv
);
//printf("select in %d out %d\n", all_fds.max_fd, n);
// Note: curl apparently likes to have curl_multi_perform()
// (called from net_xfers->got_select())
// called pretty often, even if no descriptors are enabled.
// So do the "if (n==0) break" AFTER the got_selects().
http_ops->got_select(all_fds, x);
gui_rpcs.got_select(all_fds);
if (!action && n==0) break;
#if 0
// Limit number of times thru this loop.
// Can get stuck in while loop, if network isn't available,
// DNS lookups tend to eat CPU cycles.
//
if (loops++ > 99) {
boinc_sleep(.01);
#ifdef __EMX__
DosSleep(0);
#endif
break;
}
#endif
set_now();
if (now > end_time) break;
x = end_time - now;
}
}
#define POLL_ACTION(name, func) \
do { if (func()) { \
++actions; \
if (log_flags.poll_debug) { \
msg_printf(0, MSG_INFO, "[poll] CLIENT_STATE::poll_slow_events(): " #name "\n"); \
} \
} } while(0)
// Poll the client's finite-state machines
// possibly triggering state transitions.
// Returns true if something happened
// (in which case should call this again immediately)
//
bool CLIENT_STATE::poll_slow_events() {
int actions = 0, retval;
static int last_suspend_reason=0;
static bool tasks_restarted = false;
static bool first=true;
double old_now = now;
#ifdef __APPLE__
double idletime;
#endif
set_now();
if (cant_write_state_file) {
return false;
}
if (now - old_now > POLL_INTERVAL*10) {
if (log_flags.network_status_debug) {
msg_printf(0, MSG_INFO,
"[network_status] woke up after %f seconds",
now - old_now
);
}
last_wakeup_time = now;
}
if (should_run_cpu_benchmarks() && !benchmarks_running) {
run_cpu_benchmarks = false;
start_cpu_benchmarks();
}
#ifdef _WIN32
if (have_sysmon_msg) {
msg_printf(NULL, MSG_INFO, sysmon_msg);
have_sysmon_msg = false;
}
#endif
bool old_user_active = user_active;
user_active = !host_info.users_idle(
check_all_logins, global_prefs.idle_time_to_run
#ifdef __APPLE__
, &idletime
#endif
);
if (user_active != old_user_active) {
request_schedule_cpus("Idle state change");
}
#if 0
// NVIDIA provides an interface for finding if a GPU is
// running a graphics app. ATI doesn't as far as I know
//
if (host_info.have_nvidia() && user_active && !global_prefs.run_gpu_if_user_active) {
if (host_info.coprocs.nvidia.check_running_graphics_app()) {
request_schedule_cpus("GPU state change");
}
}
#endif
#ifdef __APPLE__
// Mac screensaver launches client if not already running.
// OS X quits screensaver when energy saver puts display to sleep,
// but we want to keep crunching.
// Also, user can start Mac screensaver by putting cursor in "hot corner"
// so idletime may be very small initially.
// If screensaver started client, this code tells client
// to exit when user becomes active, accounting for all these factors.
//
if (started_by_screensaver && (idletime < 30) && (getppid() == 1)) {
// pid is 1 if parent has exited
requested_exit = true;
}
// Exit if we were launched by Manager and it crashed.
//
if (launched_by_manager && (getppid() == 1)) {
gstate.requested_exit = true;
}
#endif
// active_tasks.get_memory_usage() sets variables needed by
// check_suspend_processing(), so it must be called first.
//
active_tasks.get_memory_usage();
suspend_reason = check_suspend_processing();
// suspend or resume activities (but only if already did startup)
//
if (tasks_restarted) {
if (suspend_reason) {
if (!tasks_suspended) {
suspend_tasks(suspend_reason);
}
last_suspend_reason = suspend_reason;
} else {
if (tasks_suspended) {
resume_tasks(last_suspend_reason);
}
}
} else if (first) {
// if suspended, show message the first time around
//
first = false;
if (suspend_reason) {
msg_printf(NULL, MSG_INFO,
"Suspending computation - %s",
suspend_reason_string(suspend_reason)
);
}
}
tasks_suspended = (suspend_reason != 0);
if (benchmarks_running) {
cpu_benchmarks_poll();
}
int old_network_suspend_reason = network_suspend_reason;
bool old_network_suspended = network_suspended;
check_suspend_network();
if (network_suspend_reason) {
if (!old_network_suspend_reason) {
char buf[256];
if (network_suspended) {
sprintf(buf,
"Suspending network activity - %s",
suspend_reason_string(network_suspend_reason)
);
request_schedule_cpus("network suspended");
// in case any "needs_network" jobs are running
} else {
sprintf(buf,
"Suspending file transfers - %s",
suspend_reason_string(network_suspend_reason)
);
}
msg_printf(NULL, MSG_INFO, "%s", buf);
pers_file_xfers->suspend();
}
} else {
if (old_network_suspend_reason) {
if (old_network_suspended) {
msg_printf(NULL, MSG_INFO, "Resuming network activity");
} else {
msg_printf(NULL, MSG_INFO, "Resuming file transfers");
}
}
// if we're emerging from a bandwidth quota suspension,
// add a random delay to avoid DDOS effect
//
if (
old_network_suspend_reason == SUSPEND_REASON_NETWORK_QUOTA_EXCEEDED
&& network_run_mode.get_current() == RUN_MODE_AUTO
) {
pers_file_xfers->add_random_delay(3600);
}
}
// NOTE:
// The order of calls in the following lists generally doesn't matter,
// except for the following:
// must have:
// active_tasks_poll
// handle_finished_apps
// schedule_cpus
// in that order (active_tasks_poll() sets must_schedule_cpus,
// and handle_finished_apps() must be done before schedule_cpus()
check_project_timeout();
#ifdef ENABLE_AUTO_UPDATE
auto_update.poll();
#endif
POLL_ACTION(active_tasks , active_tasks.poll );
POLL_ACTION(garbage_collect , garbage_collect );
// remove PERS_FILE_XFERs (and associated FILE_XFERs and HTTP_OPs)
// for unreferenced files
POLL_ACTION(gui_http , gui_http.poll );
POLL_ACTION(gui_rpc_http , gui_rpcs.poll );
POLL_ACTION(trickle_up_ops, trickle_up_poll);
// scan FILE_INFOS and create PERS_FILE_XFERs
// for PERS_FILE_XFERS that are done, delete them
if (!network_suspended && suspend_reason != SUSPEND_REASON_BENCHMARKS) {
// don't initiate network activity if we're doing CPU benchmarks
net_status.poll();
daily_xfer_history.poll();
POLL_ACTION(acct_mgr , acct_mgr_info.poll );
POLL_ACTION(file_xfers , file_xfers->poll );
// check for file xfer completion; don't delete anything
POLL_ACTION(pers_file_xfers , pers_file_xfers->poll );
// poll PERS_FILE_XFERS
// if we need to start xfer, creat FILE_XFER and init
// if FILE_XFER is complete
// handle transient and permanent failures
// delete the FILE_XFER
if (!config.no_info_fetch) {
POLL_ACTION(rss_feed_op , rss_feed_op.poll );
}
}
POLL_ACTION(create_and_delete_pers_file_xfers ,
create_and_delete_pers_file_xfers
);
POLL_ACTION(handle_finished_apps , handle_finished_apps );
POLL_ACTION(update_results , update_results );
if (!tasks_suspended) {
POLL_ACTION(schedule_cpus, schedule_cpus );
tasks_restarted = true;
}
if (!network_suspended) {
POLL_ACTION(scheduler_rpc , scheduler_rpc_poll );
}
retval = write_state_file_if_needed();
if (retval) {
msg_printf(NULL, MSG_INTERNAL_ERROR,
"Couldn't write state file: %s; giving up", boincerror(retval)
);
exit(EXIT_STATEFILE_WRITE);
}
if (log_flags.poll_debug) {
msg_printf(0, MSG_INFO,
"[poll] CLIENT_STATE::do_something(): End poll: %d tasks active\n", actions
);
}
if (actions > 0) {
return true;
} else {
time_stats.update(suspend_reason, gpu_suspend_reason);
// on some systems, DNS resolution only starts working
// a few minutes after system boot.
// If it didn't work before, try it again.
//
if (!strlen(host_info.domain_name)) {
host_info.get_local_network_info();
}
return false;
}
}
#endif // ifndef SIM
// See if the project specified by master_url already exists
// in the client state record. Ignore any trailing "/" characters
//
PROJECT* CLIENT_STATE::lookup_project(const char* master_url) {
int len1, len2;
char *mu;
len1 = (int)strlen(master_url);
if (master_url[strlen(master_url)-1] == '/') len1--;