[project @ 1996-06-27 16:13:29 by partain]
[ghc-hetmet.git] / ghc / runtime / io / 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 #define NON_POSIX_SOURCE /*needed for solaris2 only?*/
8
9 /* how is this to work given we have not read platform.h yet? */
10 #ifdef hpux_TARGET_OS
11 #define _INCLUDE_HPUX_SOURCE
12 #endif
13
14 #include "rtsdefs.h"
15 #include "stgio.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 /* 
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.
53  */
54
55 StgByteArray
56 getCPUTime(cpuStruct)
57 StgByteArray cpuStruct;
58 {
59     StgInt *cpu=(StgInt *)cpuStruct;
60
61 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
62     struct rusage t;
63
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;
69
70 #else
71 # if defined(HAVE_TIMES)
72     struct tms t;
73 #  if defined(CLK_TCK)
74 #   define ticks CLK_TCK
75 #  else
76     long ticks;
77     ticks = sysconf(_SC_CLK_TCK);
78 #  endif
79
80     times(&t);
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);
85
86 # else
87     return NULL;
88 # endif
89 #endif
90     return (StgByteArray) cpuStruct;
91 }
92
93 \end{code}