public inbox for java-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* 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

* Re: Patch: FYI: System.nanoTime
  2006-03-10  8:22 Patch: FYI: System.nanoTime Cédric Berger
@ 2006-03-10  8:31 ` David Daney
  2006-03-11  0:42 ` Tom Tromey
  2006-03-28 19:19 ` Tom Tromey
  2 siblings, 0 replies; 9+ messages in thread
From: David Daney @ 2006-03-10  8:31 UTC (permalink / raw)
  To: Cédric Berger; +Cc: tromey, java-patches

Cédric Berger wrote:
> 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)

I think this is an excellent idea.

I cannot begin to tell you how much a pain in the a** it is to manage 
multiple time outs on a single thread while at the same time trying to 
maintain a 'wall clock' the can be changed.

David Daney

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

* Re: Patch: FYI: System.nanoTime
  2006-03-10  8:22 Patch: FYI: System.nanoTime Cédric Berger
  2006-03-10  8:31 ` David Daney
@ 2006-03-11  0:42 ` Tom Tromey
  2006-03-28 19:19 ` Tom Tromey
  2 siblings, 0 replies; 9+ messages in thread
From: Tom Tromey @ 2006-03-11  0:42 UTC (permalink / raw)
  To: Cédric Berger; +Cc: java-patches

>>>>> "Cédric" == Cédric Berger <cedric@berger.to> writes:

Cédric> +  if (clock_gettime (CLOCK_REALTIME, &now) == 0)

Cédric> Why do you use CLOCK_REALTIME?

Just ignorance I'm afraid.

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

Thanks.  I will change it to something like this next week.

Tom

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

* Re: Patch: FYI: System.nanoTime
  2006-03-10  8:22 Patch: FYI: System.nanoTime 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
  2 siblings, 1 reply; 9+ messages in thread
From: Tom Tromey @ 2006-03-28 19:19 UTC (permalink / raw)
  To: Cédric Berger; +Cc: java-patches

>>>>> "Cédric" == Cédric Berger <cedric@berger.to> writes:

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

What do you think of the appended?

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* posix.cc (_Jv_platform_nanotime): Look for CLOCK_MONOTONIC and
	CLOCK_HIGHRES.

Index: posix.cc
===================================================================
--- posix.cc	(revision 112466)
+++ posix.cc	(working copy)
@@ -71,7 +71,15 @@
 {
 #ifdef HAVE_CLOCK_GETTIME
   struct timespec now;
-  if (clock_gettime (CLOCK_REALTIME, &now) == 0)
+  int id;
+#ifdef CLOCK_MONOTONIC
+  id = CLOCK_MONOTONIC;
+#elif defined (CLOCK_HIGHRES)
+  id = CLOCK_HIGHRES;
+#else
+  id = CLOCK_REALTIME;
+#endif
+  if (clock_gettime (id, &now) == 0)
     {
       jlong result = (jlong) now.tv_sec;
       result = result * 1000 * 1000 + now.tv_nsec;

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

* Re: Patch: FYI: System.nanoTime
  2006-03-28 19:19 ` Tom Tromey
@ 2006-03-28 20:14   ` Cédric Berger
  2006-03-29 15:22     ` Tom Tromey
  0 siblings, 1 reply; 9+ messages in thread
From: Cédric Berger @ 2006-03-28 20:14 UTC (permalink / raw)
  To: tromey; +Cc: java-patches

Tom Tromey wrote:
>>>>>> "Cédric" == Cédric Berger <cedric@berger.to> writes:
> 
> Cédric> Why do you use CLOCK_REALTIME? it represents the time
> Cédric> from the epoch, but nanoTime() javadoc atate that
> Cédric> nanoTime() value should be used for measuring elapsed
> Cédric> time and is unrelated to wall-clock time.
> 
> What do you think of the appended?

I've not tested it, but by reading it, it looks perfect to me.

Thanks,
Cedric

> 
> Tom
> 
> Index: ChangeLog
> from  Tom Tromey  <tromey@redhat.com>
> 
> 	* posix.cc (_Jv_platform_nanotime): Look for CLOCK_MONOTONIC and
> 	CLOCK_HIGHRES.
> 
> Index: posix.cc
> ===================================================================
> --- posix.cc	(revision 112466)
> +++ posix.cc	(working copy)
> @@ -71,7 +71,15 @@
>  {
>  #ifdef HAVE_CLOCK_GETTIME
>    struct timespec now;
> -  if (clock_gettime (CLOCK_REALTIME, &now) == 0)
> +  int id;
> +#ifdef CLOCK_MONOTONIC
> +  id = CLOCK_MONOTONIC;
> +#elif defined (CLOCK_HIGHRES)
> +  id = CLOCK_HIGHRES;
> +#else
> +  id = CLOCK_REALTIME;
> +#endif
> +  if (clock_gettime (id, &now) == 0)
>      {
>        jlong result = (jlong) now.tv_sec;
>        result = result * 1000 * 1000 + now.tv_nsec;

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

* Re: Patch: FYI: System.nanoTime
  2006-03-28 20:14   ` Cédric Berger
@ 2006-03-29 15:22     ` Tom Tromey
  2006-03-30  6:27       ` Cédric Berger
  0 siblings, 1 reply; 9+ messages in thread
From: Tom Tromey @ 2006-03-29 15:22 UTC (permalink / raw)
  To: Cédric Berger; +Cc: java-patches

>>>>> "Cédric" == Cédric Berger <cedric@berger.to> writes:

Cédric> I've not tested it, but by reading it, it looks perfect to me.

I'm checking it in.

Tom

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

* Re: Patch: FYI: System.nanoTime
  2006-03-29 15:22     ` Tom Tromey
@ 2006-03-30  6:27       ` Cédric Berger
  2006-03-30 17:04         ` Tom Tromey
  0 siblings, 1 reply; 9+ messages in thread
From: Cédric Berger @ 2006-03-30  6:27 UTC (permalink / raw)
  To: tromey; +Cc: java-patches

Tom Tromey wrote:
>>>>>> "Cédric" == Cédric Berger <cedric@berger.to> writes:
> 
> Cédric> I've not tested it, but by reading it, it looks perfect to me.
> 
> I'm checking it in.

Cool!

It should work fine on Solaris/*BSD/Linux.

Actually, thinking about it, the only problem could arise if one
platform for example defines CLOCK_MONOTONIC but does not implements
it in the clock_gettime() call (and returns -1)

If such a platform exists, then I guess a run-time fallback mechanism
would need to be added.

Cedric

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

* Re: Patch: FYI: System.nanoTime
  2006-03-30  6:27       ` Cédric Berger
@ 2006-03-30 17:04         ` Tom Tromey
  0 siblings, 0 replies; 9+ messages in thread
From: Tom Tromey @ 2006-03-30 17:04 UTC (permalink / raw)
  To: Cédric Berger; +Cc: java-patches

>>>>> "Cédric" == Cédric Berger <cedric@berger.to> writes:

Cédric> Actually, thinking about it, the only problem could arise if one
Cédric> platform for example defines CLOCK_MONOTONIC but does not implements
Cédric> it in the clock_gettime() call (and returns -1)

Cédric> If such a platform exists, then I guess a run-time fallback mechanism
Cédric> would need to be added.

The way the code is written, if clock_gettime fails, we fall back to
multiplying _Jv_platform_gettimeofday by 1000.  So, we're covered :-)

Tom

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

* 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

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-10  8:22 Patch: FYI: System.nanoTime 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
  -- strict thread matches above, loose matches on Subject: below --
2006-03-09 18:43 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).