Skip to content

Commit

Permalink
sb-sprof: New interface functions MAP-{TRACES,TRACE-SAMPLES,ALL-SAMPLES}
Browse files Browse the repository at this point in the history
  • Loading branch information
scymtym committed Mar 13, 2018
1 parent cc58b59 commit 0ed8eb0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
changes relative to sbcl-1.4.5:
* enhancement: DISASSEMBLE on a symbol naming a macro will disassemble
the expander, not the code that traps attempted FUNCALL of the macro.
* enhancement: The sb-sprof contrib now provides an experimental interface
for accessing collected profiler data.

changes in sbcl-1.4.5 relative to sbcl-1.4.4:
* minor incompatible change: building with/without the :sb-package-locks
Expand Down
1 change: 1 addition & 0 deletions contrib/sb-sprof/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(:export
;; Recording
#:start-sampling #:stop-sampling #:with-sampling
#:map-traces #:map-trace-samples #:map-all-samples

;; Call counting
#:profile-call-counts #:unprofile-call-counts
Expand Down
60 changes: 60 additions & 0 deletions contrib/sb-sprof/record.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,67 @@
(setf (aref vector index) info
(aref vector (1+ index)) pc-or-offset)
(setf (samples-index samples) (+ index 2))))

;;; Trace and sample and access functions

(defun map-traces (function samples)
"Call FUNCTION on each trace in SAMPLES
The lambda list of FUNCTION has to be compatible to
(trace)
. FUNCTION is called once for each trace such that TRACE is an opaque
object whose only purpose is being used as the second argument to
MAP-CALLS.
EXPERIMENTAL: Interface subject to change."
(let ((function (sb-kernel:%coerce-callable-to-fun function))
(vector (samples-vector samples))
(index (samples-index samples)))
(sb-int:aver (eq (aref vector 0) 'trace-start))
(loop for start = 0 then end
while (< start index)
for end = (or (position 'trace-start vector :start (+ start 1))
index)
do (let ((trace (list vector start end)))
(funcall function trace)))))

(defun map-trace-samples (function trace)
"Call FUNCTION on each sample in TRACE.
The lambda list of FUNCTION has to be compatible to
(info pc-or-offset)
.
TRACE is an object as received by a function passed to MAP-TRACES.
EXPERIMENTAL: Interface subject to change."
(let ((function (sb-kernel:%coerce-callable-to-fun function)))
(destructuring-bind (samples start end) trace
(loop for i from (- end 2) downto (+ start 2) by 2
for info = (aref samples i)
for pc-or-offset = (aref samples (1+ i))
do (funcall function info pc-or-offset)))))

(declaim (special *samples*))
(defun map-all-samples (function &optional (samples *samples*))
"Call FUNCTION on each sample in SAMPLES.
The lambda list of FUNCTION has to be compatible to
(info pc-or-offset)
.
SAMPLES is usually the value of *SAMPLES* after a profiling run.
EXPERIMENTAL: Interface subject to change."
(sb-int:dx-flet ((do-trace (trace)
(map-trace-samples function trace)))
(map-traces #'do-trace samples)))

;;; Sampling

Expand Down
6 changes: 6 additions & 0 deletions contrib/sb-sprof/sb-sprof.texinfo
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ depth of more than two levels is only supported on x86 and x86-64.

@subsection Functions

@include fun-sb-sprof-map-traces.texinfo

@include fun-sb-sprof-map-trace-samples.texinfo

@include fun-sb-sprof-map-all-samples.texinfo

@include fun-sb-sprof-report.texinfo

@include fun-sb-sprof-reset.texinfo
Expand Down

0 comments on commit 0ed8eb0

Please sign in to comment.