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):
|
|
|
|
//
|
|
|
|
|
2002-09-11 21:56:20 +00:00
|
|
|
// API_IGNORE_CLIENT will make the app ignore the core client
|
|
|
|
// this is useful for debugging just the application
|
2002-10-02 20:38:32 +00:00
|
|
|
//#define API_IGNORE_CLIENT
|
2002-09-11 21:56:20 +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>
|
|
|
|
|
2002-10-31 00:24:25 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
|
|
|
#ifdef __APPLE_CC__
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <gl\gl.h> // Header File For The OpenGL32 Library
|
|
|
|
#include <gl\glu.h> // Header File For The GLu32 Library
|
|
|
|
#include <gl\glaux.h> // Header File For The Glaux Library
|
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
|
2002-11-12 20:59:00 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_GL_LIB
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#endif
|
2002-10-31 00:24:25 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "parse.h"
|
2002-11-18 23:09:11 +00:00
|
|
|
#include "util.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "error_numbers.h"
|
2002-09-11 21:56:20 +00:00
|
|
|
#include "graphics_api.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2002-10-29 18:51:45 +00:00
|
|
|
#ifdef __APPLE_CC__
|
|
|
|
#include <CoreServices/CoreServices.h>
|
|
|
|
#include "mac_app_opengl.h"
|
|
|
|
#endif
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "boinc_api.h"
|
|
|
|
|
2002-11-22 19:57:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LONG CALLBACK boinc_catch_signal(EXCEPTION_POINTERS *ExceptionInfo);
|
|
|
|
#else
|
|
|
|
extern void boinc_catch_signal(int signal);
|
|
|
|
#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;
|
2003-02-27 23:42:38 +00:00
|
|
|
static double time_until_quit_check;
|
2002-08-05 00:29:34 +00:00
|
|
|
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;
|
2003-02-27 23:42:38 +00:00
|
|
|
static bool check_quit = 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;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
// read the INIT_DATA and FD_INIT files
|
|
|
|
//
|
|
|
|
int boinc_init() {
|
2002-08-22 20:19:18 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
freopen(STDERR_FILE, "a", stderr);
|
|
|
|
#endif
|
|
|
|
|
2002-09-11 21:56:20 +00:00
|
|
|
#ifndef API_IGNORE_CLIENT
|
2002-09-26 05:57:10 +00:00
|
|
|
FILE* f;
|
|
|
|
int retval;
|
2002-08-05 00:29:34 +00:00
|
|
|
f = fopen(INIT_DATA_FILE, "r");
|
2002-08-24 00:41:25 +00:00
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't open init data file\n");
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
retval = parse_init_data_file(f, aid);
|
2002-08-24 00:41:25 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't parse init data file\n");
|
|
|
|
return retval;
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
fclose(f);
|
|
|
|
|
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");
|
|
|
|
gi.refresh_period = 0.5;
|
|
|
|
gi.xsize = 640;
|
|
|
|
gi.ysize = 480;
|
|
|
|
}
|
|
|
|
if (f) {
|
|
|
|
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);
|
|
|
|
}
|
2002-09-11 21:56:20 +00:00
|
|
|
#else
|
2002-09-18 22:15:49 +00:00
|
|
|
strcpy(aid.app_preferences, "");
|
|
|
|
strcpy(aid.user_name, "John Smith");
|
|
|
|
strcpy(aid.team_name, "The A-Team");
|
2002-09-11 21:56:20 +00:00
|
|
|
aid.wu_cpu_time = 1000;
|
2003-02-24 21:25:16 +00:00
|
|
|
aid.user_total_credit = 1000;
|
|
|
|
aid.user_expavg_credit = 500;
|
|
|
|
aid.host_total_credit = 1000;
|
|
|
|
aid.host_expavg_credit = 500;
|
2002-09-11 21:56:20 +00:00
|
|
|
aid.checkpoint_period = DEFAULT_CHECKPOINT_PERIOD;
|
|
|
|
aid.fraction_done_update_period = DEFAULT_FRACTION_DONE_UPDATE_PERIOD;
|
|
|
|
|
2002-09-18 22:15:49 +00:00
|
|
|
gi.graphics_mode = MODE_WINDOW;
|
|
|
|
gi.refresh_period = 0.1; // 1/10th of a second
|
|
|
|
gi.xsize = 640;
|
|
|
|
gi.ysize = 480;
|
2002-09-11 21:56:20 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
time_until_checkpoint = aid.checkpoint_period;
|
|
|
|
time_until_fraction_done_update = aid.fraction_done_update_period;
|
2003-02-27 23:42:38 +00:00
|
|
|
time_until_quit_check = 1; // check every 1 second for quit request from core client
|
2002-08-05 00:29:34 +00:00
|
|
|
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);
|
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
|
|
|
|
signal( SIGQUIT, boinc_catch_signal ); // 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
|
|
|
|
#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];
|
|
|
|
|
|
|
|
switch (exceptionCode) {
|
2002-12-06 18:38:36 +00:00
|
|
|
case STATUS_WAIT_0: strcpy(status,"Wait 0"); break;
|
|
|
|
case STATUS_ABANDONED_WAIT_0: strcpy(status,"Abandoned Wait 0"); break;
|
|
|
|
case STATUS_USER_APC: strcpy(status,"User APC"); break;
|
|
|
|
case STATUS_TIMEOUT: strcpy(status,"Timeout"); break;
|
|
|
|
case STATUS_PENDING: strcpy(status,"Pending"); break;
|
|
|
|
case STATUS_SEGMENT_NOTIFICATION: return DBG_EXCEPTION_NOT_HANDLED;
|
|
|
|
case STATUS_GUARD_PAGE_VIOLATION: strcpy(status,"Guard Page Violation"); break;
|
|
|
|
case STATUS_DATATYPE_MISALIGNMENT: strcpy(status,"Data Type Misalignment"); break;
|
|
|
|
case STATUS_BREAKPOINT: return DBG_EXCEPTION_NOT_HANDLED;
|
|
|
|
case STATUS_SINGLE_STEP: return DBG_EXCEPTION_NOT_HANDLED;
|
|
|
|
case STATUS_ACCESS_VIOLATION: strcpy(status,"Access Violation"); break;
|
|
|
|
case STATUS_IN_PAGE_ERROR: strcpy(status,"In Page Error"); break;
|
|
|
|
case STATUS_NO_MEMORY: strcpy(status,"No Memory"); break;
|
|
|
|
case STATUS_ILLEGAL_INSTRUCTION: strcpy(status,"Illegal Instruction"); break;
|
|
|
|
case STATUS_NONCONTINUABLE_EXCEPTION: strcpy(status,"Noncontinuable Exception"); break;
|
|
|
|
case STATUS_INVALID_DISPOSITION: strcpy(status,"Invalid Disposition"); break;
|
|
|
|
case STATUS_ARRAY_BOUNDS_EXCEEDED: strcpy(status,"Array Bounds Exceeded"); break;
|
|
|
|
case STATUS_FLOAT_DENORMAL_OPERAND: strcpy(status,"Float Denormal Operand"); break;
|
|
|
|
case STATUS_FLOAT_DIVIDE_BY_ZERO: strcpy(status,"Divide by Zero"); break;
|
|
|
|
case STATUS_FLOAT_INEXACT_RESULT: strcpy(status,"Float Inexact Result"); break;
|
|
|
|
case STATUS_FLOAT_INVALID_OPERATION: strcpy(status,"Float Invalid Operation"); break;
|
|
|
|
case STATUS_FLOAT_OVERFLOW: strcpy(status,"Float Overflow"); break;
|
|
|
|
case STATUS_FLOAT_STACK_CHECK: strcpy(status,"Float Stack Check"); break;
|
|
|
|
case STATUS_FLOAT_UNDERFLOW: strcpy(status,"Float Uderflow"); break;
|
|
|
|
case STATUS_INTEGER_DIVIDE_BY_ZERO: strcpy(status,"Integer Divide by Zero"); break;
|
|
|
|
case STATUS_INTEGER_OVERFLOW: strcpy(status,"Integer Overflow"); break;
|
|
|
|
case STATUS_PRIVILEGED_INSTRUCTION: strcpy(status,"Privileged Instruction"); break;
|
|
|
|
case STATUS_STACK_OVERFLOW: strcpy(status,"Stack Overflow"); break;
|
|
|
|
case STATUS_CONTROL_C_EXIT: strcpy(status,"Ctrl+C Exit"); break;
|
|
|
|
default: strcpy(status,"Unknown exception"); 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);
|
|
|
|
exit(ERR_SIGNAL_CATCH);
|
|
|
|
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 SIGQUIT: fprintf( stderr, "SIGQUIT: quit 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
|
|
|
}
|
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) {
|
2002-11-19 22:39:58 +00:00
|
|
|
last_checkpoint_cpu_time = boinc_cpu_time();
|
2002-11-19 21:26:34 +00:00
|
|
|
write_fraction_done_file(fraction_done,last_checkpoint_cpu_time,last_checkpoint_cpu_time);
|
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
|
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];
|
|
|
|
|
2002-08-22 20:19:18 +00:00
|
|
|
strcpy(physical_name, virtual_name);
|
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-02-27 23:42:38 +00:00
|
|
|
if (check_quit) {
|
|
|
|
FILE* f = fopen(QUIT_FILE, "r");
|
2002-12-02 22:51:11 +00:00
|
|
|
if(f) {
|
2003-02-27 23:42:38 +00:00
|
|
|
parse_quit_file(f,time_to_quit);
|
2002-12-02 22:51:11 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2003-02-27 23:42:38 +00:00
|
|
|
time_until_quit_check = 1; // reset to 1 second
|
|
|
|
check_quit = false;
|
2002-12-02 22:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (write_frac_done) {
|
|
|
|
write_fraction_done_file(fraction_done, boinc_cpu_time(), last_checkpoint_cpu_time);
|
|
|
|
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() {
|
2002-11-19 22:39:58 +00:00
|
|
|
last_checkpoint_cpu_time = boinc_cpu_time();
|
2002-11-19 21:26:34 +00:00
|
|
|
write_fraction_done_file(fraction_done,last_checkpoint_cpu_time,last_checkpoint_cpu_time);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
double boinc_cpu_time() {
|
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);
|
|
|
|
if(retval) fprintf(stderr, "error: could not get cpu time for %d\n", pid);
|
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));
|
|
|
|
return cpu_secs;
|
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
|
2002-11-20 20:14:48 +00:00
|
|
|
cpu_secs += totTime / 10000000.0;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
// Convert to seconds and return
|
2002-11-20 20:14:48 +00:00
|
|
|
return cpu_secs;
|
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();
|
2002-11-20 20:14:48 +00:00
|
|
|
return cpu_secs + ((cur - first_count)/1000.);
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif // _WIN32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fprintf(stderr, "boinc_cpu_time(): not implemented\n");
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-27 23:42:38 +00:00
|
|
|
if (!check_quit) {
|
|
|
|
time_until_quit_check -= timer_period;
|
|
|
|
if (time_until_quit_check <= 0) {
|
|
|
|
check_quit = true;
|
2002-11-18 23:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-18 21:20:54 +00:00
|
|
|
|
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
|
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
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_init_data_file(FILE* f, APP_INIT_DATA& ai) {
|
|
|
|
if (strlen(ai.app_preferences)) {
|
|
|
|
fprintf(f, "<app_preferences>\n%s</app_preferences>\n", ai.app_preferences);
|
|
|
|
}
|
|
|
|
if (strlen(ai.team_name)) {
|
2003-01-23 21:11:33 +00:00
|
|
|
fprintf(f, "<team_name>%s</team_name>\n", ai.team_name);
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
if (strlen(ai.user_name)) {
|
2003-01-23 21:11:33 +00:00
|
|
|
fprintf(f, "<user_name>%s</user_name>\n", ai.user_name);
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
fprintf(f,
|
|
|
|
"<wu_cpu_time>%f</wu_cpu_time>\n"
|
2003-02-24 21:25:16 +00:00
|
|
|
"<user_total_credit>%f</user_total_credit>\n"
|
|
|
|
"<user_expavg_credit>%f</user_expavg_credit>\n"
|
|
|
|
"<host_total_credit>%f</host_total_credit>\n"
|
|
|
|
"<host_expavg_credit>%f</host_expavg_credit>\n"
|
2002-08-05 00:29:34 +00:00
|
|
|
"<checkpoint_period>%f</checkpoint_period>\n"
|
|
|
|
"<fraction_done_update_period>%f</fraction_done_update_period>\n",
|
|
|
|
ai.wu_cpu_time,
|
2003-02-24 21:25:16 +00:00
|
|
|
ai.user_total_credit,
|
|
|
|
ai.user_expavg_credit,
|
|
|
|
ai.host_total_credit,
|
|
|
|
ai.host_expavg_credit,
|
2002-08-05 00:29:34 +00:00
|
|
|
ai.checkpoint_period,
|
|
|
|
ai.fraction_done_update_period
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_init_data_file(FILE* f, APP_INIT_DATA& ai) {
|
|
|
|
char buf[256];
|
|
|
|
memset(&ai, 0, sizeof(ai));
|
|
|
|
while (fgets(buf, 256, f)) {
|
|
|
|
if (match_tag(buf, "<app_preferences>")) {
|
|
|
|
strcpy(ai.app_preferences, "");
|
|
|
|
while (fgets(buf, 256, f)) {
|
2002-12-06 18:38:36 +00:00
|
|
|
if (match_tag(buf, "</app_preferences>")) break;
|
2002-08-05 00:29:34 +00:00
|
|
|
strcat(ai.app_preferences, buf);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2002-09-22 23:27:14 +00:00
|
|
|
else if (parse_str(buf, "<user_name>", ai.user_name, sizeof(ai.user_name))) continue;
|
|
|
|
else if (parse_str(buf, "<team_name>", ai.team_name, sizeof(ai.team_name))) continue;
|
2003-02-24 21:25:16 +00:00
|
|
|
else if (parse_double(buf, "<user_total_credit>", ai.user_total_credit)) continue;
|
|
|
|
else if (parse_double(buf, "<user_expavg_credit>", ai.user_expavg_credit)) continue;
|
|
|
|
else if (parse_double(buf, "<host_total_credit>", ai.host_total_credit)) continue;
|
|
|
|
else if (parse_double(buf, "<host_expavg_credit>", ai.host_expavg_credit)) continue;
|
2002-08-24 00:41:25 +00:00
|
|
|
else if (parse_double(buf, "<wu_cpu_time>", ai.wu_cpu_time)) continue;
|
2002-08-05 00:29:34 +00:00
|
|
|
else if (parse_double(buf, "<checkpoint_period>", ai.checkpoint_period)) continue;
|
|
|
|
else if (parse_double(buf, "<fraction_done_update_period>", ai.fraction_done_update_period)) continue;
|
|
|
|
else fprintf(stderr, "parse_init_data_file: unrecognized %s", buf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-19 21:26:34 +00:00
|
|
|
int write_fraction_done_file(double pct, double cpu, double checkpoint_cpu) {
|
2003-01-24 00:40:00 +00:00
|
|
|
FILE* f = fopen(FRACTION_DONE_TEMP_FILE, "w");
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2003-02-27 23:42:38 +00:00
|
|
|
if (!f) return -1;
|
2002-11-26 21:33:43 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
fprintf(f,
|
|
|
|
"<fraction_done>%f</fraction_done>\n"
|
2002-11-19 21:26:34 +00:00
|
|
|
"<cpu_time>%f</cpu_time>\n"
|
|
|
|
"<checkpoint_cpu_time>%f</checkpoint_cpu_time>\n",
|
2002-08-05 00:29:34 +00:00
|
|
|
pct,
|
2002-11-19 21:26:34 +00:00
|
|
|
cpu,
|
2002-11-19 22:39:58 +00:00
|
|
|
checkpoint_cpu
|
2002-08-05 00:29:34 +00:00
|
|
|
);
|
2002-11-19 21:26:34 +00:00
|
|
|
|
2002-11-19 22:39:58 +00:00
|
|
|
fclose(f);
|
2003-01-24 00:40:00 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
unlink(FRACTION_DONE_FILE);
|
|
|
|
#endif
|
|
|
|
rename(FRACTION_DONE_TEMP_FILE, FRACTION_DONE_FILE);
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-19 21:26:34 +00:00
|
|
|
int parse_fraction_done_file(FILE* f, double& pct, double& cpu, double& checkpoint_cpu) {
|
2002-08-05 00:29:34 +00:00
|
|
|
char buf[256];
|
|
|
|
while (fgets(buf, 256, f)) {
|
|
|
|
if (parse_double(buf, "<fraction_done>", pct)) continue;
|
|
|
|
else if (parse_double(buf, "<cpu_time>", cpu)) continue;
|
2002-11-19 21:26:34 +00:00
|
|
|
else if (parse_double(buf, "<checkpoint_cpu_time>", checkpoint_cpu)) continue;
|
2002-08-05 00:29:34 +00:00
|
|
|
else fprintf(stderr, "parse_fraction_done_file: unrecognized %s", buf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-27 23:42:38 +00:00
|
|
|
int write_quit_file(FILE* f) {
|
|
|
|
fprintf(f, "<quit/>\n");
|
2002-11-18 21:20:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-27 23:42:38 +00:00
|
|
|
int parse_quit_file(FILE* f, bool& quit) {
|
2002-11-18 21:20:54 +00:00
|
|
|
char buf[256];
|
2002-11-18 23:09:11 +00:00
|
|
|
|
2002-11-18 21:20:54 +00:00
|
|
|
while (fgets(buf, 256, f)) {
|
2003-02-27 23:42:38 +00:00
|
|
|
if (match_tag(buf, "<quit/>")) quit = true;
|
|
|
|
else fprintf(stderr, "parse_quit_file: unrecognized %s", buf);
|
2002-11-18 21:20:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
// TODO: this should handle arbitrarily many fd/filename pairs.
|
|
|
|
// Also, give the tags better names
|
|
|
|
int write_fd_init_file(FILE* f, char *file_name, int fdesc, int input_file ) {
|
|
|
|
if (input_file) {
|
|
|
|
fprintf(f, "<fdesc_dup_infile>%s</fdesc_dup_infile>\n", file_name);
|
|
|
|
fprintf(f, "<fdesc_dup_innum>%d</fdesc_dup_innum>\n", fdesc);
|
|
|
|
} else {
|
|
|
|
fprintf(f, "<fdesc_dup_outfile>%s</fdesc_dup_outfile>\n", file_name);
|
|
|
|
fprintf(f, "<fdesc_dup_outnum>%d</fdesc_dup_outnum>\n", fdesc);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this should handle arbitrarily many fd/filename pairs.
|
|
|
|
// Also, this shouldn't be doing the actual duping!
|
|
|
|
//
|
|
|
|
int parse_fd_init_file(FILE* f) {
|
|
|
|
char buf[256],filename[256];
|
2002-08-28 21:50:51 +00:00
|
|
|
int filedesc;
|
2002-08-05 00:29:34 +00:00
|
|
|
while (fgets(buf, 256, f)) {
|
2002-09-22 23:27:14 +00:00
|
|
|
if (parse_str(buf, "<fdesc_dup_infile>", filename, sizeof(filename))) {
|
2002-08-05 00:29:34 +00:00
|
|
|
if (fgets(buf, 256, f)) {
|
|
|
|
if (parse_int(buf, "<fdesc_dup_innum>", filedesc)) {
|
2002-08-28 21:50:51 +00:00
|
|
|
freopen(filename, "r", stdin);
|
|
|
|
fprintf(stderr, "opened input file %s\n", filename);
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-22 23:27:14 +00:00
|
|
|
} else if (parse_str(buf, "<fdesc_dup_outfile>", filename, sizeof(filename))) {
|
2002-08-05 00:29:34 +00:00
|
|
|
if (fgets(buf, 256, f)) {
|
|
|
|
if (parse_int(buf, "<fdesc_dup_outnum>", filedesc)) {
|
2002-08-28 21:50:51 +00:00
|
|
|
freopen(filename, "w", stdout);
|
|
|
|
fprintf(stderr, "opened output file %s\n", filename);
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
} else fprintf(stderr, "parse_fd_init_file: unrecognized %s", buf);
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|