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 ;;; Returns two values:
74 ;;; - the minutes west of GMT.
75 ;;; - T if daylight savings is in effect, NIL if not.
76 (sb!alien:define-alien-routine get-timezone sb!alien:void
77 (when sb!alien:long :in)
78 (minutes-west sb!alien:int :out)
79 (daylight-savings-p sb!alien:boolean :out))
81 ;;; Subtract from the returned Internal-Time to get the universal
82 ;;; time. The offset between our time base and the Perq one is 2145
83 ;;; weeks and five days.
84 (defconstant seconds-in-week (* 60 60 24 7))
85 (defconstant weeks-offset 2145)
86 (defconstant seconds-offset 432000)
87 (defconstant minutes-per-day (* 24 60))
88 (defconstant quarter-days-per-year (1+ (* 365 4)))
89 (defconstant quarter-days-per-century 146097)
90 (defconstant november-17-1858 678882)
91 (defconstant weekday-november-17-1858 2)
92 (defconstant unix-to-universal-time 2208988800)
94 (defun get-universal-time ()
96 "Return a single integer for the current time of
97 day in universal time format."
98 (multiple-value-bind (res secs) (sb!unix:unix-gettimeofday)
99 (declare (ignore res))
100 (+ secs unix-to-universal-time)))
102 (defun get-decoded-time ()
104 "Return nine values specifying the current time as follows:
105 second, minute, hour, date, month, year, day of week (0 = Monday), T
106 (daylight savings times) or NIL (standard time), and timezone."
107 (decode-universal-time (get-universal-time)))
109 (defun decode-universal-time (universal-time &optional time-zone)
111 "Converts a universal-time to decoded time format returning the following
112 nine values: second, minute, hour, date, month, year, day of week (0 =
113 Monday), T (daylight savings time) or NIL (standard time), and timezone.
114 Completely ignores daylight-savings-time when time-zone is supplied."
115 (multiple-value-bind (weeks secs)
116 (truncate (+ universal-time seconds-offset)
118 (let* ((weeks (+ weeks weeks-offset))
127 (timezone (if (null time-zone)
130 (get-timezone (- universal-time
131 unix-to-universal-time))
132 (declare (ignore ignore))
136 (declare (fixnum timezone))
137 (multiple-value-bind (t1 seconds) (truncate secs 60)
138 (setq second seconds)
139 (setq t1 (- t1 timezone))
140 (let* ((tday (if (< t1 0)
141 (1- (truncate (1+ t1) minutes-per-day))
142 (truncate t1 minutes-per-day))))
143 (multiple-value-setq (hour minute)
144 (truncate (- t1 (* tday minutes-per-day)) 60))
145 (let* ((t2 (1- (* (+ (* weeks 7) tday november-17-1858) 4)))
146 (tcent (truncate t2 quarter-days-per-century)))
147 (setq t2 (mod t2 quarter-days-per-century))
148 (setq t2 (+ (- t2 (mod t2 4)) 3))
149 (setq year (+ (* tcent 100) (truncate t2 quarter-days-per-year)))
150 (let ((days-since-mar0 (1+ (truncate (mod t2 quarter-days-per-year)
152 (setq day (mod (+ tday weekday-november-17-1858) 7))
153 (let ((t3 (+ (* days-since-mar0 5) 456)))
155 (setq t3 (- t3 1836))
156 (setq year (1+ year))))
157 (multiple-value-setq (month t3) (truncate t3 153))
158 (setq date (1+ (truncate t3 5))))))))
159 (values second minute hour date month year day
165 (defun pick-obvious-year (year)
166 (declare (type (mod 100) year))
167 (let* ((current-year (nth-value 5 (get-decoded-time)))
168 (guess (+ year (* (truncate (- current-year 50) 100) 100))))
169 (declare (type (integer 1900 9999) current-year guess))
170 (if (> (- current-year guess) 50)
174 (defun leap-years-before (year)
175 (let ((years (- year 1901)))
176 (+ (- (truncate years 4)
177 (truncate years 100))
178 (truncate (+ years 300) 400))))
180 (defvar *days-before-month*
181 #.(let ((reversed-result nil)
183 (push nil reversed-result)
184 (dolist (days-in-month '(31 28 31 30 31 30 31 31 30 31 30 31))
185 (push sum reversed-result)
186 (incf sum days-in-month))
187 (coerce (nreverse reversed-result) 'simple-vector)))
189 (defun encode-universal-time (second minute hour date month year
192 "The time values specified in decoded format are converted to
193 universal time, which is returned."
194 (declare (type (mod 60) second)
195 (type (mod 60) minute)
197 (type (integer 1 31) date)
198 (type (integer 1 12) month)
199 (type (or (integer 0 99) (integer 1900)) year)
200 (type (or null rational) time-zone))
201 (let* ((year (if (< year 100)
202 (pick-obvious-year year)
205 (aref *days-before-month* month)
207 (leap-years-before (1+ year))
208 (leap-years-before year))
209 (* (- year 1900) 365)))
210 (hours (+ hour (* days 24))))
212 (+ second (* (+ minute (* (+ hours time-zone) 60)) 60))
213 (let* ((minwest-guess
215 (get-timezone (- (* hours 60 60)
216 unix-to-universal-time))))
217 (guess (+ minute (* hours 60) minwest-guess))
220 (get-timezone (- (* guess 60)
221 unix-to-universal-time)))))
222 (+ second (* (+ guess (- minwest minwest-guess)) 60))))))
226 (defmacro time (form)
228 "Execute FORM and print timing information on *TRACE-OUTPUT*."
229 `(%time (lambda () ,form)))
231 ;;; Return all the data that we want TIME to report.
232 (defun time-get-sys-info ()
233 (multiple-value-bind (user sys faults) (sb!sys:get-system-info)
234 (values user sys faults (get-bytes-consed))))
236 ;;; The guts of the TIME macro. Compute overheads, run the (compiled)
237 ;;; function, report the times.
254 ;; Calculate the overhead...
256 (old-run-utime old-run-stime old-page-faults old-bytes-consed)
258 ;; Do it a second time to make sure everything is faulted in.
260 (old-run-utime old-run-stime old-page-faults old-bytes-consed)
263 (new-run-utime new-run-stime new-page-faults new-bytes-consed)
265 (setq run-utime-overhead (- new-run-utime old-run-utime))
266 (setq run-stime-overhead (- new-run-stime old-run-stime))
267 (setq page-faults-overhead (- new-page-faults old-page-faults))
268 (setq old-real-time (get-internal-real-time))
269 (setq old-real-time (get-internal-real-time))
270 (setq new-real-time (get-internal-real-time))
271 (setq real-time-overhead (- new-real-time old-real-time))
272 (setq cons-overhead (- new-bytes-consed old-bytes-consed))
273 ;; Now get the initial times.
275 (old-run-utime old-run-stime old-page-faults old-bytes-consed)
277 (setq old-real-time (get-internal-real-time))
278 (let ((start-gc-run-time *gc-run-time*))
279 (multiple-value-prog1
280 ;; Execute the form and return its values.
283 (new-run-utime new-run-stime new-page-faults new-bytes-consed)
285 (setq new-real-time (- (get-internal-real-time) real-time-overhead))
286 (let ((gc-run-time (max (- *gc-run-time* start-gc-run-time) 0)))
287 (format *trace-output*
288 "~&Evaluation took:~% ~
289 ~S second~:P of real time~% ~
290 ~S second~:P of user run time~% ~
291 ~S second~:P of system run time~% ~
292 ~@[ [Run times include ~S second~:P GC run time.]~% ~]~
293 ~S page fault~:P and~% ~
295 (max (/ (- new-real-time old-real-time)
296 (float sb!xc:internal-time-units-per-second))
298 (max (/ (- new-run-utime old-run-utime) 1000000.0) 0.0)
299 (max (/ (- new-run-stime old-run-stime) 1000000.0) 0.0)
300 (unless (zerop gc-run-time)
301 (/ (float gc-run-time)
302 (float sb!xc:internal-time-units-per-second)))
303 (max (- new-page-faults old-page-faults) 0)
304 (max (- new-bytes-consed old-bytes-consed) 0)))))))