X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/40dc499e35463c28c4572e077206864092452e6a..29493aabe8625e8359c877d61b143c3f98bb1072:/tools/tesh/run_context.c diff --git a/tools/tesh/run_context.c b/tools/tesh/run_context.c index b234a084ac..130c7f969e 100644 --- a/tools/tesh/run_context.c +++ b/tools/tesh/run_context.c @@ -17,7 +17,7 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh); xbt_dynar_t bg_jobs = NULL; rctx_t armageddon_initiator = NULL; -xbt_mutex_t armageddon_mutex = NULL; +xbt_os_mutex_t armageddon_mutex = NULL; /* * Module management @@ -26,67 +26,76 @@ xbt_mutex_t armageddon_mutex = NULL; static void kill_it(void*r) { rctx_t rctx = *(rctx_t*)r; - VERB1("Join thread %p which were running a background cmd",rctx->runner); - xbt_thread_join(rctx->runner,NULL); + VERB2("Join thread %p which were running background cmd <%s>",rctx->runner,rctx->filepos); + xbt_os_thread_join(rctx->runner,NULL); rctx_free(rctx); } void rctx_init(void) { - bg_jobs = xbt_dynar_new(sizeof(rctx_t),kill_it); - armageddon_mutex = xbt_mutex_init(); + bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it); + armageddon_mutex = xbt_os_mutex_init(); armageddon_initiator = NULL; } void rctx_exit(void) { - if (bg_jobs) + if (bg_jobs) { + /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */ + while (xbt_dynar_length(bg_jobs)) { + rctx_t rctx; + xbt_dynar_pop(bg_jobs,&rctx); + kill_it(&rctx); + } xbt_dynar_free(&bg_jobs); - xbt_mutex_destroy(armageddon_mutex); + } + xbt_os_mutex_destroy(armageddon_mutex); } void rctx_wait_bg(void) { - xbt_dynar_free(&bg_jobs); - bg_jobs = xbt_dynar_new(sizeof(rctx_t),kill_it); + if (bg_jobs) { + /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */ + while (xbt_dynar_length(bg_jobs)) { + rctx_t rctx; + xbt_dynar_pop(bg_jobs,&rctx); + kill_it(&rctx); + } + xbt_dynar_free(&bg_jobs); + } + bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it); } void rctx_armageddon(rctx_t initiator, int exitcode) { rctx_t rctx; - int cpt; - xbt_mutex_lock(armageddon_mutex); + DEBUG2("Armageddon request by <%s> (exit=%d)",initiator->filepos,exitcode); + xbt_os_mutex_lock(armageddon_mutex); if (armageddon_initiator != NULL) { VERB0("Armageddon already started. Let it go"); + xbt_os_mutex_unlock(initiator->interruption); + xbt_os_mutex_unlock(armageddon_mutex); return; } + DEBUG1("Armageddon request by <%s> got the lock. Let's go amok",initiator->filepos); armageddon_initiator = initiator; - xbt_mutex_unlock(armageddon_mutex); - + xbt_os_mutex_unlock(armageddon_mutex); /* Kill any background commands */ - xbt_dynar_foreach(bg_jobs,cpt,rctx) { + while (xbt_dynar_length(bg_jobs)) { + xbt_dynar_pop(bg_jobs,&rctx); if (rctx != initiator) { - xbt_mutex_lock(rctx->interruption); - rctx->interrupted = 1; INFO2("Kill <%s> because <%s> failed",rctx->filepos,initiator->filepos); + xbt_os_mutex_lock(rctx->interruption); + rctx->interrupted = 1; + xbt_os_mutex_unlock(rctx->interruption); + INFO2("Do Kill <%s> because <%s> failed",rctx->filepos,initiator->filepos); if (!rctx->reader_done) { kill(rctx->pid,SIGTERM); usleep(100); kill(rctx->pid,SIGKILL); } - xbt_mutex_unlock(rctx->interruption); } } - /* Remove myself from the tasks */ - if (xbt_dynar_member(bg_jobs, &initiator)) { - int mypos = xbt_dynar_search(bg_jobs, &initiator); - rctx_t myself; - xbt_dynar_remove_at(bg_jobs,mypos,&myself); - // rctx_free(myself); - } - - /* Cleanup the place */ - xbt_dynar_free(&bg_jobs); - + VERB0("Shut everything down!"); exit(exitcode); } @@ -119,7 +128,7 @@ rctx_t rctx_new() { res->input=buff_new(); res->output_wanted=buff_new(); res->output_got=buff_new(); - res->interruption = xbt_mutex_init(); + res->interruption = xbt_os_mutex_init(); rctx_empty(res); return res; } @@ -134,7 +143,7 @@ void rctx_free(rctx_t rctx) { free(rctx->cmd); if (rctx->filepos) free(rctx->filepos); - xbt_mutex_destroy(rctx->interruption); + xbt_os_mutex_destroy(rctx->interruption); buff_free(rctx->input); buff_free(rctx->output_got); buff_free(rctx->output_wanted); @@ -168,6 +177,7 @@ void rctx_pushline(const char* filepos, char kind, char *line) { filepos,rctx->cmd); ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name); rctx_armageddon(rctx,1); + return; } rctx_start(); VERB1("[%s] More than one command in this chunk of lines",filepos); @@ -200,8 +210,11 @@ void rctx_pushline(const char* filepos, char kind, char *line) { if (rctx->cmd) rctx_start(); - if (!strncmp(line,"set timeout ",strlen("set timeout "))) { - timeout_value=atoi(line+strlen("set timeout")); + if (!strncmp(line,"timeout no",strlen("timeout no"))) { + VERB1("[%s] (disable timeout)", filepos); + timeout_value = -1; + } else if (!strncmp(line,"timeout ",strlen("timeout "))) { + timeout_value=atoi(line+strlen("timeout")); VERB2("[%s] (new timeout value: %d)", filepos,timeout_value); @@ -228,6 +241,7 @@ void rctx_pushline(const char* filepos, char kind, char *line) { ERROR2("%s: Malformed metacommand: %s",filepos,line); ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name); rctx_armageddon(rctx,1); + return; } break; } @@ -256,6 +270,7 @@ static void* thread_writer(void *r) { perror("Error while writing input to child"); ERROR1("Test suite `%s': NOK (system error)",testsuite_name); rctx_armageddon(rctx,4); + return NULL; } } DEBUG1("written %d chars so far",posw); @@ -280,6 +295,7 @@ static void *thread_reader(void *r) { perror("Error while reading output of child"); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); rctx_armageddon(rctx,4); + return NULL; } if (posr>0) { buffout[posr]='\0'; @@ -296,6 +312,7 @@ static void *thread_reader(void *r) { perror(bprintf("Cannot wait for the child %s",rctx->cmd)); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); rctx_armageddon(rctx,4); + return NULL; } rctx->reader_done = 1; @@ -322,6 +339,7 @@ void rctx_start(void) { perror("Cannot fork the command"); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); rctx_armageddon(rctx,4); + return; } if (rctx->pid) { /* father */ @@ -331,11 +349,14 @@ void rctx_start(void) { close(child_out[1]); rctx->child_from = child_out[0]; - rctx->end_time = time(NULL) + timeout_value; + if (timeout_value > 0) + rctx->end_time = time(NULL) + timeout_value; + else + rctx->end_time = -1; rctx->reader_done = 0; - rctx->reader = xbt_thread_create(thread_reader,(void*)rctx); - rctx->writer = xbt_thread_create(thread_writer,(void*)rctx); + rctx->reader = xbt_os_thread_create("reader",thread_reader,(void*)rctx); + rctx->writer = xbt_os_thread_create("writer",thread_writer,(void*)rctx); } else { /* child */ @@ -358,13 +379,13 @@ void rctx_start(void) { } else { /* Damn. Copy the rctx and launch a thread to handle it */ rctx_t old = rctx; - xbt_thread_t runner; + xbt_os_thread_t runner; rctx = rctx_new(); DEBUG2("RCTX: new bg=%p, new fg=%p",old,rctx); DEBUG2("Launch a thread to wait for %s %d",old->cmd,old->pid); - runner = xbt_thread_create(rctx_wait,(void*)old); + runner = xbt_os_thread_create(old->cmd,rctx_wait,(void*)old); old->runner = runner; VERB3("Launched thread %p to wait for %s %d", runner,old->cmd, old->pid); @@ -375,7 +396,7 @@ void rctx_start(void) { /* Waits for the child to end (or to timeout), and check its ending conditions. This is launched from rctx_start but either in main thread (for foreground jobs) or in a separate one for background jobs. - That explains the prototype, forced by xbt_thread_create. */ + That explains the prototype, forced by xbt_os_thread_create. */ void *rctx_wait(void* r) { rctx_t rctx = (rctx_t)r; @@ -389,14 +410,13 @@ void *rctx_wait(void* r) { rctx->cmd); /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */ - while (!rctx->interrupted && !rctx->reader_done && rctx->end_time >= now) { + while (!rctx->interrupted && !rctx->reader_done && (rctx->end_time <0 ||rctx->end_time >= now)) { usleep(100); now = time(NULL); } - xbt_mutex_lock(rctx->interruption); - - if (!rctx->interrupted && rctx->end_time < now) { + xbt_os_mutex_lock(rctx->interruption); + if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) { INFO1("<%s> timeouted. Kill the process.",rctx->filepos); rctx->timeout = 1; kill(rctx->pid,SIGTERM); @@ -408,13 +428,13 @@ void *rctx_wait(void* r) { /* Make sure helper threads die. Cannot block since they wait for the child we just killed if not already dead. */ - xbt_thread_join(rctx->writer,NULL); - xbt_thread_join(rctx->reader,NULL); + xbt_os_thread_join(rctx->writer,NULL); + xbt_os_thread_join(rctx->reader,NULL); - /* xbt_mutex_unlock(rctx->interruption); + /* xbt_os_mutex_unlock(rctx->interruption); if (rctx->interrupted) return NULL; - xbt_mutex_lock(rctx->interruption);*/ + xbt_os_mutex_lock(rctx->interruption);*/ buff_chomp(rctx->output_got); buff_chomp(rctx->output_wanted); @@ -435,8 +455,11 @@ void *rctx_wait(void* r) { rctx->filepos); ERROR3("Test suite `%s': NOK (<%s> timeout after %d sec)", testsuite_name,rctx->filepos,timeout_value); - if (!rctx->interrupted) + DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted); + if (!rctx->interrupted) { rctx_armageddon(rctx, 3); + return NULL; + } } DEBUG2("RCTX=%p (pid=%d)",rctx,rctx->pid); @@ -491,8 +514,7 @@ void *rctx_wait(void* r) { || strcmp(rctx->output_got->data, rctx->output_wanted->data))) { if (XBT_LOG_ISENABLED(tesh,xbt_log_priority_info)) { char *diff= xbt_str_diff(rctx->output_wanted->data,rctx->output_got->data); - ERROR1("Output mismatch:\n%s", - diff); + ERROR2("Output of <%s> mismatch:\n%s",rctx->filepos,diff); free(diff); } ERROR2("Test suite `%s': NOK (<%s> output mismatch)", @@ -500,7 +522,7 @@ void *rctx_wait(void* r) { errcode=2; } else if (rctx->output == e_output_ignore) { - INFO0("(ignoring the output as requested)"); + INFO1("(ignoring the output of <%s> as requested)",rctx->filepos); } else if (rctx->output == e_output_display) { xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n"); char *out = xbt_str_join(a,"\n||"); @@ -520,11 +542,13 @@ void *rctx_wait(void* r) { rctx_empty(rctx); } if (errcode) { - if (!rctx->interrupted) + if (!rctx->interrupted) { rctx_armageddon(rctx, errcode); + return NULL; + } } - xbt_mutex_unlock(rctx->interruption); + xbt_os_mutex_unlock(rctx->interruption); return NULL; }