1 ;;;; low-level time functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 (defconstant sb!xc:internal-time-units-per-second 1000
16 "The number of internal time units that fit into a second. See
17 GET-INTERNAL-REAL-TIME and GET-INTERNAL-RUN-TIME.")
19 (defconstant micro-seconds-per-internal-time-unit
20 (/ 1000000 sb!xc:internal-time-units-per-second))
22 ;;; The base number of seconds for our internal "epoch". We initialize
23 ;;; this to the time of the first call to GET-INTERNAL-REAL-TIME, and
24 ;;; then subtract this out of the result.
25 (defvar *internal-real-time-base-seconds* nil)
26 (declaim (type (or (unsigned-byte 32) null) *internal-real-time-base-seconds*))
28 (defun get-internal-real-time ()
30 "Return the real time in the internal time format. This is useful for
31 finding elapsed time. See INTERNAL-TIME-UNITS-PER-SECOND."
32 (multiple-value-bind (ignore seconds useconds) (sb!unix:unix-gettimeofday)
33 (declare (ignore ignore) (type (unsigned-byte 32) seconds useconds))
34 (let ((base *internal-real-time-base-seconds*)
35 (uint (truncate useconds
36 micro-seconds-per-internal-time-unit)))
37 (declare (type (unsigned-byte 32) uint))
39 (truly-the (unsigned-byte 32)
40 (+ (the (unsigned-byte 32)
41 (* (the (unsigned-byte 32) (- seconds base))
42 sb!xc:internal-time-units-per-second))
45 (setq *internal-real-time-base-seconds* seconds)
48 (defun get-internal-run-time ()
50 "Return the run time in the internal time format. This is useful for
52 (declare (values (unsigned-byte 32)))
53 (multiple-value-bind (ignore utime-sec utime-usec stime-sec stime-usec)
54 (sb!unix:unix-fast-getrusage sb!unix:rusage_self)
55 (declare (ignore ignore)
56 (type (unsigned-byte 31) utime-sec stime-sec)
57 ;; (Classic CMU CL had these (MOD 1000000) instead, but
58 ;; at least in Linux 2.2.12, the type doesn't seem to be
59 ;; documented anywhere and the observed behavior is to
60 ;; sometimes return 1000000 exactly.)
61 (type (integer 0 1000000) utime-usec stime-usec))
62 (let ((result (+ (the (unsigned-byte 32)
63 (* (the (unsigned-byte 32) (+ utime-sec stime-sec))
64 sb!xc:internal-time-units-per-second))
67 (floor micro-seconds-per-internal-time-unit 2))
68 micro-seconds-per-internal-time-unit))))
71 ;;;; Encode and decode universal times.
73 ;;; Subtract from the returned Internal-Time to get the universal
74 ;;; time. The offset between our time base and the Perq one is 2145
75 ;;; weeks and five days.
76 (defconstant seconds-in-week (* 60 60 24 7))
77 (defconstant weeks-offset 2145)
78 (defconstant seconds-offset 432000)
79 (defconstant minutes-per-day (* 24 60))
80 (defconstant quarter-days-per-year (1+ (* 365 4)))
81 (defconstant quarter-days-per-century 146097)
82 (defconstant november-17-1858 678882)
83 (defconstant weekday-november-17-1858 2)
84 (defconstant unix-to-universal-time 2208988800)
86 (defun get-universal-time ()
88 "Return a single integer for the current time of
89 day in universal time format."
90 (multiple-value-bind (res secs) (sb!unix:unix-gettimeofday)
91 (declare (ignore res))
92 (+ secs unix-to-universal-time)))
94 (defun get-decoded-time ()
96 "Return nine values specifying the current time as follows:
97 second, minute, hour, date, month, year, day of week (0 = Monday), T
98 (daylight savings times) or NIL (standard time), and timezone."
99 (decode-universal-time (get-universal-time)))
101 (defun decode-universal-time (universal-time &optional time-zone)
103 "Converts a universal-time to decoded time format returning the following
104 nine values: second, minute, hour, date, month, year, day of week (0 =
105 Monday), T (daylight savings time) or NIL (standard time), and timezone.
106 Completely ignores daylight-savings-time when time-zone is supplied."
107 (multiple-value-bind (weeks secs)
108 (truncate (+ universal-time seconds-offset)
110 (let* ((weeks (+ weeks weeks-offset))
123 (sb!unix::get-timezone (- universal-time
124 unix-to-universal-time))
125 (declare (ignore ignore))
126 (declare (fixnum minwest))
129 (t (* time-zone 60)))))
130 (declare (fixnum timezone))
131 (multiple-value-bind (t1 seconds) (truncate secs 60)
132 (setq second seconds)
133 (setq t1 (- t1 timezone))
134 (let* ((tday (if (< t1 0)
135 (1- (truncate (1+ t1) minutes-per-day))
136 (truncate t1 minutes-per-day))))
137 (multiple-value-setq (hour minute)
138 (truncate (- t1 (* tday minutes-per-day)) 60))
139 (let* ((t2 (1- (* (+ (* weeks 7) tday november-17-1858) 4)))
140 (tcent (truncate t2 quarter-days-per-century)))
141 (setq t2 (mod t2 quarter-days-per-century))
142 (setq t2 (+ (- t2 (mod t2 4)) 3))
143 (setq year (+ (* tcent 100) (truncate t2 quarter-days-per-year)))
144 (let ((days-since-mar0 (1+ (truncate (mod t2 quarter-days-per-year)
146 (setq day (mod (+ tday weekday-november-17-1858) 7))
147 (let ((t3 (+ (* days-since-mar0 5) 456)))
149 (setq t3 (- t3 1836))
150 (setq year (1+ year))))
151 (multiple-value-setq (month t3) (truncate t3 153))
152 (setq date (1+ (truncate t3 5))))))))
153 (values second minute hour date month year day
159 (defun pick-obvious-year (year)
160 (declare (type (mod 100) year))
161 (let* ((current-year (nth-value 5 (get-decoded-time)))
162 (guess (+ year (* (truncate (- current-year 50) 100) 100))))
163 (declare (type (integer 1900 9999) current-year guess))
164 (if (> (- current-year guess) 50)
168 (defun leap-years-before (year)
169 (let ((years (- year 1901)))
170 (+ (- (truncate years 4)
171 (truncate years 100))
172 (truncate (+ years 300) 400))))
174 (defvar *days-before-month*
175 #.(let ((reversed-result nil)
177 (push nil reversed-result)
178 (dolist (days-in-month '(31 28 31 30 31 30 31 31 30 31 30 31))
179 (push sum reversed-result)
180 (incf sum days-in-month))
181 (coerce (nreverse reversed-result) 'simple-vector)))
183 (defun encode-universal-time (second minute hour date month year
186 "The time values specified in decoded format are converted to
187 universal time, which is returned."
188 (declare (type (mod 60) second)
189 (type (mod 60) minute)
191 (type (integer 1 31) date)
192 (type (integer 1 12) month)
193 (type (or (integer 0 99) (integer 1900)) year)
194 (type (or null rational) time-zone))
195 (let* ((year (if (< year 100)
196 (pick-obvious-year year)
199 (aref *days-before-month* month)
201 (leap-years-before (1+ year))
202 (leap-years-before year))
203 (* (- year 1900) 365)))
204 (hours (+ hour (* days 24))))
206 (+ second (* (+ minute (* (+ hours time-zone) 60)) 60))
207 (let* ((minwest-guess
208 (sb!unix::unix-get-minutes-west (- (* hours 60 60)
209 unix-to-universal-time)))
210 (guess (+ minute (* hours 60) minwest-guess))
212 (sb!unix::unix-get-minutes-west (- (* guess 60)
213 unix-to-universal-time))))
214 (+ second (* (+ guess (- minwest minwest-guess)) 60))))))
218 (defmacro time (form)
220 "Execute FORM and print timing information on *TRACE-OUTPUT*."
221 `(%time (lambda () ,form)))
223 ;;; Return all the data that we want TIME to report.
224 (defun time-get-sys-info ()
225 (multiple-value-bind (user sys faults) (sb!sys:get-system-info)
226 (values user sys faults (get-bytes-consed))))
228 ;;; The guts of the TIME macro. Compute overheads, run the (compiled)
229 ;;; function, report the times.
231 (declare (type function fun))
247 ;; Calculate the overhead...
249 (old-run-utime old-run-stime old-page-faults old-bytes-consed)
251 ;; Do it a second time to make sure everything is faulted in.
253 (old-run-utime old-run-stime old-page-faults old-bytes-consed)
256 (new-run-utime new-run-stime new-page-faults new-bytes-consed)
258 (setq run-utime-overhead (- new-run-utime old-run-utime))
259 (setq run-stime-overhead (- new-run-stime old-run-stime))
260 (setq page-faults-overhead (- new-page-faults old-page-faults))
261 (setq old-real-time (get-internal-real-time))
262 (setq old-real-time (get-internal-real-time))
263 (setq new-real-time (get-internal-real-time))
264 (setq real-time-overhead (- new-real-time old-real-time))
265 (setq cons-overhead (- new-bytes-consed old-bytes-consed))
266 ;; Now get the initial times.
268 (old-run-utime old-run-stime old-page-faults old-bytes-consed)
270 (setq old-real-time (get-internal-real-time))
271 (let ((start-gc-run-time *gc-run-time*))
272 (multiple-value-prog1
273 ;; Execute the form and return its values.
276 (new-run-utime new-run-stime new-page-faults new-bytes-consed)
278 (setq new-real-time (- (get-internal-real-time) real-time-overhead))
279 (let ((gc-run-time (max (- *gc-run-time* start-gc-run-time) 0)))
280 (format *trace-output*
281 "~&Evaluation took:~% ~
282 ~S second~:P of real time~% ~
283 ~S second~:P of user run time~% ~
284 ~S second~:P of system run time~% ~
285 ~@[ [Run times include ~S second~:P GC run time.]~% ~]~
286 ~S page fault~:P and~% ~
288 (max (/ (- new-real-time old-real-time)
289 (float sb!xc:internal-time-units-per-second))
291 (max (/ (- new-run-utime old-run-utime) 1000000.0) 0.0)
292 (max (/ (- new-run-stime old-run-stime) 1000000.0) 0.0)
293 (unless (zerop gc-run-time)
294 (/ (float gc-run-time)
295 (float sb!xc:internal-time-units-per-second)))
296 (max (- new-page-faults old-page-faults) 0)
297 (max (- new-bytes-consed old-bytes-consed) 0)))))))