[project @ 1996-01-08 20:28:12 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
8 #ifdef hpux_TARGET_OS
9 #define _INCLUDE_HPUX_SOURCE
10 #endif
11
12 #include "rtsdefs.h"
13 #include "stgio.h"
14
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #ifdef HAVE_SYS_TIMES_H
24 #include <sys/times.h>
25 #endif
26
27 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30
31 #if defined(HAVE_SYS_RESOURCE_H) && ! irix_TARGET_OS
32 #include <sys/resource.h>
33 #endif
34
35 #ifdef HAVE_SYS_TIMEB_H
36 #include <sys/timeb.h>
37 #endif
38
39 #ifdef hpux_TARGET_OS
40 #include <sys/syscall.h>
41 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
42 #define HAVE_GETRUSAGE
43 #endif
44
45 /* 
46  * Our caller wants a pointer to four StgInts,
47  * user seconds, user nanoseconds, system seconds, system nanoseconds.
48  * Yes, the timerval has unsigned components, but nanoseconds take only
49  * 30 bits, and our CPU usage would have to be over 68 years for the 
50  * seconds to overflow 31 bits.
51  */
52
53 StgAddr
54 getCPUTime(STG_NO_ARGS)
55 {
56     static StgInt cpu[4];
57
58 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
59     struct rusage t;
60
61     getrusage(RUSAGE_SELF, &t);
62     cpu[0] = t.ru_utime.tv_sec;
63     cpu[1] = 1000 * t.ru_utime.tv_usec;
64     cpu[2] = t.ru_stime.tv_sec;
65     cpu[3] = 1000 * t.ru_stime.tv_usec;
66
67 #else
68 # if defined(HAVE_TIMES)
69     struct tms t;
70 #  if defined(CLK_TCK)
71 #   define ticks CLK_TCK
72 #  else
73     long ticks;
74     ticks = sysconf(_SC_CLK_TCK);
75 #  endif
76
77     times(&t);
78     cpu[0] = t.tms_utime / ticks;
79     cpu[1] = (t.tms_utime - cpu[0] * ticks) * (1000000000 / ticks);
80     cpu[2] = t.tms_stime / ticks;
81     cpu[3] = (t.tms_stime - cpu[2] * ticks) * (1000000000 / ticks);
82
83 # else
84     return NULL;
85 # endif
86 #endif
87     return (StgAddr) cpu;
88 }
89
90 \end{code}