0.8.2.29:
[sbcl.git] / src / code / time.lisp
index c808845..fb35b18 100644 (file)
 
 (in-package "SB!IMPL")
 
-(defconstant internal-time-units-per-second 100
+(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 internal-time-units-per-second))
+  (/ 1000000 sb!xc:internal-time-units-per-second))
 \f
 ;;; 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
 
 (defun get-internal-real-time ()
   #!+sb-doc
-  "Return the real time in the internal time format. This is useful for
-  finding elapsed time. See Internal-Time-Units-Per-Second."
-  ;; FIXME: See comment on OPTIMIZE declaration in GET-INTERNAL-RUN-TIME.
-  (declare (optimize (speed 3) (safety 3)))
+  "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*)
             (truly-the (unsigned-byte 32)
                        (+ (the (unsigned-byte 32)
                                (* (the (unsigned-byte 32) (- seconds base))
-                                  internal-time-units-per-second))
+                                  sb!xc:internal-time-units-per-second))
                           uint)))
            (t
             (setq *internal-real-time-base-seconds* seconds)
             uint)))))
 
-#!-(and sparc svr4)
 (defun get-internal-run-time ()
   #!+sb-doc
-  "Return the run time in the internal time format. This is useful for
-  finding CPU usage."
+  "Return the run time in the internal time format. (See
+  INTERNAL-TIME-UNITS-PER-SECOND.) This is useful for finding CPU usage."
   (declare (values (unsigned-byte 32)))
-  ;; FIXME: In CMU CL this was (SPEED 3) (SAFETY 0), and perhaps
-  ;; someday it should be again, since overhead here is annoying. But
-  ;; it's even more annoying to worry about this function returning
-  ;; out-of-range values, so while debugging the profiling code,
-  ;; I set it to (SAFETY 3) for now.
-  (declare (optimize (speed 3) (safety 3)))
   (multiple-value-bind (ignore utime-sec utime-usec stime-sec stime-usec)
       (sb!unix:unix-fast-getrusage sb!unix:rusage_self)
     (declare (ignore ignore)
             ;; documented anywhere and the observed behavior is to
             ;; sometimes return 1000000 exactly.)
             (type (integer 0 1000000) utime-usec stime-usec))
