ck-perf/trace.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * $Source: /cvsroot/charm/src/ck-perf/trace.h,v $
00003  * $Author: gioachin $
00004  * $Date: 2008-03-26 03:21:30 $
00005  * $Revision: 2.78 $
00006  *****************************************************************************/
00007 
00008 #ifndef _TRACE_H
00009 #define _TRACE_H
00010 
00011 #if CMK_HAS_COUNTER_PAPI
00012 #include <papi.h>
00013 #endif
00014 
00015 class envelope;
00016 
00021 
00022 // An additional interface for summary data
00023 extern "C" void traceClearEps();
00024 extern double CmiTraceTimer();
00025 
00026 extern int _dummyMsg, _dummyChare, _dummyEP;
00027 
00028 /* CW Support for Thread Listener interface */
00029 extern "C" void traceAddThreadListeners(CthThread tid, envelope *e);
00030 
00031 // trace_in_charm means only do trace for Charm++ level, skip converse tracing
00032 // Cpv traceOnPe controls if charm pe will generate trace logs (a default value)
00033 // while traceOnPe flag in each trace module can also control independently if 
00034 // tracing is wanted for each module
00035 CkpvExtern(int, traceOnPe);
00036 
00037 // A hack. We need to somehow tell the pup framework what size
00038 // long_long is wrt PAPI.
00039 #if CMK_HAS_COUNTER_PAPI
00040 typedef long_long LONG_LONG_PAPI;
00041 #else
00042 typedef CMK_TYPEDEF_INT8 LONG_LONG_PAPI;
00043 #endif
00044 
00045 // Base class of all tracing strategies.
00046 // 
00047 class Trace {
00048   protected:
00049     int _traceOn;
00050   public:
00051     Trace(): _traceOn(0) {}
00052     virtual void setTraceOnPE(int flag) { _traceOn = flag; }
00053     virtual inline int traceOnPE() { return _traceOn; }
00054     // turn trace on/off, note that charm will automatically call traceBegin()
00055     // at the beginning of every run unless the command line option "+traceoff"
00056     // is specified
00057     virtual void traceBegin() {}
00058     virtual void traceEnd() {}
00059     // registers user event trace module returns int identifier 
00060     virtual int traceRegisterUserEvent(const char* eventName, int e) { 
00061       return 0; 
00062     }
00063     // a user event has just occured
00064     virtual void userEvent(int eventID) {}
00065     // a pair of begin/end user event has just occured
00066     virtual void userBracketEvent(int eventID, double bt, double et) {}
00067 
00068         // a user supplied integer value(likely a timestep)
00069         virtual void userSuppliedData(int e) {}
00070 
00071         // the current memory usage as a double
00072         virtual void memoryUsage(double currentMemUsage) {}
00073         
00074 
00075     // creation of message(s)
00076     virtual void creation(envelope *, int epIdx, int num=1) {}
00077     virtual void creationMulticast(envelope *, int epIdx, int num=1,
00078                                      int *pelist=NULL) {}
00079     virtual void creationDone(int num=1) {}
00080     // ???
00081     virtual void messageRecv(char *env, int pe) {}
00082     virtual void beginSDAGBlock(
00083       int event,   // event type defined in trace-common.h
00084       int msgType, // message type
00085       int ep,      // Charm++ entry point (will correspond to sts file) 
00086       int srcPe,   // Which PE originated the call
00087       int ml,      // message size
00088       CmiObjId* idx)    // index
00089     { }
00090     virtual void endSDAGBlock(void) {}
00091     // **************************************************************
00092     // begin/end execution of a Charm++ entry point
00093     // NOTE: begin/endPack and begin/endUnpack can be called in between
00094     //       a beginExecute and its corresponding endExecute.
00095     virtual void beginExecute(envelope *) {}
00096     virtual void beginExecute(CmiObjId *tid) {}
00097     virtual void beginExecute(
00098       int event,   // event type defined in trace-common.h
00099       int msgType, // message type
00100       int ep,      // Charm++ entry point (will correspond to sts file) 
00101       int srcPe,   // Which PE originated the call
00102       int ml,      // message size
00103       CmiObjId* idx)    // index
00104     { }
00105     virtual void endExecute(void) {}
00106     // begin/end idle time for this pe
00107     virtual void beginIdle(double curWallTime) {}
00108     virtual void endIdle(double curWallTime) {}
00109     // begin/end the process of packing a message (to send)
00110     virtual void beginPack(void) {}
00111     virtual void endPack(void) {}
00112     // begin/end the process of unpacking a message (can occur before calling
00113     // a entry point or during an entry point when 
00114     virtual void beginUnpack(void) {}
00115     virtual void endUnpack(void) {}
00116     // ???
00117     virtual void enqueue(envelope *) {}
00118     virtual void dequeue(envelope *) {}
00119     // begin/end of execution
00120     virtual void beginComputation(void) {}
00121     virtual void endComputation(void) {}
00122     // clear all data collected for entry points
00123     virtual void traceClearEps() {}
00124     // write the summary sts file for this trace
00125     virtual void traceWriteSts() {}
00126     // do any clean-up necessary for tracing
00127     virtual void traceClose() {}
00128     // flush log buffer immediately
00129     virtual void traceFlushLog() {}
00130 
00131     //for tracing function calls
00132     virtual void regFunc(const char *name, int &idx, int idxSpecifiedByUser=0){}
00133     virtual void beginFunc(char *name,char *file,int line){}
00134     virtual void beginFunc(int idx,char *file,int line){}
00135     virtual void endFunc(char *name){}    
00136     virtual void endFunc(int idx){}
00137 
00138     /* Memory tracing */
00139     virtual void malloc(void *where, int size, void **stack, int stackSize){}
00140     virtual void free(void *where, int size){}
00141 
00142     /* for implementing thread listeners */
00143     virtual void traceAddThreadListeners(CthThread tid, envelope *e) {}
00144 
00145     virtual ~Trace() {} /* for whining compilers */
00146 };
00147 
00148 #define ALLDO(x) for (int i=0; i<length(); i++) if (traces[i]->traceOnPE()) traces[i]->x
00149 #define ALLREVERSEDO(x) for (int i=length()-1; i>=0; i--) if (traces[i]->traceOnPE()) traces[i]->x
00150 
00152 class TraceArray {
00153 private:
00154   CkVec<Trace *>  traces;
00155   int n;
00156   int cancel_beginIdle, cancel_endIdle;
00157 public:
00158     TraceArray(): n(0) {}
00159     inline void addTrace(Trace *tr) { traces.push_back(tr); n++;}
00160     inline void setTraceOnPE(int flag) { for (int i=0; i<length(); i++) traces[i]->setTraceOnPE(flag); }
00161     // to allow traceCLose() to be called multiple times, remove trace module
00162     // from the array in each individual trace, and clean up (clearTrace)
00163     // after the loop.
00164     inline void removeTrace(Trace *tr) {    // remove a Trace from TraceArray
00165         int i;
00166         for (i=0; i<n; i++) if (tr == traces[i]) break;
00167         CmiAssert(i<n);
00168         traces[i] = NULL;
00169     }
00170     inline void clearTrace() {    // remove holes in TraceArray
00171         int len = traces.length();
00172         int removed = 0;
00173         for (int i=0; i<len; i++) {
00174           if (traces[i-removed] == NULL) { traces.remove(i-removed); removed++; }
00175         }
00176         n -= removed;
00177     }
00178     inline int length() const { return n; }
00179 
00180     inline void userEvent(int e) { ALLDO(userEvent(e));}
00181     inline void userBracketEvent(int e,double bt, double et) {ALLDO(userBracketEvent(e,bt,et));}
00182     
00183         inline void userSuppliedData(int d) { ALLDO(userSuppliedData(d));}
00184 
00185         inline void memoryUsage(double memUsage) { ALLDO(memoryUsage(memUsage));}
00186 
00187 
00188     /* Creation needs to access _entryTable, so moved into trace-common.C */
00189     void creation(envelope *env, int ep, int num=1);
00190     void creationMulticast(envelope *env, int ep, int num=1, int *pelist=NULL);
00191     
00192     inline void creationDone(int num=1) { ALLDO(creationDone(num)); }
00193     inline void beginSDAGBlock(int event,int msgType,int ep,int srcPe, int mlen,CmiObjId *idx=NULL) {ALLDO(beginSDAGBlock(event, msgType, ep, srcPe, mlen,idx));}
00194     inline void endSDAGBlock(void) {ALLREVERSEDO(endExecute());}
00195     inline void beginExecute(envelope *env) {ALLDO(beginExecute(env));}
00196     inline void beginExecute(CmiObjId *tid) {ALLDO(beginExecute(tid));}
00197     inline void beginExecute(int event,int msgType,int ep,int srcPe, int mlen,CmiObjId *idx=NULL) {ALLDO(beginExecute(event, msgType, ep, srcPe, mlen,idx));}
00198     inline void endExecute(void) {ALLREVERSEDO(endExecute());}
00199     inline void messageRecv(char *env, int pe) {ALLDO(messageRecv(env, pe));}
00200     inline void beginPack(void) {ALLDO(beginPack());}
00201     inline void endPack(void) {ALLDO(endPack());}
00202     inline void beginUnpack(void) {ALLDO(beginUnpack());}
00203     inline void endUnpack(void) {ALLDO(endUnpack());}
00204     inline void enqueue(envelope *e) {ALLDO(enqueue(e));}
00205     inline void dequeue(envelope *e) {ALLDO(dequeue(e));}
00206     inline void beginComputation(void) {ALLDO(beginComputation());}
00207     inline void endComputation(void) {ALLDO(endComputation());}
00208     inline int traceRegisterUserEvent(const char*x, int evt) {
00209           int eno = 0;
00210           for (int i=0; i<length(); i++) {
00211             if (traces[i]->traceOnPE() == 0) {
00212               continue;
00213             }
00214             int e = traces[i]->traceRegisterUserEvent(x, evt);
00215             if (e) eno = e;
00216           }
00217           return eno;
00218     }  
00219     inline void traceClearEps() {ALLDO(traceClearEps());}
00220     inline void traceWriteSts() {ALLDO(traceWriteSts());}
00221     inline void traceClose() {ALLDO(traceClose()); clearTrace();}
00222     inline void traceFlushLog() {ALLDO(traceFlushLog());}
00223     
00224     // Tracing module registers *itself* for begin/end idle callbacks:
00225     inline void beginIdle(double curWallTime) {ALLDO(beginIdle(curWallTime));}
00226     inline void endIdle(double curWallTime) {ALLDO(endIdle(curWallTime));}
00227     void traceBegin();
00228     void traceEnd();
00229 
00230     /*Calls for tracing function begins and ends*/
00231     inline void regFunc(const char *name, int &idx, int idxSpecifiedByUser=0){ ALLDO(regFunc(name, idx, idxSpecifiedByUser)); }
00232     inline void beginFunc(char *name,char *file,int line){ ALLDO(beginFunc(name,file,line)); };
00233     inline void beginFunc(int idx,char *file,int line){ ALLDO(beginFunc(idx,file,line)); };
00234     inline void endFunc(char *name){ ALLDO(endFunc(name)); }
00235     inline void endFunc(int idx){ ALLDO(endFunc(idx)); }
00236 
00237     /* Memory tracing */
00238     inline void malloc(void *where, int size, void **stack, int stackSize){ ALLDO(malloc(where,size,stack,stackSize)); }
00239     inline void free(void *where, int size){ ALLDO(free(where, size)); }
00240 
00241     /* calls for thread listener registration for each trace module */
00242     inline void traceAddThreadListeners(CthThread tid, envelope *e) {
00243       ALLDO(traceAddThreadListeners(tid, e));
00244     }
00245 };
00246 
00247 CkpvExtern(TraceArray*, _traces);
00248 
00249 #ifndef CMK_OPTIMIZE
00250 #  define _TRACE_ONLY(code) do{if(CpvAccess(traceOn)){ code; }} while(0)
00251 #  define _TRACE_ALWAYS(code) do{ code; } while(0)
00252 #else
00253 #  define _TRACE_ONLY(code) /*empty*/
00254 #  define _TRACE_ALWAYS(code) /*empty*/
00255 #endif
00256 
00257 extern "C" {
00258 #include "conv-trace.h"
00259 }
00260 
00261 #define _TRACE_USER_EVENT(x) _TRACE_ONLY(CkpvAccess(_traces)->userEvent(x))
00262 #define _TRACE_USER_EVENT_BRACKET(x,bt,et) _TRACE_ONLY(CkpvAccess(_traces)->userBracketEvent(x,bt,et))
00263 #define _TRACE_CREATION_1(env) _TRACE_ONLY(CkpvAccess(_traces)->creation(env,env->getEpIdx()))
00264 #define _TRACE_CREATION_DETAILED(env,ep) _TRACE_ONLY(CkpvAccess(_traces)->creation(env,ep))
00265 #define _TRACE_CREATION_N(env, num) _TRACE_ONLY(CkpvAccess(_traces)->creation(env, env->getEpIdx(), num))
00266 #define _TRACE_CREATION_MULTICAST(env, num, pelist) _TRACE_ONLY(CkpvAccess(_traces)->creationMulticast(env, env->getEpIdx(), num, pelist))
00267 #define _TRACE_CREATION_DONE(num) _TRACE_ONLY(CkpvAccess(_traces)->creationDone(num))
00268 #define _TRACE_BEGIN_SDAG(env) _TRACE_ONLY(CkpvAccess(_traces)->beginSDAGBlock(env))
00269 #define _TRACE_END_SDAG(env) _TRACE_ONLY(CkpvAccess(_traces)->endSDAGBlock(env))
00270 #define _TRACE_BEGIN_EXECUTE(env) _TRACE_ONLY(CkpvAccess(_traces)->beginExecute(env))
00271 #define _TRACE_BEGIN_EXECUTE_DETAILED(evt,typ,ep,src,mlen,idx) \
00272         _TRACE_ONLY(CkpvAccess(_traces)->beginExecute(evt,typ,ep,src,mlen,idx))
00273 #define _TRACE_END_EXECUTE() _TRACE_ONLY(CkpvAccess(_traces)->endExecute())
00274 #define _TRACE_MESSAGE_RECV(env, pe) _TRACE_ONLY(CkpvAccess(_traces)->messageRecv(env, pe))
00275 #define _TRACE_BEGIN_PACK() _TRACE_ONLY(CkpvAccess(_traces)->beginPack())
00276 #define _TRACE_END_PACK() _TRACE_ONLY(CkpvAccess(_traces)->endPack())
00277 #define _TRACE_BEGIN_UNPACK() _TRACE_ONLY(CkpvAccess(_traces)->beginUnpack())
00278 #define _TRACE_END_UNPACK() _TRACE_ONLY(CkpvAccess(_traces)->endUnpack())
00279 #define _TRACE_BEGIN_COMPUTATION() _TRACE_ALWAYS(CkpvAccess(_traces)->beginComputation())
00280 #define _TRACE_END_COMPUTATION() _TRACE_ALWAYS(CkpvAccess(_traces)->endComputation())
00281 #define _TRACE_ENQUEUE(env) _TRACE_ONLY(CkpvAccess(_traces)->enqueue(env))
00282 #define _TRACE_DEQUEUE(env) _TRACE_ONLY(CkpvAccess(_traces)->dequeue(env))
00283 
00284 /* Memory tracing */
00285 #define _TRACE_MALLOC(where, size, stack, stackSize) _TRACE_ONLY(CkpvAccess(_traces)->malloc(where,size,stack,stackSize))
00286 #define _TRACE_FREE(where, size) _TRACE_ONLY(CkpvAccess(_traces)->free(where, size))
00287 
00288 
00289 #include "trace-bluegene.h"
00290 
00291 #endif
00292 
00293 

Generated on Sun Jun 29 13:29:11 2008 for Charm++ by  doxygen 1.5.1