/[cmucl]/src/code/time.lisp
ViewVC logotype

Contents of /src/code/time.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Fri Feb 14 23:45:37 1992 UTC (22 years, 2 months ago) by wlott
Branch: MAIN
Changes since 1.8: +6 -6 lines
Merged new-alien changes onto trunk.
1 ram 1.1 ;;; -*- Mode: Lisp; Package: Lisp; Log: code.log -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.6 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7     ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8     ;;;
9     (ext:file-comment
10 wlott 1.9 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/time.lisp,v 1.9 1992/02/14 23:45:37 wlott Exp $")
11 ram 1.6 ;;;
12 ram 1.1 ;;; **********************************************************************
13     ;;;
14     ;;; This file contains the definitions for the Spice Lisp time functions.
15     ;;; They are mostly fairly straightforwardly implemented as calls to the
16     ;;; time server.
17     ;;;
18     ;;; Written by Rob MacLachlan.
19     ;;;
20     (in-package 'lisp)
21     (export '(internal-time-units-per-second get-internal-real-time
22     get-internal-run-time get-universal-time
23     get-decoded-time encode-universal-time decode-universal-time))
24    
25     (defconstant internal-time-units-per-second 100
26     "The number of internal time units that fit into a second. See
27     Get-Internal-Real-Time and Get-Internal-Run-Time.")
28    
29 wlott 1.4 (defconstant micro-seconds-per-internal-time-unit
30     (/ 1000000 internal-time-units-per-second))
31    
32    
33 ram 1.1 (defmacro not-leap-year (year)
34     (let ((sym (gensym)))
35     `(let ((,sym ,year))
36     (cond ((eq (mod ,sym 4) 0)
37     (and (eq (mod ,sym 100) 0)
38     (not (eq (mod ,sym 400) 0))))
39     (T T)))))
40    
41 ram 1.5
42     ;;; The base number of seconds for our internal "epoch". We initialize this to
43     ;;; the time of the first call to G-I-R-T, and then subtract this out of the
44     ;;; result.
45     ;;;
46     (defvar *internal-real-time-base-seconds* nil)
47     (declaim (type (or (unsigned-byte 32) null) *internal-real-time-base-seconds*))
48    
49 ram 1.1 ;;; Get-Internal-Real-Time -- Public
50     ;;;
51     (defun get-internal-real-time ()
52     "Return the real time in the internal time format. This is useful for
53     finding elapsed time. See Internal-Time-Units-Per-Second."
54 ram 1.5 (locally (declare (optimize (speed 3) (safety 0)))
55 wlott 1.9 (multiple-value-bind (ignore seconds useconds) (unix:unix-gettimeofday)
56 ram 1.5 (declare (ignore ignore))
57     (let ((base *internal-real-time-base-seconds*)
58     (uint (truncate useconds
59     micro-seconds-per-internal-time-unit)))
60     (declare (type (unsigned-byte 32) uint))
61     (cond (base
62     (+ (* (the (unsigned-byte 32) (- seconds base))
63     internal-time-units-per-second)
64     uint))
65     (t
66     (setq *internal-real-time-base-seconds* seconds)
67     uint))))))
68 ram 1.1
69 ram 1.5
70 ram 1.1 ;;; Get-Internal-Run-Time -- Public
71     ;;;
72     (defun get-internal-run-time ()
73     "Return the run time in the internal time format. This is useful for
74     finding CPU usage."
75 ram 1.5 (locally (declare (optimize (speed 3) (safety 0)))
76     (multiple-value-bind (ignore utime stime)
77 wlott 1.9 (unix:unix-getrusage unix:rusage_self)
78 ram 1.5 (declare (ignore ignore))
79     (values (truncate (the (unsigned-byte 32) (+ utime stime))
80     micro-seconds-per-internal-time-unit)))))
81 wlott 1.4
82 ram 1.1
83     ;;; Subtract from the returned Internal_Time to get the universal time.
84     ;;; The offset between our time base and the Perq one is 2145 weeks and
85     ;;; five days.
86     ;;;
87     (defconstant seconds-in-week (* 60 60 24 7))
88     (defconstant weeks-offset 2145)
89     (defconstant seconds-offset 432000)
90     (defconstant minutes-per-day (* 24 60))
91     (defconstant quarter-days-per-year (1+ (* 365 4)))
92     (defconstant quarter-days-per-century 146097)
93     (defconstant november-17-1858 678882)
94     (defconstant weekday-november-17-1858 2)
95     (defconstant unix-to-universal-time 2208988800)
96    
97     ;;; Make-Universal-Time -- Internal
98     ;;;
99     ;;; Convert a Unix Internal_Time into a universal time.
100     ;;;
101     (defun make-universal-time (weeks msec)
102     (+ (* (- weeks weeks-offset) seconds-in-week)
103     (- (truncate msec 1000) seconds-offset)))
104    
105 ram 1.3
106 ram 1.1 ;;; Get-Universal-Time -- Public
107     ;;;
108     ;;;
109     (defun get-universal-time ()
110     "Returns a single integer for the current time of
111     day in universal time format."
112 wlott 1.9 (multiple-value-bind (res secs) (unix:unix-gettimeofday)
113 ram 1.1 (declare (ignore res))
114     (+ secs unix-to-universal-time)))
115    
116     (defun get-decoded-time ()
117     "Returns nine values specifying the current time as follows:
118 ram 1.3 second, minute, hour, date, month, year, day of week (0 = Monday), T
119     (daylight savings times) or NIL (standard time), and timezone."
120 ram 1.1 (decode-universal-time (get-universal-time)))
121    
122     (defun decode-universal-time (universal-time &optional time-zone)
123 ram 1.3 "Converts a universal-time to decoded time format returning the following
124     nine values: second, minute, hour, date, month, year, day of week (0 =
125     Monday), T (daylight savings time) or NIL (standard time), and timezone.
126     Completely ignores daylight-savings-time when time-zone is supplied."
127 ram 1.1 (declare (type (or fixnum null) time-zone))
128     (multiple-value-bind (weeks secs)
129     (truncate (+ universal-time seconds-offset)
130     seconds-in-week)
131     (let ((weeks (+ weeks weeks-offset))
132     (second NIL)
133     (minute NIL)
134     (hour NIL)
135     (date NIL)
136     (month NIL)
137     (year NIL)
138     (day NIL)
139     (daylight NIL)
140     (timezone (if (null time-zone)
141     (multiple-value-bind (res s us tz)
142 wlott 1.9 (unix:unix-gettimeofday)
143 ram 1.1 (declare (ignore s us))
144     (if res tz 0))
145     (* time-zone 60))))
146     (declare (fixnum timezone))
147     (multiple-value-bind (t1 seconds) (truncate secs 60)
148     (setq second seconds)
149     (setq t1 (- t1 timezone))
150     (let* ((tday (if (< t1 0)
151     (1- (truncate (1+ t1) minutes-per-day))
152     (truncate t1 minutes-per-day))))
153     (multiple-value-setq (hour minute)
154     (truncate (- t1 (* tday minutes-per-day)) 60))
155     (let* ((t2 (1- (* (+ (* weeks 7) tday november-17-1858) 4)))
156     (tcent (truncate t2 quarter-days-per-century)))
157     (setq t2 (mod t2 quarter-days-per-century))
158     (setq t2 (+ (- t2 (mod t2 4)) 3))
159     (setq year (+ (* tcent 100) (truncate t2 quarter-days-per-year)))
160     (let ((days-since-mar0 (1+ (truncate (mod t2 quarter-days-per-year)
161     4))))
162     (setq day (mod (+ tday weekday-november-17-1858) 7))
163 ram 1.3 (unless time-zone
164     (if (setq daylight (dst-check days-since-mar0 hour day))
165     (cond ((eq hour 23)
166     (setq hour 0)
167     (setq day (mod (1+ day) 7))
168     (setq days-since-mar0 (1+ days-since-mar0))
169     (if (>= days-since-mar0 366)
170     (if (or (> days-since-mar0 366)
171     (not-leap-year (1+ year)))
172     (setq days-since-mar0 368))))
173     (T (setq hour (1+ hour))))))
174 ram 1.1 (let ((t3 (+ (* days-since-mar0 5) 456)))
175     (cond ((>= t3 1989)
176     (setq t3 (- t3 1836))
177     (setq year (1+ year))))
178     (multiple-value-setq (month t3) (truncate t3 153))
179     (setq date (1+ (truncate t3 5))))))))
180     (values second minute hour date month year day
181     daylight (truncate timezone 60)))))
182    
183     ;;; Encode-Universal-Time -- Public
184     ;;;
185     ;;; Just do a TimeUser:T_UserToInt. If the year is between 0 and 99 we
186     ;;; have to figure out which the "obvious" year is.
187     ;;;
188    
189     (defun encode-universal-time (second minute hour date month year
190     &optional time-zone)
191     "The time values specified in decoded format are converted to
192     universal time, which is returned."
193     (let* ((year (if (< year 100)
194     (multiple-value-bind (sec min hour day month now-year)
195     (get-decoded-time)
196     (declare (ignore sec min hour day month))
197     (do ((y (+ year (* 100 (1- (truncate now-year 100))))
198     (+ y 100)))
199     ((<= (abs (- y now-year)) 50) y)))
200     year))
201     (zone (if time-zone (* time-zone 60)
202 wlott 1.9 (multiple-value-bind (res s us tz) (unix:unix-gettimeofday)
203 ram 1.1 (declare (ignore s us))
204     (if res tz))))
205     (tmonth (- month 3)))
206     (cond ((< tmonth 0)
207     (setq tmonth (+ tmonth 12))
208     (setq year (1- year))))
209     (let ((days-since-mar0 (+ (truncate (+ (* tmonth 153) 2) 5) date)))
210     (multiple-value-bind (tcent tyear) (truncate year 100)
211     (let* ((tday (- (+ (truncate (* tcent quarter-days-per-century) 4)
212     (truncate (* tyear quarter-days-per-year) 4)
213     days-since-mar0)
214     november-17-1858))
215     (daylight (dst-check days-since-mar0 (1- hour)
216     (mod (+ tday weekday-november-17-1858) 7)))
217     (tminutes (+ (* hour 60) minute zone)))
218     (if daylight (setq tminutes (- tminutes 60)))
219     (do ((i tminutes (+ i minutes-per-day)))
220     ((>= i 0) (setq tminutes i))
221     (declare (fixnum i))
222     (decf tday 1))
223     (do ((i tminutes (- i minutes-per-day)))
224     ((< i minutes-per-day) (setq tminutes i))
225     (declare (fixnum i))
226     (incf tday 1))
227     (multiple-value-bind (weeks dpart) (truncate tday 7)
228     (make-universal-time weeks (* (+ (* (+ (* dpart minutes-per-day)
229     tminutes) 60)
230     second) 1000))))))))
231    
232     ;;; Dst-check -- Internal
233     (defconstant april-1 (+ (truncate (+ (* (- 4 3) 153) 2) 5) 1))
234     (defconstant october-31 (+ (truncate (+ (* (- 10 3) 153) 2) 5) 31))
235    
236     (eval-when (compile eval)
237    
238     (defmacro dst-check-start-of-month-ge (day hour weekday daybound)
239     (let ((d (gensym))
240     (h (gensym))
241     (w (gensym))
242     (db (gensym)))
243     `(let ((,d ,day)
244     (,h ,hour)
245     (,w ,weekday)
246     (,db ,daybound))
247     (declare (fixnum ,d ,h ,w ,db))
248     (cond ((< ,d ,db) NIL)
249     ((> (the fixnum (- ,d ,w)) ,db) T)
250     ((and (eq ,w 6) (> ,h 0)) T)
251     (T NIL)))))
252    
253     (defmacro dst-check-end-of-month-ge (day hour weekday daybound)
254     (let ((d (gensym))
255     (h (gensym))
256     (w (gensym))
257     (db (gensym)))
258     `(let ((,d ,day)
259     (,h ,hour)
260     (,w ,weekday)
261     (,db ,daybound))
262     (declare (fixnum ,d ,h ,w ,db))
263     (cond ((< (the fixnum (+ ,d 6)) ,db) NIL)
264     ((> (the fixnum (- (the fixnum (+ ,d 6)) ,w)) ,db) T)
265     ((and (eq ,w 6) (> ,h 0)) T)
266     (T NIL)))))
267     )
268    
269     (defun dst-check (day hour weekday)
270     (and (dst-check-start-of-month-ge day hour weekday april-1)
271     (not (dst-check-end-of-month-ge day hour weekday october-31))))
272    
273 ram 1.8 ;;;; Time:
274    
275 ram 1.1 (defmacro time (form)
276     "Evaluates the Form and prints timing information on *Trace-Output*."
277 ram 1.2 `(%time #'(lambda () ,form)))
278 ram 1.1
279 ram 1.7 ;;; MASSAGE-TIME-FUNCTION -- Internal
280     ;;;
281     ;;; Try to compile the closure arg to %TIME if it is interpreted.
282     ;;;
283     (defun massage-time-function (fun)
284     (cond
285     ((eval:interpreted-function-p fun)
286     (multiple-value-bind (def env-p)
287     (function-lambda-expression fun)
288     (declare (ignore def))
289     (cond
290     (env-p
291     (warn "TIME form in a non-null environment, forced to interpret.~@
292     Compiling entire form will produce more accurate times.")
293     fun)
294     (t
295     (compile nil fun)))))
296     (t fun)))
297    
298 ram 1.8 ;;; TIME-GET-SYS-INFO -- Internal
299     ;;;
300     ;;; Return all the files that we want time to report.
301     ;;;
302     (defun time-get-sys-info ()
303     (multiple-value-bind (user sys faults)
304     (system:get-system-info)
305     (values user sys faults (get-bytes-consed))))
306    
307     ;;; %TIME -- Internal
308     ;;;
309     ;;; The guts of the TIME macro. Compute overheads, run the (compiled)
310     ;;; function, report the times.
311     ;;;
312 ram 1.2 (defun %time (fun)
313 ram 1.7 (let ((fun (massage-time-function fun))
314     old-run-utime
315 ram 1.8 new-run-utime
316     old-run-stime
317     new-run-stime
318     old-real-time
319     new-real-time
320     old-page-faults
321     new-page-faults
322     real-time-overhead
323     run-utime-overhead
324     run-stime-overhead
325     page-faults-overhead
326     old-bytes-consed
327     new-bytes-consed
328     cons-overhead)
329 ram 1.2 ;; Calculate the overhead...
330 ram 1.8 (multiple-value-setq
331     (old-run-utime old-run-stime old-page-faults old-bytes-consed)
332     (time-get-sys-info))
333 ram 1.2 ;; Do it a second time to make sure everything is faulted in.
334 ram 1.8 (multiple-value-setq
335     (old-run-utime old-run-stime old-page-faults old-bytes-consed)
336     (time-get-sys-info))
337     (multiple-value-setq
338     (new-run-utime new-run-stime new-page-faults new-bytes-consed)
339     (time-get-sys-info))
340 ram 1.2 (setq run-utime-overhead (- new-run-utime old-run-utime))
341     (setq run-stime-overhead (- new-run-stime old-run-stime))
342     (setq page-faults-overhead (- new-page-faults old-page-faults))
343     (setq old-real-time (get-internal-real-time))
344     (setq old-real-time (get-internal-real-time))
345     (setq new-real-time (get-internal-real-time))
346     (setq real-time-overhead (- new-real-time old-real-time))
347     (setq cons-overhead (- new-bytes-consed old-bytes-consed))
348     ;; Now get the initial times.
349 ram 1.8 (multiple-value-setq
350     (old-run-utime old-run-stime old-page-faults old-bytes-consed)
351     (time-get-sys-info))
352 ram 1.2 (setq old-real-time (get-internal-real-time))
353     (multiple-value-prog1
354 ram 1.8 ;; Execute the form and return its values.
355     (funcall fun)
356     (multiple-value-setq
357     (new-run-utime new-run-stime new-page-faults new-bytes-consed)
358     (time-get-sys-info))
359 ram 1.2 (setq new-real-time (- (get-internal-real-time) real-time-overhead))
360     (format *trace-output*
361 ram 1.8 "~&Evaluation took:~% ~
362     ~S second~:P of real time~% ~
363     ~S second~:P of user run time~% ~
364     ~S second~:P of system run time~% ~
365     ~S page fault~:P and~% ~
366     ~S bytes consed.~%"
367     (max (/ (- new-real-time old-real-time)
368     (float internal-time-units-per-second))
369     0.0)
370     (max (/ (- new-run-utime old-run-utime) 1000000.0) 0.0)
371     (max (/ (- new-run-stime old-run-stime) 1000000.0) 0.0)
372     (max (- new-page-faults old-page-faults) 0)
373     (max (- new-bytes-consed old-bytes-consed) 0)))))

  ViewVC Help
Powered by ViewVC 1.1.5