8 Replies Latest reply on Jan 18, 2013 10:45 PM by Steve.Clamage-Oracle

    About gethrtime() function


      First of all, I apologize if this is not the adequate forum for this question. I'm trying to use the gethrtime() in order to compute proccesing times. I'm using Solaris Studio 12.3 in a LINUX box. The problem is that I obtain ans error (undefined definition of the function and linker error). So my question is:

      Is the gethrtime() a specific function of Solaris OS or comes with Solaris compilers. As I obtain an undefined definition error I suppose that it not belongs with the compiler (I have included sys/time.h)

      I'm trying to use too the clock_gettime() function, but I obtain errors. This function should be declared in unistd.h header, but apparently does not exists in solaris Studio conpiler. Am I right?


      Edited by: 801533 on 18-ene-2013 6:41
        • 1. Re: About gethrtime() function
          This is a suitable forum. :-)

          Function gethrtime is a Solaris function to get high-resolution time.
          Linux has function clock_gettime for a similar purpose. See the man page.

          If you need to run on both Solaris and Linux, you can tell whether you are compiling on Solaris by using
          #ifdef __SunOS
          • 2. Re: About gethrtime() function
            Thank you. It runs. In my first try I was used -xc99=all where clock_gettime() is not present

            And about the detection of Solaris, __SunOS variable is only defined in Solaris Studio but not in clang/LLVM nor GNU compilers, in which are defines __sun and __sun__. Can use clang/LLVM and gcc gethrtime()

            And what about the difference between gethrtime() and gthrvtime()? How should I use in order to get time for performance computations?

            • 3. Re: About gethrtime() function
              More questions about

              Sorry, this post appears because I marked the original question as resolved but then I unmarked it again in order to ask a bit more in the previous post

              Edited by: 801533 on 18-ene-2013 7:47
              • 4. Re: About gethrtime() function
                On Linux. clock_gettime is declared in <time.h>, not in <sys/time.h>.
                On Solaris, you can include <time.h> to get <sys/time.h>, along with other declarations you will probably need.

                You asked about Studio compilers. On Solaris, Studio compilers predefine the macro __SunOS.
                You could also check for __sun, which is not predefined on Linux. For other compilers, you will have to verify that __sun is predefined as a macro when running on Solaris.

                For details of library functions, consult the man pages.

                On Solaris,
                % man gethrtime
                shows both gethrtime and gethrvtime. The difference is that gethrtime shows clock time, and gethrvtime shows the virtual execution time used by the current thread.

                On Linux, clock_gettime has a parameter that describes the kind of clock you want. See the man page.

                These are functions provided by the operating system. It does not matter which compiler you use. You do need to include the appropriate headers to get function declarations, and declarations of the types and macros that the functions use.

                If you are using multiple compilers and multiple operating systems, I recommend that you isolate these variations in one file that defines a single programming interface (API). For example, create a data structure my_time that holds the data you want in a suitable way, and function my_gettime that fills in the data. Put declarations for my_time and my_gettime in mytime.h, and hide all the details of obtaining the time in mytime.c.
                1 person found this helpful
                • 5. Re: About gethrtime() function
                  Thank you for your answer and your recommendations.

                  The timers in clock_gettime are (http://linux.die.net/man/3/clock_gettime):

                  System-wide realtime clock. Setting this clock requires appropriate privileges.
                  Clock that cannot be set and represents monotonic time since some unspecified starting point.
                  High-resolution per-process timer from the CPU.
                  Thread-specific CPU-time clock.

                  So, can I understand thar gethrtime() is the equivalent to clock_gettime() plus CLOCK_PROCESS_CPUTIME_ID and gethrvtime() is clock_gettime() plus CLOCK_THREAD_CPUTIME_ID?

                  And about the gethrvtime() function, I've found (I'm not access to Solaris by the moment) two man pages: http://www.unix.com/man-page/OpenSolaris/3c/gethrtime/ and http://docs.oracle.com/cd/E19455-01/806-0627/6j9vhfmt6/index.html The first one corresponds to Solaris 5.11 and the second one to Solaris 5.8. In the second one says that gethrvtime() should be used along the ptime utility? But, what is ptime? In the man page for Solarin 5.11 does not says anything about ptime. What should I do?

                  And about the difference between gethrtime and gethrvtime I'm a bit confused. For example, imagine I want to compute the execution time for DGEMM from a threaded BLAS executed by 4 threads. If I put:

                  t = gethrtime()-t1;

                  Will I get the correct result?
                  Or should I do:

                  t = gethrvtime()-t1;

                  • 6. Re: About gethrtime() function
                    Solaris follows the Unix Standard, as revised from time to time. Over time standards are modified, but generally in a forward-compatible way, meaning that old source code and old binaries continue to work. See, for example,
                    % man -s 5 standards

                    Solaris 8 (SunOS 5.8) dates from the late 1990's and is End Of Life. Solaris 11 (SunOS 5.11) is current, and follows the most recent Unix standard. The details of gethrtime and gethrvtime might have changed in between.

                    The time function to use depends on what you want to measure.
                    If you want to measure elapsed real time (like using a stopwatch), use gethrtime.
                    If you want to measure the CPU time taken by the thread, use gethrvtime.
                    These are the high-precision versions of other timing functions that generally have millisecond precision.

                    For ptime, see the man page.
                    1 person found this helpful
                    • 7. Re: About gethrtime() function
                      In the Solaris OpenMP implementation, in the function omp_get_wtime(), is used gethrtime() or gethrvtime()? Or is implemented in other way?

                      • 8. Re: About gethrtime() function
                        In the Oracle implementation of OpenMP, omp_get_wtime() uses
                        gethrtime() on Solaris, and
                        clock_gettime(CLOCK_REALTIME) on Linux.

                        If you check omp_get_wtime() at openmp.org, the definition of the function says it gives elapsed wall-clock time.

                        Edited by: Steve_Clamage on Jan 18, 2013 2:44 PM
                        1 person found this helpful