1 | ;;; profiler.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2005 Peter Graves |
---|
4 | ;;; $Id: profiler.lisp,v 1.17 2005-05-03 01:43:33 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 | (export '(*hidden-functions*)) |
---|
23 | |
---|
24 | (require '#:clos) |
---|
25 | (require '#:format) |
---|
26 | |
---|
27 | (defvar *type* nil) |
---|
28 | |
---|
29 | (defvar *granularity* 1 "Sampling interval (in milliseconds).") |
---|
30 | |
---|
31 | (defvar *hidden-functions* |
---|
32 | '(funcall apply eval |
---|
33 | sys::%eval sys::interactive-eval |
---|
34 | tpl::repl tpl::top-level-loop)) |
---|
35 | |
---|
36 | (defstruct (profile-info |
---|
37 | (:constructor make-profile-info (object count))) |
---|
38 | object |
---|
39 | count) |
---|
40 | |
---|
41 | ;; Returns list of all symbols with non-zero call counts. |
---|
42 | (defun list-called-objects () |
---|
43 | (let ((result '())) |
---|
44 | (dolist (pkg (list-all-packages)) |
---|
45 | (dolist (sym (sys:package-symbols pkg)) |
---|
46 | (unless (memq sym *hidden-functions*) |
---|
47 | (when (fboundp sym) |
---|
48 | (let* ((definition (fdefinition sym)) |
---|
49 | (count (sys:call-count definition))) |
---|
50 | (unless (zerop count) |
---|
51 | (cond ((typep definition 'generic-function) |
---|
52 | (push (make-profile-info definition count) result) |
---|
53 | (dolist (method (mop::generic-function-methods definition)) |
---|
54 | (setf count (sys:call-count (sys:%method-function method))) |
---|
55 | (unless (zerop count) |
---|
56 | (push (make-profile-info method count) result)))) |
---|
57 | (t |
---|
58 | (push (make-profile-info sym count) result))))))))) |
---|
59 | (remove-duplicates result :key 'profile-info-object :test 'eq))) |
---|
60 | |
---|
61 | (defun object-name (object) |
---|
62 | (cond ((symbolp object) |
---|
63 | object) |
---|
64 | ((typep object 'generic-function) |
---|
65 | (sys:%generic-function-name object)) |
---|
66 | ((typep object 'method) |
---|
67 | (list 'METHOD |
---|
68 | (sys:%generic-function-name (sys:%method-generic-function object)) |
---|
69 | (sys:%method-specializers object))))) |
---|
70 | |
---|
71 | (defun object-compiled-function-p (object) |
---|
72 | (cond ((symbolp object) |
---|
73 | (compiled-function-p (fdefinition object))) |
---|
74 | ((typep object 'method) |
---|
75 | (compiled-function-p (sys:%method-function object))) |
---|
76 | (t |
---|
77 | (compiled-function-p object)))) |
---|
78 | |
---|
79 | (defun show-call-count (info max-count) |
---|
80 | (let* ((object (profile-info-object info)) |
---|
81 | (count (profile-info-count info))) |
---|
82 | (if max-count |
---|
83 | (format t "~5,1F ~8D ~S~A~%" |
---|
84 | (/ (* count 100.0) max-count) |
---|
85 | count |
---|
86 | (object-name object) |
---|
87 | (if (object-compiled-function-p object) |
---|
88 | "" |
---|
89 | " [interpreted function]")) |
---|
90 | (format t "~8D ~S~A~%" |
---|
91 | count |
---|
92 | (object-name object) |
---|
93 | (if (object-compiled-function-p object) |
---|
94 | "" |
---|
95 | " [interpreted function]"))))) |
---|
96 | |
---|
97 | (defun show-call-counts () |
---|
98 | (let ((list (list-called-objects))) |
---|
99 | (setf list (sort list #'< :key 'profile-info-count)) |
---|
100 | (let ((max-count nil)) |
---|
101 | (when (eq *type* :time) |
---|
102 | (let ((last-info (car (last list)))) |
---|
103 | (setf max-count (if last-info |
---|
104 | (profile-info-count last-info) |
---|
105 | nil)) |
---|
106 | (when (eql max-count 0) |
---|
107 | (setf max-count nil)))) |
---|
108 | (dolist (info list) |
---|
109 | (show-call-count info max-count)))) |
---|
110 | (values)) |
---|
111 | |
---|
112 | (defun start-profiler (&key type) |
---|
113 | "Starts the profiler. |
---|
114 | :TYPE may be either :TIME (statistical sampling) or :COUNT-ONLY (exact call |
---|
115 | counts)." |
---|
116 | (unless type |
---|
117 | (setf type :time)) |
---|
118 | (unless (memq type '(:time :count-only)) |
---|
119 | (error ":TYPE must be :TIME or :COUNT-ONLY")) |
---|
120 | (setf *type* type) |
---|
121 | (%start-profiler type *granularity*)) |
---|
122 | |
---|
123 | (defmacro with-profiling ((&key type) &body body) |
---|
124 | `(unwind-protect (progn (start-profiler :type ,type) ,@body) |
---|
125 | (stop-profiler))) |
---|