buildscript options to enable exaclty which coreprof events are needed, keeps instrum...
[IRC.git] / Robust / src / Runtime / coreprof / coreprof.h
1 #ifndef COREPROF_H
2 #define COREPROF_H
3
4
5 #ifndef COREPROF
6 // Core Prof turned off
7
8 #define CP_LOGEVENT( eventID, eventType ) ;
9 #define CP_CREATE()                       ;
10 #define CP_EXIT()                         ; 
11 #define CP_DUMP()                         ;
12
13
14 #else
15 // Core Prof defined
16
17 #include <stdlib.h>
18 #include "runtime.h"
19
20 #ifndef CP_MAXEVENTWORDS
21 #define CP_MAXEVENTWORDS (1024*128)
22 #endif
23
24
25 // MASK should be enough bits to mask
26 // the values of the following event types
27 // and BASESHIFT is for shifting IDs
28 // past the type bits
29 #define CP_EVENTTYPE_BEGIN  0x1
30 #define CP_EVENTTYPE_END    0x2
31 #define CP_EVENTTYPE_ONEOFF 0x3
32
33 #define CP_EVENT_MASK       3
34 #define CP_EVENT_BASESHIFT  8
35
36
37 // Event IDs, only those enabled explicitly as a build option
38 // will be defined and included in the compilation
39 #ifdef cpe_main
40 #define CP_EVENTID_MAIN               0x04
41 #endif
42
43 #ifdef cpe_runmalloc
44 #define CP_EVENTID_RUNMALLOC          0x10
45 #endif
46
47 #ifdef cpe_runfree
48 #define CP_EVENTID_RUNFREE            0x11
49 #endif
50
51 #ifdef cpe_count_poolalloc
52 #define CP_EVENTID_COUNT_POOLALLOC    0x15
53 #endif
54
55 #ifdef cpe_count_poolreuse
56 #define CP_EVENTID_COUNT_POOLREUSE    0x16
57 #endif
58
59 #ifdef cpe_workschedgrab
60 #define CP_EVENTID_WORKSCHEDGRAB      0x20
61 #endif
62
63 #ifdef cpe_taskdispatch
64 #define CP_EVENTID_TASKDISPATCH       0x30
65 #endif
66
67 #ifdef cpe_taskexecute
68 #define CP_EVENTID_TASKEXECUTE        0x31
69 #endif
70
71 #ifdef cpe_taskretire
72 #define CP_EVENTID_TASKRETIRE         0x32
73 #endif
74
75 #ifdef cpe_taskstallvar
76 #define CP_EVENTID_TASKSTALLVAR       0x40
77 #endif
78
79 #ifdef cpe_taskstallmem
80 #define CP_EVENTID_TASKSTALLMEM       0x41
81 #endif
82
83
84 // Note: application-specific events (assigned
85 // during code gen) start at 0x200
86
87
88
89 extern __thread int                     cp_threadnum;
90 extern __thread struct coreprofmonitor* cp_monitor;
91 extern          struct coreprofmonitor* cp_monitorList;
92
93
94 struct coreprofmonitor {
95   struct coreprofmonitor* next;
96   
97   // index for next unused word in the following array;
98   // individual events may use a variable number of
99   // words to store information
100   int          numWords;
101   unsigned int data[CP_MAXEVENTWORDS];
102 };
103
104
105 #ifndef COREPROF_CHECKOVERFLOW
106 #define CP_CHECKOVERFLOW ;
107 #else
108 #define CP_CHECKOVERFLOW if \
109   ( cp_monitor->numWords >= CP_MAXEVENTWORDS ) \
110   { cp_reportOverflow(); }
111 #endif
112
113
114 #define CP_LOGEVENT( eventID, eventType ) { \
115   CP_CHECKOVERFLOW; \
116   cp_monitor->data[cp_monitor->numWords] = \
117     ((eventID << CP_EVENT_BASESHIFT) | eventType); \
118   cp_monitor->numWords += 1; \
119   CP_LOGTIME; \
120 }
121
122
123 #define CP_LOGTIME CP_CHECKOVERFLOW; \
124   *((long long *)&cp_monitor->data[cp_monitor->numWords]) = rdtsc(); \
125   cp_monitor->numWords += 2;
126
127
128
129 #define CP_CREATE() cp_create();
130 #define CP_EXIT()   cp_exit();
131 #define CP_DUMP()   cp_dump();
132
133 void cp_create();
134 void cp_exit();
135 void cp_dump();
136 void cp_reportOverflow();
137
138
139 static inline void* cp_calloc( int size ) {
140 #ifdef CP_EVENTID_RUNMALLOC
141     CP_LOGEVENT( CP_EVENTID_RUNMALLOC, CP_EVENTTYPE_BEGIN );
142 #endif
143   void* mem = calloc( 1, size );
144 #ifdef CP_EVENTID_RUNMALLOC
145   CP_LOGEVENT( CP_EVENTID_RUNMALLOC, CP_EVENTTYPE_END );
146 #endif
147   return mem;
148 }
149
150 static inline void cp_free( void* ptr ) {
151 #ifdef CP_EVENTID_RUNFREE
152   CP_LOGEVENT( CP_EVENTID_RUNFREE, CP_EVENTTYPE_BEGIN );
153 #endif
154   free( ptr );
155 #ifdef CP_EVENTID_RUNFREE
156   CP_LOGEVENT( CP_EVENTID_RUNFREE, CP_EVENTTYPE_END );
157 #endif
158 }
159
160
161 #endif
162 #endif