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
|
|
|
//
|
2006-07-17 16:55:47 +00:00
|
|
|
// See http://boinc.berkeley.edu/wrapper.php 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>
|
2007-03-20 20:09:37 +00:00
|
|
|
#include "procinfo.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"
|
2006-05-29 20:24:05 +00:00
|
|
|
#include "filesys.h"
|
|
|
|
#include "parse.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"
|
|
|
|
#include "error_numbers.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;
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
struct TASK {
|
|
|
|
string application;
|
|
|
|
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;
|
2008-06-17 20:39:59 +00:00
|
|
|
double weight;
|
|
|
|
// contribution of this task to overall fraction done
|
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;
|
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();
|
2006-08-17 21:25:14 +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;
|
|
|
|
}
|
2006-07-17 16:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
vector<TASK> tasks;
|
2008-06-18 18:55:06 +00:00
|
|
|
APP_INIT_DATA aid;
|
2008-06-26 18:17:01 +00:00
|
|
|
bool graphics = false;
|
2006-07-17 16:55:47 +00:00
|
|
|
|
2006-09-10 02:21:48 +00:00
|
|
|
int TASK::parse(XML_PARSER& xp) {
|
2008-06-18 18:55:06 +00:00
|
|
|
char tag[1024], buf[8192], buf2[8192];
|
2006-09-10 02:21:48 +00:00
|
|
|
bool is_tag;
|
|
|
|
|
2008-06-17 20:39:59 +00:00
|
|
|
weight = 1;
|
2007-05-28 16:31:39 +00:00
|
|
|
final_cpu_time = 0;
|
2008-06-17 20:39:59 +00:00
|
|
|
stat_first = true;
|
2006-09-10 02:21:48 +00:00
|
|
|
while (!xp.get(tag, sizeof(tag), is_tag)) {
|
|
|
|
if (!is_tag) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s TASK::parse(): unexpected text %s\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), tag
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2006-07-17 16:55:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-09-10 02:21:48 +00:00
|
|
|
if (!strcmp(tag, "/task")) {
|
|
|
|
return 0;
|
2006-07-17 16:55:47 +00:00
|
|
|
}
|
2006-09-10 02:21:48 +00:00
|
|
|
else if (xp.parse_string(tag, "application", application)) continue;
|
|
|
|
else if (xp.parse_string(tag, "stdin_filename", stdin_filename)) continue;
|
|
|
|
else if (xp.parse_string(tag, "stdout_filename", stdout_filename)) continue;
|
2007-04-26 20:10:54 +00:00
|
|
|
else if (xp.parse_string(tag, "stderr_filename", stderr_filename)) continue;
|
2008-06-18 18:55:06 +00:00
|
|
|
else if (xp.parse_str(tag, "command_line", buf, sizeof(buf))) {
|
|
|
|
while (1) {
|
|
|
|
char* p = strstr(buf, "$PROJECT_DIR");
|
|
|
|
if (!p) break;
|
|
|
|
strcpy(buf2, p+strlen("$PROJECT_DIR"));
|
|
|
|
strcpy(p, aid.project_dir);
|
|
|
|
strcat(p, buf2);
|
|
|
|
}
|
|
|
|
command_line = buf;
|
|
|
|
continue;
|
|
|
|
}
|
2008-06-17 20:39:59 +00:00
|
|
|
else if (xp.parse_string(tag, "checkpoint_filename", checkpoint_filename)) continue;
|
2009-09-24 20:51:32 +00:00
|
|
|
else if (xp.parse_string(tag, "fraction_done_filename", fraction_done_filename)) continue;
|
2008-06-17 20:39:59 +00:00
|
|
|
else if (xp.parse_double(tag, "weight", weight)) 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;
|
2010-06-21 23:29:37 +00:00
|
|
|
char tag[1024], buf[256], buf2[256];
|
2006-09-10 02:21:48 +00:00
|
|
|
bool is_tag;
|
|
|
|
|
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;
|
|
|
|
while (!xp.get(tag, sizeof(tag), is_tag)) {
|
|
|
|
if (!is_tag) {
|
2010-09-15 23:03:30 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s SCHED_CONFIG::parse(): unexpected text %s\n",
|
|
|
|
boinc_msg_prefix(buf2, sizeof(buf2)), tag
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2006-09-10 02:21:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-05-29 20:24:05 +00:00
|
|
|
if (!strcmp(tag, "/job_desc")) {
|
2008-06-19 22:31:10 +00:00
|
|
|
fclose(f);
|
2006-05-29 20:24:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-17 16:55:47 +00:00
|
|
|
if (!strcmp(tag, "task")) {
|
|
|
|
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) {
|
|
|
|
tasks.push_back(task);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
|
|
|
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")) {
|
2007-09-20 20:34:24 +00:00
|
|
|
HANDLE hAppend = CreateFile(
|
2006-11-25 22:13:27 +00:00
|
|
|
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;
|
2006-11-25 22:13:27 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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 (checkpoint_filename.size()) {
|
|
|
|
boinc_delete_file(checkpoint_filename.c_str());
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2007-04-26 20:10:54 +00:00
|
|
|
// Append wrapper's command-line arguments to those in the job file.
|
|
|
|
//
|
|
|
|
for (int i=1; i<argct; i++){
|
2009-06-15 23:27:37 +00:00
|
|
|
command_line += string(" ");
|
2007-04-26 20:10:54 +00:00
|
|
|
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;
|
2006-11-25 22:13:27 +00:00
|
|
|
if (stdout_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stdout_filename.c_str(), stdout_path);
|
2008-10-03 05:13:25 +00:00
|
|
|
startup_info.hStdOutput = win_fopen(stdout_path.c_str(), "a");
|
2006-11-25 22:13:27 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2006-08-17 22:09:20 +00:00
|
|
|
if (!CreateProcess(
|
2008-06-18 20:33:21 +00:00
|
|
|
app_path,
|
2006-08-17 22:09:20 +00:00
|
|
|
(LPSTR)command.c_str(),
|
2006-05-29 20:24:05 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2006-08-12 22:34:34 +00:00
|
|
|
TRUE, // bInheritHandles
|
2006-05-29 20:24:05 +00:00
|
|
|
CREATE_NO_WINDOW|IDLE_PRIORITY_CLASS,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&startup_info,
|
|
|
|
&process_info
|
|
|
|
)) {
|
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);
|
2006-05-29 20:24:05 +00:00
|
|
|
return ERR_EXEC;
|
|
|
|
}
|
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
|
2006-08-17 22:09:20 +00:00
|
|
|
int retval, argc;
|
2008-06-18 20:12:55 +00:00
|
|
|
char progname[256];
|
2006-08-17 22:09:20 +00:00
|
|
|
char* argv[256];
|
2007-04-26 20:10:54 +00:00
|
|
|
char arglist[4096];
|
2006-11-25 22:13:27 +00:00
|
|
|
FILE* stdout_file;
|
|
|
|
FILE* stdin_file;
|
2007-04-26 20:10:54 +00:00
|
|
|
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) {
|
2006-11-25 22:13:27 +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);
|
2008-10-03 05:13:25 +00:00
|
|
|
stdout_file = freopen(stdout_path.c_str(), "a", stdout);
|
2006-11-25 22:13:27 +00:00
|
|
|
if (!stdout_file) return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
if (stdin_filename != "") {
|
|
|
|
boinc_resolve_filename_s(stdin_filename.c_str(), stdin_path);
|
|
|
|
stdin_file = freopen(stdin_path.c_str(), "r", stdin);
|
|
|
|
if (!stdin_file) return ERR_FOPEN;
|
|
|
|
}
|
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);
|
2007-04-26 20:10:54 +00:00
|
|
|
if (!stderr_file) return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
|
2006-11-25 22:13:27 +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));
|
2006-08-17 22:09:20 +00:00
|
|
|
argc = parse_command_line(arglist, argv+1);
|
2007-06-13 16:42:36 +00:00
|
|
|
setpriority(PRIO_PROCESS, 0, PROCESS_IDLE_PRIORITY);
|
2008-06-18 18:55:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
#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();
|
2006-06-14 20:14:20 +00:00
|
|
|
return true;
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int wpid, stat;
|
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) {
|
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;
|
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
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
void TASK::kill() {
|
2006-05-29 20:24:05 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
TerminateProcess(pid_handle, -1);
|
|
|
|
#else
|
2006-07-17 16:55:47 +00:00
|
|
|
::kill(pid, SIGKILL);
|
2006-05-29 20:24:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
void TASK::stop() {
|
2006-05-29 20:54:28 +00:00
|
|
|
#ifdef _WIN32
|
2010-01-01 05:03:13 +00:00
|
|
|
suspend_or_resume_threads(pid, 0, false);
|
2006-05-29 20:54:28 +00:00
|
|
|
#else
|
2006-07-17 16:55:47 +00:00
|
|
|
::kill(pid, SIGSTOP);
|
2006-05-29 20:54:28 +00:00
|
|
|
#endif
|
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() {
|
2006-05-29 20:54:28 +00:00
|
|
|
#ifdef _WIN32
|
2010-01-01 05:03:13 +00:00
|
|
|
suspend_or_resume_threads(pid, 0, true);
|
2006-05-29 20:54:28 +00:00
|
|
|
#else
|
2006-07-17 16:55:47 +00:00
|
|
|
::kill(pid, SIGCONT);
|
2006-05-29 20:54:28 +00:00
|
|
|
#endif
|
2008-03-06 18:31:33 +00:00
|
|
|
suspended = false;
|
2006-05-29 20:54:28 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-17 21:25:14 +00:00
|
|
|
double TASK::cpu_time() {
|
2006-05-30 04:36:15 +00:00
|
|
|
#ifdef _WIN32
|
2009-03-24 17:18:09 +00:00
|
|
|
double x;
|
|
|
|
int retval = boinc_process_cpu_time(pid_handle, x);
|
|
|
|
if (retval) return wall_cpu_time;
|
|
|
|
return x;
|
2008-03-07 17:53:58 +00:00
|
|
|
#elif defined(__APPLE__)
|
2008-03-07 04:43:08 +00:00
|
|
|
// There's no easy way to get another process's CPU time in Mac OS X
|
|
|
|
//
|
|
|
|
return wall_cpu_time;
|
2006-05-30 04:36:15 +00:00
|
|
|
#else
|
2008-06-17 20:39:59 +00:00
|
|
|
return linux_cpu_time(pid);
|
2006-05-30 04:36:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-12-08 04:47:57 +00:00
|
|
|
void send_status_message(
|
|
|
|
TASK& task, double frac_done, double checkpoint_cpu_time
|
|
|
|
) {
|
2008-06-17 20:39:59 +00:00
|
|
|
double current_cpu_time = task.starting_cpu + task.cpu_time();
|
2007-06-28 22:49:48 +00:00
|
|
|
boinc_report_app_status(
|
2008-06-17 20:39:59 +00:00
|
|
|
current_cpu_time,
|
2008-12-08 04:47:57 +00:00
|
|
|
checkpoint_cpu_time,
|
2007-06-28 22:49:48 +00:00
|
|
|
frac_done
|
|
|
|
);
|
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) {
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-08-13 17:26:26 +00:00
|
|
|
for (i=1; i<(unsigned int)argc; i++) {
|
2008-06-26 18:17:01 +00:00
|
|
|
if (!strcmp(argv[i], "--graphics")) {
|
|
|
|
graphics = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2008-06-26 18:17:01 +00:00
|
|
|
if (graphics) {
|
|
|
|
options.backwards_compatible_graphics = true;
|
|
|
|
}
|
2006-05-29 20:24:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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;
|
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);
|
|
|
|
}
|
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.
|
|
|
|
//
|
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;
|
2009-09-24 20:51:32 +00:00
|
|
|
send_status_message(task, frac_done+delta, checkpoint_cpu_time);
|
2008-12-08 04:47:57 +00:00
|
|
|
if (task.has_checkpointed()) {
|
|
|
|
checkpoint_cpu_time = task.starting_cpu + task.cpu_time();
|
|
|
|
write_checkpoint(i, checkpoint_cpu_time);
|
|
|
|
}
|
2007-11-27 19:15:32 +00:00
|
|
|
boinc_sleep(POLL_PERIOD);
|
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
|
|
|
}
|
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
|