1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 19:50:24 +02:00

statprof: when/unless instead of if.

* module/statprof.scm: Use when or unless instead of if, where
  appropriate.
This commit is contained in:
Andy Wingo 2014-02-22 14:54:17 +01:00
parent d20dd74eca
commit cad444e31a

View file

@ -258,7 +258,7 @@
(hit-count-call? #f) (hit-count-call? #f)
(state (existing-profiler-state))) (state (existing-profiler-state)))
(if (record-full-stacks? state) (when (record-full-stacks? state)
(set-stacks! state (cons stack (stacks state)))) (set-stacks! state (cons stack (stacks state))))
(set-sample-count! state (+ (sample-count state) 1)) (set-sample-count! state (+ (sample-count state) 1))
@ -301,7 +301,7 @@
;; FIXME: with-statprof should be able to set an outer frame for the ;; FIXME: with-statprof should be able to set an outer frame for the
;; stack cut ;; stack cut
(if (positive? (profile-level state)) (when (positive? (profile-level state))
(let* ((stop-time (get-internal-run-time)) (let* ((stop-time (get-internal-run-time))
;; cut down to the signal handler. note that this will only ;; cut down to the signal handler. note that this will only
;; work if statprof.scm is compiled; otherwise we get ;; work if statprof.scm is compiled; otherwise we get
@ -313,28 +313,26 @@
(pk 'what! (make-stack #t)))) (pk 'what! (make-stack #t))))
(inside-apply-trap? (sample-stack-procs stack))) (inside-apply-trap? (sample-stack-procs stack)))
(if (not inside-apply-trap?) (unless inside-apply-trap?
(begin
;; disabling here is just a little more efficient, but ;; disabling here is just a little more efficient, but
;; not necessary given inside-profiler?. We can't just ;; not necessary given inside-profiler?. We can't just
;; disable unconditionally at the top of this function ;; disable unconditionally at the top of this function
;; and eliminate inside-profiler? because it seems to ;; and eliminate inside-profiler? because it seems to
;; confuse guile wrt re-enabling the trap when ;; confuse guile wrt re-enabling the trap when
;; count-call finishes. ;; count-call finishes.
(if (count-calls? state) (when (count-calls? state)
(set-vm-trace-level! (1- (vm-trace-level)))) (set-vm-trace-level! (1- (vm-trace-level))))
(accumulate-time state stop-time))) (accumulate-time state stop-time))
(setitimer ITIMER_PROF (setitimer ITIMER_PROF
0 0 0 0
(car (sampling-frequency state)) (car (sampling-frequency state))
(cdr (sampling-frequency state))) (cdr (sampling-frequency state)))
(if (not inside-apply-trap?) (unless inside-apply-trap?
(begin
(set-last-start-time! state (get-internal-run-time)) (set-last-start-time! state (get-internal-run-time))
(if (count-calls? state) (when (count-calls? state)
(set-vm-trace-level! (1+ (vm-trace-level)))))))) (set-vm-trace-level! (1+ (vm-trace-level)))))))
(set-inside-profiler?! state #f)) (set-inside-profiler?! state #f))
@ -344,8 +342,7 @@
(define (count-call frame) (define (count-call frame)
(define state (existing-profiler-state)) (define state (existing-profiler-state))
(if (not (inside-profiler? state)) (unless (inside-profiler? state)
(begin
(accumulate-time state (get-internal-run-time)) (accumulate-time state (get-internal-run-time))
(and=> (frame-procedure frame) (and=> (frame-procedure frame)
@ -353,7 +350,7 @@
(inc-call-data-call-count! (inc-call-data-call-count!
(get-call-data proc)))) (get-call-data proc))))
(set-last-start-time! state (get-internal-run-time))))) (set-last-start-time! state (get-internal-run-time))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -370,12 +367,13 @@ than @code{statprof-stop}, @code{#f} otherwise."
;; signals here, but if I'm wrong, please let me know. ;; signals here, but if I'm wrong, please let me know.
(define state (ensure-profiler-state)) (define state (ensure-profiler-state))
(set-profile-level! state (+ (profile-level state) 1)) (set-profile-level! state (+ (profile-level state) 1))
(if (= (profile-level state) 1) (when (= (profile-level state) 1)
(let* ((rpt (remaining-prof-time state)) (let* ((rpt (remaining-prof-time state))
(use-rpt? (and rpt (use-rpt? (and rpt
(or (positive? (car rpt)) (or (positive? (car rpt))
(positive? (cdr rpt)))))) (positive? (cdr rpt))))))
(set-remaining-prof-time! state #f) (set-remaining-prof-time! state #f)
;; FIXME: Use per-thread run time.
(set-last-start-time! state (get-internal-run-time)) (set-last-start-time! state (get-internal-run-time))
(set-gc-time-taken! state (set-gc-time-taken! state
(cdr (assq 'gc-time-taken (gc-stats)))) (cdr (assq 'gc-time-taken (gc-stats))))
@ -385,7 +383,7 @@ than @code{statprof-stop}, @code{#f} otherwise."
0 0 0 0
(car (sampling-frequency state)) (car (sampling-frequency state))
(cdr (sampling-frequency state)))) (cdr (sampling-frequency state))))
(if (count-calls? state) (when (count-calls? state)
(add-hook! (vm-apply-hook) count-call)) (add-hook! (vm-apply-hook) count-call))
(set-vm-trace-level! (1+ (vm-trace-level))) (set-vm-trace-level! (1+ (vm-trace-level)))
#t))) #t)))
@ -397,19 +395,18 @@ than @code{statprof-stop}, @code{#f} otherwise."
;; signals here, but if I'm wrong, please let me know. ;; signals here, but if I'm wrong, please let me know.
(define state (ensure-profiler-state)) (define state (ensure-profiler-state))
(set-profile-level! state (- (profile-level state) 1)) (set-profile-level! state (- (profile-level state) 1))
(if (zero? (profile-level state)) (when (zero? (profile-level state))
(begin
(set-gc-time-taken! state (set-gc-time-taken! state
(- (cdr (assq 'gc-time-taken (gc-stats))) (- (cdr (assq 'gc-time-taken (gc-stats)))
(gc-time-taken state))) (gc-time-taken state)))
(set-vm-trace-level! (1- (vm-trace-level))) (set-vm-trace-level! (1- (vm-trace-level)))
(if (count-calls? state) (when (count-calls? state)
(remove-hook! (vm-apply-hook) count-call)) (remove-hook! (vm-apply-hook) count-call))
;; I believe that we need to do this before getting the time ;; I believe that we need to do this before getting the time
;; (unless we want to make things even more complicated). ;; (unless we want to make things even more complicated).
(set-remaining-prof-time! state (setitimer ITIMER_PROF 0 0 0 0)) (set-remaining-prof-time! state (setitimer ITIMER_PROF 0 0 0 0))
(accumulate-time state (get-internal-run-time)) (accumulate-time state (get-internal-run-time))
(set-last-start-time! state #f)))) (set-last-start-time! state #f)))
(define* (statprof-reset sample-seconds sample-microseconds count-calls? (define* (statprof-reset sample-seconds sample-microseconds count-calls?
#:optional full-stacks?) #:optional full-stacks?)
@ -568,7 +565,7 @@ statistics.@code{}"
(statprof-fold-call-data (statprof-fold-call-data
(lambda (data prior-value) (lambda (data prior-value)
(if (and (count-calls? state) (when (and (count-calls? state)
(zero? (call-data-call-count data)) (zero? (call-data-call-count data))
(positive? (call-data-cum-sample-count data))) (positive? (call-data-cum-sample-count data)))
(simple-format #t (simple-format #t
@ -686,10 +683,9 @@ whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
(statprof-start)) (statprof-start))
(lambda () (lambda ()
(let lp ((i loop)) (let lp ((i loop))
(if (not (zero? i)) (unless (zero? i)
(begin
(thunk) (thunk)
(lp (1- i)))))) (lp (1- i)))))
(lambda () (lambda ()
(statprof-stop) (statprof-stop)
(statprof-display) (statprof-display)
@ -752,7 +748,7 @@ whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
(define state (ensure-profiler-state)) (define state (ensure-profiler-state))
(define (reset) (define (reset)
(if (positive? (profile-level state)) (when (positive? (profile-level state))
(error "Can't reset profiler while profiler is running.")) (error "Can't reset profiler while profiler is running."))
(set-accumulated-time! state 0) (set-accumulated-time! state 0)
(set-last-start-time! state #f) (set-last-start-time! state #f)
@ -783,25 +779,23 @@ whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
(define (start) (define (start)
(set-profile-level! state (+ (profile-level state) 1)) (set-profile-level! state (+ (profile-level state) 1))
(if (= (profile-level state) 1) (when (= (profile-level state) 1)
(begin
(set-remaining-prof-time! state #f) (set-remaining-prof-time! state #f)
(set-last-start-time! state (get-internal-run-time)) (set-last-start-time! state (get-internal-run-time))
(set-gc-time-taken! state (cdr (assq 'gc-time-taken (gc-stats)))) (set-gc-time-taken! state (cdr (assq 'gc-time-taken (gc-stats))))
(add-hook! after-gc-hook gc-callback) (add-hook! after-gc-hook gc-callback)
(set-vm-trace-level! (1+ (vm-trace-level))) (set-vm-trace-level! (1+ (vm-trace-level)))
#t))) #t))
(define (stop) (define (stop)
(set-profile-level! state (- (profile-level state) 1)) (set-profile-level! state (- (profile-level state) 1))
(if (zero? (profile-level state)) (when (zero? (profile-level state))
(begin
(set-gc-time-taken! state (set-gc-time-taken! state
(- (cdr (assq 'gc-time-taken (gc-stats))) (- (cdr (assq 'gc-time-taken (gc-stats)))
(gc-time-taken state))) (gc-time-taken state)))
(remove-hook! after-gc-hook gc-callback) (remove-hook! after-gc-hook gc-callback)
(accumulate-time state (get-internal-run-time)) (accumulate-time state (get-internal-run-time))
(set-last-start-time! state #f)))) (set-last-start-time! state #f)))
(dynamic-wind (dynamic-wind
(lambda () (lambda ()
@ -809,10 +803,9 @@ whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
(start)) (start))
(lambda () (lambda ()
(let lp ((i loop)) (let lp ((i loop))
(if (not (zero? i)) (unless (zero? i)
(begin
(thunk) (thunk)
(lp (1- i)))))) (lp (1- i)))))
(lambda () (lambda ()
(stop) (stop)
(statprof-display) (statprof-display)