0.7.1.47:
[sbcl.git] / src / code / time.lisp
1 ;;;; low-level time functions
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!IMPL")
13
14 (defconstant sb!xc:internal-time-units-per-second 1000
15   #!+sb-doc
16   "The number of internal time units that fit into a second. See
17   GET-INTERNAL-REAL-TIME and GET-INTERNAL-RUN-TIME.")
18
19 (defconstant micro-seconds-per-internal-time-unit
20   (/ 1000000 sb!xc:internal-time-units-per-second))
21 \f
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*))
27
28 (defun get-internal-real-time ()
29   #!+sb-doc
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))
38       (cond (base
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))
43                            uint)))
44             (t
45              (setq *internal-real-time-base-seconds* seconds)
46              uint)))))
47
48 (defun get-internal-run-time ()
49   #!+sb-doc
50   "Return the run time in the internal time format. This is useful for
51   finding CPU usage."
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))
65                      (floor (+ utime-usec
66                                stime-usec
67                                (floor micro-seconds-per-internal-time-unit 2))
68                             micro-seconds-per-internal-time-unit))))
69       result)))
70 \f
71 ;;;; Encode and decode universal times.
72
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))
80
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)
93
94 (defun get-universal-time ()
95   #!+sb-doc
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)))
101
102 (defun get-decoded-time ()
103   #!+sb-doc
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)))
108
109 (defun decode-universal-time (universal-time &optional time-zone)
110   #!+sb-doc
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)
117                 seconds-in-week)
118     (let* ((weeks (+ weeks weeks-offset))
119            (second NIL)
120            (minute NIL)
121            (hour NIL)
122            (date NIL)
123            (month NIL)
124            (year NIL)
125            (day NIL)
126            (daylight NIL)
127            (timezone (if (null time-zone)
128                          (multiple-value-bind
129                              (ignore minwest dst)
130                              (get-timezone (- universal-time
131                                               unix-to-universal-time))
132                            (declare (ignore ignore))
133                            (setf daylight dst)
134                            minwest)
135                          (* time-zone 60))))
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)
151                                                  4))))
152               (setq day (mod (+ tday weekday-november-17-1858) 7))
153               (let ((t3 (+ (* days-since-mar0 5) 456)))
154                 (cond ((>= t3 1989)
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
160               daylight
161               (if daylight
162                   (1+ (/ timezone 60))
163                   (/ timezone 60))))))
164
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)
171         (+ guess 100)
172         guess)))
173
174 (defun leap-years-before (year)
175   (let ((years (- year 1901)))
176     (+ (- (truncate years 4)
177           (truncate years 100))
178        (truncate (+ years 300) 400))))
179
180 (defvar *days-before-month*
181   #.(let ((reversed-result nil)
182           (sum 0))
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)))
188
189 (defun encode-universal-time (second minute hour date month year
190                                      &optional time-zone)
191   #!+sb-doc
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)
196            (type (mod 24) hour)
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)
203                    year))
204          (days (+ (1- date)
205                   (aref *days-before-month* month)
206                   (if (> month 2)
207                       (leap-years-before (1+ year))
208                       (leap-years-before year))
209                   (* (- year 1900) 365)))
210          (hours (+ hour (* days 24))))
211     (if time-zone
212         (+ second (* (+ minute (* (+ hours time-zone) 60)) 60))
213         (let* ((minwest-guess
214                 (nth-value 1
215                            (get-timezone (- (* hours 60 60)
216                                             unix-to-universal-time))))
217                (guess (+ minute (* hours 60) minwest-guess))
218                (minwest
219                 (nth-value 1
220                            (get-timezone (- (* guess 60)
221                                             unix-to-universal-time)))))
222           (+ second (* (+ guess (- minwest minwest-guess)) 60))))))
223 \f
224 ;;;; TIME
225
226 (defmacro time (form)
227   #!+sb-doc
228   "Execute FORM and print timing information on *TRACE-OUTPUT*."
229   `(%time (lambda () ,form)))
230
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))))
235
236 ;;; The guts of the TIME macro. Compute overheads, run the (compiled)
237 ;;; function, report the times.
238 (defun %time (fun)
239   (let (old-run-utime
240         new-run-utime
241         old-run-stime
242         new-run-stime
243         old-real-time
244         new-real-time
245         old-page-faults
246         new-page-faults
247         real-time-overhead
248         run-utime-overhead
249         run-stime-overhead
250         page-faults-overhead
251         old-bytes-consed
252         new-bytes-consed
253         cons-overhead)
254     ;; Calculate the overhead...
255     (multiple-value-setq
256         (old-run-utime old-run-stime old-page-faults old-bytes-consed)
257       (time-get-sys-info))
258     ;; Do it a second time to make sure everything is faulted in.
259     (multiple-value-setq
260         (old-run-utime old-run-stime old-page-faults old-bytes-consed)
261       (time-get-sys-info))
262     (multiple-value-setq
263         (new-run-utime new-run-stime new-page-faults new-bytes-consed)
264       (time-get-sys-info))
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.
274     (multiple-value-setq
275         (old-run-utime old-run-stime old-page-faults old-bytes-consed)
276       (time-get-sys-info))
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.
281         (funcall fun)
282       (multiple-value-setq
283           (new-run-utime new-run-stime new-page-faults new-bytes-consed)
284         (time-get-sys-info))
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~%  ~
294                  ~S bytes consed.~%"
295                 (max (/ (- new-real-time old-real-time)
296                         (float sb!xc:internal-time-units-per-second))
297                      0.0)
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)))))))