X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2Fposix%2FGetTime.c;h=15643b8319e5420a967d395b82957c7f667f1e0c;hb=cf5905ea24904cf73a041fd7535e8723a668cb9a;hp=89d83a31cb35102a701f3519260daaa7fa4c990b;hpb=ab1e183aa7801f9260a9cbb6edbd553cf8249b25;p=ghc-hetmet.git diff --git a/rts/posix/GetTime.c b/rts/posix/GetTime.c index 89d83a3..15643b8 100644 --- a/rts/posix/GetTime.c +++ b/rts/posix/GetTime.c @@ -32,6 +32,10 @@ # include #endif +#ifdef USE_PAPI +# include +#endif + #if ! ((defined(HAVE_GETRUSAGE) && !irix_HOST_OS) || defined(HAVE_TIMES)) #error No implementation for getProcessCPUTime() available. #endif @@ -42,17 +46,39 @@ Ticks getProcessCPUTime(void) { - struct rusage t; - getrusage(RUSAGE_SELF, &t); - return (t.ru_utime.tv_sec * TICKS_PER_SECOND + - ((Ticks)t.ru_utime.tv_usec * TICKS_PER_SECOND)/1000000); +#if !defined(BE_CONSERVATIVE) && defined(HAVE_CLOCK_GETTIME) && defined (_SC_CPUTIME) && defined(CLOCK_PROCESS_CPUTIME_ID) && defined(HAVE_SYSCONF) + static int checked_sysconf = 0; + static int sysconf_result = 0; + + if (!checked_sysconf) { + sysconf_result = sysconf(_SC_CPUTIME); + checked_sysconf = 1; + } + if (sysconf_result != -1) { + struct timespec ts; + int res; + res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); + if (res == 0) { + return ((Ticks)ts.tv_sec * TICKS_PER_SECOND + + ((Ticks)ts.tv_nsec * TICKS_PER_SECOND) / 1000000000); + } + } +#endif + + // fallback to getrusage + { + struct rusage t; + getrusage(RUSAGE_SELF, &t); + return ((Ticks)t.ru_utime.tv_sec * TICKS_PER_SECOND + + ((Ticks)t.ru_utime.tv_usec * TICKS_PER_SECOND)/1000000); + } } Ticks getProcessElapsedTime(void) { struct timeval tv; gettimeofday(&tv, (struct timezone *) NULL); - return (tv.tv_sec * TICKS_PER_SECOND + + return ((Ticks)tv.tv_sec * TICKS_PER_SECOND + ((Ticks)tv.tv_usec * TICKS_PER_SECOND)/1000000); } @@ -68,9 +94,17 @@ void getProcessTimes(Ticks *user, Ticks *elapsed) Ticks getProcessCPUTime(void) { +#if !defined(THREADED_RTS) && USE_PAPI + long long usec; + if ((usec = PAPI_get_virt_usec()) < 0) { + barf("PAPI_get_virt_usec: %lld", usec); + } + return ((usec * TICKS_PER_SECOND) / 1000000); +#else Ticks user, elapsed; getProcessTimes(&user,&elapsed); return user; +#endif } Ticks getProcessElapsedTime(void) @@ -115,22 +149,42 @@ void getProcessTimes(Ticks *user, Ticks *elapsed) Ticks getThreadCPUTime(void) { -#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_THREAD_CPUTIME_ID) - // clock_gettime() gives us per-thread CPU time. It isn't - // reliable on Linux, but it's the best we have. - struct timespec ts; - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); - return (ts.tv_sec * TICKS_PER_SECOND + - ((Ticks)ts.tv_nsec * TICKS_PER_SECOND) / 1000000000); -#else - return getProcessCPUTime(); +#if USE_PAPI + long long usec; + if ((usec = PAPI_get_virt_usec()) < 0) { + barf("PAPI_get_virt_usec: %lld", usec); + } + return ((usec * TICKS_PER_SECOND) / 1000000); + +#elif !defined(BE_CONSERVATIVE) && defined(HAVE_CLOCK_GETTIME) && defined (_SC_THREAD_CPUTIME) && defined(CLOCK_THREAD_CPUTIME_ID) && defined(HAVE_SYSCONF) + { + static int checked_sysconf = 0; + static int sysconf_result = 0; + + if (!checked_sysconf) { + sysconf_result = sysconf(_SC_THREAD_CPUTIME); + checked_sysconf = 1; + } + if (sysconf_result != -1) { + // clock_gettime() gives us per-thread CPU time. It isn't + // reliable on Linux, but it's the best we have. + struct timespec ts; + int res; + res = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); + if (res == 0) { + return ((Ticks)ts.tv_sec * TICKS_PER_SECOND + + ((Ticks)ts.tv_nsec * TICKS_PER_SECOND) / 1000000000); + } + } + } #endif + return getProcessCPUTime(); } nat getPageFaults(void) { -#if !defined(HAVE_GETRUSAGE) || irix_HOST_OS +#if !defined(HAVE_GETRUSAGE) || irix_HOST_OS || haiku_HOST_OS return 0; #else struct rusage t;