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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
; Milawa - A Reflective Theorem Prover
; Copyright (C) 2005-2009 Kookamara LLC
;
; Contact:
;
; Kookamara LLC
; 11410 Windermere Meadows
; Austin, TX 78759, USA
; http://www.kookamara.com/
;
; License: (An MIT/X11-style license)
;
; Permission is hereby granted, free of charge, to any person obtaining a
; copy of this software and associated documentation files (the "Software"),
; to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense,
; and/or sell copies of the Software, and to permit persons to whom the
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@kookamara.com>
(in-package "MILAWA")
(ACL2::set-current-package "MILAWA" ACL2::*the-live-state*)
;; This is the raw-lisp code for our rule profiler.
;;
;; Our goal is to pretend that the rewriter keeps a stack of the rules it is
;; backchaining through. For each rule, we want to count:
;;
;; - cost, the number of stack frames generated because of this rule,
;; - tries, the number of times the rule was actually tried explicitly, and
;; - successes, the number of times all the rule's hyps were relieved.
;;
;; Eventually all of this information gets put into the "stored costs" table
;; which maps rule names to cons-structures of the form:
;;
;; (successes . (cost . tries))
;;
;; We really want profiling to be efficient, so the code is relatively ugly and
;; optimized. But for a moment, let's consider the most naive implementation
;; of the profiler:
;;
;; every time a rule is tried,
;; - increment the "cost" for each "active rule",
;; - increment the "tries" for this rule,
;; - increment the "successes" for this rule, if successful
;;
;; But this wouldn't be very efficient because the number of "active rules"
;; will grow during backchaining, so incrementing them all might require quite
;; a bit of work.
;;
;; To make this efficient, we introduce the following strategy. There are two
;; data structures involved:
;;
;; - The "stored costs" table, as before, and
;; - The "active rules" stack, which has the form:
;; ( (cost1 . rune1) (cost2 . rune2) ... (costN . runeN) )
;;
;; Here, costI is the number of frames which runeN will be blamed for. We
;; update this structure as follows:
;;
;; (1) every time a rule is tried, we simply augment the list as:
;; (1 . newrune) (cost1 . rune1) ... (costN . runeN)
;;
;; (2) every time a cost1/rule1 are popped, we:
;; - add cost1 to cost2, so that rune2 will eventually inherit
; all the blame attributed to rune1
;; - add cost1 to rune1's cost in the stored costs table, so
;; that all the frames we want to give to rune1 are recorded
;; - add 1 to rune1's tried count in the stored costs table,
;; so that this additional try of rune1 is recorded
;;
;; This approach is dirtier but saves us from having to walk down the list and
;; increment all the costs, and still keeps us from hammering the table all the
;; time.
(ACL2::declaim (ACL2::type ACL2::fixnum *profile.cache-tries*))
(ACL2::declaim (ACL2::type ACL2::fixnum *profile.cache-hits*))
(ACL2::declaim (ACL2::type ACL2::hash-table *profile.stored-costs*))
(ACL2::declaim (ACL2::inline profile.update-stored-costs))
(ACL2::declaim (ACL2::inline profile.push-rune))
(ACL2::declaim (ACL2::inline profile.pop-rune))
;; This size is almost surely excessive, but it's only going to be allocated
;; once and I'd prefer if it never needed to be re-consed.
(ACL2::defparameter *profile.stored-costs* (ACL2::make-hash-table :size 8192 :test #'ACL2::eq))
(ACL2::defparameter *profile.active-rules* nil)
(ACL2::defparameter *profile.cache-tries* 0)
(ACL2::defparameter *profile.cache-hits* 0)
(ACL2::defmacro the-fixnum (n) `(ACL2::the ACL2::fixnum ,n))
(ACL2::defmacro when (&rest args) `(ACL2::when ,@args))
(ACL2::defmacro progn (&rest args) `(ACL2::progn ,@args))
(ACL2::defmacro incf (&rest args) `(ACL2::incf ,@args))
(ACL2::defmacro setf (&rest args) `(ACL2::setf ,@args))
(ACL2::defmacro gethash (&rest args) `(ACL2::gethash ,@args))
(ACL2::defun profile.update-stored-costs (rune cost successp)
(declare (ACL2::type ACL2::fixnum cost))
(let ((entry (gethash rune *profile.stored-costs*)))
(if entry
;; Entry has the form (successes . (cost . tries)); we are going to
;; destructively update each component.
(progn
(when successp (incf (the-fixnum (ACL2::car entry))))
(incf (the-fixnum (ACL2::cadr entry))
(the-fixnum cost))
(incf (the-fixnum (ACL2::cddr entry)))
nil)
;; Otherwise we need to create the initial entry for this rune. I was
;; quite surprised that (setf entry (cons ...)) didn't seem to work here.
;; Instead I had to repeat the gethash. I'm not sure why this would be.
(progn
(setf (gethash rune *profile.stored-costs*)
(cons (if successp 1 0)
(cons cost 1)))
nil))))
(ACL2::defun profile.push-rune (rune)
(ACL2::push (cons 1 rune) *profile.active-rules*))
(ACL2::defun profile.pop-rune (successp)
(when (consp *profile.active-rules*)
;; This guard is needed in order to properly handle interrupts.
;; If you interrupt in the middle of rewriting, generate a
;; report, and then hit :go to continue, the rewriter will be
;; exiting and trying to pop rules that you've already popped
;; when generating the report. We just silently ignore these
;; frames now. It's not quite accurate, but who really cares.
;; Profiling is just to give you an idea of big trends, not
;; little things.
(let* ((pair (ACL2::pop *profile.active-rules*))
(cost (the-fixnum (ACL2::car pair)))
(rune (ACL2::cdr pair)))
;; Normally there will be subsequent pairs, which will need to be updated.
(ACL2::when (consp *profile.active-rules*)
(ACL2::incf (the-fixnum (ACL2::caar *profile.active-rules*))
(the-fixnum cost)))
;; And of course we need to update the stored costs.
(profile.update-stored-costs rune cost successp))))
(ACL2::defun profile.pop-all ()
(if (consp *profile.active-rules*)
(progn (profile.pop-rune nil)
(profile.pop-all))
nil))
;; We also will profile execution of functions.
(ACL2::defparameter *profile.executed-fns* (ACL2::make-hash-table :size 8192 :test #'ACL2::eq))
(ACL2::defun %profile.clear ()
(setf *profile.active-rules* nil)
(setf *profile.cache-tries* 0)
(setf *profile.cache-hits* 0)
(ACL2::clrhash *profile.stored-costs*)
(ACL2::clrhash *profile.executed-fns*)
nil)
(acl2::defun left-pad (x desired-width)
(let ((actual-width (ACL2::length x)))
(STR::cat (STR::implode (repeat #\Space (- desired-width actual-width))) x)))
(acl2::defun pretty-fraction (x places)
(let* ((pretty-intpart (STR::pretty-number (ACL2::floor x 1)))
(floatpart (ACL2::mod (ACL2::floor (ACL2::* (ACL2::expt 10 places) (ACL2::abs x)) 1)
(ACL2::expt 10 places))))
(STR::cat pretty-intpart
"."
(STR::implode (ACL2::explode-atom floatpart 10)))))
(ACL2::defun profile.report-line (alist-entry)
(let* ((rune (ACL2::car alist-entry))
(successes (ACL2::cadr alist-entry))
(cost (ACL2::caddr alist-entry))
(tries (ACL2::cdddr alist-entry))
(rule (tactic.find-rule rune (ACL2::w ACL2::*the-live-state*)))
(splits (not (clause.simple-termp (rw.rule->rhs rule))))
(true-ratio (ACL2::/ cost tries))
(succ-color (if (zp successes) *red* *black*))
(ratio-color (cond ((ACL2::< true-ratio 20) *green*)
((ACL2::< true-ratio 100) *blue*)
(t *red*)))
(line (STR::cat succ-color (left-pad (STR::pretty-number successes) 10) *black*
" " (left-pad (STR::pretty-number tries) 10)
" " (left-pad (STR::pretty-number cost) 12)
" " ratio-color (left-pad (pretty-fraction true-ratio 2) 12) *black*
" " (if splits "*" " ") (ACL2::symbol-name rune))))
(ACL2::cw! "~s0~%" line)))
(ACL2::defun profile.names-of-unsuccessful-rules (alist)
(if (consp alist)
(let ((alist-entry (ACL2::car alist)))
(let ((rune (ACL2::car alist-entry))
(successes (ACL2::cadr alist-entry))
(cost (ACL2::caddr alist-entry)))
(if (and (zp successes)
(< 99 cost))
(cons rune (profile.names-of-unsuccessful-rules (cdr alist)))
(profile.names-of-unsuccessful-rules (cdr alist)))))
nil))
(ACL2::defun profile.report-lines (alist)
(if (consp alist)
(progn (profile.report-line (car alist))
(profile.report-lines (cdr alist)))
nil))
(ACL2::defun profile.exec-report-line (alist-entry)
(let* ((name (ACL2::car alist-entry))
(tries (ACL2::cadr alist-entry))
(successes (ACL2::cddr alist-entry))
(line (STR::cat
(left-pad (STR::pretty-number tries) 10)
" " (left-pad (STR::pretty-number successes) 10)
" " (ACL2::symbol-name name))))
(ACL2::cw! "~s0~%" line)))
(ACL2::defun profile.exec-report-lines (alist)
(if (consp alist)
(progn (profile.exec-report-line (car alist))
(profile.exec-report-lines (cdr alist)))
nil))
(ACL2::defun %profile.report ()
;; If we were interrupted, there may be entries on the active rule
;; stack. We pop them all to put them into stored costs.
(profile.pop-all)
(let ((alist nil))
(ACL2::loop for key being the hash-keys of *profile.stored-costs*
using (hash-value value)
do (ACL2::push (cons key value) alist))
(let ((sorted-alist (ACL2::sort alist #'(lambda (x y) (ACL2::> (ACL2::caddr x) (ACL2::caddr y))))))
(ACL2::cw "~%The following statistics were gathered since the last (%profile) or ~
(%profile.clear) was issued.~%~%")
(ACL2::cw (STR::cat *blue* "Rewrite Rule Report~%~%" *black*))
(let* ((percent (acl2::floor (acl2::* 100 *profile.cache-hits*)
(if (acl2::= *profile.cache-tries* 0)
1
*profile.cache-tries*)))
(pct-color (cond ((< percent 15) *red*)
((< percent 30) *blue*)
(t *green*))))
(ACL2::cw "~s0~%~%"
(STR::cat "Cache hit rate: " pct-color percent "%" *black* " ("
(STR::pretty-number *profile.cache-hits*)
" hits in "
(STR::pretty-number *profile.cache-tries*)
" tries)")))
(ACL2::cw "In the following table,~% ~
- \"Success\" counts how many times all the hyps were relieved.~% ~
- \"Frames\" counts how many rules were tried due to this rule backchaining.~% ~
- \"Tries\" counts how many times this rule itself was tried.~% ~
- \"Ratio\" is the average number of frames per try.~% ~
- A star indicates this rule can cause case splits.~%~% ~
Success Tries Frames Ratio Rule~%~%")
(profile.report-lines sorted-alist)
;; Execution report.
(let ((exec-alist nil))
(ACL2::loop for key being the hash-keys of *profile.executed-fns*
using (hash-value value)
do (ACL2::push (cons key value) exec-alist))
(let ((sorted-exec-alist
(ACL2::sort exec-alist #'(lambda (x y)
(ACL2::> (ACL2::cadr x) (ACL2::cadr y))))))
(ACL2::cw (STR::cat *blue* "~%Execution report.~%~%" *black*))
(ACL2::cw " Tries Successes Function~%")
(profile.exec-report-lines sorted-exec-alist)))
(let ((useless-rules (profile.names-of-unsuccessful-rules sorted-alist)))
(if useless-rules
(progn
(ACL2::cw (STR::cat *blue* "~%~%Useless, Expensive Rules~%~%" *black*))
(ACL2::cw "The following rules were never successful and each took over 100 frames. ~
To speed up your rewriting, you may wish to consider disabling them:~%~%~
~x0.~%~%" useless-rules))
(ACL2::cw "~%~%")))
)))
#+ccl
(ACL2::defun %profile ()
(ACL2::progn
(ACL2::redef-notinline rw.flag-crewrite)
(ACL2::redef-notinline rw.fast-flag-crewrite) ;; wtf is this?
(%profile.clear)
(CCL:advise rw.try-ground-simplify
(let ((x (ACL2::second CCL::arglist))
(answer (ACL2::first CCL::values)))
(when (logic.functionp x)
(let* ((name (logic.function-name x))
;; Entry ::= (tries . successes) or NIL if not yet entered.
(entry (gethash name *profile.executed-fns*))
(tries (if entry (car entry) 0))
(succs (if entry (cdr entry) 0)))
(setf (gethash name *profile.executed-fns*)
(cons (+ 1 tries)
(if answer (+ 1 succs) succs))))))
:when :after)
(CCL:advise rw.fast-try-ground-simplify
(let ((x (ACL2::second CCL::arglist))
(answer (ACL2::first CCL::values)))
(when (logic.functionp x)
(let* ((name (logic.function-name x))
;; Entry ::= (tries . successes) or NIL if not yet entered.
(entry (gethash name *profile.executed-fns*))
(tries (if entry (car entry) 0))
(succs (if entry (cdr entry) 0)))
(setf (gethash name *profile.executed-fns*)
(cons (+ 1 tries)
(if answer (+ 1 succs) succs))))))
:when :after)
(CCL:advise rw.flag-crewrite
(let ((flag (ACL2::first CCL::arglist)))
(if (ACL2::eq flag 'match)
(let ((rule[s] (ACL2::fourth CCL::arglist)))
(profile.push-rune (rw.rule->name rule[s])))
nil))
:when :before)
(CCL:advise rw.flag-crewrite
(let ((flag (ACL2::first CCL::arglist)))
(if (ACL2::eq flag 'match)
;; We have to "car" this twice, because CCL::values supports multiple-value
;; returns. The first car gets us to our 3-tuple, and the second car gets
;; us our trace/nil entry.
(let ((trace/nil (ACL2::car (ACL2::first CCL::values))))
(profile.pop-rune trace/nil))
nil))
:when :after)
(CCL:advise rw.fast-flag-crewrite
(let ((flag (ACL2::first CCL::arglist)))
(if (ACL2::eq flag 'match)
(let ((rule[s] (ACL2::fourth CCL::arglist)))
(profile.push-rune (rw.rule->name rule[s])))
nil))
:when :before)
(CCL:advise rw.fast-flag-crewrite
(let ((flag (ACL2::first CCL::arglist)))
(if (ACL2::eq flag 'match)
;; We have to "car" this twice, because CCL::values supports multiple-value
;; returns. The first car gets us to our 3-tuple, and the second car gets
;; us our trace/nil entry.
(let ((trace/nil (ACL2::car (ACL2::first CCL::values))))
(profile.pop-rune trace/nil))
nil))
:when :after)
(CCL:advise rw.cache-lookup
(let ((answer (ACL2::first CCL::values)))
(ACL2::incf *profile.cache-tries*)
(ACL2::when answer (ACL2::incf *profile.cache-hits*)))
:when :after)
(CCL:advise rw.fast-cache-lookup
(let ((answer (ACL2::first CCL::values)))
(ACL2::incf *profile.cache-tries*)
(ACL2::when answer (ACL2::incf *profile.cache-hits*)))
:when :after)
nil))
#+ccl
(ACL2::defun %profile.stop ()
(ACL2::progn
(CCL:unadvise rw.try-ground-simplify)
(CCL:unadvise rw.fast-try-ground-simplify)
(CCL:unadvise rw.flag-crewrite)
(CCL:unadvise rw.fast-flag-crewrite)
(CCL:unadvise rw.cache-lookup)
(CCL:unadvise rw.fast-cache-lookup)
(ACL2::redef-original rw.flag-crewrite) ;; wtf is this?
(ACL2::redef-original rw.fast-flag-crewrite) ;; wtf is this?
nil))
#|
(profile.push-rune 'a)
(profile.push-rune 'b)
(profile.push-rune 'c)
(profile.push-rune 'd)
(profile.pop-rune)
(profile.pop-rune)
(profile.push-rune 'e)
(profile.push-rune 'f)
(profile.pop-rune)
(profile.pop-rune)
(profile.pop-rune)
(profile.pop-rune)
(%profile.report)
|#
|