From: Stephen Adams Date: Wed, 14 Dec 1994 20:33:47 +0000 (+0000) Subject: Added some compiler statistics utilities. The idea is that all you do X-Git-Tag: 20090517-FFI~6884 X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=637116d5a6e2230f4ac241d1e16e512aac2a3fc8;p=mit-scheme.git Added some compiler statistics utilities. The idea is that all you do is place a call to SAMPLE/1 or SAMPLE/2 at the appropriate place in the code, and NOT rely on their arguments for a side effect. Each statistic has a name (a symbol), a type (a symbol) and perhaps some optional parameters. The calls to SAMPLE/n create the statistic if it unknown, and then incorporate the data for generating a report. (COMPILER:RESET-STATISTICS!) (COMPILER:DISPLAY-STATISTICS) (SAMPLE/1 '(name type [parameters]) datum) (SAMPLE/2 '(name type [parameters]) datum1 datum2) Current unary statistics are '(name AVERAGE) ) - number, sum and average '(name HISTOGRAM VECTOR) - histogram small non-negative integers '(name HISTOGRAM HASH-TABLE) - histogram numbers, strings, symbols etc '(name HISTOGRAM) - same as HASH-TABLE Currently there are no binary statistics. --- diff --git a/v8/src/compiler/base/utils.scm b/v8/src/compiler/base/utils.scm index 4830c6f9c..1ffab4620 100644 --- a/v8/src/compiler/base/utils.scm +++ b/v8/src/compiler/base/utils.scm @@ -1,6 +1,6 @@ #| -*-Scheme-*- -$Id: utils.scm,v 1.1 1994/11/19 02:02:36 adams Exp $ +$Id: utils.scm,v 1.2 1994/12/14 20:33:47 adams Exp $ Copyright (c) 1987-1994 Massachusetts Institute of Technology @@ -387,4 +387,179 @@ MIT in each case. |# ((predicate (car l)) (loop (cdr l) (cons (car l) yes) no)) (else - (loop (cdr l) yes (cons (car l) no)))))) \ No newline at end of file + (loop (cdr l) yes (cons (car l) no)))))) + +;; Statistics gathering machinery +;; +;; (compiler:reset-statistics!) +;; (compiler:display-statistics) +;; (sample/1 '(name type [parameters]) datum) +;; (sample/2 '(name type [parameters]) datum1 datum2) + + +(define *compiler-statistics-enabled?* #T) +(define *compiler-statistics* (make-eq-hash-table)) + +(define (compiler:reset-statistics!) + (set! *compiler-statistics* (make-eq-hash-table)) + unspecific) + +(define-structure + (compiler-statistic + (conc-name compiler-statistic/)) + sample + display) + +(define (compiler:display-statistics) + (for-each (lambda (name.statistic) + (newline) (newline) + ((compiler-statistic/display (cdr name.statistic)))) + (sort + (hash-table->alist *compiler-statistics*) + (lambda (pair1 pair2) + (symbol (lambda (stat) + (apply (compiler-statistic/sample stat) data) + datum)) + (else + (define-compiler-statistic (length data) statistic) + (warn "SAMPLE should be replaced with call to SAMPLE/1 or SAMPLE/2" + `(sample ,statistic ...)) + (apply sample/1/really statistic data)))) + +(define (sample/1/really statistic datum) + (cond ((find-statistic statistic) + => (lambda (stat) + ((compiler-statistic/sample stat) datum))) + (else + (define-compiler-statistic 1 statistic) + (sample/1/really statistic datum)))) + +(define (sample/2/really statistic datum1 datum2) + (cond ((find-statistic statistic) + => (lambda (stat) + ((compiler-statistic/sample stat) datum1 datum2))) + (else + (define-compiler-statistic 2 statistic) + (sample/2/really statistic datum1 datum2)))) + +(define-integrable (find-statistic specification) + (if (pair? statistic) + (hash-table/get *compiler-statistics* (car specification) #F) + #F)) + +(define (define-compiler-statistic arity specification) + arity ; ignored + (if (not (and (pair? specification) + (pair? (cdr specification)))) + (error "Illegal compiler-statistic specification:" specification)) + (let ((name (first specification)) + (type (second specification))) + (cond ((hash-table/get *compiler-statistic-types* type #F) + => (lambda (maker) + (let ((statistic (apply maker specification))) + (hash-table/put! *compiler-statistics* name statistic)))) + (else + (error "Unknown compiler-statistic type:" type specification))))) + +(define *compiler-statistic-types* (make-eq-hash-table)) + +(define (define-statistic-type name maker) + (hash-table/put! *compiler-statistic-types* name maker)) + + +(define-statistic-type 'COUNT + (lambda (name type) + (let ((count 0)) + (define (sample datum) + (set! count (+ count datum)) + unspecific) + (define (print) + (define (say . stuff) (for-each display stuff)) + (say name " " type " " count)) + (make-compiler-statistic sample print)))) + + +(define-statistic-type 'AVERAGE + (lambda (name type) + (let ((n 0) + (sum 0)) + (define (sample datum) + (set! n (+ n 1)) + (set! sum (+ sum datum)) + unspecific) + (define (print) + (define (say . stuff) (for-each display stuff)) + (say name " " type " ") + (say " n = " n " sum = " sum " mean = " (/ sum n))) + (make-compiler-statistic sample print)))) + +(define-statistic-type 'HISTOGRAM + (lambda (name type #!optional method) + + (define (key (object-type u) (object-type v)) + #F) + ((and (symbol? u) (symbol? v)) + (symbolalist samples) + (lambda (u v) (key= datum (vector-length samples)) + (set! samples (vector-grow samples (+ 1 datum)))) + (vector-set! samples datum + (+ 1 (or (vector-ref samples datum) 0)))) + (define (print) + (pp-alist (list-transform-positive + (vector->list + (make-initialized-vector (vector-length samples) + (lambda (i) (cons i (vector-ref samples i))))) + cdr))) + (make-compiler-statistic sample print))) + + (let ((method (if (default-object? method) 'HASH-TABLE method))) + (case method + ((HASH-TABLE HASH) (make-hash-table-histogram)) + ((VECTOR) (make-vector-histogram)) + (else + (error "Unknown histogram method:" method `(,name ,type ,method)))))))