2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: getCPUTime.c,v 1.1 1998/04/10 10:54:34 simonm Exp $
6 * getCPUTime Runtime Support
10 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
13 /* how is this to work given we have not read platform.h yet? */
15 #define _INCLUDE_HPUX_SOURCE
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
28 #ifdef HAVE_SYS_TIMES_H
29 #include <sys/times.h>
32 #ifdef HAVE_SYS_TIME_H
36 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
37 #include <sys/resource.h>
40 #ifdef HAVE_SYS_TIMEB_H
41 #include <sys/timeb.h>
45 #include <sys/syscall.h>
46 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
47 #define HAVE_GETRUSAGE
63 * Our caller wants a pointer to four StgInts,
64 * user seconds, user nanoseconds, system seconds, system nanoseconds.
65 * Yes, the timerval has unsigned components, but nanoseconds take only
66 * 30 bits, and our CPU usage would have to be over 68 years for the
67 * seconds to overflow 31 bits.
71 getCPUTime(StgByteArray cpuStruct)
73 StgInt *cpu=(StgInt *)cpuStruct;
75 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
78 getrusage(RUSAGE_SELF, &t);
79 cpu[0] = t.ru_utime.tv_sec;
80 cpu[1] = 1000 * t.ru_utime.tv_usec;
81 cpu[2] = t.ru_stime.tv_sec;
82 cpu[3] = 1000 * t.ru_stime.tv_usec;
85 # if defined(HAVE_TIMES)
88 # define ticks CLK_TCK
91 ticks = sysconf(_SC_CLK_TCK);
95 cpu[0] = t.tms_utime / ticks;
96 cpu[1] = (t.tms_utime - cpu[0] * ticks) * (1000000000 / ticks);
97 cpu[2] = t.tms_stime / ticks;
98 cpu[3] = (t.tms_stime - cpu[2] * ticks) * (1000000000 / ticks);
104 return (StgByteArray)cpuStruct;