bug fixes for coreprof
[IRC.git] / Robust / src / Runtime / coreprof / coreprof.c
index 7b61c8a66ea13be2d70fde46c34da6106063c48a..3c5f876061707d4c21db21fad40cb05aada08938 100644 (file)
@@ -1,18 +1,32 @@
 #include "runtime.h"
 #include "coreprof.h"
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "mlp_lock.h"
 
-__thread struct coreprofmonitor * cp_events;
+__thread struct coreprofmonitor * cp_events=NULL;
 struct coreprofmonitor * cp_eventlist=NULL;
 static volatile int cp_threadcount=0;
-__thread int threadnum;
+__thread int cp_threadnum;
+
+static inline int atomicinc(volatile int *lock) {
+  int retval=1;
+  __asm__ __volatile__("lock; xadd %0,%1"
+                       : "=r"(retval)
+                       : "m"(*lock), "0"(retval)
+                       : "memory");
+  return retval;
+}
+
 
 //Need to have global lock before calling this method
 void createprofiler() {
+  if (cp_events!=NULL)
+    return;
   struct coreprofmonitor *event=calloc(1, sizeof(struct coreprofmonitor));
   //add new eventmonitor to list
   struct coreprofmonitor *tmp;
@@ -21,19 +35,19 @@ void createprofiler() {
   do {
     tmp=cp_eventlist;
     event->next=tmp;
-  } while(CAS(&cp_eventlist, tmp, event)!=tmp);
+  } while(CAS(&cp_eventlist, (INTPTR) tmp, (INTPTR) event)!=((INTPTR)tmp));
 
-  int ourcount=atomic_inc(&cp_threadcount);
+  int ourcount=atomicinc(&cp_threadcount);
   cp_threadnum=ourcount;
 
   //point thread lock variable to eventmonitor
   cp_events=event;
-  CPLOGEVENT(CP_START, CP_BEGIN);
+  CPLOGEVENT(CP_MAIN, CP_BEGIN);
 }
 
 //Place to do shutdown stuff
 void exitprofiler() {
-  CPLOGEVENT(CP_START, CP_END);
+  CPLOGEVENT(CP_MAIN, CP_END);
 }
 
 void cpwritedata(int fd, char * buffer, int count) {
@@ -49,9 +63,9 @@ void dumpprofiler() {
   int fd=open("logdata",O_RDWR|O_CREAT,S_IRWXU);
   int count=0;
   struct coreprofmonitor * ptr=cp_eventlist;
-  int VERSION=0;
+  int version=0;
   //Write version number
-  cpwritedata(fd, &version, sizeof(int));
+  cpwritedata(fd, (char *)&version, sizeof(int));
   while(ptr!=NULL) {
     count++;
     if (ptr->index>CPMAXEVENTS) {
@@ -66,7 +80,7 @@ void dumpprofiler() {
   //Write the number of events for each thread
   ptr=cp_eventlist;
   while(ptr!=NULL) {
-    cpwritedata(fd, &ptr->index, sizeof(int));
+    cpwritedata(fd, (char *)&ptr->index, sizeof(int));
     ptr=ptr->next;
   }