Adding new ffi calls into the Hpc rts subsystem
authorandy@galois.com <unknown>
Tue, 12 Jun 2007 07:46:55 +0000 (07:46 +0000)
committerandy@galois.com <unknown>
Tue, 12 Jun 2007 07:46:55 +0000 (07:46 +0000)
foreign import ccall unsafe hs_hpc_write :: CString -> IO ()
foreign import ccall unsafe hs_hpc_read  :: CString -> IO ()

These write a Hpc description of the state of the world to a file,
or read a description into the current Hpc tickbox subsystem.

includes/RtsExternal.h
rts/Hpc.c

index d1becf5..58b6ef0 100644 (file)
@@ -75,6 +75,8 @@ extern int hs_hpc_module(char *modName,int modCount,int modHashNo,StgWord64 *tix
 extern void hs_hpc_tick(int globIx,struct StgTSO_ *current_tso);
 extern void hs_hpc_raise_event(struct StgTSO_ *current_tso);   
 extern void hs_hpc_thread_finished_event(struct StgTSO_ *current_tso);
+extern void hs_hpc_read(char *filename);
+extern void hs_hpc_write(char *filename);
 
 #if defined(mingw32_HOST_OS)
 extern int  rts_InstallConsoleEvent ( int action, StgStablePtr *handler );
index b183253..8411e61 100644 (file)
--- a/rts/Hpc.c
+++ b/rts/Hpc.c
@@ -73,9 +73,8 @@ static void failure(char *msg) {
   exit(-1);
 }
 
-static int init_open(char *filename) 
-{
-  tixFile = fopen(filename,"r");
+static int init_open(FILE *file) {
+  tixFile = file;
  if (tixFile == 0) {
     return 0;
   }
@@ -121,76 +120,82 @@ static StgWord64 expectWord64(void) {
   return tmp;
 }
 
-static void hpc_init(void) {
+static void
+readTix(void) {
   int i;
-  Info *tmpModule;  
+  Info *tmpModule;
 
-  if (hpc_inited != 0) {
-    return;
-  }
-  hpc_inited = 1;
-
-  tixFilename = (char *) malloc(strlen(prog_name) + 6);
-  sprintf(tixFilename, "%s.tix", prog_name);
-
-  if (init_open(tixFilename)) { 
-    totalTixes = 0;
-
-    ws();
+  totalTixes = 0;
+    
+  ws();
+  expect('T');
+  expect('i');
+  expect('x');
+  ws();
+  expect('[');
+  ws();
+  
+  while(tix_ch != ']') {
+    tmpModule = (Info *)calloc(1,sizeof(Info));
     expect('T');
     expect('i');
     expect('x');
+    expect('M');
+    expect('o');
+    expect('d');
+    expect('u');
+    expect('l');
+    expect('e');
+    ws();
+    tmpModule -> modName = expectString();
+    ws();
+    tmpModule -> hashNo = (unsigned int)expectWord64();
+    ws();
+    tmpModule -> tickCount = (int)expectWord64();
+    tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
+    tmpModule -> tickOffset = totalTixes;
+    totalTixes += tmpModule -> tickCount;
     ws();
     expect('[');
     ws();
-    while(tix_ch != ']') {
-      tmpModule = (Info *)calloc(1,sizeof(Info));
-      expect('T');
-      expect('i');
-      expect('x');
-      expect('M');
-      expect('o');
-      expect('d');
-      expect('u');
-      expect('l');
-      expect('e');
-      ws();
-      tmpModule -> modName = expectString();
-      ws();
-      tmpModule -> hashNo = (unsigned int)expectWord64();
-      ws();
-      tmpModule -> tickCount = (int)expectWord64();
-      tmpModule -> tixArr = (StgWord64 *)calloc(tmpModule->tickCount,sizeof(StgWord64));
-      tmpModule -> tickOffset = totalTixes;
-      totalTixes += tmpModule -> tickCount;
-      ws();
-      expect('[');
-      ws();
-      for(i = 0;i < tmpModule->tickCount;i++) {
-       tmpModule->tixArr[i] = expectWord64();
-       ws();
-       if (tix_ch == ',') {
-         expect(',');
-         ws();
-       }
-      }
-      expect(']');
+    for(i = 0;i < tmpModule->tickCount;i++) {
+      tmpModule->tixArr[i] = expectWord64();
       ws();
-
-      if (!modules) {
-       modules = tmpModule;
-      } else {
-       nextModule->next=tmpModule;
-      }
-      nextModule=tmpModule;
-      
       if (tix_ch == ',') {
        expect(',');
        ws();
       }
     }
     expect(']');
-    fclose(tixFile);
+    ws();
+    
+    if (!modules) {
+      modules = tmpModule;
+    } else {
+      nextModule->next=tmpModule;
+    }
+    nextModule=tmpModule;
+    
+    if (tix_ch == ',') {
+      expect(',');
+      ws();
+    }
+  }
+  expect(']');
+  fclose(tixFile);
+}
+
+static void hpc_init(void) {
+  if (hpc_inited != 0) {
+    return;
+  }
+  hpc_inited = 1;
+
+  tixFilename = (char *) malloc(strlen(prog_name) + 6);
+  sprintf(tixFilename, "%s.tix", prog_name);
+
+  if (init_open(fopen(tixFilename,"r"))) {
+    readTix();
   }
 }
 
@@ -526,24 +531,17 @@ startupHpc(void) {
 }
 
 
-/* Called at the end of execution, to write out the Hpc *.tix file  
- * for this exection. Safe to call, even if coverage is not used.
- */
-void
-exitHpc(void) {
+static void
+writeTix(FILE *f) {
   Info *tmpModule;  
   int i, inner_comma, outer_comma;
 
-  debugTrace(DEBUG_hpc,"exitHpc");
+  outer_comma = 0;
 
-  if (hpc_inited == 0) {
+  if (f == 0) {
     return;
   }
 
-  FILE *f = fopen(tixFilename,"w");
-  
-  outer_comma = 0;
-
   fprintf(f,"Tix [");
   tmpModule = modules;
   for(;tmpModule != 0;tmpModule = tmpModule->next) {
@@ -580,34 +578,22 @@ exitHpc(void) {
   }
   fprintf(f,"]\n");
   
-  /*
-  tmpModule = modules;
-  for(;tmpModule != 0;tmpModule = tmpModule->next) {
-      if (!tmpModule->tixArr) {
-       debugTrace(DEBUG_hpc,
-                  "warning: module %s did not register any hpc tick data\n",
-                  tmpModule->modName);
-      }
-
-    for(i = 0;i < tmpModule->tickCount;i++) {
-      if (comma) {
-       fprintf(f,",");
-      } else {
-       comma = 1;
-      }
+  fclose(f);
+}
 
-      if (tmpModule->tixArr) {
-       fprintf(f,"%" PRIuWORD64,tmpModule->tixArr[i]);
-      } else {
-       fprintf(f,"0");
-      }
+/* Called at the end of execution, to write out the Hpc *.tix file  
+ * for this exection. Safe to call, even if coverage is not used.
+ */
+void
+exitHpc(void) {
+  debugTrace(DEBUG_hpc,"exitHpc");
 
-    }
+  if (hpc_inited == 0) {
+    return;
   }
-      
-  fprintf(f,"]\n");
-  */
-  fclose(f);
+
+  FILE *f = fopen(tixFilename,"w");
+  writeTix(f);
 
   if (rixFile != NULL) {
     hs_hpc_tick(RixFinishedOp,(StgThreadID)0);
@@ -619,3 +605,40 @@ exitHpc(void) {
   
 }
 
+void hs_hpc_read(char *filename) {
+  Info *orig_modules = 0, *tmpModule, *tmpOrigModule;
+  int i;
+
+  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;
+       }
+      }
+    }
+  }
+}
+
+void hs_hpc_write(char *filename) {
+  writeTix(fopen(filename,"w"));
+}
+
+
+