[project @ 1998-04-10 10:49:39 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / getCPUTime.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: getCPUTime.c,v 1.1 1998/04/10 10:54:34 simonm Exp $
5  *
6  * getCPUTime Runtime Support
7  */
8
9 #ifndef _AIX
10 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
11 #endif
12
13 /* how is this to work given we have not read platform.h yet? */
14 #ifdef hpux_TARGET_OS
15 #define _INCLUDE_HPUX_SOURCE
16 #endif
17
18 #include "Rts.h"
19
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #ifdef HAVE_SYS_TIMES_H
29 #include <sys/times.h>
30 #endif
31
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35
36 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
37 #include <sys/resource.h>
38 #endif
39
40 #ifdef HAVE_SYS_TIMEB_H
41 #include <sys/timeb.h>
42 #endif
43
44 #ifdef hpux_TARGET_OS
45 #include <sys/syscall.h>
46 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
47 #define HAVE_GETRUSAGE
48 #endif
49
50 StgInt 
51 clockTicks (void)
52 {
53  return (
54 #if defined(CLK_TCK)
55     CLK_TCK
56 #else
57     sysconf(_SC_CLK_TCK)
58 #endif
59     ); 
60 }
61
62 /* 
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.
68  */
69
70 StgByteArray
71 getCPUTime(StgByteArray cpuStruct)
72 {
73     StgInt *cpu=(StgInt *)cpuStruct;
74
75 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
76     struct rusage t;
77
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;
83
84 #else
85 # if defined(HAVE_TIMES)
86     struct tms t;
87 #  if defined(CLK_TCK)
88 #   define ticks CLK_TCK
89 #  else
90     long ticks;
91     ticks = sysconf(_SC_CLK_TCK);
92 #  endif
93
94     times(&t);
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);
99
100 # else
101     return NULL;
102 # endif
103 #endif
104     return (StgByteArray)cpuStruct;
105 }