|
From: <kon...@us...> - 2007-02-14 23:00:33
|
Revision: 3126
http://jnode.svn.sourceforge.net/jnode/?rev=3126&view=rev
Author: konkubinaten
Date: 2007-02-14 15:00:16 -0800 (Wed, 14 Feb 2007)
Log Message:
-----------
added getNanoTime to java.lang.System based on cpu cycles
Modified Paths:
--------------
trunk/core/src/classpath/vm/java/lang/VMSystem.java
trunk/core/src/core/org/jnode/vm/Unsafe.java
trunk/core/src/core/org/jnode/vm/VmSystem.java
trunk/core/src/native/x86/unsafe.asm
Added Paths:
-----------
trunk/core/src/test/org/jnode/test/NanoTimeTest.java
Modified: trunk/core/src/classpath/vm/java/lang/VMSystem.java
===================================================================
--- trunk/core/src/classpath/vm/java/lang/VMSystem.java 2007-02-14 19:59:46 UTC (rev 3125)
+++ trunk/core/src/classpath/vm/java/lang/VMSystem.java 2007-02-14 23:00:16 UTC (rev 3126)
@@ -195,8 +195,7 @@
* @since 1.5
*/
public static long nanoTime(){
- //TODO implement it
- throw new UnsupportedOperationException();
+ return VmSystem.nanoTime();
};
/**
Modified: trunk/core/src/core/org/jnode/vm/Unsafe.java
===================================================================
--- trunk/core/src/core/org/jnode/vm/Unsafe.java 2007-02-14 19:59:46 UTC (rev 3125)
+++ trunk/core/src/core/org/jnode/vm/Unsafe.java 2007-02-14 23:00:16 UTC (rev 3126)
@@ -742,4 +742,12 @@
VmProcessor.current().getArchitecture().getStackReader()
.debugStackTrace(max);
}
+
+ /**
+ * return a nanosecond accurate timer based on the clock cycles
+ * of the CPU
+ * @return nanosecond accurate time
+ */
+ public static native long getCpuCycles();
+
}
Modified: trunk/core/src/core/org/jnode/vm/VmSystem.java
===================================================================
--- trunk/core/src/core/org/jnode/vm/VmSystem.java 2007-02-14 19:59:46 UTC (rev 3125)
+++ trunk/core/src/core/org/jnode/vm/VmSystem.java 2007-02-14 23:00:16 UTC (rev 3126)
@@ -90,6 +90,8 @@
private static volatile long currentTimeMillis;
+ private static long mhz = -1;
+
private static long rtcIncrement;
private static RTCService rtcService;
@@ -700,6 +702,47 @@
}
/**
+ * <p>
+ * Returns the current value of a nanosecond-precise system timer.
+ * The value of the timer is an offset relative to some arbitrary fixed
+ * time, which may be in the future (making the value negative). This
+ * method is useful for timing events where nanosecond precision is
+ * required. This is achieved by calling this method before and after the
+ * event, and taking the difference betweent the two times:
+ * </p>
+ * <p>
+ * <code>long startTime = System.nanoTime();</code><br />
+ * <code>... <emph>event code</emph> ...</code><br />
+ * <code>long endTime = System.nanoTime();</code><br />
+ * <code>long duration = endTime - startTime;</code><br />
+ * </p>
+ * <p>
+ * Note that the value is only nanosecond-precise, and not accurate; there
+ * is no guarantee that the difference between two values is really a
+ * nanosecond. Also, the value is prone to overflow if the offset
+ * exceeds 2^63.
+ * </p>
+ *
+ * @return the time of a system timer in nanoseconds.
+ * @since 1.5
+ */
+ public static long nanoTime(){
+ if (mhz == -1) {
+ long start = Unsafe.getCpuCycles();
+ try {
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ // set some "random" value
+ mhz = 1000;
+ }
+ long end = Unsafe.getCpuCycles();
+ mhz = end - start;
+ mhz = mhz / 1000000;
+ }
+ return Unsafe.getCpuCycles() / mhz;
+ }
+
+ /**
* Returns the number of milliseconds since booting the kernel of JNode.
*
* This method does not call any other method and CAN be used in the
Modified: trunk/core/src/native/x86/unsafe.asm
===================================================================
--- trunk/core/src/native/x86/unsafe.asm 2007-02-14 19:59:46 UTC (rev 3125)
+++ trunk/core/src/native/x86/unsafe.asm 2007-02-14 23:00:16 UTC (rev 3126)
@@ -355,4 +355,9 @@
; static native int readKdbInput();
GLABEL Q43org5jnode2vm6Unsafe23readKdbInput2e2829I
jmp kdb_recv_char
-
\ No newline at end of file
+
+; public static native long getCpuCycles();
+GLABEL Q43org5jnode2vm6Unsafe23getCpuCycles2e2829J
+ rdtsc
+ ret
+
Added: trunk/core/src/test/org/jnode/test/NanoTimeTest.java
===================================================================
--- trunk/core/src/test/org/jnode/test/NanoTimeTest.java (rev 0)
+++ trunk/core/src/test/org/jnode/test/NanoTimeTest.java 2007-02-14 23:00:16 UTC (rev 3126)
@@ -0,0 +1,53 @@
+/*
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package org.jnode.test;
+
+
+/**
+ * @author Peter
+ */
+public class NanoTimeTest {
+
+ public static void main(String[] args) {
+ NanoTimeTest ntt = new NanoTimeTest();
+ ntt.run(500);
+ ntt.run(1000);
+ ntt.run(1500);
+ ntt.run(2000);
+ ntt.run(2500);
+ ntt.run(3000);
+ }
+
+ public void run(int ms) {
+ long start = System.nanoTime();
+ try {
+ Thread.sleep(ms);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ long end = System.nanoTime();
+ long nano = end - start;
+
+ System.out.println("The test ran " + ms + "ms and according to " +
+ "System.nanoTime that was " + nano + "ns");
+ long p = Math.abs(ms * 1000L - nano) / ms;
+ System.out.println("Aberration : " + p + " promill");
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|