changes
[IRC.git] / Robust / src / Runtime / coreprof / coreprof.c
1 #include "runtime.h"
2 #include "coreprof.h"
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "mlp_lock.h"
8
9 __thread struct coreprofmonitor * cp_events;
10 struct coreprofmonitor * cp_eventlist=NULL;
11 static volatile int cp_threadcount=0;
12 __thread int threadnum;
13
14 //Need to have global lock before calling this method
15 void createprofiler() {
16   struct coreprofmonitor *event=calloc(1, sizeof(struct coreprofmonitor));
17   //add new eventmonitor to list
18   struct coreprofmonitor *tmp;
19
20   //add ourself to the list
21   do {
22     tmp=cp_eventlist;
23     event->next=tmp;
24   } while(CAS(&cp_eventlist, tmp, event)!=tmp);
25
26   int ourcount=atomic_inc(&cp_threadcount);
27   cp_threadnum=ourcount;
28
29   //point thread lock variable to eventmonitor
30   cp_events=event;
31   CPLOGEVENT((CP_START<<CP_BASE_SHIFT)|CP_BEGIN);
32 }
33
34 void cpwritedata(int fd, char * buffer, int count) {
35   int offset=0;
36   while(count>0) {
37     int size=write(fd, &buffer[offset], count);
38     offset+=size;
39     count-=size;
40   }
41 }
42
43 void dumpprofiler() {
44   int fd=open("logdata",O_RDWR|O_CREAT,S_IRWXU);
45   int count=0;
46   struct coreprofmonitor * ptr=cp_eventlist;
47   int VERSION=0;
48   //Write version number
49   cpwritedata(fd, &version, sizeof(int));
50   while(ptr!=NULL) {
51     count++;
52     if (ptr->index>CPMAXEVENTS) {
53       printf("ERROR: EVENT COUNT EXCEEDED\n");
54     }
55     ptr=ptr->next;
56   }
57
58   //Write the number of threads
59   cpwritedata(fd, (char *)&count, sizeof(int));
60
61   //Write the number of events for each thread
62   ptr=cp_eventlist;
63   while(ptr!=NULL) {
64     cpwritedata(fd, &ptr->index, sizeof(int));
65     ptr=ptr->next;
66   }
67
68   //Dump the data
69   ptr=cp_eventlist;
70   while(ptr!=NULL) {
71     cpwritedata(fd, (char *) ptr->value, sizeof(int)*ptr->index);
72     ptr=ptr->next;
73   }  
74   close(fd);
75 }