2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-08-21 23:44:57 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2003-08-21 23:44:57 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-08-05 00:29:34 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-08-05 00:29:34 +00:00
|
|
|
|
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
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
2004-06-16 21:34:31 +00:00
|
|
|
#include "boinc_win.h"
|
2005-07-14 16:46:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-12-30 03:03:26 +00:00
|
|
|
#include "version.h"
|
2004-06-16 21:34:31 +00:00
|
|
|
#else
|
2004-12-14 20:28:13 +00:00
|
|
|
#include "config.h"
|
2004-06-16 12:01:49 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
2004-06-12 18:44:53 +00:00
|
|
|
#include <sys/types.h>
|
2005-03-19 21:39:17 +00:00
|
|
|
#include <errno.h>
|
2002-08-05 00:29:34 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
2005-05-23 00:07:29 +00:00
|
|
|
#include <sys/resource.h>
|
2005-01-27 09:19:42 +00:00
|
|
|
#include <pthread.h>
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifndef __EMX__
|
2005-01-27 09:19:42 +00:00
|
|
|
#include <sched.h>
|
2005-12-02 22:29:35 +00:00
|
|
|
#endif
|
2004-03-05 04:37:53 +00:00
|
|
|
using namespace std;
|
2004-06-16 21:34:31 +00:00
|
|
|
#endif
|
2004-03-05 04:37:53 +00:00
|
|
|
|
2004-03-24 07:39:24 +00:00
|
|
|
#include "diagnostics.h"
|
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"
|
2003-12-24 00:50:51 +00:00
|
|
|
#include "filesys.h"
|
2004-12-14 20:28:13 +00:00
|
|
|
#include "mem_usage.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"
|
2004-10-26 21:12:00 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "boinc_api.h"
|
|
|
|
|
2005-08-11 18:18:03 +00:00
|
|
|
// The BOINC API has various functions:
|
|
|
|
// - check heartbeat from core client, exit if none
|
|
|
|
// - handle trickle up/down messages
|
|
|
|
// - report CPU time and fraction done to the core client.
|
|
|
|
|
|
|
|
// Implementation notes:
|
|
|
|
// Unix: getting CPU time and suspend/resume have to be done
|
|
|
|
// in the worker thread, so we use a SIGALRM signal handler.
|
|
|
|
// However, many library functions and system calls
|
|
|
|
// are not "asynch signal safe": see, e.g.
|
|
|
|
// http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03
|
|
|
|
// (e.g. sprintf() in a signal handler hangs Mac OS X)
|
|
|
|
// so we do as little as possible in the signal handler,
|
|
|
|
// and do the rest in a separate "timer thread".
|
|
|
|
|
|
|
|
// Terminology:
|
|
|
|
// The processing of a result can be divided
|
2004-05-21 20:25:12 +00:00
|
|
|
// into multiple "episodes" (executions of the app),
|
|
|
|
// each of which resumes from the checkpointed state of the previous episode.
|
|
|
|
// Unless otherwise noted, "CPU time" refers to the sum over all episodes
|
|
|
|
// (not counting the part after the last checkpoint in an episode).
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2005-09-23 20:59:39 +00:00
|
|
|
void (*stop_graphics_thread_ptr)() = 0;
|
2005-03-19 21:39:17 +00:00
|
|
|
static APP_INIT_DATA aid;
|
2005-03-05 23:12:31 +00:00
|
|
|
static FILE_LOCK file_lock;
|
2005-03-19 21:39:17 +00:00
|
|
|
APP_CLIENT_SHM* app_client_shm = 0;
|
2005-03-21 19:57:44 +00:00
|
|
|
static volatile int time_until_checkpoint;
|
2004-10-26 20:33:16 +00:00
|
|
|
// time until enable checkpoint
|
2005-03-21 19:57:44 +00:00
|
|
|
static volatile int time_until_fraction_done_update;
|
2004-10-26 20:33:16 +00:00
|
|
|
// time until report fraction done to core client
|
2005-03-19 21:39:17 +00:00
|
|
|
static double fraction_done;
|
|
|
|
static double last_checkpoint_cpu_time;
|
|
|
|
static bool ready_to_checkpoint = false;
|
2005-08-12 21:24:07 +00:00
|
|
|
static bool in_critical_section = false;
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile double last_wu_cpu_time;
|
|
|
|
static bool standalone = false;
|
|
|
|
static double initial_wu_cpu_time;
|
|
|
|
static volatile bool have_new_trickle_up = false;
|
2005-04-28 23:19:58 +00:00
|
|
|
static volatile bool have_trickle_down = true;
|
2004-07-06 04:01:15 +00:00
|
|
|
// on first call, scan slot dir for msgs
|
2005-03-21 19:57:44 +00:00
|
|
|
static volatile int heartbeat_giveup_time;
|
2005-07-23 08:10:39 +00:00
|
|
|
// interrupt count value at which to give up on core client
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile bool heartbeat_active;
|
2004-12-05 23:52:17 +00:00
|
|
|
// if false, suppress heartbeat mechanism
|
2005-02-16 23:30:29 +00:00
|
|
|
#ifdef _WIN32
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile int nrunning_ticks = 0;
|
2005-02-16 23:30:29 +00:00
|
|
|
#endif
|
2005-07-23 08:10:39 +00:00
|
|
|
static volatile int interrupt_count = 0;
|
|
|
|
// number of timer interrupts
|
|
|
|
// used to measure elapsed time in a way that's
|
|
|
|
// not affected by user changing system clock,
|
|
|
|
// and that doesn't have big jumps around hibernation
|
2005-06-24 04:34:45 +00:00
|
|
|
static double fpops_per_cpu_sec = 0;
|
|
|
|
static double fpops_cumulative = 0;
|
2005-11-03 05:31:21 +00:00
|
|
|
static double intops_per_cpu_sec = 0;
|
|
|
|
static double intops_cumulative = 0;
|
2005-08-17 21:05:26 +00:00
|
|
|
static int non_cpu_intensive = 0;
|
2005-08-17 22:08:35 +00:00
|
|
|
static int want_network = 0;
|
|
|
|
static int have_network = 1;
|
2004-06-29 01:15:51 +00:00
|
|
|
|
2005-03-21 19:57:44 +00:00
|
|
|
#define TIMER_PERIOD 1
|
2005-07-23 08:10:39 +00:00
|
|
|
// period of worker-thread timer interrupts.
|
2005-03-15 20:23:24 +00:00
|
|
|
// This determines the resolution of fraction done and CPU time reporting
|
|
|
|
// to the core client, and of checkpoint enabling.
|
|
|
|
// It doesn't influence graphics, so 1 sec is enough.
|
2005-07-23 08:10:39 +00:00
|
|
|
#define HEARTBEAT_GIVEUP_PERIOD (30/TIMER_PERIOD)
|
|
|
|
// quit if no heartbeat from core in this #interrupts
|
2005-03-21 19:57:44 +00:00
|
|
|
#define HEARTBEAT_TIMEOUT_PERIOD 35
|
2004-07-20 21:28:24 +00:00
|
|
|
// quit if we cannot aquire slot resource in this #secs
|
2004-02-06 09:34:00 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2005-06-08 20:28:05 +00:00
|
|
|
static HANDLE hSharedMem;
|
|
|
|
HANDLE worker_thread_handle;
|
2005-01-28 01:58:11 +00:00
|
|
|
// used to suspend worker thread, and to measure its CPU time
|
2004-08-03 09:50:24 +00:00
|
|
|
static MMRESULT timer_id;
|
2005-08-11 18:18:03 +00:00
|
|
|
#else
|
|
|
|
static pthread_t timer_thread_handle;
|
|
|
|
static struct rusage worker_thread_ru;
|
2004-02-06 09:34:00 +00:00
|
|
|
#endif
|
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
static BOINC_OPTIONS options;
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile BOINC_STATUS boinc_status;
|
2004-08-03 09:50:24 +00:00
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
// vars related to intermediate file upload
|
|
|
|
struct UPLOAD_FILE_STATUS {
|
|
|
|
std::string name;
|
|
|
|
int status;
|
|
|
|
};
|
|
|
|
static bool have_new_upload_file;
|
|
|
|
static std::vector<UPLOAD_FILE_STATUS> upload_file_status;
|
|
|
|
|
2005-03-29 23:13:28 +00:00
|
|
|
static int setup_shared_mem() {
|
|
|
|
if (standalone) {
|
|
|
|
fprintf(stderr, "Standalone mode, so not using shared memory.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
app_client_shm = new APP_CLIENT_SHM;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "%s%s", SHM_PREFIX, aid.shmem_seg_name);
|
|
|
|
hSharedMem = attach_shmem(buf, (void**)&app_client_shm->shm);
|
|
|
|
if (hSharedMem == NULL) {
|
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (attach_shmem(aid.shmem_seg_name, (void**)&app_client_shm->shm)) {
|
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (app_client_shm == NULL) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-11 18:18:03 +00:00
|
|
|
// Return CPU time of worker thread.
|
|
|
|
// This may be called from other threads
|
|
|
|
//
|
2005-03-29 23:13:28 +00:00
|
|
|
static int boinc_worker_thread_cpu_time(double& cpu) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (boinc_thread_cpu_time(worker_thread_handle, cpu)) {
|
|
|
|
cpu = nrunning_ticks * TIMER_PERIOD; // for Win9x
|
|
|
|
}
|
|
|
|
#else
|
2005-08-11 18:18:03 +00:00
|
|
|
cpu = (double)worker_thread_ru.ru_utime.tv_sec
|
|
|
|
+ (((double)worker_thread_ru.ru_utime.tv_usec)/1000000.0);
|
|
|
|
cpu += (double)worker_thread_ru.ru_stime.tv_sec
|
|
|
|
+ (((double)worker_thread_ru.ru_stime.tv_usec)/1000000.0);
|
2005-03-29 23:13:28 +00:00
|
|
|
#endif
|
2005-06-08 20:28:05 +00:00
|
|
|
return 0;
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// communicate to the core client (via shared mem)
|
|
|
|
// the current CPU time and fraction done
|
|
|
|
//
|
2005-03-30 21:11:49 +00:00
|
|
|
static bool update_app_progress(
|
|
|
|
double cpu_t, double cp_cpu_t, double rss=0, double vm=0
|
2005-03-29 23:13:28 +00:00
|
|
|
) {
|
|
|
|
char msg_buf[MSG_CHANNEL_SIZE], buf[256];
|
|
|
|
|
2005-07-07 20:32:28 +00:00
|
|
|
if (standalone) return true;
|
2005-03-29 23:13:28 +00:00
|
|
|
|
2005-08-11 18:18:03 +00:00
|
|
|
sprintf(msg_buf,
|
|
|
|
"<current_cpu_time>%.15e</current_cpu_time>\n"
|
2005-08-17 21:05:26 +00:00
|
|
|
"<checkpoint_cpu_time>%.15e</checkpoint_cpu_time>\n"
|
|
|
|
"<non_cpu_intensive>%d</non_cpu_intensive>\n",
|
|
|
|
cpu_t, cp_cpu_t, non_cpu_intensive
|
2005-08-11 18:18:03 +00:00
|
|
|
);
|
2005-08-17 22:08:35 +00:00
|
|
|
if (want_network) {
|
|
|
|
strcat(msg_buf, "<want_network>1</want_network>\n");
|
|
|
|
}
|
2005-03-29 23:13:28 +00:00
|
|
|
if (fraction_done >= 0) {
|
|
|
|
double range = aid.fraction_done_end - aid.fraction_done_start;
|
|
|
|
double fdone = aid.fraction_done_start + fraction_done*range;
|
2005-08-11 18:18:03 +00:00
|
|
|
sprintf(buf, "<fraction_done>%2.8f</fraction_done>\n", fdone);
|
2005-03-29 23:13:28 +00:00
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
2005-03-30 21:11:49 +00:00
|
|
|
if (rss) {
|
2005-08-11 18:18:03 +00:00
|
|
|
sprintf(buf, "<rss_bytes>%f</rss_bytes>\n", rss);
|
2005-03-29 23:13:28 +00:00
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
2005-03-30 21:11:49 +00:00
|
|
|
if (vm) {
|
2005-08-11 18:18:03 +00:00
|
|
|
sprintf(buf, "<vm_bytes>%f</vm_bytes>\n", vm);
|
2005-03-30 21:11:49 +00:00
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
2005-06-24 04:34:45 +00:00
|
|
|
if (fpops_per_cpu_sec) {
|
2005-08-11 18:18:03 +00:00
|
|
|
sprintf(buf, "<fpops_per_cpu_sec>%f</fpops_per_cpu_sec>\n", fpops_per_cpu_sec);
|
2005-06-24 04:34:45 +00:00
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
|
|
|
if (fpops_cumulative) {
|
2005-08-11 18:18:03 +00:00
|
|
|
sprintf(buf, "<fpops_cumulative>%f</fpops_cumulative>\n", fpops_cumulative);
|
2005-06-24 04:34:45 +00:00
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
2005-11-03 05:31:21 +00:00
|
|
|
if (intops_per_cpu_sec) {
|
|
|
|
sprintf(buf, "<intops_per_cpu_sec>%f</intops_per_cpu_sec>\n", intops_per_cpu_sec);
|
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
|
|
|
if (intops_cumulative) {
|
|
|
|
sprintf(buf, "<intops_cumulative>%f</intops_cumulative>\n", intops_cumulative);
|
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
2005-03-30 21:11:49 +00:00
|
|
|
return app_client_shm->shm->app_status.send_msg(msg_buf);
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
// the following 2 functions are used for apps without graphics
|
2004-10-26 21:12:00 +00:00
|
|
|
//
|
|
|
|
int boinc_init() {
|
2004-12-13 19:14:54 +00:00
|
|
|
boinc_options_defaults(options);
|
2006-03-13 20:45:08 +00:00
|
|
|
return boinc_init_options(&options);
|
2004-10-26 21:12:00 +00:00
|
|
|
}
|
2004-11-22 19:17:13 +00:00
|
|
|
|
2006-03-13 20:45:08 +00:00
|
|
|
int boinc_init_options(BOINC_OPTIONS* opt) {
|
2004-10-26 21:48:37 +00:00
|
|
|
int retval;
|
2006-03-13 20:45:08 +00:00
|
|
|
retval = boinc_init_options_general(*opt);
|
2004-10-26 21:48:37 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
return set_worker_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the following can be called by either graphics or worker thread
|
|
|
|
//
|
|
|
|
int boinc_init_options_general(BOINC_OPTIONS& opt) {
|
2004-02-06 09:34:00 +00:00
|
|
|
int retval;
|
2004-08-03 09:50:24 +00:00
|
|
|
options = opt;
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2005-03-19 21:39:17 +00:00
|
|
|
boinc_status.no_heartbeat = false;
|
|
|
|
boinc_status.suspended = false;
|
|
|
|
boinc_status.quit_request = false;
|
2006-02-03 20:48:48 +00:00
|
|
|
boinc_status.abort_request = false;
|
2004-08-03 09:50:24 +00:00
|
|
|
|
|
|
|
if (options.main_program) {
|
2004-07-03 20:03:16 +00:00
|
|
|
// make sure we're the only app running in this slot
|
2004-06-29 04:34:55 +00:00
|
|
|
//
|
2005-03-05 23:12:31 +00:00
|
|
|
retval = file_lock.lock(LOCKFILE);
|
2004-07-03 20:03:16 +00:00
|
|
|
if (retval) {
|
|
|
|
// give any previous occupant a chance to timeout and exit
|
|
|
|
//
|
2004-07-20 21:28:24 +00:00
|
|
|
boinc_sleep(HEARTBEAT_TIMEOUT_PERIOD);
|
2005-03-05 23:12:31 +00:00
|
|
|
retval = file_lock.lock(LOCKFILE);
|
2004-07-03 20:03:16 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "Can't acquire lockfile - exiting\n");
|
2005-03-15 20:23:24 +00:00
|
|
|
boinc_exit(0); // not un-recoverable ==> status=0
|
2004-07-03 20:03:16 +00:00
|
|
|
}
|
2004-06-29 04:34:55 +00:00
|
|
|
}
|
|
|
|
|
2004-02-06 09:34:00 +00:00
|
|
|
retval = boinc_parse_init_data_file();
|
2004-05-21 20:25:12 +00:00
|
|
|
if (retval) {
|
|
|
|
standalone = true;
|
2004-06-03 04:25:44 +00:00
|
|
|
} else {
|
|
|
|
retval = setup_shared_mem();
|
|
|
|
if (retval) {
|
2004-07-08 17:59:46 +00:00
|
|
|
fprintf(stderr, "Can't set up shared mem: %d\n", retval);
|
2004-06-03 04:25:44 +00:00
|
|
|
standalone = true;
|
|
|
|
}
|
2004-05-21 20:25:12 +00:00
|
|
|
}
|
|
|
|
|
2004-02-06 09:34:00 +00:00
|
|
|
// copy the WU CPU time to a separate var,
|
|
|
|
// since we may reread the structure again later.
|
|
|
|
//
|
|
|
|
initial_wu_cpu_time = aid.wu_cpu_time;
|
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
// the following may not be needed, but do it anyway
|
|
|
|
//
|
2004-05-21 20:25:12 +00:00
|
|
|
fraction_done = -1;
|
2005-03-21 19:57:44 +00:00
|
|
|
time_until_checkpoint = (int)aid.checkpoint_period;
|
2004-02-06 09:34:00 +00:00
|
|
|
last_checkpoint_cpu_time = aid.wu_cpu_time;
|
2005-03-21 19:57:44 +00:00
|
|
|
time_until_fraction_done_update = (int)aid.fraction_done_update_period;
|
2004-02-06 09:34:00 +00:00
|
|
|
last_wu_cpu_time = aid.wu_cpu_time;
|
|
|
|
|
2004-06-29 01:15:51 +00:00
|
|
|
heartbeat_active = !standalone;
|
2005-07-23 08:10:39 +00:00
|
|
|
heartbeat_giveup_time = interrupt_count + HEARTBEAT_GIVEUP_PERIOD;
|
2004-06-29 01:15:51 +00:00
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2006-02-03 20:48:48 +00:00
|
|
|
int boinc_get_status(BOINC_STATUS *s) {
|
|
|
|
s->no_heartbeat = boinc_status.no_heartbeat;
|
|
|
|
s->suspended = boinc_status.suspended;
|
|
|
|
s->quit_request = boinc_status.quit_request;
|
|
|
|
s->abort_request = boinc_status.abort_request;
|
2004-02-06 09:34:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
// if we have any new trickle-ups or file upload requests,
|
|
|
|
// send a message describing them
|
|
|
|
//
|
2004-08-03 09:50:24 +00:00
|
|
|
static void send_trickle_up_msg() {
|
2005-04-27 06:55:28 +00:00
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
|
|
|
strcpy(buf, "");
|
2004-08-03 09:50:24 +00:00
|
|
|
if (have_new_trickle_up) {
|
2005-04-27 06:55:28 +00:00
|
|
|
strcat(buf, "<have_new_trickle_up/>\n");
|
|
|
|
}
|
|
|
|
if (have_new_upload_file) {
|
|
|
|
strcat(buf, "<have_new_upload_file/>\n");
|
|
|
|
}
|
|
|
|
if (strlen(buf)) {
|
|
|
|
if (app_client_shm->shm->trickle_up.send_msg(buf)) {
|
2004-08-03 09:50:24 +00:00
|
|
|
have_new_trickle_up = false;
|
2005-04-27 06:55:28 +00:00
|
|
|
have_new_upload_file = false;
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2005-01-08 01:17:51 +00:00
|
|
|
|
2005-03-05 23:12:31 +00:00
|
|
|
// NOTE: a non-zero status tells the core client that we're exiting with
|
2005-01-19 15:54:04 +00:00
|
|
|
// an "unrecoverable error", which will be reported back to server.
|
2005-03-05 23:12:31 +00:00
|
|
|
// A zero exit-status tells the client we've successfully finished the result.
|
|
|
|
//
|
2004-08-03 09:50:24 +00:00
|
|
|
int boinc_finish(int status) {
|
|
|
|
if (options.send_status_msgs) {
|
2005-03-30 21:11:49 +00:00
|
|
|
double total_cpu;
|
|
|
|
boinc_worker_thread_cpu_time(total_cpu);
|
|
|
|
total_cpu += initial_wu_cpu_time;
|
|
|
|
|
|
|
|
// NOTE: the app_status slot may already contain a message.
|
|
|
|
// So retry a couple of times.
|
|
|
|
//
|
|
|
|
for (int i=0; i<3; i++) {
|
|
|
|
if (update_app_progress(total_cpu, total_cpu)) break;
|
|
|
|
boinc_sleep(1.0);
|
|
|
|
}
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
if (options.handle_trickle_ups) {
|
|
|
|
send_trickle_up_msg();
|
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
#ifdef _WIN32
|
2004-08-03 09:50:24 +00:00
|
|
|
// Stop the timer
|
|
|
|
timeKillEvent(timer_id);
|
|
|
|
CloseHandle(worker_thread_handle);
|
2002-11-21 23:27:27 +00:00
|
|
|
#endif
|
2004-08-03 09:50:24 +00:00
|
|
|
if (options.main_program && status==0) {
|
|
|
|
FILE* f = fopen(BOINC_FINISH_CALLED_FILE, "w");
|
|
|
|
if (f) fclose(f);
|
|
|
|
}
|
|
|
|
if (options.send_status_msgs) {
|
2004-07-02 22:14:13 +00:00
|
|
|
aid.wu_cpu_time = last_checkpoint_cpu_time;
|
|
|
|
boinc_write_init_data_file();
|
2004-04-04 01:59:47 +00:00
|
|
|
}
|
2005-01-08 01:17:51 +00:00
|
|
|
|
2005-01-19 15:54:04 +00:00
|
|
|
boinc_exit(status);
|
|
|
|
|
|
|
|
return(0); // doh... we never get here
|
2005-03-05 23:12:31 +00:00
|
|
|
}
|
2005-01-19 15:54:04 +00:00
|
|
|
|
|
|
|
|
2005-03-05 23:12:31 +00:00
|
|
|
// unlock the lockfile and call the appropriate exit function
|
2005-09-23 20:59:39 +00:00
|
|
|
// This is called from the worker thread or the timer thread.
|
2005-03-05 23:12:31 +00:00
|
|
|
//
|
2005-09-16 18:16:49 +00:00
|
|
|
void boinc_exit(int status) {
|
2005-09-23 20:59:39 +00:00
|
|
|
// Shutdown graphics thread if it is running
|
2005-09-16 18:16:49 +00:00
|
|
|
//
|
2005-09-23 20:59:39 +00:00
|
|
|
if (stop_graphics_thread_ptr) {
|
|
|
|
(*stop_graphics_thread_ptr)();
|
|
|
|
}
|
2005-09-16 18:16:49 +00:00
|
|
|
|
|
|
|
// Unlock the lock file
|
|
|
|
//
|
2005-03-05 23:12:31 +00:00
|
|
|
file_lock.unlock(LOCKFILE);
|
2005-01-19 15:54:04 +00:00
|
|
|
|
2006-03-14 00:46:41 +00:00
|
|
|
// flush all the output buffers
|
|
|
|
//
|
|
|
|
fflush(NULL);
|
|
|
|
|
2005-03-05 23:12:31 +00:00
|
|
|
// on Mac, calling exit() can lead to infinite exit-atexit loops,
|
|
|
|
// while _exit() seems to behave nicely.
|
|
|
|
// This is not pretty but unless someone finds a cleaner solution,
|
|
|
|
// we handle the Mac-case separately .
|
2005-01-19 15:54:04 +00:00
|
|
|
#ifdef __APPLE_CC__
|
2005-03-05 23:12:31 +00:00
|
|
|
_exit(status);
|
2005-01-19 15:54:04 +00:00
|
|
|
#else
|
2006-03-11 21:30:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (options.terminate_on_exit) {
|
|
|
|
TerminateProcess(GetCurrentProcess(), status);
|
|
|
|
}
|
|
|
|
#endif
|
2005-03-05 23:12:31 +00:00
|
|
|
exit(status);
|
2005-01-08 01:17:51 +00:00
|
|
|
#endif
|
2005-03-05 23:12:31 +00:00
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2006-01-06 09:02:26 +00:00
|
|
|
int boinc_is_standalone() {
|
|
|
|
if (standalone) return 1;
|
|
|
|
return 0;
|
2004-02-06 09:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parse the init data file.
|
|
|
|
// This is done at startup, and also if a "reread prefs" message is received
|
|
|
|
//
|
|
|
|
int boinc_parse_init_data_file() {
|
|
|
|
FILE* f;
|
|
|
|
int retval;
|
|
|
|
|
2004-10-14 22:01:05 +00:00
|
|
|
memset(&aid, 0, sizeof(aid));
|
2006-01-05 06:42:45 +00:00
|
|
|
strlcpy(aid.user_name, "Unknown user", sizeof(aid.user_name));
|
|
|
|
strlcpy(aid.team_name, "Unknown team", sizeof(aid.team_name));
|
2004-10-14 22:01:05 +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;
|
|
|
|
|
2004-04-02 08:07:12 +00:00
|
|
|
if (!boinc_file_exists(INIT_DATA_FILE)) {
|
2004-10-14 22:01:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Can't open init data file - running in standalone mode\n"
|
|
|
|
);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
f = boinc_fopen(INIT_DATA_FILE, "r");
|
|
|
|
retval = parse_init_data_file(f, aid);
|
|
|
|
fclose(f);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Can't parse init data file - running in standalone mode\n"
|
|
|
|
);
|
|
|
|
return retval;
|
2004-02-06 09:34:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-24 04:03:34 +00:00
|
|
|
int boinc_write_init_data_file() {
|
|
|
|
FILE* f = boinc_fopen(INIT_DATA_FILE, "w");
|
|
|
|
if (!f) return ERR_FOPEN;
|
|
|
|
int retval = write_init_data_file(f, aid);
|
|
|
|
fclose(f);
|
|
|
|
return retval;
|
|
|
|
}
|
2002-10-28 22:30:37 +00:00
|
|
|
|
2004-12-09 01:07:01 +00:00
|
|
|
int boinc_report_app_status(
|
|
|
|
double cpu_time,
|
|
|
|
double checkpoint_cpu_time,
|
2005-02-16 23:30:29 +00:00
|
|
|
double _fraction_done
|
2004-12-09 01:07:01 +00:00
|
|
|
) {
|
2004-12-10 04:18:40 +00:00
|
|
|
char msg_buf[MSG_CHANNEL_SIZE];
|
2004-12-09 01:07:01 +00:00
|
|
|
sprintf(msg_buf,
|
|
|
|
"<current_cpu_time>%10.4f</current_cpu_time>\n"
|
|
|
|
"<checkpoint_cpu_time>%.15e</checkpoint_cpu_time>\n"
|
|
|
|
"<fraction_done>%2.8f</fraction_done>\n",
|
|
|
|
cpu_time,
|
|
|
|
checkpoint_cpu_time,
|
2005-02-16 23:30:29 +00:00
|
|
|
_fraction_done
|
2004-12-09 01:07:01 +00:00
|
|
|
);
|
|
|
|
app_client_shm->shm->app_status.send_msg(msg_buf);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-08-21 23:44:57 +00:00
|
|
|
|
2003-11-06 20:27:07 +00:00
|
|
|
// this can be called from the graphics thread
|
|
|
|
//
|
|
|
|
int boinc_wu_cpu_time(double& cpu_t) {
|
|
|
|
cpu_t = last_wu_cpu_time;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-02 22:51:11 +00:00
|
|
|
|
2006-02-23 07:54:11 +00:00
|
|
|
// this can be called from the graphics thread
|
|
|
|
//
|
|
|
|
int suspend_activities() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (options.direct_process_action) {
|
|
|
|
// in Windows this is called from a separate "timer thread",
|
|
|
|
// and Windows lets us suspend the worker thread
|
|
|
|
//
|
|
|
|
SuspendThread(worker_thread_handle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this can be called from the graphics thread
|
|
|
|
//
|
|
|
|
int resume_activities() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (options.direct_process_action) {
|
|
|
|
// in Windows this is called from a separate "timer thread",
|
|
|
|
// and Windows lets us resume the worker thread
|
|
|
|
//
|
|
|
|
ResumeThread(worker_thread_handle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-02 08:28:23 +00:00
|
|
|
// this can be called from the graphics thread
|
|
|
|
//
|
|
|
|
int restore_activities() {
|
|
|
|
int retval;
|
|
|
|
if (boinc_status.suspended) {
|
|
|
|
retval = suspend_activities();
|
|
|
|
} else {
|
|
|
|
retval = resume_activities();
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
static void handle_heartbeat_msg() {
|
2004-07-08 03:38:52 +00:00
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
|
|
|
if (app_client_shm->shm->heartbeat.get_msg(buf)) {
|
|
|
|
if (match_tag(buf, "<heartbeat/>")) {
|
2005-07-23 08:10:39 +00:00
|
|
|
heartbeat_giveup_time = interrupt_count + HEARTBEAT_GIVEUP_PERIOD;
|
2004-07-08 03:38:52 +00:00
|
|
|
}
|
|
|
|
if (match_tag(buf, "<enable_heartbeat/>")) {
|
|
|
|
heartbeat_active = true;
|
|
|
|
}
|
|
|
|
if (match_tag(buf, "<disable_heartbeat/>")) {
|
|
|
|
heartbeat_active = false;
|
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
static void handle_upload_file_status() {
|
2005-04-28 23:19:58 +00:00
|
|
|
char path[256], buf[256], log_name[256];
|
2005-04-27 06:55:28 +00:00
|
|
|
std::string filename;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
relative_to_absolute("", path);
|
|
|
|
DirScanner dirscan(path);
|
|
|
|
while (dirscan.scan(filename)) {
|
2005-04-28 23:19:58 +00:00
|
|
|
strcpy(buf, filename.c_str());
|
|
|
|
if (strstr(buf, UPLOAD_FILE_STATUS_PREFIX) != buf) continue;
|
|
|
|
strcpy(log_name, buf+strlen(UPLOAD_FILE_STATUS_PREFIX));
|
2005-04-27 06:55:28 +00:00
|
|
|
FILE* f = boinc_fopen(filename.c_str(), "r");
|
2005-04-28 23:19:58 +00:00
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "handle_file_upload_status: can't open %s\n", filename.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-27 06:55:28 +00:00
|
|
|
fgets(buf, 256, f);
|
2005-04-28 23:19:58 +00:00
|
|
|
fclose(f);
|
|
|
|
if (parse_int(buf, "<status>", status)) {
|
|
|
|
UPLOAD_FILE_STATUS uf;
|
|
|
|
uf.name = std::string(log_name);
|
|
|
|
uf.status = status;
|
|
|
|
upload_file_status.push_back(uf);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "handle_upload_file_status: can't parse %s\n", buf);
|
|
|
|
}
|
2005-04-27 06:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle trickle and file upload messages
|
|
|
|
//
|
2004-08-03 09:50:24 +00:00
|
|
|
static void handle_trickle_down_msg() {
|
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
2004-07-08 03:38:52 +00:00
|
|
|
if (app_client_shm->shm->trickle_down.get_msg(buf)) {
|
|
|
|
if (match_tag(buf, "<have_trickle_down/>")) {
|
|
|
|
have_trickle_down = true;
|
|
|
|
}
|
2005-04-28 23:19:58 +00:00
|
|
|
if (match_tag(buf, "<upload_file_status/>")) {
|
2005-04-27 06:55:28 +00:00
|
|
|
handle_upload_file_status();
|
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_process_control_msg() {
|
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
2004-07-08 03:38:52 +00:00
|
|
|
if (app_client_shm->shm->process_control_request.get_msg(buf)) {
|
|
|
|
if (match_tag(buf, "<suspend/>")) {
|
2004-12-05 23:52:17 +00:00
|
|
|
boinc_status.suspended = true;
|
2006-02-23 08:08:49 +00:00
|
|
|
suspend_activities();
|
2004-07-08 03:38:52 +00:00
|
|
|
}
|
2004-08-03 09:50:24 +00:00
|
|
|
|
|
|
|
if (match_tag(buf, "<resume/>")) {
|
2004-12-05 23:52:17 +00:00
|
|
|
boinc_status.suspended = false;
|
2006-02-23 08:08:49 +00:00
|
|
|
resume_activities();
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
|
2004-07-08 03:38:52 +00:00
|
|
|
if (match_tag(buf, "<quit/>")) {
|
2004-12-05 23:52:17 +00:00
|
|
|
boinc_status.quit_request = true;
|
2004-08-03 09:50:24 +00:00
|
|
|
if (options.direct_process_action) {
|
2005-03-15 20:23:24 +00:00
|
|
|
boinc_exit(0);
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2004-07-08 03:38:52 +00:00
|
|
|
}
|
2006-02-03 20:48:48 +00:00
|
|
|
if (match_tag(buf, "<abort/>")) {
|
|
|
|
boinc_status.abort_request = true;
|
|
|
|
if (options.direct_process_action) {
|
2006-03-11 21:30:26 +00:00
|
|
|
boinc_exit(0);
|
2006-02-03 20:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2005-05-20 00:48:52 +00:00
|
|
|
if (match_tag(buf, "<reread_app_info/>")) {
|
|
|
|
boinc_status.reread_init_data_file = true;
|
|
|
|
}
|
2005-08-17 22:08:35 +00:00
|
|
|
if (match_tag(buf, "<network_available/>")) {
|
|
|
|
have_network = 1;
|
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#ifdef _WIN32
|
2004-10-26 20:33:16 +00:00
|
|
|
static void CALLBACK worker_timer(
|
|
|
|
UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2
|
|
|
|
) {
|
2002-08-05 00:29:34 +00:00
|
|
|
#else
|
2005-02-16 23:30:29 +00:00
|
|
|
static void worker_timer(int /*a*/) {
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif
|
2005-07-23 08:10:39 +00:00
|
|
|
interrupt_count++;
|
2002-08-05 00:29:34 +00:00
|
|
|
if (!ready_to_checkpoint) {
|
2005-03-15 20:23:24 +00:00
|
|
|
time_until_checkpoint -= TIMER_PERIOD;
|
2002-08-05 00:29:34 +00:00
|
|
|
if (time_until_checkpoint <= 0) {
|
|
|
|
ready_to_checkpoint = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-09 15:29:18 +00:00
|
|
|
// handle messages from the core client
|
|
|
|
//
|
2004-06-29 01:15:51 +00:00
|
|
|
if (app_client_shm) {
|
2004-08-03 09:50:24 +00:00
|
|
|
if (options.check_heartbeat) {
|
|
|
|
handle_heartbeat_msg();
|
|
|
|
}
|
|
|
|
if (options.handle_trickle_downs) {
|
|
|
|
handle_trickle_down_msg();
|
|
|
|
}
|
2005-08-12 21:24:07 +00:00
|
|
|
if (!in_critical_section && options.handle_process_control) {
|
2004-08-03 09:50:24 +00:00
|
|
|
handle_process_control_msg();
|
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
|
|
|
|
2004-10-12 20:56:44 +00:00
|
|
|
// see if the core client has died, which means we need to die too
|
2005-08-12 21:24:07 +00:00
|
|
|
// (unless we're in a critical section)
|
2004-07-09 15:29:18 +00:00
|
|
|
//
|
2005-08-12 21:24:07 +00:00
|
|
|
if (!in_critical_section && options.check_heartbeat && heartbeat_active) {
|
2005-07-23 08:10:39 +00:00
|
|
|
if (heartbeat_giveup_time < interrupt_count) {
|
2004-10-12 20:56:44 +00:00
|
|
|
fprintf(stderr,
|
2005-03-21 19:57:44 +00:00
|
|
|
"No heartbeat from core client for %d sec - exiting\n",
|
2005-07-23 08:10:39 +00:00
|
|
|
interrupt_count - (heartbeat_giveup_time - HEARTBEAT_GIVEUP_PERIOD)
|
2004-10-12 20:56:44 +00:00
|
|
|
);
|
2004-08-03 09:50:24 +00:00
|
|
|
if (options.direct_process_action) {
|
2005-03-15 20:23:24 +00:00
|
|
|
boinc_exit(0);
|
2004-08-03 09:50:24 +00:00
|
|
|
} else {
|
2005-03-15 20:23:24 +00:00
|
|
|
boinc_status.no_heartbeat = true;
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
if (options.send_status_msgs) {
|
2005-03-15 20:23:24 +00:00
|
|
|
time_until_fraction_done_update -= TIMER_PERIOD;
|
2004-08-03 09:50:24 +00:00
|
|
|
if (time_until_fraction_done_update <= 0) {
|
|
|
|
double cur_cpu;
|
2004-12-05 23:52:17 +00:00
|
|
|
boinc_worker_thread_cpu_time(cur_cpu);
|
2004-08-03 09:50:24 +00:00
|
|
|
last_wu_cpu_time = cur_cpu + initial_wu_cpu_time;
|
2005-03-30 21:11:49 +00:00
|
|
|
update_app_progress(last_wu_cpu_time, last_checkpoint_cpu_time);
|
2005-03-21 19:57:44 +00:00
|
|
|
time_until_fraction_done_update = (int)aid.fraction_done_update_period;
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.handle_trickle_ups) {
|
|
|
|
send_trickle_up_msg();
|
2004-07-08 03:38:52 +00:00
|
|
|
}
|
2004-12-05 23:52:17 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// poor man's CPU time accounting for Win9x
|
|
|
|
//
|
2004-12-06 00:08:04 +00:00
|
|
|
if (!boinc_status.suspended) {
|
2004-12-05 23:52:17 +00:00
|
|
|
nrunning_ticks++;
|
|
|
|
}
|
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2005-08-11 18:18:03 +00:00
|
|
|
#ifndef _WIN32
|
2005-06-08 20:28:05 +00:00
|
|
|
void* timer_thread(void*) {
|
2005-08-11 21:26:02 +00:00
|
|
|
block_sigalrm();
|
2005-05-22 00:13:16 +00:00
|
|
|
while(1) {
|
2005-07-05 10:23:20 +00:00
|
|
|
boinc_sleep(TIMER_PERIOD);
|
2005-05-22 00:13:16 +00:00
|
|
|
worker_timer(0);
|
|
|
|
}
|
2005-11-21 18:34:44 +00:00
|
|
|
return 0;
|
2005-05-22 00:13:16 +00:00
|
|
|
}
|
2005-08-11 18:18:03 +00:00
|
|
|
|
|
|
|
void worker_signal_handler(int) {
|
|
|
|
getrusage(RUSAGE_SELF, &worker_thread_ru);
|
|
|
|
if (options.direct_process_action) {
|
|
|
|
while (boinc_status.suspended) {
|
|
|
|
sleep(1); // don't use boinc_sleep() because it does FP math
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-22 23:12:09 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2005-08-11 18:18:03 +00:00
|
|
|
// set up timer actitivies.
|
2005-01-28 01:58:11 +00:00
|
|
|
// This is called only and always by the worker thread
|
|
|
|
//
|
2004-10-26 20:33:16 +00:00
|
|
|
int set_worker_timer() {
|
2002-08-05 00:29:34 +00:00
|
|
|
int retval=0;
|
2005-01-28 01:58:11 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#ifdef _WIN32
|
2005-01-28 01:58:11 +00:00
|
|
|
DuplicateHandle(
|
|
|
|
GetCurrentProcess(),
|
|
|
|
GetCurrentThread(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&worker_thread_handle,
|
|
|
|
0,
|
|
|
|
FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS
|
|
|
|
);
|
2003-03-18 19:37:09 +00:00
|
|
|
|
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
|
2004-03-26 18:32:57 +00:00
|
|
|
//
|
2002-11-14 00:49:24 +00:00
|
|
|
timer_id = timeSetEvent(
|
2005-03-15 20:23:24 +00:00
|
|
|
(int)(TIMER_PERIOD*1000), // uDelay
|
|
|
|
(int)(TIMER_PERIOD*1000), // uResolution
|
2004-10-26 20:33:16 +00:00
|
|
|
worker_timer, // lpTimeProc
|
2002-10-28 22:30:37 +00:00
|
|
|
NULL, // dwUser
|
|
|
|
TIME_PERIODIC // fuEvent
|
2003-09-05 21:26:21 +00:00
|
|
|
);
|
2005-01-28 01:58:11 +00:00
|
|
|
|
|
|
|
// lower our priority here
|
|
|
|
//
|
2005-04-12 23:58:01 +00:00
|
|
|
SetThreadPriority(worker_thread_handle, THREAD_PRIORITY_IDLE);
|
2005-05-22 00:13:16 +00:00
|
|
|
#else
|
2005-05-23 00:07:29 +00:00
|
|
|
retval = pthread_create(&timer_thread_handle, NULL, timer_thread, NULL);
|
2005-05-22 00:13:16 +00:00
|
|
|
if (retval) {
|
2005-06-25 17:48:17 +00:00
|
|
|
fprintf(stderr, "set_worker_timer(): pthread_create(): %d", retval);
|
2005-05-22 00:13:16 +00:00
|
|
|
}
|
2005-08-11 18:18:03 +00:00
|
|
|
|
2005-07-05 10:23:20 +00:00
|
|
|
struct sigaction sa;
|
|
|
|
itimerval value;
|
2005-08-11 18:18:03 +00:00
|
|
|
sa.sa_handler = worker_signal_handler;
|
2005-07-05 10:23:20 +00:00
|
|
|
sa.sa_flags = SA_RESTART;
|
2005-10-02 18:23:38 +00:00
|
|
|
sigemptyset(&sa.sa_mask);
|
2005-07-05 10:23:20 +00:00
|
|
|
retval = sigaction(SIGALRM, &sa, NULL);
|
|
|
|
if (retval) {
|
|
|
|
perror("boinc set_worker_timer() sigaction");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
value.it_value.tv_sec = TIMER_PERIOD;
|
|
|
|
value.it_value.tv_usec = 0;
|
|
|
|
value.it_interval = value.it_value;
|
|
|
|
retval = setitimer(ITIMER_REAL, &value, NULL);
|
|
|
|
if (retval) {
|
|
|
|
perror("boinc set_worker_timer() setitimer");
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2004-07-06 04:01:15 +00:00
|
|
|
int boinc_send_trickle_up(char* variety, char* p) {
|
2004-08-03 09:50:24 +00:00
|
|
|
if (!options.handle_trickle_ups) return ERR_NO_OPTION;
|
2004-04-23 00:05:16 +00:00
|
|
|
FILE* f = boinc_fopen(TRICKLE_UP_FILENAME, "wb");
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!f) return ERR_FOPEN;
|
2004-07-06 04:01:15 +00:00
|
|
|
fprintf(f, "<variety>%s</variety>\n", variety);
|
2004-01-04 06:48:40 +00:00
|
|
|
size_t n = fwrite(p, strlen(p), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
if (n != 1) return ERR_WRITE;
|
2004-04-23 00:05:16 +00:00
|
|
|
have_new_trickle_up = true;
|
2004-01-04 06:48:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-11-06 20:27:07 +00:00
|
|
|
|
2005-05-22 23:00:35 +00:00
|
|
|
// logically this should be a bool.
|
|
|
|
// But it needs to be an int to be compatible with C
|
|
|
|
//
|
2004-12-22 15:02:03 +00:00
|
|
|
int boinc_time_to_checkpoint() {
|
2005-01-19 15:54:04 +00:00
|
|
|
if (ready_to_checkpoint) {
|
2005-08-12 21:24:07 +00:00
|
|
|
in_critical_section = true;
|
2005-05-22 23:00:35 +00:00
|
|
|
return 1;
|
2003-11-06 20:27:07 +00:00
|
|
|
}
|
2004-12-22 15:02:03 +00:00
|
|
|
return 0;
|
2003-11-06 20:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_checkpoint_completed() {
|
2004-12-05 23:52:17 +00:00
|
|
|
double cur_cpu;
|
|
|
|
boinc_calling_thread_cpu_time(cur_cpu);
|
2003-11-06 20:27:07 +00:00
|
|
|
last_wu_cpu_time = cur_cpu + aid.wu_cpu_time;
|
|
|
|
last_checkpoint_cpu_time = last_wu_cpu_time;
|
2005-03-30 21:11:49 +00:00
|
|
|
update_app_progress(last_checkpoint_cpu_time, last_checkpoint_cpu_time);
|
2005-03-21 19:57:44 +00:00
|
|
|
time_until_checkpoint = (int)aid.checkpoint_period;
|
2005-08-12 21:24:07 +00:00
|
|
|
in_critical_section = false;
|
2003-11-06 20:27:07 +00:00
|
|
|
ready_to_checkpoint = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-12 21:24:07 +00:00
|
|
|
void boinc_begin_critical_section() {
|
|
|
|
in_critical_section = true;
|
|
|
|
}
|
|
|
|
void boinc_end_critical_section() {
|
|
|
|
in_critical_section = false;
|
|
|
|
}
|
|
|
|
|
2003-11-06 20:27:07 +00:00
|
|
|
int boinc_fraction_done(double x) {
|
|
|
|
fraction_done = x;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-07-02 05:06:30 +00:00
|
|
|
|
2006-03-15 19:27:37 +00:00
|
|
|
// for use by graphics code.
|
|
|
|
// Caller should check for values outside [0..1];
|
|
|
|
// that means undefined (no information available).
|
|
|
|
//
|
|
|
|
double boinc_get_fraction_done() {
|
|
|
|
return fraction_done;
|
|
|
|
}
|
|
|
|
|
2006-01-06 23:38:49 +00:00
|
|
|
int boinc_receive_trickle_down(char* buf, int len) {
|
2004-07-02 05:06:30 +00:00
|
|
|
std::string filename;
|
2004-07-06 04:01:15 +00:00
|
|
|
char path[256];
|
2004-08-03 09:50:24 +00:00
|
|
|
|
|
|
|
if (!options.handle_trickle_downs) return false;
|
|
|
|
|
2004-07-02 05:06:30 +00:00
|
|
|
if (have_trickle_down) {
|
2004-08-03 09:50:24 +00:00
|
|
|
relative_to_absolute("", path);
|
2004-07-06 04:01:15 +00:00
|
|
|
DirScanner dirscan(path);
|
2004-07-02 05:06:30 +00:00
|
|
|
while (dirscan.scan(filename)) {
|
2004-07-06 04:01:15 +00:00
|
|
|
fprintf(stderr, "scan: %s\n", filename.c_str());
|
2004-07-02 05:06:30 +00:00
|
|
|
if (strstr(filename.c_str(), "trickle_down")) {
|
|
|
|
strncpy(buf, filename.c_str(), len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2004-10-22 05:46:25 +00:00
|
|
|
have_trickle_down = false;
|
2004-07-02 05:06:30 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
int boinc_upload_file(std::string& name) {
|
|
|
|
char buf[256];
|
2005-04-28 23:19:58 +00:00
|
|
|
std::string pname;
|
|
|
|
int retval;
|
2005-04-27 06:55:28 +00:00
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
retval = boinc_resolve_filename_s(name.c_str(), pname);
|
|
|
|
if (retval) return retval;
|
|
|
|
sprintf(buf, "%s%s", UPLOAD_FILE_REQ_PREFIX, name.c_str());
|
2005-04-27 06:55:28 +00:00
|
|
|
FILE* f = boinc_fopen(buf, "w");
|
|
|
|
if (!f) return ERR_FOPEN;
|
|
|
|
have_new_upload_file = true;
|
2005-05-04 22:38:14 +00:00
|
|
|
fclose(f);
|
2005-04-27 06:55:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int boinc_upload_status(std::string& name) {
|
|
|
|
for (unsigned int i=0; i<upload_file_status.size(); i++) {
|
|
|
|
UPLOAD_FILE_STATUS& ufs = upload_file_status[i];
|
|
|
|
if (ufs.name == name) {
|
|
|
|
return ufs.status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2005-11-03 05:31:21 +00:00
|
|
|
void boinc_ops_per_cpu_sec(double fp, double i) {
|
|
|
|
fpops_per_cpu_sec = fp;
|
|
|
|
intops_per_cpu_sec = i;
|
2005-06-24 04:34:45 +00:00
|
|
|
}
|
|
|
|
|
2005-11-03 05:31:21 +00:00
|
|
|
void boinc_ops_cumulative(double fp, double i) {
|
|
|
|
fpops_cumulative = fp;
|
|
|
|
intops_cumulative = i;
|
2005-06-24 04:34:45 +00:00
|
|
|
}
|
2005-04-27 06:55:28 +00:00
|
|
|
|
2005-08-17 21:05:26 +00:00
|
|
|
void boinc_not_using_cpu() {
|
|
|
|
non_cpu_intensive = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void boinc_using_cpu() {
|
|
|
|
non_cpu_intensive = 0;
|
|
|
|
}
|
|
|
|
|
2005-08-17 22:08:35 +00:00
|
|
|
void boinc_need_network() {
|
|
|
|
want_network = 1;
|
|
|
|
have_network = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_network_poll() {
|
|
|
|
return have_network?0:1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void boinc_network_done() {
|
|
|
|
want_network = 0;
|
|
|
|
}
|
|
|
|
|
2005-08-11 21:26:02 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
// block SIGALRM, so that the worker thread will be forced to handle it
|
|
|
|
//
|
|
|
|
void block_sigalrm() {
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGALRM);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_0fa0410386 = "$Id$";
|