Debian Codegraph @

scheme/ikarus.timer.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus timers)
18 (export time-it verbose-timer)
19 (import (except (ikarus) time-it verbose-timer))
20
21 (define-struct stats
22 (user-secs user-usecs
23 sys-secs sys-usecs
24 real-secs real-usecs
25 collection-id
26 gc-user-secs gc-user-usecs
27 gc-sys-secs gc-sys-usecs
28 gc-real-secs gc-real-usecs
29 bytes-minor bytes-major
30 ))
31
32 (define (mk-stats)
33 (make-stats #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f))
34
35 (define verbose-timer (make-parameter #f))
36
37 (define (set-stats! t)
38 (foreign-call "ikrt_stats_now" t))
39
40 (define (print-stats message t1 t0)
41 (define (print-time msg msecs gc-msecs)
42 (fprintf
43 (console-error-port)
44 " ~a ms elapsed ~a time, including ~a ms collecting\n" msecs msg
45 gc-msecs))
46 (define (msecs s1 s0 u1 u0)
47 (+ (* (- s1 s0) 1000) (quotient (- u1 u0) 1000)))
48 (if message
49 (fprintf (console-error-port) "running stats for ~a:\n" message)
50 (fprintf (console-error-port) "running stats:\n"))
51 (let ([collections
52 (fx- (stats-collection-id t1) (stats-collection-id t0))])
53 (case collections
54 [(0) (display " no collections\n" (console-error-port))]
55 [(1) (display " 1 collection\n" (console-error-port))]
56 [else (fprintf (console-error-port) " ~a collections\n" collections)]))
57 (print-time "cpu"
58 (+ (msecs (stats-user-secs t1) (stats-user-secs t0)
59 (stats-user-usecs t1) (stats-user-usecs t0))
60 (msecs (stats-sys-secs t1) (stats-sys-secs t0)
61 (stats-sys-usecs t1) (stats-sys-usecs t0)))
62 (+ (msecs (stats-gc-user-secs t1) (stats-gc-user-secs t0)
63 (stats-gc-user-usecs t1) (stats-gc-user-usecs t0))
64 (msecs (stats-gc-sys-secs t1) (stats-gc-sys-secs t0)
65 (stats-gc-sys-usecs t1) (stats-gc-sys-usecs t0))))
66 (print-time "real"
67 (msecs (stats-real-secs t1) (stats-real-secs t0)
68 (stats-real-usecs t1) (stats-real-usecs t0))
69 (msecs (stats-gc-real-secs t1) (stats-gc-real-secs t0)
70 (stats-gc-real-usecs t1) (stats-gc-real-usecs t0)))
71 (when (verbose-timer)
72 (print-time "user"
73 (msecs (stats-user-secs t1) (stats-user-secs t0)
74 (stats-user-usecs t1) (stats-user-usecs t0))
75 (msecs (stats-gc-user-secs t1) (stats-gc-user-secs t0)
76 (stats-gc-user-usecs t1) (stats-gc-user-usecs t0)))
77 (print-time "sys"
78 (msecs (stats-sys-secs t1) (stats-sys-secs t0)
79 (stats-sys-usecs t1) (stats-sys-usecs t0))
80 (msecs (stats-gc-sys-secs t1) (stats-gc-sys-secs t0)
81 (stats-gc-sys-usecs t1) (stats-gc-sys-usecs t0))))
82 (fprintf (console-error-port) " ~a bytes allocated\n"
83 (diff-bytes
84 (stats-bytes-minor t0)
85 (stats-bytes-major t0)
86 (stats-bytes-minor t1)
87 (stats-bytes-major t1))))
88
89 (define time-it
90 (case-lambda
91 [(proc)
92 (time-it #f proc)]
93 [(message proc)
94 (unless (procedure? proc)
95 (die 'time-it "not a procedure" proc))
96 (let* ([t0 (mk-stats)]
97 [t1 (mk-stats)])
98 (define k
99 (case-lambda
100 [(v)
101 (set-stats! t1)
102 (print-stats message t1 t0)
103 v]
104 [v*
105 (set-stats! t1)
106 (print-stats message t1 t0)
107 (apply values v*)]))
108 (set-stats! t0)
109 (call-with-values proc k))]))
110
111 (define (diff-bytes mnr0 mjr0 mnr1 mjr1)
112 (+ (fx- mnr1 mnr0) (* (fx- mjr1 mjr0) #x10000000)))
113
114 )