Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a *very* primitive statistical profiler
  • Loading branch information
sorear committed Nov 16, 2010
1 parent 21f2eac commit 29ec560
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions lib/Kernel.cs
Expand Up @@ -760,8 +760,6 @@ public class Kernel {
public static readonly DynMetaObject SubMO;
public static readonly IP6 StashP;

public static bool TraceCont;

public static IP6 MakeSub(SubInfo info, Frame outer) {
DynObject n = new DynObject(info.mo ?? SubMO);
n.slots[0] = outer;
Expand Down Expand Up @@ -1280,7 +1278,19 @@ public class Kernel {
}

public static void RunLoop(SubInfo boot) {
Kernel.TraceCont = (Environment.GetEnvironmentVariable("NIECZA_TRACE") != null);
string trace = Environment.GetEnvironmentVariable("NIECZA_TRACE");
if (trace != null) {
if (trace == "all") {
TraceFlags = TRACE_CUR;
TraceFreq = 1;
} else if (trace == "stat") {
TraceFlags = TRACE_ALL;
TraceFreq = 1000000;
} else {
Console.Error.WriteLine("Unknown trace option {0}", trace);
}
TraceCount = TraceFreq;
}
RunCore(new Frame(new Frame(null,null, ExitRunloopSI), null, boot));
}

Expand All @@ -1291,13 +1301,31 @@ class ExitRunloopException : Exception { }
throw new ExitRunloopException();
}

public const int TRACE_CUR = 1;
public const int TRACE_ALL = 2;

public static int TraceFreq;
public static int TraceCount;
public static int TraceFlags;

private static void DoTrace(Frame cur) {
TraceCount = TraceFreq;
if ((TraceFlags & TRACE_CUR) != 0)
System.Console.WriteLine("{0}|{1} @ {2}",
cur.DepthMark(), cur.info.name, cur.ip);
if ((TraceFlags & TRACE_ALL) != 0) {
Console.Error.WriteLine("Context:");
DoBacktrace(cur);
}
}

public static void RunCore(Frame cur) {
for(;;) {
try {
if (TraceCont) {
if (TraceCount != 0) {
for(;;) {
System.Console.WriteLine("{0}|{1} @ {2}",
cur.DepthMark(), cur.info.name, cur.ip);
if (--TraceCount == 0)
DoTrace(cur);
cur = cur.code(cur);
}
} else {
Expand Down Expand Up @@ -1456,23 +1484,24 @@ class ExitRunloopException : Exception { }
case 1:
Console.Error.WriteLine("Unhandled exception: {0}",
(string) UnboxAny(((Variable)th.resultSlot).Fetch()));
th.lex0 = th.caller;
goto case 2;
case 2:
if (th.lex0 == null)
Environment.Exit(1);
Frame f = (Frame) th.lex0;
Console.Error.WriteLine(" at {0} line {1} ({2} @ {3})",
new object[] {
f.ExecutingFile(), f.ExecutingLine(),
f.info.name, f.ip });
th.lex0 = ((Frame) th.lex0).caller;
goto case 2;
DoBacktrace(th.caller);
Environment.Exit(1);
return null;
default:
return Kernel.Die(th, "Invalid IP");
}
}

public static void DoBacktrace(Frame from) {
while (from != null) {
Console.Error.WriteLine(" at {0} line {1} ({2} @ {3})",
new object[] {
from.ExecutingFile(), from.ExecutingLine(),
from.info.name, from.ip });
from = from.caller;
}
}

public static Frame Unwind(Frame th, int type, Frame tf, int tip,
object td) {
// LEAVE handlers aren't implemented yet.
Expand Down

0 comments on commit 29ec560

Please sign in to comment.