X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2FHpc.c;h=6d79f260d90487a1e6747c90a29119ff3d0b324f;hb=d50e93cf95b68bf858be82025b56c9977335ed76;hp=b1b26082946f46529480d4b855624150d5b08170;hpb=0df7dbb1b98d08521f177ade22615861752f8981;p=ghc-hetmet.git diff --git a/rts/Hpc.c b/rts/Hpc.c index b1b2608..6d79f26 100644 --- a/rts/Hpc.c +++ b/rts/Hpc.c @@ -2,14 +2,13 @@ * (c)2006 Galois Connections, Inc. */ -// #include "HsFFI.h" - #include #include #include #include #include #include "HsFFI.h" + #include "Rts.h" #include "Hpc.h" @@ -20,9 +19,13 @@ #define DEBUG_HPC 0 -static int hpc_inited = 0; // Have you started this component? -static FILE *tixFile; // file being read/written -static int tix_ch; // current char +static int hpc_inited = 0; // Have you started this component? +static FILE *tixFile; // file being read/written +static int tix_ch; // current char +static StgWord64 magicTixNumber; // Magic/Hash number to mark .tix files + +static int hpc_ticks_inited = 0; // Have you started the dynamic external ticking? +static FILE *rixFile; // The tracer file/pipe typedef struct _Info { char *modName; // name of module @@ -32,6 +35,13 @@ typedef struct _Info { struct _Info *next; } Info; +// This is a cruel hack, we should completely redesign the format specifier handling in the RTS. +#if SIZEOF_LONG == 8 +#define PRIuWORD64 "lu" +#else +#define PRIuWORD64 "llu" +#endif + Info *modules = 0; Info *nextModule = 0; StgWord64 *tixBoxes = 0; // local copy of tixBoxes array, from file. @@ -117,7 +127,7 @@ static void hpc_init(void) { expect('i'); expect('x'); ws(); - expectWord64(); + magicTixNumber = expectWord64(); ws(); expect('['); ws(); @@ -149,7 +159,8 @@ static void hpc_init(void) { if (tix_ch == ',') { expect(','); ws(); - }} + } + } expect(']'); ws(); tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64)); @@ -166,6 +177,9 @@ static void hpc_init(void) { expect(']'); fclose(tixFile); + } else { + // later, we will find a binary specific + magicTixNumber = (StgWord64)0; } } @@ -174,10 +188,11 @@ static void hpc_init(void) { * of the tix file, or all zeros. */ -void +int hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) { Info *tmpModule, *lastModule; int i; + int offset = 0; #if DEBUG_HPC printf("hs_hpc_module(%s,%d)\n",modName,modCount); @@ -199,7 +214,7 @@ hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) { for(i=0;i < modCount;i++) { tixArr[i] = tixBoxes[i + tmpModule->tickOffset]; } - return; + return tmpModule->tickOffset; } lastModule = tmpModule; } @@ -227,6 +242,80 @@ hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) { #if DEBUG_HPC printf("end: hs_hpc_module\n"); #endif + return offset; +} + + +/* + * Called on *every* exception thrown + */ +void +hs_hpc_throw() { + // Assumes that we have had at least *one* tick first. + // All exceptions before the first tick are not reported. + // The only time this might be an issue is in bootstrapping code, + // so this is a feature. + if (hpc_inited != 0 && hpc_ticks_inited != 0) { + fprintf(rixFile,"Throw\n"); + } +} + +/* Called on every tick + */ + +void +hs_hpc_tick(int globIx) { + int threadId = 0; // for now, assume single thread + // TODO: work out how to get the thread Id to here. + + +#if DEBUG_HPC && DEBUG + printf("hs_hpc_tick(%d)\n",globIx); +#endif + if (!hpc_ticks_inited) { + char* trace_filename; + int comma; + Info *tmpModule; + + assert(hpc_inited); + hpc_ticks_inited = 1; + + trace_filename = (char *) malloc(strlen(prog_name) + 6); + sprintf(trace_filename, "%s.rix", prog_name); + rixFile = fopen(trace_filename,"w+"); + + comma = 0; + + fprintf(rixFile,"START %s\n",prog_name); + fprintf(rixFile,"["); + tmpModule = modules; + for(;tmpModule != 0;tmpModule = tmpModule->next) { + if (comma) { + fprintf(rixFile,","); + } else { + comma = 1; + } + fprintf(rixFile,"(\"%s\",%u)", + tmpModule->modName, + tmpModule->tickCount); +#if DEBUG_HPC + fprintf(stderr,"(tracer)%s: %u (offset=%u)\n", + tmpModule->modName, + tmpModule->tickCount, + tmpModule->tickOffset); +#endif + } + fprintf(rixFile,"]\n"); + fflush(rixFile); + } + assert(rixFile != 0); + + fprintf(rixFile,"%d\n",globIx); + +#if DEBUG_HPC + printf("end: hs_hpc_tick\n"); +#endif + } /* This is called after all the modules have registered their local tixboxes, @@ -258,6 +347,7 @@ startupHpc(void) { } } + /* Called at the end of execution, to write out the Hpc *.tix file * for this exection. Safe to call, even if coverage is not used. */ @@ -278,7 +368,7 @@ exitHpc(void) { comma = 0; - fprintf(f,"Tix 0 ["); + fprintf(f,"Tix %" PRIuWORD64 " [", magicTixNumber); tmpModule = modules; for(;tmpModule != 0;tmpModule = tmpModule->next) { if (comma) { @@ -286,11 +376,11 @@ exitHpc(void) { } else { comma = 1; } - fprintf(f,"(\"%s\",%d)", + fprintf(f,"(\"%s\",%u)", tmpModule->modName, tmpModule->tickCount); #if DEBUG_HPC - fprintf(stderr,"%s: %d (offset=%d)\n", + fprintf(stderr,"%s: %u (offset=%u)\n", tmpModule->modName, tmpModule->tickCount, tmpModule->tickOffset); @@ -314,7 +404,7 @@ exitHpc(void) { } if (tmpModule->tixArr) { - fprintf(f,"%lld",tmpModule->tixArr[i]); + fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]); } else { fprintf(f,"0"); } @@ -324,6 +414,10 @@ exitHpc(void) { fprintf(f,"]\n"); fclose(f); + + if (hpc_ticks_inited && rixFile != 0) { + fclose(rixFile); + } }