2006-05-29 20:24:05 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
|
|
|
//
|
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// Does NOT handle:
|
|
|
|
// - checkpointing
|
|
|
|
// If your app does checkpointing,
|
|
|
|
// and there's some way to figure out when it's done it,
|
|
|
|
// this program could be modified to report to the core client.
|
|
|
|
//
|
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"
|
2006-05-29 20:24:05 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.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"
|
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"
|
|
|
|
#define CHECKPOINT_FILENAME "checkpoint.txt"
|
|
|
|
|
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;
|
2006-07-17 16:55:47 +00:00
|
|
|
string command_line;
|
2007-05-28 16:31:39 +00:00
|
|
|
double final_cpu_time;
|
|
|
|
double starting_cpu;
|
2006-05-29 20:24:05 +00:00
|
|
|
#ifdef _WIN32
|
2006-07-17 16:55:47 +00:00
|
|
|
HANDLE pid_handle;
|
|
|
|
HANDLE thread_handle;
|
2006-05-29 20:24:05 +00:00
|
|
|
#else
|
2006-07-17 16:55:47 +00:00
|
|
|
int pid;
|
2006-05-29 20:24:05 +00:00
|
|
|
#endif
|
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();
|
2006-07-17 16:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
vector<TASK> tasks;
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
bool app_suspended = false;
|
|
|
|
|
2006-09-10 02:21:48 +00:00
|
|
|
int TASK::parse(XML_PARSER& xp) {
|
|
|
|
char tag[1024];
|
|
|
|
bool is_tag;
|
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
final_cpu_time = 0;
|
2006-09-10 02:21:48 +00:00
|
|
|
while (!xp.get(tag, sizeof(tag), is_tag)) {
|
|
|
|
if (!is_tag) {
|
|
|
|
fprintf(stderr, "SCHED_CONFIG::parse(): unexpected text %s\n", tag);
|
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;
|
2006-09-10 02:21:48 +00:00
|
|
|
else if (xp.parse_string(tag, "command_line", command_line)) 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;
|
|
|
|
char tag[1024], buf[256];
|
|
|
|
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) {
|
|
|
|
fprintf(stderr, "can't open job file %s\n", buf);
|
|
|
|
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) {
|
|
|
|
fprintf(stderr, "SCHED_CONFIG::parse(): unexpected text %s\n", tag);
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-29 20:24:05 +00:00
|
|
|
if (!strcmp(tag, "/job_desc")) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
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")) {
|
|
|
|
return CreateFile(
|
|
|
|
path,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
FILE_SHARE_WRITE,
|
|
|
|
&sa,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
0, 0
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
// the "state file" might tell us which app we're in the middle of,
|
|
|
|
// what the starting CPU time is, etc.
|
2007-04-26 20:10:54 +00:00
|
|
|
// Not implemented yet.
|
2006-05-29 20:24:05 +00:00
|
|
|
//
|
|
|
|
void parse_state_file() {
|
|
|
|
}
|
|
|
|
|
2007-04-26 20:10:54 +00:00
|
|
|
int TASK::run(int argct, char** argvt) {
|
|
|
|
string app_path, stdout_path, stdin_path, stderr_path;
|
2006-08-12 22:34:34 +00:00
|
|
|
|
2006-08-17 22:09:20 +00:00
|
|
|
boinc_resolve_filename_s(application.c_str(), app_path);
|
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++){
|
|
|
|
command_line += argvt[i];
|
|
|
|
if ((i+1) < argct){
|
|
|
|
command_line += string(" ");
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
2006-05-29 20:24:05 +00:00
|
|
|
memset(&process_info, 0, sizeof(process_info));
|
|
|
|
memset(&startup_info, 0, sizeof(startup_info));
|
2006-08-17 22:09:20 +00:00
|
|
|
command = 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);
|
|
|
|
startup_info.hStdOutput = win_fopen(stdout_path.c_str(), "w");
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
startup_info.hStdError = win_fopen(stderr_path.c_str(), "w");
|
|
|
|
} else {
|
|
|
|
startup_info.hStdError = win_fopen(STDERR_FILE, "a");
|
|
|
|
}
|
2006-05-29 20:24:05 +00:00
|
|
|
|
2006-08-17 22:09:20 +00:00
|
|
|
if (!CreateProcess(
|
|
|
|
app_path.c_str(),
|
|
|
|
(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
|
|
|
|
)) {
|
|
|
|
return ERR_EXEC;
|
|
|
|
}
|
2006-05-29 20:54:28 +00:00
|
|
|
pid_handle = process_info.hProcess;
|
|
|
|
thread_handle = process_info.hThread;
|
2006-05-29 20:24:05 +00:00
|
|
|
#else
|
2006-08-17 22:09:20 +00:00
|
|
|
int retval, argc;
|
2006-05-29 20:24:05 +00:00
|
|
|
char progname[256], buf[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) {
|
|
|
|
boinc_finish(ERR_FORK);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
stdout_file = freopen(stdout_path.c_str(), "w", stdout);
|
|
|
|
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);
|
|
|
|
stderr_file = freopen(stderr_path.c_str(), "w", stderr);
|
|
|
|
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
|
|
|
//
|
|
|
|
strcpy(buf, app_path.c_str());
|
2006-05-29 20:24:05 +00:00
|
|
|
argv[0] = buf;
|
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-05-28 16:31:39 +00:00
|
|
|
fprintf(stderr, "wrapper: running %s (%s)\n", buf, arglist);
|
2006-05-29 20:24:05 +00:00
|
|
|
retval = execv(buf, argv);
|
|
|
|
exit(ERR_EXEC);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
bool TASK::poll(int& status) {
|
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
|
|
|
|
SuspendThread(thread_handle);
|
|
|
|
#else
|
2006-07-17 16:55:47 +00:00
|
|
|
::kill(pid, SIGSTOP);
|
2006-05-29 20:54:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-07-17 16:55:47 +00:00
|
|
|
void TASK::resume() {
|
2006-05-29 20:54:28 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
ResumeThread(thread_handle);
|
|
|
|
#else
|
2006-07-17 16:55:47 +00:00
|
|
|
::kill(pid, SIGCONT);
|
2006-05-29 20:54:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (!app_suspended) {
|
2006-07-17 16:55:47 +00:00
|
|
|
task.stop();
|
2006-05-29 20:24:05 +00:00
|
|
|
app_suspended = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (app_suspended) {
|
2006-07-17 16:55:47 +00:00
|
|
|
task.resume();
|
2006-05-29 20:24:05 +00:00
|
|
|
app_suspended = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-17 21:25:14 +00:00
|
|
|
double TASK::cpu_time() {
|
2006-05-30 04:36:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
FILETIME creation_time, exit_time, kernel_time, user_time;
|
|
|
|
ULARGE_INTEGER tKernel, tUser;
|
|
|
|
LONGLONG totTime;
|
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
int retval = GetProcessTimes(
|
2006-05-30 04:36:15 +00:00
|
|
|
pid_handle, &creation_time, &exit_time, &kernel_time, &user_time
|
|
|
|
);
|
2007-05-28 16:31:39 +00:00
|
|
|
if (retval == 0) return 0;
|
2006-05-30 04:36:15 +00:00
|
|
|
|
|
|
|
tKernel.LowPart = kernel_time.dwLowDateTime;
|
|
|
|
tKernel.HighPart = kernel_time.dwHighDateTime;
|
|
|
|
tUser.LowPart = user_time.dwLowDateTime;
|
|
|
|
tUser.HighPart = user_time.dwHighDateTime;
|
|
|
|
totTime = tKernel.QuadPart + tUser.QuadPart;
|
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
return totTime / 1.e7;
|
2006-05-30 04:36:15 +00:00
|
|
|
#else
|
2007-05-28 16:31:39 +00:00
|
|
|
return linux_cpu_time(pid);
|
2006-05-30 04:36:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
void send_status_message(TASK& task, double frac_done) {
|
|
|
|
boinc_report_app_status(task.cpu_time(), task.starting_cpu, frac_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
void write_checkpoint(int ntasks, double cpu) {
|
|
|
|
FILE* f = fopen(CHECKPOINT_FILENAME, "w");
|
|
|
|
if (!f) return;
|
|
|
|
fprintf(f, "%d %f\n", ntasks, cpu);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_checkpoint(int& ntasks, double& cpu) {
|
|
|
|
int nt;
|
|
|
|
double c;
|
|
|
|
|
|
|
|
ntasks = 0;
|
|
|
|
cpu = 0;
|
|
|
|
FILE* f = fopen(CHECKPOINT_FILENAME, "r");
|
|
|
|
if (!f) return;
|
|
|
|
int n = fscanf(f, "%d %lf", &nt, &c);
|
|
|
|
if (n != 2) return;
|
|
|
|
ntasks = nt;
|
|
|
|
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;
|
|
|
|
int retval;
|
2007-05-28 16:31:39 +00:00
|
|
|
int ntasks;
|
|
|
|
double cpu;
|
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;
|
|
|
|
|
2006-06-14 20:14:20 +00:00
|
|
|
fprintf(stderr, "wrapper: starting\n");
|
2006-05-29 20:24:05 +00:00
|
|
|
boinc_init_options(&options);
|
|
|
|
retval = parse_job_file();
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "can't parse job file: %d\n", retval);
|
|
|
|
boinc_finish(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_state_file();
|
|
|
|
|
2007-05-28 16:31:39 +00:00
|
|
|
read_checkpoint(ntasks, cpu);
|
|
|
|
if (ntasks > tasks.size()) {
|
|
|
|
fprintf(stderr, "Checkpoint file: ntasks %d too large\n", ntasks);
|
|
|
|
boinc_finish(1);
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
2007-05-28 16:31:39 +00:00
|
|
|
for (unsigned int i=ntasks; i<tasks.size(); i++) {
|
|
|
|
TASK& task = tasks[i];
|
|
|
|
double frac_done = ((double)i)/((double)tasks.size());
|
|
|
|
|
|
|
|
fprintf(stderr, "running %s\n", task.application.c_str());
|
|
|
|
task.starting_cpu = cpu;
|
|
|
|
retval = task.run(argc, argv);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "can't run app: %d\n", retval);
|
|
|
|
boinc_finish(retval);
|
|
|
|
}
|
|
|
|
while(1) {
|
|
|
|
int status;
|
|
|
|
if (task.poll(status)) {
|
|
|
|
if (status) {
|
|
|
|
fprintf(stderr, "app error: 0x%x\n", status);
|
|
|
|
boinc_finish(status);
|
|
|
|
}
|
|
|
|
break;
|
2006-11-25 00:28:45 +00:00
|
|
|
}
|
2007-05-28 16:31:39 +00:00
|
|
|
poll_boinc_messages(task);
|
|
|
|
send_status_message(task, frac_done);
|
|
|
|
boinc_sleep(1.);
|
2006-05-29 20:24:05 +00:00
|
|
|
}
|
2007-05-28 16:31:39 +00:00
|
|
|
cpu += task.final_cpu_time;
|
|
|
|
write_checkpoint(i+1, cpu);
|
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();
|
|
|
|
argc = parse_command_line( command_line, argv );
|
|
|
|
return main(argc, argv);
|
|
|
|
}
|
2006-05-30 04:36:15 +00:00
|
|
|
#endif
|