-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathcs_prefs.cpp
More file actions
1139 lines (1070 loc) · 40 KB
/
cs_prefs.cpp
File metadata and controls
1139 lines (1070 loc) · 40 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
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/>.
// Logic related to general (also known as global) preferences:
// when to compute, how much disk to use, etc.
//
#include "cpp.h"
#ifdef _WIN32
#include "boinc_win.h"
#else
#include "config.h"
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#endif
#include "common_defs.h"
#include "filesys.h"
#include "parse.h"
#include "str_util.h"
#include "str_replace.h"
#include "util.h"
#include "client_msgs.h"
#include "client_state.h"
#include "cpu_benchmark.h"
#include "file_names.h"
#include "project.h"
using std::min;
using std::max;
using std::string;
#define MAX_PROJ_PREFS_LEN 65536
// max length of project-specific prefs
// Return the maximum allowed disk usage by projects as determined by user preferences.
// There are three different settings in the prefs;
// return the least of the three.
//
double CLIENT_STATE::allowed_disk_usage(double boinc_total) {
double limit = boinc_total + host_info.d_free - global_prefs.disk_min_free_gb*GIGA;
if (global_prefs.disk_max_used_pct) {
double limit_pct = host_info.d_total*global_prefs.disk_max_used_pct/100.0;
limit = min(limit, limit_pct);
}
if (global_prefs.disk_max_used_gb) {
double limit_abs = global_prefs.disk_max_used_gb*(GIGA);
limit = min(limit, limit_abs);
}
return max(limit, 0.);
}
#ifndef SIM
// populate:
// PROJECT::disk_usage for all projects
// GLOBAL_STATE::client_disk_usage
// GLOBAL_STATE::total_disk_usage
//
int CLIENT_STATE::get_disk_usages() {
unsigned int i;
double size;
PROJECT* p;
int retval;
char buf[MAXPATHLEN];
client_disk_usage = 0;
total_disk_usage = 0;
for (i=0; i<projects.size(); i++) {
p = projects[i];
p->disk_usage = 0;
retval = dir_size_alloc(p->project_dir(), size);
if (!retval) p->disk_usage = size;
}
for (i=0; i<active_tasks.active_tasks.size(); i++) {
ACTIVE_TASK* atp = active_tasks.active_tasks[i];
get_slot_dir(atp->slot, buf, sizeof(buf));
retval = dir_size_alloc(buf, size);
if (retval) continue;
atp->wup->project->disk_usage += size;
}
for (i=0; i<projects.size(); i++) {
p = projects[i];
total_disk_usage += p->disk_usage;
}
retval = dir_size_alloc(".", size, false);
if (!retval) {
client_disk_usage = size;
total_disk_usage += size;
}
return 0;
}
// populate PROJECT::disk_share for all projects,
// i.e. the max space we should allocate to the project.
// This is calculated as follows:
// - each project has a "disk_resource_share" (DRS)
// This is the resource share plus .1*(max resource share).
// This ensures that backup projects get some disk.
// - each project has a "desired_disk_usage (DDU)",
// which is either its current usage
// or an amount sent from the scheduler.
// - each project has a "quota": (available space)*(drs/total_drs).
// - a project is "greedy" if DDU > quota.
// - if a project is non-greedy, share = quota
// - X = available space - space used by non-greedy projects
// - if a project is greedy, share = quota
// + X*drs/(total drs of greedy projects)
//
void CLIENT_STATE::get_disk_shares() {
PROJECT* p;
unsigned int i;
// compute disk resource shares
//
double trs = 0;
double max_rs = 0;
for (i=0; i<projects.size(); i++) {
p = projects[i];
p->ddu = std::max(p->disk_usage, p->desired_disk_usage);
double rs = p->resource_share;
trs += rs;
if (rs > max_rs) max_rs = rs;
}
if (trs) {
max_rs /= 10;
for (i=0; i<projects.size(); i++) {
p = projects[i];
p->disk_resource_share = p->resource_share + max_rs;
}
} else {
for (i=0; i<projects.size(); i++) {
p = projects[i];
p->disk_resource_share = 1;
}
}
// Compute:
// greedy_drs: total disk resource share of greedy projects
// non_greedy_ddu: total desired disk usage of non-greedy projects
//
double greedy_drs = 0;
double non_greedy_ddu = 0;
double allowed = allowed_disk_usage(total_disk_usage);
for (i=0; i<projects.size(); i++) {
p = projects[i];
p->disk_quota = allowed*p->disk_resource_share/trs;
if (p->ddu > p->disk_quota) {
greedy_drs += p->disk_resource_share;
} else {
non_greedy_ddu += p->ddu;
}
}
double greedy_allowed = allowed - non_greedy_ddu;
if (log_flags.disk_usage_debug) {
msg_printf(0, MSG_INFO,
"[disk_usage] allowed %.2fMB used %.2fMB",
allowed/MEGA, total_disk_usage/MEGA
);
}
for (i=0; i<projects.size(); i++) {
p = projects[i];
double rs = p->disk_resource_share/trs;
if (p->ddu > allowed*rs) {
p->disk_share = greedy_allowed*p->disk_resource_share/greedy_drs;
} else {
p->disk_share = p->disk_quota;
}
if (log_flags.disk_usage_debug) {
msg_printf(p, MSG_INFO,
"[disk_usage] usage %.2fMB share %.2fMB",
p->disk_usage/MEGA, p->disk_share/MEGA
);
}
}
}
// See if we should suspend CPU and/or GPU processing;
// return the CPU suspend_reason,
// and if it's zero set gpu_suspend_reason
//
int CLIENT_STATE::check_suspend_processing() {
static double last_cpu_usage_suspend=0;
if (benchmarks_running) {
return SUSPEND_REASON_BENCHMARKS;
}
if (cc_config.start_delay && now < time_stats.client_start_time + cc_config.start_delay) {
return SUSPEND_REASON_INITIAL_DELAY;
}
if (os_requested_suspend) {
return SUSPEND_REASON_OS;
}
switch (cpu_run_mode.get_current()) {
case RUN_MODE_ALWAYS: break;
case RUN_MODE_NEVER:
return SUSPEND_REASON_USER_REQ;
default:
// "run according to prefs" checks:
//
if (!global_prefs.run_on_batteries
&& host_info.host_is_running_on_batteries()
) {
return SUSPEND_REASON_BATTERIES;
}
#ifndef ANDROID
// perform this check after SUSPEND_REASON_BATTERY_CHARGING on Android
if (!global_prefs.run_if_user_active && user_active) {
return SUSPEND_REASON_USER_ACTIVE;
}
#endif
if (global_prefs.cpu_times.suspended(now)) {
return SUSPEND_REASON_TIME_OF_DAY;
}
if (global_prefs.suspend_if_no_recent_input) {
long idle_time = host_info.user_idle_time(check_all_logins);
if (idle_time != USER_IDLE_TIME_INF
&& idle_time > global_prefs.suspend_if_no_recent_input*60
) {
return SUSPEND_REASON_NO_RECENT_INPUT;
}
}
if (now - exclusive_app_running < MEMORY_USAGE_PERIOD + EXCLUSIVE_APP_WAIT) {
return SUSPEND_REASON_EXCLUSIVE_APP_RUNNING;
}
// if we suspended because of CPU usage,
// don't unsuspend for at least 2*MEMORY_USAGE_PERIOD
//
if (current_suspend_cpu_usage()) {
if (now < last_cpu_usage_suspend+2*MEMORY_USAGE_PERIOD) {
return SUSPEND_REASON_CPU_USAGE;
}
if (non_boinc_cpu_usage*100 > current_suspend_cpu_usage()) {
last_cpu_usage_suspend = now;
return SUSPEND_REASON_CPU_USAGE;
}
}
}
#ifdef ANDROID
// suspend if we haven't heard from the GUI in 30 sec
// (we rely on it for battery info)
//
if (now > device_status_time + ANDROID_KEEPALIVE_TIMEOUT) {
requested_exit = true;
return SUSPEND_REASON_NO_GUI_KEEPALIVE;
}
// check for hot battery
// If suspend because of hot battery, don't resume for at least 5 min
// (crude hysteresis)
//
static double battery_heat_resume_time=0;
if (now < battery_heat_resume_time) {
return SUSPEND_REASON_BATTERY_OVERHEATED;
}
if (device_status.battery_state == BATTERY_STATE_OVERHEATED) {
battery_heat_resume_time = now + ANDROID_BATTERY_BACKOFF;
return SUSPEND_REASON_BATTERY_OVERHEATED;
}
if (device_status.battery_temperature_celsius > global_prefs.battery_max_temperature) {
battery_heat_resume_time = now + ANDROID_BATTERY_BACKOFF;
return SUSPEND_REASON_BATTERY_OVERHEATED;
}
// on some devices, running jobs can drain the battery even
// while it's recharging.
// So compute only if 95% charged or more.
//
static double battery_charge_resume_time=0;
if (now < battery_charge_resume_time) {
return SUSPEND_REASON_BATTERY_CHARGING;
}
int cp = device_status.battery_charge_pct;
if (cp >= 0) {
if (cp < global_prefs.battery_charge_min_pct) {
battery_charge_resume_time = now + ANDROID_BATTERY_BACKOFF;
return SUSPEND_REASON_BATTERY_CHARGING;
}
}
// user active.
// Do this check after checks that user can not influence on Android.
// E.g.
// 1. "connect to charger to continue computing"
// 2. "charge battery until 90%"
// 3. "turn screen off to continue computing"
if (!global_prefs.run_if_user_active && user_active) {
return SUSPEND_REASON_USER_ACTIVE;
}
#endif
// CPU is not suspended. See if GPUs are
//
if (!coprocs.none()) {
int old_gpu_suspend_reason = gpu_suspend_reason;
gpu_suspend_reason = 0;
switch (gpu_run_mode.get_current()) {
case RUN_MODE_ALWAYS:
break;
case RUN_MODE_NEVER:
gpu_suspend_reason = SUSPEND_REASON_USER_REQ;
break;
default:
if (now - exclusive_gpu_app_running < MEMORY_USAGE_PERIOD + EXCLUSIVE_APP_WAIT) {
gpu_suspend_reason = SUSPEND_REASON_EXCLUSIVE_APP_RUNNING;
break;
}
if (user_active && !global_prefs.run_gpu_if_user_active) {
gpu_suspend_reason = SUSPEND_REASON_USER_ACTIVE;
break;
}
}
if (old_gpu_suspend_reason && !gpu_suspend_reason) {
if (log_flags.task) {
msg_printf(NULL, MSG_INFO, "Resuming GPU computation");
}
request_schedule_cpus("GPU resumption");
} else if (!old_gpu_suspend_reason && gpu_suspend_reason) {
if (log_flags.task) {
msg_printf(NULL, MSG_INFO, "Suspending GPU computation - %s",
suspend_reason_string(gpu_suspend_reason)
);
}
request_schedule_cpus("GPU suspension");
}
}
return 0;
}
void CLIENT_STATE::show_suspend_tasks_message(int reason) {
if (reason != SUSPEND_REASON_CPU_THROTTLE) {
if (log_flags.task) {
msg_printf(NULL, MSG_INFO,
"Suspending computation - %s",
suspend_reason_string(reason)
);
}
switch (reason) {
case SUSPEND_REASON_BATTERY_OVERHEATED:
if (log_flags.task) {
msg_printf(NULL, MSG_INFO,
"(battery temperature %.1f > limit %.1f Celsius)",
device_status.battery_temperature_celsius,
global_prefs.battery_max_temperature
);
}
break;
case SUSPEND_REASON_BATTERY_CHARGING:
if (log_flags.task) {
msg_printf(NULL, MSG_INFO,
"(battery charge level %.1f%% < threshold %.1f%%",
device_status.battery_charge_pct,
global_prefs.battery_charge_min_pct
);
}
break;
}
}
}
int CLIENT_STATE::resume_tasks(int reason) {
if (reason == SUSPEND_REASON_CPU_THROTTLE) {
active_tasks.unsuspend_all(SUSPEND_REASON_CPU_THROTTLE);
} else {
if (log_flags.task) {
msg_printf(NULL, MSG_INFO, "Resuming computation");
}
active_tasks.unsuspend_all();
request_schedule_cpus("Resuming computation");
}
return 0;
}
// Check whether to set network_suspended and file_xfers_suspended.
//
void CLIENT_STATE::check_suspend_network() {
network_suspended = false;
file_xfers_suspended = false;
network_suspend_reason = 0;
bool recent_rpc;
// don't start network ops if system is shutting down
//
if (os_requested_suspend) {
network_suspend_reason = SUSPEND_REASON_OS;
network_suspended = true;
goto done;
}
// no network traffic if we're allowing unsigned apps
//
if (cc_config.unsigned_apps_ok) {
network_suspended = true;
file_xfers_suspended = true;
network_suspend_reason = SUSPEND_REASON_USER_REQ;
goto done;
}
// was there a recent GUI RPC that needs network?
//
recent_rpc = gui_rpcs.recent_rpc_needs_network(
ALLOW_NETWORK_IF_RECENT_RPC_PERIOD
);
switch(network_run_mode.get_current()) {
case RUN_MODE_ALWAYS:
goto done;
case RUN_MODE_NEVER:
file_xfers_suspended = true;
if (!recent_rpc) network_suspended = true;
network_suspend_reason = SUSPEND_REASON_USER_REQ;
goto done;
}
#ifdef ANDROID
if (now > device_status_time + ANDROID_KEEPALIVE_TIMEOUT) {
requested_exit = true;
file_xfers_suspended = true;
if (!recent_rpc) network_suspended = true;
network_suspend_reason = SUSPEND_REASON_NO_GUI_KEEPALIVE;
}
// use only WiFi
//
if (global_prefs.network_wifi_only && !device_status.wifi_online) {
file_xfers_suspended = true;
if (!recent_rpc) network_suspended = true;
network_suspend_reason = SUSPEND_REASON_WIFI_STATE;
}
#endif
if (global_prefs.daily_xfer_limit_mb && global_prefs.daily_xfer_period_days) {
double up, down;
daily_xfer_history.totals(
global_prefs.daily_xfer_period_days, up, down
);
if (up+down > global_prefs.daily_xfer_limit_mb*MEGA) {
file_xfers_suspended = true;
if (!recent_rpc) network_suspended = true;
network_suspend_reason = SUSPEND_REASON_NETWORK_QUOTA_EXCEEDED;
}
}
#ifndef ANDROID
// allow network transfers while user active, i.e. screen on.
// otherwise nothing (visible to the user) happens after initial attach
//
if (!global_prefs.run_if_user_active && user_active) {
file_xfers_suspended = true;
if (!recent_rpc) network_suspended = true;
network_suspend_reason = SUSPEND_REASON_USER_ACTIVE;
}
#endif
if (global_prefs.net_times.suspended(now)) {
file_xfers_suspended = true;
if (!recent_rpc) network_suspended = true;
network_suspend_reason = SUSPEND_REASON_TIME_OF_DAY;
}
if (now - exclusive_app_running < MEMORY_USAGE_PERIOD + EXCLUSIVE_APP_WAIT) {
file_xfers_suspended = true;
if (!recent_rpc) network_suspended = true;
network_suspend_reason = SUSPEND_REASON_EXCLUSIVE_APP_RUNNING;
}
done:
if (log_flags.suspend_debug) {
msg_printf(0, MSG_INFO, "[suspend] net_susp: %s; file_xfer_susp: %s; reason: %s",
network_suspended?"yes":"no",
file_xfers_suspended?"yes":"no",
suspend_reason_string(network_suspend_reason)
);
}
}
#endif // ifndef SIM
// call this only after parsing global prefs
//
PROJECT* CLIENT_STATE::global_prefs_source_project() {
return lookup_project(global_prefs.source_project);
}
void CLIENT_STATE::show_global_prefs_source(bool found_venue) {
PROJECT* pp = global_prefs_source_project();
if (pp) {
msg_printf(pp, MSG_INFO,
"General prefs: from %s (last modified %s)",
pp->get_project_name(), time_to_string(global_prefs.mod_time)
);
} else {
msg_printf(NULL, MSG_INFO,
"General prefs: from %s (last modified %s)",
global_prefs.source_project,
time_to_string(global_prefs.mod_time)
);
}
if (strlen(main_host_venue)) {
msg_printf(pp, MSG_INFO, "Computer location: %s", main_host_venue);
if (found_venue) {
msg_printf(NULL, MSG_INFO,
"General prefs: using separate prefs for %s", main_host_venue
);
} else {
msg_printf(pp, MSG_INFO,
"General prefs: no separate prefs for %s; using your defaults",
main_host_venue
);
}
} else {
msg_printf(pp, MSG_INFO, "Host location: none");
msg_printf(pp, MSG_INFO, "General prefs: using your defaults");
}
}
// parse user's project preferences,
// generating FILE_REF and FILE_INFO objects for each <app_file> element.
//
int PROJECT::parse_preferences_for_user_files() {
char buf[1024];
string timestamp, open_name, url, filename;
FILE_INFO* fip;
FILE_REF fr;
user_files.clear();
size_t n=0, start, end;
const size_t app_file_open_tag_len = strlen("<app_file>");
const size_t app_file_close_tag_len = strlen("</app_file>");
while (1) {
start = project_specific_prefs.find("<app_file>", n);
if (start == string::npos) break;
end = project_specific_prefs.find("</app_file>", n);
if (end == string::npos) break;
start += app_file_open_tag_len;
string x = project_specific_prefs.substr(start, end);
n = end + app_file_close_tag_len;
strlcpy(buf, x.c_str(), sizeof(buf));
if (!parse_str(buf, "<timestamp>", timestamp)) break;
if (!parse_str(buf, "<open_name>", open_name)) break;
if (!parse_str(buf, "<url>", url)) break;
filename = open_name + "_" + timestamp;
fip = gstate.lookup_file_info(this, filename.c_str());
if (!fip) {
fip = new FILE_INFO;
fip->project = this;
fip->download_urls.add(url);
safe_strcpy(fip->name, filename.c_str());
fip->is_user_file = true;
gstate.file_infos.push_back(fip);
}
fr.file_info = fip;
safe_strcpy(fr.open_name, open_name.c_str());
user_files.push_back(fr);
}
return 0;
}
// Read global preferences into the global_prefs structure.
// 1) read the override file to get venue in case it's there
// 2) read global_prefs.xml
// 3) read the override file again
//
// This is called:
// - on startup
// - on completion of a scheduler or AMS RPC, if they sent prefs
// - in response to read_global_prefs_override GUI RPC
//
void CLIENT_STATE::read_global_prefs(
const char* fname, const char* override_fname
) {
bool found_venue;
bool venue_specified_in_override = false;
int retval;
FILE* f;
string foo;
#ifdef USE_NET_PREFS
if (override_fname) {
retval = read_file_string(override_fname, foo);
if (!retval) {
parse_str(foo.c_str(), "<host_venue>", main_host_venue, sizeof(main_host_venue));
if (strlen(main_host_venue)) {
venue_specified_in_override = true;
}
}
}
retval = global_prefs.parse_file(
fname, main_host_venue, found_venue
);
if (retval) {
if (retval == ERR_FOPEN) {
msg_printf(NULL, MSG_INFO,
"No general preferences found - using defaults"
);
} else {
msg_printf(NULL, MSG_INFO,
"Couldn't parse preferences file - using defaults"
);
boinc_delete_file(fname);
}
global_prefs.init();
} else {
if (!venue_specified_in_override) {
// check that the source project's venue matches main_host_venue.
// If not, read file again.
// This is a fix for cases where main_host_venue is out of synch
//
PROJECT* p = global_prefs_source_project();
if (p && strcmp(main_host_venue, p->host_venue)) {
safe_strcpy(main_host_venue, p->host_venue);
global_prefs.parse_file(fname, main_host_venue, found_venue);
}
}
show_global_prefs_source(found_venue);
}
#endif
// read the override file
//
global_prefs.override_file_present = false;
if (override_fname) {
f = fopen(override_fname, "r");
if (f) {
MIOFILE mf;
GLOBAL_PREFS_MASK mask;
mf.init_file(f);
XML_PARSER xp(&mf);
global_prefs.parse_override(xp, "", found_venue, mask);
msg_printf(NULL, MSG_INFO, "Reading preferences override file");
fclose(f);
global_prefs.override_file_present = true;
validate_global_prefs(mask);
}
}
#ifndef SIM
get_disk_usages();
#endif
set_n_usable_cpus();
#ifdef ANDROID
global_prefs.run_if_user_active = false;
#endif
#ifndef SIM
file_xfers->set_bandwidth_limits(true);
file_xfers->set_bandwidth_limits(false);
#endif
print_global_prefs();
request_schedule_cpus("Prefs update");
request_work_fetch("Prefs update");
#ifndef SIM
active_tasks.request_reread_app_info();
#endif
}
// Validates preferences that were parsed. Uses the mask structure, so
// this should be called immediately after parsing. This function will check if
// any preferences are out of a reasonable range for each variable. This could be
// any of the following:
// 1. A negative number.
// 2. Maximum limits.
// 3. Checking to make sure a percentage is between 0% and 100%.
// 4. A time variable that is out of range.
// If the variable is out of range, it will either be set to the default value, 0,
// 0%, 100%, 0:00, or 24:00, depending on the variable. Default values are derived
// from GLOBAL_PREFS::defaults().
// Any new preference variables added should be validated here as well.
//
void CLIENT_STATE::validate_global_prefs(const GLOBAL_PREFS_MASK& mask) {
// ubound is an upper limit for doubles as a sanity check. This matches
// the upper limit as preferences are validated from the Manager. Refer to
// CDlgAdvPreferences::ValidateInput as an example.
//
double const ubound = 9999999999999.99;
char* outrange = "is out of range. Setting changed to";
char* timechange = "is not a valid time. Setting changed to";
char* forday = "for day";
if (mask.battery_charge_min_pct) {
if (global_prefs.battery_charge_min_pct < 0) {
global_prefs.battery_charge_min_pct = 90;
msg_printf(0, MSG_USER_ALERT, "'battery_charge_min_pct' %s 90.", outrange);
} else if (global_prefs.battery_charge_min_pct > 100) {
global_prefs.battery_charge_min_pct = 100;
msg_printf(0, MSG_USER_ALERT, "'battery_charge_min_pct' %s 100.", outrange);
}
}
if (mask.battery_max_temperature) {
if (global_prefs.battery_max_temperature < -273.15 || global_prefs.battery_max_temperature > ubound) {
global_prefs.battery_max_temperature = 40;
msg_printf(0, MSG_USER_ALERT, "'battery_max_temperature' %s 40.", outrange);
}
}
if (mask.idle_time_to_run) {
if (global_prefs.idle_time_to_run < 0 || global_prefs.idle_time_to_run > ubound) {
global_prefs.idle_time_to_run = 3;
msg_printf(0, MSG_USER_ALERT, "'idle_time_to_run' %s 3.", outrange);
}
}
if (mask.suspend_if_no_recent_input) {
if (global_prefs.suspend_if_no_recent_input < 0 || global_prefs.suspend_if_no_recent_input > ubound) {
global_prefs.suspend_if_no_recent_input = 0;
msg_printf(0, MSG_USER_ALERT, "'suspend_if_no_recent_input' %s 0.", outrange);
}
}
if (mask.suspend_cpu_usage) {
if (global_prefs.suspend_cpu_usage < 0) {
global_prefs.suspend_cpu_usage = 25;
msg_printf(0, MSG_USER_ALERT, "'suspend_cpu_usage' %s 25.", outrange);
} else if (global_prefs.suspend_cpu_usage > 100) {
global_prefs.suspend_cpu_usage = 100;
msg_printf(0, MSG_USER_ALERT, "'suspend_cpu_usage' %s 100.", outrange);
}
}
if (mask.niu_suspend_cpu_usage) {
if (global_prefs.niu_suspend_cpu_usage < 0) {
global_prefs.niu_suspend_cpu_usage = 50;
msg_printf(0, MSG_USER_ALERT, "'niu_suspend_cpu_usage' %s 50.", outrange);
} else if (global_prefs.niu_suspend_cpu_usage > 100) {
global_prefs.niu_suspend_cpu_usage = 100;
msg_printf(0, MSG_USER_ALERT, "'niu_suspend_cpu_usage' %s 100.", outrange);
}
}
if (mask.start_hour) {
if (global_prefs.cpu_times.start_hour < 0) {
global_prefs.cpu_times.start_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'start_hour' %s 0:00.", timechange);
} else if (global_prefs.cpu_times.start_hour > 24) {
global_prefs.cpu_times.start_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'start_hour' %s 24:00.", timechange);
}
}
if (mask.end_hour) {
if (global_prefs.cpu_times.end_hour < 0) {
global_prefs.cpu_times.end_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'end_hour' %s 0:00.", timechange);
} else if (global_prefs.cpu_times.end_hour > 24) {
global_prefs.cpu_times.end_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'end_hour' %s 24:00.", timechange);
}
}
if (mask.net_start_hour) {
if (global_prefs.net_times.start_hour < 0) {
global_prefs.net_times.start_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'net_start_hour' %s 0:00.", timechange);
} else if (global_prefs.net_times.start_hour > 24) {
global_prefs.net_times.start_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'net_start_hour' %s 24:00.", timechange);
}
}
if (mask.net_end_hour) {
if (global_prefs.net_times.end_hour < 0) {
global_prefs.net_times.end_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'net_end_hour' %s 0:00.", timechange);
} else if (global_prefs.net_times.end_hour > 24) {
global_prefs.net_times.end_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'net_end_hour' %s 24:00.", timechange);
}
}
// Validate day-of-week override preferences, if set.
for (int i = 0; i < 7; i++) {
if (global_prefs.cpu_times.week.days[i].present) {
if (global_prefs.cpu_times.week.days[i].start_hour < 0) {
global_prefs.cpu_times.week.days[i].start_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'start_hour' %s %d %s 0:00", forday, i, timechange);
} else if (global_prefs.cpu_times.week.days[i].start_hour > 24) {
global_prefs.cpu_times.week.days[i].start_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'start_hour' %s %d %s 24:00", forday, i, timechange);
}
if (global_prefs.cpu_times.week.days[i].end_hour < 0) {
global_prefs.cpu_times.week.days[i].end_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'end_hour' %s %d %s 0:00", forday, i, timechange);
} else if (global_prefs.cpu_times.week.days[i].end_hour > 24) {
global_prefs.cpu_times.week.days[i].end_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'end_hour' %s %d %s 24:00", forday, i, timechange);
}
}
if (global_prefs.net_times.week.days[i].present) {
if (global_prefs.net_times.week.days[i].start_hour < 0) {
global_prefs.net_times.week.days[i].start_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'net_start_hour' %s %d %s 0:00", forday, i, timechange);
} else if (global_prefs.net_times.week.days[i].start_hour > 24) {
global_prefs.net_times.week.days[i].start_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'net_start_hour' %s %d %s 24:00", forday, i, timechange);
}
if (global_prefs.net_times.week.days[i].end_hour < 0) {
global_prefs.net_times.week.days[i].end_hour = 0;
msg_printf(0, MSG_USER_ALERT, "'net_end_hour' %s %d %s 00:00", forday, i, timechange);
} else if (global_prefs.net_times.week.days[i].end_hour > 24) {
global_prefs.net_times.week.days[i].end_hour = 24;
msg_printf(0, MSG_USER_ALERT, "'net_end_hour' %s %d %s 24:00", forday, i, timechange);
}
}
}
if (mask.work_buf_min_days) {
if (global_prefs.work_buf_min_days < 0 || global_prefs.work_buf_min_days > 10) {
global_prefs.work_buf_min_days = 0.1;
msg_printf(0, MSG_USER_ALERT, "'work_buf_min_days' %s 0.1.", outrange);
}
}
if (mask.work_buf_additional_days) {
if (global_prefs.work_buf_additional_days < 0 || global_prefs.work_buf_additional_days > 10) {
global_prefs.work_buf_additional_days = 0.5;
msg_printf(0, MSG_USER_ALERT, "'work_buf_additional_days' %s 0.5.", outrange);
}
}
if (mask.max_ncpus_pct) {
if (global_prefs.max_ncpus_pct < 0) {
#ifdef ANDROID
global_prefs.max_ncpus_pct = 50;
msg_printf(0, MSG_USER_ALERT, "'max_ncpus_pct' %s 50.", outrange);
#else
global_prefs.max_ncpus_pct = 0;
msg_printf(0, MSG_USER_ALERT, "'max_ncpus_pct' %s 0.", outrange);
#endif
} else if (global_prefs.max_ncpus_pct > 100) {
global_prefs.max_ncpus_pct = 100;
msg_printf(0, MSG_USER_ALERT, "'max_ncpus_pct' %s 100.", outrange);
}
}
if (mask.niu_max_ncpus_pct) {
if (global_prefs.niu_max_ncpus_pct < 0) {
global_prefs.niu_max_ncpus_pct = 0;
msg_printf(0, MSG_USER_ALERT, "'niu_max_ncpus_pct' %s 0.", outrange);
} else if (global_prefs.niu_max_ncpus_pct > 100) {
global_prefs.niu_max_ncpus_pct = 100;
msg_printf(0, MSG_USER_ALERT, "'niu_max_ncpus_pct' %s 100.", outrange);
}
}
if (mask.max_ncpus) {
if (global_prefs.max_ncpus < 0 || global_prefs.max_ncpus > ubound) {
global_prefs.max_ncpus = 0;
msg_printf(0, MSG_USER_ALERT, "'max_ncpus' %s 0.", outrange);
}
}
if (mask.disk_interval) {
if (global_prefs.disk_interval < 0 || global_prefs.disk_interval > ubound) {
global_prefs.disk_interval = 60;
msg_printf(0, MSG_USER_ALERT, "'disk_interval' %s 60.", outrange);
}
}
if (mask.cpu_scheduling_period_minutes) {
if (global_prefs.cpu_scheduling_period_minutes < 0.0001 || global_prefs.cpu_scheduling_period_minutes > ubound) {
global_prefs.cpu_scheduling_period_minutes = 60;
msg_printf(0, MSG_USER_ALERT, "'cpu_scheduling_period_minutes' %s 60.", outrange);
}
}
if (mask.disk_max_used_gb) {
if (global_prefs.disk_max_used_gb < 0 || global_prefs.disk_max_used_gb > ubound) {
global_prefs.disk_max_used_gb = 0;
msg_printf(0, MSG_USER_ALERT, "'disk_max_used_gb' %s 0.", outrange);
}
}
if (mask.disk_max_used_pct) {
if (global_prefs.disk_max_used_pct < 0) {
global_prefs.disk_max_used_pct = 90;
msg_printf(0, MSG_USER_ALERT, "'disk_max_used_pct' %s 90.", outrange);
} else if (global_prefs.disk_max_used_pct > 100) {
global_prefs.disk_max_used_pct = 100;
msg_printf(0, MSG_USER_ALERT, "'disk_max_used_pct' %s 100.", outrange);
}
}
if (mask.disk_min_free_gb) {
if (global_prefs.disk_min_free_gb < 0 || global_prefs.disk_min_free_gb > ubound) {
global_prefs.disk_min_free_gb = 0.1;
msg_printf(0, MSG_USER_ALERT, "'disk_min_free_gb' %s 0.1.", outrange);
}
}
if (mask.vm_max_used_frac) {
if (global_prefs.vm_max_used_frac < 0 || global_prefs.vm_max_used_frac > 1) {
global_prefs.vm_max_used_frac = 0.75;
msg_printf(0, MSG_USER_ALERT, "'vm_max_used_frac' %s 0.75.", outrange);
}
}
if (mask.ram_max_used_busy_frac) {
if (global_prefs.ram_max_used_busy_frac < 0 || global_prefs.ram_max_used_busy_frac > 1) {
global_prefs.ram_max_used_busy_frac = 0.5;
msg_printf(0, MSG_USER_ALERT, "'ram_max_used_busy_frac' %s 0.5.", outrange);
}
}
if (mask.ram_max_used_idle_frac) {
if (global_prefs.ram_max_used_idle_frac < 0 || global_prefs.ram_max_used_idle_frac >1) {
#ifdef ANDROID
global_prefs.ram_max_used_idle_frac = 0.5;
msg_printf(0, MSG_USER_ALERT, "'ram_max_used_idle_frac' %s 0.5.", outrange);
#else
global_prefs.ram_max_used_idle_frac = 0.9;
msg_printf(0, MSG_USER_ALERT, "'ram_max_used_idle_frac' %s 0.9.", outrange);
#endif
}
}
if (mask.max_bytes_sec_up) {
if (global_prefs.max_bytes_sec_up < 0 || global_prefs.max_bytes_sec_up > ubound) {
global_prefs.max_bytes_sec_up = 0;
msg_printf(0, MSG_USER_ALERT, "'max_bytes_sec_up' %s 0.", outrange);
}
}
if (mask.max_bytes_sec_down) {
if (global_prefs.max_bytes_sec_down < 0 || global_prefs.max_bytes_sec_down > ubound) {
global_prefs.max_bytes_sec_down = 0;
msg_printf(0, MSG_USER_ALERT, "'max_bytes_sec_down' %s 0.", outrange);
}
}
if (mask.cpu_usage_limit) {
if (global_prefs.cpu_usage_limit < 0 || global_prefs.cpu_usage_limit > 100) {
global_prefs.cpu_usage_limit = 100;
msg_printf(0, MSG_USER_ALERT, "'cpu_usage_limit' %s 100.", outrange);
}
}
if (mask.niu_cpu_usage_limit) {
if (global_prefs.niu_cpu_usage_limit < 0 || global_prefs.niu_cpu_usage_limit > 100) {
global_prefs.niu_cpu_usage_limit = 100;
msg_printf(0, MSG_USER_ALERT, "'niu_cpu_usage_limit' %s 100.", outrange);
}
}
if (mask.daily_xfer_limit_mb) {
if (global_prefs.daily_xfer_limit_mb < 0 || global_prefs.daily_xfer_limit_mb > ubound) {
global_prefs.daily_xfer_limit_mb = 0;
msg_printf(0, MSG_USER_ALERT, "'daily_xfer_limit_mb' %s 0.", outrange);
}
}
if (mask.daily_xfer_period_days) {
if (global_prefs.daily_xfer_period_days < 0 || global_prefs.daily_xfer_period_days > ubound) {
global_prefs.daily_xfer_period_days = 0;
msg_printf(0, MSG_USER_ALERT, "'daily_xfer_period_days' %s 0.", outrange);
}
}
}
void CLIENT_STATE::print_global_prefs() {
msg_printf(NULL, MSG_INFO, "Preferences:");
// in use
//
msg_printf(NULL, MSG_INFO, "- When computer is in use");
msg_printf(NULL, MSG_INFO,
"- 'In use' means mouse/keyboard input in last %.2f minutes",
global_prefs.idle_time_to_run
);
if (!global_prefs.run_if_user_active) {
msg_printf(NULL, MSG_INFO, "- don't compute");
}
if (!global_prefs.run_gpu_if_user_active) {
msg_printf(NULL, MSG_INFO, "- don't use GPU");
}
double p = global_prefs.max_ncpus_pct;
if (p) {
int n = (int)((host_info.p_ncpus * p)/100);
msg_printf(NULL, MSG_INFO,
"- max CPUs used: %d", n
);
}
if (global_prefs.cpu_usage_limit) {
msg_printf(NULL, MSG_INFO,
"- Use at most %.0f%% of the CPU time",
global_prefs.cpu_usage_limit
);
}
if (global_prefs.suspend_cpu_usage) {
msg_printf(NULL, MSG_INFO,
"- suspend if non-BOINC CPU load exceeds %.0f%%",
global_prefs.suspend_cpu_usage
);
}
msg_printf(NULL, MSG_INFO,
"- max memory usage: %.2f GB",
(host_info.m_nbytes*global_prefs.ram_max_used_busy_frac)/GIGA
);