2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-05-29 20:24:05 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-05-29 20:24:05 +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.
|
2006-05-29 20:24:05 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-05-29 20:24:05 +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.
|
|
|
|
//
|
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/>.
|
2006-05-29 20:24:05 +00:00
|
|
|
|
|
|
|
// wrapper.C
|
2006-07-17 16:55:47 +00:00
|
|
|
// wrapper program - lets you use non-BOINC apps with BOINC
|
2006-05-29 20:24:05 +00:00
|
|
|
//
|
|
|
|
// Handles:
|
|
|
|
// - suspend/resume/quit/abort
|
|
|
|
// - reporting CPU time
|
|
|
|
// - loss of heartbeat from core client
|
|
|
|
// - checkpointing
|
2008-12-08 04:47:57 +00:00
|
|
|
// (at the level of task; or potentially within task)
|
2006-05-29 20:24:05 +00:00
|
|
|
//
|
2011-01-02 03:07:14 +00:00
|
|
|
// See http://boinc.berkeley.edu/trac/wiki/WrapperApp for details
|
2007-04-26 20:10:54 +00:00
|
|
|
// Contributor: Andrew J. Younge (ajy4490@umiacs.umd.edu)
|
2006-05-29 20:24:05 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#ifdef _WIN32
|
2006-06-13 23:12:36 +00:00
|
|
|
#include "boinc_win.h"
|
2008-03-06 18:34:32 +00:00
|
|
|
#include "win_util.h"
|
2006-05-29 20:24:05 +00:00
|
|
|
#else
|
|
|
|
#include <sys/wait.h>
|
2008-06-17 20:39:59 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2006-05-29 20:24:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "boinc_api.h"
|
2006-06-14 20:14:20 +00:00
|
|
|
#include "diagnostics.h"
|
2011-11-29 15:06:08 +00:00
|
|
|
#include "error_numbers.h"
|
2006-05-29 20:24:05 +00:00
|
|
|
#include "filesys.h"
|
|
|
|
#include "parse.h"
|
2011-11-29 15:06:08 +00:00
|
|
|
#include "proc_control.h"
|
|
|
|
#include "procinfo.h"
|
2007-02-21 20:04:14 +00:00
|
|
|
#include "str_util.h"
|
2009-08-20 05:11:42 +00:00
|
|
|
#include "str_replace.h"
|
2006-05-29 20:24:05 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
#define JOB_FILENAME "job.xml"
|
2009-09-24 20:51:32 +00:00
|
|
|
#define CHECKPOINT_FILENAME "wrapper_checkpoint.txt"
|
2007-05-28 16:31:39 +00:00
|
|
|
|
2007-11-27 19:15:32 +00:00
|
|
|
#define POLL_PERIOD 1.0
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
2011-03-21 09:30:29 +00:00
|
|
|
int nthreads = 1;
|
2006-05-29 20:24:05 +00:00
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
struct TASK {
|
|
|
|
string application;
|
2011-02-20 04:01:04 +00:00
|
|
|
string exec_dir;
|
2011-03-21 09:30:29 +00:00
|
|
|
// optional execution directory;
|
|
|
|
// macro-substituted for $PROJECT_DIR and $NTHREADS
|
|
|
|
vector<string> vsetenv;
|
|
|
|
// vector of strings for environment variables
|
|
|
|
// macro-substituted
|
2006-07-17 16:55:47 +00:00
|
|
|
string stdin_filename;
|
|
|
|
string stdout_filename;
|
2007-04-26 20:10:54 +00:00
|
|
|
string stderr_filename;
|
2008-06-17 20:39:59 +00:00
|
|
|
string checkpoint_filename;
|
|
|
|
// name of task's checkpoint file, if any
|
2009-09-24 20:51:32 +00:00
|
|
|
string fraction_done_filename;
|
|
|
|
// name of file where app will write its fraction done
|
2006-07-17 16:55:47 +00:00
|
|
|
string command_line;
|
2011-03-21 09:30:29 +00:00
|
|
|
// macro-substituted
|
2008-06-17 20:39:59 +00:00
|
|
|
double weight;
|
|
|
|
// contribution of this task to overall fraction done
|
2011-02-24 21:39:07 +00:00
|
|
|
bool is_daemon;
|
2011-03-14 17:28:52 +00:00
|
|
|
bool append_cmdline_args;
|
2011-04-14 22:25:38 +00:00
|
|
|
bool multi_process;
|
2011-02-24 21:39:07 +00:00
|
|
|
|
|
|
|
// dynamic stuff follows
|
2012-02-29 20:58:45 +00:00
|
|
|
double current_cpu_time;
|
2007-05-28 16:31:39 +00:00
|
|
|
double final_cpu_time;
|
|
|
|
double starting_cpu;
|
2007-06-28 22:49:48 +00:00
|
|
|
// how much CPU time was used by tasks before this in the job file
|
2007-11-27 19:15:32 +00:00
|
|
|
bool suspended;
|
|
|
|
double wall_cpu_time;
|
2008-03-09 18:18:21 +00:00
|
|
|
// for estimating CPU time on Win98/ME and Mac
|
|
|
|
#ifdef _WIN32
|
2006-07-17 16:55:47 +00:00
|
|
|
HANDLE pid_handle;
|
2008-03-06 18:31:33 +00:00
|
|
|
DWORD pid;
|
2006-07-17 16:55:47 +00:00
|
|
|
HANDLE thread_handle;
|
2008-06-17 20:44:17 +00:00
|
|
|
struct _stat last_stat; // mod time of checkpoint file
|
2006-05-29 20:24:05 +00:00
|
|
|
#else
|
2006-07-17 16:55:47 +00:00
|
|
|
int pid;
|
2008-06-17 20:39:59 +00:00
|
|
|
struct stat last_stat;
|
2006-05-29 20:24:05 +00:00
|
|
|
#endif
|
2008-06-17 20:39:59 +00:00
|
|
|
bool stat_first;
|
2011-02-20 23:46:28 +00:00
|
|
|
|
2006-09-10 02:21:48 +00:00
|
|
|
int parse(XML_PARSER&);
|
2006-07-17 16:55:47 +00:00
|
|
|
bool poll(int& status);
|
2007-04-26 20:10:54 +00:00
|
|
|
int run(int argc, char** argv);
|
2006-07-17 16:55:47 +00:00
|
|
|
void kill();
|
|
|
|
void stop();
|
|
|
|
void resume();
|
2011-02-20 04:01:04 +00:00
|
|
|
double cpu_time();
|
2008-06-17 20:39:59 +00:00
|
|
|
inline bool has_checkpointed() {
|
|
|
|
bool changed = false;
|
|
|
|
if (checkpoint_filename.size() == 0) return false;
|
|
|
|
struct stat new_stat;
|
|
|
|
int retval = stat(checkpoint_filename.c_str(), &new_stat);
|
|
|
|
if (retval) return false;
|
|
|
|
if (!stat_first && new_stat.st_mtime != last_stat.st_mtime) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
stat_first = false;
|
|
|
|
last_stat.st_mtime = new_stat.st_mtime;
|
|
|
|
return changed;
|
|
|
|
}
|
2009-09-24 20:51:32 +00:00
|
|
|
inline double fraction_done() {
|
|
|
|
if (fraction_done_filename.size() == 0) return 0;
|
|
|
|
FILE* f = fopen(fraction_done_filename.c_str(), "r");
|
|
|
|
if (!f) return 0;
|
|
|
|
double frac;
|
2009-10-02 18:48:50 +00:00
|
|
|
int n = fscanf(f, "%lf", &frac);
|
2009-09-24 20:51:32 +00:00
|
|
|
fclose(f);
|
|
|
|
if (n != 1) return 0;
|
|
|
|
if (frac < 0) return 0;
|
|
|
|
if (frac > 1) return 1;
|
|
|
|
return frac;
|
|
|
|
}
|
2011-02-20 23:46:28 +00:00
|
|
|
|
2011-02-21 06:40:22 +00:00
|
|
|
#ifdef _WIN32
|
2011-02-22 23:11:34 +00:00
|
|
|
// Windows uses a "null-terminated sequence of null-terminated strings"
|
|
|
|
// to represent env vars.
|
|
|
|
// I guess arg/argv didn't cut it for them.
|
|
|
|
//
|
2011-02-21 06:40:22 +00:00
|
|
|
void set_up_env_vars(char** env_vars, const int nvars) {
|
|
|
|
int bufsize = 0;
|
|
|
|
int len = 0;
|
|
|
|
for (int j = 0; j < nvars; j++) {
|
|
|
|
bufsize += (1 + vsetenv[j].length());
|
|
|
|
}
|
|
|
|
bufsize++; // add a final byte for array null ptr
|
|
|
|
*env_vars = new char[bufsize];
|
2011-02-22 23:11:34 +00:00
|
|
|
memset(*env_vars, 0, sizeof(char) * bufsize);
|
2011-02-21 06:40:22 +00:00
|
|
|
char* p = *env_vars;
|
|
|
|
// copy each env string to a buffer for the process
|
|
|
|
for (vector<string>::iterator it = vsetenv.begin();
|
|
|
|
it != vsetenv.end() && len < bufsize-1;
|
|
|
|
it++
|
|
|
|
) {
|
|
|
|
strncpy(p, it->c_str(), it->length());
|
|
|
|
len = strlen(p);
|
|
|
|
p += len + 1; // move pointer ahead
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2011-02-20 23:46:28 +00:00
|
|
|
void set_up_env_vars(char*** env_vars, const int nvars) {
|
|
|
|
*env_vars = new char*[nvars+1];
|
|
|
|
// need one more than the # of vars, for a NULL ptr at the end
|
|
|
|
memset(*env_vars, 0x00, sizeof(char*) * (nvars+1));
|
|
|
|
// get all environment vars for this task
|
|
|
|
for (int i = 0; i < nvars; i++) {
|
2011-09-01 22:48:29 +00:00
|
|
|
(*env_vars)[i] = const_cast<char*>(vsetenv[i].c_str());
|
2011-02-20 23:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-21 06:40:22 +00:00
|
|
|
#endif
|
2006-07-17 16:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
vector<TASK> tasks;
|
2011-02-24 21:39:07 +00:00
|
|
|
vector<TASK> daemons;
|
2008-06-18 18:55:06 +00:00
|
|
|
APP_INIT_DATA aid;
|
2006-07-17 16:55:47 +00:00
|
|
|
|
2011-03-21 09:30:29 +00:00
|
|
|
// replace s1 with s2
|
2011-02-20 23:46:28 +00:00
|
|
|
//
|
2011-03-21 09:30:29 +00:00
|
|
|
void str_replace_all(char* buf, const char* s1, const char* s2) {
|
|
|
|
char buf2[64000];
|
2011-02-20 23:46:28 +00:00
|
|
|
while (1) {
|
2011-03-21 09:30:29 +00:00
|
|
|
char* p = strstr(buf, s1);
|
2011-02-20 23:46:28 +00:00
|
|
|
if (!p) break;
|
2011-03-21 09:30:29 +00:00
|
|
|
strcpy(buf2, p+strlen(s1));
|
|
|
|
strcpy(p, s2);
|
2011-02-20 23:46:28 +00:00
|
|
|
strcat(p, buf2);
|
|
|
|
}
|
2011-03-21 09:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// macro-substitute strings from job.xml
|
|
|
|
// $PROJECT_DIR -> project directory
|
|
|
|
// $NTHREADS --> --nthreads arg if present, else 1
|
|
|
|
//
|
|
|
|
void macro_substitute(char* buf) {
|
|
|
|
const char* pd = strlen(aid.project_dir)?aid.project_dir:".";
|
|
|
|
str_replace_all(buf, "$PROJECT_DIR", pd);
|
|
|
|
char nt[256];
|
|
|
|
sprintf(nt, "%d", nthreads);
|
|
|
|
str_replace_all(buf, "$NTHREADS", nt);
|
2011-02-20 23:46:28 +00:00
|
|
|
}
|
|
|
|
|
2006-09-10 02:21:48 +00:00
|
|
|
int TASK::parse(XML_PARSER& xp) {
|
2011-09-01 22:48:29 +00:00
|
|
|
char buf[8192];
|
2006-09-10 02:21:48 +00:00
|
|
|
|
2008-06-17 20:39:59 +00:00
|
|
|
weight = 1;
|
2012-02-29 20:58:45 +00:00
|
|
|
current_cpu_time = 0;
|
2007-05-28 16:31:39 +00:00
|
|
|
final_cpu_time = 0;
|
2008-06-17 20:39:59 +00:00
|
|
|
stat_first = true;
|
2011-02-24 21:39:07 +00:00
|
|
|
pid = 0;
|
2011-02-24 23:21:55 +00:00
|
|
|
is_daemon = false;
|
2011-04-14 22:25:38 +00:00
|
|
|
multi_process = false;
|
2011-03-14 17:28:52 +00:00
|
|
|
append_cmdline_args = false;
|
2011-02-20 23:46:28 +00:00
|
|
|
|
2011-09-01 22:48:29 +00:00
|
|
|
while (!xp.get_tag()) {
|
|
|
|
if (!xp.is_tag) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s TASK::parse(): unexpected text %s\n",
|
2011-09-01 22:48:29 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), xp.parsed_tag
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2006-07-17 16:55:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-09-01 22:48:29 +00:00
|
|
|
if (xp.match_tag("/task")) {
|
2006-09-10 02:21:48 +00:00
|
|
|
return 0;
|
2006-07-17 16:55:47 +00:00
|
|
|
}
|
2011-09-01 22:48:29 +00:00
|
|
|
else if (xp.parse_string("application", application)) continue;
|
|
|
|
else if (xp.parse_str("exec_dir", buf, sizeof(buf))) {
|
2011-03-21 09:30:29 +00:00
|
|
|
macro_substitute(buf);
|
2011-02-20 04:01:04 +00:00
|
|
|
exec_dir = buf;
|
2011-02-20 23:46:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-09-01 22:48:29 +00:00
|
|
|
else if (xp.parse_str("setenv", buf, sizeof(buf))) {
|
2011-03-21 09:30:29 +00:00
|
|
|
macro_substitute(buf);
|
2011-02-20 23:46:28 +00:00
|
|
|
vsetenv.push_back(buf);
|
|
|
|
continue;
|
2011-02-20 04:01:04 +00:00
|
|
|
}
|
2011-09-01 22:48:29 +00:00
|
|
|
else if (xp.parse_string("stdin_filename", stdin_filename)) continue;
|
|
|
|
else if (xp.parse_string("stdout_filename", stdout_filename)) continue;
|
|
|
|
else if (xp.parse_string("stderr_filename", stderr_filename)) continue;
|
|
|
|
else if (xp.parse_str("command_line", buf, sizeof(buf))) {
|
2011-03-21 09:30:29 +00:00
|
|
|
macro_substitute(buf);
|
2008-06-18 18:55:06 +00:00
|
|
|
command_line = buf;
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-01 22:48:29 +00:00
|
|
|
else if (xp.parse_string("checkpoint_filename", checkpoint_filename)) continue;
|
|
|
|
else if (xp.parse_string("fraction_done_filename", fraction_done_filename)) continue;
|
|
|
|
else if (xp.parse_double("weight", weight)) continue;
|
|
|
|
else if (xp.parse_bool("daemon", is_daemon)) continue;
|
|
|
|
else if (xp.parse_bool("multi_process", multi_process)) continue;
|
|
|
|
else if (xp.parse_bool("append_cmdline_args", append_cmdline_args)) continue;
|
2006-07-17 16:55:47 +00:00
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
int parse_job_file() {
|
2006-09-10 02:21:48 +00:00
|
|
|
MIOFILE mf;
|
2011-09-01 22:48:29 +00:00
|
|
|
char buf[256], buf2[256];
|
2006-09-10 02:21:48 +00:00
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
boinc_resolve_filename(JOB_FILENAME, buf, 1024);
|
2006-05-29 20:24:05 +00:00
|
|
|
FILE* f = boinc_fopen(buf, "r");
|
2007-05-28 16:31:39 +00:00
|
|
|
if (!f) {
|
2010-09-15 23:03:30 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s can't open job file %s\n",
|
|
|
|
boinc_msg_prefix(buf2, sizeof(buf2)), buf
|
|
|
|
);
|
2007-05-28 16:31:39 +00:00
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2006-09-10 02:21:48 +00:00
|
|
|
mf.init_file(f);
|
|
|
|
XML_PARSER xp(&mf);
|
|
|
|
|
|
|
|
if (!xp.parse_start("job_desc")) return ERR_XML_PARSE;
|
2011-09-01 22:48:29 +00:00
|
|
|
while (!xp.get_tag()) {
|
|
|
|
if (!xp.is_tag) {
|
2010-09-15 23:03:30 +00:00
|
|
|
fprintf(stderr,
|
2011-03-14 17:28:52 +00:00
|
|
|
"%s unexpected text in job.xml: %s\n",
|
2011-09-01 22:48:29 +00:00
|
|
|
boinc_msg_prefix(buf2, sizeof(buf2)), xp.parsed_tag
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2006-09-10 02:21:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-09-01 22:48:29 +00:00
|
|
|
if (xp.match_tag("/job_desc")) {
|
2008-06-19 22:31:10 +00:00
|
|
|
fclose(f);
|
2006-05-29 20:24:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-09-01 22:48:29 +00:00
|
|
|
if (xp.match_tag("task")) {
|
2006-07-17 16:55:47 +00:00
|
|
|
TASK task;
|
2006-09-10 02:21:48 +00:00
|
|
|
int retval = task.parse(xp);
|
2006-07-17 16:55:47 +00:00
|
|
|
if (!retval) {
|
2011-02-24 21:39:07 +00:00
|
|
|
if (task.is_daemon) {
|
|
|
|
daemons.push_back(task);
|
|
|
|
} else {
|
|
|
|
tasks.push_back(task);
|
|
|
|
}
|
2006-07-17 16:55:47 +00:00
|
|
|
}
|
2011-03-14 17:28:52 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s unexpected tag in job.xml: %s\n",
|
2011-09-01 22:48:29 +00:00
|
|
|
boinc_msg_prefix(buf2, sizeof(buf2)), xp.parsed_tag
|
2011-03-14 17:28:52 +00:00
|
|
|
);
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-19 22:31:10 +00:00
|
|
|
fclose(f);
|
2006-05-29 20:24:05 +00:00
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
|
2011-02-24 21:39:07 +00:00
|
|
|
int start_daemons(int argc, char** argv) {
|
|
|
|
for (unsigned int i=0; i<daemons.size(); i++) {
|
|
|
|
TASK& task = daemons[i];
|
|
|
|
int retval = task.run(argc, argv);
|
|
|
|
if (retval) return retval;
|
|
|
|
}
|
2011-02-24 21:55:17 +00:00
|
|
|
return 0;
|
2011-02-24 21:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void kill_daemons() {
|
|
|
|
vector<int> daemon_pids;
|
|
|
|
for (unsigned int i=0; i<daemons.size(); i++) {
|
|
|
|
TASK& task = daemons[i];
|
|
|
|
if (task.pid) {
|
|
|
|
daemon_pids.push_back(task.pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kill_all(daemon_pids);
|
|
|
|
}
|
|
|
|
|
2006-11-25 22:13:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// CreateProcess() takes HANDLEs for the stdin/stdout.
|
|
|
|
// We need to use CreateFile() to get them. Ugh.
|
|
|
|
//
|
|
|
|
HANDLE win_fopen(const char* path, const char* mode) {
|
2011-02-20 04:01:04 +00:00
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.bInheritHandle = TRUE;
|
2006-11-25 22:13:27 +00:00
|
|
|
|
2011-02-20 04:01:04 +00:00
|
|
|
if (!strcmp(mode, "r")) {
|
|
|
|
return CreateFile(
|
|
|
|
path,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
&sa,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
0, 0
|
|
|
|
);
|
|
|
|
} else if (!strcmp(mode, "w")) {
|
|
|
|
return CreateFile(
|
|
|
|
path,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
FILE_SHARE_WRITE,
|
|
|
|
&sa,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
0, 0
|
|
|
|
);
|
|
|
|
} else if (!strcmp(mode, "a")) {
|
|
|
|
HANDLE hAppend = CreateFile(
|
|
|
|
path,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
FILE_SHARE_WRITE,
|
|
|
|
&sa,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
0, 0
|
|
|
|
);
|
2007-09-13 07:30:27 +00:00
|
|
|
SetFilePointer(hAppend, 0, NULL, FILE_END);
|
2007-09-20 20:32:33 +00:00
|
|
|
return hAppend;
|
2011-02-20 04:01:04 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-25 22:13:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-18 21:34:43 +00:00
|
|
|
void slash_to_backslash(char* p) {
|
|
|
|
while (1) {
|
|
|
|
char* q = strchr(p, '/');
|
|
|
|
if (!q) break;
|
|
|
|
*q = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-26 20:10:54 +00:00
|
|
|
int TASK::run(int argct, char** argvt) {
|
2008-06-18 18:55:06 +00:00
|
|
|
string stdout_path, stdin_path, stderr_path;
|
|
|
|
char app_path[1024], buf[256];
|
|
|
|
|
2009-09-24 20:51:32 +00:00
|
|
|
if (fraction_done_filename.size()) {
|
|
|
|
boinc_delete_file(fraction_done_filename.c_str());
|
|
|
|
}
|
|
|
|
|
2008-06-18 18:55:06 +00:00
|
|
|
strcpy(buf, application.c_str());
|
|
|
|
char* p = strstr(buf, "$PROJECT_DIR");
|
|
|
|
if (p) {
|
|
|
|
p += strlen("$PROJECT_DIR");
|
|
|
|
sprintf(app_path, "%s%s", aid.project_dir, p);
|
|
|
|
} else {
|
2008-06-18 20:12:55 +00:00
|
|
|
boinc_resolve_filename(buf, app_path, sizeof(app_path));
|
2008-06-18 18:55:06 +00:00
|
|
|
}
|
2006-07-17 16:55:47 +00:00
|
|
|
|
2011-03-14 17:28:52 +00:00
|
|
|
// Optionally append wrapper's command-line arguments
|
|
|
|
// to those in the job file.
|
2007-04-26 20:10:54 +00:00
|
|
|
//
|
2011-03-14 17:28:52 +00:00
|
|
|
if (append_cmdline_args) {
|
|
|
|
for (int i=1; i<argct; i++){
|
|
|
|
command_line += string(" ");
|
|
|
|
command_line += argvt[i];
|
|
|
|
}
|
2008-05-16 22:12:05 +00:00
|
|
|
}
|
|
|
|
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s wrapper: running %s (%s)\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), app_path, command_line.c_str()
|
2008-05-16 22:12:05 +00:00
|
|
|
);
|
2006-11-25 22:13:27 +00:00
|
|
|
|
2006-08-12 22:34:34 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-29 20:24:05 +00:00
|
|
|
PROCESS_INFORMATION process_info;
|
|
|
|
STARTUPINFO startup_info;
|
2006-08-17 22:09:20 +00:00
|
|
|
string command;
|
|
|
|
|
2008-06-18 21:34:43 +00:00
|
|
|
slash_to_backslash(app_path);
|
2006-05-29 20:24:05 +00:00
|
|
|
memset(&process_info, 0, sizeof(process_info));
|
|
|
|
memset(&startup_info, 0, sizeof(startup_info));
|
2008-06-19 22:31:10 +00:00
|
|
|
command = string("\"") + app_path + string("\" ") + command_line;
|
2006-06-14 20:14:20 +00:00
|
|
|
|
2006-08-12 22:34:34 +00:00
|
|
|
// pass std handles to app
|
2006-06-14 20:14:20 +00:00
|
|
|
//
|
|
|
|
startup_info.dwFlags = STARTF_USESTDHANDLES;
|
2011-02-20 04:01:04 +00:00
|
|
|
if (stdout_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stdout_filename.c_str(), stdout_path);
|
|
|
|
startup_info.hStdOutput = win_fopen(stdout_path.c_str(), "a");
|
|
|
|
}
|
|
|
|
if (stdin_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stdin_filename.c_str(), stdin_path);
|
|
|
|
startup_info.hStdInput = win_fopen(stdin_path.c_str(), "r");
|
|
|
|
}
|
2007-04-26 20:10:54 +00:00
|
|
|
if (stderr_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stderr_filename.c_str(), stderr_path);
|
2008-10-03 05:13:25 +00:00
|
|
|
startup_info.hStdError = win_fopen(stderr_path.c_str(), "a");
|
2007-04-26 20:10:54 +00:00
|
|
|
} else {
|
|
|
|
startup_info.hStdError = win_fopen(STDERR_FILE, "a");
|
|
|
|
}
|
2008-05-16 22:12:05 +00:00
|
|
|
|
2011-02-20 23:46:28 +00:00
|
|
|
// setup environment vars if needed
|
|
|
|
//
|
|
|
|
int nvars = vsetenv.size();
|
2011-02-21 06:40:22 +00:00
|
|
|
char* env_vars = NULL;
|
2011-02-20 23:46:28 +00:00
|
|
|
if (nvars > 0) {
|
|
|
|
set_up_env_vars(&env_vars, nvars);
|
|
|
|
}
|
|
|
|
|
2011-11-03 19:36:28 +00:00
|
|
|
BOOL success;
|
|
|
|
if (ends_with((string)app_path, ".bat")) {
|
|
|
|
char cmd[1024];
|
|
|
|
sprintf(cmd, "cmd.exe /c %s", command.c_str());
|
|
|
|
success = CreateProcess(
|
|
|
|
"cmd.exe",
|
|
|
|
(LPSTR)cmd,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
TRUE, // bInheritHandles
|
|
|
|
CREATE_NO_WINDOW|IDLE_PRIORITY_CLASS,
|
|
|
|
(LPVOID) env_vars,
|
|
|
|
exec_dir.empty()?NULL:exec_dir.c_str(),
|
|
|
|
&startup_info,
|
|
|
|
&process_info
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
success = CreateProcess(
|
|
|
|
app_path,
|
|
|
|
(LPSTR)command.c_str(),
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
TRUE, // bInheritHandles
|
|
|
|
CREATE_NO_WINDOW|IDLE_PRIORITY_CLASS,
|
|
|
|
(LPVOID) env_vars,
|
|
|
|
exec_dir.empty()?NULL:exec_dir.c_str(),
|
|
|
|
&startup_info,
|
|
|
|
&process_info
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!success) {
|
2010-02-18 19:37:25 +00:00
|
|
|
char error_msg[1024];
|
|
|
|
windows_error_string(error_msg, sizeof(error_msg));
|
|
|
|
fprintf(stderr, "can't run app: %s\n", error_msg);
|
2011-02-21 06:40:22 +00:00
|
|
|
if (env_vars) delete [] env_vars;
|
2006-05-29 20:24:05 +00:00
|
|
|
return ERR_EXEC;
|
|
|
|
}
|
2011-02-20 23:46:28 +00:00
|
|
|
if (env_vars) delete [] env_vars;
|
2006-05-29 20:54:28 +00:00
|
|
|
pid_handle = process_info.hProcess;
|
2008-03-06 18:31:33 +00:00
|
|
|
pid = process_info.dwProcessId;
|
2006-05-29 20:54:28 +00:00
|
|
|
thread_handle = process_info.hThread;
|
2007-06-13 16:42:36 +00:00
|
|
|
SetThreadPriority(thread_handle, THREAD_PRIORITY_IDLE);
|
2006-05-29 20:24:05 +00:00
|
|
|
#else
|
2011-11-29 15:06:08 +00:00
|
|
|
int retval;
|
2006-08-17 22:09:20 +00:00
|
|
|
char* argv[256];
|
2007-04-26 20:10:54 +00:00
|
|
|
char arglist[4096];
|
2011-02-20 04:01:04 +00:00
|
|
|
FILE* stdout_file;
|
|
|
|
FILE* stdin_file;
|
|
|
|
FILE* stderr_file;
|
2006-08-17 22:09:20 +00:00
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
2010-02-18 19:37:25 +00:00
|
|
|
perror("fork(): ");
|
|
|
|
return ERR_FORK;
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
if (pid == 0) {
|
2011-02-20 04:01:04 +00:00
|
|
|
// we're in the child process here
|
|
|
|
//
|
|
|
|
// open stdout, stdin if file names are given
|
|
|
|
// NOTE: if the application is restartable,
|
|
|
|
// we should deal with atomicity somehow
|
|
|
|
//
|
|
|
|
if (stdout_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stdout_filename.c_str(), stdout_path);
|
|
|
|
stdout_file = freopen(stdout_path.c_str(), "a", stdout);
|
2011-09-07 22:45:00 +00:00
|
|
|
if (!stdout_file) {
|
|
|
|
fprintf(stderr, "Can't open %s for stdout; exiting\n", stdout_path.c_str());
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2011-02-20 04:01:04 +00:00
|
|
|
}
|
|
|
|
if (stdin_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stdin_filename.c_str(), stdin_path);
|
|
|
|
stdin_file = freopen(stdin_path.c_str(), "r", stdin);
|
2011-09-07 22:45:00 +00:00
|
|
|
if (!stdin_file) {
|
|
|
|
fprintf(stderr, "Can't open %s for stdin; exiting\n", stdin_path.c_str());
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2011-02-20 04:01:04 +00:00
|
|
|
}
|
2007-04-26 20:10:54 +00:00
|
|
|
if (stderr_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stderr_filename.c_str(), stderr_path);
|
2008-10-03 05:13:25 +00:00
|
|
|
stderr_file = freopen(stderr_path.c_str(), "a", stderr);
|
2011-09-07 22:45:00 +00:00
|
|
|
if (!stderr_file) {
|
|
|
|
fprintf(stderr, "Can't open %s for stderr; exiting\n", stderr_path.c_str());
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
2007-04-26 20:10:54 +00:00
|
|
|
}
|
|
|
|
|
2011-02-20 04:01:04 +00:00
|
|
|
// construct argv
|
2007-04-26 20:10:54 +00:00
|
|
|
// TODO: use malloc instead of stack var
|
2006-08-17 22:09:20 +00:00
|
|
|
//
|
2008-06-18 18:55:06 +00:00
|
|
|
argv[0] = app_path;
|
2007-04-26 20:10:54 +00:00
|
|
|
strlcpy(arglist, command_line.c_str(), sizeof(arglist));
|
2011-11-29 15:06:08 +00:00
|
|
|
parse_command_line(arglist, argv+1);
|
2007-06-13 16:42:36 +00:00
|
|
|
setpriority(PRIO_PROCESS, 0, PROCESS_IDLE_PRIORITY);
|
2011-02-20 04:01:04 +00:00
|
|
|
if (!exec_dir.empty()) {
|
2011-06-12 20:58:43 +00:00
|
|
|
retval = chdir(exec_dir.c_str());
|
|
|
|
if (!retval) {
|
|
|
|
fprintf(stderr, "chdir() to %s failed\n", exec_dir.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-02-20 04:01:04 +00:00
|
|
|
}
|
2011-02-20 23:46:28 +00:00
|
|
|
|
|
|
|
// setup environment variables (if any)
|
|
|
|
//
|
|
|
|
const int nvars = vsetenv.size();
|
|
|
|
char** env_vars = NULL;
|
|
|
|
if (nvars > 0) {
|
|
|
|
set_up_env_vars(&env_vars, nvars);
|
|
|
|
retval = execve(app_path, argv, env_vars);
|
|
|
|
} else {
|
|
|
|
retval = execv(app_path, argv);
|
|
|
|
}
|
2010-02-18 19:37:25 +00:00
|
|
|
perror("execv() failed: ");
|
2006-05-29 20:24:05 +00:00
|
|
|
exit(ERR_EXEC);
|
2011-02-20 23:46:28 +00:00
|
|
|
} // pid = 0 i.e. child proc of the fork
|
2006-05-29 20:24:05 +00:00
|
|
|
#endif
|
2008-03-09 18:18:21 +00:00
|
|
|
wall_cpu_time = 0;
|
2008-03-06 18:31:33 +00:00
|
|
|
suspended = false;
|
2006-05-29 20:24:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
bool TASK::poll(int& status) {
|
2008-03-09 18:18:21 +00:00
|
|
|
if (!suspended) wall_cpu_time += POLL_PERIOD;
|
2006-05-29 20:24:05 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
unsigned long exit_code;
|
|
|
|
if (GetExitCodeProcess(pid_handle, &exit_code)) {
|
|
|
|
if (exit_code != STILL_ACTIVE) {
|
|
|
|
status = exit_code;
|
2007-05-28 16:31:39 +00:00
|
|
|
final_cpu_time = cpu_time();
|
2012-02-29 20:58:45 +00:00
|
|
|
if (final_cpu_time < current_cpu_time) {
|
|
|
|
final_cpu_time = current_cpu_time;
|
|
|
|
}
|
2006-06-14 20:14:20 +00:00
|
|
|
return true;
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2011-06-12 20:58:43 +00:00
|
|
|
int wpid;
|
2007-05-28 16:31:39 +00:00
|
|
|
struct rusage ru;
|
|
|
|
|
|
|
|
wpid = wait4(pid, &status, WNOHANG, &ru);
|
2006-05-29 20:24:05 +00:00
|
|
|
if (wpid) {
|
2012-02-29 20:58:45 +00:00
|
|
|
getrusage(RUSAGE_CHILDREN, &ru);
|
2007-05-28 16:31:39 +00:00
|
|
|
final_cpu_time = (float)ru.ru_utime.tv_sec + ((float)ru.ru_utime.tv_usec)/1e+6;
|
2012-02-29 20:58:45 +00:00
|
|
|
if (final_cpu_time < current_cpu_time) {
|
|
|
|
final_cpu_time = current_cpu_time;
|
|
|
|
}
|
2006-05-29 20:24:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2006-05-29 20:54:28 +00:00
|
|
|
return false;
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
|
2011-02-24 21:39:07 +00:00
|
|
|
// kill this task (gracefully if possible) and any other subprocesses
|
|
|
|
//
|
2006-07-17 16:55:47 +00:00
|
|
|
void TASK::kill() {
|
2011-02-24 21:39:07 +00:00
|
|
|
kill_daemons();
|
2011-04-14 22:48:04 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
kill_descendants();
|
|
|
|
#else
|
2011-04-14 22:25:38 +00:00
|
|
|
kill_descendants(pid);
|
2011-04-14 22:48:04 +00:00
|
|
|
#endif
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
void TASK::stop() {
|
2011-04-14 22:25:38 +00:00
|
|
|
if (multi_process) {
|
|
|
|
suspend_or_resume_descendants(0, false);
|
|
|
|
} else {
|
|
|
|
suspend_or_resume_process(pid, false);
|
|
|
|
}
|
2008-03-06 18:31:33 +00:00
|
|
|
suspended = true;
|
2006-05-29 20:54:28 +00:00
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
void TASK::resume() {
|
2011-04-14 22:25:38 +00:00
|
|
|
if (multi_process) {
|
|
|
|
suspend_or_resume_descendants(0, true);
|
|
|
|
} else {
|
|
|
|
suspend_or_resume_process(pid, true);
|
|
|
|
}
|
2008-03-06 18:31:33 +00:00
|
|
|
suspended = false;
|
2006-05-29 20:54:28 +00:00
|
|
|
}
|
|
|
|
|
2011-02-24 21:39:07 +00:00
|
|
|
double TASK::cpu_time() {
|
2012-02-29 20:58:45 +00:00
|
|
|
current_cpu_time = process_tree_cpu_time(pid);
|
|
|
|
return current_cpu_time;
|
2011-02-24 21:39:07 +00:00
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
void poll_boinc_messages(TASK& task) {
|
2006-05-29 20:24:05 +00:00
|
|
|
BOINC_STATUS status;
|
|
|
|
boinc_get_status(&status);
|
|
|
|
if (status.no_heartbeat) {
|
2006-07-17 16:55:47 +00:00
|
|
|
task.kill();
|
2006-05-29 20:24:05 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (status.quit_request) {
|
2006-07-17 16:55:47 +00:00
|
|
|
task.kill();
|
2006-05-29 20:24:05 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (status.abort_request) {
|
2006-07-17 16:55:47 +00:00
|
|
|
task.kill();
|
2006-05-29 20:24:05 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (status.suspended) {
|
2008-03-06 18:31:33 +00:00
|
|
|
if (!task.suspended) {
|
2006-07-17 16:55:47 +00:00
|
|
|
task.stop();
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-03-06 18:31:33 +00:00
|
|
|
if (task.suspended) {
|
2006-07-17 16:55:47 +00:00
|
|
|
task.resume();
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
// Support for multiple tasks.
|
|
|
|
// We keep a checkpoint file that says how many tasks we've completed
|
|
|
|
// and how much CPU time has been used so far
|
|
|
|
//
|
2009-09-24 20:51:32 +00:00
|
|
|
void write_checkpoint(int ntasks_completed, double cpu) {
|
2011-05-06 12:33:12 +00:00
|
|
|
boinc_begin_critical_section();
|
2007-05-28 16:31:39 +00:00
|
|
|
FILE* f = fopen(CHECKPOINT_FILENAME, "w");
|
|
|
|
if (!f) return;
|
2009-09-24 20:51:32 +00:00
|
|
|
fprintf(f, "%d %f\n", ntasks_completed, cpu);
|
2007-05-28 16:31:39 +00:00
|
|
|
fclose(f);
|
2011-05-06 12:33:12 +00:00
|
|
|
boinc_checkpoint_completed();
|
2007-05-28 16:31:39 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 20:51:32 +00:00
|
|
|
void read_checkpoint(int& ntasks_completed, double& cpu) {
|
2007-05-28 16:31:39 +00:00
|
|
|
int nt;
|
|
|
|
double c;
|
|
|
|
|
2009-09-24 20:51:32 +00:00
|
|
|
ntasks_completed = 0;
|
2007-05-28 16:31:39 +00:00
|
|
|
cpu = 0;
|
|
|
|
FILE* f = fopen(CHECKPOINT_FILENAME, "r");
|
|
|
|
if (!f) return;
|
|
|
|
int n = fscanf(f, "%d %lf", &nt, &c);
|
2008-06-19 22:31:10 +00:00
|
|
|
fclose(f);
|
2007-05-28 16:31:39 +00:00
|
|
|
if (n != 2) return;
|
2009-09-24 20:51:32 +00:00
|
|
|
ntasks_completed = nt;
|
2007-05-28 16:31:39 +00:00
|
|
|
cpu = c;
|
2006-05-30 04:36:15 +00:00
|
|
|
}
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
BOINC_OPTIONS options;
|
2009-09-24 20:51:32 +00:00
|
|
|
int retval, ntasks_completed;
|
2008-06-17 20:39:59 +00:00
|
|
|
unsigned int i;
|
2009-09-24 20:51:32 +00:00
|
|
|
double total_weight=0, weight_completed=0;
|
2008-12-08 04:47:57 +00:00
|
|
|
double checkpoint_cpu_time;
|
|
|
|
// overall CPU time at last checkpoint
|
2006-05-29 20:24:05 +00:00
|
|
|
|
2011-06-12 20:58:43 +00:00
|
|
|
for (int j=1; j<argc; j++) {
|
|
|
|
if (!strcmp(argv[j], "--nthreads")) {
|
|
|
|
nthreads = atoi(argv[++j]);
|
2011-03-21 09:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
memset(&options, 0, sizeof(options));
|
|
|
|
options.main_program = true;
|
|
|
|
options.check_heartbeat = true;
|
|
|
|
options.handle_process_control = true;
|
|
|
|
|
|
|
|
boinc_init_options(&options);
|
2008-01-06 22:27:35 +00:00
|
|
|
fprintf(stderr, "wrapper: starting\n");
|
2008-06-18 18:55:06 +00:00
|
|
|
|
|
|
|
boinc_get_init_data(aid);
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
retval = parse_job_file();
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "can't parse job file: %d\n", retval);
|
|
|
|
boinc_finish(retval);
|
|
|
|
}
|
|
|
|
|
2009-09-24 20:51:32 +00:00
|
|
|
read_checkpoint(ntasks_completed, checkpoint_cpu_time);
|
|
|
|
if (ntasks_completed > (int)tasks.size()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Checkpoint file: ntasks_completed too large: %d > %d\n",
|
|
|
|
ntasks_completed, (int)tasks.size()
|
|
|
|
);
|
2007-05-28 16:31:39 +00:00
|
|
|
boinc_finish(1);
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
2008-06-17 20:39:59 +00:00
|
|
|
for (i=0; i<tasks.size(); i++) {
|
|
|
|
total_weight += tasks[i].weight;
|
|
|
|
}
|
2010-11-29 22:27:26 +00:00
|
|
|
|
2011-02-24 21:39:07 +00:00
|
|
|
retval = start_daemons(argc, argv);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "start_daemons(): %d\n", retval);
|
|
|
|
kill_daemons();
|
|
|
|
boinc_finish(retval);
|
|
|
|
}
|
|
|
|
|
2010-11-29 22:27:26 +00:00
|
|
|
// loop over tasks
|
|
|
|
//
|
2008-06-17 20:39:59 +00:00
|
|
|
for (i=0; i<tasks.size(); i++) {
|
2007-05-28 16:31:39 +00:00
|
|
|
TASK& task = tasks[i];
|
2009-09-24 20:51:32 +00:00
|
|
|
if ((int)i<ntasks_completed) {
|
|
|
|
weight_completed += task.weight;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double frac_done = weight_completed/total_weight;
|
2011-11-29 15:06:08 +00:00
|
|
|
double cpu_time = 0;
|
2007-05-28 16:31:39 +00:00
|
|
|
|
2008-12-08 04:47:57 +00:00
|
|
|
task.starting_cpu = checkpoint_cpu_time;
|
2007-05-28 16:31:39 +00:00
|
|
|
retval = task.run(argc, argv);
|
|
|
|
if (retval) {
|
|
|
|
boinc_finish(retval);
|
|
|
|
}
|
2011-11-29 15:06:08 +00:00
|
|
|
int counter = 0;
|
2009-09-24 20:51:32 +00:00
|
|
|
while (1) {
|
2007-05-28 16:31:39 +00:00
|
|
|
int status;
|
|
|
|
if (task.poll(status)) {
|
|
|
|
if (status) {
|
2008-03-06 16:15:17 +00:00
|
|
|
fprintf(stderr, "app exit status: 0x%x\n", status);
|
|
|
|
// On Unix, if the app is non-executable,
|
|
|
|
// the child status will be 0x6c00.
|
|
|
|
// If we return this the client will treat it
|
|
|
|
// as recoverable, and restart us.
|
|
|
|
// We don't want this, so return an 8-bit error code.
|
|
|
|
//
|
2011-02-24 21:39:07 +00:00
|
|
|
kill_daemons();
|
2008-06-18 16:44:12 +00:00
|
|
|
boinc_finish(EXIT_CHILD_FAILED);
|
2007-05-28 16:31:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-11-25 00:28:45 +00:00
|
|
|
}
|
2007-05-28 16:31:39 +00:00
|
|
|
poll_boinc_messages(task);
|
2009-09-24 20:51:32 +00:00
|
|
|
double task_fraction_done = task.fraction_done();
|
2009-12-13 18:06:04 +00:00
|
|
|
double delta = task_fraction_done*task.weight/total_weight;
|
2011-11-29 15:06:08 +00:00
|
|
|
|
|
|
|
// getting CPU time of task tree is inefficient,
|
|
|
|
// so do it only every 10 sec
|
|
|
|
//
|
|
|
|
if (counter%10 == 0) {
|
|
|
|
cpu_time = task.cpu_time();
|
|
|
|
}
|
|
|
|
boinc_report_app_status(
|
|
|
|
task.starting_cpu + cpu_time,
|
|
|
|
checkpoint_cpu_time,
|
|
|
|
frac_done + delta
|
|
|
|
);
|
2008-12-08 04:47:57 +00:00
|
|
|
if (task.has_checkpointed()) {
|
2011-11-29 15:06:08 +00:00
|
|
|
cpu_time = task.cpu_time();
|
|
|
|
checkpoint_cpu_time = task.starting_cpu + cpu_time;
|
2008-12-08 04:47:57 +00:00
|
|
|
write_checkpoint(i, checkpoint_cpu_time);
|
|
|
|
}
|
2007-11-27 19:15:32 +00:00
|
|
|
boinc_sleep(POLL_PERIOD);
|
2011-11-29 15:06:08 +00:00
|
|
|
counter++;
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
2008-12-08 04:47:57 +00:00
|
|
|
checkpoint_cpu_time = task.starting_cpu + task.final_cpu_time;
|
|
|
|
write_checkpoint(i+1, checkpoint_cpu_time);
|
2009-09-24 20:51:32 +00:00
|
|
|
weight_completed += task.weight;
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
2011-02-24 21:39:07 +00:00
|
|
|
kill_daemons();
|
2007-05-28 16:31:39 +00:00
|
|
|
boinc_finish(0);
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
2006-05-29 20:54:28 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
|
|
|
|
LPSTR command_line;
|
|
|
|
char* argv[100];
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
command_line = GetCommandLine();
|
2008-05-16 22:12:05 +00:00
|
|
|
argc = parse_command_line(command_line, argv);
|
2006-05-29 20:54:28 +00:00
|
|
|
return main(argc, argv);
|
|
|
|
}
|
2006-05-30 04:36:15 +00:00
|
|
|
#endif
|