Fixing HPCTIXDIR problem with mkdir usage on Windows
[ghc.git] / rts / Hpc.c
1 /*
2 * (c)2006 Galois Connections, Inc.
3 */
4
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10
11 #include "Rts.h"
12 #include "Hpc.h"
13 #include "Trace.h"
14
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27
28 /* This is the runtime support for the Haskell Program Coverage (hpc) toolkit,
29 * inside GHC.
30 *
31 */
32
33 static int hpc_inited = 0; // Have you started this component?
34 static pid_t hpc_pid = 0; // pid of this process at hpc-boot time.
35 // Only this pid will read or write .tix file(s).
36 static FILE *tixFile; // file being read/written
37 static int tix_ch; // current char
38
39 // This is a cruel hack, we should completely redesign the format specifier handling in the RTS.
40 #if SIZEOF_LONG == 8
41 #define PRIuWORD64 "lu"
42 #else
43 #define PRIuWORD64 "llu"
44 #endif
45
46 HpcModuleInfo *modules = 0;
47 HpcModuleInfo *nextModule = 0;
48 int totalTixes = 0; // total number of tix boxes.
49
50 static char *tixFilename;
51
52 static void failure(char *msg) {
53 debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
54 fprintf(stderr,"Hpc failure: %s\n",msg);
55 if (tixFilename) {
56 fprintf(stderr,"(perhaps remove %s file?)\n",tixFilename);
57 } else {
58 fprintf(stderr,"(perhaps remove .tix file?)\n");
59 }
60 exit(-1);
61 }
62
63 static int init_open(FILE *file) {
64 tixFile = file;
65 if (tixFile == 0) {
66 return 0;
67 }
68 tix_ch = getc(tixFile);
69 return 1;
70 }
71
72 static void expect(char c) {
73 if (tix_ch != c) {
74 fprintf(stderr,"('%c' '%c')\n",tix_ch,c);
75 failure("parse error when reading .tix file");
76 }
77 tix_ch = getc(tixFile);
78 }
79
80 static void ws(void) {
81 while (tix_ch == ' ') {
82 tix_ch = getc(tixFile);
83 }
84 }
85
86 static char *expectString(void) {
87 char tmp[256], *res;
88 int tmp_ix = 0;
89 expect('"');
90 while (tix_ch != '"') {
91 tmp[tmp_ix++] = tix_ch;
92 tix_ch = getc(tixFile);
93 }
94 tmp[tmp_ix++] = 0;
95 expect('"');
96 res = malloc(tmp_ix);
97 strcpy(res,tmp);
98 return res;
99 }
100
101 static StgWord64 expectWord64(void) {
102 StgWord64 tmp = 0;
103 while (isdigit(tix_ch)) {
104 tmp = tmp * 10 + (tix_ch -'0');
105 tix_ch = getc(tixFile);
106 }
107 return tmp;
108 }
109
110 static void
111 readTix(void) {
112 unsigned int i;
113 HpcModuleInfo *tmpModule;
114
115 totalTixes = 0;
116
117 ws();
118 expect('T');
119 expect('i');
120 expect('x');
121 ws();
122 expect('[');
123 ws();
124
125 while(tix_ch != ']') {
126 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
127 expect('T');
128 expect('i');
129 expect('x');
130 expect('M');
131 expect('o');
132 expect('d');
133 expect('u');
134 expect('l');
135 expect('e');
136 ws();
137 tmpModule -> modName = expectString();
138 ws();
139 tmpModule -> hashNo = (unsigned int)expectWord64();
140 ws();
141 tmpModule -> tickCount = (int)expectWord64();
142 tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
143 tmpModule -> tickOffset = totalTixes;
144 totalTixes += tmpModule -> tickCount;
145 ws();
146 expect('[');
147 ws();
148 for(i = 0;i < tmpModule->tickCount;i++) {
149 tmpModule->tixArr[i] = expectWord64();
150 ws();
151 if (tix_ch == ',') {
152 expect(',');
153 ws();
154 }
155 }
156 expect(']');
157 ws();
158
159 if (!modules) {
160 modules = tmpModule;
161 } else {
162 nextModule->next=tmpModule;
163 }
164 nextModule=tmpModule;
165
166 if (tix_ch == ',') {
167 expect(',');
168 ws();
169 }
170 }
171 expect(']');
172 fclose(tixFile);
173 }
174
175 static void hpc_init(void) {
176 char *hpc_tixdir;
177 if (hpc_inited != 0) {
178 return;
179 }
180 hpc_inited = 1;
181 hpc_pid = getpid();
182 hpc_tixdir = getenv("HPCTIXDIR");
183
184 if (hpc_tixdir != NULL) {
185 /* Make sure the directory is present;
186 * conditional code for mkdir lifted from lndir.c
187 */
188 #ifdef WIN32
189 mkdir(hpc_tixdir);
190 #else
191 mkdir(hpc_tixdir,0777);
192 #endif
193 /* Then, try open the file
194 */
195 tixFilename = (char *) malloc(strlen(hpc_tixdir) + strlen(prog_name) + 12);
196 sprintf(tixFilename,"%s/%s-%d.tix",hpc_tixdir,prog_name,hpc_pid);
197 } else {
198 tixFilename = (char *) malloc(strlen(prog_name) + 6);
199 sprintf(tixFilename, "%s.tix", prog_name);
200 }
201
202 if (init_open(fopen(tixFilename,"r"))) {
203 readTix();
204 }
205 }
206
207 /* Called on a per-module basis, at startup time, declaring where the tix boxes are stored in memory.
208 * This memory can be uninitized, because we will initialize it with either the contents
209 * of the tix file, or all zeros.
210 */
211
212 int
213 hs_hpc_module(char *modName,
214 StgWord32 modCount,
215 StgWord32 modHashNo,
216 StgWord64 *tixArr) {
217 HpcModuleInfo *tmpModule, *lastModule;
218 unsigned int i;
219 int offset = 0;
220
221 debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,(nat)modCount);
222
223 hpc_init();
224
225 tmpModule = modules;
226 lastModule = 0;
227
228 for(;tmpModule != 0;tmpModule = tmpModule->next) {
229 if (!strcmp(tmpModule->modName,modName)) {
230 if (tmpModule->tickCount != modCount) {
231 failure("inconsistent number of tick boxes");
232 }
233 assert(tmpModule->tixArr != 0);
234 if (tmpModule->hashNo != modHashNo) {
235 fprintf(stderr,"in module '%s'\n",tmpModule->modName);
236 failure("module mismatch with .tix/.mix file hash number");
237 fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
238 exit(-1);
239
240 }
241 for(i=0;i < modCount;i++) {
242 tixArr[i] = tmpModule->tixArr[i];
243 }
244 tmpModule->tixArr = tixArr;
245 return tmpModule->tickOffset;
246 }
247 lastModule = tmpModule;
248 }
249 // Did not find entry so add one on.
250 tmpModule = (HpcModuleInfo *)calloc(1,sizeof(HpcModuleInfo));
251 tmpModule->modName = modName;
252 tmpModule->tickCount = modCount;
253 tmpModule->hashNo = modHashNo;
254 if (lastModule) {
255 tmpModule->tickOffset = lastModule->tickOffset + lastModule->tickCount;
256 } else {
257 tmpModule->tickOffset = 0;
258 }
259 tmpModule->tixArr = tixArr;
260 for(i=0;i < modCount;i++) {
261 tixArr[i] = 0;
262 }
263 tmpModule->next = 0;
264
265 if (!modules) {
266 modules = tmpModule;
267 } else {
268 lastModule->next=tmpModule;
269 }
270
271 debugTrace(DEBUG_hpc,"end: hs_hpc_module");
272
273 return offset;
274 }
275
276
277 /* This is called after all the modules have registered their local tixboxes,
278 * and does a sanity check: are we good to go?
279 */
280
281 void
282 startupHpc(void) {
283 debugTrace(DEBUG_hpc,"startupHpc");
284
285 if (hpc_inited == 0) {
286 return;
287 }
288 }
289
290
291 static void
292 writeTix(FILE *f) {
293 HpcModuleInfo *tmpModule;
294 unsigned int i, inner_comma, outer_comma;
295
296 outer_comma = 0;
297
298 if (f == 0) {
299 return;
300 }
301
302 fprintf(f,"Tix [");
303 tmpModule = modules;
304 for(;tmpModule != 0;tmpModule = tmpModule->next) {
305 if (outer_comma) {
306 fprintf(f,",");
307 } else {
308 outer_comma = 1;
309 }
310 fprintf(f," TixModule \"%s\" %u %u [",
311 tmpModule->modName,
312 (nat)tmpModule->hashNo,
313 (nat)tmpModule->tickCount);
314 debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
315 tmpModule->modName,
316 (nat)tmpModule->tickCount,
317 (nat)tmpModule->hashNo,
318 (nat)tmpModule->tickOffset);
319
320 inner_comma = 0;
321 for(i = 0;i < tmpModule->tickCount;i++) {
322 if (inner_comma) {
323 fprintf(f,",");
324 } else {
325 inner_comma = 1;
326 }
327
328 if (tmpModule->tixArr) {
329 fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
330 } else {
331 fprintf(f,"0");
332 }
333 }
334 fprintf(f,"]");
335 }
336 fprintf(f,"]\n");
337
338 fclose(f);
339 }
340
341 /* Called at the end of execution, to write out the Hpc *.tix file
342 * for this exection. Safe to call, even if coverage is not used.
343 */
344 void
345 exitHpc(void) {
346 debugTrace(DEBUG_hpc,"exitHpc");
347
348 if (hpc_inited == 0) {
349 return;
350 }
351
352 // Only write the tix file if you are the original process.
353 // Any sub-process from use of fork from inside Haskell will
354 // not clober the .tix file.
355
356 if (hpc_pid == getpid()) {
357 FILE *f = fopen(tixFilename,"w");
358 writeTix(f);
359 }
360 }
361
362 //////////////////////////////////////////////////////////////////////////////
363 // This is the API into Hpc RTS from Haskell, allowing the tixs boxes
364 // to be first class.
365
366 HpcModuleInfo *hs_hpc_rootModule(void) {
367 return modules;
368 }