00001 #include <stdlib.h>
00002 #include "charm++.h"
00003 #include "trace-Tau.h"
00004 #include "trace-TauBOC.h"
00005 #include "trace-common.h"
00006 #include "TAU.h"
00007
00008 #include "map"
00009 #include "stack"
00010 #include <string>
00011 using namespace std;
00012
00013 #if defined(_WIN32)
00014 #include <direct.h>
00015 #define CHDIR _chdir
00016 #define GETCWD _getcwd
00017 #define PATHSEP '\\'
00018 #define PATHSEPSTR "\\"
00019 #else
00020 #include <unistd.h>
00021 #define CHDIR chdir
00022 #define GETCWD getcwd
00023 #define PATHSEP '/'
00024 #define PATHSEPSTR "/"
00025 #endif
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifdef DEBUG_PROF
00044 #define dprintf printf
00045 #else // DEBUG_PROF
00046 #define dprintf if (0) printf
00047 #endif
00048
00049 extern bool processFileForInstrumentation(const string& file_name);
00050 extern void printExcludeList();
00051 extern bool instrumentEntity(const string& function_name);
00052 extern int processInstrumentationRequests(char *fname);
00053
00054
00055 CkpvStaticDeclare(TraceTau*, _trace);
00056
00057
00058
00059 CkGroupID traceTauGID;
00060
00066 void *idle, *comp;
00067
00068 bool profile = true, snapshotProfiling = false;
00069
00070
00071 void* events[5000];
00072 stack<void*> eventStack;
00073 int EX_VALUE = 0;
00074 void *EXCLUDED = &EX_VALUE;
00075 void startEntryEvent(int id)
00076 {
00077 dprintf("---------> starting Entry Event with id: %d\n", id);
00078
00079 if ((id == -1) || (events[id] == NULL))
00080 {
00081 dprintf("-------> create event with id: %d\n", id);
00082
00083 if (id == -1)
00084 {
00085
00086
00087
00088
00089
00090
00091
00092
00093 dprintf("------> excluding dummy function");
00094 eventStack.push(EXCLUDED);
00095 }
00096 else
00097 {
00098
00099
00100
00101
00102
00103 char name [500];
00104 sprintf(name, "%s::%s::%d", _chareTable[_entryTable[id]->chareIdx]->name,
00105 _entryTable[id]->name, id);
00106
00107 if (!instrumentEntity(name))
00108 {
00109
00110 dprintf("------> excluding function %s\n", name);
00111 events[id] = EXCLUDED;
00112 eventStack.push(events[id]);
00113 }
00114 else
00115 {
00116 dprintf(" ------> creating event: %s\n", name);
00117 TAU_PROFILER_CREATE(events[id], name, "", TAU_DEFAULT);
00118 dprintf("timer created.\n");
00119 eventStack.push(events[id]);
00120 dprintf("starting event\n");
00121 dprintf(" ------> starting event: %s\n", (char*) name);
00122 TAU_PROFILER_START(eventStack.top());
00123 }
00124 dprintf("done.\n");
00125 }
00126 }
00127 else
00128 {
00129 eventStack.push(events[id]);
00130 if (events[id] != EXCLUDED)
00131 {
00132 TAU_PROFILER_START(eventStack.top());
00133 }
00134 }
00135 }
00136
00137 void stopEntryEvent()
00138 {
00139 dprintf("stop timer...\n");
00140 if (eventStack.top() != EXCLUDED)
00141 {
00142 TAU_PROFILER_STOP(eventStack.top());
00143 }
00144 eventStack.pop();
00145 }
00146
00147
00148 void _createTraceTau(char **argv)
00149 {
00150
00151 memset(events, 0, sizeof(void *)*5000);
00152
00153 dprintf("arguments:\n");
00154 dprintf("[0] = %s, ", argv[0]);
00155 dprintf("[1] = %s, ", argv[1]);
00156 dprintf("[2] = %s, ", argv[2]);
00157 dprintf("\n");
00158 string disable = "disable-profiling";
00159 if (argv[1] == NULL) { profile = true; }
00160 else if (argv[1] == disable) { profile = false; }
00161 if (not CkpvAccess(traceOn)) {
00162 dprintf("traceoff selected using snapshot profiling.\n");
00163 snapshotProfiling = true;
00164 }
00165
00166 CkpvInitialize(TraceTau*, _trace);
00167 CkpvAccess(_trace) = new TraceTau(argv);
00168 CkpvAccess(_traces)->addTrace(CkpvAccess(_trace));
00169 }
00170
00171 TraceTau::TraceTau(char **argv)
00172 {
00173 if (CkpvAccess(traceOnPe) == 0) return;
00174
00175
00176 CmiGetArgIntDesc(argv,"+TauPar0", &par0, "Fake integer parameter 0");
00177 CmiGetArgDoubleDesc(argv,"+TauPar1", &par1, "Fake double parameter 1");
00178
00179 if (profile)
00180 {
00181 if (strcmp(CkpvAccess(selective), ""))
00182 {
00183
00184
00185 processInstrumentationRequests(CkpvAccess(selective));
00186 printExcludeList();
00187 if (!instrumentEntity("Main::done(void)::99"))
00188 {
00189 dprintf("selective file working...\n");
00190 }
00191 else
00192 dprintf("selective flile not working...\n");
00193 }
00194
00195 TAU_PROFILER_CREATE(idle, "Idle", "", TAU_DEFAULT);
00196
00197 dprintf("before %p\n", comp);
00198 TAU_PROFILER_CREATE(comp, "Main", "", TAU_DEFAULT);
00199 dprintf("after %p\n", comp);
00200
00201
00202
00203 eventStack.push(EXCLUDED);
00204 }
00205 else
00206 {
00207 dprintf("--> [TAU] creating timers...\n");
00208 }
00209 }
00210
00211 void TraceTau::userEvent(int eventID)
00212 {
00213 dprintf("[%d] User Point Event id %d encountered\n", CkMyPe(), eventID);
00214 }
00215
00216 void TraceTau::userBracketEvent(int eventID, double bt, double et) {
00217 dprintf("[%d] User Bracket Event id %d encountered\n", CkMyPe(), eventID);
00218 }
00219
00220 void TraceTau::creation(envelope *, int epIdx, int num) {
00221 dprintf("[%d] Point-to-Point Message for Entry Method id %d sent\n",
00222 CkMyPe(), epIdx);
00223 }
00224
00225 void TraceTau::creationMulticast(envelope *, int epIdx, int num,
00226 const int *pelist) {
00227 dprintf("[%d] Multicast Message for Entry Method id %d sent to %d pes\n",
00228 CkMyPe(), epIdx, num);
00229 }
00230
00231 void TraceTau::creationDone(int num) {
00232 dprintf("[%d] Last initiated send completes\n", CkMyPe());
00233 }
00234
00235 void TraceTau::messageRecv(char *env, int pe) {
00236 dprintf("[%d] Message from pe %d received by scheduler\n",
00237 CkMyPe(), pe);
00238 }
00239
00240 void TraceTau::beginExecute(CmiObjId *tid)
00241 {
00242
00243
00244
00245 dprintf("[%d] Entry Method invoked using object id\n", CkMyPe());
00246 if (profile) {
00247 startEntryEvent(-1);
00248 }
00249 else
00250 {
00251 dprintf("--> [TAU] starting entry timer...\n");
00252 }
00253 }
00254
00255 void TraceTau::beginExecute(envelope *e)
00256 {
00257
00258 if (e == NULL) {
00259 dprintf("[%d] Entry Method invoked via thread id %d\n", CkMyPe(),
00260 _threadEP);
00261 if (profile) {
00262 startEntryEvent(-1);
00263 }
00264 else
00265 {
00266 dprintf("--> [TAU] starting entry timer...\n");
00267 }
00268
00269
00270 } else {
00271 dprintf("[%d] Entry Method %d invoked via message envelope\n",
00272 CkMyPe(), e->getEpIdx());
00273 if (profile) {
00274 startEntryEvent(e->getEpIdx());
00275 }
00276 else
00277 {
00278 dprintf("--> [TAU] starting entry timer...\n");
00279 }
00280
00281
00282 }
00283 }
00284
00285 void TraceTau::beginExecute(int event,int msgType,int ep,int srcPe,
00286 int mlen, CmiObjId *idx)
00287 {
00288 dprintf("[%d] Entry Method %d invoked by parameters\n", CkMyPe(),
00289 ep);
00290 if (profile) {
00291 startEntryEvent(ep);
00292 }
00293 else
00294 {
00295 dprintf("--> [TAU] starting entry timer...\n");
00296 }
00297 }
00298
00299 void TraceTau::endExecute(void)
00300 {
00301 if (profile) {
00302 stopEntryEvent();
00303 }
00304 else
00305 {
00306 dprintf("--> [TAU] stoping entry timer...\n");
00307 }
00308 dprintf("[%d] Previously executing Entry Method completes\n", CkMyPe());
00309 }
00310
00311 void TraceTau::beginIdle(double curWallTime) {
00312 dprintf("[%d] Scheduler has no useful user-work\n", CkMyPe());
00313 if (profile) {
00314 TAU_PROFILER_START(idle);
00315 }
00316 else
00317 {
00318 dprintf("--> [TAU] starting idle timer...\n");
00319 }
00320 }
00321
00322 void TraceTau::endIdle(double curWallTime) {
00323 if (profile) {
00324 TAU_PROFILER_STOP(idle);
00325 }
00326 else
00327 {
00328 dprintf("--> [TAU] stopping idle timer...\n");
00329 }
00330 dprintf("[%d] Scheduler now has useful user-work\n", CkMyPe());
00331 }
00332
00333 void TraceTau::beginComputation(void)
00334 {
00335 dprintf("[%d] Computation Begins\n", CkMyPe());
00336
00337
00338
00339
00340 }
00341
00342 void TraceTau::endComputation(void)
00343 {
00344 dprintf("[%d] Computation Ends\n", CkMyPe());
00345 }
00346
00347 void TraceTau::traceBegin(void)
00348 {
00349 dprintf("[%d] >>>>>> Tracing Begins\n", CkMyPe());
00350 if (profile) {
00351 dprintf("ptr: %p\n", comp);
00352 TAU_DB_PURGE();
00353 TAU_ENABLE_ALL_GROUPS();
00354 TAU_PROFILER_START(comp);
00355 }
00356 else
00357 {
00358 dprintf("--> [TAU] starting computation timer...\n");
00359 }
00360 }
00361
00362 void TraceTau::traceEnd(void)
00363 {
00364 dprintf("[%d] >>>>>> Tracing Ends\n", CkMyPe());
00365 if (profile){
00366 dprintf("ptr: %p\n", comp);
00367
00368 TAU_PROFILE_EXIT("tracing complete.");
00369 TAU_DISABLE_ALL_GROUPS();
00370 }
00371 else
00372 {
00373 dprintf("--> [TAU] stopping computation timer and writing profiles\n");
00374 }
00375 dprintf("[%d] Computation Ends\n", CkMyPe());
00376 }
00377
00378 void TraceTau::malloc(void *where, int size, void **stack, int stackSize)
00379 {
00380 dprintf("[%d] Memory allocation of size %d occurred\n", CkMyPe(), size);
00381 }
00382
00383 void TraceTau::free(void *where, int size) {
00384 dprintf("[%d] %d-byte Memory block freed\n", CkMyPe(), size);
00385 }
00386
00387 void TraceTau::traceClose(void)
00388 {
00389 dprintf("traceClose called.\n");
00390 CkpvAccess(_trace)->endComputation();
00391 CkpvAccess(_trace)->traceEnd();
00392
00393
00394
00395
00396 CkpvAccess(_traces)->removeTrace(this);
00397 }
00398
00399 extern "C" void traceTauExitFunction() {
00400 dprintf("traceTauExitFunction called.\n");
00401
00402
00403
00404 CkContinueExit();
00405 }
00406
00407
00408 void initTraceTauBOC() {
00409
00410 dprintf("tracetauboc setting node %d\n", CmiMyPe());
00411 if (profile) {
00412 TAU_PROFILE_SET_NODE(CmiMyPe());
00413 }
00414 else
00415 {
00416 dprintf("---> [TAU] settting node.\n");
00417 }
00418
00419
00420 #ifdef __BIGSIM__
00421 if (BgNodeRank()==0) {
00422 #else
00423 if (CkMyRank() == 0) {
00424 #endif
00425 registerExitFn(traceTauExitFunction);
00426 }
00427 }
00428
00429 #include "TraceTau.def.h"