-    (+ (the (unsigned-byte 32)
-           (* (the (unsigned-byte 32) (+ utime-sec stime-sec))
-              internal-time-units-per-second))
-       (truncate (+ utime-usec stime-usec)
-                micro-seconds-per-internal-time-unit))))
-
-#!+(and sparc svr4)
-(defun get-internal-run-time ()
-  #!+sb-doc
-  "Return the run time in the internal time format. This is useful for
-  finding CPU usage."
-  (declare (values (unsigned-byte 32)))
-  ;; FIXME: See comment on OPTIMIZE declaration in other
-  ;; version of GET-INTERNAL-RUN-TIME.
-  (declare (optimize (speed 3) (safety 3)))
-  (multiple-value-bind (ignore utime stime cutime cstime)
-      (sb!unix:unix-times)
-    (declare (ignore ignore cutime cstime)
-            (type (unsigned-byte 31) utime stime))
-    (the (unsigned-byte 32) (+ utime stime))))
+    (let ((result (+ (the (unsigned-byte 32)
+                         (* (the (unsigned-byte 32) (+ 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)))
 \f
 ;;;; Encode and decode universal times.
 
-;;; Returns two values:
-;;;  - the minutes west of GMT.
-;;;  - T if daylight savings is in effect, NIL if not.
-(sb!alien:def-alien-routine get-timezone sb!c-call:void
-  (when sb!c-call:long :in)
-  (minutes-west sb!c-call:int :out)
-  (daylight-savings-p sb!alien:boolean :out))
+;;; In August 2003, work was done in this file for more plausible
+;;; timezone handling after the unix timezone database runs out in
+;;; 2038.  We assume that timezone rules are trending sane rather than
+;;; insane, so for all years after the end of time_t we apply the
+;;; rules for 2035/2036 instead of the actual date asked for.  Making
+;;; the same assumption about the early 1900s would be less
+;;; reasonable, however, so please note that we're still broken for
+;;; local time between 1900-1-1 and 1901-12-13
+
+;;; It should be noted that 64 bit machines don't actually fix this
+;;; problem, at least as of 2003, because the Unix zonefiles are
+;;; specified in terms of 32 bit fields even on, say, the Alpha.  So,
+;;; references to the range of time_t elsewhere in this file should
+;;; rightly be read as shorthand for the range of an signed 32 bit
+;;; number of seconds since 1970-01-01
+
+;;; I'm obliged to Erik Naggum's "Long, Painful History of Time" paper
+;;; <http://heim.ifi.uio.no/~enag/lugm-time.html> for the choice of epoch
+;;; here.  By starting the year in March, we avoid having to test the month
+;;; whenever deciding whether to account for a leap day.  2000 is especially
+;;; special, because it's disvisible by 400, hence the start of a 400 year
+;;; leap year cycle
 
-;;; Subtract from the returned Internal-Time to get the universal time.
-;;; The offset between our time base and the Perq one is 2145 weeks and
-;;; five days.
+;;; If a universal-time is after time_t runs out, we find its offset
+;;; from 1st March of whichever year it falls in, then add that to
+;;; 2035-3-1.  This date has two relevant properties: (1) somewhere
+;;; near the end of time_t, and (2) preceding a leap year.  Thus a
+;;; date which is e.g. 365.5 days from March 1st in its year will be
+;;; treated for timezone lookup as if it were Feb 29th 2036
+
+;;; This epoch is used only for fixing the timezones-outside-time_t
+;;; problem.  Someday it would be nice to come back to this code and
+;;; see if the rest of the file and its references to Spice Lisp
+;;; history (Perq time base?) could be cleaned up any on this basis.
+;;; -- dan, 2003-08-08
+
+
+;;; Subtract from the returned Internal-Time to get the universal
+;;; time. The offset between our time base and the Perq one is 2145
+;;; weeks and five days.
 (defconstant seconds-in-week (* 60 60 24 7))
 (defconstant weeks-offset 2145)
 (defconstant seconds-offset 432000)
 
 (defun get-universal-time ()
   #!+sb-doc
-  "Returns a single integer for the current time of
+  "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))
 
 (defun get-decoded-time ()
   #!+sb-doc
-  "Returns nine values specifying the current time as follows:
+  "Return nine values specifying the current time as follows:
    second, minute, hour, date, month, year, day of week (0 = Monday), T
    (daylight savings times) or NIL (standard time), and timezone."
   (decode-universal-time (get-universal-time)))
 
+(defconstant +mar-1-2000+ #.(encode-universal-time 0 0 0 1 3 2000 0))
+(defconstant +mar-1-2035+ #.(encode-universal-time 0 0 0 1 3 2035 0))
+
+(defun years-since-mar-2000 (utime)
+  "Returns number of complete years since March 1st 2000, and remainder in seconds" 
+  (let* ((days-in-year (* 86400 365))
+        (days-in-4year (+ (* 4 days-in-year) 86400))
+        (days-in-100year (- (* 25 days-in-4year) 86400))
+        (days-in-400year (+ (* 4 days-in-100year) 86400))
+        (offset (- utime +mar-1-2000+))
+        (year 0))
+    (labels ((whole-num (x y inc max)
+              (let ((w (truncate x y)))
+                (when (and max (> w max)) (setf w max))
+                (incf year (* w inc))
+                (* w y))))
+      (decf offset (whole-num offset days-in-400year 400 nil))
+      (decf offset (whole-num offset days-in-100year 100 3))
+      (decf offset (whole-num offset days-in-4year 4 25))
+      (decf offset (whole-num offset days-in-year 1 3))
+      (values year offset))))
+
+(defun truncate-to-unix-range (utime)
+  (let ((unix-time (- utime unix-to-universal-time)))
+    (if (< unix-time (ash 1 31))
+       unix-time
+       (multiple-value-bind (year offset) (years-since-mar-2000 utime)
+         (+  +mar-1-2035+  (- unix-to-universal-time)  offset)))))
+  
 (defun decode-universal-time (universal-time &optional time-zone)
   #!+sb-doc
   "Converts a universal-time to decoded time format returning the following
           (year NIL)
           (day NIL)
           (daylight NIL)
-          (timezone (if (null time-zone)
-                        (multiple-value-bind
-                            (ignore minwest dst)
-                            (get-timezone (- universal-time
-                                             unix-to-universal-time))
-                          (declare (ignore ignore))
-                          (setf daylight dst)
-                          minwest)
-                        (* time-zone 60))))
+          (timezone (cond
+                      ((null time-zone)
+                       (multiple-value-bind (ignore minwest dst)
+                           (sb!unix::get-timezone
+                            (truncate-to-unix-range universal-time))
+                         (declare (ignore ignore))
+                         (declare (fixnum minwest))
+                         (setf daylight dst)
+                         minwest))
+                      (t (* time-zone 60)))))
       (declare (fixnum timezone))
       (multiple-value-bind (t1 seconds) (truncate secs 60)
        (setq second seconds)
        (incf sum days-in-month))
       (coerce (nreverse reversed-result) 'simple-vector)))
 
+           
 (defun encode-universal-time (second minute hour date month year
                                     &optional time-zone)
   #!+sb-doc
         (hours (+ hour (* days 24))))
     (if time-zone
        (+ second (* (+ minute (* (+ hours time-zone) 60)) 60))
-       (let* ((minwest-guess
-               (nth-value 1
-                          (get-timezone (- (* hours 60 60)
-                                           unix-to-universal-time))))
-              (guess (+ minute (* hours 60) minwest-guess))
-              (minwest
-               (nth-value 1
-                          (get-timezone (- (* guess 60)
-                                           unix-to-universal-time)))))
-         (+ second (* (+ guess (- minwest minwest-guess)) 60))))))
+       ;; can't ask unix for times after 2037: this is only a problem
+       ;; if we need to query the system timezone
+       (if (> year 2037)
+           (labels ((leap-year-p (year)
+                      (cond ((zerop (mod year 400)) t)
+                            ((zerop (mod year 100)) t)
+                            ((zerop (mod year 4)) t)
+                            (t nil))))
+             (let* ((fake-year (if (leap-year-p year) 2036 2037))
+                    (fake-time (encode-universal-time second minute hour
+                                                      date month fake-year)))
+               (+ fake-time
+                  (* 86400 (+ (* 365 (- year fake-year))
+                              (- (leap-years-before year)
+                                 (leap-years-before fake-year)))))))
+           (let* ((minwest-guess
+                   (sb!unix::unix-get-minutes-west
+                    (- (* hours 60 60) unix-to-universal-time)))
+                  (guess (+ minute (* hours 60) minwest-guess))
+                  (minwest
+                   (sb!unix::unix-get-minutes-west
+                    (- (* guess 60) unix-to-universal-time))))
+             (+ second (* (+ guess (- minwest minwest-guess)) 60)))))))
 \f
 ;;;; TIME
 
 (defmacro time (form)
   #!+sb-doc
-  "Evaluates the Form and prints timing information on *Trace-Output*."
-  `(%time #'(lambda () ,form)))
-
-;;; Try to compile the closure arg to %TIME if it is interpreted.
-(defun massage-time-function (fun)
-  (cond
-   ((sb!eval:interpreted-function-p fun)
-    (multiple-value-bind (def env-p) (function-lambda-expression fun)
-      (declare (ignore def))
-      (cond
-       (env-p
-       (warn "TIME form in a non-null environment, forced to interpret.~@
-              Compiling entire form will produce more accurate times.")
-       fun)
-       (t
-       (compile nil fun)))))
-   (t fun)))
+  "Execute FORM and print timing information on *TRACE-OUTPUT*."
+  `(%time (lambda () ,form)))
 
-;;; Return all the files that we want time to report.
+;;; Return all the data that we want TIME to report.
 (defun time-get-sys-info ()
   (multiple-value-bind (user sys faults) (sb!sys:get-system-info)
     (values user sys faults (get-bytes-consed))))
 ;;; The guts of the TIME macro. Compute overheads, run the (compiled)
 ;;; function, report the times.
 (defun %time (fun)
-  (let ((fun (massage-time-function fun))
-       old-run-utime
+  (declare (type function fun))
+  (let (old-run-utime
        new-run-utime
        old-run-stime
        new-run-stime
                 ~S page fault~:P and~%  ~
                 ~S bytes consed.~%"
                (max (/ (- new-real-time old-real-time)
-                       (float internal-time-units-per-second))
+                       (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 internal-time-units-per-second)))
+                    (float sb!xc:internal-time-units-per-second)))
                (max (- new-page-faults old-page-faults) 0)
                (max (- new-bytes-consed old-bytes-consed) 0)))))))