2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \subsection[getCPUTime.lc]{getCPUTime Runtime Support}
9 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
12 /* how is this to work given we have not read platform.h yet? */
14 #define _INCLUDE_HPUX_SOURCE
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
27 #ifdef HAVE_SYS_TIMES_H
28 #include <sys/times.h>
31 #ifdef HAVE_SYS_TIME_H
35 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
36 #include <sys/resource.h>
39 #ifdef HAVE_SYS_TIMEB_H
40 #include <sys/timeb.h>
44 #include <sys/syscall.h>
45 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
46 #define HAVE_GETRUSAGE
62 * Our caller wants a pointer to four StgInts,
63 * user seconds, user nanoseconds, system seconds, system nanoseconds.
64 * Yes, the timerval has unsigned components, but nanoseconds take only
65 * 30 bits, and our CPU usage would have to be over 68 years for the
66 * seconds to overflow 31 bits.
70 getCPUTime(StgByteArray cpuStruct)
72 StgInt *cpu=(StgInt *)cpuStruct;
74 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
77 getrusage(RUSAGE_SELF, &t);
78 cpu[0] = t.ru_utime.tv_sec;
79 cpu[1] = 1000 * t.ru_utime.tv_usec;
80 cpu[2] = t.ru_stime.tv_sec;
81 cpu[3] = 1000 * t.ru_stime.tv_usec;
84 # if defined(HAVE_TIMES)
87 # define ticks CLK_TCK
90 ticks = sysconf(_SC_CLK_TCK);
94 cpu[0] = t.tms_utime / ticks;
95 cpu[1] = (t.tms_utime - cpu[0] * ticks) * (1000000000 / ticks);
96 cpu[2] = t.tms_stime / ticks;
97 cpu[3] = (t.tms_stime - cpu[2] * ticks) * (1000000000 / ticks);
103 return (StgByteArray) cpuStruct;