int main(int argc, char** argv) { int r; unsigned int v; unsigned long long ll; int i; #ifndef NO_PROFILE struct profile_data pdf1, pdf2, pdf4, pdf5, pdf6, pdf8; struct profile_data pdl1, pdl2, pdl4, pdl5, pdl6, pdl8; #ifdef HAS_BIT_SCAN_ASM struct profile_data pdf3, pdf7, pdl3, pdl7; #endif struct profile_data pdf_32, pdf_64, pdl_32, pdl_64; struct profile_data pdf_long, pdl_long; #endif /* NO_PROFILE */ profile_init(&pdf1, "first_debruijn32"); profile_init(&pdf2, "first_slow32"); #ifdef HAS_BIT_SCAN_ASM profile_init(&pdf3, "first_asm32"); #endif profile_init(&pdf4, "first_br32"); profile_init(&pdf5, "first_debruijn64"); profile_init(&pdf6, "first_slow64"); #ifdef HAS_BIT_SCAN_ASM profile_init(&pdf7, "first_asm64"); #endif profile_init(&pdf8, "first_br64"); profile_init(&pdl1, "last_debruijn32"); profile_init(&pdl2, "last_slow32"); #ifdef HAS_BIT_SCAN_ASM profile_init(&pdl3, "last_asm32"); #endif profile_init(&pdl4, "last_br32"); profile_init(&pdl5, "last_debruijn64"); profile_init(&pdl6, "last_slow64"); #ifdef HAS_BIT_SCAN_ASM profile_init(&pdl7, "last_asm64"); #endif profile_init(&pdl8, "last_br64"); profile_init(&pdf_32, "scan_forward32"); profile_init(&pdf_64, "scan_forward64"); profile_init(&pdl_32, "scan_reverse32"); profile_init(&pdl_64, "scan_reverse64"); profile_init(&pdf_long, "scan_forward_l"); profile_init(&pdl_long, "scan_reverse_l"); for (i=0; i<100; i++){ for (r=0; r<32; r++){ v=(1U<<r); CHECK("first debruijn 32bit", r, v, bit_scan_forward_debruijn32, &pdf1); CHECK("first slow 32bit", r, v, bit_scan_forward_slow32, &pdf2); #ifdef HAS_BIT_SCAN_ASM CHECK("first asm 32bit", r, v, bit_scan_forward_asm32, &pdf3); #endif CHECK("first br 32bit", r, v, bit_scan_forward_br32, &pdf4); CHECK("scan_forward32", r, v, bit_scan_forward32, &pdf_32); if (sizeof(long)<=4){ CHECK("scan_forward_l", r, v, bit_scan_forward, &pdf_long); } v+=(v-1); CHECK("last debruijn 32bit", r, v, bit_scan_reverse_debruijn32, &pdl1); CHECK("last slow 32bit", r, v, bit_scan_reverse_slow32, &pdl2); #ifdef HAS_BIT_SCAN_ASM CHECK("last asm 32bit", r, v, bit_scan_reverse_asm32, &pdl3); #endif CHECK("last br 32bit", r, v, bit_scan_reverse_br32, &pdl4); CHECK("scan_reverse32", r, v, bit_scan_reverse32, &pdl_32); if (sizeof(long)<=4){ CHECK("scan_reverse_l", r, v, bit_scan_reverse, &pdl_long); } } for (r=0; r<64; r++){ ll=(1ULL<<r); CHECK("first debruijn 64bit", r, ll, bit_scan_forward_debruijn64, &pdf5); CHECK("first slow 64bit", r, ll, bit_scan_forward_slow64, &pdf6); #ifdef HAS_BIT_SCAN_ASM CHECK("first asm 64bit", r, ll, bit_scan_forward_asm64, &pdf7); #endif CHECK("first br 64bit", r, ll, bit_scan_forward_br64, &pdf8); CHECK("scan_forward64", r, ll, bit_scan_forward64, &pdf_64); if (sizeof(long)>4){ CHECK("scan_forward_l", r, ll, bit_scan_forward, &pdf_long); } ll+=ll-1; CHECK("last debruijn 64bit", r, ll, bit_scan_reverse_debruijn64, &pdl5); CHECK("last slow 64bit", r, ll, bit_scan_reverse_slow64, &pdl6); #ifdef HAS_BIT_SCAN_ASM CHECK("last asm 64bit", r, ll, bit_scan_reverse_asm64, &pdl7); #endif CHECK("last br 64bit", r, ll, bit_scan_reverse_br64, &pdl8); CHECK("scan_reverse64", r, ll, bit_scan_reverse64, &pdl_64); if (sizeof(long)>4){ CHECK("scan_reverse_l", r, ll, bit_scan_reverse, &pdl_long); } } } PROFILE_PRINT(&pdf1); PROFILE_PRINT(&pdf2); #ifdef HAS_BIT_SCAN_ASM PROFILE_PRINT(&pdf3); #endif PROFILE_PRINT(&pdf4); PROFILE_PRINT(&pdl1); PROFILE_PRINT(&pdl2); #ifdef HAS_BIT_SCAN_ASM PROFILE_PRINT(&pdl3); #endif PROFILE_PRINT(&pdl4); PROFILE_PRINT(&pdf5); PROFILE_PRINT(&pdf6); #ifdef HAS_BIT_SCAN_ASM PROFILE_PRINT(&pdf7); #endif PROFILE_PRINT(&pdf8); PROFILE_PRINT(&pdl5); PROFILE_PRINT(&pdl6); #ifdef HAS_BIT_SCAN_ASM PROFILE_PRINT(&pdl7); #endif PROFILE_PRINT(&pdl8); PROFILE_PRINT(&pdf_32); PROFILE_PRINT(&pdf_64); PROFILE_PRINT(&pdf_long); PROFILE_PRINT(&pdl_32); PROFILE_PRINT(&pdl_64); PROFILE_PRINT(&pdl_long); return 0; }
static void profiler_hook( lua_State * L, lua_Debug * ar) { static const char * id = NULL; static char info[256]; static size_t len=0; int event = ar->event; //if( event == LUA_HOOKCALL || event == LUA_HOOKRET || event == LUA_HOOKTAILRET) { static timeval now; FuncProfileRec * prec = NULL; if(event != LUA_HOOKCALL) { if(funcIds.empty()) return; gettimeofday(&now,NULL); prec = funcIds.back(); funcIds.pop_back(); } else { lua_getinfo(L,"nS",ar); //if( !strcmp(ar->what, "C")) if( ar->what[0] == 'C') { id = ar->name ? ar->name : "UNKNOWN"; //sprintf(id,"%s",ar->name ? ar->name : "UNKNOWN" ); } else { len = strlen(ar->short_src); if(len <= LUA_IDSIZE -6 ) { ar->short_src[len] = 58; int line = ar->linedefined; ar->short_src[len+1] = line/1000 + 48; ar->short_src[len+2] = (line%1000)/100 + 48; ar->short_src[len+3] = (line%100)/10 + 48; ar->short_src[len+4] = (line%10) + 48; ar->short_src[len+5] = 0; } id = ar->short_src; //sprintf(id,"%s:%d",ar->short_src, ar->linedefined); } static std::map<std::string,FuncProfileRec *>::iterator found; found = s_rec.find(id); if(found == s_rec.end()) { prec = new FuncProfileRec(); s_rec[id] = prec; static char name[256]; if(ar->what[0] == 'C') { sprintf(name,"[C]%s",ar->name); } else { sprintf(name,"%s (%s)",ar->name ? ar->name : "",id); } prec->name = name; } else { prec = found->second; } funcIds.push_back(prec); //»Ö¸´ if( ar->what[0] != 'C') ar->short_src[len] = 0; } FuncProfileRec & rec = *prec; if( event == LUA_HOOKCALL) { #if 0 if(rec.callts.size() > 50) //µÝ¹éµ÷ÓÃÌ«¶à´ÎÁË { sprintf(info,"too deeply call: %s\n",rec.name.c_str()); PROFILE_PRINT(info); } #endif ++rec.callcount; ++totalstatcount; rec.callstatcount.push_back(totalstatcount); gettimeofday(&now,NULL); rec.callts.push_back(now); } else { if( !rec.callts.empty() ) { timeval& t = rec.callts.back(); rec.totalt +=( (now.tv_sec - t.tv_sec)*1000.f + ( now.tv_usec - t.tv_usec)*0.001f); rec.callts.pop_back(); rec.statcount += (totalstatcount - rec.callstatcount.back()); rec.callstatcount.pop_back(); } } } }