1 21 22 package org.armedbear.lisp; 23 24 public class Profiler extends Lisp 25 { 26 private static int sleep = 1; 27 28 public static final void sample(LispThread thread) 29 throws ConditionThrowable 30 { 31 sampleNow = false; 32 thread.incrementCallCounts(); 33 } 34 35 private static final Runnable profilerRunnable = new Runnable () { 36 public void run() 37 { 38 while (profiling) { 39 sampleNow = true; 40 try { 41 Thread.sleep(sleep); 42 } 43 catch (InterruptedException e) { 44 Debug.trace(e); 45 } 46 } 47 } 48 }; 49 50 public static final Primitive2 _START_PROFILER = 53 new Primitive2("%start-profiler", PACKAGE_PROF, false) 54 { 55 public LispObject execute(LispObject first, LispObject second) 56 throws ConditionThrowable 57 { 58 final LispThread thread = LispThread.currentThread(); 59 Stream out = getStandardOutput(); 60 out.freshLine(); 61 if (profiling) { 62 out._writeLine("; Profiler already started."); 63 } else { 64 if (first == Keyword.TIME) 65 sampling = true; 66 else if (first == Keyword.COUNT_ONLY) 67 sampling = false; 68 else 69 return signal(new LispError( 70 "%START-PROFILER: argument must be either :TIME or :COUNT-ONLY")); 71 Package [] packages = Packages.getAllPackages(); 72 for (int i = 0; i < packages.length; i++) { 73 Package pkg = packages[i]; 74 Symbol[] symbols = pkg.symbols(); 75 for (int j = 0; j < symbols.length; j++) { 76 Symbol symbol = symbols[j]; 77 LispObject f = symbol.getSymbolFunction(); 78 if (f != null) 79 f.setCallCount(0); 80 } 81 } 82 if (sampling) { 83 sleep = Fixnum.getValue(second); 84 thread.resetStack(); 85 Thread t = new Thread (profilerRunnable); 86 int priority = 87 Math.min(Thread.currentThread().getPriority() + 1, 88 Thread.MAX_PRIORITY); 89 t.setPriority(priority); 90 new Thread (profilerRunnable).start(); 91 } 92 out._writeLine("; Profiler started."); 93 profiling = true; 94 } 95 return thread.nothing(); 96 } 97 }; 98 99 public static final Primitive0 STOP_PROFILER = 101 new Primitive0("stop-profiler", PACKAGE_PROF, true) 102 { 103 public LispObject execute() throws ConditionThrowable 104 { 105 Stream out = getStandardOutput(); 106 out.freshLine(); 107 if (profiling) { 108 profiling = false; 109 out._writeLine("; Profiler stopped."); 110 } else 111 out._writeLine("; Profiler was not started."); 112 out._finishOutput(); 113 return LispThread.currentThread().nothing(); 114 } 115 }; 116 } 117 | Popular Tags |