public inbox for java-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Patch: FYI: System.nanoTime
@ 2006-03-09 18:43 Tom Tromey
  0 siblings, 0 replies; 9+ messages in thread
From: Tom Tromey @ 2006-03-09 18:43 UTC (permalink / raw)
  To: Java Patch List

I'm checking this in on the trunk.

This implements the new System.nanoTime API.

I haven't tested the Windows implementation, but it looks correct.
It isn't very good, however.

Tom

2006-03-09  Tom Tromey  <tromey@redhat.com>

	* win32.cc (_Jv_platform_nanotime): New function.
	* include/win32.h (_Jv_platform_nanotime): Declare.
	* posix.cc (_Jv_platform_nanotime): New function.
	* include/posix.h (_Jv_platform_nanotime): Declare.
	* java/lang/natSystem.cc (nanoTime): New method.
	* java/lang/System.java (nanoTime): Declare.
	* include/config.h.in, configure: Rebuilt.
	* configure.ac: Check for clock_gettime.

Index: configure.ac
===================================================================
--- configure.ac	(revision 111843)
+++ configure.ac	(working copy)
@@ -1015,6 +1015,14 @@
 	    AC_DEFINE(HAVE_SCHED_YIELD)
 	    THREADLIBS="$THREADLIBS -lposix4"
 	    THREADSPEC="$THREADSPEC -lposix4"])])])
+
+      AC_CHECK_LIB(rt, clock_gettime, [
+         AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [Define if you have clock_gettime()])
+	 case "$THREADSPEC" in
+	   *-lrt*) ;;
+	   *) THREADSPEC="$THREADSPEC -lrt" ;;
+	 esac])
+
       LIBS="$save_LIBS"
 
       # We can save a little space at runtime if the mutex has m_count
