[project @ 1997-03-14 07:52:06 by simonpj]
[ghc-hetmet.git] / ghc / lib / cbits / getCPUTime.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[getCPUTime.lc]{getCPUTime Runtime Support}
5
6 \begin{code}
7
8 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
9
10 /* how is this to work given we have not read platform.h yet? */
11 #ifdef hpux_TARGET_OS
12 #define _INCLUDE_HPUX_SOURCE
13 #endif
14
15 #include "rtsdefs.h"
16
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
19 #endif
20
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #ifdef HAVE_SYS_TIMES_H
26 #include <sys/times.h>
27 #endif
28
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32
33 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
34 #include <sys/resource.h>
35 #endif
36
37 #ifdef HAVE_SYS_TIMEB_H
38 #include <sys/timeb.h>
39 #endif
40
41 #ifdef hpux_TARGET_OS
42 #include <sys/syscall.h>
43 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
44 #define HAVE_GETRUSAGE
45 #endif
46
47 StgInt 
48 clockTicks ()
49 {
50  return (
51 #if defined(CLK_TCK)
52     CLK_TCK
53 #else
54     sysconf(_SC_CLK_TCK)
55 #endif
56     ); 
57 }
58
59 /* 
60  * Our caller wants a pointer to four StgInts,
61  * user seconds, user nanoseconds, system seconds, system nanoseconds.
62  * Yes, the timerval has unsigned components, but nanoseconds take only
63  * 30 bits, and our CPU usage would have to be over 68 years for the 
64  * seconds to overflow 31 bits.
65  */
66
67 StgByteArray
68 getCPUTime(StgByteArray cpuStruct)
69 {
70     StgInt *cpu=(StgInt *)cpuStruct;
71
72 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
73     struct rusage t;
74
75     getrusage(RUSAGE_SELF, &t);
76     cpu[0] = t.ru_utime.tv_sec;
77     cpu[1] = 1000 * t.ru_utime.tv_usec;
78     cpu[2] = t.ru_stime.tv_sec;
79     cpu[3] = 1000 * t.ru_stime.tv_usec;
80
81 #else
82 # if defined(HAVE_TIMES)
83     struct tms t;
84 #  if defined(CLK_TCK)
85 #   define ticks CLK_TCK
86 #  else
87     long ticks;
88     ticks = sysconf(_SC_CLK_TCK);
89 #  endif
90
91     times(&t);
92     cpu[0] = t.tms_utime / ticks;
93     cpu[1] = (t.tms_utime - cpu[0] * ticks) * (1000000000 / ticks);
94     cpu[2] = t.tms_stime / ticks;
95     cpu[3] = (t.tms_stime - cpu[2] * ticks) * (1000000000 / ticks);
96
97 # else
98     return NULL;
99 # endif
100 #endif
101     return (StgByteArray) cpuStruct;
102 }
103
104 \end{code}
105