2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \subsection[getCPUTime.lc]{getCPUTime Runtime Support}
7 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
9 /* how is this to work given we have not read platform.h yet? */
11 #define _INCLUDE_HPUX_SOURCE
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
25 #ifdef HAVE_SYS_TIMES_H
26 #include <sys/times.h>
29 #ifdef HAVE_SYS_TIME_H
33 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
34 #include <sys/resource.h>
37 #ifdef HAVE_SYS_TIMEB_H
38 #include <sys/timeb.h>
42 #include <sys/syscall.h>
43 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
44 #define HAVE_GETRUSAGE
48 * Our caller wants a pointer to four StgInts,
49 * user seconds, user nanoseconds, system seconds, system nanoseconds.
50 * Yes, the timerval has unsigned components, but nanoseconds take only
51 * 30 bits, and our CPU usage would have to be over 68 years for the
52 * seconds to overflow 31 bits.
57 StgByteArray cpuStruct;
59 StgInt *cpu=(StgInt *)cpuStruct;
61 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
64 getrusage(RUSAGE_SELF, &t);
65 cpu[0] = t.ru_utime.tv_sec;
66 cpu[1] = 1000 * t.ru_utime.tv_usec;
67 cpu[2] = t.ru_stime.tv_sec;
68 cpu[3] = 1000 * t.ru_stime.tv_usec;
71 # if defined(HAVE_TIMES)
74 # define ticks CLK_TCK
77 ticks = sysconf(_SC_CLK_TCK);
81 cpu[0] = t.tms_utime / ticks;
82 cpu[1] = (t.tms_utime - cpu[0] * ticks) * (1000000000 / ticks);
83 cpu[2] = t.tms_stime / ticks;
84 cpu[3] = (t.tms_stime - cpu[2] * ticks) * (1000000000 / ticks);
90 return (StgByteArray) cpuStruct;