[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / std / CPUTime.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1997
3 %
4 \section[CPUTime]{Haskell 1.4 CPU Time Library}
5
6 \begin{code}
7 {-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
8
9 module CPUTime 
10         (
11          getCPUTime,       -- :: IO Integer
12          cpuTimePrecision  -- :: Integer
13         ) where
14
15 #ifdef __HUGS__
16 import PreludeBuiltin
17 #else
18 import PrelBase
19 import PrelArr          ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
20 import PrelMaybe
21 import PrelNum
22 import PrelNumExtra
23 import PrelAddr
24 import PrelIOBase
25 import PrelST
26 #endif
27 import IO               ( fail )
28 import Ratio
29
30 #ifdef __HUGS__
31 #define cat2(x,y)  x/**/y
32 #define CCALL(fun) cat2(prim_,fun)
33 #define stToIO id
34 #define sizeof_int64 8
35 #else
36 #define CCALL(fun) _ccall_ fun
37 #define const_BUFSIZ ``BUFSIZ''
38 #define primPackString
39 #endif
40
41 \end{code}
42
43 Computation @getCPUTime@ returns the number of picoseconds CPU time
44 used by the current program.  The precision of this result is
45 implementation-dependent.
46
47 The @cpuTimePrecision@ constant is the smallest measurable difference
48 in CPU time that the implementation can record, and is given as an
49 integral number of picoseconds.
50
51 \begin{code}
52 #ifdef __HUGS__
53
54 getCPUTime :: IO Integer
55 getCPUTime = do
56     marr <- primNewByteArray (sizeof_int * 4)
57     ptr  <- CCALL(getCPUTime) marr
58     if (ptr /= nullAddr) then do
59         x0 <- primReadIntArray marr 0
60         x1 <- primReadIntArray marr 1
61         x2 <- primReadIntArray marr 2
62         x3 <- primReadIntArray marr 3
63         return ((fromIntegral x0 * 1000000000 + fromIntegral  x1 + 
64                  fromIntegral x2 * 1000000000 + fromIntegral  x3)
65                * 1000)
66       else
67         fail (IOError Nothing UnsupportedOperation "getCPUTime"
68                 "can't get CPU time")
69
70 #else
71
72 getCPUTime :: IO Integer
73 getCPUTime = 
74     stToIO (newIntArray (0,3))          >>= \ marr ->
75     stToIO (unsafeFreezeByteArray marr) >>= \ barr@(ByteArray _ frozen#) ->
76     _ccall_ getCPUTime barr             >>= \ ptr ->
77     if (ptr::Addr) /= ``NULL'' then
78         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
79                  fromIntegral (I# (indexIntArray# frozen# 1#)) + 
80                  fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
81                  fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
82     else
83         fail (IOError Nothing UnsupportedOperation "getCPUTime"
84                 "can't get CPU time")
85
86 #endif
87
88 cpuTimePrecision :: Integer
89 cpuTimePrecision = round ((1000000000000::Integer) % 
90                           fromInt (unsafePerformIO (CCALL(clockTicks) )))
91 \end{code}
92
93 \begin{code}
94 #ifdef __HUGS__
95
96 sizeof_int = 4
97
98 foreign import stdcall "libHS_cbits.so" "getCPUTime" prim_getCPUTime :: Bytes -> IO Addr
99 foreign import stdcall "libHS_cbits.so" "clockTicks" prim_clockTicks :: IO Int
100 #endif
101 \end{code}
102
103
104