[project @ 1997-03-14 07:52:06 by simonpj]
[ghc-hetmet.git] / ghc / lib / required / 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 module CPUTime 
8         (
9          getCPUTime,       -- :: IO Integer
10          cpuTimePrecision  -- :: Integer
11         ) where
12
13 import PrelBase (Int(..), indexIntArray#, Addr(..) )
14 import ArrBase  --(ByteArray(..))
15 import IOBase
16 import IO
17 import STBase
18 import Ratio
19
20 \end{code}
21
22 Computation @getCPUTime@ returns the number of picoseconds CPU time
23 used by the current program.  The precision of this result is
24 implementation-dependent.
25
26 The @cpuTimePrecision@ constant is the resolution (in picoseconds!) of
27 the number of 
28
29 \begin{code}
30 getCPUTime :: IO Integer
31 getCPUTime =
32     newIntArray (0,3)                       `thenIO_Prim` \ marr ->
33     unsafeFreezeByteArray marr              `thenIO_Prim` \ barr@(ByteArray _ frozen#) ->
34     _ccall_ getCPUTime barr                 `thenIO_Prim` \ ptr ->
35     if (ptr::Addr) /= ``NULL'' then
36         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
37                 fromIntegral (I# (indexIntArray# frozen# 1#)) + 
38                 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
39                 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
40     else
41         fail (IOError Nothing UnsupportedOperation "getCPUTime: can't get CPU time")
42
43 cpuTimePrecision :: Integer
44 cpuTimePrecision = round ((1000000000000::Integer) % 
45                           fromInt (unsafePerformPrimIO (_ccall_ clockTicks )))
46 \end{code}
47
48
49
50
51