From 89e2644dfe0affa7f9425ed841a70a00a15b624a Mon Sep 17 00:00:00 2001 From: gombasg Date: Tue, 8 Sep 2009 15:28:03 +0000 Subject: [PATCH] Add the missing API functions to the local backend git-svn-id: svn+ssh://cvs.lpds.sztaki.hu/var/lib/svn/szdg/dcapi/trunk@2246 a7169a2c-3604-0410-bc95-c702d8d87f7a --- dcapi/debian/changelog | 6 ++++ dcapi/local/local_master.c | 32 ++++++++++++++++-- dcapi/local/local_master.h | 5 ++- dcapi/local/local_result.c | 68 +++++++++++++++++++++++++++++++------- 4 files changed, 95 insertions(+), 16 deletions(-) diff --git a/dcapi/debian/changelog b/dcapi/debian/changelog index f4b033ba51..af5bc772bc 100644 --- a/dcapi/debian/changelog +++ b/dcapi/debian/changelog @@ -1,3 +1,9 @@ +dcapi (0.9-25) unstable; urgency=low + + * Add some missing API functions for the local backend. + + -- Gábor Gombás Tue, 08 Sep 2009 17:27:04 +0200 + dcapi (0.9-24) unstable; urgency=low * Add symbols files support for the shared libraries diff --git a/dcapi/local/local_master.c b/dcapi/local/local_master.c index 68191bf06d..fcb51f590e 100644 --- a/dcapi/local/local_master.c +++ b/dcapi/local/local_master.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -33,9 +34,9 @@ * Global variables */ -DC_ResultCallback _DC_result_callback/*_dc_resultcb*/; -DC_SubresultCallback _DC_subresult_callback/*_dc_subresultcb*/; -DC_MessageCallback _DC_message_callback/*_dc_messagecb*/; +DC_ResultCallback _DC_result_callback; +DC_SubresultCallback _DC_subresult_callback; +DC_MessageCallback _DC_message_callback; char project_uuid_str[37]; uuid_t project_uuid; @@ -128,6 +129,21 @@ void DC_setMasterCb(DC_ResultCallback resultcb, DC_SubresultCallback subresultcb _DC_message_callback = msgcb; } +void DC_setResultCb(DC_ResultCallback cb) +{ + _DC_result_callback = cb; +} + +void DC_setSubresultCb(DC_SubresultCallback cb) +{ + _DC_subresult_callback = cb; +} + +void DC_setMessageCb(DC_MessageCallback cb) +{ + _DC_message_callback = cb; +} + /******************************************************************** * Functions @@ -616,6 +632,16 @@ int DC_submitWU(DC_Workunit *wu) return DC_OK; } +int DC_cancelWU(DC_Workunit *wu) +{ + if (!wu || wu->state != DC_WU_RUNNING) + return DC_ERR_BADPARAM; + + kill(wu->pid, SIGTERM); + wu->state = DC_WU_ABORTED; + return 0; +} + DC_Workunit *_DC_getWUByName(const char *name) { DC_Workunit *wu; diff --git a/dcapi/local/local_master.h b/dcapi/local/local_master.h index 19d7b5f005..47747c85e8 100644 --- a/dcapi/local/local_master.h +++ b/dcapi/local/local_master.h @@ -17,6 +17,8 @@ extern "C" { #endif +#include + #include #include #include @@ -63,7 +65,7 @@ struct _DC_Workunit char *uuid_str; DC_WUState state; char *workdir; - int pid; + pid_t pid; /* Input file definitions. Elements are of type DC_LogicalFile */ GList *input_files; @@ -82,6 +84,7 @@ struct _DC_Result DC_Workunit *wu; int exit_code; + double cpu_time; /* List of output files. Elements are of type DC_PhysicalFile */ GList *output_files; diff --git a/dcapi/local/local_result.c b/dcapi/local/local_result.c index 18d1d3589e..935fdc6341 100644 --- a/dcapi/local/local_result.c +++ b/dcapi/local/local_result.c @@ -104,28 +104,72 @@ DC_Workunit *DC_getResultWU(DC_Result *result) return result->wu; } +static int wait_result(DC_Result *result) +{ + struct rusage rusage; + pid_t pid; + + if (!result->wu->pid) + return 0; + + pid = wait4(result->wu->pid, &result->exit_code, WNOHANG, &rusage); + if (pid != result->wu->pid) + { + DC_log(LOG_ERR, "wu with pid %d not exited according to waitpid. retval: %d", + result->wu->pid, pid); + return DC_ERR_SYSTEM; + } + + result->cpu_time = rusage.ru_utime.tv_sec + + (double)rusage.ru_utime.tv_usec / 1000000 + + rusage.ru_stime.tv_sec + + (double)rusage.ru_stime.tv_usec / 1000000; + + /* Make sure we do not call wait4() again */ + result->wu->pid = 0; + + return 0; +} + int DC_getResultExit(const DC_Result *result) { - int status; - pid_t retval, pid; + int ret; - pid = (pid_t)result->wu->pid; - - retval = waitpid(pid, &status, WNOHANG); - if (retval != pid) + if (!result) { - DC_log(LOG_ERR, "DC_getResultExit: wu with pid %d not exited according to waitpid. retval: %d", - pid, retval); + DC_log(LOG_ERR, "%s: Missing result", __func__); + return 0.0; + } + + ret = wait_result((DC_Result *)result); /* XXX */ + if (ret) + return -1; + + if (!WIFEXITED(result->exit_code)) + { + DC_log(LOG_WARNING, "DC_getResultExit: wu with pid %d terminated not normally.", + result->wu->pid); /* XXX pid is 0 at this point */ return -1; } - if (!WIFEXITED(status)) + return WEXITSTATUS(result->exit_code); +} + +double DC_getResultCPUTime(const DC_Result *result) +{ + int ret; + + if (!result) { - DC_log(LOG_WARNING, "DC_getResultExit: wu with pid %d terminated not normally.", pid); - return -1; + DC_log(LOG_ERR, "%s: Missing result", __func__); + return 0.0; } - return WEXITSTATUS(status); + ret = wait_result((DC_Result *)result); /* XXX */ + if (ret) + return -1; + + return result->cpu_time; } char *DC_getResultOutput(const DC_Result *result, const char *logicalFileName)