2 * (c)2006 Galois Connections, Inc.
20 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
25 static int hpc_inited = 0; // Have you started this component?
26 static FILE *tixFile; // file being read/written
27 static int tix_ch; // current char
29 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
31 #define PRIuWORD64 "lu"
33 #define PRIuWORD64 "llu"
36 HpcModuleInfo *modules = 0;
37 HpcModuleInfo *nextModule = 0;
38 int totalTixes = 0; // total number of tix boxes.
40 static char *tixFilename;
42 void hs_hpc_read(char *filename);
43 void hs_hpc_write(char *filename);
45 static void failure(char *msg) {
46 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
47 fprintf(stderr,"Hpc failure: %s\n",msg);
49 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
51 fprintf(stderr,"(perhaps remove .tix file?)\n");
56 static int init_open(FILE *file) {
61 tix_ch = getc(tixFile);
65 static void expect(char c) {
67 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
68 failure("parse error when reading .tix file");
70 tix_ch = getc(tixFile);
73 static void ws(void) {
74 while (tix_ch == ' ') {
75 tix_ch = getc(tixFile);
79 static char *expectString(void) {
83 while (tix_ch != '"') {
84 tmp[tmp_ix++] = tix_ch;
85 tix_ch = getc(tixFile);
94 static StgWord64 expectWord64(void) {
96 while (isdigit(tix_ch)) {
97 tmp = tmp * 10 + (tix_ch -'0');
98 tix_ch = getc(tixFile);
106 HpcModuleInfo *tmpModule;
118 while(tix_ch != ']') {
119 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
130 tmpModule -> modName = expectString();
132 tmpModule -> hashNo = (unsigned int)expectWord64();
134 tmpModule -> tickCount = (int)expectWord64();
135 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
136 tmpModule -> tickOffset = totalTixes;
137 totalTixes += tmpModule -> tickCount;
141 for(i = 0;i < tmpModule->tickCount;i++) {
142 tmpModule->tixArr[i] = expectWord64();
155 nextModule->next=tmpModule;
157 nextModule=tmpModule;
168 static void hpc_init(void) {
169 if (hpc_inited != 0) {
174 tixFilename = (char *) malloc(strlen(prog_name) + 6);
175 sprintf(tixFilename, "%s.tix", prog_name);
177 if (init_open(fopen(tixFilename,"r"))) {
182 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
183 * This memory can be uninitized, because we will initialize it with either the contents
184 * of the tix file, or all zeros.
188 hs_hpc_module(char *modName,
192 HpcModuleInfo *tmpModule, *lastModule;
196 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,modCount);
203 for(;tmpModule != 0;tmpModule = tmpModule->next) {
204 if (!strcmp(tmpModule->modName,modName)) {
205 if (tmpModule->tickCount != modCount) {
206 failure("inconsistent number of tick boxes");
208 assert(tmpModule->tixArr != 0);
209 if (tmpModule->hashNo != modHashNo) {
210 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
211 failure("module mismatch with .tix/.mix file hash number");
212 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
216 for(i=0;i < modCount;i++) {
217 tixArr[i] = tmpModule->tixArr[i];
219 tmpModule->tixArr = tixArr;
220 return tmpModule->tickOffset;
222 lastModule = tmpModule;
224 // Did not find entry so add one on.
225 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
226 tmpModule->modName = modName;
227 tmpModule->tickCount = modCount;
228 tmpModule->hashNo = modHashNo;
230 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
232 tmpModule->tickOffset = 0;
234 tmpModule->tixArr = tixArr;
235 for(i=0;i < modCount;i++) {
243 lastModule->next=tmpModule;
246 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
252 /* This is called after all the modules have registered their local tixboxes,
253 * and does a sanity check: are we good to go?
258 debugTrace(DEBUG_hpc,"startupHpc");
260 if (hpc_inited == 0) {
268 HpcModuleInfo *tmpModule;
269 int i, inner_comma, outer_comma;
279 for(;tmpModule != 0;tmpModule = tmpModule->next) {
285 fprintf(f," TixModule \"%s\" %u %u [",
288 tmpModule->tickCount);
289 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
291 tmpModule->tickCount,
293 tmpModule->tickOffset);
296 for(i = 0;i < tmpModule->tickCount;i++) {
303 if (tmpModule->tixArr) {
304 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
316 /* Called at the end of execution, to write out the Hpc *.tix file
317 * for this exection. Safe to call, even if coverage is not used.
321 debugTrace(DEBUG_hpc,"exitHpc");
323 if (hpc_inited == 0) {
327 FILE *f = fopen(tixFilename,"w");
331 void hs_hpc_read(char *filename) {
332 HpcModuleInfo *orig_modules = 0, *tmpModule, *tmpOrigModule;
335 orig_modules = modules;
337 if (init_open(fopen(filename,"r"))) {
339 // Now we copy across the arrays. O(n^2), but works
340 for(tmpModule = modules;
342 tmpModule = tmpModule->next) {
344 for(tmpOrigModule = orig_modules;
346 tmpOrigModule = tmpOrigModule->next) {
347 if (!strcmp(tmpModule->modName,tmpOrigModule->modName)) {
348 assert(tmpModule->tixArr != 0);
349 assert(tmpOrigModule->tixArr != 0);
350 assert(tmpModule->tickCount == tmpOrigModule->tickCount);
351 for(i=0;i < tmpModule->tickCount;i++) {
352 tmpOrigModule->tixArr[i] = tmpModule->tixArr[i];
354 tmpModule->tixArr = tmpOrigModule->tixArr;
362 void hs_hpc_write(char *filename) {
363 writeTix(fopen(filename,"w"));
366 //////////////////////////////////////////////////////////////////////////////
367 // This is the API into Hpc RTS from Haskell, allowing the tixs boxes
368 // to be first class.
370 HpcModuleInfo *hs_hpc_rootModule(void) {