00001
00005
00006
00007 #ifndef _TRACE_UTILIZATION_H
00008 #define _TRACE_UTILIZATION_H
00009
00010 #include <stdio.h>
00011 #include <errno.h>
00012 #include <deque>
00013
00014 #include "charm++.h"
00015
00016
00017 #include "trace.h"
00018 #include "envelope.h"
00019 #include "register.h"
00020 #include "trace-common.h"
00021 #include "ckcallback-ccs.h"
00022
00023 #include "TraceUtilization.decl.h"
00024
00025 #define INVALIDEP -2
00026 #define TRACEON_EP -3
00027 #define NUM_DUMMY_EPS 9
00028
00029
00030
00031 #define BIN_PER_SEC 1000
00032 #define BIN_SIZE 0.001
00033 #define NUM_BINS 32768
00034
00035
00037 #define numBins_T int
00038 #define numProcs_T int
00039 #define entriesInBin_T short
00040 #define ep_T short
00041 #define utilization_T unsigned char
00042 #define other_EP 10000
00043
00044
00045 extern CProxy_TraceUtilizationBOC traceUtilizationGroupProxy;
00046
00047 void collectUtilizationData(void *data, double currT);
00048
00049
00050
00052 class TraceUtilizationInit : public Chare {
00053 public:
00054 TraceUtilizationInit(CkArgMsg *m) {
00055 CkPrintf("[%d] TraceUtilizationInit creating traceUtilizationGroupProxy");
00056 fflush(stdout);
00057
00058 traceUtilizationGroupProxy = CProxy_TraceUtilizationBOC::ckNew();
00059
00060 CkPrintf("Trace Summary now listening in for CCS Client\n");
00061 CcsRegisterHandler("CkPerfSumDetail compressed", CkCallback(CkIndex_TraceUtilizationBOC::ccsRequestSumDetailCompressed(NULL), traceUtilizationGroupProxy[0]));
00062
00063 CkPrintf("[%d] Setting up periodic startCollectData callback\n", CkMyPe());
00064 CcdCallOnConditionKeep(CcdPERIODIC_1second, collectUtilizationData, (void *)NULL);
00065
00066 }
00067 };
00068
00069
00070
00071
00072
00073
00081 class compressedBuffer {
00082 public:
00083 char* buf;
00084 int pos;
00085
00086 compressedBuffer(){
00087 buf = NULL;
00088 pos = 0;
00089 }
00090
00091 compressedBuffer(int bytes){
00092 buf = (char*)malloc(bytes);
00093 pos = 0;
00094 }
00095
00096 compressedBuffer(void *buffer){
00097 buf = (char*)buffer;
00098 pos = 0;
00099 }
00100
00101 void init(void *buffer){
00102 buf = (char*)buffer;
00103 pos = 0;
00104 }
00105
00106 inline void * currentPtr(){
00107 return (void*)(buf+pos);
00108 }
00109
00110 template <typename T>
00111 T read(int offset){
00112
00113 T v;
00114 memcpy(&v, buf+offset, sizeof(T));
00115 return v;
00116 }
00117
00118 template <typename T>
00119 void write(T v, int offset){
00120 T v2 = v;
00121
00122 memcpy(buf+offset, &v2, sizeof(T));
00123 }
00124
00125 template <typename T>
00126 void increment(int offset){
00127 T temp;
00128 temp = read<T>(offset);
00129 temp ++;
00130 write<T>(temp, offset);
00131 }
00132
00133 template <typename T>
00134 void accumulate(T v, int offset){
00135 T temp;
00136 temp = read<T>(offset);
00137 temp += v;
00138 write<T>(temp, offset);
00139 }
00140
00141 template <typename T>
00142 int push(T v){
00143 int oldpos = pos;
00144 write<T>(v, pos);
00145 pos += sizeof(T);
00146 return oldpos;
00147 }
00148
00149 template <typename T>
00150 T pop(){
00151 T temp = read<T>(pos);
00152 pos += sizeof(T);
00153 return temp;
00154 }
00155
00156 template <typename T>
00157 T peek(){
00158 T temp = read<T>(pos);
00159 return temp;
00160 }
00161
00162 template <typename T0, typename T>
00163 T peekSecond(){
00164 T temp;
00165 memcpy(&temp, buf+pos+sizeof(T0), sizeof(T));
00166 return temp;
00167 }
00168
00169 int datalength(){
00170 return pos;
00171 }
00172
00173 void * buffer(){
00174 return (void*) buf;
00175 }
00176
00177 void freeBuf(){
00178 free(buf);
00179 }
00180
00181 ~compressedBuffer(){
00182
00183 }
00184
00185 };
00186
00187
00188
00189 compressedBuffer compressAvailableNewSumDetail(int max=10000);
00190 void mergeCompressedBin(compressedBuffer *srcBufferArray, int numSrcBuf, int *numProcsRepresentedInMessage, int totalProcsAcrossAllMessages, compressedBuffer &destBuffer);
00191
00192 CkReductionMsg *sumDetailCompressedReduction(int nMsg,CkReductionMsg **msgs);
00193 void printCompressedBuf(compressedBuffer b);
00194 compressedBuffer fakeCompressedMessage();
00195 compressedBuffer emptyCompressedBuffer();
00196 void sanityCheckCompressedBuf(compressedBuffer b);
00197 bool isCompressedBufferSane(compressedBuffer b);
00198 double averageUtilizationInBuffer(compressedBuffer b);
00199
00200
00201
00202
00203
00204 class TraceUtilization : public Trace {
00205 public:
00206 int execEp;
00207 double start;
00208
00209 unsigned int epInfoSize;
00210
00211 double *cpuTime;
00212 int lastBinUsed;
00213 unsigned int numBinsSent;
00214 unsigned int previouslySentBins;
00215
00216
00217 TraceUtilization() {
00218 execEp == TRACEON_EP;
00219 cpuTime = NULL;
00220 lastBinUsed = -1;
00221 numBinsSent = 0;
00222 }
00223
00224
00226 void initMem(){
00227 int _numEntries=_entryTable.size();
00228 epInfoSize = _numEntries + NUM_DUMMY_EPS + 1;
00229
00230 cpuTime = new double[NUM_BINS*epInfoSize];
00231 _MEMCHECK(cpuTime);
00232
00233 if(CkMyPe() == 0)
00234 writeSts();
00235
00236 }
00237
00238 void writeSts(void);
00239
00240 void creation(envelope *e, int epIdx, int num=1) {}
00241
00242 void beginExecute(envelope *e);
00243 void beginExecute(CmiObjId *tid);
00244 void beginExecute(int event,int msgType,int ep,int srcPe, int mlen=0, CmiObjId *idx=NULL);
00245 void endExecute(void);
00246 void beginIdle(double currT) {}
00247 void endIdle(double currT) {}
00248 void beginPack(void){}
00249 void endPack(void) {}
00250 void beginUnpack(void) {}
00251 void endUnpack(void) {}
00252 void beginComputation(void) { initMem(); }
00253 void endComputation(void) {}
00254 void traceClearEps() {}
00255 void traceWriteSts() {}
00256 void traceClose() {}
00257
00258 void addEventType(int eventType);
00259
00260 int cpuTimeEntriesAvailable() const { return lastBinUsed+1; }
00261 int cpuTimeEntriesSentSoFar() const { return numBinsSent; }
00262 void incrementNumCpuTimeEntriesSent(int n) { numBinsSent += n; }
00263
00264
00265 double sumUtilization(int startBin, int endBin);
00266
00267
00268 void updateCpuTime(int epIdx, double startTime, double endTime){
00269
00270
00271
00272 if (epIdx >= epInfoSize) {
00273 CkPrintf("WARNING: epIdx=%d >= epInfoSize=%d\n", (int)epIdx, (int)epInfoSize );
00274 return;
00275 }
00276
00277 int startingBinIdx = (int)(startTime/BIN_SIZE);
00278 int endingBinIdx = (int)(endTime/BIN_SIZE);
00279
00280 if (startingBinIdx == endingBinIdx) {
00281 addToCPUtime(startingBinIdx, epIdx, endTime - startTime);
00282 } else if (startingBinIdx < endingBinIdx) {
00283 addToCPUtime(startingBinIdx, epIdx, (startingBinIdx+1)*BIN_SIZE - startTime);
00284 while(++startingBinIdx < endingBinIdx)
00285 addToCPUtime(startingBinIdx, epIdx, BIN_SIZE);
00286 addToCPUtime(endingBinIdx, epIdx, endTime - endingBinIdx*BIN_SIZE);
00287 }
00288 }
00289
00290
00292 inline void zeroIfNecessary(unsigned int interval){
00293 for(unsigned int i=lastBinUsed+1; i<= interval; i++){
00294
00295 for(unsigned int j=0;j<epInfoSize;j++){
00296 cpuTime[(i%NUM_BINS)*epInfoSize+j] = 0.0;
00297 }
00298 }
00299 lastBinUsed = interval;
00300 }
00301
00302 UInt getEpInfoSize() { return epInfoSize; }
00303
00305 inline double getCPUtime(unsigned int interval, unsigned int ep) {
00306 CkAssert(ep < epInfoSize);
00307 if(cpuTime != NULL && interval <= lastBinUsed)
00308 return cpuTime[(interval%NUM_BINS)*epInfoSize+ep];
00309 else {
00310 CkPrintf("getCPUtime called with invalid options: cpuTime=%p interval=%d ep=%d\n", cpuTime, (int)interval, (int)ep);
00311 return 0.0;
00312 }
00313 }
00314
00315 inline void addToCPUtime(unsigned int interval, unsigned int ep, double val){
00316
00317 zeroIfNecessary(interval);
00318
00319 cpuTime[(interval%NUM_BINS)*epInfoSize+ep] += val;
00320 }
00321
00322
00323 inline double getUtilization(int interval, int ep){
00324 return getCPUtime(interval, ep) * 100.0 * (double)BIN_PER_SEC;
00325 }
00326
00327
00328 compressedBuffer compressNRecentSumDetail(int desiredBinsToSend);
00329
00330
00331 };
00332
00333
00334
00335
00336
00337
00338 class TraceUtilizationBOC : public CBase_TraceUtilizationBOC {
00339
00340 std::deque<CkReductionMsg *> storedSumDetailResults;
00341
00342 public:
00343 TraceUtilizationBOC() {}
00344 TraceUtilizationBOC(CkMigrateMessage* msg) {}
00345 ~TraceUtilizationBOC() {}
00346
00347
00349 void ccsRequestSumDetailCompressed(CkCcsRequestMsg *m);
00350 void collectSumDetailData();
00351 void sumDetailDataCollected(CkReductionMsg *);
00352
00353 };
00354
00355
00356
00357
00358
00359 #endif
00360