2002-08-05 00:29:34 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2003-05-07 23:42:17 +00:00
|
|
|
// Code that's in the BOINC app library (but NOT in the core client)
|
|
|
|
|
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>
|
|
|
|
|
2002-10-31 00:24:25 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
|
|
|
#ifdef _WIN32
|
2003-02-07 00:06:10 +00:00
|
|
|
HANDLE hQuitEvent;
|
2002-11-14 00:56:01 +00:00
|
|
|
extern HANDLE graphics_threadh;
|
2002-11-18 23:09:11 +00:00
|
|
|
extern BOOL win_loop_done;
|
2002-10-31 00:24:25 +00:00
|
|
|
#endif
|
2003-05-14 23:51:54 +00:00
|
|
|
#ifdef __APPLE_CC__
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#endif
|
2002-10-31 00:24:25 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#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
|
|
|
|
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-09-18 22:19:38 +00:00
|
|
|
GRAPHICS_INFO gi;
|
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-05-07 23:42:17 +00:00
|
|
|
APP_CLIENT_SHM *app_client_shm;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
// read the INIT_DATA and FD_INIT files
|
|
|
|
//
|
|
|
|
int boinc_init() {
|
2003-06-03 21:31:53 +00:00
|
|
|
bool standalone = false;
|
|
|
|
FILE* f;
|
|
|
|
int retval;
|
|
|
|
|
2002-08-22 20:19:18 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
freopen(STDERR_FILE, "a", stderr);
|
|
|
|
#endif
|
|
|
|
|
2003-06-03 21:31:53 +00:00
|
|
|
#ifdef API_STANDALONE
|
|
|
|
standalone = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
safe_strncpy(aid.user_name, "John Smith", sizeof(aid.user_name));
|
|
|
|
safe_strncpy(aid.team_name, "The A-Team", sizeof(aid.team_name));
|
|
|
|
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
|
|
|
|
2002-09-11 21:56:20 +00:00
|
|
|
f = fopen(GRAPHICS_DATA_FILE, "r");
|
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't open graphics data file\n");
|
2003-02-14 18:47:26 +00:00
|
|
|
fprintf(stderr, "Using default graphics settings.\n");
|
2003-06-03 21:31:53 +00:00
|
|
|
gi.refresh_period = 0.1; // 1/10th of a second
|
2003-02-14 18:47:26 +00:00
|
|
|
gi.xsize = 640;
|
|
|
|
gi.ysize = 480;
|
2003-03-28 18:10:36 +00:00
|
|
|
} else {
|
2003-02-14 18:47:26 +00:00
|
|
|
retval = parse_graphics_file(f, &gi);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't parse graphics data file\n");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
fclose(f);
|
2002-09-11 21:56:20 +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;
|
2002-10-29 18:51:45 +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
|
2002-11-21 23:27:27 +00:00
|
|
|
signal( SIGHUP, boinc_catch_signal ); // terminal line hangup
|
|
|
|
signal( SIGINT, boinc_catch_signal ); // interrupt program
|
2003-03-12 18:15:48 +00:00
|
|
|
signal( SIGQUIT, boinc_quit ); // quit program
|
2002-11-21 23:27:27 +00:00
|
|
|
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
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
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;
|
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?
|
|
|
|
fprintf( stderr, "\n***UNHANDLED EXCEPTION****\n");
|
|
|
|
fprintf( stderr, "Reason: %s at address 0x%p\n",status,exceptionAddr);
|
|
|
|
fprintf( stderr, "Exiting...\n" );
|
|
|
|
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) {
|
2002-12-06 18:38:36 +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
|
|
|
}
|
2002-10-28 23:06:44 +00:00
|
|
|
fprintf( stderr, "\nExiting...\n" );
|
|
|
|
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-03-18 19:37:09 +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-05-28 22:27:58 +00:00
|
|
|
int 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);
|
2002-11-14 00:56:01 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2002-11-26 21:33:43 +00:00
|
|
|
if (using_opengl) {
|
|
|
|
// If the graphics thread is running, tell it to quit and wait for it
|
|
|
|
win_loop_done = TRUE;
|
|
|
|
if (hQuitEvent != NULL) {
|
|
|
|
WaitForSingleObject(hQuitEvent, 1000); // Wait up to 1000 ms
|
|
|
|
}
|
2002-11-14 00:49:24 +00:00
|
|
|
}
|
2002-11-14 00:56:01 +00:00
|
|
|
#endif
|
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
|
|
|
// resolve XML soft links
|
|
|
|
//
|
2002-09-22 23:27:14 +00:00
|
|
|
int boinc_resolve_filename(char *virtual_name, char *physical_name, int len) {
|
2002-08-05 00:29:34 +00:00
|
|
|
FILE *fp;
|
|
|
|
char buf[512];
|
|
|
|
|
2003-03-10 22:07:34 +00:00
|
|
|
safe_strncpy(physical_name, virtual_name, len);
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
// Open the file and load the first line
|
|
|
|
fp = fopen(virtual_name, "r");
|
|
|
|
if (!fp) return 0;
|
|
|
|
|
|
|
|
fgets(buf, 512, fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
// If it's the <soft_link> XML tag, return its value,
|
|
|
|
// otherwise, return the original file name
|
|
|
|
//
|
2002-09-22 23:27:14 +00:00
|
|
|
parse_str(buf, "<soft_link>", physical_name, len);
|
2002-08-05 00:29:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool boinc_time_to_checkpoint() {
|
2003-03-12 18:15:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD eventState;
|
|
|
|
// Check if core client has requested us to exit
|
2003-03-12 19:21:29 +00:00
|
|
|
eventState = WaitForSingleObject(hQuitRequest, 0L);
|
2003-03-12 18:15:48 +00:00
|
|
|
|
|
|
|
switch (eventState) {
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_ABANDONED:
|
2003-03-17 19:24:38 +00:00
|
|
|
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;
|
|
|
|
int 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
|
|
|
|
//
|
|
|
|
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-05-28 22:27:58 +00:00
|
|
|
int 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;
|
2002-11-18 23:09:11 +00:00
|
|
|
// If it's time to quit, call boinc_finish which will
|
|
|
|
// exit the app properly
|
|
|
|
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-05-28 22:27:58 +00:00
|
|
|
int boinc_cpu_time(double &cpu_t, int &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
|
|
|
|
int retval, pid = getpid();
|
|
|
|
struct rusage ru;
|
|
|
|
retval = getrusage(RUSAGE_SELF, &ru);
|
2003-05-28 20:58:01 +00:00
|
|
|
if(retval) {
|
|
|
|
fprintf(stderr, "error: could not get cpu time for %d\n", pid);
|
|
|
|
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-05-28 22:27:58 +00:00
|
|
|
ws_t = ru.ru_idrss;
|
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
|
|
|
}
|
|
|
|
|
2002-12-02 22:51:11 +00:00
|
|
|
// This function should be as fast as possible, and shouldn't make any system calls
|
|
|
|
//
|
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
|
|
|
|
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
|
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-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-11-14 00:49:24 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2002-10-28 22:30:37 +00:00
|
|
|
// Create the event object used to signal between the
|
|
|
|
// worker and event threads
|
2002-11-14 00:49:24 +00:00
|
|
|
|
|
|
|
hQuitEvent = CreateEvent(
|
|
|
|
NULL, // no security attributes
|
|
|
|
TRUE, // manual reset event
|
|
|
|
TRUE, // initial state is signaled
|
|
|
|
NULL); // object not named
|
|
|
|
#endif
|
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) {
|
|
|
|
perror( "boinc set_timer() sigaction" );
|
|
|
|
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) {
|
|
|
|
perror( "boinc set_timer() setitimer" );
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
void setup_shared_mem(void) {
|
2003-05-21 23:23:42 +00:00
|
|
|
app_client_shm = new APP_CLIENT_SHM;
|
|
|
|
#ifdef API_STANDALONE
|
2003-05-20 22:32:03 +00:00
|
|
|
app_client_shm->shm = NULL;
|
2003-05-21 23:23:42 +00:00
|
|
|
fprintf( stderr, "Standalone mode, so not attaching to shared memory.\n" );
|
2003-03-17 19:24:38 +00:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
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-03-18 19:37:09 +00:00
|
|
|
if (hSharedMem == NULL)
|
|
|
|
app_client_shm = NULL;
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_shared_mem(void) {
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (app_client_shm != NULL)
|
2003-05-05 23:40:34 +00:00
|
|
|
detach_shmem(hSharedMem, app_client_shm->shm);
|
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
|
|
|
|
if (app_client_shm != NULL)
|
2003-05-05 23:40:34 +00:00
|
|
|
detach_shmem(app_client_shm->shm);
|
2003-03-17 19:24:38 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-05-07 23:42:17 +00:00
|
|
|
|
2003-05-28 22:27:58 +00:00
|
|
|
int update_app_progress(double frac_done, double cpu_t, double cp_cpu_t, int ws_t) {
|
2003-05-21 20:28:50 +00:00
|
|
|
char msg_buf[SHM_SEG_SIZE];
|
2003-03-17 19:24:38 +00:00
|
|
|
|
2003-05-05 23:40:34 +00:00
|
|
|
sprintf( msg_buf,
|
|
|
|
"<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"
|
|
|
|
"<working_set_size>%d</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
|
|
|
|
|
|
|
return app_client_shm->send_msg(msg_buf, APP_CORE_WORKER_SEG);
|
2003-03-17 19:24:38 +00:00
|
|
|
}
|