2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2003-08-21 23:44:57 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC 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 3 of the License, or (at your option) any later version.
|
2003-08-21 23:44:57 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-08-05 00:29:34 +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>
|
2008-02-27 23:26:38 +00:00
|
|
|
#include <cstring>
|
2004-06-16 12:01:49 +00:00
|
|
|
#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-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"
|
2007-10-31 20:52:25 +00:00
|
|
|
#include "str_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"
|
2007-09-25 16:04:45 +00:00
|
|
|
#include "common_defs.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"
|
|
|
|
|
2009-03-23 17:26:20 +00:00
|
|
|
//#define DEBUG_BOINC_API
|
2009-03-20 23:02:31 +00:00
|
|
|
|
2006-03-23 11:38:49 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include "mac_backtrace.h"
|
2008-01-08 18:58:44 +00:00
|
|
|
#define GETRUSAGE_IN_TIMER_THREAD
|
|
|
|
// call getrusage() in the timer thread,
|
|
|
|
// rather than in the worker thread's signal handler
|
|
|
|
// (which can cause crashes on Mac)
|
|
|
|
// If you want, you can set this for Linux too:
|
|
|
|
// CPPFLAGS=-DGETRUSAGE_IN_TIMER_THREAD
|
2006-03-23 11:38:49 +00:00
|
|
|
#endif
|
|
|
|
|
2005-08-11 18:18:03 +00:00
|
|
|
// Implementation notes:
|
2007-11-29 19:48:04 +00:00
|
|
|
// 1) Thread structure, 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".
|
|
|
|
// 2) All variables that are accessed by two threads (i.e. worker and timer)
|
|
|
|
// MUST be declared volatile.
|
|
|
|
// 3) For compatibility with C, we use int instead of bool various places
|
2005-08-11 18:18:03 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2007-09-21 18:10:54 +00:00
|
|
|
const char* api_version="API_VERSION_"PACKAGE_VERSION;
|
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
|
|
|
|
// time until report fraction done to core client
|
2006-12-09 00:10:53 +00:00
|
|
|
static volatile double fraction_done;
|
|
|
|
static volatile double last_checkpoint_cpu_time;
|
|
|
|
static volatile bool ready_to_checkpoint = false;
|
2008-02-06 20:39:10 +00:00
|
|
|
static volatile int in_critical_section=0;
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile double last_wu_cpu_time;
|
2006-12-09 00:10:53 +00:00
|
|
|
static volatile bool standalone = false;
|
|
|
|
static volatile double initial_wu_cpu_time;
|
2005-03-19 21:39:17 +00:00
|
|
|
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-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,
|
2007-11-29 19:48:04 +00:00
|
|
|
// and doesn't have big jump after 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 22:08:35 +00:00
|
|
|
static int want_network = 0;
|
|
|
|
static int have_network = 1;
|
2006-12-07 17:30:48 +00:00
|
|
|
bool g_sleep = false;
|
2007-09-25 16:04:45 +00:00
|
|
|
// simulate unresponsive app by setting to true (debugging)
|
2007-06-08 07:55:27 +00:00
|
|
|
static FUNC_PTR timer_callback = 0;
|
2006-05-11 18:28:35 +00:00
|
|
|
|
2007-12-21 20:57:48 +00:00
|
|
|
#define TIMER_PERIOD 0.1
|
2005-07-23 08:10:39 +00:00
|
|
|
// period of worker-thread timer interrupts.
|
2007-12-21 20:57:48 +00:00
|
|
|
// Determines rate of handlling messages from client.
|
|
|
|
#define TIMERS_PER_SEC 10
|
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.
|
2008-01-13 00:12:14 +00:00
|
|
|
#define HEARTBEAT_GIVEUP_COUNT ((int)(30/TIMER_PERIOD))
|
2005-07-23 08:10:39 +00:00
|
|
|
// quit if no heartbeat from core in this #interrupts
|
2007-12-21 20:57:48 +00:00
|
|
|
#define LOCKFILE_TIMEOUT_PERIOD 35
|
|
|
|
// quit if we cannot aquire slot lock file in this #secs after startup
|
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
|
2005-08-11 18:18:03 +00:00
|
|
|
#else
|
2008-01-13 00:12:14 +00:00
|
|
|
static volatile bool worker_thread_exit_flag = false;
|
|
|
|
static volatile int worker_thread_exit_status;
|
|
|
|
// the above are used by the timer thread to tell
|
|
|
|
// the worker thread to exit
|
2005-08-11 18:18:03 +00:00
|
|
|
static pthread_t timer_thread_handle;
|
2008-01-08 18:58:44 +00:00
|
|
|
#ifndef GETRUSAGE_IN_TIMER_THREAD
|
2007-12-21 19:23:22 +00:00
|
|
|
static struct rusage worker_thread_ru;
|
2004-02-06 09:34:00 +00:00
|
|
|
#endif
|
2008-01-03 20:43:48 +00:00
|
|
|
#endif
|
2004-02-06 09:34:00 +00:00
|
|
|
|
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;
|
|
|
|
|
2008-01-09 17:32:02 +00:00
|
|
|
static void graphics_cleanup();
|
2008-01-17 18:09:30 +00:00
|
|
|
//static int suspend_activities();
|
|
|
|
//static int resume_activities();
|
|
|
|
//static void boinc_exit(int);
|
2008-01-09 17:32:02 +00:00
|
|
|
static void block_sigalrm();
|
|
|
|
static int start_worker_signals();
|
2007-09-27 13:09:48 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2009-01-23 20:30:34 +00:00
|
|
|
#else
|
|
|
|
#ifdef __EMX__
|
|
|
|
if (attach_shmem(aid.shmem_seg_name, (void**)&app_client_shm->shm)) {
|
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
2007-08-27 09:17:38 +00:00
|
|
|
#else
|
2007-09-26 10:17:11 +00:00
|
|
|
if (aid.shmem_seg_name == -1) {
|
|
|
|
// Version 6 Unix/Linux/Mac client
|
- server code: at some point I made a global var "SCHED_CONFIG config",
mostly so that the parse function could assume
that everything was initially zero.
However, various back-end functions pass around SCHED_CONFIG&
as an argument (also named "config").
This creates a shadow, which is always bad.
Worse is the possibility that some projects have back-end programs
that have a SCHED_CONFIG variable that's automatic,
and therefore isn't zero initially,
and therefore isn't parsing correctly.
To fix this, I changed the 2 vectors in SCHED_CONFIG into pointers,
and have the parse routine zero the structure.
I was tempted to remove the SCHED_CONFIG& args to back-end functions,
but this would have broken some projects' code.
I did, however, change the name from config to config_loc
to avoid shadowing.
Also fixed various other compiler warnings.
svn path=/trunk/boinc/; revision=15541
2008-07-02 17:24:53 +00:00
|
|
|
if (attach_shmem_mmap(MMAPPED_FILE_NAME, (void**)&app_client_shm->shm)) {
|
2007-09-26 10:17:11 +00:00
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2009-01-23 20:30:34 +00:00
|
|
|
// version 5 Unix/Linux/Mac client
|
2007-09-26 10:17:11 +00:00
|
|
|
if (attach_shmem(aid.shmem_seg_name, (void**)&app_client_shm->shm)) {
|
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
2009-01-23 20:30:34 +00:00
|
|
|
#endif
|
2007-08-27 09:17:38 +00:00
|
|
|
#endif // ! _WIN32
|
2005-03-29 23:13:28 +00:00
|
|
|
if (app_client_shm == NULL) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-27 18:25:29 +00:00
|
|
|
// Return CPU time of process.
|
2005-08-11 18:18:03 +00:00
|
|
|
//
|
2007-06-08 07:55:27 +00:00
|
|
|
double boinc_worker_thread_cpu_time() {
|
2007-10-16 23:02:13 +00:00
|
|
|
double cpu;
|
2005-03-29 23:13:28 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-01 18:34:08 +00:00
|
|
|
int retval;
|
2009-03-24 17:18:09 +00:00
|
|
|
retval = boinc_process_cpu_time(GetCurrentProcess(), cpu);
|
2006-05-01 05:54:52 +00:00
|
|
|
if (retval) {
|
2005-03-29 23:13:28 +00:00
|
|
|
cpu = nrunning_ticks * TIMER_PERIOD; // for Win9x
|
|
|
|
}
|
|
|
|
#else
|
2008-01-08 18:58:44 +00:00
|
|
|
#ifdef GETRUSAGE_IN_TIMER_THREAD
|
2008-01-03 20:43:48 +00:00
|
|
|
struct rusage worker_thread_ru;
|
|
|
|
getrusage(RUSAGE_SELF, &worker_thread_ru);
|
|
|
|
#endif
|
2007-06-08 07:55:27 +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
|
2007-10-16 23:02:13 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// The following paranoia is (I hope) not needed anymore.
|
|
|
|
// In any case, the check for CPU incrementing faster than real time
|
|
|
|
// is misguided - it assumes no multi-threading.
|
|
|
|
//
|
|
|
|
static double last_cpu=0;
|
|
|
|
// last value returned by this func
|
|
|
|
static time_t last_time=0;
|
|
|
|
// when it was returned
|
|
|
|
time_t now = time(0);
|
|
|
|
double time_diff = (double)(now - last_time);
|
2007-10-12 23:12:50 +00:00
|
|
|
if (!finite(cpu)) {
|
|
|
|
fprintf(stderr, "CPU time infinite or NaN\n");
|
|
|
|
last_time = now;
|
|
|
|
return last_cpu;
|
2006-06-15 18:22:07 +00:00
|
|
|
}
|
2007-10-12 23:12:50 +00:00
|
|
|
double cpu_diff = cpu - last_cpu;
|
|
|
|
if (cpu_diff < 0) {
|
|
|
|
fprintf(stderr, "Negative CPU time change\n");
|
2007-06-08 07:55:27 +00:00
|
|
|
last_time = now;
|
2007-10-12 23:12:50 +00:00
|
|
|
return last_cpu;
|
|
|
|
}
|
|
|
|
if (cpu_diff>(time_diff + 1)) {
|
2007-10-16 23:02:13 +00:00
|
|
|
fprintf(stderr, "CPU time incrementing faster than real time. Correcting.\n");
|
2007-10-12 23:12:50 +00:00
|
|
|
cpu = last_cpu + time_diff + 1; // allow catch-up
|
2006-06-15 18:22:07 +00:00
|
|
|
}
|
2007-10-12 23:12:50 +00:00
|
|
|
last_cpu = cpu;
|
|
|
|
last_time = now;
|
2007-10-16 23:02:13 +00:00
|
|
|
#endif
|
2007-06-08 07:55:27 +00:00
|
|
|
return cpu;
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
|
|
|
|
2008-01-08 18:58:44 +00:00
|
|
|
// Communicate to the core client (via shared mem)
|
|
|
|
// the current CPU time and fraction done.
|
2007-10-30 23:03:21 +00:00
|
|
|
// NOTE: various bugs could cause some of these FP numbers to be enormous,
|
|
|
|
// possibly overflowing the buffer.
|
|
|
|
// So use strlcat() instead of strcat()
|
2005-03-29 23:13:28 +00:00
|
|
|
//
|
2008-01-08 18:58:44 +00:00
|
|
|
// This is called only from the timer thread (so no need for synch)
|
|
|
|
//
|
2006-09-05 19:00:59 +00:00
|
|
|
static bool update_app_progress(double cpu_t, double cp_cpu_t) {
|
2007-11-07 18:53:36 +00:00
|
|
|
char msg_buf[MSG_CHANNEL_SIZE], buf[256];
|
2005-03-29 23:13:28 +00:00
|
|
|
|
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,
|
2007-11-07 18:53:36 +00:00
|
|
|
"<current_cpu_time>%e</current_cpu_time>\n"
|
|
|
|
"<checkpoint_cpu_time>%e</checkpoint_cpu_time>\n",
|
2006-06-26 17:09:23 +00:00
|
|
|
cpu_t, cp_cpu_t
|
2005-08-11 18:18:03 +00:00
|
|
|
);
|
2005-08-17 22:08:35 +00:00
|
|
|
if (want_network) {
|
2007-10-30 23:03:21 +00:00
|
|
|
strlcat(msg_buf, "<want_network>1</want_network>\n", MSG_CHANNEL_SIZE);
|
2005-08-17 22:08:35 +00:00
|
|
|
}
|
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;
|
2007-11-07 18:53:36 +00:00
|
|
|
sprintf(buf, "<fraction_done>%e</fraction_done>\n", fdone);
|
2007-10-30 23:03:21 +00:00
|
|
|
strlcat(msg_buf, buf, MSG_CHANNEL_SIZE);
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
2005-06-24 04:34:45 +00:00
|
|
|
if (fpops_per_cpu_sec) {
|
2007-11-07 18:53:36 +00:00
|
|
|
sprintf(buf, "<fpops_per_cpu_sec>%e</fpops_per_cpu_sec>\n", fpops_per_cpu_sec);
|
2007-10-30 23:03:21 +00:00
|
|
|
strlcat(msg_buf, buf, MSG_CHANNEL_SIZE);
|
2005-06-24 04:34:45 +00:00
|
|
|
}
|
|
|
|
if (fpops_cumulative) {
|
2007-11-07 18:53:36 +00:00
|
|
|
sprintf(buf, "<fpops_cumulative>%e</fpops_cumulative>\n", fpops_cumulative);
|
2007-10-30 23:03:21 +00:00
|
|
|
strlcat(msg_buf, buf, MSG_CHANNEL_SIZE);
|
2005-06-24 04:34:45 +00:00
|
|
|
}
|
2005-11-03 05:31:21 +00:00
|
|
|
if (intops_per_cpu_sec) {
|
2007-11-07 18:53:36 +00:00
|
|
|
sprintf(buf, "<intops_per_cpu_sec>%e</intops_per_cpu_sec>\n", intops_per_cpu_sec);
|
2007-10-30 23:03:21 +00:00
|
|
|
strlcat(msg_buf, buf, MSG_CHANNEL_SIZE);
|
2005-11-03 05:31:21 +00:00
|
|
|
}
|
|
|
|
if (intops_cumulative) {
|
2007-11-07 18:53:36 +00:00
|
|
|
sprintf(buf, "<intops_cumulative>%e</intops_cumulative>\n", intops_cumulative);
|
2007-10-30 23:03:21 +00:00
|
|
|
strlcat(msg_buf, buf, MSG_CHANNEL_SIZE);
|
2005-11-03 05:31:21 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2004-10-26 21:12:00 +00:00
|
|
|
int boinc_init() {
|
2006-04-19 23:28:08 +00:00
|
|
|
int retval;
|
2006-05-01 21:36:38 +00:00
|
|
|
if (!diagnostics_is_initialized()) {
|
2007-06-08 07:38:17 +00:00
|
|
|
retval = boinc_init_diagnostics(BOINC_DIAG_DEFAULTS);
|
2006-04-19 23:28:08 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
}
|
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-05-01 21:36:38 +00:00
|
|
|
if (!diagnostics_is_initialized()) {
|
2007-06-08 07:38:17 +00:00
|
|
|
retval = boinc_init_diagnostics(BOINC_DIAG_DEFAULTS);
|
2006-04-19 23:28:08 +00:00
|
|
|
if (retval) return 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;
|
2008-01-09 17:32:02 +00:00
|
|
|
retval = start_timer_thread();
|
|
|
|
if (retval) return retval;
|
|
|
|
#ifndef _WIN32
|
|
|
|
retval = start_worker_signals();
|
|
|
|
if (retval) return retval;
|
|
|
|
#endif
|
|
|
|
return 0;
|
2004-10-26 21:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
//
|
2009-03-23 17:26:20 +00:00
|
|
|
fprintf(stderr, "Can't acquire lockfile (%d) - waiting %ds\n",
|
|
|
|
retval, LOCKFILE_TIMEOUT_PERIOD
|
|
|
|
);
|
2007-12-21 20:57:48 +00:00
|
|
|
boinc_sleep(LOCKFILE_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) {
|
2009-03-23 17:00:34 +00:00
|
|
|
fprintf(stderr, "Can't acquire lockfile (%d) - exiting\n", retval);
|
|
|
|
#ifdef _WIN32
|
|
|
|
char buf[256];
|
|
|
|
windows_error_string(buf, 256);
|
|
|
|
fprintf(stderr, "Error: %s\n", buf);
|
|
|
|
#endif
|
|
|
|
boinc_exit(0); // status=0 means recoverable
|
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) {
|
2006-10-31 00:29:30 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Can't set up shared mem: %d\n"
|
|
|
|
"Will run in standalone mode.\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-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;
|
|
|
|
last_wu_cpu_time = aid.wu_cpu_time;
|
|
|
|
|
2007-11-29 19:48:04 +00:00
|
|
|
if (standalone) {
|
|
|
|
options.check_heartbeat = false;
|
|
|
|
}
|
2008-01-13 00:12:14 +00:00
|
|
|
heartbeat_giveup_time = interrupt_count + HEARTBEAT_GIVEUP_COUNT;
|
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;
|
2006-10-03 15:43:38 +00:00
|
|
|
s->reread_init_data_file = boinc_status.reread_init_data_file;
|
2006-02-03 20:48:48 +00:00
|
|
|
s->abort_request = boinc_status.abort_request;
|
2006-10-03 15:43:38 +00:00
|
|
|
s->working_set_size = boinc_status.working_set_size;
|
|
|
|
s->max_working_set_size = boinc_status.max_working_set_size;
|
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];
|
2006-06-07 08:56:55 +00:00
|
|
|
BOINCINFO("Sending Trickle Up Message");
|
2005-04-27 06:55:28 +00:00
|
|
|
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-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) {
|
2008-01-08 18:58:44 +00:00
|
|
|
fraction_done = 1;
|
2008-01-09 17:32:02 +00:00
|
|
|
fprintf(stderr, "called boinc_finish\n");
|
2008-01-08 18:58:44 +00:00
|
|
|
boinc_sleep(2.0); // let the timer thread send final messages
|
|
|
|
g_sleep = true; // then disable it
|
2005-03-30 21:11:49 +00:00
|
|
|
|
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;
|
2006-12-19 22:52:58 +00:00
|
|
|
boinc_write_init_data_file(aid);
|
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);
|
|
|
|
|
2007-06-28 22:51:51 +00:00
|
|
|
return 0; // never reached
|
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
|
2008-01-13 00:12:14 +00:00
|
|
|
// Unix: called only from the worker thread.
|
|
|
|
// Win: called from the worker or timer thread.
|
2005-03-05 23:12:31 +00:00
|
|
|
//
|
2008-01-17 18:09:30 +00:00
|
|
|
// make static eventually
|
2008-09-22 20:33:59 +00:00
|
|
|
//
|
2008-01-17 18:09:30 +00:00
|
|
|
void boinc_exit(int status) {
|
2009-03-23 17:26:20 +00:00
|
|
|
int retval;
|
2007-09-28 04:23:34 +00:00
|
|
|
if (options.backwards_compatible_graphics) {
|
|
|
|
graphics_cleanup();
|
2007-09-27 13:09:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-23 17:00:34 +00:00
|
|
|
if (options.main_program && file_lock.locked) {
|
2009-03-23 17:26:20 +00:00
|
|
|
retval = file_lock.unlock(LOCKFILE);
|
|
|
|
if (retval) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
char buf[256];
|
|
|
|
windows_error_string(buf, 256);
|
|
|
|
fprintf(stderr, "Can't unlock lockfile (%d): %s\n", retval, buf);
|
|
|
|
#else
|
|
|
|
perror("file unlock failed");
|
|
|
|
#endif
|
|
|
|
}
|
2008-09-22 20:33:59 +00:00
|
|
|
}
|
2005-01-19 15:54:04 +00:00
|
|
|
|
2006-03-14 00:46:41 +00:00
|
|
|
fflush(NULL);
|
|
|
|
|
2006-05-17 07:44:54 +00:00
|
|
|
boinc_finish_diag();
|
|
|
|
|
2007-11-29 19:48:04 +00:00
|
|
|
// various platforms have problems shutting down a process
|
|
|
|
// while other threads are still executing,
|
|
|
|
// or triggering endless exit()/atexit() loops.
|
|
|
|
//
|
2006-06-07 08:56:55 +00:00
|
|
|
BOINCINFO("Exit Status: %d", status);
|
2006-03-23 03:05:59 +00:00
|
|
|
#if defined(_WIN32)
|
2008-09-22 20:33:59 +00:00
|
|
|
// Halt all the threads and cleans up.
|
2006-03-23 03:05:59 +00:00
|
|
|
TerminateProcess(GetCurrentProcess(), status);
|
2009-03-20 21:05:26 +00:00
|
|
|
// note: the above CAN return!
|
|
|
|
Sleep(1000);
|
|
|
|
DebugBreak();
|
2006-03-23 03:05:59 +00:00
|
|
|
#elif defined(__APPLE_CC__)
|
|
|
|
// stops endless exit()/atexit() loops.
|
2005-03-05 23:12:31 +00:00
|
|
|
_exit(status);
|
2005-01-19 15:54:04 +00:00
|
|
|
#else
|
2008-06-23 19:51:07 +00:00
|
|
|
// arrange to exit with given status even if errors happen
|
|
|
|
// in atexit() functions
|
|
|
|
//
|
|
|
|
set_signal_exit_code(status);
|
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
|
|
|
}
|
|
|
|
|
2008-01-13 00:12:14 +00:00
|
|
|
static void exit_from_timer_thread(int status) {
|
2009-03-23 17:26:20 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
2009-03-20 23:02:31 +00:00
|
|
|
fprintf(stderr, "%f: exit_from_timer_thread(%d) called\n",
|
|
|
|
dtime(), status
|
|
|
|
);
|
|
|
|
#endif
|
2008-01-13 00:12:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// this seems to work OK on Windows
|
|
|
|
//
|
|
|
|
boinc_exit(status);
|
|
|
|
#else
|
|
|
|
// but on Unix there are synchronization problems;
|
|
|
|
// set a flag telling the worker thread to exit
|
|
|
|
//
|
|
|
|
worker_thread_exit_status = status;
|
|
|
|
worker_thread_exit_flag = true;
|
|
|
|
pthread_exit(NULL);
|
|
|
|
#endif
|
|
|
|
}
|
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;
|
|
|
|
|
2008-11-11 23:39:00 +00:00
|
|
|
if (aid.project_preferences) {
|
|
|
|
free(aid.project_preferences);
|
|
|
|
aid.project_preferences = NULL;
|
|
|
|
}
|
2004-10-14 22:01:05 +00:00
|
|
|
memset(&aid, 0, sizeof(aid));
|
2006-12-19 22:52:58 +00:00
|
|
|
strcpy(aid.user_name, "");
|
|
|
|
strcpy(aid.team_name, "");
|
|
|
|
aid.wu_cpu_time = 0;
|
|
|
|
aid.user_total_credit = 0;
|
|
|
|
aid.user_expavg_credit = 0;
|
|
|
|
aid.host_total_credit = 0;
|
|
|
|
aid.host_expavg_credit = 0;
|
2004-10-14 22:01:05 +00:00
|
|
|
aid.checkpoint_period = DEFAULT_CHECKPOINT_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;
|
|
|
|
}
|
|
|
|
|
2006-12-19 22:52:58 +00:00
|
|
|
int boinc_write_init_data_file(APP_INIT_DATA& x) {
|
2004-05-24 04:03:34 +00:00
|
|
|
FILE* f = boinc_fopen(INIT_DATA_FILE, "w");
|
|
|
|
if (!f) return ERR_FOPEN;
|
2006-12-19 22:52:58 +00:00
|
|
|
int retval = write_init_data_file(f, x);
|
2004-05-24 04:03:34 +00:00
|
|
|
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];
|
2006-06-16 21:19:00 +00:00
|
|
|
if (standalone) return 0;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-06-09 02:33:00 +00:00
|
|
|
int boinc_get_init_data_p(APP_INIT_DATA* app_init_data) {
|
|
|
|
*app_init_data = aid;
|
|
|
|
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-11-06 20:27:07 +00:00
|
|
|
int boinc_wu_cpu_time(double& cpu_t) {
|
|
|
|
cpu_t = last_wu_cpu_time;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-02 22:51:11 +00:00
|
|
|
|
2008-01-17 18:09:30 +00:00
|
|
|
// make static eventually
|
|
|
|
int suspend_activities() {
|
2006-06-07 08:56:55 +00:00
|
|
|
BOINCINFO("Received Suspend Message");
|
2006-02-23 07:54:11 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (options.direct_process_action) {
|
|
|
|
SuspendThread(worker_thread_handle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-17 18:09:30 +00:00
|
|
|
// make static eventually
|
|
|
|
int resume_activities() {
|
2006-06-07 08:56:55 +00:00
|
|
|
BOINCINFO("Received Resume Message");
|
2006-02-23 07:54:11 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (options.direct_process_action) {
|
|
|
|
ResumeThread(worker_thread_handle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-12 18:35:19 +00:00
|
|
|
|
2008-01-17 18:09:30 +00:00
|
|
|
int restore_activities() {
|
|
|
|
int retval;
|
|
|
|
if (boinc_status.suspended) {
|
2008-06-12 18:35:19 +00:00
|
|
|
retval = suspend_activities();
|
|
|
|
} else {
|
|
|
|
retval = resume_activities();
|
|
|
|
}
|
2008-01-17 18:09:30 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2006-02-23 07:54:11 +00:00
|
|
|
|
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];
|
2006-10-03 15:43:38 +00:00
|
|
|
double dtemp;
|
|
|
|
|
2004-07-08 03:38:52 +00:00
|
|
|
if (app_client_shm->shm->heartbeat.get_msg(buf)) {
|
|
|
|
if (match_tag(buf, "<heartbeat/>")) {
|
2008-01-13 00:12:14 +00:00
|
|
|
heartbeat_giveup_time = interrupt_count + HEARTBEAT_GIVEUP_COUNT;
|
2004-07-08 03:38:52 +00:00
|
|
|
}
|
2006-10-03 17:24:25 +00:00
|
|
|
if (parse_double(buf, "<wss>", dtemp)) {
|
2006-10-03 15:43:38 +00:00
|
|
|
boinc_status.working_set_size = dtemp;
|
|
|
|
}
|
2006-10-03 17:24:25 +00:00
|
|
|
if (parse_double(buf, "<max_wss>", dtemp)) {
|
2006-10-03 15:43:38 +00:00
|
|
|
boinc_status.max_working_set_size = dtemp;
|
|
|
|
}
|
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() {
|
2007-03-04 20:52:31 +00:00
|
|
|
char path[256], buf[256], log_name[256], *p;
|
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;
|
|
|
|
}
|
2007-03-04 20:52:31 +00:00
|
|
|
p = fgets(buf, 256, f);
|
2005-04-28 23:19:58 +00:00
|
|
|
fclose(f);
|
2007-03-04 20:52:31 +00:00
|
|
|
if (p && parse_int(buf, "<status>", status)) {
|
2005-04-28 23:19:58 +00:00
|
|
|
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)) {
|
2006-06-07 08:56:55 +00:00
|
|
|
BOINCINFO("Received Trickle Down Message");
|
2004-07-08 03:38:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-01-13 00:12:14 +00:00
|
|
|
// runs in timer thread
|
|
|
|
//
|
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)) {
|
2009-03-23 17:26:20 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
2009-03-20 23:02:31 +00:00
|
|
|
fprintf(stderr, "%f: got process control msg %s\n", dtime(), buf);
|
|
|
|
#endif
|
2004-07-08 03:38:52 +00:00
|
|
|
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/>")) {
|
2006-10-03 15:43:38 +00:00
|
|
|
BOINCINFO("Received quit message");
|
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) {
|
2008-01-13 00:12:14 +00:00
|
|
|
exit_from_timer_thread(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/>")) {
|
2006-10-03 15:43:38 +00:00
|
|
|
BOINCINFO("Received abort message");
|
2006-02-03 20:48:48 +00:00
|
|
|
boinc_status.abort_request = true;
|
|
|
|
if (options.direct_process_action) {
|
2006-05-08 18:35:56 +00:00
|
|
|
diagnostics_set_aborted_via_gui();
|
2006-03-23 20:23:22 +00:00
|
|
|
#if defined(_WIN32)
|
2006-03-21 12:04:14 +00:00
|
|
|
// Cause a controlled assert and dump the callstacks.
|
|
|
|
DebugBreak();
|
2006-03-23 20:23:22 +00:00
|
|
|
#elif defined(__APPLE__)
|
2006-03-23 11:38:49 +00:00
|
|
|
PrintBacktrace();
|
|
|
|
#endif
|
2008-06-18 16:43:05 +00:00
|
|
|
exit_from_timer_thread(EXIT_ABORTED_BY_CLIENT);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-29 19:48:04 +00:00
|
|
|
// The following is used by V6 apps so that graphics
|
|
|
|
// will work with pre-V6 clients.
|
|
|
|
// If we get a graphics message, run/kill the (separate) graphics app
|
|
|
|
//
|
|
|
|
//
|
2007-09-28 04:23:34 +00:00
|
|
|
struct GRAPHICS_APP {
|
|
|
|
bool fullscreen;
|
2007-09-25 16:04:45 +00:00
|
|
|
#ifdef _WIN32
|
2007-09-28 04:23:34 +00:00
|
|
|
HANDLE pid;
|
2007-09-25 16:04:45 +00:00
|
|
|
#else
|
2007-09-28 04:23:34 +00:00
|
|
|
int pid;
|
2007-09-25 16:04:45 +00:00
|
|
|
#endif
|
2007-09-28 04:23:34 +00:00
|
|
|
GRAPHICS_APP(bool f) {fullscreen=f;}
|
|
|
|
void run(char* path) {
|
2007-09-25 16:04:45 +00:00
|
|
|
int argc;
|
|
|
|
char* argv[4];
|
2007-09-27 03:34:27 +00:00
|
|
|
char abspath[1024];
|
|
|
|
#ifdef _WIN32
|
2008-03-29 22:07:50 +00:00
|
|
|
GetFullPathName(path, 1024, abspath, NULL);
|
2007-09-27 03:34:27 +00:00
|
|
|
#else
|
|
|
|
strcpy(abspath, path);
|
|
|
|
#endif
|
2007-09-25 16:04:45 +00:00
|
|
|
argv[0] = GRAPHICS_APP_FILENAME;
|
|
|
|
if (fullscreen) {
|
|
|
|
argv[1] = "--fullscreen";
|
|
|
|
argv[2] = 0;
|
|
|
|
argc = 2;
|
|
|
|
} else {
|
|
|
|
argv[1] = 0;
|
|
|
|
argc = 1;
|
|
|
|
}
|
2007-09-27 03:34:27 +00:00
|
|
|
int retval = run_program(0, abspath, argc, argv, 0, pid);
|
2007-09-25 16:04:45 +00:00
|
|
|
if (retval) {
|
2007-09-27 13:09:48 +00:00
|
|
|
pid = 0;
|
2007-09-25 16:04:45 +00:00
|
|
|
}
|
2007-09-28 04:23:34 +00:00
|
|
|
}
|
|
|
|
bool is_running() {
|
|
|
|
if (pid && process_exists(pid)) return true;
|
|
|
|
pid = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void kill() {
|
|
|
|
if (pid) {
|
2007-09-25 16:04:45 +00:00
|
|
|
kill_program(pid);
|
2007-09-27 13:09:48 +00:00
|
|
|
pid = 0;
|
2007-09-25 16:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-28 04:23:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static GRAPHICS_APP ga_win(false), ga_full(true);
|
|
|
|
static bool have_graphics_app;
|
2007-09-25 16:04:45 +00:00
|
|
|
|
2008-02-21 00:47:50 +00:00
|
|
|
// The following is for backwards compatibility with version 5 clients.
|
|
|
|
//
|
2007-09-25 16:04:45 +00:00
|
|
|
static inline void handle_graphics_messages() {
|
2007-09-28 04:23:34 +00:00
|
|
|
static char graphics_app_path[1024];
|
2007-09-25 16:04:45 +00:00
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
|
|
|
GRAPHICS_MSG m;
|
|
|
|
static bool first=true;
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
boinc_resolve_filename(
|
|
|
|
GRAPHICS_APP_FILENAME, graphics_app_path,
|
|
|
|
sizeof(graphics_app_path)
|
|
|
|
);
|
2008-02-21 00:47:50 +00:00
|
|
|
// if the above returns "graphics_app", there was no link file,
|
|
|
|
// so there's no graphics app
|
|
|
|
//
|
2007-09-25 16:04:45 +00:00
|
|
|
if (!strcmp(graphics_app_path, GRAPHICS_APP_FILENAME)) {
|
|
|
|
have_graphics_app = false;
|
|
|
|
} else {
|
|
|
|
have_graphics_app = true;
|
2007-09-28 04:23:34 +00:00
|
|
|
app_client_shm->shm->graphics_reply.send_msg(
|
|
|
|
xml_graphics_modes[MODE_HIDE_GRAPHICS]
|
|
|
|
);
|
2007-09-25 16:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_graphics_app) return;
|
|
|
|
|
|
|
|
if (app_client_shm->shm->graphics_request.get_msg(buf)) {
|
|
|
|
app_client_shm->decode_graphics_msg(buf, m);
|
|
|
|
switch (m.mode) {
|
|
|
|
case MODE_HIDE_GRAPHICS:
|
2007-09-28 04:23:34 +00:00
|
|
|
if (ga_full.is_running()) {
|
|
|
|
ga_full.kill();
|
|
|
|
} else if (ga_win.is_running()) {
|
|
|
|
ga_win.kill();
|
|
|
|
}
|
2007-09-25 16:04:45 +00:00
|
|
|
break;
|
|
|
|
case MODE_WINDOW:
|
2007-09-28 04:23:34 +00:00
|
|
|
if (!ga_win.is_running()) ga_win.run(graphics_app_path);
|
2007-09-25 16:04:45 +00:00
|
|
|
break;
|
|
|
|
case MODE_FULLSCREEN:
|
2007-09-28 04:23:34 +00:00
|
|
|
if (!ga_full.is_running()) ga_full.run(graphics_app_path);
|
2007-09-25 16:04:45 +00:00
|
|
|
break;
|
|
|
|
case MODE_BLANKSCREEN:
|
|
|
|
// we can't actually blank the screen; just kill the app
|
|
|
|
//
|
2007-09-28 04:23:34 +00:00
|
|
|
if (ga_full.is_running()) {
|
|
|
|
ga_full.kill();
|
|
|
|
}
|
2007-09-25 16:04:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
app_client_shm->shm->graphics_reply.send_msg(
|
|
|
|
xml_graphics_modes[m.mode]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-09 17:32:02 +00:00
|
|
|
static void graphics_cleanup() {
|
2007-09-28 04:23:34 +00:00
|
|
|
if (!have_graphics_app) return;
|
|
|
|
if (ga_full.is_running()) ga_full.kill();
|
|
|
|
if (ga_win.is_running()) ga_win.kill();
|
|
|
|
}
|
|
|
|
|
2008-01-08 18:58:44 +00:00
|
|
|
// timer handler; runs in the timer thread
|
2007-06-08 07:55:27 +00:00
|
|
|
//
|
|
|
|
static void timer_handler() {
|
2007-09-25 16:04:45 +00:00
|
|
|
if (g_sleep) return;
|
2005-07-23 08:10:39 +00:00
|
|
|
interrupt_count++;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2009-03-23 17:26:20 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
2009-03-20 23:02:31 +00:00
|
|
|
if (in_critical_section) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%f: timer_handler(): in critical section\n", dtime()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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) {
|
2008-07-03 19:40:49 +00:00
|
|
|
if (options.check_heartbeat) {
|
|
|
|
handle_heartbeat_msg();
|
|
|
|
}
|
2004-08-03 09:50:24 +00:00
|
|
|
if (options.handle_trickle_downs) {
|
|
|
|
handle_trickle_down_msg();
|
|
|
|
}
|
2008-02-06 20:39:10 +00:00
|
|
|
if (in_critical_section==0 && options.handle_process_control) {
|
2007-03-15 19:08:22 +00:00
|
|
|
handle_process_control_msg();
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2007-09-25 16:04:45 +00:00
|
|
|
if (options.backwards_compatible_graphics) {
|
|
|
|
handle_graphics_messages();
|
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
|
|
|
|
2007-12-21 20:57:48 +00:00
|
|
|
if (interrupt_count % TIMERS_PER_SEC) return;
|
|
|
|
|
2009-03-23 17:26:20 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
2009-03-23 01:33:17 +00:00
|
|
|
fprintf(stderr, "%f: 1 sec elapsed\n", dtime());
|
2009-03-20 23:02:31 +00:00
|
|
|
#endif
|
|
|
|
|
2007-12-21 20:57:48 +00:00
|
|
|
// here it we're at a one-second boundary; do slow stuff
|
|
|
|
//
|
|
|
|
|
|
|
|
if (!ready_to_checkpoint) {
|
|
|
|
time_until_checkpoint -= 1;
|
|
|
|
if (time_until_checkpoint <= 0) {
|
|
|
|
ready_to_checkpoint = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2008-02-06 20:39:10 +00:00
|
|
|
if (in_critical_section==0 && options.check_heartbeat) {
|
2007-03-15 19:08:22 +00:00
|
|
|
if (heartbeat_giveup_time < interrupt_count) {
|
2004-10-12 20:56:44 +00:00
|
|
|
fprintf(stderr,
|
2009-03-20 23:02:31 +00:00
|
|
|
"%f: No heartbeat from core client for 30 sec - exiting\n",
|
|
|
|
dtime()
|
2004-10-12 20:56:44 +00:00
|
|
|
);
|
2004-08-03 09:50:24 +00:00
|
|
|
if (options.direct_process_action) {
|
2008-01-13 00:12:14 +00:00
|
|
|
exit_from_timer_thread(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 18:33:28 +00:00
|
|
|
// don't bother reporting CPU time etc. if we're suspended
|
|
|
|
//
|
|
|
|
if (options.send_status_msgs && !boinc_status.suspended) {
|
2008-06-12 18:35:19 +00:00
|
|
|
double cur_cpu = boinc_worker_thread_cpu_time();
|
|
|
|
last_wu_cpu_time = cur_cpu + initial_wu_cpu_time;
|
|
|
|
update_app_progress(last_wu_cpu_time, last_checkpoint_cpu_time);
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2008-02-21 10:50:59 +00:00
|
|
|
|
2008-06-12 18:35:19 +00:00
|
|
|
// If running under V5 client, notify the client if the graphics app exits
|
|
|
|
// (e.g., if user clicked in the graphics window's close box.)
|
|
|
|
//
|
2008-02-21 10:50:59 +00:00
|
|
|
if (ga_win.pid) {
|
|
|
|
if (! ga_win.is_running()) {
|
|
|
|
app_client_shm->shm->graphics_reply.send_msg(
|
|
|
|
xml_graphics_modes[MODE_HIDE_GRAPHICS]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2007-06-08 07:55:27 +00:00
|
|
|
if (timer_callback) {
|
|
|
|
timer_callback();
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2008-03-29 22:07:50 +00:00
|
|
|
DWORD WINAPI timer_thread(void *) {
|
2007-08-10 01:51:14 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
while (1) {
|
2008-03-29 22:07:50 +00:00
|
|
|
Sleep((int)(TIMER_PERIOD*1000));
|
2007-06-08 07:55:27 +00:00
|
|
|
timer_handler();
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// poor man's CPU time accounting for Win9x
|
|
|
|
//
|
|
|
|
if (!boinc_status.suspended) {
|
|
|
|
nrunning_ticks++;
|
2006-05-15 17:14:15 +00:00
|
|
|
}
|
2006-05-15 21:01:35 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2006-05-15 17:14:15 +00:00
|
|
|
|
2008-01-09 17:32:02 +00:00
|
|
|
static 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);
|
2007-06-08 07:55:27 +00:00
|
|
|
timer_handler();
|
2005-05-22 00:13:16 +00:00
|
|
|
}
|
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
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// This SIGALRM handler gets handled only by the worker thread.
|
|
|
|
// It gets CPU time and implements sleeping.
|
|
|
|
// It must call only signal-safe functions, and must not do FP math
|
|
|
|
//
|
2008-01-09 17:32:02 +00:00
|
|
|
static void worker_signal_handler(int) {
|
2008-01-08 18:58:44 +00:00
|
|
|
#ifndef GETRUSAGE_IN_TIMER_THREAD
|
2008-01-07 23:10:17 +00:00
|
|
|
getrusage(RUSAGE_SELF, &worker_thread_ru);
|
|
|
|
#endif
|
2008-01-13 00:12:14 +00:00
|
|
|
if (worker_thread_exit_flag) {
|
|
|
|
boinc_exit(worker_thread_exit_status);
|
|
|
|
}
|
2007-03-15 19:08:22 +00:00
|
|
|
if (options.direct_process_action) {
|
2008-02-06 20:39:10 +00:00
|
|
|
while (boinc_status.suspended && in_critical_section==0) {
|
2007-03-15 19:08:22 +00:00
|
|
|
sleep(1); // don't use boinc_sleep() because it does FP math
|
2005-08-11 18:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2005-05-22 23:12:09 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
|
2007-11-29 19:48:04 +00:00
|
|
|
// Called from the worker thread; create the timer thread
|
2005-01-28 01:58:11 +00:00
|
|
|
//
|
2008-01-13 04:16:58 +00:00
|
|
|
int start_timer_thread() {
|
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
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// get the worker thread handle
|
2006-06-12 20:09:19 +00:00
|
|
|
//
|
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
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// Create the timer thread
|
2006-06-12 20:09:19 +00:00
|
|
|
//
|
2008-03-29 22:07:50 +00:00
|
|
|
DWORD id;
|
|
|
|
if (!CreateThread(NULL, 0, timer_thread, 0, 0, &id)) {
|
|
|
|
fprintf(stderr, "start_timer_thread(): CreateThread() failed, errno %d\n", errno);
|
2008-01-09 17:32:02 +00:00
|
|
|
return errno;
|
2006-05-11 18:28:35 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 00:49:50 +00:00
|
|
|
if (!options.normal_thread_priority) {
|
|
|
|
// lower our (worker thread) priority
|
|
|
|
//
|
|
|
|
SetThreadPriority(worker_thread_handle, THREAD_PRIORITY_IDLE);
|
|
|
|
}
|
2005-05-22 00:13:16 +00:00
|
|
|
#else
|
2007-06-13 19:09:46 +00:00
|
|
|
pthread_attr_t thread_attrs;
|
|
|
|
pthread_attr_init(&thread_attrs);
|
|
|
|
pthread_attr_setstacksize(&thread_attrs, 16384);
|
|
|
|
retval = pthread_create(&timer_thread_handle, &thread_attrs, timer_thread, NULL);
|
2005-05-22 00:13:16 +00:00
|
|
|
if (retval) {
|
2008-01-09 17:32:02 +00:00
|
|
|
fprintf(stderr, "start_timer_thread(): pthread_create(): %d", retval);
|
2006-06-12 20:09:19 +00:00
|
|
|
return retval;
|
2005-05-22 00:13:16 +00:00
|
|
|
}
|
2008-01-09 17:32:02 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-11 18:18:03 +00:00
|
|
|
|
2008-01-09 17:32:02 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
// set up a periodic SIGALRM, to be handled by the worker thread
|
|
|
|
//
|
|
|
|
static int start_worker_signals() {
|
|
|
|
int retval;
|
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) {
|
2008-01-09 17:32:02 +00:00
|
|
|
perror("boinc start_timer_thread() sigaction");
|
2005-07-05 10:23:20 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2007-12-21 20:57:48 +00:00
|
|
|
value.it_value.tv_sec = 0;
|
|
|
|
value.it_value.tv_usec = (int)(TIMER_PERIOD*1e6);
|
2005-07-05 10:23:20 +00:00
|
|
|
value.it_interval = value.it_value;
|
|
|
|
retval = setitimer(ITIMER_REAL, &value, NULL);
|
|
|
|
if (retval) {
|
2008-01-09 17:32:02 +00:00
|
|
|
perror("boinc start_timer_thread() setitimer");
|
|
|
|
return retval;
|
2005-07-05 10:23:20 +00:00
|
|
|
}
|
2008-01-09 17:32:02 +00:00
|
|
|
return 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
2008-01-09 17:32:02 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
|
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
|
|
|
|
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) {
|
2008-02-06 20:39:10 +00:00
|
|
|
boinc_begin_critical_section();
|
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;
|
2007-06-08 07:55:27 +00:00
|
|
|
cur_cpu = boinc_worker_thread_cpu_time();
|
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-21 19:57:44 +00:00
|
|
|
time_until_checkpoint = (int)aid.checkpoint_period;
|
2008-02-06 20:39:10 +00:00
|
|
|
boinc_end_critical_section();
|
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() {
|
2008-02-06 20:39:10 +00:00
|
|
|
in_critical_section++;
|
2005-08-12 21:24:07 +00:00
|
|
|
}
|
2007-03-15 19:08:22 +00:00
|
|
|
|
2005-08-12 21:24:07 +00:00
|
|
|
void boinc_end_critical_section() {
|
2008-02-06 20:39:10 +00:00
|
|
|
in_critical_section--;
|
|
|
|
if (in_critical_section < 0) {
|
|
|
|
in_critical_section = 0; // just in case
|
|
|
|
}
|
2005-08-12 21:24:07 +00:00
|
|
|
}
|
|
|
|
|
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-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)) {
|
|
|
|
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 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
|
|
|
|
//
|
2008-01-09 17:32:02 +00:00
|
|
|
static void block_sigalrm() {
|
2005-08-11 21:26:02 +00:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGALRM);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
void boinc_register_timer_callback(FUNC_PTR p) {
|
|
|
|
timer_callback = p;
|
|
|
|
}
|
|
|
|
|
2007-11-30 04:14:48 +00:00
|
|
|
double boinc_get_fraction_done() {
|
|
|
|
return fraction_done;
|
|
|
|
}
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_0fa0410386 = "$Id$";
|