2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-08-05 00:29:34 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-08-21 23:44:57 +00:00
|
|
|
//
|
2002-08-05 00:29:34 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-08-21 23:44:57 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-08-05 00:29:34 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-08-21 23:44:57 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-08-05 00:29:34 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2003-05-07 23:42:17 +00:00
|
|
|
// Code that's in the BOINC app library (but NOT in the core client)
|
2003-09-30 21:17:20 +00:00
|
|
|
// graphics-related code goes in graphics_api.C, not here
|
2003-05-07 23:42:17 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2002-09-18 22:19:38 +00:00
|
|
|
|
2003-02-19 18:55:07 +00:00
|
|
|
#ifdef _WIN32
|
2002-08-05 00:29:34 +00:00
|
|
|
#include <io.h>
|
|
|
|
#include <sys/stat.h>
|
2002-10-06 00:43:54 +00:00
|
|
|
#include <afxwin.h>
|
2002-08-05 00:29:34 +00:00
|
|
|
#include <winuser.h>
|
2002-10-28 22:30:37 +00:00
|
|
|
#include <mmsystem.h> // for timing
|
2002-10-29 23:32:24 +00:00
|
|
|
|
2002-11-14 00:49:24 +00:00
|
|
|
MMRESULT timer_id;
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif
|
2002-09-18 22:19:38 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "parse.h"
|
2003-03-17 19:24:38 +00:00
|
|
|
#include "shmem.h"
|
2002-11-18 23:09:11 +00:00
|
|
|
#include "util.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "error_numbers.h"
|
2003-05-07 23:42:17 +00:00
|
|
|
#include "app_ipc.h"
|
2002-10-29 18:51:45 +00:00
|
|
|
|
2003-09-30 23:09:44 +00:00
|
|
|
#include "graphics_api.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "boinc_api.h"
|
|
|
|
|
2002-11-22 19:57:37 +00:00
|
|
|
#ifdef _WIN32
|
2003-03-18 19:37:09 +00:00
|
|
|
HANDLE hQuitRequest, hSharedMem;
|
2002-11-22 19:57:37 +00:00
|
|
|
LONG CALLBACK boinc_catch_signal(EXCEPTION_POINTERS *ExceptionInfo);
|
|
|
|
#else
|
|
|
|
extern void boinc_catch_signal(int signal);
|
2003-03-12 18:15:48 +00:00
|
|
|
extern void boinc_quit(int sig);
|
2002-11-22 19:57:37 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
static APP_INIT_DATA aid;
|
2002-10-28 22:30:37 +00:00
|
|
|
static double timer_period = 1.0/50.0; // 50 Hz timer
|
2002-08-05 00:29:34 +00:00
|
|
|
static double time_until_checkpoint;
|
|
|
|
static double time_until_fraction_done_update;
|
|
|
|
static double fraction_done;
|
2002-11-19 21:26:34 +00:00
|
|
|
static double last_checkpoint_cpu_time;
|
2002-08-05 00:29:34 +00:00
|
|
|
static bool ready_to_checkpoint = false;
|
2002-12-02 22:51:11 +00:00
|
|
|
static bool write_frac_done = false;
|
2002-08-05 00:29:34 +00:00
|
|
|
static bool this_process_active;
|
2003-02-27 23:42:38 +00:00
|
|
|
static bool time_to_quit = false;
|
2002-11-26 21:33:43 +00:00
|
|
|
bool using_opengl = false;
|
2003-08-21 23:44:57 +00:00
|
|
|
bool standalone = false;
|
2003-05-07 23:42:17 +00:00
|
|
|
APP_CLIENT_SHM *app_client_shm;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2003-09-27 17:53:43 +00:00
|
|
|
bool boinc_is_standalone() {
|
2003-08-21 23:44:57 +00:00
|
|
|
return standalone;
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
// read the INIT_DATA and FD_INIT files
|
|
|
|
//
|
2003-08-21 23:44:57 +00:00
|
|
|
int boinc_init(bool standalone_ /* = false */) {
|
2003-06-03 21:31:53 +00:00
|
|
|
FILE* f;
|
|
|
|
int retval;
|
|
|
|
|
2002-08-22 20:19:18 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
freopen(STDERR_FILE, "a", stderr);
|
|
|
|
#endif
|
|
|
|
|
2003-08-21 23:44:57 +00:00
|
|
|
standalone = standalone_;
|
2003-06-03 21:31:53 +00:00
|
|
|
|
|
|
|
// If in standalone mode, use init files if they're there,
|
|
|
|
// but don't demand that they exist
|
|
|
|
//
|
2002-08-05 00:29:34 +00:00
|
|
|
f = fopen(INIT_DATA_FILE, "r");
|
2002-08-24 00:41:25 +00:00
|
|
|
if (!f) {
|
2003-06-03 21:31:53 +00:00
|
|
|
if (standalone) {
|
|
|
|
safe_strncpy(aid.app_preferences, "", sizeof(aid.app_preferences));
|
2003-09-27 23:20:40 +00:00
|
|
|
safe_strncpy(aid.user_name, "Unknown user", sizeof(aid.user_name));
|
|
|
|
safe_strncpy(aid.team_name, "Unknown team", sizeof(aid.team_name));
|
2003-06-03 21:31:53 +00:00
|
|
|
aid.wu_cpu_time = 1000;
|
|
|
|
aid.user_total_credit = 1000;
|
|
|
|
aid.user_expavg_credit = 500;
|
|
|
|
aid.host_total_credit = 1000;
|
|
|
|
aid.host_expavg_credit = 500;
|
|
|
|
aid.checkpoint_period = DEFAULT_CHECKPOINT_PERIOD;
|
|
|
|
aid.fraction_done_update_period = DEFAULT_FRACTION_DONE_UPDATE_PERIOD;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "boinc_init(): can't open init data file\n");
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
retval = parse_init_data_file(f, aid);
|
|
|
|
fclose(f);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't parse init data file\n");
|
|
|
|
return retval;
|
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
f = fopen(FD_INIT_FILE, "r");
|
|
|
|
if (f) {
|
|
|
|
parse_fd_init_file(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2003-06-03 21:31:53 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
time_until_checkpoint = aid.checkpoint_period;
|
|
|
|
time_until_fraction_done_update = aid.fraction_done_update_period;
|
|
|
|
this_process_active = true;
|
2003-08-21 23:44:57 +00:00
|
|
|
|
2002-10-28 22:30:37 +00:00
|
|
|
boinc_install_signal_handlers();
|
2002-11-26 21:33:43 +00:00
|
|
|
set_timer(timer_period);
|
2003-03-17 19:24:38 +00:00
|
|
|
setup_shared_mem();
|
2002-09-11 21:56:20 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-28 22:30:37 +00:00
|
|
|
// Install signal handlers to aid in debugging
|
|
|
|
// TODO: write Windows equivalent error handlers?
|
|
|
|
//
|
|
|
|
int boinc_install_signal_handlers() {
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
2003-09-05 21:26:21 +00:00
|
|
|
signal(SIGHUP, boinc_catch_signal); // terminal line hangup
|
|
|
|
signal(SIGINT, boinc_catch_signal); // interrupt program
|
|
|
|
signal(SIGQUIT, boinc_quit); // quit program
|
|
|
|
signal(SIGILL, boinc_catch_signal); // illegal instruction
|
|
|
|
signal(SIGABRT, boinc_catch_signal); // abort(2) call
|
|
|
|
signal(SIGBUS, boinc_catch_signal); // bus error
|
|
|
|
signal(SIGSEGV, boinc_catch_signal); // segmentation violation
|
|
|
|
signal(SIGSYS, boinc_catch_signal); // system call given invalid argument
|
|
|
|
signal(SIGPIPE, boinc_catch_signal); // write on a pipe with no reader
|
2002-11-21 23:27:27 +00:00
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
2003-09-05 21:26:21 +00:00
|
|
|
//SetUnhandledExceptionFilter(boinc_catch_signal);
|
2002-10-28 22:30:37 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-21 23:27:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LONG CALLBACK boinc_catch_signal(EXCEPTION_POINTERS *ExceptionInfo) {
|
|
|
|
PVOID exceptionAddr = ExceptionInfo->ExceptionRecord->ExceptionAddress;
|
|
|
|
DWORD exceptionCode = ExceptionInfo->ExceptionRecord->ExceptionCode;
|
|
|
|
char status[256];
|
2003-05-21 20:28:50 +00:00
|
|
|
static int already_caught_signal = 0;
|
|
|
|
|
|
|
|
// If we've been in this procedure before, something went wrong so we immediately exit
|
|
|
|
if (already_caught_signal) _exit(ERR_SIGNAL_CATCH);
|
|
|
|
already_caught_signal = 1;
|
2003-08-21 23:44:57 +00:00
|
|
|
|
2002-11-21 23:27:27 +00:00
|
|
|
switch (exceptionCode) {
|
2003-03-10 22:07:34 +00:00
|
|
|
case STATUS_WAIT_0: safe_strncpy(status,"Wait 0",sizeof(status)); break;
|
|
|
|
case STATUS_ABANDONED_WAIT_0: safe_strncpy(status,"Abandoned Wait 0",sizeof(status)); break;
|
|
|
|
case STATUS_USER_APC: safe_strncpy(status,"User APC",sizeof(status)); break;
|
|
|
|
case STATUS_TIMEOUT: safe_strncpy(status,"Timeout",sizeof(status)); break;
|
|
|
|
case STATUS_PENDING: safe_strncpy(status,"Pending",sizeof(status)); break;
|
2002-12-06 18:38:36 +00:00
|
|
|
case STATUS_SEGMENT_NOTIFICATION: return DBG_EXCEPTION_NOT_HANDLED;
|
2003-03-10 22:07:34 +00:00
|
|
|
case STATUS_GUARD_PAGE_VIOLATION: safe_strncpy(status,"Guard Page Violation",sizeof(status)); break;
|
|
|
|
case STATUS_DATATYPE_MISALIGNMENT: safe_strncpy(status,"Data Type Misalignment",sizeof(status)); break;
|
2002-12-06 18:38:36 +00:00
|
|
|
case STATUS_BREAKPOINT: return DBG_EXCEPTION_NOT_HANDLED;
|
|
|
|
case STATUS_SINGLE_STEP: return DBG_EXCEPTION_NOT_HANDLED;
|
2003-03-10 22:07:34 +00:00
|
|
|
case STATUS_ACCESS_VIOLATION: safe_strncpy(status,"Access Violation",sizeof(status)); break;
|
|
|
|
case STATUS_IN_PAGE_ERROR: safe_strncpy(status,"In Page Error",sizeof(status)); break;
|
|
|
|
case STATUS_NO_MEMORY: safe_strncpy(status,"No Memory",sizeof(status)); break;
|
|
|
|
case STATUS_ILLEGAL_INSTRUCTION: safe_strncpy(status,"Illegal Instruction",sizeof(status)); break;
|
|
|
|
case STATUS_NONCONTINUABLE_EXCEPTION: safe_strncpy(status,"Noncontinuable Exception",sizeof(status)); break;
|
|
|
|
case STATUS_INVALID_DISPOSITION: safe_strncpy(status,"Invalid Disposition",sizeof(status)); break;
|
|
|
|
case STATUS_ARRAY_BOUNDS_EXCEEDED: safe_strncpy(status,"Array Bounds Exceeded",sizeof(status)); break;
|
|
|
|
case STATUS_FLOAT_DENORMAL_OPERAND: safe_strncpy(status,"Float Denormal Operand",sizeof(status)); break;
|
|
|
|
case STATUS_FLOAT_DIVIDE_BY_ZERO: safe_strncpy(status,"Divide by Zero",sizeof(status)); break;
|
|
|
|
case STATUS_FLOAT_INEXACT_RESULT: safe_strncpy(status,"Float Inexact Result",sizeof(status)); break;
|
|
|
|
case STATUS_FLOAT_INVALID_OPERATION: safe_strncpy(status,"Float Invalid Operation",sizeof(status)); break;
|
|
|
|
case STATUS_FLOAT_OVERFLOW: safe_strncpy(status,"Float Overflow",sizeof(status)); break;
|
|
|
|
case STATUS_FLOAT_STACK_CHECK: safe_strncpy(status,"Float Stack Check",sizeof(status)); break;
|
|
|
|
case STATUS_FLOAT_UNDERFLOW: safe_strncpy(status,"Float Underflow",sizeof(status)); break;
|
|
|
|
case STATUS_INTEGER_DIVIDE_BY_ZERO: safe_strncpy(status,"Integer Divide by Zero",sizeof(status)); break;
|
|
|
|
case STATUS_INTEGER_OVERFLOW: safe_strncpy(status,"Integer Overflow",sizeof(status)); break;
|
|
|
|
case STATUS_PRIVILEGED_INSTRUCTION: safe_strncpy(status,"Privileged Instruction",sizeof(status)); break;
|
|
|
|
case STATUS_STACK_OVERFLOW: safe_strncpy(status,"Stack Overflow",sizeof(status)); break;
|
|
|
|
case STATUS_CONTROL_C_EXIT: safe_strncpy(status,"Ctrl+C Exit",sizeof(status)); break;
|
|
|
|
default: safe_strncpy(status,"Unknown exception",sizeof(status)); break;
|
2002-11-21 23:27:27 +00:00
|
|
|
}
|
|
|
|
// TODO: also output info in CONTEXT structure?
|
2003-09-05 21:26:21 +00:00
|
|
|
fprintf(stderr, "\n***UNHANDLED EXCEPTION****\n");
|
|
|
|
fprintf(stderr, "Reason: %s at address 0x%p\n",status,exceptionAddr);
|
|
|
|
fprintf(stderr, "Exiting...\n");
|
2002-11-21 23:27:27 +00:00
|
|
|
fflush(stderr);
|
2003-05-21 20:28:50 +00:00
|
|
|
_exit(ERR_SIGNAL_CATCH);
|
2002-11-21 23:27:27 +00:00
|
|
|
return(EXCEPTION_EXECUTE_HANDLER);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-10-28 22:30:37 +00:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
2002-11-21 23:27:27 +00:00
|
|
|
void boinc_catch_signal(int signal) {
|
2002-10-28 22:30:37 +00:00
|
|
|
switch(signal) {
|
2003-09-05 21:26:21 +00:00
|
|
|
case SIGHUP: fprintf(stderr, "SIGHUP: terminal line hangup"); break;
|
|
|
|
case SIGINT: fprintf(stderr, "SIGINT: interrupt program"); break;
|
|
|
|
case SIGILL: fprintf(stderr, "SIGILL: illegal instruction"); break;
|
|
|
|
case SIGABRT: fprintf(stderr, "SIGABRT: abort called"); break;
|
|
|
|
case SIGBUS: fprintf(stderr, "SIGBUS: bus error"); break;
|
|
|
|
case SIGSEGV: fprintf(stderr, "SIGSEGV: segmentation violation"); break;
|
|
|
|
case SIGSYS: fprintf(stderr, "SIGSYS: system call given invalid argument"); break;
|
|
|
|
case SIGPIPE: fprintf(stderr, "SIGPIPE: write on a pipe with no reader"); break;
|
|
|
|
default: fprintf(stderr, "unknown signal %d", signal); break;
|
2002-10-28 22:30:37 +00:00
|
|
|
}
|
2003-09-05 21:26:21 +00:00
|
|
|
fprintf(stderr, "\nExiting...\n");
|
2002-10-28 23:06:44 +00:00
|
|
|
exit(ERR_SIGNAL_CATCH);
|
2002-10-28 22:30:37 +00:00
|
|
|
}
|
2003-03-12 18:15:48 +00:00
|
|
|
|
|
|
|
void boinc_quit(int sig) {
|
2003-09-05 21:26:21 +00:00
|
|
|
signal(SIGQUIT, boinc_quit); // reset signal
|
2003-03-12 18:15:48 +00:00
|
|
|
time_to_quit = true;
|
|
|
|
}
|
2002-11-21 23:27:27 +00:00
|
|
|
#endif
|
2002-10-28 22:30:37 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
int boinc_finish(int status) {
|
2003-07-15 18:19:29 +00:00
|
|
|
double cur_mem;
|
2003-05-28 20:58:01 +00:00
|
|
|
|
|
|
|
boinc_cpu_time(last_checkpoint_cpu_time, cur_mem);
|
|
|
|
update_app_progress(fraction_done, last_checkpoint_cpu_time, last_checkpoint_cpu_time, cur_mem);
|
2002-11-14 00:49:24 +00:00
|
|
|
#ifdef _WIN32
|
2002-11-18 23:09:11 +00:00
|
|
|
// Stop the timer
|
2002-11-14 00:49:24 +00:00
|
|
|
timeKillEvent(timer_id);
|
2003-09-30 21:17:20 +00:00
|
|
|
boinc_finish_opengl();
|
2002-11-14 00:49:24 +00:00
|
|
|
#endif
|
2003-03-17 19:24:38 +00:00
|
|
|
cleanup_shared_mem();
|
2002-08-05 00:29:34 +00:00
|
|
|
exit(status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-09 23:34:11 +00:00
|
|
|
int boinc_get_init_data(APP_INIT_DATA& app_init_data) {
|
|
|
|
app_init_data = aid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
bool boinc_time_to_checkpoint() {
|
2003-06-06 21:49:18 +00:00
|
|
|
#ifdef __APPLE_CC__
|
2003-06-24 20:44:32 +00:00
|
|
|
//YieldToAnyThread();
|
2003-06-10 17:53:18 +00:00
|
|
|
#elif _WIN32
|
2003-03-12 18:15:48 +00:00
|
|
|
DWORD eventState;
|
|
|
|
// Check if core client has requested us to exit
|
2003-03-12 19:21:29 +00:00
|
|
|
eventState = WaitForSingleObject(hQuitRequest, 0L);
|
2003-08-21 23:44:57 +00:00
|
|
|
|
2003-03-12 18:15:48 +00:00
|
|
|
switch (eventState) {
|
2003-09-27 17:53:43 +00:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_ABANDONED:
|
|
|
|
time_to_quit = true;
|
|
|
|
break;
|
2002-12-02 22:51:11 +00:00
|
|
|
}
|
2003-03-12 18:15:48 +00:00
|
|
|
#endif
|
2002-12-02 22:51:11 +00:00
|
|
|
|
|
|
|
if (write_frac_done) {
|
2003-05-28 22:27:58 +00:00
|
|
|
double cur_cpu;
|
2003-07-15 18:19:29 +00:00
|
|
|
double cur_mem;
|
2003-05-28 20:58:01 +00:00
|
|
|
boinc_cpu_time(cur_cpu, cur_mem);
|
|
|
|
update_app_progress(fraction_done, cur_cpu, last_checkpoint_cpu_time, cur_mem);
|
2002-12-02 22:51:11 +00:00
|
|
|
time_until_fraction_done_update = aid.fraction_done_update_period;
|
|
|
|
write_frac_done = false;
|
|
|
|
}
|
|
|
|
|
2003-02-27 23:42:38 +00:00
|
|
|
// If the application has received a quit request it should checkpoint
|
2003-08-21 23:44:57 +00:00
|
|
|
//
|
2003-02-27 23:42:38 +00:00
|
|
|
if (time_to_quit) {
|
2002-11-18 23:09:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
return ready_to_checkpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_checkpoint_completed() {
|
2003-07-15 18:19:29 +00:00
|
|
|
double cur_mem;
|
2003-05-28 20:58:01 +00:00
|
|
|
boinc_cpu_time(last_checkpoint_cpu_time, cur_mem);
|
|
|
|
update_app_progress(fraction_done, last_checkpoint_cpu_time, last_checkpoint_cpu_time, cur_mem);
|
2002-08-05 00:29:34 +00:00
|
|
|
ready_to_checkpoint = false;
|
|
|
|
time_until_checkpoint = aid.checkpoint_period;
|
2003-09-05 21:26:21 +00:00
|
|
|
|
|
|
|
// If it's time to quit, call boinc_finish which will exit the app properly
|
|
|
|
//
|
2002-11-18 23:09:11 +00:00
|
|
|
if (time_to_quit) {
|
2002-11-19 00:28:33 +00:00
|
|
|
boinc_finish(ERR_QUIT_REQUEST);
|
2002-11-18 23:09:11 +00:00
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_fraction_done(double x) {
|
|
|
|
fraction_done = x;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_child_start() {
|
|
|
|
this_process_active = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_child_done(double cpu) {
|
|
|
|
this_process_active = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-07-15 18:19:29 +00:00
|
|
|
int boinc_cpu_time(double &cpu_t, double &ws_t) {
|
2002-11-20 20:14:48 +00:00
|
|
|
double cpu_secs;
|
|
|
|
|
|
|
|
// Start with the CPU time from previous runs, then
|
|
|
|
// add the CPU time of the current run
|
|
|
|
cpu_secs = aid.wu_cpu_time;
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
2003-09-30 18:09:58 +00:00
|
|
|
int retval;
|
2002-08-05 00:29:34 +00:00
|
|
|
struct rusage ru;
|
|
|
|
retval = getrusage(RUSAGE_SELF, &ru);
|
2003-09-27 17:53:43 +00:00
|
|
|
if (retval) {
|
2003-09-30 18:09:58 +00:00
|
|
|
fprintf(stderr, "error: could not get CPU time\n");
|
2003-05-28 20:58:01 +00:00
|
|
|
return ERR_GETRUSAGE;
|
|
|
|
}
|
2002-10-19 05:22:02 +00:00
|
|
|
// Sum the user and system time spent in this process
|
2002-11-20 20:14:48 +00:00
|
|
|
cpu_secs += (double)ru.ru_utime.tv_sec + (((double)ru.ru_utime.tv_usec) / ((double)1000000.0));
|
2002-10-19 05:22:02 +00:00
|
|
|
cpu_secs += (double)ru.ru_stime.tv_sec + (((double)ru.ru_stime.tv_usec) / ((double)1000000.0));
|
2003-05-28 20:58:01 +00:00
|
|
|
cpu_t = cpu_secs;
|
2003-07-15 18:19:29 +00:00
|
|
|
ws_t = ru.ru_idrss; // TODO: fix this (mult by page size)
|
2003-05-28 20:58:01 +00:00
|
|
|
return 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
#else
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE hProcess;
|
|
|
|
FILETIME creationTime,exitTime,kernelTime,userTime;
|
|
|
|
|
2002-10-31 00:24:25 +00:00
|
|
|
// TODO: Could we speed this up by retaining the process handle?
|
2002-08-05 00:29:34 +00:00
|
|
|
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, GetCurrentProcessId());
|
|
|
|
if (GetProcessTimes(
|
|
|
|
hProcess, &creationTime, &exitTime, &kernelTime, &userTime)
|
2002-09-11 18:28:36 +00:00
|
|
|
) {
|
2002-08-05 00:29:34 +00:00
|
|
|
ULARGE_INTEGER tKernel, tUser;
|
|
|
|
LONGLONG totTime;
|
|
|
|
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
|
|
|
|
tKernel.LowPart = kernelTime.dwLowDateTime;
|
|
|
|
tKernel.HighPart = kernelTime.dwHighDateTime;
|
|
|
|
tUser.LowPart = userTime.dwLowDateTime;
|
|
|
|
tUser.HighPart = userTime.dwHighDateTime;
|
2002-11-21 23:27:27 +00:00
|
|
|
totTime = tKernel.QuadPart + tUser.QuadPart;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
// Runtimes in 100-nanosecond units
|
2003-03-17 19:24:38 +00:00
|
|
|
cpu_secs += totTime / 1.e7;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
// Convert to seconds and return
|
2003-05-28 20:58:01 +00:00
|
|
|
cpu_t = cpu_secs;
|
|
|
|
ws_t = 0;
|
|
|
|
return 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
2002-08-09 21:43:59 +00:00
|
|
|
CloseHandle(hProcess);
|
2002-11-20 20:19:30 +00:00
|
|
|
|
2002-11-21 23:27:27 +00:00
|
|
|
// TODO: Handle timer wraparound
|
2002-08-24 00:41:25 +00:00
|
|
|
static bool first=true;
|
2002-11-20 20:14:48 +00:00
|
|
|
static DWORD first_count = 0;
|
2002-08-24 00:41:25 +00:00
|
|
|
|
|
|
|
if (first) {
|
2002-11-20 20:14:48 +00:00
|
|
|
first_count = GetTickCount();
|
|
|
|
first = false;
|
2002-08-24 00:41:25 +00:00
|
|
|
}
|
|
|
|
DWORD cur = GetTickCount();
|
2003-05-28 20:58:01 +00:00
|
|
|
cpu_t = cpu_secs + ((cur - first_count)/1000.);
|
|
|
|
ws_t = 0;
|
|
|
|
return 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif // _WIN32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fprintf(stderr, "boinc_cpu_time(): not implemented\n");
|
2003-05-28 20:58:01 +00:00
|
|
|
return -1;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2003-09-30 18:09:58 +00:00
|
|
|
// This function should be as fast as possible,
|
|
|
|
// and shouldn't make any system calls
|
2002-12-02 22:51:11 +00:00
|
|
|
//
|
2002-08-05 00:29:34 +00:00
|
|
|
#ifdef _WIN32
|
2002-10-25 18:23:59 +00:00
|
|
|
void CALLBACK on_timer(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) {
|
2002-08-05 00:29:34 +00:00
|
|
|
#else
|
|
|
|
void on_timer(int a) {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!ready_to_checkpoint) {
|
|
|
|
time_until_checkpoint -= timer_period;
|
|
|
|
if (time_until_checkpoint <= 0) {
|
|
|
|
ready_to_checkpoint = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-02 22:51:11 +00:00
|
|
|
if (!write_frac_done && this_process_active) {
|
2002-08-05 00:29:34 +00:00
|
|
|
time_until_fraction_done_update -= timer_period;
|
2002-12-02 22:51:11 +00:00
|
|
|
if (time_until_fraction_done_update <= 0) {
|
|
|
|
write_frac_done = true;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-29 18:51:45 +00:00
|
|
|
|
2003-09-30 21:17:20 +00:00
|
|
|
#if 0
|
|
|
|
// ???? shouldn't have graphics-related stuff here
|
2002-11-07 18:52:47 +00:00
|
|
|
#if defined __APPLE_CC__ && defined BOINC_APP_GRAPHICS
|
|
|
|
// Yield to the graphics thread to let it draw if needed
|
2002-10-29 18:51:45 +00:00
|
|
|
YieldToAnyThread();
|
|
|
|
#endif
|
2003-09-30 21:17:20 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int set_timer(double period) {
|
|
|
|
int retval=0;
|
|
|
|
#ifdef _WIN32
|
2003-03-18 19:37:09 +00:00
|
|
|
char buf[256];
|
|
|
|
|
2002-10-28 22:30:37 +00:00
|
|
|
// Use Windows multimedia timer, since it is more accurate
|
|
|
|
// than SetTimer and doesn't require an associated event loop
|
2002-11-14 00:49:24 +00:00
|
|
|
timer_id = timeSetEvent(
|
2002-10-28 22:30:37 +00:00
|
|
|
(int)(period*1000), // uDelay
|
|
|
|
(int)(period*1000), // uResolution
|
|
|
|
on_timer, // lpTimeProc
|
|
|
|
NULL, // dwUser
|
|
|
|
TIME_PERIODIC // fuEvent
|
2003-09-05 21:26:21 +00:00
|
|
|
);
|
2003-03-18 19:37:09 +00:00
|
|
|
sprintf(buf, "%s%s", QUIT_PREFIX, aid.comm_obj_name);
|
|
|
|
hQuitRequest = OpenEvent(EVENT_ALL_ACCESS, FALSE, buf);
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#if HAVE_SYS_TIME_H
|
|
|
|
struct sigaction sa;
|
|
|
|
itimerval value;
|
2002-12-02 22:51:11 +00:00
|
|
|
sa.sa_handler = on_timer;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
retval = sigaction(SIGALRM, &sa, NULL);
|
|
|
|
if (retval) {
|
2003-09-05 21:26:21 +00:00
|
|
|
perror("boinc set_timer() sigaction");
|
2002-12-02 22:51:11 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
value.it_value.tv_sec = (int)period;
|
|
|
|
value.it_value.tv_usec = ((int)(period*1000000))%1000000;
|
|
|
|
value.it_interval = value.it_value;
|
2002-10-19 05:22:02 +00:00
|
|
|
retval = setitimer(ITIMER_REAL, &value, NULL);
|
2002-12-02 22:51:11 +00:00
|
|
|
if (retval) {
|
2003-09-05 21:26:21 +00:00
|
|
|
perror("boinc set_timer() setitimer");
|
2002-12-02 22:51:11 +00:00
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
void setup_shared_mem() {
|
2003-05-21 23:23:42 +00:00
|
|
|
app_client_shm = new APP_CLIENT_SHM;
|
2003-08-21 23:44:57 +00:00
|
|
|
if (standalone) {
|
|
|
|
app_client_shm->shm = NULL;
|
2003-09-05 21:26:21 +00:00
|
|
|
fprintf(stderr, "Standalone mode, so not attaching to shared memory.\n");
|
2003-08-21 23:44:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-03-17 19:24:38 +00:00
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "%s%s", SHM_PREFIX, aid.comm_obj_name);
|
2003-05-05 23:40:34 +00:00
|
|
|
hSharedMem = attach_shmem(buf, (void**)&app_client_shm->shm);
|
2003-09-05 21:26:21 +00:00
|
|
|
if (hSharedMem == NULL) {
|
2003-03-18 19:37:09 +00:00
|
|
|
app_client_shm = NULL;
|
2003-09-05 21:26:21 +00:00
|
|
|
}
|
2003-03-18 19:37:09 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
#ifdef HAVE_SYS_SHM_H
|
|
|
|
#ifdef HAVE_SYS_IPC_H
|
2003-05-05 23:40:34 +00:00
|
|
|
if (attach_shmem(aid.shm_key, (void**)&app_client_shm->shm)) {
|
2003-03-17 19:24:38 +00:00
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
void cleanup_shared_mem() {
|
2003-06-24 20:54:05 +00:00
|
|
|
if (!app_client_shm) return;
|
2003-08-21 23:44:57 +00:00
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
2003-09-05 21:26:21 +00:00
|
|
|
if (app_client_shm->shm != NULL) {
|
2003-05-05 23:40:34 +00:00
|
|
|
detach_shmem(hSharedMem, app_client_shm->shm);
|
2003-09-05 21:26:21 +00:00
|
|
|
}
|
2003-03-18 19:37:09 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
#ifdef HAVE_SYS_SHM_H
|
|
|
|
#ifdef HAVE_SYS_IPC_H
|
2003-09-05 21:26:21 +00:00
|
|
|
if (app_client_shm->shm != NULL) {
|
2003-05-05 23:40:34 +00:00
|
|
|
detach_shmem(app_client_shm->shm);
|
2003-09-05 21:26:21 +00:00
|
|
|
}
|
2003-03-17 19:24:38 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-05-07 23:42:17 +00:00
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
// communicate to the core client (via shared mem)
|
|
|
|
// the current CPU time and fraction done
|
|
|
|
//
|
|
|
|
int update_app_progress(
|
|
|
|
double frac_done, double cpu_t, double cp_cpu_t, double ws_t
|
|
|
|
) {
|
2003-05-21 20:28:50 +00:00
|
|
|
char msg_buf[SHM_SEG_SIZE];
|
2003-08-21 23:44:57 +00:00
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
if (!app_client_shm) return 0;
|
|
|
|
|
|
|
|
sprintf(msg_buf,
|
2003-05-05 23:40:34 +00:00
|
|
|
"<fraction_done>%2.8f</fraction_done>\n"
|
|
|
|
"<current_cpu_time>%10.4f</current_cpu_time>\n"
|
2003-05-28 22:27:58 +00:00
|
|
|
"<checkpoint_cpu_time>%10.4f</checkpoint_cpu_time>\n"
|
2003-07-15 18:19:29 +00:00
|
|
|
"<working_set_size>%f</working_set_size>\n",
|
2003-05-28 20:58:01 +00:00
|
|
|
frac_done, cpu_t, cp_cpu_t, ws_t
|
2003-05-21 20:28:50 +00:00
|
|
|
);
|
2003-05-05 23:40:34 +00:00
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
return app_client_shm->send_msg(msg_buf, APP_CORE_WORKER_SEG);
|
2003-03-17 19:24:38 +00:00
|
|
|
}
|