/
boinc_api.cpp
1556 lines (1427 loc) · 44.5 KB
/
boinc_api.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/>.
// The BOINC API and runtime system.
//
// Notes:
// 1) Thread structure:
// Sequential apps
// Unix
// getting CPU time and suspend/resume have to be done
// in the worker thread, so we use a SIGALRM signal handler.
// However, many library functions and system calls
// are not "asynch signal safe": see, e.g.
// http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03
// (e.g. sprintf() in a signal handler hangs Mac OS X)
// so we do as little as possible in the signal handler,
// and do the rest in a separate "timer thread".
// Win
// the timer thread does everything
// Multi-thread apps:
// Unix:
// fork
// original process runs timer loop:
// handle suspend/resume/quit, heartbeat (use signals)
// new process call boinc_init_options() with flags to
// send status messages and handle checkpoint stuff,
// and returns from boinc_init_parallel()
// NOTE: THIS DOESN'T RESPECT CRITICAL SECTIONS.
// NEED TO MASK SIGNALS IN CHILD DURING CRITICAL SECTIONS
// Win:
// like sequential case, except suspend/resume must enumerate
// all threads (except timer) and suspend/resume them all
//
// 2) All variables that are accessed by two threads (i.e. worker and timer)
// MUST be declared volatile.
//
// 3) For compatibility with C, we use int instead of bool various places
//
// 4) We must periodically check that the client is still alive and exit if not.
// Originally this was done using heartbeat msgs from client.
// This is unreliable, e.g. if the client is blocked for a long time.
// As of Oct 11 2012 we use a different mechanism:
// the client passes its PID and we periodically check whether it exists.
// But we need to support the heartbeat mechanism also for compatibility.
//
// Terminology:
// The processing of a result can be divided
// into multiple "episodes" (executions of the app),
// each of which resumes from the checkpointed state of the previous episode.
// Unless otherwise noted, "CPU time" refers to the sum over all episodes
// (not counting the part after the last checkpoint in an episode).
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
#include "boinc_win.h"
#endif
#ifdef _WIN32
#include "version.h"
#include "win_util.h"
#else
#include "config.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstdarg>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <pthread.h>
#include <vector>
#ifndef __EMX__
#include <sched.h>
#endif
#endif
#include "app_ipc.h"
#include "common_defs.h"
#include "diagnostics.h"
#include "error_numbers.h"
#include "filesys.h"
#include "mem_usage.h"
#include "parse.h"
#include "proc_control.h"
#include "shmem.h"
#include "str_replace.h"
#include "str_util.h"
#include "util.h"
#include "boinc_api.h"
using std::vector;
//#define DEBUG_BOINC_API
#ifdef __APPLE__
#include "mac_backtrace.h"
#define GETRUSAGE_IN_TIMER_THREAD
// call getrusage() in the timer thread,
// rather than in the worker thread's signal handler
// (which can cause crashes on Mac)
// If you want, you can set this for Linux too:
// CPPFLAGS=-DGETRUSAGE_IN_TIMER_THREAD
#endif
const char* api_version = "API_VERSION_" PACKAGE_VERSION;
static APP_INIT_DATA aid;
static FILE_LOCK file_lock;
APP_CLIENT_SHM* app_client_shm = 0;
static volatile int time_until_checkpoint;
// time until enable checkpoint
static volatile double fraction_done;
static volatile double last_checkpoint_cpu_time;
static volatile bool ready_to_checkpoint = false;
static volatile int in_critical_section = 0;
static volatile double last_wu_cpu_time;
static volatile bool standalone = false;
static volatile double initial_wu_cpu_time;
static volatile bool have_new_trickle_up = false;
static volatile bool have_trickle_down = true;
// set if the client notified us of a trickle-down.
// init to true so the first call to boinc_receive_trickle_down()
// will scan the slot dir for old trickle-down files
static volatile bool handle_trickle_downs = false;
// whether we should check for notifications of trickle_downs
// and file upload status.
// set by boinc_receive_trickle_down() and boinc_upload_file().
static volatile int heartbeat_giveup_count;
// interrupt count value at which to give up on client
#ifdef _WIN32
static volatile int nrunning_ticks = 0;
#endif
static volatile int interrupt_count = 0;
// number of timer interrupts
// used to measure elapsed time in a way that's
// not affected by user changing system clock,
// and doesn't have big jump after hibernation
static volatile int running_interrupt_count = 0;
// number of timer interrupts while not suspended.
// Used to compute elapsed time
static volatile bool finishing;
// used for worker/timer synch during boinc_finish();
static int want_network = 0;
static int have_network = 1;
static double bytes_sent = 0;
static double bytes_received = 0;
bool boinc_disable_timer_thread = false;
// simulate unresponsive app by setting to true (debugging)
static FUNC_PTR timer_callback = 0;
char web_graphics_url[256];
bool send_web_graphics_url = false;
char remote_desktop_addr[256];
bool send_remote_desktop_addr = false;
int app_min_checkpoint_period = 0;
// min checkpoint period requested by app
#define TIMER_PERIOD 0.1
// Sleep interval for timer thread;
// determines max rate of handling messages from client.
// Unix: period of worker-thread timer interrupts.
#define TIMERS_PER_SEC 10
// reciprocal of TIMER_PERIOD
// This determines the resolution of fraction done and CPU time reporting
// to the client, and of checkpoint enabling.
#define HEARTBEAT_GIVEUP_SECS 30
#define HEARTBEAT_GIVEUP_COUNT ((int)(HEARTBEAT_GIVEUP_SECS/TIMER_PERIOD))
// quit if no heartbeat from client in this #interrupts
#define LOCKFILE_TIMEOUT_PERIOD 35
// quit if we cannot aquire slot lock file in this #secs after startup
#ifdef _WIN32
static HANDLE hSharedMem;
HANDLE worker_thread_handle;
// used to suspend worker thread, and to measure its CPU time
DWORD timer_thread_id;
#else
static volatile bool worker_thread_exit_flag = false;
static volatile int worker_thread_exit_status;
// the above are used by the timer thread to tell
// the worker thread to exit
static pthread_t worker_thread_handle;
static pthread_t timer_thread_handle;
#ifndef GETRUSAGE_IN_TIMER_THREAD
static struct rusage worker_thread_ru;
#endif
#endif
static BOINC_OPTIONS options;
volatile BOINC_STATUS boinc_status;
// vars related to intermediate file upload
struct UPLOAD_FILE_STATUS {
std::string name;
int status;
};
static bool have_new_upload_file;
static std::vector<UPLOAD_FILE_STATUS> upload_file_status;
static int resume_activities();
static void boinc_exit(int);
static void block_sigalrm();
static int start_worker_signals();
char* boinc_msg_prefix(char* sbuf, int len) {
char buf[256];
struct tm tm;
struct tm *tmp = &tm;
int n;
time_t x = time(0);
if (x == -1) {
strlcpy(sbuf, "time() failed", len);
return sbuf;
}
#ifdef _WIN32
#ifdef __MINGW32__
if ((tmp = localtime(&x)) == NULL) {
#else
if (localtime_s(&tm, &x) == EINVAL) {
#endif
#else
if (localtime_r(&x, &tm) == NULL) {
#endif
strlcpy(sbuf, "localtime() failed", len);
return sbuf;
}
if (strftime(buf, sizeof(buf)-1, "%H:%M:%S", tmp) == 0) {
strlcpy(sbuf, "strftime() failed", len);
return sbuf;
}
#ifdef _WIN32
n = _snprintf(sbuf, len, "%s (%d):", buf, GetCurrentProcessId());
#else
n = snprintf(sbuf, len, "%s (%d):", buf, getpid());
#endif
if (n < 0) {
strlcpy(sbuf, "sprintf() failed", len);
return sbuf;
}
sbuf[len-1] = 0; // just in case
return sbuf;
}
static int setup_shared_mem() {
char buf[256];
if (standalone) {
fprintf(stderr,
"%s Standalone mode, so not using shared memory.\n",
boinc_msg_prefix(buf, sizeof(buf))
);
return 0;
}
app_client_shm = new APP_CLIENT_SHM;
#ifdef _WIN32
sprintf(buf, "%s%s", SHM_PREFIX, aid.shmem_seg_name);
hSharedMem = attach_shmem(buf, (void**)&app_client_shm->shm);
if (hSharedMem == NULL) {
delete app_client_shm;
app_client_shm = NULL;
}
#else
#ifdef __EMX__
if (attach_shmem(aid.shmem_seg_name, (void**)&app_client_shm->shm)) {
delete app_client_shm;
app_client_shm = NULL;
}
#else
if (aid.shmem_seg_name == -1) {
// Version 6 Unix/Linux/Mac client
if (attach_shmem_mmap(MMAPPED_FILE_NAME, (void**)&app_client_shm->shm)) {
delete app_client_shm;
app_client_shm = NULL;
}
} else {
// version 5 Unix/Linux/Mac client
if (attach_shmem(aid.shmem_seg_name, (void**)&app_client_shm->shm)) {
delete app_client_shm;
app_client_shm = NULL;
}
}
#endif
#endif // ! _WIN32
if (app_client_shm == NULL) return -1;
return 0;
}
// a mutex for data structures shared between time and worker threads
//
#ifdef _WIN32
static HANDLE mutex;
static void init_mutex() {
mutex = CreateMutex(NULL, FALSE, NULL);
}
static inline void acquire_mutex() {
WaitForSingleObject(mutex, INFINITE);
}
static inline void release_mutex() {
ReleaseMutex(mutex);
}
#else
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static void init_mutex() {}
static inline void acquire_mutex() {
#ifdef DEBUG_BOINC_API
char buf[256];
fprintf(stderr, "%s acquiring mutex\n",
boinc_msg_prefix(buf, sizeof(buf))
);
#endif
pthread_mutex_lock(&mutex);
}
static inline void release_mutex() {
#ifdef DEBUG_BOINC_API
char buf[256];
fprintf(stderr, "%s releasing mutex\n",
boinc_msg_prefix(buf, sizeof(buf))
);
#endif
pthread_mutex_unlock(&mutex);
}
#endif
// Return CPU time of process.
//
double boinc_worker_thread_cpu_time() {
double cpu;
#ifdef _WIN32
int retval;
retval = boinc_process_cpu_time(GetCurrentProcess(), cpu);
if (retval) {
cpu = nrunning_ticks * TIMER_PERIOD; // for Win9x
}
#else
#ifdef GETRUSAGE_IN_TIMER_THREAD
struct rusage worker_thread_ru;
getrusage(RUSAGE_SELF, &worker_thread_ru);
#endif
cpu = (double)worker_thread_ru.ru_utime.tv_sec
+ (((double)worker_thread_ru.ru_utime.tv_usec)/1000000.0);
cpu += (double)worker_thread_ru.ru_stime.tv_sec
+ (((double)worker_thread_ru.ru_stime.tv_usec)/1000000.0);
#endif
return cpu;
}
// Communicate to the client (via shared mem)
// the current CPU time and fraction done.
// NOTE: various bugs could cause some of these FP numbers to be enormous,
// possibly overflowing the buffer.
// So use strlcat() instead of strcat()
//
// This is called only from the timer thread (so no need for synch)
//
static bool update_app_progress(double cpu_t, double cp_cpu_t) {
char msg_buf[MSG_CHANNEL_SIZE], buf[256];
if (standalone) return true;
sprintf(msg_buf,
"<current_cpu_time>%e</current_cpu_time>\n"
"<checkpoint_cpu_time>%e</checkpoint_cpu_time>\n",
cpu_t, cp_cpu_t
);
if (want_network) {
strlcat(msg_buf, "<want_network>1</want_network>\n", sizeof(msg_buf));
}
if (fraction_done >= 0) {
double range = aid.fraction_done_end - aid.fraction_done_start;
double fdone = aid.fraction_done_start + fraction_done*range;
sprintf(buf, "<fraction_done>%e</fraction_done>\n", fdone);
strlcat(msg_buf, buf, sizeof(msg_buf));
}
if (bytes_sent) {
sprintf(buf, "<bytes_sent>%f</bytes_sent>\n", bytes_sent);
strlcat(msg_buf, buf, sizeof(msg_buf));
}
if (bytes_received) {
sprintf(buf, "<bytes_received>%f</bytes_received>\n", bytes_received);
strlcat(msg_buf, buf, sizeof(msg_buf));
}
return app_client_shm->shm->app_status.send_msg(msg_buf);
}
static void handle_heartbeat_msg() {
char buf[MSG_CHANNEL_SIZE];
double dtemp;
bool btemp;
if (app_client_shm->shm->heartbeat.get_msg(buf)) {
boinc_status.network_suspended = false;
if (match_tag(buf, "<heartbeat/>")) {
heartbeat_giveup_count = interrupt_count + HEARTBEAT_GIVEUP_COUNT;
}
if (parse_double(buf, "<wss>", dtemp)) {
boinc_status.working_set_size = dtemp;
}
if (parse_double(buf, "<max_wss>", dtemp)) {
boinc_status.max_working_set_size = dtemp;
}
if (parse_bool(buf, "suspend_network", btemp)) {
boinc_status.network_suspended = btemp;
}
}
}
static bool client_dead() {
char buf[256];
bool dead;
if (aid.client_pid) {
// check every 10 sec
//
if (interrupt_count%(TIMERS_PER_SEC*10)) return false;
#ifdef _WIN32
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, aid.client_pid);
// If the process exists but is running under a different user account (boinc_master)
// then the handle returned is NULL and GetLastError() returns ERROR_ACCESS_DENIED.
//
if ((h == NULL) && (GetLastError() != ERROR_ACCESS_DENIED)) {
dead = true;
} else {
if (h) CloseHandle(h);
dead = false;
}
#else
int retval = kill(aid.client_pid, 0);
dead = (retval == -1 && errno == ESRCH);
#endif
} else {
dead = (interrupt_count > heartbeat_giveup_count);
}
if (dead) {
boinc_msg_prefix(buf, sizeof(buf));
fputs(buf, stderr); // don't use fprintf() here
if (aid.client_pid) {
fputs(" BOINC client no longer exists - exiting\n", stderr);
} else {
fputs(" No heartbeat from client for 30 sec - exiting\n", stderr);
}
return true;
}
return false;
}
#ifndef _WIN32
// For multithread apps on Unix, the main process executes the following.
//
static void parallel_master(int child_pid) {
char buf[MSG_CHANNEL_SIZE];
int exit_status;
while (1) {
boinc_sleep(TIMER_PERIOD);
interrupt_count++;
if (app_client_shm) {
handle_heartbeat_msg();
if (app_client_shm->shm->process_control_request.get_msg(buf)) {
if (match_tag(buf, "<suspend/>")) {
kill(child_pid, SIGSTOP);
} else if (match_tag(buf, "<resume/>")) {
kill(child_pid, SIGCONT);
} else if (match_tag(buf, "<quit/>")) {
kill(child_pid, SIGKILL);
exit(0);
} else if (match_tag(buf, "<abort/>")) {
kill(child_pid, SIGKILL);
exit(EXIT_ABORTED_BY_CLIENT);
}
}
if (client_dead()) {
kill(child_pid, SIGKILL);
exit(0);
}
}
if (interrupt_count % TIMERS_PER_SEC) continue;
if (waitpid(child_pid, &exit_status, WNOHANG) == child_pid) break;
}
boinc_finish(exit_status);
}
#endif
int boinc_init() {
int retval;
if (!diagnostics_is_initialized()) {
retval = boinc_init_diagnostics(BOINC_DIAG_DEFAULTS);
if (retval) return retval;
}
boinc_options_defaults(options);
return boinc_init_options(&options);
}
int boinc_init_options(BOINC_OPTIONS* opt) {
int retval;
#ifndef _WIN32
if (options.multi_thread) {
int child_pid = fork();
if (child_pid) {
// original process - master
//
options.send_status_msgs = false;
retval = boinc_init_options_general(options);
if (retval) {
kill(child_pid, SIGKILL);
return retval;
}
parallel_master(child_pid);
}
// new process - slave
//
options.main_program = false;
options.check_heartbeat = false;
options.handle_process_control = false;
options.multi_thread = false;
options.multi_process = false;
return boinc_init_options(&options);
}
#endif
retval = boinc_init_options_general(*opt);
if (retval) return retval;
retval = start_timer_thread();
if (retval) return retval;
#ifndef _WIN32
retval = start_worker_signals();
if (retval) return retval;
#endif
return 0;
}
int boinc_init_parallel() {
BOINC_OPTIONS _options;
boinc_options_defaults(_options);
_options.multi_thread = true;
return boinc_init_options(&_options);
}
static int min_checkpoint_period() {
int x = (int)aid.checkpoint_period;
if (app_min_checkpoint_period > x) {
x = app_min_checkpoint_period;
}
if (x == 0) x = DEFAULT_CHECKPOINT_PERIOD;
return x;
}
int boinc_set_min_checkpoint_period(int x) {
app_min_checkpoint_period = x;
if (x > time_until_checkpoint) {
time_until_checkpoint = x;
}
return 0;
}
int boinc_init_options_general(BOINC_OPTIONS& opt) {
int retval;
char buf[256];
options = opt;
if (!diagnostics_is_initialized()) {
retval = boinc_init_diagnostics(BOINC_DIAG_DEFAULTS);
if (retval) return retval;
}
boinc_status.no_heartbeat = false;
boinc_status.suspended = false;
boinc_status.quit_request = false;
boinc_status.abort_request = false;
if (options.main_program) {
// make sure we're the only app running in this slot
//
retval = file_lock.lock(LOCKFILE);
if (retval) {
// give any previous occupant a chance to timeout and exit
//
fprintf(stderr, "%s Can't acquire lockfile (%d) - waiting %ds\n",
boinc_msg_prefix(buf, sizeof(buf)),
retval, LOCKFILE_TIMEOUT_PERIOD
);
boinc_sleep(LOCKFILE_TIMEOUT_PERIOD);
retval = file_lock.lock(LOCKFILE);
}
if (retval) {
fprintf(stderr, "%s Can't acquire lockfile (%d) - exiting\n",
boinc_msg_prefix(buf, sizeof(buf)),
retval
);
#ifdef _WIN32
char buf2[256];
windows_format_error_string(GetLastError(), buf2, 256);
fprintf(stderr, "%s Error: %s\n", boinc_msg_prefix(buf, sizeof(buf)), buf2);
#endif
// if we can't acquire the lock file there must be
// another app instance running in this slot.
// If we exit(0), the client will keep restarting us.
// Instead, tell the client not to restart us for 10 min.
//
boinc_temporary_exit(600, "Waiting to acquire lock");
}
}
retval = boinc_parse_init_data_file();
if (retval) {
standalone = true;
} else {
retval = setup_shared_mem();
if (retval) {
fprintf(stderr,
"%s Can't set up shared mem: %d. Will run in standalone mode.\n",
boinc_msg_prefix(buf, sizeof(buf)), retval
);
standalone = true;
}
}
// copy the WU CPU time to a separate var,
// since we may reread the structure again later.
//
initial_wu_cpu_time = aid.wu_cpu_time;
fraction_done = -1;
time_until_checkpoint = min_checkpoint_period();
last_checkpoint_cpu_time = aid.wu_cpu_time;
last_wu_cpu_time = aid.wu_cpu_time;
if (standalone) {
options.check_heartbeat = false;
}
heartbeat_giveup_count = interrupt_count + HEARTBEAT_GIVEUP_COUNT;
init_mutex();
return 0;
}
int boinc_get_status(BOINC_STATUS *s) {
s->no_heartbeat = boinc_status.no_heartbeat;
s->suspended = boinc_status.suspended;
s->quit_request = boinc_status.quit_request;
s->reread_init_data_file = boinc_status.reread_init_data_file;
s->abort_request = boinc_status.abort_request;
s->working_set_size = boinc_status.working_set_size;
s->max_working_set_size = boinc_status.max_working_set_size;
s->network_suspended = boinc_status.network_suspended;
return 0;
}
// if we have any new trickle-ups or file upload requests,
// send a message describing them
//
static void send_trickle_up_msg() {
char buf[MSG_CHANNEL_SIZE];
if (standalone) return;
strcpy(buf, "");
if (have_new_trickle_up) {
strcat(buf, "<have_new_trickle_up/>\n");
}
if (have_new_upload_file) {
strcat(buf, "<have_new_upload_file/>\n");
}
if (strlen(buf)) {
BOINCINFO("Sending Trickle Up Message");
if (app_client_shm->shm->trickle_up.send_msg(buf)) {
have_new_trickle_up = false;
have_new_upload_file = false;
}
}
}
// NOTE: a non-zero status tells the client that we're exiting with
// an "unrecoverable error", which will be reported back to server.
// A zero exit-status tells the client we've successfully finished the result.
//
int boinc_finish_message(int status, const char* msg, bool is_notice) {
char buf[256];
fraction_done = 1;
fprintf(stderr,
"%s called boinc_finish(%d)\n",
boinc_msg_prefix(buf, sizeof(buf)), status
);
finishing = true;
boinc_sleep(2.0); // let the timer thread send final messages
boinc_disable_timer_thread = true; // then disable it
if (options.main_program) {
FILE* f = fopen(BOINC_FINISH_CALLED_FILE, "w");
if (f) {
fprintf(f, "%d\n", status);
if (msg) {
fprintf(f, "%s\n%s\n", msg, is_notice?"notice":"");
}
fclose(f);
}
}
boinc_exit(status);
return 0; // never reached
}
int boinc_finish(int status) {
return boinc_finish_message(status, NULL, false);
}
int boinc_temporary_exit(int delay, const char* reason, bool is_notice) {
FILE* f = fopen(TEMPORARY_EXIT_FILE, "w");
if (!f) {
return ERR_FOPEN;
}
fprintf(f, "%d\n", delay);
if (reason) {
fprintf(f, "%s\n", reason);
if (is_notice) {
fprintf(f, "notice\n");
}
}
fclose(f);
boinc_exit(0);
return 0;
}
// unlock the lockfile and call the appropriate exit function
// Unix: called only from the worker thread.
// Win: called from the worker or timer thread.
//
// make static eventually
//
void boinc_exit(int status) {
int retval;
char buf[256];
if (options.main_program && file_lock.locked) {
retval = file_lock.unlock(LOCKFILE);
if (retval) {
#ifdef _WIN32
windows_format_error_string(GetLastError(), buf, 256);
fprintf(stderr,
"%s Can't unlock lockfile (%d): %s\n",
boinc_msg_prefix(buf, sizeof(buf)), retval, buf
);
#else
fprintf(stderr,
"%s Can't unlock lockfile (%d)\n",
boinc_msg_prefix(buf, sizeof(buf)), retval
);
perror("file unlock failed");
#endif
}
}
// kill any processes the app may have created
//
if (options.multi_process) {
kill_descendants();
}
boinc_finish_diag();
// various platforms have problems shutting down a process
// while other threads are still executing,
// or triggering endless exit()/atexit() loops.
//
BOINCINFO("Exit Status: %d", status);
fflush(NULL);
#if defined(_WIN32)
// Halt all the threads and clean up.
TerminateProcess(GetCurrentProcess(), status);
// note: the above CAN return!
Sleep(1000);
DebugBreak();
#elif defined(__APPLE_CC__)
// stops endless exit()/atexit() loops.
_exit(status);
#else
// arrange to exit with given status even if errors happen
// in atexit() functions
//
set_signal_exit_code(status);
exit(status);
#endif
}
void boinc_network_usage(double sent, double received) {
bytes_sent = sent;
bytes_received = received;
}
int boinc_is_standalone() {
if (standalone) return 1;
return 0;
}
static void exit_from_timer_thread(int status) {
#ifdef DEBUG_BOINC_API
char buf[256];
fprintf(stderr, "%s exit_from_timer_thread(%d) called\n",
boinc_msg_prefix(buf, sizeof(buf)), status
);
#endif
#ifdef _WIN32
// TerminateProcess() doesn't work if there are suspended threads?
if (boinc_status.suspended) {
resume_activities();
}
// this seems to work OK on Windows
//
boinc_exit(status);
#else
// but on Unix there are synchronization problems;
// set a flag telling the worker thread to exit
//
worker_thread_exit_status = status;
worker_thread_exit_flag = true;
pthread_exit(NULL);
#endif
}
// parse the init data file.
// This is done at startup, and also if a "reread prefs" message is received
//
int boinc_parse_init_data_file() {
FILE* f;
int retval;
char buf[256];
if (aid.project_preferences) {
free(aid.project_preferences);
aid.project_preferences = NULL;
}
aid.clear();
aid.checkpoint_period = DEFAULT_CHECKPOINT_PERIOD;
if (!boinc_file_exists(INIT_DATA_FILE)) {
fprintf(stderr,
"%s Can't open init data file - running in standalone mode\n",
boinc_msg_prefix(buf, sizeof(buf))
);
return ERR_FOPEN;
}
f = boinc_fopen(INIT_DATA_FILE, "r");
retval = parse_init_data_file(f, aid);
fclose(f);
if (retval) {
fprintf(stderr,
"%s Can't parse init data file - running in standalone mode\n",
boinc_msg_prefix(buf, sizeof(buf))
);
return retval;
}
return 0;
}
// used by wrappers
//
int boinc_report_app_status_aux(
double cpu_time,
double checkpoint_cpu_time,
double _fraction_done,
int other_pid,
double _bytes_sent,
double _bytes_received
) {
char msg_buf[MSG_CHANNEL_SIZE], buf[1024];
if (standalone) return 0;
sprintf(msg_buf,
"<current_cpu_time>%e</current_cpu_time>\n"
"<checkpoint_cpu_time>%e</checkpoint_cpu_time>\n"
"<fraction_done>%e</fraction_done>\n",
cpu_time,
checkpoint_cpu_time,
_fraction_done
);
if (other_pid) {
sprintf(buf, "<other_pid>%d</other_pid>\n", other_pid);
strcat(msg_buf, buf);
}
if (_bytes_sent) {
sprintf(buf, "<bytes_sent>%f</bytes_sent>\n", _bytes_sent);
strcat(msg_buf, buf);
}
if (_bytes_received) {
sprintf(buf, "<bytes_received>%f</bytes_received>\n", _bytes_received);
strcat(msg_buf, buf);
}
if (app_client_shm->shm->app_status.send_msg(msg_buf)) {
return 0;
}
return ERR_WRITE;
}
int boinc_report_app_status(
double cpu_time,
double checkpoint_cpu_time,
double _fraction_done
){
return boinc_report_app_status_aux(
cpu_time, checkpoint_cpu_time, _fraction_done, 0, 0, 0
);
}
int boinc_get_init_data_p(APP_INIT_DATA* app_init_data) {
*app_init_data = aid;
return 0;
}
int boinc_get_init_data(APP_INIT_DATA& app_init_data) {
app_init_data = aid;
return 0;
}
int boinc_wu_cpu_time(double& cpu_t) {
cpu_t = last_wu_cpu_time;
return 0;
}
// Suspend this job.
// Can be called from either timer or worker thread.
//
static int suspend_activities(bool called_from_worker) {
#ifdef DEBUG_BOINC_API
char log_buf[256];
fprintf(stderr, "%s suspend_activities() called from %s\n",
boinc_msg_prefix(log_buf, sizeof(log_buf)),
called_from_worker?"worker thread":"timer thread"
);
#endif
#ifdef _WIN32
static vector<int> pids;
if (options.multi_thread) {
if (pids.size() == 0) {
pids.push_back(GetCurrentProcessId());
}
suspend_or_resume_threads(pids, timer_thread_id, false, true);
} else {
SuspendThread(worker_thread_handle);
}
#else
if (options.multi_process) {
suspend_or_resume_descendants(false);
}
// if called from worker thread, sleep until suspension is over
// if called from time thread, don't need to do anything;
// suspension is done by signal handler in worker thread
//
if (called_from_worker) {
while (boinc_status.suspended) {
sleep(1);
}
}
#endif
return 0;
}
int resume_activities() {
#ifdef DEBUG_BOINC_API
char log_buf[256];
fprintf(stderr, "%s resume_activities()\n",
boinc_msg_prefix(log_buf, sizeof(log_buf))
);
#endif
#ifdef _WIN32
static vector<int> pids;
if (options.multi_thread) {
if (pids.size() == 0) pids.push_back(GetCurrentProcessId());
suspend_or_resume_threads(pids, timer_thread_id, true, true);
} else {
ResumeThread(worker_thread_handle);
}
#else
if (options.multi_process) {
suspend_or_resume_descendants(true);
}
#endif
return 0;
}
static void handle_upload_file_status() {
char path[MAXPATHLEN], buf[256], log_name[256], *p, log_buf[256];
std::string filename;
int status;