ZealOS/docs/System/Utils/Profiler.CC.html
2021-12-04 05:03:02 -05:00

177 lines
18 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.06">
<style type="text/css">
body {background-color:#1f1f1f;}
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
.cF1{color:#4f84a6;background-color:#1f1f1f;}
.cF2{color:#73a255;background-color:#1f1f1f;}
.cF3{color:#297582;background-color:#1f1f1f;}
.cF4{color:#b34f4b;background-color:#1f1f1f;}
.cF5{color:#8a52c3;background-color:#1f1f1f;}
.cF6{color:#b7822f;background-color:#1f1f1f;}
.cF7{color:#444444;background-color:#1f1f1f;}
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
.cF9{color:#94bfde;background-color:#1f1f1f;}
.cFA{color:#a1ce97;background-color:#1f1f1f;}
.cFB{color:#6db4be;background-color:#1f1f1f;}
.cFC{color:#e88e88;background-color:#1f1f1f;}
.cFD{color:#ca94e8;background-color:#1f1f1f;}
.cFE{color:#d4b475;background-color:#1f1f1f;}
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF0>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Debugging/Profiler;Profiler;Cmd Line (Typically)/Profiler&quot;</span><span class=cF0>
<a name="l2"></a>#</span><span class=cF1>help_file</span><span class=cF0> </span><span class=cF6>&quot;::/Doc/Profiler&quot;</span><span class=cF0>
<a name="l3"></a>
<a name="l4"></a>#</span><span class=cF1>define</span><span class=cF0> </span><span class=cF3>PF_ARRAY_COUNT</span><span class=cF0> </span><span class=cFE>0x100000</span><span class=cF0>
<a name="l5"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFB>pf_jiffy_start</span><span class=cF0>, </span><span class=cFB>pf_jiffy_end</span><span class=cF0>;
<a name="l6"></a></span><span class=cF9>I64</span><span class=cF0> *</span><span class=cFB>pf_array</span><span class=cF0> = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l7"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFB>pf_cpu</span><span class=cF0> = </span><span class=cFE>0</span><span class=cF0>;
<a name="l8"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFB>pf_buf_in_ptr</span><span class=cF0> = </span><span class=cFE>0</span><span class=cF0>, </span><span class=cFB>pf_depth</span><span class=cF0>;
<a name="l9"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFB>pf_prof_active</span><span class=cF0> = </span><span class=cFE>0</span><span class=cF0>;
<a name="l10"></a>
<a name="l11"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>ProfTimerInt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *task)
<a name="l12"></a>{</span><span class=cF2>//See </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/KInterrupts.CC.html#l37"><span class=cF4>profiler_timer_irq</span></a><span class=cF2>.</span><span class=cF0>
<a name="l13"></a> </span><span class=cF9>I64</span><span class=cF0> i, k;
<a name="l14"></a>
<a name="l15"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>pf_prof_active</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF7>)</span><span class=cF0>)
<a name="l16"></a> </span><span class=cF1>for</span><span class=cF0> (k = </span><span class=cFE>0</span><span class=cF0>; k &lt;= </span><span class=cFB>pf_depth</span><span class=cF0>; k++)
<a name="l17"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l18"></a> </span><span class=cF1>if</span><span class=cF0> (task == </span><span class=cF5>Gs</span><span class=cF0>-&gt;idle_task)
<a name="l19"></a> i = </span><span class=cFD>SYS_IDLE_PT</span><span class=cF0>;
<a name="l20"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l21"></a> i = </span><span class=cF5>TaskCaller</span><span class=cF0>(task, k, </span><span class=cF3>TRUE</span><span class=cF0>);
<a name="l22"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>pf_buf_in_ptr</span><span class=cF0> &lt; </span><span class=cF3>PF_ARRAY_COUNT</span><span class=cF0>)
<a name="l23"></a> {
<a name="l24"></a> </span><span class=cFB>pf_array</span><span class=cF0>[</span><span class=cFB>pf_buf_in_ptr</span><span class=cF0>++] = i;
<a name="l25"></a> </span><span class=cFB>pf_jiffy_end</span><span class=cF0> = </span><span class=cFB>counts</span><span class=cF0>.jiffies;
<a name="l26"></a> }
<a name="l27"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l28"></a>}
<a name="l29"></a>
<a name="l30"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>Prof</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> depth=</span><span class=cFE>0</span><span class=cF0>, </span><span class=cF9>I64</span><span class=cF0> cpu_num=</span><span class=cFE>0</span><span class=cF0>)
<a name="l31"></a>{</span><span class=cF2>/*Start collecting profiler statistics.</span><span class=cF0>
<a name="l32"></a></span><span class=cF2>Profilers report where time is spent</span><span class=cF0>
<a name="l33"></a></span><span class=cF2>by sampling RIP during the 1000Hz</span><span class=cF0>
<a name="l34"></a></span><span class=cF2>timer interrupt.</span><span class=cF0>
<a name="l35"></a>
<a name="l36"></a></span><span class=cF2>Do a </span><a href="https://zeal-operating-system.github.io/ZealOS/System/Utils/Profiler.CC.html#l95"><span class=cF4>ProfRep</span></a><span class=cF2>(), (profiler report)</span><span class=cF0>
<a name="l37"></a></span><span class=cF2>after you have collected data.</span><span class=cF0>
<a name="l38"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l39"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cFE>0</span><span class=cF0> &lt;= cpu_num &lt; </span><span class=cFB>mp_count</span><span class=cF7>)</span><span class=cF0>)
<a name="l40"></a> </span><span class=cF3>ST_ERR_ST</span><span class=cF0> </span><span class=cF6>&quot;Invalid CPU\n&quot;</span><span class=cF0>;
<a name="l41"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l42"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l43"></a> </span><span class=cFB>cpu_structs</span><span class=cF0>[</span><span class=cFB>pf_cpu</span><span class=cF0>].profiler_timer_irq = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l44"></a> </span><span class=cFB>pf_cpu</span><span class=cF0> = cpu_num;
<a name="l45"></a>
<a name="l46"></a> </span><span class=cFB>pf_depth</span><span class=cF0> = depth;
<a name="l47"></a> </span><span class=cFB>pf_buf_in_ptr</span><span class=cF0> = </span><span class=cFE>0</span><span class=cF0>;
<a name="l48"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFB>pf_array</span><span class=cF0>)
<a name="l49"></a> </span><span class=cFB>pf_array</span><span class=cF0> = </span><span class=cF5>SysMAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I64</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF3>PF_ARRAY_COUNT</span><span class=cF0>);
<a name="l50"></a> </span><span class=cFB>pf_jiffy_end</span><span class=cF0> = </span><span class=cFB>pf_jiffy_start</span><span class=cF0> = </span><span class=cFB>counts</span><span class=cF0>.jiffies;
<a name="l51"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;</span><span class=cFB>pf_prof_active</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>);
<a name="l52"></a> </span><span class=cFB>cpu_structs</span><span class=cF0>[</span><span class=cFB>pf_cpu</span><span class=cF0>].profiler_timer_irq = &amp;</span><span class=cF5>ProfTimerInt</span><span class=cF0>;
<a name="l53"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l54"></a>}
<a name="l55"></a>
<a name="l56"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>ProfCompare</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *i1, </span><span class=cF1>U8</span><span class=cF0> *i2)
<a name="l57"></a>{ </span><span class=cF2>// Sort Profiler function pointer array by least--&gt;greatest.</span><span class=cF0>
<a name="l58"></a> </span><span class=cF1>return</span><span class=cF0> i1 - i2;
<a name="l59"></a>}
<a name="l60"></a>
<a name="l61"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>ProfSort</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *array, </span><span class=cF9>I64</span><span class=cF0> count)
<a name="l62"></a>{
<a name="l63"></a> </span><span class=cF9>I64</span><span class=cF0> v1, v2, i1, i2, start_i1, start_i2, end_i1, end_i2, j;
<a name="l64"></a>
<a name="l65"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l66"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l67"></a> start_i1 = i1 = </span><span class=cFE>0</span><span class=cF0>;
<a name="l68"></a>profsort_walk:
<a name="l69"></a> v1 = array[i1];
<a name="l70"></a> </span><span class=cF1>while</span><span class=cF0> (v1 == array[i1])
<a name="l71"></a> {
<a name="l72"></a> end_i1 = i1;
<a name="l73"></a> start_i2 = i2 = ++i1;
<a name="l74"></a> }
<a name="l75"></a> </span><span class=cF1>if</span><span class=cF0> (i1 &gt; count) </span><span class=cF1>break</span><span class=cF0>; </span><span class=cF2>// bail if first search went out of bounds</span><span class=cF0>
<a name="l76"></a> v2 = array[i2];
<a name="l77"></a> </span><span class=cF1>while</span><span class=cF0> (v2 == array[i2])
<a name="l78"></a> end_i2 = i2++;
<a name="l79"></a> </span><span class=cF1>if</span><span class=cF0> (i2 &gt; count) </span><span class=cF1>break</span><span class=cF0>; </span><span class=cF2>// bail if second search went out of bounds</span><span class=cF0>
<a name="l80"></a> </span><span class=cF1>if</span><span class=cF0> (end_i1 - start_i1 &gt; end_i2 - start_i2)
<a name="l81"></a> {
<a name="l82"></a> </span><span class=cF1>for</span><span class=cF0> (j = start_i1; j &lt;= start_i1 + end_i2 - start_i2; j++)
<a name="l83"></a> array[j] = v2;
<a name="l84"></a> </span><span class=cF1>for</span><span class=cF0> (j = start_i1 + end_i2 - start_i2 + </span><span class=cFE>1</span><span class=cF0>; j &lt;= end_i2 - start_i2 + end_i1 + </span><span class=cFE>1</span><span class=cF0>; j++)
<a name="l85"></a> array[j] = v1;
<a name="l86"></a> }
<a name="l87"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l88"></a> {
<a name="l89"></a> start_i1 = i1 = start_i2;
<a name="l90"></a> </span><span class=cF1>goto</span><span class=cF0> profsort_walk;
<a name="l91"></a> }
<a name="l92"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l93"></a>}
<a name="l94"></a>
<a name="l95"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>ProfRep</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> filter_count=</span><span class=cFE>1</span><span class=cF0>, </span><span class=cF1>Bool</span><span class=cF0> leave_it=</span><span class=cF3>OFF</span><span class=cF0>)
<a name="l96"></a>{</span><span class=cF2>//Profiler report. Call </span><a href="https://zeal-operating-system.github.io/ZealOS/System/Utils/Profiler.CC.html#l30"><span class=cF4>Prof</span></a><span class=cF2>() first and collect data.</span><span class=cF0>
<a name="l97"></a> </span><span class=cF9>I64</span><span class=cF0> i, hits, rip, last_rip = </span><span class=cFE>0</span><span class=cF0>, routine_total = </span><span class=cFE>0</span><span class=cF0>;
<a name="l98"></a> </span><span class=cF1>F64</span><span class=cF0> total_time;
<a name="l99"></a> </span><span class=cF1>U8</span><span class=cF0> buf[</span><span class=cFE>256</span><span class=cF0>], buf2[</span><span class=cFE>256</span><span class=cF0>], last_buf[</span><span class=cFE>256</span><span class=cF0>];
<a name="l100"></a>
<a name="l101"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>LBtr</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>pf_prof_active</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF7>)</span><span class=cF0>)
<a name="l102"></a> </span><span class=cF6>&quot;Profiler Not Active\n&quot;</span><span class=cF0>;
<a name="l103"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFB>pf_buf_in_ptr</span><span class=cF0>)
<a name="l104"></a> </span><span class=cF6>&quot;No Profiler Statistic\n&quot;</span><span class=cF0>;
<a name="l105"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l106"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l107"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>total_time = </span><span class=cFB>pf_jiffy_end</span><span class=cF0> - </span><span class=cFB>pf_jiffy_start</span><span class=cF7>)</span><span class=cF0>)
<a name="l108"></a> total_time = </span><span class=cFE>1</span><span class=cF0>;
<a name="l109"></a> </span><span class=cF5>QuickSortI64</span><span class=cF0>(</span><span class=cFB>pf_array</span><span class=cF0>, </span><span class=cFB>pf_buf_in_ptr</span><span class=cF0>, &amp;</span><span class=cF5>ProfCompare</span><span class=cF0>);
<a name="l110"></a> </span><span class=cF5>ProfSort</span><span class=cF0>(</span><span class=cFB>pf_array</span><span class=cF0>, </span><span class=cFB>pf_buf_in_ptr</span><span class=cF0>);
<a name="l111"></a> *last_buf = </span><span class=cFE>0</span><span class=cF0>;
<a name="l112"></a> </span><span class=cF6>&quot;$LTCYAN$ %%Time Hits: &amp;Function+Offset$FG$\n&quot;</span><span class=cF0>;
<a name="l113"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cFB>pf_buf_in_ptr</span><span class=cF0>; i += hits)
<a name="l114"></a> {
<a name="l115"></a> rip = </span><span class=cFB>pf_array</span><span class=cF0>[i];
<a name="l116"></a> hits = </span><span class=cFE>0</span><span class=cF0>;
<a name="l117"></a> </span><span class=cF1>do</span><span class=cF0> hits++;
<a name="l118"></a> </span><span class=cF1>while</span><span class=cF0> (i + hits &lt; </span><span class=cFB>pf_buf_in_ptr</span><span class=cF0> &amp;&amp; </span><span class=cFB>pf_array</span><span class=cF0>[i + hits] == rip);
<a name="l119"></a>
<a name="l120"></a> </span><span class=cF5>StrPrint</span><span class=cF0>(buf, </span><span class=cF6>&quot;%p&quot;</span><span class=cF0>, rip);
<a name="l121"></a> </span><span class=cF5>StrFirstRemove</span><span class=cF0>(buf, </span><span class=cF6>&quot;+&quot;</span><span class=cF0>, buf2);
<a name="l122"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>StrCompare</span><span class=cF7>(</span><span class=cF0>buf2, last_buf</span><span class=cF7>)</span><span class=cF0>)
<a name="l123"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l124"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>if (*last_buf &amp;&amp; routine_total &gt;= filter_count)</span><span class=cF0>
<a name="l125"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>&quot;$GREEN$%6.2f %08X:%s\n$FG$&quot;, 100 * routine_total / total_time, routine_total, last_buf;</span><span class=cF0>
<a name="l126"></a> </span><span class=cF5>StrCopy</span><span class=cF0>(last_buf, buf2);
<a name="l127"></a> routine_total = </span><span class=cFE>0</span><span class=cF0>;
<a name="l128"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l129"></a> routine_total += hits;
<a name="l130"></a> </span><span class=cF1>if</span><span class=cF0> (hits &gt;= filter_count)
<a name="l131"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l132"></a> </span><span class=cF6>&quot;%6.2f %08X:%P\n&quot;</span><span class=cF0>, </span><span class=cFE>100</span><span class=cF0> * hits / total_time, hits, rip;
<a name="l133"></a> last_rip = rip;
<a name="l134"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l135"></a> }
<a name="l136"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>if (*last_buf &amp;&amp; routine_total &gt;= filter_count)</span><span class=cF0>
<a name="l137"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>&quot;$GREEN$%6.2f %08X:%s\n$FG$&quot;, 100 * routine_total / total_time, routine_total, last_buf;</span><span class=cF0>
<a name="l138"></a> </span><span class=cF6>&quot;$LTCYAN$ %%Time Hits: &amp;Function+Offset$FG$\n&quot;</span><span class=cF0>;
<a name="l139"></a> </span><span class=cF6>&quot;Total Time:%0.6fs\n&quot;</span><span class=cF0>, total_time / </span><span class=cF3>JIFFY_FREQ</span><span class=cF0>;
<a name="l140"></a> </span><span class=cF1>if</span><span class=cF0> (leave_it)
<a name="l141"></a> {
<a name="l142"></a> </span><span class=cFB>cpu_structs</span><span class=cF0>[</span><span class=cFB>pf_cpu</span><span class=cF0>].profiler_timer_irq = &amp;</span><span class=cF5>ProfTimerInt</span><span class=cF0>;
<a name="l143"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;</span><span class=cFB>pf_prof_active</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>);
<a name="l144"></a> }
<a name="l145"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l146"></a> </span><span class=cFB>cpu_structs</span><span class=cF0>[</span><span class=cFB>pf_cpu</span><span class=cF0>].profiler_timer_irq = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l147"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l148"></a>}
</span></pre></body>
</html>