Index: java/lang/System.java
===================================================================
--- java/lang/System.java	(revision 111843)
+++ java/lang/System.java	(working copy)
@@ -1,5 +1,5 @@
 /* System.java -- useful methods to interface with the system
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
    Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -208,6 +208,15 @@
   public static native long currentTimeMillis();
 
   /**
+   * Get the current time, measured in nanoseconds.  The result is as
+   * precise as possible, and is measured against a fixed epoch.
+   * However, unlike currentTimeMillis(), the epoch chosen is
+   * arbitrary and may vary by platform, etc.
+   * @since 1.5
+   */
+  public static native long nanoTime();
+
+  /**
    * Copy one array onto another from <code>src[srcStart]</code> ...
    * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
    * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
Index: java/lang/natSystem.cc
===================================================================
--- java/lang/natSystem.cc	(revision 111843)
+++ java/lang/natSystem.cc	(working copy)
@@ -1,6 +1,6 @@
 // natSystem.cc - Native code implementing System class.
 
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2006 Free Software Foundation
 
    This file is part of libgcj.
 
@@ -124,6 +124,12 @@
   return _Jv_platform_gettimeofday ();
 }
 
+jlong
+java::lang::System::nanoTime ()
+{
+  return _Jv_platform_nanotime ();
+}
+
 jint
 java::lang::System::identityHashCode (jobject obj)
 {
Index: include/posix.h
===================================================================
--- include/posix.h	(revision 111843)
+++ include/posix.h	(working copy)
@@ -1,6 +1,6 @@
 // posix.h -- Helper functions for POSIX-flavored OSs.
 
-/* Copyright (C) 2000, 2002, 2003  Free Software Foundation
+/* Copyright (C) 2000, 2002, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -79,6 +79,7 @@
 
 extern int _Jv_select (int n, fd_set *, fd_set *, fd_set *, struct timeval *);
 extern jlong _Jv_platform_gettimeofday ();
+extern jlong _Jv_platform_nanotime ();
 extern void _Jv_platform_initialize (void);
 extern void _Jv_platform_initProperties (java::util::Properties*);
 
Index: include/win32.h
===================================================================
--- include/win32.h	(revision 111843)
+++ include/win32.h	(working copy)
@@ -1,6 +1,6 @@
 // win32.h -- Helper functions for Microsoft-flavored OSs.
 
-/* Copyright (C) 2002, 2003  Free Software Foundation
+/* Copyright (C) 2002, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -154,6 +154,7 @@
 extern void _Jv_platform_initialize (void);
 extern void _Jv_platform_initProperties (java::util::Properties*);
 extern jlong _Jv_platform_gettimeofday ();
+extern jlong _Jv_platform_nanotime ();
 extern int _Jv_pipe (int filedes[2]);
 
 extern void
Index: posix.cc
===================================================================
--- posix.cc	(revision 111843)
+++ posix.cc	(working copy)
@@ -1,6 +1,6 @@
 // posix.cc -- Helper functions for POSIX-flavored OSs.
 
-/* Copyright (C) 2000, 2001, 2002  Free Software Foundation
+/* Copyright (C) 2000, 2001, 2002, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -66,6 +66,22 @@
 #endif
 }
 
+jlong
+_Jv_platform_nanotime ()
+{
+#ifdef HAVE_CLOCK_GETTIME
+  struct timespec now;
+  if (clock_gettime (CLOCK_REALTIME, &now) == 0)
+    {
+      jlong result = (jlong) now.tv_sec;
+      result = result * 1000 * 1000 + now.tv_nsec;
+      return result;
+    }
+  // clock_gettime failed, but we can fall through.
+#endif // HAVE_CLOCK_GETTIME
+  return _Jv_platform_gettimeofday () * 1000LL;
+}
+
 // Platform-specific VM initialization.
 void
 _Jv_platform_initialize (void)
Index: win32.cc
===================================================================
--- win32.cc	(revision 111843)
+++ win32.cc	(working copy)
@@ -1,6 +1,6 @@
 // win32.cc - Helper functions for Microsoft-flavored OSs.
 
-/* Copyright (C) 2002, 2003  Free Software Foundation
+/* Copyright (C) 2002, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -279,6 +279,12 @@
   return t.time * 1000LL + t.millitm;
 }
 
+jlong
+_Jv_platform_nanotime ()
+{
+  return _Jv_platform_gettimeofday () * 1000LL;
+}
+
 // The following definitions "fake out" mingw to think that -mthreads
 // was enabled and that mingwthr.dll was linked. GCJ-compiled
 // applications don't need this helper library because we can safely

^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: Patch: FYI: System.nanoTime
@ 2006-03-10  8:22 Cédric Berger
  2006-03-10  8:31 ` David Daney
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Cédric Berger @ 2006-03-10  8:22 UTC (permalink / raw)
  To: tromey; +Cc: java-patches

Hi Tom,

+jlong
+_Jv_platform_nanotime ()
+{
+#ifdef HAVE_CLOCK_GETTIME
+  struct timespec now;
+  if (clock_gettime (CLOCK_REALTIME, &now) == 0)
+    {
+      jlong result = (jlong) now.tv_sec;
+      result = result * 1000 * 1000 + now.tv_nsec;
+      return result;
+    }
+  // clock_gettime failed, but we can fall through.
+#endif // HAVE_CLOCK_GETTIME
+  return _Jv_platform_gettimeofday () * 1000LL;
+}

Why do you use CLOCK_REALTIME? it represents the time
from the epoch, but nanoTime() javadoc atate that
nanoTime() value should be used for measuring elapsed
time and is unrelated to wall-clock time.

The problem is when you use CLOCK_REALTIME to measure
elapsed time and the user changes the time...
I had to write the same code for another project, and
I used the following snipped for Linux/BSD/Solaris:

   int		id;
   #ifdef CLOCK_MONOTONIC
	id = CLOCK_MONOTONIC;
   #else
   #ifdef CLOCK_HIGHRES
	id = CLOCK_HIGHRES;
   #else
   #error bad platform
   #endif
   #endif
	if (clock_gettime(id, &tv) - 0)

Cedric

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-03-30 17:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-09 18:43 Patch: FYI: System.nanoTime Tom Tromey
2006-03-10  8:22 Cédric Berger
2006-03-10  8:31 ` David Daney
2006-03-11  0:42 ` Tom Tromey
2006-03-28 19:19 ` Tom Tromey
2006-03-28 20:14   ` Cédric Berger
2006-03-29 15:22     ` Tom Tromey
2006-03-30  6:27       ` Cédric Berger
2006-03-30 17:04         ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).