Fix breakages caused by commit 48ed568c6
[charm.git] / src / ck-perf / trace-Tau.C
blobd4c7a1d30f13f0d4f11745034ff1f5d335fa496a
1 #include <stdlib.h>
2 #include "charm++.h"
3 #include "trace-Tau.h"
4 #include "trace-TauBOC.h"
5 #include "trace-common.h"
6 #include "TAU.h"
7 //#include "tau_selective.cpp"
8 #include "map"
9 #include "stack"
10 #include <string>
11 using namespace std;
13 #if defined(_WIN32)
14 #include <direct.h>
15 #define CHDIR _chdir
16 #define GETCWD _getcwd
17 #define PATHSEP '\\'
18 #define PATHSEPSTR "\\"
19 #else
20 #include <unistd.h>
21 #define CHDIR chdir
22 #define GETCWD getcwd
23 #define PATHSEP '/'
24 #define PATHSEPSTR "/"
25 #endif
27 /*#ifndef PROFILING_ON
28 void TAU_PROFILER_CREATE(void *p, char *n, char *s, taugroup_t t) {
29 dprintf("---> tau
30 create profiler: %s \n", s); }
32 void TAU_PROFILER_STOP(void *p) { dprintf("---> tau
33 stop profiler"); }
35 void TAU_PROFILER_START(void *p) { dprintf("---> tau
36 start profiler"); }
38 void TAU_PROFILE_SET_NODE(int i) { dprintf("---> tau
39 set node"); }
40 #endif
43 #ifdef DEBUG_PROF
44 #define dprintf printf
45 #else // DEBUG_PROF 
46 #define dprintf if (0) printf
47 #endif
49 extern bool processFileForInstrumentation(const string& file_name);
50 extern void printExcludeList();
51 extern bool instrumentEntity(const string& function_name);
52 extern int processInstrumentationRequests(char *fname);
54 // Charm++ "processor"(user thread)-private global variable
55 CkpvStaticDeclare(TraceTau*, _trace);
57 // This global variable is required for any post-execution 
58 // parallel analysis or activities the trace module might wish to perform.
59 CkGroupID traceTauGID;
61 /**
62    For each TraceFoo module, _createTraceFoo() must be defined.
63    This function is called in _createTraces() generated in moduleInit.C
66 void *idle, *comp;
67 //char *name = "default";
68 bool profile = true, snapshotProfiling = false;
70 //map<const int, void*> events;
71 void* events[5000];
72 stack<void*> eventStack;
73 int EX_VALUE = 0;
74 void *EXCLUDED = &EX_VALUE;
75 void startEntryEvent(int id)
77   dprintf("---------> starting Entry Event with id: %d\n", id);
79   if ((id == -1) || (events[id] == NULL))
80     {
81       dprintf("-------> create event with id: %d\n", id);
82       //sprintf(name, "Event %d", id);
83       if (id == -1)
84         { /*
85             char *name = "dummy_thread_ep";
86             dprintf(" ------> creating event: %s\n", name);
87             TAU_PROFILER_CREATE(events[id], name, "", TAU_DEFAULT);
88             dprintf("timer created.\n");
89             eventStack.push(events[id]);
90             dprintf(" ------> starting event: %s\n", (char*) name);
91             TAU_PROFILER_START(eventStack.top());*/
92           //exclude dummy event
93           dprintf("------> excluding dummy function");
94           eventStack.push(EXCLUDED);
95         }
96       else
97         {
98           //string check("doFFT(RSFFTMsg* impl_msg)");
99           //string name_s(_entryTable[id]->name);
100           //printf("checking name4: %s", _entryTable[id]->name);
101           //if (check.compare(name_s) != 0)
102           //{
103           char name [500];
104           sprintf(name, "%s::%s::%d", _chareTable[_entryTable[id]->chareIdx]->name,
105                   _entryTable[id]->name, id);
106           //should this fuction be excluded from instrumentation?
107           if (!instrumentEntity(name))
108             {
109               //exclude function.
110               dprintf("------> excluding function %s\n", name);
111               events[id] = EXCLUDED;
112               eventStack.push(events[id]);
113             }
114           else
115             {
116               dprintf(" ------> creating event: %s\n", name);
117               TAU_PROFILER_CREATE(events[id], name, "", TAU_DEFAULT);
118               dprintf("timer created.\n");
119               eventStack.push(events[id]);
120               dprintf("starting event\n");
121               dprintf(" ------> starting event: %s\n", (char*) name);
122               TAU_PROFILER_START(eventStack.top());
123             }
124           dprintf("done.\n");
125         }
126     }
127   else
128     {
129       eventStack.push(events[id]);
130       if (events[id] != EXCLUDED)
131         {
132           TAU_PROFILER_START(eventStack.top());
133         }
134     }
137 void stopEntryEvent()
139   dprintf("stop timer...\n");
140   if (eventStack.top() != EXCLUDED)
141         {
142           TAU_PROFILER_STOP(eventStack.top());
143         }
144   eventStack.pop();
148 void _createTraceTau(char **argv)
150   //TAU_INIT(1, argv);
151   memset(events, 0, sizeof(void *)*5000);
152   //CkPrintf("NEWEST VERSION");
153   dprintf("arguments:\n");
154   dprintf("[0] = %s, ", argv[0]);
155   dprintf("[1] = %s, ", argv[1]);
156   dprintf("[2] = %s, ", argv[2]);
157   dprintf("\n");
158   string disable = "disable-profiling";
159   if (argv[1] == NULL) { profile = true; }
160   else if (argv[1] == disable) { profile = false; }
161   if (not CkpvAccess(traceOn)) { 
162     dprintf("traceoff selected using snapshot profiling.\n");
163     snapshotProfiling = true; 
164   }
166   CkpvInitialize(TraceTau*, _trace);
167   CkpvAccess(_trace) = new TraceTau(argv);
168   CkpvAccess(_traces)->addTrace(CkpvAccess(_trace));
171 TraceTau::TraceTau(char **argv)
173   if (CkpvAccess(traceOnPe) == 0) return;
174   
175   // Process runtime arguments intended for the module
176   CmiGetArgIntDesc(argv,"+TauPar0", &par0, "Fake integer parameter 0");
177   CmiGetArgDoubleDesc(argv,"+TauPar1", &par1, "Fake double parameter 1");
178   //TAU_REGISTER_THREAD();
179   if (profile)
180     {
181       if (strcmp(CkpvAccess(selective), ""))
182         {
183           //printf("select file: %s\n", CkpvAccess(selective));
184           //processFileForInstrumentation(CkpvAccess(selective));
185           processInstrumentationRequests(CkpvAccess(selective));
186           printExcludeList();
187           if (!instrumentEntity("Main::done(void)::99"))
188             {
189               dprintf("selective file working...\n");
190             }
191           else
192             dprintf("selective flile not working...\n");
193         }
194       
195       TAU_PROFILER_CREATE(idle, "Idle", "", TAU_DEFAULT);
196       //TAU_PROFILER_CREATE(entry,name,"", TAU_DEFAULT);
197       dprintf("before %p\n", comp);
198       TAU_PROFILER_CREATE(comp, "Main", "", TAU_DEFAULT);
199       dprintf("after %p\n", comp);
200       
201       //Need to add an entry timer to the top of the stack because
202       //traceTauExitFunction calls CkExit() which calls endExecute
203       eventStack.push(EXCLUDED);
204     }
205   else 
206     {
207       dprintf("--> [TAU] creating timers...\n");
208     }
211 void TraceTau::userEvent(int eventID) 
213   dprintf("[%d] User Point Event id %d encountered\n", CkMyPe(), eventID);
216 void TraceTau::userBracketEvent(int eventID, double bt, double et) {
217   dprintf("[%d] User Bracket Event id %d encountered\n", CkMyPe(), eventID);
220 void TraceTau::creation(envelope *, int epIdx, int num) {
221   dprintf("[%d] Point-to-Point Message for Entry Method id %d sent\n",
222           CkMyPe(), epIdx);
225 void TraceTau::creationMulticast(envelope *, int epIdx, int num, 
226                                  int *pelist) {
227   dprintf("[%d] Multicast Message for Entry Method id %d sent to %d pes\n",
228           CkMyPe(), epIdx, num);
231 void TraceTau::creationDone(int num) {
232   dprintf("[%d] Last initiated send completes\n", CkMyPe());
235 void TraceTau::messageRecv(char *env, int pe) {
236   dprintf("[%d] Message from pe %d received by scheduler\n", 
237           CkMyPe(), pe);
240 void TraceTau::beginExecute(CmiObjId *tid)
242   // CmiObjId is a 4-integer tuple uniquely identifying a migratable
243   // Charm++ object. Note that there are other non-migratable Charm++
244   // objects that CmiObjId will not identify.
245   dprintf("[%d] Entry Method invoked using object id\n", CkMyPe());
246   if (profile) {
247     startEntryEvent(-1);
248   }
249   else
250     {
251       dprintf("--> [TAU] starting entry timer...\n");
252     }
255 void TraceTau::beginExecute(envelope *e)
257   // no message means thread execution
258   if (e == NULL) {
259     dprintf("[%d] Entry Method invoked via thread id %d\n", CkMyPe(),
260             _threadEP);
261     if (profile) {
262       startEntryEvent(-1);
263     }
264     else
265       {
266         dprintf("--> [TAU] starting entry timer...\n");
267       }
268     // Below is what is found in trace-summary.
269     // beginExecute(-1,-1,_threadEP,-1);
270   } else {
271     dprintf("[%d] Entry Method %d invoked via message envelope\n", 
272             CkMyPe(), e->getEpIdx());
273     if (profile) {
274       startEntryEvent(e->getEpIdx());
275     }
276     else
277       {
278         dprintf("--> [TAU] starting entry timer...\n");
279       }
280     // Below is what is found in trace-summary.
281     // beginExecute(-1,-1,e->getEpIdx(),-1);
282   }
285 void TraceTau::beginExecute(int event,int msgType,int ep,int srcPe, 
286                             int mlen, CmiObjId *idx)
288   dprintf("[%d] Entry Method %d invoked by parameters\n", CkMyPe(),
289           ep);
290   if (profile) {
291     startEntryEvent(ep);
292   }
293   else
294     {
295       dprintf("--> [TAU] starting entry timer...\n");
296     }
299 void TraceTau::endExecute(void)
301   if (profile) {
302     stopEntryEvent();
303   }
304   else
305     {
306       dprintf("--> [TAU] stoping entry timer...\n");
307     }
308   dprintf("[%d] Previously executing Entry Method completes\n", CkMyPe());
311 void TraceTau::beginIdle(double curWallTime) {
312   dprintf("[%d] Scheduler has no useful user-work\n", CkMyPe());
313   if (profile) {
314     TAU_PROFILER_START(idle);
315   }
316   else
317     {
318       dprintf("--> [TAU] starting idle timer...\n");
319     }
322 void TraceTau::endIdle(double curWallTime) {
323   if (profile) {
324     TAU_PROFILER_STOP(idle);
325   }
326   else
327     {
328       dprintf("--> [TAU] stopping idle timer...\n");
329     }
330   dprintf("[%d] Scheduler now has useful user-work\n", CkMyPe());
333 void TraceTau::beginComputation(void)
335   dprintf("[%d] Computation Begins\n", CkMyPe());
336   //TAU_DISABLE_ALL_GROUPS();
337   // Code Below shows what trace-summary would do.
338   // initialze arrays because now the number of entries is known.
339   // _logPool->initMem();
342 void TraceTau::endComputation(void)
344   dprintf("[%d] Computation Ends\n", CkMyPe());
347 void TraceTau::traceBegin(void)
349   dprintf("[%d] >>>>>> Tracing Begins\n", CkMyPe());
350   if (profile) {
351     dprintf("ptr: %p\n", comp);
352       TAU_DB_PURGE();
353       TAU_ENABLE_ALL_GROUPS();
354       TAU_PROFILER_START(comp);
355   }
356   else
357     {
358       dprintf("--> [TAU] starting computation timer...\n");
359     }
362 void TraceTau::traceEnd(void)
364   dprintf("[%d] >>>>>> Tracing Ends\n", CkMyPe());
365   if (profile){
366     dprintf("ptr: %p\n", comp);
367       //TAU_PROFILER_STOP(comp);
368     TAU_PROFILE_EXIT("tracing complete.");
369     TAU_DISABLE_ALL_GROUPS();
370   }
371   else
372     {
373       dprintf("--> [TAU] stopping computation timer and writing profiles\n");
374     }
375   dprintf("[%d] Computation Ends\n", CkMyPe());
378 void TraceTau::malloc(void *where, int size, void **stack, int stackSize)
380   dprintf("[%d] Memory allocation of size %d occurred\n", CkMyPe(), size);
383 void TraceTau::free(void *where, int size) {
384   dprintf("[%d] %d-byte Memory block freed\n", CkMyPe(), size);
387 void TraceTau::traceClose(void)
389   dprintf("traceClose called.\n");
390   CkpvAccess(_trace)->endComputation();
391   CkpvAccess(_trace)->traceEnd();
392   //TAU_PROFILE_EXIT("closing trace...");
393   //dprintf(" [%d] Exit called \n", CkMyPe());
394   //TAU_PROFILE_EXIT("exiting...");
395   // remove myself from traceArray so that no tracing will be called.
396   CkpvAccess(_traces)->removeTrace(this);
399 extern "C" void traceTauExitFunction() {
400   dprintf("traceTauExitFunction called.\n");
401   //TAU_PROFILE_EXIT("exiting...");
402   //TAU_PROFILE_EXIT("done");
403   //eventStack.push(NULL);
404   CkContinueExit();
407 // Initialization of the parallel trace module.
408 void initTraceTauBOC() {
409   //void *main;
410   dprintf("tracetauboc setting node %d\n", CmiMyPe());
411   if (profile) {
412     TAU_PROFILE_SET_NODE(CmiMyPe());
413   }
414   else
415     {
416       dprintf("---> [TAU] settting node.\n");
417     }
418   //TAU_PROFILER_CREATE(main, "main", "", TAU_DEFAULT);
419   //TAU_PROFILER_START(main);
420 #ifdef __BIGSIM__
421   if (BgNodeRank()==0) {
422 #else
423   if (CkMyRank() == 0) {
424 #endif
425     registerExitFn(traceTauExitFunction);
426   }
428   
429 #include "TraceTau.def.h"