2 * (c)2006 Galois Connections, Inc.
15 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
22 static int hpc_inited = 0; // Have you started this component?
23 static FILE *tixFile; // file being read/written
24 static int tix_ch; // current char
25 static StgWord64 magicTixNumber; // Magic/Hash number to mark .tix files
27 static FILE *rixFile = NULL; // The tracer file/pipe
29 typedef struct _Info {
30 char *modName; // name of module
31 int tickCount; // number of ticks
32 int tickOffset; // offset into a single large .tix Array
33 StgWord64 *tixArr; // tix Array from the program execution (local for this module)
37 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
39 #define PRIuWORD64 "lu"
41 #define PRIuWORD64 "llu"
46 StgWord64 *tixBoxes = 0; // local copy of tixBoxes array, from file.
47 int totalTixes = 0; // total number of tix boxes.
51 static char *tixFilename;
53 static void failure(char *msg) {
54 printf("Hpc failure: %s\n",msg);
55 printf("(perhaps remove .tix file?)\n");
60 static int init_open(char *filename)
62 tixFile = fopen(filename,"r");
66 tix_ch = getc(tixFile);
70 static void expect(char c) {
72 printf("Hpc: parse failed (%c,%c)\n",tix_ch,c);
75 tix_ch = getc(tixFile);
78 static void ws(void) {
79 while (tix_ch == ' ') {
80 tix_ch = getc(tixFile);
84 static char *expectString(void) {
88 while (tix_ch != '"') {
89 tmp[tmp_ix++] = tix_ch;
90 tix_ch = getc(tixFile);
99 static StgWord64 expectWord64(void) {
101 while (isdigit(tix_ch)) {
102 tmp = tmp * 10 + (tix_ch -'0');
103 tix_ch = getc(tixFile);
108 static void hpc_init(void) {
112 if (hpc_inited != 0) {
118 tixFilename = (char *) malloc(strlen(prog_name) + 6);
119 sprintf(tixFilename, "%s.tix", prog_name);
121 if (init_open(tixFilename)) {
129 magicTixNumber = expectWord64();
133 while(tix_ch != ']') {
134 tmpModule = (Info *)calloc(1,sizeof(Info));
137 tmpModule -> modName = expectString();
141 tmpModule -> tickCount = (int)expectWord64();
146 tmpModule -> tickOffset = totalTixes;
147 totalTixes += tmpModule -> tickCount;
149 tmpModule -> tixArr = 0;
154 nextModule->next=tmpModule;
156 nextModule=tmpModule;
165 tixBoxes = (StgWord64 *)calloc(totalTixes,sizeof(StgWord64));
168 for(i = 0;i < totalTixes;i++) {
173 tixBoxes[i] = expectWord64();
180 // later, we will find a binary specific
181 magicTixNumber = (StgWord64)0;
185 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
186 * This memory can be uninitized, because we will initialize it with either the contents
187 * of the tix file, or all zeros.
191 hs_hpc_module(char *modName,int modCount,StgWord64 *tixArr) {
192 Info *tmpModule, *lastModule;
197 printf("hs_hpc_module(%s,%d)\n",modName,modCount);
205 for(;tmpModule != 0;tmpModule = tmpModule->next) {
206 if (!strcmp(tmpModule->modName,modName)) {
207 if (tmpModule->tickCount != modCount) {
208 failure("inconsistent number of tick boxes");
210 assert(tmpModule->tixArr == 0);
211 assert(tixBoxes != 0);
212 tmpModule->tixArr = tixArr;
213 for(i=0;i < modCount;i++) {
214 tixArr[i] = tixBoxes[i + tmpModule->tickOffset];
216 return tmpModule->tickOffset;
218 lastModule = tmpModule;
220 // Did not find entry so add one on.
221 tmpModule = (Info *)calloc(1,sizeof(Info));
222 tmpModule->modName = modName;
223 tmpModule->tickCount = modCount;
225 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
227 tmpModule->tickOffset = 0;
229 tmpModule->tixArr = tixArr;
230 for(i=0;i < modCount;i++) {
238 lastModule->next=tmpModule;
242 printf("end: hs_hpc_module\n");
247 static StgThreadID previous_tid = 0;
250 send_ThreadId(StgTSO *current_tso) {
251 // This assumes that there is no real thread 0.
252 StgThreadID tid = (current_tso == 0) ? 0 : current_tso->id;
253 if (tid != previous_tid) {
255 // How do we print StgWord32's without a cast?
256 fprintf(rixFile,"Thread Switch %d\n",(unsigned int)tid);
261 * Called on *every* exception thrown
264 hs_hpc_event(char *msg,StgTSO *current_tso) {
265 // Assumes that we have had at least *one* tick first.
266 // All exceptions before the first tick are not reported.
267 // The only time this might be an issue is in bootstrapping code,
268 // so this is a feature.
270 // This is called on *every* exception, even when Hpc is not enabled.
272 if (rixFile != NULL) {
273 assert(hpc_inited != 0);
274 send_ThreadId(current_tso);
275 fprintf(rixFile,"%s\n",msg);
279 /* Called on every tick, dynamically to our file record of program execution
283 hs_hpc_tick(int globIx, StgTSO *current_tso) {
284 #if DEBUG_HPC && DEBUG
285 printf("hs_hpc_tick(%d)\n",globIx);
287 assert(hpc_inited != 0);
288 if (rixFile != NULL) {
289 send_ThreadId(current_tso);
290 fprintf(rixFile,"%d\n",globIx);
294 printf("end: hs_hpc_tick\n");
299 /* This is called after all the modules have registered their local tixboxes,
300 * and does a sanity check: are we good to go?
308 printf("startupHpc\n");
311 if (hpc_inited == 0) {
318 for(;tmpModule != 0;tmpModule = tmpModule->next) {
319 if (!tmpModule->tixArr) {
320 fprintf(stderr,"error: module %s did not register any hpc tick data\n",
322 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
328 // HPCRIX contains the name of the file to send our dynamic runtime output to.
329 // This might be a real file, or perhaps a named pipe.
330 hpcRix = getenv("HPCRIX");
337 rixFile = fopen(hpcRix,"w");
341 fprintf(rixFile,"Starting %s\n",prog_name);
342 fprintf(rixFile,"[");
344 for(;tmpModule != 0;tmpModule = tmpModule->next) {
346 fprintf(rixFile,",");
350 fprintf(rixFile,"(\"%s\",%u)",
352 tmpModule->tickCount);
354 fprintf(stderr,"(tracer)%s: %u (offset=%u)\n",
356 tmpModule->tickCount,
357 tmpModule->tickOffset);
360 fprintf(rixFile,"]\n");
367 /* Called at the end of execution, to write out the Hpc *.tix file
368 * for this exection. Safe to call, even if coverage is not used.
379 if (hpc_inited == 0) {
383 FILE *f = fopen(tixFilename,"w");
387 fprintf(f,"Tix %" PRIuWORD64 " [", magicTixNumber);
389 for(;tmpModule != 0;tmpModule = tmpModule->next) {
395 fprintf(f,"(\"%s\",%u)",
397 tmpModule->tickCount);
399 fprintf(stderr,"%s: %u (offset=%u)\n",
401 tmpModule->tickCount,
402 tmpModule->tickOffset);
409 for(;tmpModule != 0;tmpModule = tmpModule->next) {
410 if (!tmpModule->tixArr) {
411 fprintf(stderr,"warning: module %s did not register any hpc tick data\n",
415 for(i = 0;i < tmpModule->tickCount;i++) {
422 if (tmpModule->tixArr) {
423 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
434 if (rixFile != NULL) {
435 fprintf(rixFile,"Finished\n");