Mercurial > hg > plml
changeset 12:85c19a49cf7e
Trying to debug hang in triserver.
author | samer |
---|---|
date | Mon, 30 Jan 2012 23:10:44 +0000 |
parents | 5e2967a54316 |
children | 440734a35533 |
files | cpp/plml.cpp prolog/plml.pl |
diffstat | 2 files changed, 45 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/cpp/plml.cpp Thu Jan 26 13:29:05 2012 +0000 +++ b/cpp/plml.cpp Mon Jan 30 23:10:44 2012 +0000 @@ -79,6 +79,7 @@ #include <SWI-cpp.h> #include <stdio.h> +#include <unistd.h> #include "engine.h" /* The maximum number of simultaneous connections to Matlab from one @@ -87,7 +88,7 @@ #define BUFSIZE 32768 // buffer for matlab output #define MAXCMDLEN 256 // #define EVALFMT "t__ex=[];\ntry\n%s\ncatch t__ex\ndisp(getReport(t__ex))\nend" -#define EVALFMT "lasterr(''); %s\nt__ex=lasterr;" +#define EVALFMT "lasterr(''); %s\nt__ex=lasterr;if ~isempty(t__ex), beep; end" using namespace std; @@ -532,7 +533,9 @@ foreign_t mlWSGet(term_t var, term_t val) { try { struct wsvar *x = term_to_wsvar(var); + printf("-- Entering mlWSGET \r"); fflush(stdout); mxArray *p = engGetVariable(x->engine, x->name); + printf("-- Leaving mlWSGET \r"); fflush(stdout); return PL_unify_blob(val, (void **)&p, sizeof(p), &mx_blob); } catch (PlException &e) { return e.plThrow(); @@ -557,7 +560,7 @@ // Call a Matlab engine to execute the given command foreign_t mlExec(term_t engine, term_t cmd) { -// printf(">>> Entering mlEXEC\n"); + printf("-- Entering mlEXEC \r"); fflush(stdout); try { eng *eng=findEngine(engine); const char *cmdstr=PlTerm(cmd); @@ -568,9 +571,7 @@ // if string is very long, send it via local mxArray if (cmdlen>MAXCMDLEN) { mxArray *mxcmd=mxCreateString(cmdstr); -// printf(" >>> Putting command\n"); engPutVariable(eng->ep,"t__cmd",mxcmd); -// printf(" <<< Put command ok\n"); mxDestroyArray(mxcmd); cmdstr="eval(t__cmd)"; cmdlen=strlen(cmdstr); @@ -579,30 +580,53 @@ eval_cmd = new char[cmdlen+strlen(EVALFMT)-1]; if (eval_cmd==NULL) throw PlException("Failed to allocate memory for command"); sprintf(eval_cmd, EVALFMT, cmdstr); -// printf(" >>> Calling Matlab engine...\n"), + printf("-- Calling Matlab engine... \r"); fflush(stdout); rc=engEvalString(eng->ep,eval_cmd); -// printf(" <<< Returned from Matlab engine...\n"), + printf("-- Returned from Matlab engine... \r"); fflush(stdout); delete [] eval_cmd; - if (rc) { - // printf("*** MATLAB evaluation error. Output buffer contains:\n"), - // fputs(eng->outbuf,stdout); - // printf("*** throwing exception.\n"); - // throw PlException("MATLAB evaluation error"); + if (rc) printf("** MATLAB evaluation error. Output buffer contains:\n"); + // write whatever is in the output buffer now. + fputs(eng->outbuf,stdout); + + if (PL_handle_signals()<0) { + printf("** ml_exec: evaluation interrupted by signal.\n"); + return FALSE; } + /* { + int rc; + printf( "--- sleeping for 3 seconds\n"); + rc=usleep(3000000); + //printf("press return.\n"); + //rc=getchar(); + printf( "--- returned %d.\n",rc); + } */ - // write whatever is in the output buffer now. - fputs(eng->outbuf,stdout); - // SA 2010. Giving up any pretence of being thread-safe - // each engine is to be used by one Prolog thread ONLY. // If you want fancy threading stuff, do it in Prolog. + -// printf(" >>> Getting variable\n"); + printf("-- Getting last error... \r"); fflush(stdout); mxArray *lasterr = engGetVariable(eng->ep, "t__ex"); -// printf(" <<< Got variable\n"); -// if (!lasterr) throw PlException("Failed to get status information."); + printf("-- Got last error (%p) \r",lasterr); fflush(stdout); + +// if (PL_handle_signals()<0) { +// printf(" * ml_exec: getting lasterr interrupted by signal.\n"); +// return FALSE; +// } + + if (!lasterr) { + printf("** Failing due to inability to get lasterr.\n"); + return FALSE; + + //PlTerm msg=PlTerm("Failed to get Matlab lasterr."); + //printf("Preparing lasterr=0 exception.\n"); + //PlException ex=PlException(msg); + //printf("Throwing lasterr=0 exception.\n"); + //throw ex; + } if (mxGetNumberOfElements(lasterr)>0) { //char *string=mxArrayToString(mxGetField(lasterr,0,"message")); @@ -622,9 +646,10 @@ } else mxDestroyArray(lasterr); // if we've got this far, then everything went well, so -// printf("<<< Returning from mlEXEC\n"); - PL_succeed; + printf("-- Returning from mlEXEC \r"); fflush(stdout); + return TRUE; } catch (PlException &e) { + printf("** Throwing exception\n"); return e.plThrow(); } }
--- a/prolog/plml.pl Thu Jan 26 13:29:05 2012 +0000 +++ b/prolog/plml.pl Mon Jan 30 23:10:44 2012 +0000 @@ -338,7 +338,7 @@ % even when multiple values are required. ml_eval(Id,X,Types,Vals) :- maplist(alloc_ws(Id),Types,Vars), - ml_exec(Id,hide(wsx(Vars)=X)), + ml_exec(Id,hide(wsx(Vars)=X)), flush_output, maplist(convert_ws,Types,Vars,Vals). alloc_ws(I,_,Z) :- mlWSALLOC(I,Z).