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

Last change on this file since 11391 was 11391, checked in by vvoutilainen, 14 years ago

ABCL license is GPL + Classpath exception. This was intended
by Peter Graves, the original author. For reference, see
http://sourceforge.net/mailarchive/forum.php?thread_name=20040721115302.839%40prufrock&forum_name=armedbear-j-announce

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1;;; profiler.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: profiler.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $
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;;; As a special exception, the copyright holders of this library give you
21;;; permission to link this library with independent modules to produce an
22;;; executable, regardless of the license terms of these independent
23;;; modules, and to copy and distribute the resulting executable under
24;;; terms of your choice, provided that you also meet, for each linked
25;;; independent module, the terms and conditions of the license of that
26;;; module.  An independent module is a module which is not derived from
27;;; or based on this library.  If you modify this library, you may extend
28;;; this exception to your version of the library, but you are not
29;;; obligated to do so.  If you do not wish to do so, delete this
30;;; exception statement from your version.
31
32(in-package #:profiler)
33
34(export '(*hidden-functions*))
35
36(require '#:clos)
37(require '#:format)
38
39(defvar *type* nil)
40
41(defvar *granularity* 1 "Sampling interval (in milliseconds).")
42
43(defvar *hidden-functions*
44  '(funcall apply eval
45    sys::%eval sys::interactive-eval
46    tpl::repl tpl::top-level-loop))
47
48(defstruct (profile-info
49            (:constructor make-profile-info (object count)))
50  object
51  count)
52
53;; Returns list of all symbols with non-zero call counts.
54(defun list-called-objects ()
55  (let ((result '()))
56    (dolist (pkg (list-all-packages))
57      (dolist (sym (sys:package-symbols pkg))
58        (unless (memq sym *hidden-functions*)
59          (when (fboundp sym)
60            (let* ((definition (fdefinition sym))
61                   (count (sys:call-count definition)))
62              (unless (zerop count)
63                (cond ((typep definition 'generic-function)
64                       (push (make-profile-info definition count) result)
65                       (dolist (method (mop::generic-function-methods definition))
66                         (setf count (sys:call-count (sys:%method-function method)))
67                         (unless (zerop count)
68                           (push (make-profile-info method count) result))))
69                      (t
70                       (push (make-profile-info sym count) result)))))))))
71    (remove-duplicates result :key 'profile-info-object :test 'eq)))
72
73(defun object-name (object)
74  (cond ((symbolp object)
75         object)
76        ((typep object 'generic-function)
77         (sys:%generic-function-name object))
78        ((typep object 'method)
79         (list 'METHOD
80               (sys:%generic-function-name (sys:%method-generic-function object))
81               (sys:%method-specializers object)))))
82
83(defun object-compiled-function-p (object)
84  (cond ((symbolp object)
85         (compiled-function-p (fdefinition object)))
86        ((typep object 'method)
87         (compiled-function-p (sys:%method-function object)))
88        (t
89         (compiled-function-p object))))
90
91(defun show-call-count (info max-count)
92  (let* ((object (profile-info-object info))
93         (count (profile-info-count info)))
94    (if max-count
95        (format t "~5,1F ~8D ~S~A~%"
96                (/ (* count 100.0) max-count)
97                count
98                (object-name object)
99                (if (object-compiled-function-p object)
100                    ""
101                    " [interpreted function]"))
102        (format t "~8D ~S~A~%"
103                count
104                (object-name object)
105                (if (object-compiled-function-p object)
106                    ""
107                    " [interpreted function]")))))
108
109(defun show-call-counts ()
110  (let ((list (list-called-objects)))
111    (setf list (sort list #'< :key 'profile-info-count))
112    (let ((max-count nil))
113      (when (eq *type* :time)
114        (let ((last-info (car (last list))))
115          (setf max-count (if last-info
116                              (profile-info-count last-info)
117                              nil))
118          (when (eql max-count 0)
119            (setf max-count nil))))
120      (dolist (info list)
121        (show-call-count info max-count))))
122  (values))
123
124(defun start-profiler (&key type)
125  "Starts the profiler.
126  :TYPE may be either :TIME (statistical sampling) or :COUNT-ONLY (exact call
127  counts)."
128  (unless type
129    (setf type :time))
130  (unless (memq type '(:time :count-only))
131    (error ":TYPE must be :TIME or :COUNT-ONLY"))
132  (setf *type* type)
133  (%start-profiler type *granularity*))
134
135(defmacro with-profiling ((&key type) &body body)
136  `(unwind-protect (progn (start-profiler :type ,type) ,@body)
137                   (stop-profiler)))
Note: See TracBrowser for help on using the repository browser.