2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
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
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "md5_file.h"
|
|
|
|
#include "log_flags.h"
|
|
|
|
#include "file_names.h"
|
2003-03-17 23:35:00 +00:00
|
|
|
#include "shmem.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "client_state.h"
|
|
|
|
|
2002-08-22 21:29:58 +00:00
|
|
|
// Make a directory for each of the available slots specified
|
|
|
|
// in the client state
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::make_slot_dirs() {
|
|
|
|
unsigned 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
|
|
|
|
2002-08-22 21:29:58 +00:00
|
|
|
retval = exit_tasks();
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
fprintf(stderr, "error: CLIENT_STATE.exit: exit_tasks failed\n");
|
|
|
|
// 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-01-07 01:02:08 +00:00
|
|
|
fprintf(stderr, "error: CLIENT_STATE.exit: write_state_file failed\n");
|
|
|
|
// don't return here - we'll exit anyway
|
2002-11-20 20:14:48 +00:00
|
|
|
}
|
2002-08-22 21:29:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CLIENT_STATE::exit_tasks() {
|
2003-03-17 19:24:38 +00:00
|
|
|
// TODO: unsuspend active tasks so they have a chance to checkpoint
|
2003-03-11 22:18:01 +00:00
|
|
|
// Send a request to the tasks to exit
|
|
|
|
active_tasks.request_tasks_exit();
|
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
// Wait a second for them to exit normally, if they don't then kill them
|
|
|
|
if (active_tasks.wait_for_exit(1))
|
|
|
|
active_tasks.kill_tasks();
|
2002-12-11 23:09:55 +00:00
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
// Check their final CPU time
|
|
|
|
active_tasks.check_apps();
|
|
|
|
|
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;
|
|
|
|
char path[256];
|
2002-08-23 00:53:00 +00:00
|
|
|
int retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
for (i=0; i<rp->output_files.size(); i++) {
|
|
|
|
fip = rp->output_files[i].file_info;
|
2002-08-12 21:54:19 +00:00
|
|
|
fip->status = FILE_PRESENT;
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!fip->upload_when_present && !fip->sticky) {
|
|
|
|
fip->delete_file();
|
|
|
|
} else {
|
|
|
|
get_pathname(fip, path);
|
2002-08-23 00:53:00 +00:00
|
|
|
retval = md5_file(path, fip->md5_cksum, fip->nbytes);
|
2002-11-19 22:57:05 +00:00
|
|
|
if (retval) {
|
|
|
|
// an output file is unexpectedly absent.
|
|
|
|
//
|
|
|
|
fip->status = retval;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
// Detach from shared memory. In Windows, this is the same as
|
|
|
|
// destroying the shared mem since we're the last one attached
|
|
|
|
//
|
2003-03-17 23:35:00 +00:00
|
|
|
#ifdef _WIN32
|
2003-03-18 19:37:09 +00:00
|
|
|
if (at.app_client_shm)
|
|
|
|
detach_shmem(at.shm_handle, at.app_client_shm);
|
2003-03-17 23:35:00 +00:00
|
|
|
#else
|
|
|
|
if (at.app_client_shm)
|
|
|
|
detach_shmem(at.app_client_shm);
|
|
|
|
destroy_shmem(at.shm_key);
|
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
at.result->is_active = false;
|
2002-08-12 21:54:19 +00:00
|
|
|
at.result->state = RESULT_COMPUTE_DONE;
|
2002-04-30 22:22:54 +00:00
|
|
|
update_avg_cpu(at.result->project);
|
2002-08-05 00:29:34 +00:00
|
|
|
at.result->project->exp_avg_cpu += at.result->final_cpu_time;
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// poll status of existing apps and and clean up after them
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
bool CLIENT_STATE::handle_running_apps() {
|
|
|
|
unsigned int i;
|
|
|
|
ACTIVE_TASK* atp;
|
|
|
|
bool action = false;
|
2003-03-13 21:49:52 +00:00
|
|
|
char buf[256];
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
for (i=0; i<active_tasks.active_tasks.size(); i++) {
|
|
|
|
atp = active_tasks.active_tasks[i];
|
|
|
|
if (atp->state != PROCESS_RUNNING) {
|
2003-03-13 21:49:52 +00:00
|
|
|
sprintf(buf, "computation for result %s finished\n", atp->wup->name);
|
|
|
|
show_message(atp->wup->project, buf, MSG_INFO);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (log_flags.task_debug) {
|
|
|
|
printf(
|
|
|
|
"task finished; pid %d, status %d\n",
|
|
|
|
atp->pid, atp->exit_status
|
|
|
|
);
|
|
|
|
}
|
|
|
|
app_finished(*atp);
|
2003-01-07 22:49:42 +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;
|
|
|
|
avp = wup->avp;
|
|
|
|
for (i=0; i<avp->app_files.size(); i++) {
|
|
|
|
fip = avp->app_files[i].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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start new app if possible
|
|
|
|
//
|
|
|
|
bool CLIENT_STATE::start_apps() {
|
|
|
|
unsigned int i;
|
|
|
|
RESULT* rp;
|
|
|
|
ACTIVE_TASK* atp;
|
|
|
|
bool action = false;
|
2003-03-13 21:49:52 +00:00
|
|
|
int open_slot, retval;
|
|
|
|
char buf[256];
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
for (i=0; i<results.size(); i++) {
|
2002-08-28 21:50:51 +00:00
|
|
|
|
|
|
|
// If all the app slots are already used, we can't start a new app
|
|
|
|
//
|
2002-08-26 22:14:06 +00:00
|
|
|
open_slot = active_tasks.get_free_slot(nslots);
|
|
|
|
if (open_slot < 0) {
|
2003-01-07 22:49:42 +00:00
|
|
|
return action;
|
2002-06-21 06:52:47 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
rp = results[i];
|
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
|
|
|
|
//
|
2002-08-12 21:54:19 +00:00
|
|
|
if (rp->state == RESULT_FILES_DOWNLOADED && !rp->is_active ) {
|
2003-03-13 21:49:52 +00:00
|
|
|
if (log_flags.task) {
|
|
|
|
sprintf(buf, "starting computation for result %s\n", rp->name);
|
|
|
|
show_message(rp->project, buf, MSG_INFO);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
rp->is_active = true;
|
|
|
|
atp = new ACTIVE_TASK;
|
2002-08-26 22:14:06 +00:00
|
|
|
atp->slot = open_slot;
|
2002-04-30 22:22:54 +00:00
|
|
|
atp->init(rp);
|
2002-12-07 00:56:51 +00:00
|
|
|
retval = active_tasks.insert(atp);
|
2003-01-07 22:49:42 +00:00
|
|
|
|
|
|
|
// couldn't start process
|
|
|
|
//
|
|
|
|
if (retval) {
|
|
|
|
atp->state = PROCESS_COULDNT_START;
|
|
|
|
atp->result->active_task_state = PROCESS_COULDNT_START;
|
2003-03-13 21:49:52 +00:00
|
|
|
report_result_error(
|
2003-02-12 19:53:46 +00:00
|
|
|
*(atp->result), retval,
|
|
|
|
"Couldn't start the app for this result.\n"
|
|
|
|
);
|
2003-01-07 22:49:42 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
action = true;
|
2002-08-22 21:29:58 +00:00
|
|
|
set_client_state_dirty("start_apps");
|
2002-08-28 21:50:51 +00:00
|
|
|
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();
|
|
|
|
}
|