df37a04ebbfebb1d2e87e8ef7abb803bf9a9b4f5
[ghc-hetmet.git] / ghc / lib / std / CPUTime.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: CPUTime.lhs,v 1.28 2001/02/22 13:17:58 simonpj Exp $
3 %
4 % (c) The University of Glasgow, 1995-2000
5 %
6 \section[CPUTime]{Haskell 98 CPU Time Library}
7
8 \begin{code}
9 {-# OPTIONS -#include "cbits/stgio.h" #-}
10
11 module CPUTime 
12         (
13          getCPUTime,       -- :: IO Integer
14          cpuTimePrecision  -- :: Integer
15         ) where
16 \end{code}
17
18
19 #ifndef __HUGS__
20
21 \begin{code}
22 import Prelude          -- To generate the dependency
23 import PrelGHC          ( indexIntArray# )
24 import PrelBase         ( Int(..) )
25 import PrelByteArr      ( ByteArray(..), newIntArray )
26 import PrelArrExtra     ( unsafeFreezeByteArray )
27 import PrelNum          ( fromInt )
28 import PrelIOBase       ( IOException(..), 
29                           IOErrorType( UnsupportedOperation ), 
30                           unsafePerformIO, stToIO, ioException )
31 import Ratio
32 \end{code}
33
34 Computation @getCPUTime@ returns the number of picoseconds CPU time
35 used by the current program.  The precision of this result is
36 implementation-dependent.
37
38 The @cpuTimePrecision@ constant is the smallest measurable difference
39 in CPU time that the implementation can record, and is given as an
40 integral number of picoseconds.
41
42 \begin{code}
43 getCPUTime :: IO Integer
44 getCPUTime = do
45     marr <- stToIO (newIntArray ((0::Int),3))
46     barr <- stToIO (unsafeFreezeByteArray marr)
47     rc   <- primGetCPUTime barr
48     if rc /= 0 then
49       case barr of
50        ByteArray _ _ frozen# -> -- avoid bounds checking
51         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
52                  fromIntegral (I# (indexIntArray# frozen# 1#)) + 
53                  fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
54                  fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
55      else
56         ioException (IOError Nothing UnsupportedOperation 
57                          "getCPUTime"
58                          "can't get CPU time"
59                          Nothing)
60
61 cpuTimePrecision :: Integer
62 cpuTimePrecision = round ((1000000000000::Integer) % 
63                           fromIntegral (unsafePerformIO clockTicks))
64
65 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
66 foreign import "libHS_cbits" "clockTicks" unsafe clockTicks :: IO Int
67
68 \end{code}
69
70 #else
71
72 \begin{code}
73 import PrelPrim ( nh_getCPUtime
74                 , nh_getCPUprec
75                 , unsafePerformIO
76                 )
77
78 getCPUTime :: IO Integer
79 getCPUTime 
80    = do seconds <- nh_getCPUtime
81         return (round (seconds * 1.0e+12))
82
83 cpuTimePrecision :: Integer
84 cpuTimePrecision
85    = unsafePerformIO (
86         do resolution <- nh_getCPUprec
87            return (round (resolution * 1.0e+12))
88      )
89 \end{code} 
90 #endif