KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > Profiler


1 /*
2  * Profiler.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: Profiler.java,v 1.6 2004/08/09 18:45:35 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

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 JavaDoc profilerRunnable = new Runnable JavaDoc() {
36         public void run()
37         {
38             while (profiling) {
39                 sampleNow = true;
40                 try {
41                     Thread.sleep(sleep);
42                 }
43                 catch (InterruptedException JavaDoc e) {
44                     Debug.trace(e);
45                 }
46             }
47         }
48     };
49
50     // ### %start-profiler
51
// %start-profiler type granularity
52
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 JavaDoc[] packages = Packages.getAllPackages();
72                 for (int i = 0; i < packages.length; i++) {
73                     Package JavaDoc 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 JavaDoc t = new Thread JavaDoc(profilerRunnable);
86                     int priority =
87                         Math.min(Thread.currentThread().getPriority() + 1,
88                                  Thread.MAX_PRIORITY);
89                     t.setPriority(priority);
90                     new Thread JavaDoc(profilerRunnable).start();
91                 }
92                 out._writeLine("; Profiler started.");
93                 profiling = true;
94             }
95             return thread.nothing();
96         }
97     };
98
99     // ### stop-profiler
100
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