-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsam.lisp
180 lines (140 loc) · 6.24 KB
/
sam.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
;; 2016-07-05.142600+0200CEST.txt:(04:38:38 PM) rme: mrottenkolber: I fiddled
;; around with a crude userland-based sampling profiler a while ago. I pasted
;; it as http://paste.lisp.org/display/319875 if you happen to feel like toying
;; with it. It is crude: it just samples every millisecond, and records the
;; name of the function that the program counter is in.
(defpackage sam
(:documentation
"Tiny, concurrent sampling profiler for Clozure Common Lisp. Sam peeks into
the stack of a running process repeatedly to figure out where time is
spent.
*Example:*
#code#
(sam:profile ()
(loop for i from 1 to 1e8 sum i))
▷ 39% CCL::>-2 <no source>
▷ 19% CCL::FIXNUM-SFLOAT-COMPARE <no source>
▷ 15% #<Anonymous Function #x30200B10843F> <no source>
▷ 12% SAM:CALL-WITH-SAMPLING (defun call-with-sampling (fn ...
▷ 11% CCL::INTEGER-DECODE-SHORT-FLOAT <no source>
▷ 5% CCL::FIXNUM-DECODE-SHORT-FLOAT <no source>
#
*Credits:*
The core functionality of Sam was kindly provided by R. Matthew Emerson.")
(:use :cl :ccl)
(:export :call-with-sampling
:sample-process
:report-samples
:profile
:profile-process))
(in-package :sam)
(defparameter *default-sample-interval* 0.001)
(defparameter *default-cutoff* 3)
(defun sample-process-loop (process hash interval)
(loop
(map-call-frames (lambda (p context)
(declare (ignore context))
(incf (gethash (ccl::cfp-lfun p) hash 0)))
:process process
:count 1)
(sleep interval)))
(defun call-with-sampling (fn &key (interval *default-sample-interval*))
"*Arguments and Values:*
_fn_—a _function designator_.
_interval_—a positive _number_ designating a time in seconds. The default is
0.001.
*Description:*
{call-with-sampling} profiles _fn_, and returns a _hash table_ containing
the collected samples. _Interval_ specifies the time between samples
collected."
(check-type interval (real 0 *))
(let* ((h (make-hash-table))
(sampling-process (process-run-function "sampler"
'sample-process-loop
*current-process*
h
interval)))
(unwind-protect
(funcall fn)
(process-kill sampling-process))
h))
(defun sample-process (process duration
&key (interval *default-sample-interval*))
"*Arguments and Values:*
_process_—a {process}.
_duration_—a positive _number_ designating a time in seconds.
_interval_—a positive _number_ designating a time in seconds. The default is
0.001.
*Description:*
{sample-process} profiles _process_ for _duration_, and returns a _hash
table_ containing the collected samples. _Interval_ is the time between
samples collected."
(check-type interval (real 0 *))
(let* ((h (make-hash-table))
(sampling-process (process-run-function "sampler"
'sample-process-loop
process
h
interval)))
(unwind-protect (sleep duration)
(process-kill sampling-process))
h))
(defun percent (n total) (* 100 (/ n total)))
(defun format-source (text &key (truncate 30))
(if text
(format nil "~a~@[...~]"
(ccl::string-sans-most-whitespace text truncate)
(> (length text) truncate))
"<no source>"))
(defun report-samples (h &key (cutoff *default-cutoff*))
"*Arguments and Values:*
_h_—a _hash table_ containing samples.
_cutoff_—a _number_ of _type_ {(real 0 100)} designating a percentage. The
default is 3.
*Description:*
{report-samples} prints a summary of the samples in _h_, omitting samples
that constitute below _cutoff_ percent of the total samples."
(check-type cutoff (real 0 100))
(let* ((cf (/ cutoff 100))
(total (loop for n being the hash-values in h sum n))
(results (loop for f being the hash-keys in h using (hash-value n)
when (> (/ n total) cf) collect (cons f n))))
(loop for (f . n) in (sort results #'> :key 'cdr) do
(format t "~2d% ~s ~a~%"
(round (percent n total))
(or (function-name f) f)
(format-source (ignore-errors
(source-note-text
(function-source-note f)))))))
(values))
(defmacro profile ((&key (interval *default-sample-interval*)
(cutoff *default-cutoff*))
&body forms)
"*Arguments and Values:*
_interval_—a positive _number_ designating a time in seconds. The default is
0.001.
_cutoff_—a _number_ of _type_ {(real 0 100)} designating a percentage. The
default is 3.
_forms_—_forms_ to be evaluated.
*Description:*
{profile} profiles the evaluation of _forms_ and prints a summary of the
results, omitting samples that constitute below _cutoff_ percent of the
total samples. _Interval_ specifies the time between samples collected."
`(report-samples (call-with-sampling (lambda () ,@forms) :interval ,interval)
:cutoff ,cutoff))
(defun profile-process (process duration
&key (interval *default-sample-interval*)
(cutoff *default-cutoff*))
"*Arguments and Values:*
_process_—a {process}.
_duration_—a positive _number_ designating a time in seconds.
_interval_—a positive _number_ designating a time in seconds. The default is
0.001.
_cutoff_—a _number_ of _type_ {(real 0 100)} designating a percentage. The
default is 3.
*Description:*
{profile-process} profiles _process_ for _duration_, and prints a summary of
the results, omitting samples that constitute below _cutoff_ percent of the
total samples. _Interval_ specifies the time between samples collected."
(report-samples (sample-process process duration :interval interval)
:cutoff cutoff))