X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Ftime.lisp;h=deda6de6f5b09c2661d9c6a269267ce419ef4251;hb=5423b2e0f7e7643001ed3ef2f66681c0114a72a6;hp=ddc8cbc9437cd6496f042e6eacfb1ff3d13ccc49;hpb=2e22486d5a66f899a2aeb08898b0cdd42dfc11f8;p=sbcl.git diff --git a/src/code/time.lisp b/src/code/time.lisp index ddc8cbc..deda6de 100644 --- a/src/code/time.lisp +++ b/src/code/time.lisp @@ -11,58 +11,21 @@ (in-package "SB!IMPL") -(defconstant sb!xc:internal-time-units-per-second 1000 - #!+sb-doc - "The number of internal time units that fit into a second. See - GET-INTERNAL-REAL-TIME and GET-INTERNAL-RUN-TIME.") - -(defconstant micro-seconds-per-internal-time-unit - (/ 1000000 sb!xc:internal-time-units-per-second)) - -;;; The base number of seconds for our internal "epoch". We initialize -;;; this to the time of the first call to GET-INTERNAL-REAL-TIME, and -;;; then subtract this out of the result. -(defvar *internal-real-time-base-seconds* nil) -(declaim (type (or (unsigned-byte 32) null) *internal-real-time-base-seconds*)) +(defun time-reinit () + (reinit-internal-real-time)) -(defun get-internal-real-time () - #!+sb-doc - "Return the real time in the internal time format. (See - INTERNAL-TIME-UNITS-PER-SECOND.) This is useful for finding elapsed time." - (multiple-value-bind (ignore seconds useconds) (sb!unix:unix-gettimeofday) - (declare (ignore ignore) (type (unsigned-byte 32) seconds useconds)) - (let ((base *internal-real-time-base-seconds*) - (uint (truncate useconds - micro-seconds-per-internal-time-unit))) - (declare (type (unsigned-byte 32) uint)) - (cond (base - (+ (* (- seconds base) - sb!xc:internal-time-units-per-second) - uint)) - (t - (setq *internal-real-time-base-seconds* seconds) - uint))))) +;;; Implemented in unix.lisp and win32.lisp. +#!+sb-doc +(setf (fdocumentation 'get-internal-real-time 'function) + "Return the real time (\"wallclock time\") since startup in the internal +time format. (See INTERNAL-TIME-UNITS-PER-SECOND.)") (defun get-internal-run-time () #!+sb-doc - "Return the run time in the internal time format. (See - INTERNAL-TIME-UNITS-PER-SECOND.) This is useful for finding CPU usage." - (multiple-value-bind (ignore utime-sec utime-usec stime-sec stime-usec) - (sb!unix:unix-fast-getrusage sb!unix:rusage_self) - (declare (ignore ignore) - (type (unsigned-byte 31) utime-sec stime-sec) - ;; (Classic CMU CL had these (MOD 1000000) instead, but - ;; at least in Linux 2.2.12, the type doesn't seem to be - ;; documented anywhere and the observed behavior is to - ;; sometimes return 1000000 exactly.) - (type (integer 0 1000000) utime-usec stime-usec)) - (let ((result (+ (* (+ utime-sec stime-sec) - sb!xc:internal-time-units-per-second) - (floor (+ utime-usec - stime-usec - (floor micro-seconds-per-internal-time-unit 2)) - micro-seconds-per-internal-time-unit)))) - result))) + "Return the run time used by the process in the internal time format. (See +INTERNAL-TIME-UNITS-PER-SECOND.) This is useful for finding CPU usage. +Includes both \"system\" and \"user\" time." + (system-internal-run-time)) ;;;; Encode and decode universal times. @@ -130,8 +93,8 @@ (defun get-universal-time () #!+sb-doc - "Return a single integer for the current time of - day in universal time format." + "Return a single integer for the current time of day in universal time +format." (multiple-value-bind (res secs) (sb!unix:unix-gettimeofday) (declare (ignore res)) (+ secs unix-to-universal-time))) @@ -302,7 +265,21 @@ (defmacro time (form) #!+sb-doc - "Execute FORM and print timing information on *TRACE-OUTPUT*." + "Execute FORM and print timing information on *TRACE-OUTPUT*. + +On some hardware platforms estimated processor cycle counts are +included in this output; this number is slightly inflated, since it +includes the pipeline involved in reading the cycle counter -- +executing \(TIME NIL) a few times will give you an idea of the +overhead, and its variance. The cycle counters are also per processor, +not per thread: if multiple threads are running on the same processor, +the reported counts will include cycles taken up by all threads +running on the processor where TIME was executed. Furthermore, if the +operating system migrates the thread to another processor between +reads of the cycle counter, the results will be completely bogus. +Finally, the counter is cycle counter, incremented by the hardware +even when the process is halted -- which is to say that cycles pass +normally during operations like SLEEP." `(%time (lambda () ,form))) ;;; Return all the data that we want TIME to report. @@ -310,6 +287,71 @@ (multiple-value-bind (user sys faults) (sb!sys:get-system-info) (values user sys faults (get-bytes-consed)))) + +(defun elapsed-cycles (h0 l0 h1 l1) + (declare (ignorable h0 l0 h1 l1)) + #!+cycle-counter + (+ (ash (- h1 h0) 32) + (- l1 l0)) + #!-cycle-counter + nil) +(declaim (inline read-cycle-counter)) +(defun read-cycle-counter () + #!+cycle-counter + (sb!vm::%read-cycle-counter) + #!-cycle-counter + (values 0 0)) + +;;; This is so that we don't have to worry about the vagaries of +;;; floating point printing, or about conversions to floats dropping +;;; or introducing decimals, which are liable to imply wrong precision. +(defun format-microseconds (stream usec &optional colonp atp) + (declare (ignore colonp)) + (%format-decimal stream usec 6) + (unless atp + (write-string " seconds" stream))) + +(defun format-milliseconds (stream usec &optional colonp atp) + (declare (ignore colonp)) + (%format-decimal stream usec 3) + (unless atp + (write-string " seconds" stream))) + +(defun %format-decimal (stream number power) + (declare (stream stream) + (integer number power)) + (when (minusp number) + (write-char #\- stream) + (setf number (- number))) + (let ((scale (expt 10 power))) + (flet ((%fraction (fraction) + (let ((scaled (* 10 fraction))) + (loop while (< scaled scale) + do (write-char #\0 stream) + (setf scaled (* scaled 10)))) + (format stream "~D" fraction)) + (%zeroes () + (let ((scaled (/ scale 10))) + (write-char #\0 stream) + (loop while (> scaled 1) + do (write-char #\0 stream) + (setf scaled (/ scaled 10)))))) + (cond ((zerop number) + (write-string "0." stream) + (%zeroes)) + ((< number scale) + (write-string "0." stream) + (%fraction number)) + ((= number scale) + (write-string "1." stream) + (%zeroes)) + ((> number scale) + (multiple-value-bind (whole fraction) (floor number scale) + (format stream "~D." whole) + (%fraction fraction)))))) + + nil) + ;;; The guts of the TIME macro. Compute overheads, run the (compiled) ;;; function, report the times. (defun %time (fun) @@ -353,30 +395,53 @@ (old-run-utime old-run-stime old-page-faults old-bytes-consed) (time-get-sys-info)) (setq old-real-time (get-internal-real-time)) - (let ((start-gc-run-time *gc-run-time*)) - (multiple-value-prog1 - ;; Execute the form and return its values. - (funcall fun) - (multiple-value-setq - (new-run-utime new-run-stime new-page-faults new-bytes-consed) - (time-get-sys-info)) - (setq new-real-time (- (get-internal-real-time) real-time-overhead)) - (let ((gc-run-time (max (- *gc-run-time* start-gc-run-time) 0))) - (format *trace-output* - "~&Evaluation took:~% ~ - ~S second~:P of real time~% ~ - ~S second~:P of user run time~% ~ - ~S second~:P of system run time~% ~ -~@[ [Run times include ~S second~:P GC run time.]~% ~]~ - ~S page fault~:P and~% ~ - ~:D bytes consed.~%" - (max (/ (- new-real-time old-real-time) - (float sb!xc:internal-time-units-per-second)) - 0.0) - (max (/ (- new-run-utime old-run-utime) 1000000.0) 0.0) - (max (/ (- new-run-stime old-run-stime) 1000000.0) 0.0) - (unless (zerop gc-run-time) - (/ (float gc-run-time) - (float sb!xc:internal-time-units-per-second))) - (max (- new-page-faults old-page-faults) 0) - (max (- new-bytes-consed old-bytes-consed) 0))))))) + (let ((start-gc-internal-run-time *gc-run-time*) + (*eval-calls* 0) + (sb!c::*lambda-conversions* 0)) + (declare (special *eval-calls* sb!c::*lambda-conversions*)) + (multiple-value-bind (h0 l0) (read-cycle-counter) + (multiple-value-prog1 + ;; Execute the form and return its values. + (funcall fun) + (multiple-value-bind (h1 l1) (read-cycle-counter) + (let ((stop-gc-internal-run-time *gc-run-time*)) + (multiple-value-setq + (new-run-utime new-run-stime new-page-faults new-bytes-consed) + (time-get-sys-info)) + (setq new-real-time (- (get-internal-real-time) real-time-overhead)) + (let* ((gc-internal-run-time (max (- stop-gc-internal-run-time start-gc-internal-run-time) 0)) + (real-time (max (- new-real-time old-real-time) 0)) + (user-run-time (max (- new-run-utime old-run-utime) 0)) + (system-run-time (max (- new-run-stime old-run-stime) 0)) + (total-run-time (+ user-run-time system-run-time)) + (cycles (elapsed-cycles h0 l0 h1 l1)) + (page-faults (max (- new-page-faults old-page-faults) 0))) + (format *trace-output* + "~&Evaluation took:~%~ + ~@< ~@;~/sb-impl::format-milliseconds/ of real time~%~ + ~/sb-impl::format-microseconds/ of total run time ~ + (~@/sb-impl::format-microseconds/ user, ~@/sb-impl::format-microseconds/ system)~%~ + ~[[ Run times consist of ~/sb-impl::format-milliseconds/ GC time, ~ + and ~/sb-impl::format-milliseconds/ non-GC time. ]~%~;~2*~]~ + ~,2F% CPU~%~ + ~@[~:D form~:P interpreted~%~]~ + ~@[~:D lambda~:P converted~%~]~ + ~@[~:D processor cycles~%~]~ + ~@[~:D page fault~:P~%~]~ + ~:D bytes consed~:>~%" + real-time + total-run-time + user-run-time + system-run-time + (if (zerop gc-internal-run-time) 1 0) + gc-internal-run-time + ;; Round up so we don't mislead by saying 0.0 seconds of non-GC time... + (- (ceiling total-run-time 1000) gc-internal-run-time) + (if (zerop real-time) + 100.0 + (float (* 100 (/ (round total-run-time 1000) real-time)))) + (unless (zerop *eval-calls*) *eval-calls*) + (unless (zerop sb!c::*lambda-conversions*) sb!c::*lambda-conversions*) + cycles + (unless (zerop page-faults) page-faults) + (max (- new-bytes-consed old-bytes-consed) 0))))))))))