2 * (c)2006 Galois Connections, Inc.
20 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
27 static int hpc_inited = 0; // Have you started this component?
28 static FILE *tixFile; // file being read/written
29 static int tix_ch; // current char
31 typedef struct _HpcModuleInfo {
32 char *modName; // name of module
33 int tickCount; // number of ticks
34 int tickOffset; // offset into a single large .tix Array
35 int hashNo; // Hash number for this module's mix info
36 StgWord64 *tixArr; // tix Array from the program execution (local for this module)
37 struct _HpcModuleInfo *next;
40 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
42 #define PRIuWORD64 "lu"
44 #define PRIuWORD64 "llu"
47 HpcModuleInfo *modules = 0;
48 HpcModuleInfo *nextModule = 0;
49 int totalTixes = 0; // total number of tix boxes.
51 static char *tixFilename;
53 static void failure(char *msg) {
54 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
55 fprintf(stderr,"Hpc failure: %s\n",msg);
57 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
59 fprintf(stderr,"(perhaps remove .tix file?)\n");
64 static int init_open(FILE *file) {
69 tix_ch = getc(tixFile);
73 static void expect(char c) {
75 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
76 failure("parse error when reading .tix file");
78 tix_ch = getc(tixFile);
81 static void ws(void) {
82 while (tix_ch == ' ') {
83 tix_ch = getc(tixFile);
87 static char *expectString(void) {
91 while (tix_ch != '"') {
92 tmp[tmp_ix++] = tix_ch;
93 tix_ch = getc(tixFile);
102 static StgWord64 expectWord64(void) {
104 while (isdigit(tix_ch)) {
105 tmp = tmp * 10 + (tix_ch -'0');
106 tix_ch = getc(tixFile);
114 HpcModuleInfo *tmpModule;
126 while(tix_ch != ']') {
127 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
138 tmpModule -> modName = expectString();
140 tmpModule -> hashNo = (unsigned int)expectWord64();
142 tmpModule -> tickCount = (int)expectWord64();
143 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
144 tmpModule -> tickOffset = totalTixes;
145 totalTixes += tmpModule -> tickCount;
149 for(i = 0;i < tmpModule->tickCount;i++) {
150 tmpModule->tixArr[i] = expectWord64();
163 nextModule->next=tmpModule;
165 nextModule=tmpModule;
176 static void hpc_init(void) {
177 if (hpc_inited != 0) {
182 tixFilename = (char *) malloc(strlen(prog_name) + 6);
183 sprintf(tixFilename, "%s.tix", prog_name);
185 if (init_open(fopen(tixFilename,"r"))) {
190 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
191 * This memory can be uninitized, because we will initialize it with either the contents
192 * of the tix file, or all zeros.
196 hs_hpc_module(char *modName,
200 HpcModuleInfo *tmpModule, *lastModule;
204 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,modCount);
211 for(;tmpModule != 0;tmpModule = tmpModule->next) {
212 if (!strcmp(tmpModule->modName,modName)) {
213 if (tmpModule->tickCount != modCount) {
214 failure("inconsistent number of tick boxes");
216 assert(tmpModule->tixArr != 0);
217 if (tmpModule->hashNo != modHashNo) {
218 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
219 failure("module mismatch with .tix/.mix file hash number");
220 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
224 for(i=0;i < modCount;i++) {
225 tixArr[i] = tmpModule->tixArr[i];
227 tmpModule->tixArr = tixArr;
228 return tmpModule->tickOffset;
230 lastModule = tmpModule;
232 // Did not find entry so add one on.
233 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
234 tmpModule->modName = modName;
235 tmpModule->tickCount = modCount;
236 tmpModule->hashNo = modHashNo;
238 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
240 tmpModule->tickOffset = 0;
242 tmpModule->tixArr = tixArr;
243 for(i=0;i < modCount;i++) {
251 lastModule->next=tmpModule;
254 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
260 /* This is called after all the modules have registered their local tixboxes,
261 * and does a sanity check: are we good to go?
266 debugTrace(DEBUG_hpc,"startupHpc");
268 if (hpc_inited == 0) {
276 HpcModuleInfo *tmpModule;
277 int i, inner_comma, outer_comma;
287 for(;tmpModule != 0;tmpModule = tmpModule->next) {
293 fprintf(f," TixModule \"%s\" %u %u [",
296 tmpModule->tickCount);
297 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
299 tmpModule->tickCount,
301 tmpModule->tickOffset);
304 for(i = 0;i < tmpModule->tickCount;i++) {
311 if (tmpModule->tixArr) {
312 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
324 /* Called at the end of execution, to write out the Hpc *.tix file
325 * for this exection. Safe to call, even if coverage is not used.
329 debugTrace(DEBUG_hpc,"exitHpc");
331 if (hpc_inited == 0) {
335 FILE *f = fopen(tixFilename,"w");
339 void hs_hpc_read(char *filename) {
340 HpcModuleInfo *orig_modules = 0, *tmpModule, *tmpOrigModule;
343 orig_modules = modules;
345 if (init_open(fopen(filename,"r"))) {
347 // Now we copy across the arrays. O(n^2), but works
348 for(tmpModule = modules;
350 tmpModule = tmpModule->next) {
352 for(tmpOrigModule = orig_modules;
354 tmpOrigModule = tmpOrigModule->next) {
355 if (!strcmp(tmpModule->modName,tmpOrigModule->modName)) {
356 assert(tmpModule->tixArr != 0);
357 assert(tmpOrigModule->tixArr != 0);
358 assert(tmpModule->tickCount == tmpOrigModule->tickCount);
359 for(i=0;i < tmpModule->tickCount;i++) {
360 tmpOrigModule->tixArr[i] = tmpModule->tixArr[i];
362 tmpModule->tixArr = tmpOrigModule->tixArr;
370 void hs_hpc_write(char *filename) {
371 writeTix(fopen(filename,"w"));