[project @ 2001-05-18 16:54:04 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / CPUTime.hsc
1 -- -----------------------------------------------------------------------------
2 -- $Id: CPUTime.hsc,v 1.3 2001/05/18 16:54:04 simonmar Exp $
3 --
4 -- (c) The University of Glasgow, 1995-2001
5 --
6
7 module CPUTime 
8         (
9          getCPUTime,       -- :: IO Integer
10          cpuTimePrecision  -- :: Integer
11         ) where
12
13 import PrelMarshalAlloc
14 import PrelCTypesISO
15 import PrelCTypes
16 import PrelStorable
17 import PrelPtr
18
19 import PrelBase         ( Int(..) )
20 import PrelByteArr      ( ByteArray(..), newIntArray )
21 import PrelArrExtra     ( unsafeFreezeByteArray )
22 import PrelIOBase       ( IOException(..), 
23                           IOErrorType( UnsupportedOperation ), 
24                           unsafePerformIO, stToIO, ioException )
25 import Ratio
26
27 #include "HsStd.h"
28
29 -- -----------------------------------------------------------------------------
30 -- Computation `getCPUTime' returns the number of picoseconds CPU time
31 -- used by the current program.  The precision of this result is
32 -- implementation-dependent.
33
34 -- The `cpuTimePrecision' constant is the smallest measurable difference
35 -- in CPU time that the implementation can record, and is given as an
36 -- integral number of picoseconds.
37
38 getCPUTime :: IO Integer
39 getCPUTime = do
40
41 #ifndef _WIN32
42 -- getrusage() is right royal pain to deal with when targetting multiple
43 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
44 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
45 -- again in libucb in 2.6..)
46 --
47 -- Avoid the problem by resorting to times() instead.
48 --
49 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS && ! solaris2_TARGET_OS
50     allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
51     getrusage (#const RUSAGE_SELF) p_rusage
52
53     let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
54     let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
55     u_sec  <- (#peek struct timeval,tv_sec)  ru_utime :: IO CLong
56     u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CLong
57     s_sec  <- (#peek struct timeval,tv_sec)  ru_stime :: IO CLong
58     s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CLong
59
60     return ((fromIntegral u_sec * 1000000 + fromIntegral u_usec + 
61              fromIntegral s_sec * 1000000 + fromIntegral s_usec) 
62                 * 1000000)
63
64 type CRUsage = ()
65 foreign import unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
66 #else
67 # if defined(HAVE_TIMES)
68     allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
69     times p_tms
70     u_ticks  <- (#peek struct tms,tms_utime) p_tms :: IO CClock
71     s_ticks  <- (#peek struct tms,tms_stime) p_tms :: IO CClock
72     return (( (fromIntegral u_ticks + fromIntegral s_ticks) * 1000000000000) 
73                         `div` clockTicks)
74
75 type CTms = ()
76 foreign import unsafe times :: Ptr CTms -> CClock
77 # else
78     ioException (IOError Nothing UnsupportedOperation 
79                          "getCPUTime"
80                          "can't get CPU time"
81                          Nothing)
82 # endif
83 #endif
84
85 #else /* _WIN32 */
86
87 #error ToDo!!!
88
89 #ifdef _WIN32
90 /* 100ns units per sec, really */
91 #define NS_PER_SEC 10000000LL
92 #define FT2usecs(ll,ft)    \
93     (ll)=(ft).dwHighDateTime; \
94     (ll) <<= 32;              \
95     (ll) |= (ft).dwLowDateTime;
96
97 #endif
98
99 /* cygwin32 or mingw32 version */
100 StgInt
101 getCPUTime(StgByteArray cpuStruct)
102 {
103     FILETIME creationTime, exitTime, kernelTime, userTime;
104     StgInt *cpu=(StgInt *)cpuStruct;
105     unsigned long long uT, kT;
106  
107     /* ToDo: pin down elapsed times to just the OS thread(s) that
108        are evaluating/managing Haskell code.
109     */
110     if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
111                           &exitTime, &kernelTime, &userTime)) {
112         /* Probably on a Win95 box..*/
113         cpu[0]=0;
114         cpu[1]=0;
115         cpu[2]=0;
116         cpu[3]=0;
117         return 1;
118     }
119
120     FT2usecs(uT, userTime);
121     FT2usecs(kT, kernelTime);
122
123     cpu[0] = (unsigned int)(uT / NS_PER_SEC);
124     cpu[1] = (unsigned int)((uT - cpu[0] * NS_PER_SEC) * 100);
125     cpu[2] = (unsigned int)(kT / NS_PER_SEC);
126     cpu[3] = (unsigned int)((kT - cpu[2] * NS_PER_SEC) * 100);
127     return 1;
128 }
129 #endif /* WIN32 */
130
131 cpuTimePrecision :: Integer
132 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
133
134 clockTicks :: Int
135 clockTicks =
136 #if defined(CLK_TCK)
137     (#const CLK_TCK)
138 #else
139     unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
140 foreign import unsafe sysconf :: CInt -> IO CLong
141 #endif