3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
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.
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.
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
21 (define-module (system vm profile)
22 #:use-module (system vm vm)
23 #:use-module (ice-9 format)
24 #:export (vm-profile))
26 (define (vm-profile vm objcode . opts)
27 (let ((flag (vm-option vm 'debug)))
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))
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)))))
44 (define (profile-next vm)
45 (set-vm-option! vm 'profile-data
46 (cons (vm-fetch-code vm) (vm-option vm 'profile-data))))
48 (define (profile-enter vm)
51 (define (profile-exit vm)
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))))))
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))))))))