001: /*
002: * Profiler.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: Profiler.java,v 1.1 2003/11/15 11:03:32 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public class Profiler extends Lisp {
025: private static int sleep = 1;
026:
027: public static final void sample(LispThread thread) {
028: sampleNow = false;
029: thread.incrementCallCounts();
030: }
031:
032: private static final Runnable profilerRunnable = new Runnable() {
033: public void run() {
034: while (profiling) {
035: sampleNow = true;
036: try {
037: Thread.sleep(sleep);
038: } catch (InterruptedException e) {
039: Debug.trace(e);
040: }
041: }
042: }
043: };
044:
045: // ### %start-profiler
046: // %start-profiler type granularity
047: public static final Primitive2 _START_PROFILER = new Primitive2(
048: "%start-profiler", PACKAGE_PROF, false) {
049: public LispObject execute(LispObject first, LispObject second)
050: throws ConditionThrowable {
051: CharacterOutputStream out = getStandardOutput();
052: out.freshLine();
053: if (profiling) {
054: out.writeLine("; Profiler already started.");
055: } else {
056: if (first == Keyword.TIME)
057: sampling = true;
058: else if (first == Keyword.COUNT_ONLY)
059: sampling = false;
060: else
061: throw new ConditionThrowable(
062: new LispError(
063: "%START-PROFILER: argument must be either :TIME or :COUNT-ONLY"));
064: Package[] packages = Packages.getAllPackages();
065: for (int i = 0; i < packages.length; i++) {
066: Package pkg = packages[i];
067: Symbol[] symbols = pkg.symbols();
068: for (int j = 0; j < symbols.length; j++) {
069: Symbol symbol = symbols[j];
070: LispObject f = symbol.getSymbolFunction();
071: if (f != null)
072: f.setCallCount(0);
073: }
074: }
075: if (sampling) {
076: sleep = Fixnum.getValue(second);
077: if (!debug) {
078: debug = true;
079: LispThread.currentThread().resetStack();
080: }
081: Thread t = new Thread(profilerRunnable);
082: int priority = Math.min(Thread.currentThread()
083: .getPriority() + 1, Thread.MAX_PRIORITY);
084: t.setPriority(priority);
085: new Thread(profilerRunnable).start();
086: }
087: out.writeLine("; Profiler started.");
088: profiling = true;
089: }
090: return LispThread.currentThread().nothing();
091: }
092: };
093:
094: // ### stop-profiler
095: public static final Primitive0 STOP_PROFILER = new Primitive0(
096: "stop-profiler", PACKAGE_PROF, true) {
097: public LispObject execute() throws ConditionThrowable {
098: CharacterOutputStream out = getStandardOutput();
099: out.freshLine();
100: if (profiling) {
101: profiling = false;
102: out.writeLine("; Profiler stopped.");
103: } else
104: out.writeLine("; Profiler was not started.");
105: out.flushOutput();
106: return LispThread.currentThread().nothing();
107: }
108: };
109: }
|