Switch remaining GPLv2+ Guile-VM headers to LGPLv3+.
[bpt/guile.git] / module / system / vm / profile.scm
1 ;;; Guile VM profiler
2
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
9 ;;;
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;;; Code:
20
21 (define-module (system vm profile)
22 #:use-module (system vm vm)
23 #:use-module (ice-9 format)
24 #:export (vm-profile))
25
26 (define (vm-profile vm objcode . opts)
27 (let ((flag (vm-option vm 'debug)))
28 (dynamic-wind
29 (lambda ()
30 (set-vm-option! vm 'debug #t)
31 (set-vm-option! vm 'profile-data '())
32 (add-hook! (vm-next-hook vm) profile-next)
33 (add-hook! (vm-enter-hook vm) profile-enter)
34 (add-hook! (vm-exit-hook vm) profile-exit))
35 (lambda ()
36 (vm-load vm objcode)
37 (print-result vm))
38 (lambda ()
39 (set-vm-option! vm 'debug flag)
40 (remove-hook! (vm-next-hook vm) profile-next)
41 (remove-hook! (vm-enter-hook vm) profile-enter)
42 (remove-hook! (vm-exit-hook vm) profile-exit)))))
43
44 (define (profile-next vm)
45 (set-vm-option! vm 'profile-data
46 (cons (vm-fetch-code vm) (vm-option vm 'profile-data))))
47
48 (define (profile-enter vm)
49 #f)
50
51 (define (profile-exit vm)
52 #f)
53
54 (define (print-result vm . opts)
55 (do ((data (vm-option vm 'profile-data) (cdr data))
56 (summary '() (let ((inst (caar data)))
57 (assq-set! summary inst
58 (1+ (or (assq-ref summary inst) 0))))))
59 ((null? data)
60 (display "Count Instruction\n")
61 (display "----- -----------\n")
62 (for-each (lambda (entry)
63 (format #t "~5@A ~A\n" (cdr entry) (car entry)))
64 (sort summary (lambda (e1 e2) (> (cdr e1) (cdr e2))))))))