File: chicken-profile.scm

package info (click to toggle)
chicken 4.11.0-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 31,084 kB
  • sloc: ansic: 609,146; lisp: 68,137; tcl: 1,417; sh: 516; makefile: 62
file content (246 lines) | stat: -rw-r--r-- 8,609 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
;;;; chicken-profile.scm - Formatted display of profile outputs - felix -*- Scheme -*-
;
; Copyright (c) 2008-2016, The CHICKEN Team
; Copyright (c) 2000-2007, Felix L. Winkelmann
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
;   Redistributions of source code must retain the above copyright notice, this list of conditions and the following
;     disclaimer.
;   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
;     disclaimer in the documentation and/or other materials provided with the distribution.
;   Neither the name of the author nor the names of its contributors may be used to endorse or promote
;     products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.


(declare
  (block)
  (uses srfi-1
	srfi-13
	srfi-69
	posix
	utils))

(define sort-by #f)
(define file #f)
(define no-unused #f)
(define seconds-digits 3)
(define average-digits 3)
(define percent-digits 3)
(define top 0)

(define (print-usage)
  (display #<#EOF
Usage: chicken-profile [FILENAME | OPTION] ...

 -sort-by-calls            sort output by call frequency
 -sort-by-time             sort output by procedure execution time
 -sort-by-avg              sort output by average procedure execution time
 -sort-by-name             sort output alphabetically by procedure name
 -decimals DDD             set number of decimals for seconds, average and
                           percent columns (three digits, default: #{seconds-digits}#{average-digits}#{percent-digits})
 -no-unused                remove procedures that are never called
 -top N                    display only the top N entries
 -help                     show this text and exit
 -version                  show version and exit
 -release                  show release number and exit

 FILENAME defaults to the `PROFILE.<number>', selecting the one with
 the highest modification time, in case multiple profiles exist.

EOF
;|
)
 (exit 64) )

(define (run args)
  (let loop ([args args])
    (if (null? args)
	(begin
	  (unless file 
	    (set! file
	      (let ((fs (glob "PROFILE.*")))
		(if (null? fs)
		    (error "no PROFILEs found")
		    (first (sort fs 
				 (lambda (f1 f2)
				   (> (file-modification-time f1)
				      (file-modification-time f2))) ) ) ) ) ) )
	  (write-profile) )
	(let ([arg (car args)]
	      [rest (cdr args)] )
	  (define (next-arg)
	    (if (null? rest)
		(error "missing argument to option" arg)
		(let ((narg (car rest)))
		  (set! rest (cdr rest))
		  narg)))
	  (define (next-number)
	    (let ((n (string->number (next-arg))))
	      (if (and n (> n 0)) n (error "invalid argument to option" arg))))
	  (cond 
	   [(member arg '("-h" "-help" "--help")) (print-usage)]
	   [(string=? arg "-version")
	    (print "chicken-profile - Version " (chicken-version))
	    (exit) ]
	   [(string=? arg "-release")
	    (print (chicken-version))
	    (exit) ]
	   [(string=? arg "-no-unused") (set! no-unused #t)]
	   [(string=? arg "-top") (set! top (next-number))]
	   [(string=? arg "-sort-by-calls") (set! sort-by sort-by-calls)]
	   [(string=? arg "-sort-by-time") (set! sort-by sort-by-time)]
	   [(string=? arg "-sort-by-avg") (set! sort-by sort-by-avg)]
	   [(string=? arg "-sort-by-name") (set! sort-by sort-by-name)]
	   [(string=? arg "-decimals") (set-decimals (next-arg))]
	   [(and (> (string-length arg) 1) (char=? #\- (string-ref arg 0)))
	    (error "invalid option" arg) ]
	   [file (print-usage)]
	   [else (set! file arg)] )
	  (loop rest) ) ) ) )

(define (sort-by-calls x y)
  (let ([c1 (second x)]
	[c2 (second y)] )
    (if (eqv? c1 c2)
	(> (third x) (third y))
	(if c1 (if c2 (> c1 c2) #t) #t) ) ) )

(define (sort-by-time x y)
  (let ([c1 (third x)]
	[c2 (third y)] )
    (if (= c1 c2)
	(> (second x) (second y))
	(> c1 c2) ) ) )

(define (sort-by-avg x y)
  (let ([c1 (cadddr x)]
	[c2 (cadddr y)] )
    (if (eqv? c1 c2)
	(> (third x) (third y))
	(> c1 c2) ) ) )

(define (sort-by-name x y)
  (string<? (symbol->string (first x)) (symbol->string (first y))) )

(set! sort-by sort-by-time)

(define (set-decimals arg)
  (if (= (string-length arg) 3)
      (begin
	(define (arg-digit n)
	  (let ((n (- (char->integer (string-ref arg n))
		      (char->integer #\0))))
	    (if (<= 0 n 9)
		(if (= n 9) 8 n) ; 9 => overflow in format-real
		(error "invalid argument to -decimals option" arg))))
	(set! seconds-digits (arg-digit 0))
	(set! average-digits (arg-digit 1))
	(set! percent-digits (arg-digit 2)))
      (error "invalid argument to -decimals option" arg)))

(define (read-profile)
  (let* ((hash (make-hash-table eq?))
	 (header (read))
	 (type (if (symbol? header) header 'instrumented)))
    (do ((line (if (symbol? header) (read) header) (read)))
	((eof-object? line))
      (hash-table-set!
       hash (first line)
       (map (lambda (x y) (and x y (+ x y)))
	    (hash-table-ref/default hash (first line) '(0 0)) 
	    (cdr line))))
    (cons type (hash-table->alist hash))))

(define (format-string str cols #!optional right (padc #\space))
  (let* ((len (string-length str))
	 (pad (make-string (fxmax 0 (fx- cols len)) padc)) )
    (if right
	(string-append pad str)
	(string-append str pad) ) ) )

(define (format-real n d)
  (let ((exact-value (inexact->exact (truncate n))))
    (string-append
     (number->string exact-value)
     (if (> d 0) "." "")
     (substring
      (number->string
       (inexact->exact
	(truncate
	 (* (- n exact-value -1) (expt 10 d)))))
      1 (+ d 1)))))

(define (write-profile)
  (print "reading `" file "' ...\n")
  (let* ((type&data0 (with-input-from-file file read-profile))
	 (type  (car type&data0))
	 (data0 (cdr type&data0))
	 ;; Instrumented profiling results in total runtime being
	 ;; counted for the outermost "main" procedure, while
	 ;; statistical counts time spent only inside the procedure
	 ;; itself.  Ideally we'd have both, but that's tricky to do.
	 (total-t (foldl (if (eq? type 'instrumented)
			     (lambda (r t) (max r (third t)))
			     (lambda (r t) (+ r (third t))))
			 0 data0))
	 (data (sort (map
		      (lambda (t)
			(append
			 t
			 (let ((c (second t)) ; count
			       (t (third t))) ; time tallied to procedure
			   (list (or (and c (> c 0) (/ t c)) ; time / count
				     0)
				 (or (and (> total-t 0) (* (/ t total-t) 100)) ; % of total-time
				     0)
				 ))))
		      data0)
                     sort-by)))
    (if (< 0 top (length data))
	(set! data (take data top)))
    (set! data (map (lambda (entry)
		      (let ([c (second entry)] ; count
			    [t (third entry)]  ; total time
			    [a (fourth entry)] ; average time
			    [p (fifth entry)] ) ; % of max time
			(list (##sys#symbol->qualified-string (first entry))
			      (if (not c) "overflow" (number->string c))
			      (format-real (/ t 1000) seconds-digits)
			      (format-real (/ a 1000) average-digits)
			      (format-real p percent-digits))))
		    (remove (lambda (entry) 
			      (if (second entry) 
				  (and (zero? (second entry)) no-unused)
				  #f) )
			    data)))
    (let* ([headers (list "procedure" "calls" "seconds" "average" "percent")]
	   [alignments (list #f #t #t #t #t)]
	   [spacing 2]
	   [spacer (make-string spacing #\space)]
	   [column-widths (fold
			   (lambda (row max-widths)
			     (map max (map string-length row) max-widths))
			   (list 0 0 0 0 0)
			   (cons headers data))])
      (define (print-row row)
	(print (string-join (map format-string row column-widths alignments) spacer)))
      (print-row headers)
      (print (make-string (+ (reduce + 0 column-widths)
			     (* spacing (- (length alignments) 1)))
			  #\-))
      (for-each print-row data))))
  
(run (command-line-arguments))