1 | /* |
---|
2 | * Time.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2005 Peter Graves |
---|
5 | * $Id: Time.java 11714 2009-03-23 20:05:37Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import java.lang.reflect.Method; |
---|
37 | import java.util.Date; |
---|
38 | import java.util.TimeZone; |
---|
39 | |
---|
40 | public final class Time extends Lisp |
---|
41 | { |
---|
42 | private static final long getCurrentThreadUserTime() |
---|
43 | { |
---|
44 | try |
---|
45 | { |
---|
46 | Class c = Class.forName("org.armedbear.lisp.Native"); |
---|
47 | Method m = c.getMethod("getCurrentThreadUserTime", (Class[]) null); |
---|
48 | Object result = m.invoke((Object) null, (Object[]) null); |
---|
49 | if (result instanceof Long) |
---|
50 | return ((Long)result).longValue(); |
---|
51 | } |
---|
52 | catch (Throwable t) {} |
---|
53 | return -1; |
---|
54 | } |
---|
55 | |
---|
56 | private static final long getCurrentThreadSystemTime() |
---|
57 | { |
---|
58 | try |
---|
59 | { |
---|
60 | Class c = Class.forName("org.armedbear.lisp.Native"); |
---|
61 | Method m = c.getMethod("getCurrentThreadSystemTime", (Class[]) null); |
---|
62 | Object result = m.invoke((Object) null, (Object[]) null); |
---|
63 | if (result instanceof Long) |
---|
64 | return ((Long)result).longValue(); |
---|
65 | } |
---|
66 | catch (Throwable t) {} |
---|
67 | return -1; |
---|
68 | } |
---|
69 | |
---|
70 | // ### %time |
---|
71 | private static final Primitive _TIME = |
---|
72 | new Primitive("%time", PACKAGE_SYS, false) |
---|
73 | { |
---|
74 | @Override |
---|
75 | public LispObject execute(LispObject arg) throws ConditionThrowable |
---|
76 | { |
---|
77 | Cons.setCount(0); |
---|
78 | long userStart = -1; |
---|
79 | long systemStart = -1; |
---|
80 | try |
---|
81 | { |
---|
82 | userStart = getCurrentThreadUserTime(); |
---|
83 | systemStart = getCurrentThreadSystemTime(); |
---|
84 | } |
---|
85 | catch (Throwable t) {} |
---|
86 | long realStart = System.currentTimeMillis(); |
---|
87 | try |
---|
88 | { |
---|
89 | return arg.execute(); |
---|
90 | } |
---|
91 | finally |
---|
92 | { |
---|
93 | long realElapsed = System.currentTimeMillis() - realStart; |
---|
94 | final long userStop; |
---|
95 | final long systemStop; |
---|
96 | if (userStart > 0) |
---|
97 | { |
---|
98 | userStop = getCurrentThreadUserTime(); |
---|
99 | systemStop = getCurrentThreadSystemTime(); |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | userStop = -1; |
---|
104 | systemStop = -1; |
---|
105 | } |
---|
106 | long count = Cons.getCount(); |
---|
107 | Stream out = |
---|
108 | checkCharacterOutputStream(Symbol.TRACE_OUTPUT.symbolValue()); |
---|
109 | out.freshLine(); |
---|
110 | FastStringBuffer sb = new FastStringBuffer(); |
---|
111 | sb.append(String.valueOf((float)realElapsed / 1000)); |
---|
112 | sb.append(" seconds real time"); |
---|
113 | sb.append(System.getProperty("line.separator")); |
---|
114 | if (userStart > 0) |
---|
115 | { |
---|
116 | sb.append(String.valueOf((float)(userStop - userStart) / 100)); |
---|
117 | sb.append(" seconds user run time"); |
---|
118 | sb.append(System.getProperty("line.separator")); |
---|
119 | sb.append(String.valueOf((float)(systemStop - systemStart) / 100)); |
---|
120 | sb.append(" seconds system run time"); |
---|
121 | sb.append(System.getProperty("line.separator")); |
---|
122 | } |
---|
123 | sb.append(count); |
---|
124 | sb.append(" cons cell"); |
---|
125 | if (count != 1) |
---|
126 | sb.append('s'); |
---|
127 | sb.append(System.getProperty("line.separator")); |
---|
128 | out._writeString(sb.toString()); |
---|
129 | out._finishOutput(); |
---|
130 | } |
---|
131 | } |
---|
132 | }; |
---|
133 | |
---|
134 | // ### get-internal-real-time |
---|
135 | private static final Primitive GET_INTERNAL_REAL_TIME = |
---|
136 | new Primitive("get-internal-real-time", "") |
---|
137 | { |
---|
138 | @Override |
---|
139 | public LispObject execute() throws ConditionThrowable |
---|
140 | { |
---|
141 | return number(System.currentTimeMillis()); |
---|
142 | } |
---|
143 | }; |
---|
144 | |
---|
145 | // ### get-internal-run-time |
---|
146 | private static final Primitive GET_INTERNAL_RUN_TIME = |
---|
147 | new Primitive("get-internal-run-time", "") |
---|
148 | { |
---|
149 | @Override |
---|
150 | public LispObject execute() throws ConditionThrowable |
---|
151 | { |
---|
152 | if (Utilities.isPlatformUnix) |
---|
153 | { |
---|
154 | long userTime = -1; |
---|
155 | long systemTime = -1; |
---|
156 | try |
---|
157 | { |
---|
158 | userTime = getCurrentThreadUserTime(); |
---|
159 | systemTime = getCurrentThreadSystemTime(); |
---|
160 | } |
---|
161 | catch (Throwable t) {} |
---|
162 | if (userTime >= 0 && systemTime >= 0) |
---|
163 | return number((userTime + systemTime) * 10); |
---|
164 | } |
---|
165 | return number(System.currentTimeMillis()); |
---|
166 | } |
---|
167 | }; |
---|
168 | |
---|
169 | // ### get-universal-time |
---|
170 | private static final Primitive GET_UNIVERSAL_TIME = |
---|
171 | new Primitive("get-universal-time", "") |
---|
172 | { |
---|
173 | @Override |
---|
174 | public LispObject execute() |
---|
175 | { |
---|
176 | return number(System.currentTimeMillis() / 1000 + 2208988800L); |
---|
177 | } |
---|
178 | }; |
---|
179 | |
---|
180 | // ### default-time-zone => offset daylight-p |
---|
181 | private static final Primitive DEFAULT_TIME_ZONE = |
---|
182 | new Primitive("default-time-zone", PACKAGE_SYS, false) |
---|
183 | { |
---|
184 | @Override |
---|
185 | public LispObject execute() throws ConditionThrowable |
---|
186 | { |
---|
187 | TimeZone tz = TimeZone.getDefault(); |
---|
188 | //int offset = tz.getOffset(System.currentTimeMillis()); |
---|
189 | // Classpath hasn't implemented TimeZone.getOffset(long). |
---|
190 | int rawOffset = tz.getRawOffset(); |
---|
191 | final boolean inDaylightTime = |
---|
192 | tz.inDaylightTime(new Date(System.currentTimeMillis())); |
---|
193 | if (inDaylightTime) |
---|
194 | rawOffset += tz.getDSTSavings(); |
---|
195 | // "Time zone values increase with motion to the west..." |
---|
196 | // Convert milliseconds to hours. |
---|
197 | return LispThread.currentThread().setValues( |
---|
198 | Fixnum.getInstance(- rawOffset).divideBy(Fixnum.getInstance(3600000)), |
---|
199 | inDaylightTime ? T : NIL); |
---|
200 | } |
---|
201 | }; |
---|
202 | } |
---|