9 Replies Latest reply: Jul 20, 2007 3:13 AM by 843810 RSS

    getTime  function


      This is fairly high level question about the use of the above method, and indeed any timers in jvmti. Generally speaking the timers return time in nanoseconds set in jlong, which are signed. This means that the time returned by this method
      "wraps" every 2.147483647 secs. This is because jlong can only store numbers in the range -2,147,483,648 to +2,147,483,647.

      So lets say you want to do something basic like determine the amount time a program takes to execute. You get the time and vm start and vm death, but this time a time at thread end: but this time may have wrapped an indeterminable amount of times and no matter how long the program took, the function will never return more than 2.1 seconds.

      What use is such a function?

        • 1. Re: getTime  function
          Size of jlong is 64bit not 32bit. So it would take more than 200years to wrap.
          • 2. Re: getTime  function

            thanks for replying. Yes, its 64 bits, so the difference between its min and max values is 4.5 billion or so, but these represents nanoseconds not actual seconds, so 4.5 billions nanoseconds is 4.5 secs.

            • 3. Re: getTime  function
              Hi Brian,

              Please, correct me if I'm wrong:
              (a) 1 sec is 10^9 nano-secs
              (b) 10^9 < 2^30
              (c) So that: 2^63 / 2^30 = 2^ (63-30) = 2^33

              The estimate is that the jlong contains about 2^33 seconds which is a big number, not 4.5 secs.

              • 4. Re: getTime  function
                In simpler terms: 2^63 is 9223372036.854775808 seconds.
                Then maybe my basic knowledge of C is being exposed here.
                Lets say the above number is set by getThreadCputTime into a jlong.
                How can i write that very same value to a file? i.e. without being truncated or values lost by the signed bit.?

                thanks again,

                • 5. Re: getTime  function
                  Let me be more specific.
                  The API doc for the getTime function is unclear and even contradictory.
                  A description of what the function returns is:

                  The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative).

                  Then a few lines later is says:

                  On return, points to the time in nanoseconds. This is an unsigned value. If tested or printed as a jlong (signed value) it may appear to be a negative number.

                  The parameter type is a jlong which is a Signed 64 bits.

                  The documentation says the time returned is unsigned, but it may be negative and it is returned is an signed jlong. Which limits the contents of the value to 2^31 which is 2.14 second or so.

                  Does anyone have any information that would suggest that this is not the case,


                  • 6. Re: getTime  function
                    Hi Brian,

                    I don't understand your sentence:
                    "Which limits the contents of the value to 2^31 which is 2.14 second or so."

                    On 32-bit machines the type jlong is "long long" or "singed long long"
                    which is 64-bit, but not "signed" or "signed long" which is 32-bit.
                    On 64-bit machines the type jlong is "long" which is again 64-bit value.

                    Let's consider the following simple C program:
                    #include <limits.h>
                    #include <stdio.h>
                    #include <jni.h>

                    void main() {
                    printf("LLONG_MAX: %#llx; %lld\n", LLONG_MAX, LLONG_MAX);
                    printf("sizeof(LLONG_MAX): %d\n", sizeof(LLONG_MAX));
                    printf("sizeof(jlong): %d\n", sizeof(jlong));

                    The program above prints this:
                    LLONG_MAX: 0x7fffffffffffffff; 9223372036854775807
                    sizeof(LLONG_MAX): 8
                    sizeof(jlong): 8

                    I agree, that the getTime doc is a little bit confusing.
                    But, as I read, it is saying that the function returns jlong value (signed 64-bit)
                    which can be negative. It is because the returned time is relative to some fixed arbitrary time which can be in the future.
                    However, this value can be interpreted as an unsigned 64-bit value.
                    To do so, just cast it to the "unsigned jlong" or unsigned long long".
                    In such a case the max value is ULLONG_MAX which is 0xffffffffffffffff
                    or 18446744073709551615LL.

                    • 7. Re: getTime  function

                      Thanks for taking the time to reply.
                      I copied your code into my program and the output was:
                      [java] LLONG_MAX: 0xffffffff; 2147483647
                      [java] sizeof(LLONG_MAX): 8
                      [java] sizeof(jlong): 8

                      This would explain why no matter what code is write, all timer values wrap after 2.1 secs.
                      Is there some way to increase the the jlong max value?

                      My system is XP.
                      • 8. Re: getTime  function
                        The problem is the MS VC++ printf incorrectly treats formats %llx and %lld .
                        I could refer to the discussion:

                        Please, add the following lines to your sample program:
                        long long ll = LLONG_MAX;
                        int addr = (int ) &ll;
                        printf("LLONG_MAX: %#lx %#lx\n", *(addr + 1), *addr);

                        It should print this:
                        LLONG_MAX: 0xffffffff 0xffffffff

                        The function GetTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr)
                        needs to be used to get the time max value.
                        On return the info_ptr->max_value should have the maximum value
                        the timer can reach. It can be negative though as the spec says. :)
                        It means that it is better to cast this value to the "unsigned long long" type.


                        • 9. Re: getTime  function
                          I'm sorry. Above I've made a wrong copy/paste.

                          Of course, it should print this:
                          LLONG_MAX: 0x7fffffff 0xffffffff
                          Not this :
                          LLONG_MAX: 0xffffffff 0xffffffff