3 exec ${GUILE-guile} --no-debug -q -l "$0" \
4 -c '(apply main (cdr (command-line)))' "$@"
6 ;;; Copyright
(C
) 2008 Free Software Foundation
, Inc.
8 ;;; This program is free software
; you can redistribute it and
/or
9 ;;; modify it under the terms of the GNU Lesser General Public License
10 ;;; as published by the Free Software Foundation
; either version
3, or
11 ;;; (at your option
) any later version.
13 ;;; This program is distributed
in the hope that it will be useful
,
14 ;;; but WITHOUT ANY WARRANTY
; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU Lesser General Public License
for more details.
18 ;;; You should have received a copy of the GNU Lesser General Public
19 ;;; License along with this software
; see the
file COPYING.LESSER. If
20 ;;; not
, write to the Free Software Foundation
, Inc.
, 51 Franklin
21 ;;; Street
, Fifth Floor
, Boston
, MA
02110-1301 USA
23 (use-modules
(ice-9 format
)
35 (define
(memory-mappings pid
)
36 "Return an list of alists, each of which contains information about a
37 memory mapping of process @var{pid}. This information is obtained by reading
38 @file{/proc/PID/smaps} on Linux. See `procs(5)' for details."
40 (define mapping-line-rx
42 "^([[:xdigit:]]+)-([[:xdigit:]]+) ([rwx-]{3}[ps]) ([[:xdigit:]]+) [0-9]{2}:[0-9]{2} [0-9]+[[:blank:]]+(.*)$"))
46 "^Rss:[[:blank:]]+([[:digit:]]+) kB$"))
48 (with-input-from-port
(open-input-file
(format
#f "/proc/~a/smaps" pid))
50 (let loop
((line
(read-line
))
52 (if (eof-object? line)
54 (cond ((regexp-exec mapping-line-rx line)
57 (let ((mapping-start (string->number
58 (match:substring match 1)
60 (mapping-end (string->number
61 (match:substring match 2)
63 (access-bits (match:substring match 3))
64 (name (match:substring match 5)))
66 (cons `((mapping-start . ,mapping-start)
67 (mapping-end . ,mapping-end)
68 (access-bits . ,access-bits)
69 (name . ,(if (string=? name "")
73 ((regexp-exec rss-line-rx line)
76 (let ((section+ (cons (cons 'rss
78 (match
:substring match
1)))
81 (cons section
+ (cdr result
))))))
83 (loop
(read-line
) result
))))))))
85 (define
(total-heap-size pid
)
86 "Return the total heap size of process @var{pid}."
88 (define heap-or-anon-rx
89 (make-regexp
"\\[(heap|anon)\\]"))
91 (define private-mapping-rx
92 (make-regexp
"^[r-][w-][x-]p$"))
94 (fold (lambda
(heap total
+rss
)
95 (let ((name
(assoc-ref heap
'name))
96 (perm (assoc-ref heap 'access-bits
)))
97 ;; Include anonymous private mappings.
98 (if (or
(and
(not name
)
99 (regexp-exec private-mapping-rx perm
))
101 (regexp-exec heap-or-anon-rx name
)))
102 (let ((start
(assoc-ref heap
'mapping-start))
103 (end (assoc-ref heap 'mapping-end
))
104 (rss
(assoc-ref heap
'rss)))
105 (cons (+ (car total+rss) (- end start))
106 (+ (cdr total+rss) rss)))
109 (memory-mappings pid
)))
112 (define
(display-stats start end
)
113 (define
(->usecs sec
+usecs
)
114 (+ (* 1000000 (car sec
+usecs
))
117 (let ((usecs
(- (->usecs end
) (->usecs start
)))
118 (heap-size
(total-heap-size
(getpid
)))
119 (gc-heap-size
(assoc-ref
(gc-stats
) 'heap-size)))
121 (format #t "execution time: ~6,3f seconds~%"
125 (format #t "GC-reported heap size: ~8d B (~1,2f MiB)~%"
127 (/ gc-heap-size 1024.0 1024.0)))
129 (format #t "heap size: ~8d B (~1,2f MiB)~%"
131 (/ (car heap-size) 1024.0 1024.0))
132 (format #t "heap RSS: ~8d KiB (~1,2f MiB)~%"
134 (/ (cdr heap-size) 1024.0))
135 ;; (system (format #f "cat /proc/~a/smaps" (getpid)))
136 ;; (system (format #f "exmtool procs | grep -E '^
(PID|~a
)'" (getpid)))
141 ;;; Larceny/Twobit benchmarking compability layer.
144 (define *iteration-count*
147 (define (run-benchmark name . args)
148 "A @code{run-benchmark} procedure compatible with Larceny's GC benchmarking
150 @url
{http
://www.ccs.neu.edu
/home
/will
/Twobit
/benchmarksAbout.html
} for
153 (define %concise-invocation?
154 ;; This procedure can be called with only two arguments, NAME and
156 (procedure? (car args)))
158 (let ((count (or (*iteration-count*)
159 (if %concise-invocation? 0 (car args))))
160 (run-maker (if %concise-invocation? (car args) (cadr args)))
161 (ok? (if %concise-invocation?
164 (args (if %concise-invocation? '() (cdddr args))))
167 (let ((result (apply run-maker args)))
168 (if (not (ok? result))
170 (format (current-output-port) "invalid result
for `~A'~%"
175 (define (save-directory-excursion directory thunk)
176 (let ((previous-dir (getcwd)))
182 (chdir previous-dir)))))
184 (define (load-larceny-benchmark file)
185 "Load the Larceny benchmark from @var{file}."
186 (let ((name (let ((base (basename file)))
187 (substring base 0 (or (string-rindex base #\.)
188 (string-length base)))))
189 (module (let ((m (make-module)))
190 (beautify-user-module! m)
191 (module-use! m (resolve-interface '(ice-9 syncase)))
193 (save-directory-excursion (dirname file)
195 (save-module-excursion
197 (set-current-module module)
198 (module-define! module 'run-benchmark run-benchmark)
199 (load (basename file))
201 ;; Invoke the benchmark's entry point.
202 (let ((entry (module-ref (current-module)
203 (symbol-append (string->symbol name)
210 ;;; Option processing.
214 (list (option '(#\h "help") #f #f
218 (option '(#\l "larceny") #f #f
219 (lambda (opt name arg result)
220 (alist-cons 'larceny? #t result)))
221 (option '(#\i "iterations") #t #f
222 (lambda (opt name arg result)
223 (alist-cons 'iterations (string->number arg) result)))))
226 (format #t "Usage: gc-profile [OPTIONS] FILE.SCM
227 Load FILE.SCM, a Guile Scheme source file, and report its execution time and
230 -h, --help Show this help message
232 -l, --larceny Provide mechanisms compatible with the Larceny/Twobit
234 -i, --iterations=COUNT
235 Run the given benchmark COUNT times, regardless of the
236 iteration count passed to `run-benchmark
' (for Larceny
239 Report bugs to <bug-guile@gnu.org>.~%"))
241 (define (parse-args args)
242 (define (leave fmt . args)
243 (apply format (current-error-port) (string-append fmt "~%") args)
246 (args-fold args %options
247 (lambda (opt name arg result)
248 (leave "~A: unrecognized option" opt))
249 (lambda (file result)
250 (if (pair? (assoc 'input result
))
251 (leave
"~a: only one input file at a time" file)
252 (alist-cons
'input file result)))
260 (define
(main . args
)
261 (let* ((options
(parse-args args
))
262 (prog
(assoc-ref options
'input))
263 (load (if (assoc-ref options 'larceny?
)
264 load-larceny-benchmark
267 (parameterize
((*iteration-count
* (assoc-ref options
'iterations)))
268 (format #t "running `~a' with Guile ~a...~
%" prog (version))
270 (let ((start (gettimeofday)))
275 (set! quit (lambda args args))
278 (let ((end (gettimeofday)))
280 (display-stats start end))))))))