source: trunk/abcl/src/org/armedbear/lisp/profiler.lisp

Last change on this file was 14120, checked in by ehuelsmann, 12 years ago

Move profiler exports to profiler.lisp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1;;; profiler.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: profiler.lisp 14120 2012-08-18 11:06:14Z ehuelsmann $
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* *granularity*
35          show-call-counts show-hot-counts with-profiling))
36
37(require '#:clos)
38(require '#:format)
39
40(defvar *type* nil)
41
42(defvar *granularity* 1 "Sampling interval (in milliseconds).")
43
44(defvar *hidden-functions*
45  '(funcall apply eval
46    sys::%eval sys::interactive-eval
47    tpl::repl tpl::top-level-loop))
48
49(defstruct (profile-info
50            (:constructor make-profile-info (object full-count hot-count)))
51  object
52  full-count
53  hot-count)
54
55;; Returns list of all symbols with non-zero call counts.
56(defun list-called-objects ()
57  (let ((result '()))
58    (dolist (pkg (list-all-packages))
59      (dolist (sym (sys:package-symbols pkg))
60        (unless (memq sym *hidden-functions*)
61          (when (fboundp sym)
62            (let* ((definition (fdefinition sym))
63                   (full-count (sys:call-count definition))
64                   (hot-count (sys:hot-count definition)))
65              (unless (zerop full-count)
66                (cond ((typep definition 'generic-function)
67                       (push (make-profile-info definition
68                                                full-count hot-count) result)
69                       (dolist (method
70                                 (mop::generic-function-methods definition))
71                         (let ((function (mop:method-function method)))
72                           (setf full-count (sys:call-count function))
73                           (setf hot-count (sys:hot-count function)))
74                         (unless (zerop full-count)
75                           (push (make-profile-info method full-count
76                                                    hot-count) result))))
77                      (t
78                       (push (make-profile-info sym full-count hot-count)
79                             result)))))))))
80    (remove-duplicates result :key 'profile-info-object :test 'eq)))
81
82(defun object-name (object)
83  (cond ((symbolp object)
84         object)
85        ((typep object 'generic-function)
86         (mop:generic-function-name object))
87        ((typep object 'method)
88         (list 'METHOD
89               (mop:generic-function-name (mop:method-generic-function object))
90               (mop:method-specializers object)))))
91
92(defun object-compiled-function-p (object)
93  (cond ((symbolp object)
94         (compiled-function-p (fdefinition object)))
95        ((typep object 'method)
96         (compiled-function-p (mop:method-function object)))
97        (t
98         (compiled-function-p object))))
99
100(defun show-call-count (info max-count)
101  (let* ((object (profile-info-object info))
102         (count (profile-info-full-count info)))
103    (if max-count
104        (format t "~5,1F ~8D ~S~A~%"
105                (/ (* count 100.0) max-count)
106                count
107                (object-name object)
108                (if (object-compiled-function-p object)
109                    ""
110                    " [interpreted function]"))
111        (format t "~8D ~S~A~%"
112                count
113                (object-name object)
114                (if (object-compiled-function-p object)
115                    ""
116                    " [interpreted function]")))))
117
118(defun show-hot-count (info max-count)
119  (let* ((object (profile-info-object info))
120         (count (profile-info-hot-count info)))
121    (if max-count
122        (format t "~5,1F ~8D ~S~A~%"
123                (/ (* count 100.0) max-count)
124                count
125                (object-name object)
126                (if (object-compiled-function-p object)
127                    ""
128                    " [interpreted function]"))
129        (format t "~8D ~S~A~%"
130                count
131                (object-name object)
132                (if (object-compiled-function-p object)
133                    ""
134                    " [interpreted function]")))))
135
136(defun show-call-counts ()
137  (let ((list (list-called-objects)))
138    (setf list (sort list #'< :key 'profile-info-full-count))
139    (let ((max-count nil))
140      (when (eq *type* :time)
141        (let ((last-info (car (last list))))
142          (setf max-count (if last-info
143                              (profile-info-full-count last-info)
144                              nil))
145          (when (eql max-count 0)
146            (setf max-count nil))))
147      (dolist (info list)
148        (show-call-count info max-count))))
149  (values))
150
151(defun show-hot-counts ()
152  (let ((list (list-called-objects)))
153    (setf list (sort list #'< :key 'profile-info-hot-count))
154    (let ((max-count nil))
155      (when (eq *type* :time)
156        (let ((last-info (car (last list))))
157          (setf max-count (if last-info
158                              (profile-info-hot-count last-info)
159                              nil))
160          (when (eql max-count 0)
161            (setf max-count nil))))
162      (dolist (info list)
163        (show-hot-count info max-count))))
164  (values))
165
166(defun start-profiler (&key type)
167  "Starts the profiler.
168  :TYPE may be either :TIME (statistical sampling) or :COUNT-ONLY (exact call
169  counts)."
170  (unless type
171    (setf type :time))
172  (unless (memq type '(:time :count-only))
173    (error ":TYPE must be :TIME or :COUNT-ONLY"))
174  (setf *type* type)
175  (%start-profiler type *granularity*))
176
177(defmacro with-profiling ((&key type) &body body)
178  `(unwind-protect (progn (start-profiler :type ,type) ,@body)
179                   (stop-profiler)))
Note: See TracBrowser for help on using the repository browser.