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
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#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>
|
|
|
|
#endif
|
2002-09-18 22:19:38 +00:00
|
|
|
|
|
|
|
#ifdef __APPLE_CC__
|
|
|
|
#include <CoreServices/CoreServices.h>
|
|
|
|
#endif
|
|
|
|
|
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"
|
|
|
|
#include "error_numbers.h"
|
2002-09-11 21:56:20 +00:00
|
|
|
#include "graphics_api.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
|
|
|
|
#include "boinc_api.h"
|
|
|
|
|
|
|
|
static APP_INIT_DATA aid;
|
2002-09-18 22:19:38 +00:00
|
|
|
GRAPHICS_INFO gi;
|
2002-08-05 00:29:34 +00:00
|
|
|
static double timer_period = 0.1;
|
|
|
|
static double time_until_checkpoint;
|
|
|
|
static double time_until_fraction_done_update;
|
|
|
|
static double fraction_done;
|
|
|
|
static bool ready_to_checkpoint = false;
|
|
|
|
static bool this_process_active;
|
|
|
|
|
|
|
|
// 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);
|
2002-08-22 20:41:56 +00:00
|
|
|
fprintf(stderr, "in boinc_init()\n");
|
2002-08-22 20:19:18 +00:00
|
|
|
#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");
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2002-09-18 17:40:39 +00:00
|
|
|
retval = parse_graphics_file(f, &gi);
|
2002-09-11 21:56:20 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "boinc_init(): can't parse graphics data file\n");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
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;
|
|
|
|
aid.total_cobblestones = 1000;
|
|
|
|
aid.recent_avg_cobblestones = 500;
|
|
|
|
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;
|
|
|
|
this_process_active = true;
|
|
|
|
set_timer(timer_period);
|
2002-09-11 21:56:20 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_finish(int status) {
|
|
|
|
write_checkpoint_cpu_file(boinc_cpu_time());
|
|
|
|
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() {
|
|
|
|
return ready_to_checkpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_checkpoint_completed() {
|
|
|
|
write_checkpoint_cpu_file(boinc_cpu_time());
|
|
|
|
ready_to_checkpoint = false;
|
|
|
|
time_until_checkpoint = aid.checkpoint_period;
|
|
|
|
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() {
|
|
|
|
#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);
|
|
|
|
return (double)ru.ru_utime.tv_sec + (
|
2002-08-24 00:41:25 +00:00
|
|
|
((double)ru.ru_utime.tv_usec) / ((double)1000000.0)
|
2002-08-05 00:29:34 +00:00
|
|
|
);
|
|
|
|
#else
|
|
|
|
#ifdef _WIN32
|
2002-08-09 21:43:59 +00:00
|
|
|
#ifdef WINNT_CLOCK
|
2002-08-05 00:29:34 +00:00
|
|
|
HANDLE hProcess;
|
|
|
|
FILETIME creationTime,exitTime,kernelTime,userTime;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Runtimes in 100-nanosecond units
|
|
|
|
totTime = tKernel.QuadPart + tUser.QuadPart;
|
|
|
|
|
|
|
|
// Convert to seconds and return
|
|
|
|
return(totTime / 10000000.0);
|
|
|
|
}
|
2002-08-09 21:43:59 +00:00
|
|
|
CloseHandle(hProcess);
|
2002-08-24 00:41:25 +00:00
|
|
|
// ... fall through
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif // WINNT_CLOCK
|
2002-08-24 00:41:25 +00:00
|
|
|
static bool first=true;
|
|
|
|
static DWORD last_count = 0;
|
|
|
|
|
|
|
|
if (first) {
|
2002-08-28 21:50:51 +00:00
|
|
|
last_count = GetTickCount();
|
|
|
|
first = true;
|
2002-08-24 00:41:25 +00:00
|
|
|
}
|
|
|
|
DWORD cur = GetTickCount();
|
|
|
|
double x = (cur - last_count)/1000.;
|
|
|
|
last_count = cur;
|
|
|
|
return x;
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif // _WIN32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef macintosh
|
|
|
|
return (double) GetCPUTime() / CLOCKS_PER_SEC;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fprintf(stderr, "boinc_cpu_time(): not implemented\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2002-09-22 23:27:14 +00:00
|
|
|
void CALLBACK on_timer(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this_process_active) {
|
|
|
|
time_until_fraction_done_update -= timer_period;
|
|
|
|
if (time_until_fraction_done_update < 0) {
|
|
|
|
FILE* f = fopen(FRACTION_DONE_FILE, "w");
|
|
|
|
write_fraction_done_file(f, boinc_cpu_time(), fraction_done);
|
|
|
|
fclose(f);
|
|
|
|
time_until_fraction_done_update = aid.fraction_done_update_period;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int set_timer(double period) {
|
|
|
|
int retval=0;
|
|
|
|
#ifdef _WIN32
|
2002-09-22 23:27:14 +00:00
|
|
|
retval = SetTimer(NULL, 0, (int)(period*1000), on_timer);
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#if HAVE_SYS_TIME_H
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_handler = on_timer;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigaction(SIGVTALRM, &sa, NULL);
|
|
|
|
itimerval value;
|
|
|
|
value.it_value.tv_sec = (int)period;
|
|
|
|
value.it_value.tv_usec = ((int)(period*1000000))%1000000;
|
|
|
|
value.it_interval = value.it_value;
|
|
|
|
retval = setitimer(ITIMER_VIRTUAL, &value, NULL);
|
|
|
|
#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)) {
|
|
|
|
fprintf(f, "<team_name>\n%s</team_name>\n", ai.team_name);
|
|
|
|
}
|
|
|
|
if (strlen(ai.user_name)) {
|
|
|
|
fprintf(f, "<user_name>\n%s</user_name>\n", ai.user_name);
|
|
|
|
}
|
|
|
|
fprintf(f,
|
|
|
|
"<wu_cpu_time>%f</wu_cpu_time>\n"
|
|
|
|
"<total_cobblestones>%f</total_cobblestones>\n"
|
|
|
|
"<recent_avg_cobblestones>%f</recent_avg_cobblestones>\n"
|
|
|
|
"<checkpoint_period>%f</checkpoint_period>\n"
|
|
|
|
"<fraction_done_update_period>%f</fraction_done_update_period>\n",
|
|
|
|
ai.wu_cpu_time,
|
|
|
|
ai.total_cobblestones,
|
|
|
|
ai.recent_avg_cobblestones,
|
|
|
|
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)) {
|
|
|
|
if (match_tag(buf, "</app_specific_prefs>")) break;
|
|
|
|
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;
|
2002-08-24 00:41:25 +00:00
|
|
|
else if (parse_double(buf, "<total_cobblestones>", ai.total_cobblestones)) continue;
|
|
|
|
else if (parse_double(buf, "<recent_avg_cobblestones>", ai.recent_avg_cobblestones)) continue;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_checkpoint_cpu_file(double cpu) {
|
|
|
|
FILE *f = fopen(CHECKPOINT_CPU_FILE, "w");
|
|
|
|
if (!f) return ERR_FOPEN;
|
|
|
|
fprintf(f, "<checkpoint_cpu_time>%f</checkpoint_cpu_time>\n", cpu);
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_checkpoint_cpu_file(FILE* f, double& checkpoint_cpu) {
|
|
|
|
char buf[256];
|
|
|
|
while (fgets(buf, 256, f)) {
|
|
|
|
if (parse_double(buf, "<checkpoint_cpu_time>", checkpoint_cpu)) continue;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_fraction_done_file(FILE* f, double pct, double cpu) {
|
|
|
|
fprintf(f,
|
|
|
|
"<fraction_done>%f</fraction_done>\n"
|
|
|
|
"<cpu_time>%f</cpu_time>\n",
|
|
|
|
pct,
|
|
|
|
cpu
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_fraction_done_file(FILE* f, double& pct, double& cpu) {
|
|
|
|
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;
|
|
|
|
else fprintf(stderr, "parse_fraction_done_file: unrecognized %s", buf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|