2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-08-02 00:02:11 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-08-02 00:02:11 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-08-02 00:02:11 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
2003-05-22 20:47:56 +00:00
|
|
|
// High-level logic involving task execution
|
|
|
|
//
|
2003-07-01 20:37:09 +00:00
|
|
|
|
2002-06-06 18:42:01 +00:00
|
|
|
#include "windows_cpp.h"
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <afxwin.h>
|
|
|
|
#endif
|
2003-05-13 21:20:08 +00:00
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "md5_file.h"
|
2003-05-21 23:23:42 +00:00
|
|
|
#include "error_numbers.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "file_names.h"
|
2003-05-21 23:23:42 +00:00
|
|
|
#include "filesys.h"
|
2003-03-17 23:35:00 +00:00
|
|
|
#include "shmem.h"
|
2003-07-02 02:02:18 +00:00
|
|
|
#include "log_flags.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "client_state.h"
|
|
|
|
|
2003-05-22 20:47:56 +00:00
|
|
|
// Make a directory for each available slot
|
2002-08-22 21:29:58 +00:00
|
|
|
//
|
|
|
|
int CLIENT_STATE::make_slot_dirs() {
|
2003-03-20 02:05:25 +00:00
|
|
|
int i;
|
2003-03-08 23:48:05 +00:00
|
|
|
int retval;
|
2002-08-22 21:29:58 +00:00
|
|
|
for (i=0; i<nslots; i++) {
|
2003-03-08 23:48:05 +00:00
|
|
|
retval = make_slot_dir(i);
|
|
|
|
if (retval) return retval;
|
2002-08-22 21:29:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a graceful shutdown of the client, including quitting
|
|
|
|
// all applications, checking their final status, and writing
|
2002-11-20 20:14:48 +00:00
|
|
|
// the client_state.xml file (should we also terminate net_xfers here?)
|
2002-08-22 21:29:58 +00:00
|
|
|
//
|
2003-01-07 01:02:08 +00:00
|
|
|
int CLIENT_STATE::cleanup_and_exit() {
|
2002-08-22 21:29:58 +00:00
|
|
|
int retval;
|
2003-01-07 01:02:08 +00:00
|
|
|
|
2003-05-22 20:47:56 +00:00
|
|
|
retval = active_tasks.exit_tasks();
|
2002-08-22 21:29:58 +00:00
|
|
|
if (retval) {
|
2003-07-03 05:01:29 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "CLIENT_STATE.cleanup_and_exit: exit_tasks failed\n");
|
2003-01-07 01:02:08 +00:00
|
|
|
// don't return here - we'll exit anyway
|
2002-08-22 21:29:58 +00:00
|
|
|
}
|
2002-11-20 20:14:48 +00:00
|
|
|
retval = write_state_file();
|
2003-03-12 18:15:48 +00:00
|
|
|
if (retval) {
|
2003-07-03 05:01:29 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "CLIENT_STATE.cleanup_and_exit: write_state_file failed\n");
|
2003-01-07 01:02:08 +00:00
|
|
|
// don't return here - we'll exit anyway
|
2002-11-20 20:14:48 +00:00
|
|
|
}
|
2003-05-13 21:20:08 +00:00
|
|
|
|
|
|
|
// Stop the CPU benchmark if it's running
|
|
|
|
if (cpu_benchmarks_id) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
TerminateThread(cpu_benchmarks_handle, 0);
|
|
|
|
CloseHandle(cpu_benchmarks_handle);
|
|
|
|
#else
|
|
|
|
kill(cpu_benchmarks_id, SIGKILL);
|
|
|
|
#endif
|
|
|
|
}
|
2003-06-03 22:47:15 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "Exiting BOINC client");
|
2002-08-22 21:29:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// Handle a task that has finished.
|
|
|
|
// Mark its output files as present, and delete scratch files.
|
|
|
|
// Don't delete input files because they might be shared with other WUs.
|
|
|
|
// Update state of result record.
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::app_finished(ACTIVE_TASK& at) {
|
|
|
|
RESULT* rp = at.result;
|
|
|
|
FILE_INFO* fip;
|
|
|
|
unsigned int i;
|
2003-06-03 22:47:15 +00:00
|
|
|
char path[256];
|
2002-08-23 00:53:00 +00:00
|
|
|
int retval;
|
2003-05-21 23:23:42 +00:00
|
|
|
double size;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-06-26 23:53:37 +00:00
|
|
|
bool had_error = false;
|
|
|
|
|
2003-06-30 22:02:57 +00:00
|
|
|
if (at.exit_status != 0 && at.exit_status != ERR_QUIT_REQUEST) had_error = true;
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<rp->output_files.size(); i++) {
|
|
|
|
fip = rp->output_files[i].file_info;
|
2003-05-21 23:23:42 +00:00
|
|
|
get_pathname(fip, path);
|
|
|
|
retval = file_size(path, size);
|
|
|
|
if (retval) {
|
|
|
|
// an output file is unexpectedly absent.
|
2003-06-26 23:53:37 +00:00
|
|
|
//
|
2003-05-21 23:23:42 +00:00
|
|
|
fip->status = retval;
|
2003-06-26 23:53:37 +00:00
|
|
|
had_error = true;
|
|
|
|
} else if (size > fip->max_nbytes) {
|
|
|
|
// Note: this is only checked when the application finishes. there
|
|
|
|
// is also a check_max_disk_exceeded that is checked while the
|
|
|
|
// application is running.
|
2003-07-29 23:26:32 +00:00
|
|
|
//
|
|
|
|
msg_printf(
|
|
|
|
rp->project, MSG_INFO,
|
|
|
|
"Output file %s for result %s exceeds size limit.",
|
|
|
|
fip->name, rp->name
|
|
|
|
);
|
2003-05-22 20:47:56 +00:00
|
|
|
|
2003-06-26 23:53:37 +00:00
|
|
|
fip->delete_file();
|
|
|
|
fip->status = ERR_FILE_TOO_BIG;
|
|
|
|
had_error = true;
|
|
|
|
} else {
|
|
|
|
if (!fip->upload_when_present && !fip->sticky) {
|
|
|
|
fip->delete_file(); // sets status to NOT_PRESENT
|
2003-05-21 23:23:42 +00:00
|
|
|
} else {
|
2003-06-26 23:53:37 +00:00
|
|
|
retval = md5_file(path, fip->md5_cksum, fip->nbytes);
|
|
|
|
if (retval) {
|
|
|
|
fip->status = retval;
|
|
|
|
had_error = true;
|
2003-05-21 23:23:42 +00:00
|
|
|
} else {
|
2003-06-26 23:53:37 +00:00
|
|
|
fip->status = FILE_PRESENT;
|
2003-05-21 23:23:42 +00:00
|
|
|
}
|
2002-11-19 22:57:05 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 23:53:37 +00:00
|
|
|
rp->is_active = false;
|
|
|
|
if (had_error) {
|
|
|
|
// dead-end state indicating we had an error at end of computation;
|
|
|
|
// do not move to RESULT_FILES_UPLOADING
|
|
|
|
rp->state = RESULT_COMPUTE_DONE;
|
|
|
|
} else {
|
|
|
|
// can now upload files.
|
|
|
|
rp->state = RESULT_FILES_UPLOADING;
|
|
|
|
}
|
|
|
|
update_avg_cpu(rp->project);
|
|
|
|
rp->project->exp_avg_cpu += rp->final_cpu_time;
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-05-22 20:47:56 +00:00
|
|
|
// clean up after finished apps
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2003-05-22 20:47:56 +00:00
|
|
|
bool CLIENT_STATE::handle_finished_apps() {
|
2002-04-30 22:22:54 +00:00
|
|
|
unsigned int i;
|
|
|
|
ACTIVE_TASK* atp;
|
|
|
|
bool action = false;
|
|
|
|
|
2003-07-02 02:02:18 +00:00
|
|
|
ScopeMessages scope_messages(log_messages, ClientMessages::DEBUG_TASK);
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<active_tasks.active_tasks.size(); i++) {
|
|
|
|
atp = active_tasks.active_tasks[i];
|
2003-05-22 20:47:56 +00:00
|
|
|
switch (atp->state) {
|
|
|
|
case PROCESS_RUNNING:
|
|
|
|
case PROCESS_ABORT_PENDING:
|
|
|
|
break;
|
|
|
|
default:
|
2003-06-03 22:47:15 +00:00
|
|
|
msg_printf(atp->wup->project, MSG_INFO, "Computation for result %s finished", atp->wup->name);
|
2003-07-29 23:26:32 +00:00
|
|
|
scope_messages.printf(
|
|
|
|
"CLIENT_STATE::handle_finished_apps(): task finished; pid %d, status %d\n",
|
|
|
|
atp->pid, atp->exit_status
|
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
app_finished(*atp);
|
2003-03-19 18:46:58 +00:00
|
|
|
active_tasks.remove(atp);
|
2002-04-30 22:22:54 +00:00
|
|
|
delete atp;
|
2002-08-22 21:29:58 +00:00
|
|
|
set_client_state_dirty("handle_running_apps");
|
2002-04-30 22:22:54 +00:00
|
|
|
action = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Returns true if all the input files for a result are available
|
|
|
|
// locally, false otherwise
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
bool CLIENT_STATE::input_files_available(RESULT* rp) {
|
|
|
|
WORKUNIT* wup = rp->wup;
|
|
|
|
FILE_INFO* fip;
|
2002-12-05 21:56:33 +00:00
|
|
|
unsigned int i;
|
2002-05-17 22:33:57 +00:00
|
|
|
APP_VERSION* avp;
|
2003-08-01 21:50:09 +00:00
|
|
|
FILE_REF fr;
|
2002-05-17 22:33:57 +00:00
|
|
|
avp = wup->avp;
|
|
|
|
for (i=0; i<avp->app_files.size(); i++) {
|
2003-08-01 21:50:09 +00:00
|
|
|
fr = avp->app_files[i];
|
|
|
|
// if it's an optional file and we couldn't download it by deadline,
|
|
|
|
// don't let that stop us from running app
|
|
|
|
//
|
|
|
|
if (fr.optional && time(0) > fr.optional_deadline) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fip = fr.file_info;
|
2002-08-12 21:54:19 +00:00
|
|
|
if (fip->status != FILE_PRESENT) return false;
|
2002-05-17 22:33:57 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
for (i=0; i<wup->input_files.size(); i++) {
|
|
|
|
fip = wup->input_files[i].file_info;
|
2002-08-12 21:54:19 +00:00
|
|
|
if (fip->status != FILE_PRESENT) return false;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-08-07 21:45:58 +00:00
|
|
|
// if necessary we could change results to a set or sorted vector to improve
|
|
|
|
// efficiency here.
|
|
|
|
RESULT* CLIENT_STATE::next_result_to_start() const
|
|
|
|
{
|
2003-08-07 22:11:25 +00:00
|
|
|
int earliest_deadline = INT_MAX;
|
2003-08-07 21:45:58 +00:00
|
|
|
RESULT* rp_earliest_deadline = NULL;
|
|
|
|
|
|
|
|
for (vector<RESULT*>::const_iterator i = results.begin();
|
|
|
|
i != results.end(); ++i)
|
|
|
|
{
|
|
|
|
RESULT* rp = *i;
|
|
|
|
if (rp->state == RESULT_FILES_DOWNLOADED && !rp->is_active) {
|
|
|
|
if (rp->report_deadline < earliest_deadline) {
|
|
|
|
earliest_deadline = rp->report_deadline;
|
|
|
|
rp_earliest_deadline = rp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rp_earliest_deadline;
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// start new app if possible
|
|
|
|
//
|
|
|
|
bool CLIENT_STATE::start_apps() {
|
|
|
|
bool action = false;
|
2003-08-07 21:45:58 +00:00
|
|
|
RESULT* rp;
|
|
|
|
int open_slot;
|
2002-08-28 21:50:51 +00:00
|
|
|
|
2003-08-07 21:45:58 +00:00
|
|
|
while ( (open_slot = active_tasks.get_free_slot(nslots)) >= 0 &&
|
|
|
|
(rp = next_result_to_start()) != NULL)
|
|
|
|
{
|
|
|
|
int retval;
|
2002-08-28 21:50:51 +00:00
|
|
|
// Start the application to compute a result if:
|
|
|
|
// 1) the result isn't done yet;
|
|
|
|
// 2) the application isn't currently computing the result;
|
|
|
|
// 3) all the input files for the result are locally available
|
|
|
|
//
|
2003-08-07 21:45:58 +00:00
|
|
|
if (log_flags.task) {
|
|
|
|
msg_printf(rp->project, MSG_INFO, "Starting computation for result %s", rp->name);
|
|
|
|
}
|
|
|
|
rp->is_active = true;
|
|
|
|
ACTIVE_TASK* atp = new ACTIVE_TASK;
|
|
|
|
atp->slot = open_slot;
|
|
|
|
atp->init(rp);
|
|
|
|
retval = active_tasks.insert(atp);
|
2003-01-07 22:49:42 +00:00
|
|
|
|
2003-08-07 21:45:58 +00:00
|
|
|
// couldn't start process
|
|
|
|
//
|
|
|
|
if (retval) {
|
|
|
|
atp->state = PROCESS_COULDNT_START;
|
|
|
|
atp->result->active_task_state = PROCESS_COULDNT_START;
|
|
|
|
report_result_error(
|
|
|
|
*(atp->result), retval,
|
|
|
|
"Couldn't start the app for this result."
|
2003-02-12 19:53:46 +00:00
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-08-07 21:45:58 +00:00
|
|
|
action = true;
|
|
|
|
set_client_state_dirty("start_apps");
|
|
|
|
app_started = time(0);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// This is called when the client is initialized.
|
2002-04-30 22:22:54 +00:00
|
|
|
// Try to restart any tasks that were running when we last shut down.
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::restart_tasks() {
|
|
|
|
return active_tasks.restart_tasks();
|
|
|
|
}
|