[project @ 1996-01-11 14:06:51 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 StgByteArray
54 getCPUTime(cpuStruct)
55 StgByteArray cpuStruct;
56 {
57     StgInt *cpu=(StgInt *)cpuStruct;
58
59 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS
60     struct rusage t;
61
62     getrusage(RUSAGE_SELF, &t);
63     cpu[0] = t.ru_utime.tv_sec;
64     cpu[1] = 1000 * t.ru_utime.tv_usec;
65     cpu[2] = t.ru_stime.tv_sec;
66     cpu[3] = 1000 * t.ru_stime.tv_usec;
67
68 #else
69 # if defined(HAVE_TIMES)
70     struct tms t;
71 #  if defined(CLK_TCK)
72 #   define ticks CLK_TCK
73 #  else
74     long ticks;
75     ticks = sysconf(_SC_CLK_TCK);
76 #  endif
77
78     times(&t);
79     cpu[0] = t.tms_utime / ticks;
80     cpu[1] = (t.tms_utime - cpu[0] * ticks) * (1000000000 / ticks);
81     cpu[2] = t.tms_stime / ticks;
82     cpu[3] = (t.tms_stime - cpu[2] * ticks) * (1000000000 / ticks);
83
84 # else
85     return NULL;
86 # endif
87 #endif
88     return (StgByteArray) cpuStruct;
89 }
90
91 \end{code}