📄 mal_profiler.c
字号:
#line 230 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"#include "mal_config.h"#include "mal_function.h"#include "mal_profiler.h"#include "mal_debugger.h"#ifdef MALprofilerstatic MT_Lock profileLock ;static int profileLockInitialized = 0;static stream *eventstream = 0;static int onlineProfiling = TRUE;static int offlineProfiling = FALSE;static int cachedProfiling = FALSE;intprofilerAvailable(void){ return 1;}#elseintprofilerAvailable(void){ return 0;}#endifstatic void onlineProfilerEvent(Module cntxt, MalBlkPtr mb, MalStkPtr stk, int pc);static void offlineProfilerEvent(Module cntxt, MalBlkPtr mb, MalStkPtr stk, int pc);static void cachedProfilerEvent(Module cntxt, MalBlkPtr mb, MalStkPtr stk, int pc);int malProfileMode = 0; /* global flag to indicate profiling mode */static int profileAll = 0; /* all instructions should be profiled */static int delayswitch = 0; /* to wait before sending the profile info */#line 265 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"#define PROFtime 0#define PROFlabel 1#define PROFcpu 2#define PROFmemory 3#define PROFio 4#define PROFpc 5#define PROFevent 6#define PROFstmt 7static struct { str name; /* which logical counter is needed */ int status; /* trace it or not */} profileCounter[] = { /* 0 */ { "time", 0}, /* 1 */ { "ticks", 0}, /* 2 */ { "cpu", 0}, /* 3 */ { "memory", 0}, /* 4 */ { "io", 0}, /* 5 */ { "pc", 0}, /* 6 */ { "event", 0}, /* 7 */ { "statement", 0}, /* 8 */ { 0, 0}};#line 302 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"stractivateCounter(str name){ #line 294 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx" int i; for(i=0; profileCounter[i].name;i++) if( strcmp(profileCounter[i].name,name)==0){ profileCounter[i].status = 1 ; return 0; } throw(MAL, " activateCounter", "profile counter '%s' undefined",name);#line 305 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"}strdeactivateCounter(str name){ #line 294 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx" int i; for(i=0; profileCounter[i].name;i++) if( strcmp(profileCounter[i].name,name)==0){ profileCounter[i].status = 0 ; return 0; } throw(MAL, " deactivateCounter", "profile counter '%s' undefined",name);#line 311 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"}#line 334 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"voidprofilerEvent(Module cntxt, MalBlkPtr mb, MalStkPtr stk, int pc){#ifdef MALprofiler if( mb->profiler == NULL) return; if (onlineProfiling) onlineProfilerEvent(cntxt, mb, stk, pc); if (offlineProfiling) offlineProfilerEvent(cntxt, mb, stk, pc); if (cachedProfiling) cachedProfilerEvent(cntxt, mb, stk, pc);#endif}#line 352 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"#define log(X,Y) stream_printf(eventstream,X,Y);#define log0(X) stream_printf(eventstream,X);#define log2(X,Y,Z) stream_printf(eventstream,X,Y,Z);#define flushLog() stream_flush(eventstream);#ifdef MALprofilerstatic voidonlineProfilerHeader(void){ #line 320 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx" if( ! profileLockInitialized) { MT_lock_init(&profileLock); profileLockInitialized= 1; } mal_set_lock(profileLock,"profileLock"); if( eventstream == NULL) { mal_unset_lock(profileLock,"profileLock"); return ; }#line 362 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx" /* profilerEvent -> void */ log0("# "); if (profileCounter[PROFpc].status) { log0("caller,\t"); log0("callee,\t"); } if (profileCounter[PROFtime].status) { log0("time,\t"); } if (profileCounter[PROFlabel].status) { log0("ticks,\t"); } if (profileCounter[PROFstmt].status) { log0("stmt,\t"); } if (profileCounter[PROFcpu].status) { log0("utime,\t"); log0("cutime,\t"); log0("stime,\t"); log0("cstime,\t"); } if (profileCounter[PROFmemory].status) { log0("maxrss,\t"); log0("arena,\t"); log0("ordblks,\t"); log0("smblks,\t"); log0("hblkhd,\t"); log0("hblks,\t"); log0("fsmblks,\t"); log0("uordblks,\t"); } if (profileCounter[PROFio].status) { log0("page reclaim,\t"); log0("page faults,\t"); log0("swaps,\t"); log0("block reads,\t"); log0("block writes,\t"); log0("context switch,\t"); log0("involunarty switch,\t"); } if (profileCounter[PROFevent].status) { log0("event\t# name\n"); } else log0("# name\n"); flushLog(); mal_unset_lock(profileLock, "profileLock");}voidonlineProfilerEvent(Module cntxt, MalBlkPtr mb, MalStkPtr stk, int pc){ static struct mallinfo prevMalloc; InstrPtr pci = getInstrPtr(mb,pc);#ifdef HAVE_SYS_RESOURCE_H static struct rusage prevUsage; struct rusage infoUsage;#endif static int eventcounter;#ifdef HAVE_TIMES struct tms newTms;#endif struct mallinfo infoMalloc; str stmt, c; (void) cntxt; if (delayswitch > 0) { /* first call to profiled */ onlineProfilerHeader(); delayswitch--; return; } #line 320 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx" if( ! profileLockInitialized) { MT_lock_init(&profileLock); profileLockInitialized= 1; } mal_set_lock(profileLock,"profileLock"); if( eventstream == NULL) { mal_unset_lock(profileLock,"profileLock"); return ; }#line 436 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx" /* profilerEvent -> void */ if (delayswitch == 0) { delayswitch = -1; } if (!profileAll && mb->profiler[pc].trace == FALSE) return;#ifdef HAVE_TIMES times(&newTms);#endif infoMalloc = MT_mallinfo();#ifdef HAVE_SYS_RESOURCE_H getrusage(RUSAGE_SELF, &infoUsage);#endif /* make basic profile event tuple */ log0("[ "); if (profileCounter[PROFpc].status) { str nme = ""; if (stk->blk) nme = getFunctionId(getInstrPtr(stk->blk, 0)); /* get identity of caller */ log2("%s.%d,\t", nme, getPC(mb, pci)); if (getModuleId(pci) && getFunctionId(pci)) { log2("\"%s.%s\",\t", getModuleId(pci), getFunctionId(pci)); } else { log("\"%s\",\t", operatorName(pci->token)); } } if (profileCounter[PROFtime].status) { char *tbuf, *c; tbuf= ctime(&mb->profiler[pc].clock); c = strchr(tbuf, '\n'); if (c) { c[-5] = '"'; c[-4] = 0; } tbuf[10] = '"'; log("%s,\t", tbuf + 10); } if (profileCounter[PROFlabel].status) { log("%d,\t", mb->profiler[pc].ticks); } if (profileCounter[PROFstmt].status) { /* generate actual call statement */ stmt = call2str(MCgetClient(),mb, stk, getPC(mb, pci)); c = stmt+1; while (c && *c && isspace((int) *c)) c++; log(" \"%s\",\t", c); }#ifdef HAVE_TIMES if (profileCounter[PROFcpu].status) { log("%d,\t", newTms.tms_utime - mb->profiler[pc].timer.tms_utime); log("%d,\t", newTms.tms_cutime - mb->profiler[pc].timer.tms_cutime); log("%d,\t", newTms.tms_stime - mb->profiler[pc].timer.tms_stime); log("%d,\t", newTms.tms_cstime - mb->profiler[pc].timer.tms_cstime); }#endif if (profileCounter[PROFmemory].status) {#ifdef HAVE_SYS_RESOURCE_H log("%d,\t", infoUsage.ru_maxrss);#endif log("%d,\t", infoMalloc.arena - prevMalloc.arena); log("%d,\t", infoMalloc.ordblks - prevMalloc.ordblks); log("%d,\t", infoMalloc.smblks - prevMalloc.smblks); log("%d,\t", infoMalloc.hblkhd - prevMalloc.hblkhd); log("%d,\t", infoMalloc.hblks - prevMalloc.hblks); log("%d,\t", infoMalloc.fsmblks - prevMalloc.fsmblks); log("%d,\t", infoMalloc.uordblks - prevMalloc.uordblks); prevMalloc = infoMalloc; }#ifdef HAVE_SYS_RESOURCE_H if (profileCounter[PROFio].status) { log("%d,\t", infoUsage.ru_minflt - prevUsage.ru_minflt); log("%d,\t", infoUsage.ru_majflt - prevUsage.ru_majflt); log("%d,\t", infoUsage.ru_nswap - prevUsage.ru_nswap); log("%d,\t", infoUsage.ru_inblock - prevUsage.ru_inblock); log("%d,\t", infoUsage.ru_oublock - prevUsage.ru_oublock); log("%d,\t", infoUsage.ru_nvcsw - prevUsage.ru_nvcsw); log("%d,\t", infoUsage.ru_nivcsw - prevUsage.ru_nivcsw); prevUsage = infoUsage; }#endif if (profileCounter[PROFevent].status) { log("%d ]\n", eventcounter); } else { log0(" ]\n"); } eventcounter++; flushLog(); mal_unset_lock(profileLock, "profileLock");}#endif#line 540 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx"voidinfoHeapProfile(Heap *hp, str nme){ char buf[PATHLENGTH], *p = buf; if (!hp) return; while (*nme) *p++ = *nme++; strcpy(p, "free"); log0(buf); log("=\"%d\" ", hp->free); strcpy(p, "size"); log0(buf); log("=\"%d\" ", hp->size); if (hp->maxsize) { strcpy(p, "maxsize"); log0(buf); log("=\"%d\" ", hp->maxsize); } strcpy(p, "storage"); if (hp->base) { log0(buf); log("=\"%s\" ", (hp->base == NULL) ? "absent" : (hp->storage == STORE_MMAP) ? (hp->filename ? "memory mapped" : "anonymous vm") : (hp->storage == STORE_PRIV) ? "private map" : "malloced"); } if (hp->filename) { strcpy(p, "filename"); log0(buf); log("=\"%s\" ", hp->filename ? hp->filename : "no file"); }}voidHASHinfoProfile(Hash *h, str s){ char buf[MAXPATHLEN]; int i; if (!h) return; snprintf(buf, MAXPATHLEN, "%s=\"\" mask=\"", s); i = strlen(buf); snprintf(buf + i, MAXPATHLEN - i, SZFMT "\" lim=\"", (size_t)h->mask); i = strlen(buf); snprintf(buf + i, MAXPATHLEN - i, SZFMT "\"", (size_t)h->lim); log0(buf);}voidofflineProfilerEvent(Module cntxt, MalBlkPtr mb, MalStkPtr stk, int pc){#ifdef MALprofiler#ifdef HAVE_TIMES struct tms newTms;#endif struct mallinfo infoMalloc;#ifdef HAVE_SYS_RESOURCE_H struct rusage infoUsage; static struct rusage prevUsage;#endif static struct mallinfo prevMalloc; str stmt, c; InstrPtr pci= getInstrPtr(mb,pc); (void) cntxt; /* still unused */ if (!profileAll && mb->profiler[pc].trace == FALSE) return; if (getDestVar(pci) <= 0) return; if (delayswitch > 0) { delayswitch--; return; } #line 320 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_profiler.mx" if( ! profileLockInitialized) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -