fix #3910
[ghc-hetmet.git] / rts / Hpc.c
index ebd5af1..81c802c 100644 (file)
--- a/rts/Hpc.c
+++ b/rts/Hpc.c
@@ -2,15 +2,23 @@
  * (c)2006 Galois Connections, Inc.
  */ 
 
+#include "PosixSource.h"
+#include "Rts.h"
+
+#include "Trace.h"
+
 #include <stdio.h>
 #include <ctype.h>
-#include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 
-#include "Rts.h"
-#include "Hpc.h"
-#include "Trace.h"
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
  */
 
 static int hpc_inited = 0;             // Have you started this component?
+static pid_t hpc_pid = 0;              // pid of this process at hpc-boot time.
+                                       // Only this pid will read or write .tix file(s).
 static FILE *tixFile;                  // file being read/written
 static int tix_ch;                     // current char
 
-// 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
-
 HpcModuleInfo *modules = 0;
 HpcModuleInfo *nextModule = 0;
 int totalTixes = 0;            // total number of tix boxes.
 
 static char *tixFilename;
 
-void hs_hpc_read(char *filename);
-void hs_hpc_write(char *filename);
-
-static void failure(char *msg) {
+static void GNU_ATTRIBUTE(__noreturn__)
+failure(char *msg) {
   debugTrace(DEBUG_hpc,"hpc failure: %s\n",msg);
   fprintf(stderr,"Hpc failure: %s\n",msg);
   if (tixFilename) {
@@ -50,7 +51,7 @@ static void failure(char *msg) {
   } else {
     fprintf(stderr,"(perhaps remove .tix file?)\n");
   }
-  exit(-1);
+  stg_exit(1);
 }
 
 static int init_open(FILE *file) {
@@ -102,7 +103,7 @@ static StgWord64 expectWord64(void) {
 
 static void
 readTix(void) {
-  int i;
+  unsigned int i;
   HpcModuleInfo *tmpModule;
 
   totalTixes = 0;
@@ -166,13 +167,37 @@ readTix(void) {
 }
 
 static void hpc_init(void) {
+  char *hpc_tixdir;
+  char *hpc_tixfile;
   if (hpc_inited != 0) {
     return;
   }
   hpc_inited = 1;
-
-  tixFilename = (char *) malloc(strlen(prog_name) + 6);
-  sprintf(tixFilename, "%s.tix", prog_name);
+  hpc_pid    = getpid();
+  hpc_tixdir = getenv("HPCTIXDIR");
+  hpc_tixfile = getenv("HPCTIXFILE");
+
+  /* XXX Check results of mallocs/strdups, and check we are requesting
+         enough bytes */
+  if (hpc_tixfile != NULL) {
+    tixFilename = strdup(hpc_tixfile);
+  } else if (hpc_tixdir != NULL) {
+    /* Make sure the directory is present;
+     * conditional code for mkdir lifted from lndir.c
+     */
+#ifdef WIN32
+    mkdir(hpc_tixdir);
+#else
+    mkdir(hpc_tixdir,0777);
+#endif
+    /* Then, try open the file
+     */
+    tixFilename = (char *) malloc(strlen(hpc_tixdir) + strlen(prog_name) + 12);
+    sprintf(tixFilename,"%s/%s-%d.tix",hpc_tixdir,prog_name,(int)hpc_pid);
+  } else {
+    tixFilename = (char *) malloc(strlen(prog_name) + 6);
+    sprintf(tixFilename, "%s.tix", prog_name);
+  }
 
   if (init_open(fopen(tixFilename,"r"))) {
     readTix();
@@ -186,14 +211,14 @@ static void hpc_init(void) {
 
 int
 hs_hpc_module(char *modName,
-             int modCount,
-             int modHashNo,
+             StgWord32 modCount,
+             StgWord32 modHashNo,
              StgWord64 *tixArr) {
   HpcModuleInfo *tmpModule, *lastModule;
-  int i;
+  unsigned int i;
   int offset = 0;
   
-  debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,modCount);
+  debugTrace(DEBUG_hpc,"hs_hpc_module(%s,%d)",modName,(nat)modCount);
 
   hpc_init();
 
@@ -210,7 +235,7 @@ hs_hpc_module(char *modName,
        fprintf(stderr,"in module '%s'\n",tmpModule->modName);
        failure("module mismatch with .tix/.mix file hash number");
        fprintf(stderr,"(perhaps remove %s ?)\n",tixFilename);
-       exit(-1);
+       stg_exit(1);
 
       }
       for(i=0;i < modCount;i++) {
@@ -266,7 +291,7 @@ startupHpc(void) {
 static void
 writeTix(FILE *f) {
   HpcModuleInfo *tmpModule;  
-  int i, inner_comma, outer_comma;
+  unsigned int i, inner_comma, outer_comma;
 
   outer_comma = 0;
 
@@ -284,13 +309,13 @@ writeTix(FILE *f) {
     }
     fprintf(f," TixModule \"%s\" %u %u [",
           tmpModule->modName,
-           tmpModule->hashNo,
-           tmpModule->tickCount);
+           (nat)tmpModule->hashNo,
+           (nat)tmpModule->tickCount);
     debugTrace(DEBUG_hpc,"%s: %u (offset=%u) (hash=%u)\n",
               tmpModule->modName,
-              tmpModule->tickCount,
-              tmpModule->hashNo,
-              tmpModule->tickOffset);
+              (nat)tmpModule->tickCount,
+              (nat)tmpModule->hashNo,
+              (nat)tmpModule->tickOffset);
 
     inner_comma = 0;
     for(i = 0;i < tmpModule->tickCount;i++) {
@@ -301,7 +326,7 @@ writeTix(FILE *f) {
       }
 
       if (tmpModule->tixArr) {
-       fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
+       fprintf(f,"%" FMT_Word64,tmpModule->tixArr[i]);
       } else {
        fprintf(f,"0");
       }
@@ -324,45 +349,16 @@ exitHpc(void) {
     return;
   }
 
-  FILE *f = fopen(tixFilename,"w");
-  writeTix(f);
-}
-
-void hs_hpc_read(char *filename) {
-  HpcModuleInfo *orig_modules = 0, *tmpModule, *tmpOrigModule;
-  int i;
+  // Only write the tix file if you are the original process.
+  // Any sub-process from use of fork from inside Haskell will
+  // not clober the .tix file.
 
-  orig_modules = modules;
-  modules = 0;
-  if (init_open(fopen(filename,"r"))) {
-    readTix();
-    // Now we copy across the arrays. O(n^2), but works
-    for(tmpModule = modules;
-       tmpModule != 0;
-       tmpModule = tmpModule->next) {
-
-      for(tmpOrigModule = orig_modules;
-         tmpOrigModule != 0;
-         tmpOrigModule = tmpOrigModule->next) {
-       if (!strcmp(tmpModule->modName,tmpOrigModule->modName)) {    
-         assert(tmpModule->tixArr != 0);               
-         assert(tmpOrigModule->tixArr != 0);           
-         assert(tmpModule->tickCount == tmpOrigModule->tickCount);
-         for(i=0;i < tmpModule->tickCount;i++) {
-           tmpOrigModule->tixArr[i] = tmpModule->tixArr[i];
-         }
-         tmpModule->tixArr = tmpOrigModule->tixArr;
-         break;
-       }
-      }
-    }
+  if (hpc_pid == getpid()) {
+    FILE *f = fopen(tixFilename,"w");
+    writeTix(f);
   }
 }
 
-void hs_hpc_write(char *filename) {
-  writeTix(fopen(filename,"w"));
-}
-
 //////////////////////////////////////////////////////////////////////////////
 // This is the API into Hpc RTS from Haskell, allowing the tixs boxes
 // to be first class.