hschumann2/TempleOS-Source-Code
0847
1 2#help_index "Debugging/Profiler;Profiler;Cmd Line (Typically)/Profiler"3#help_file "::/Doc/Profiler"4 5#define PF_ARRAY_CNT 0x1000006I64 pf_jiffy_start,pf_jiffy_end;7I64 *pf_array=NULL;8I64 pf_cpu=0;9I64 pf_buf_in_ptr=0,pf_depth;10I64 pf_prof_active=0;11 12U0 ProfTimerInt(CTask *task)13{//See profiler_timer_irq.14 I64 i,k;15 if (Bt(&pf_prof_active,0))16 for (k=0;k<=pf_depth;k++) {17 if (task==Gs->idle_task)18 i=SYS_IDLE_PT;19 else20 i=TaskCaller(task,k,TRUE);21 if (pf_buf_in_ptr<PF_ARRAY_CNT) {22 pf_array[pf_buf_in_ptr++]=i;23 pf_jiffy_end=cnts.jiffies;24 }25 }26}27 28public U0 Prof(I64 depth=0,I64 cpu_num=0)29{/*Start collecting profiler statistics.30Profilers report where time is spent31by sampling RIP during the 1000Hz32timer interrupt.33 34Do a ProfRep(), (profiler report)35after you have collected data.36*/37 if (!(0<=cpu_num<mp_cnt))38 ST_ERR_ST "Invalid CPU\n";39 else {40 cpu_structs[pf_cpu].profiler_timer_irq=NULL;41 pf_cpu=cpu_num;42 43 pf_depth=depth;44 pf_buf_in_ptr=0;45 if (!pf_array)46 pf_array=AMAlloc(sizeof(I64)*PF_ARRAY_CNT);47 pf_jiffy_end=pf_jiffy_start=cnts.jiffies;48 LBts(&pf_prof_active,0);49 cpu_structs[pf_cpu].profiler_timer_irq=&ProfTimerInt;50 }51}52 53I64 ProfCompare(U8 *i1,U8 *i2)54{55 return i1-i2;56}57 58public U0 ProfRep(I64 filter_cnt=1,Bool leave_it=OFF)59{//Profiler report. Call Prof() first and collect data.60 I64 i,hits,rip,last_rip=0,routine_total=0;61 F64 total_time;62 U8 buf[256],buf2[256],last_buf[256];63 if (!LBtr(&pf_prof_active,0))64 "Profiler Not Active\n";65 if (!pf_buf_in_ptr)66 "No Profiler Statistic\n";67 else {68 if (!(total_time=pf_jiffy_end-pf_jiffy_start))69 total_time=1;70 QSortI64(pf_array,pf_buf_in_ptr,&ProfCompare);71 *last_buf=0;72 for (i=0;i<pf_buf_in_ptr;i+=hits) {73 rip=pf_array[i];74 hits=0;75 do hits++;76 while (i+hits<pf_buf_in_ptr && pf_array[i+hits]==rip);77 78 StrPrint(buf,"%p",rip);79 StrFirstRem(buf,"+",buf2);80 if (StrCmp(buf2,last_buf)) {81 if (*last_buf && routine_total>=filter_cnt)82 "$GREEN$%6.2f %08X:%s\n$FG$",100*routine_total/total_time,83 routine_total,last_buf;84 StrCpy(last_buf,buf2);85 routine_total=0;86 }87 routine_total+=hits;88 if (hits>=filter_cnt) {89 "%6.2f %08X:%P\n",100*hits/total_time,hits,rip;90 last_rip=rip;91 }92 }93 if (*last_buf && routine_total>=filter_cnt)94 "$GREEN$%6.2f %08X:%s\n$FG$",100*routine_total/total_time,95 routine_total,last_buf;96 "Total Time:%0.6fs\n",total_time/JIFFY_FREQ;97 if (leave_it) {98 cpu_structs[pf_cpu].profiler_timer_irq=&ProfTimerInt;99 LBts(&pf_prof_active,0);100 } else101 cpu_structs[pf_cpu].profiler_timer_irq=NULL;102 }103}104 