source: trunk/j/src/org/armedbear/lisp/profiler.lisp @ 4754

Last change on this file since 4754 was 4754, checked in by piso, 18 years ago

Minor cleanup.

File size: 2.6 KB
Line 
1;;; profiler.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: profiler.lisp,v 1.9 2003-11-14 18:02:22 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package "PROFILER")
21
22(defparameter *type* nil)
23
24(defparameter *granularity* 1 "Sampling interval (in milliseconds).")
25
26;; Returns list of all symbols with non-zero call counts.
27(defun list-calls ()
28  (let ((result nil))
29    (dolist (pkg (list-all-packages))
30      (dolist (sym (sys::package-symbols pkg))
31        (when (fboundp sym)
32          (let* ((f (fdefinition sym))
33                 (n (sys::%call-count f)))
34            (unless (zerop n)
35              (setq result (cons sym result)))))))
36    result))
37
38(defun show-call-count-for-symbol (sym max-count)
39  (let ((count (sys::%call-count (fdefinition sym))))
40    (if max-count
41        (format t "~A ~A (~A%)~%" sym count
42                (/ (round (/ (* count 10000.0) max-count)) 100.0))
43        (format t "~A ~A~%" sym count))))
44
45(defun show-call-counts ()
46  (let ((syms (list-calls)))
47    (setf syms (sort syms #'<
48                     :key #'(lambda (x) (sys::%call-count (fdefinition x)))))
49    (let ((max-count nil))
50      (when (eq *type* :time)
51        (let* ((last-sym (car (last syms))))
52          (setf max-count (if last-sym
53                              (sys::%call-count (fdefinition last-sym))
54                              nil))
55          (when (eql max-count 0)
56            (setf max-count nil))))
57      (dolist (sym syms)
58        (show-call-count-for-symbol sym max-count))))
59  (values))
60
61(defun start-profiler (&key type)
62  "Starts the profiler.
63  :TYPE may be either :TIME (statistical sampling) or :COUNT-ONLY (exact call
64  counts)."
65  (unless type
66    (setf type :time))
67  (unless (memq type '(:time :count-only))
68    (error ":TYPE must be :TIME or :COUNT-ONLY"))
69  (setf *type* type)
70  (%start-profiler type *granularity*))
71
72(defmacro with-profiling ((&key type) &body body)
73  `(unwind-protect (progn (start-profiler :type ,type) ,@body)
74                   (stop-profiler)))
Note: See TracBrowser for help on using the repository browser.