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