1 ;; basic profiler for SIXPIC's pic18 simulator
2 ;; basically, just reports the execution count for each instruction that is
3 ;; kept by the simulator
5 ;; this is mostly taken from Guillaume Germain's statprof statistical profiler
6 ;; for Gambit-C, hence is distributed with the same licence as Gambit-C
8 ;; TODO this is not yet working, it has not been completely adapted. a temporary solution is being used instead
10 ;; ----------------------------------------------------------------------------
13 (define (pad-left s l c)
14 (let loop ((s (string->list s)))
20 ;; ----------------------------------------------------------------------------
21 ;; Palette generation & color formatting
23 (define (gradient from to step)
24 (let ((inc (map (lambda (x) (/ x step))
34 (round (+ x (* i o))))
43 (pad-left (number->string x 16) 2 #\0))
49 (gradient '(127 127 255)
54 ;; ----------------------------------------------------------------------------
55 ;; Functions to generate the report
57 (define (write-profile-report profile-name . sources)
63 (loop (- n 1) (cons n l))
66 (define directory-name (string-append (current-directory)
69 (with-exception-catcher
71 ;; ignore the exception, it probably means that the directory
72 ;; already existed. If there's another problem it will be
76 (create-directory (list path: directory-name
77 permissions: #o755))))
79 (let ((max-intensity 0))
80 (for-each (lambda (x) (let ((new (vector-ref instrs-counts x)))
81 (if (> new max-intensity)
82 (set! max-intensity new))))
83 (iota (vector-length instrs-counts)))
86 (let ((file (car bucket)) ;; TODO know which file we're coming from
90 (let ((i (vector-ref data n)))
92 (as-rgb (vector-ref palette 0))
93 (let ((x (* (/ (log (+ 1. i))
94 (max (ceiling (log max-intensity)) 1))
95 (- (vector-length palette) 1))))
96 (as-rgb (vector-ref palette
97 (inexact->exact (ceiling x))))))))
99 (with-output-to-file (string-append
101 (path-strip-directory file)
103 (let ((lines (call-with-input-file file
104 (lambda (p) (read-all p read-line)))))
114 style: "font-size: 12px;"
119 (number->string line#)
123 ;; ,(let ((n (vector-ref data line#)))
126 ;; (string-append "["
127 ;; (number->string n)
129 ;; (number->string *total*)
134 ,(let ((n (vector-ref data line#)))
139 (round% (/ n *total*)))
142 (td (pre style: ,(string-append
147 (iota1 (length lines)))))))))))))
149 (iota (vector-length instrs-counts))))
151 (with-output-to-file (string-append directory-name "index.html")
157 ,@(map (lambda (bucket)
158 (let ((file-path (string-append
160 (path-strip-directory (car bucket))
162 `(p (a href: ,file-path ,file-path)
165 (/ (apply + (vector->list (cdr bucket)))
176 ;; ----------------------------------------------------------------------------
177 ;; Included file "html.scm"
178 ;; ----------------------------------------------------------------------------
180 ;; html.scm -- A simple html generator for Gambit-C 4.0
182 ;; Written by Guillaume Germain (germaing@iro.umontreal.ca)
183 ;; This code is released in the public domain.
186 (define (stringify x)
187 (with-output-to-string ""
191 (define (to-escaped-string x)
202 ;; Quick and dirty conversion of s-expressions to html
203 (define (sexp->html exp)
205 ;; write the opening tag
206 (define (open-tag exp)
208 ;; null tag isn't valid
212 ;; a tag must be a list beginning with a symbol
218 (maybe-args (car exp) (cdr exp))))
221 (error "invalid tag" exp))))
223 ;; take care of the keywords / arguments
224 (define (maybe-args tag exp)
227 ;; does the rest of the list begins with a keyword
229 (keyword? (car exp)))
231 ;; does the keyword has an associated value?
232 (if (or (null? (cdr exp))
233 (keyword? (cadr exp)))
234 ;; no, we don't put an associated value
235 (list (keyword->string (car exp))
237 (maybe-args tag (cdr exp)))
238 ;; yes, we take the next element in the list as the value
239 (list (keyword->string (car exp))
243 (maybe-args tag (cddr exp)))))
245 ;; must just be some content
249 ;; handle the content of the tag and closing it
250 (define (content tag exp)
254 ;;(list "></" tag ">")) ; close as "<br></br>"
255 (list "/>")) ; close as "<br/>"
257 ;; write the content, handle tags inside
263 (to-escaped-string e)))
269 ;; non-null terminated list?
271 (error "strange content..."))))
273 (with-output-to-string ""
275 (print (open-tag exp)))))