[project @ 2001-02-22 16:48:24 by qrczak]
[ghc-hetmet.git] / ghc / lib / std / CPUTime.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: CPUTime.lhs,v 1.29 2001/02/22 16:48:24 qrczak 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 PrelIOBase       ( IOException(..), 
28                           IOErrorType( UnsupportedOperation ), 
29                           unsafePerformIO, stToIO, ioException )
30 import Ratio
31 \end{code}
32
33 Computation @getCPUTime@ returns the number of picoseconds CPU time
34 used by the current program.  The precision of this result is
35 implementation-dependent.
36
37 The @cpuTimePrecision@ constant is the smallest measurable difference
38 in CPU time that the implementation can record, and is given as an
39 integral number of picoseconds.
40
41 \begin{code}
42 getCPUTime :: IO Integer
43 getCPUTime = do
44     marr <- stToIO (newIntArray ((0::Int),3))
45     barr <- stToIO (unsafeFreezeByteArray marr)
46     rc   <- primGetCPUTime barr
47     if rc /= 0 then
48       case barr of
49        ByteArray _ _ frozen# -> -- avoid bounds checking
50         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
51                  fromIntegral (I# (indexIntArray# frozen# 1#)) + 
52                  fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
53                  fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
54      else
55         ioException (IOError Nothing UnsupportedOperation 
56                          "getCPUTime"
57                          "can't get CPU time"
58                          Nothing)
59
60 cpuTimePrecision :: Integer
61 cpuTimePrecision = round ((1000000000000::Integer) % 
62                           fromIntegral (unsafePerformIO clockTicks))
63
64 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
65 foreign import "libHS_cbits" "clockTicks" unsafe clockTicks :: IO Int
66
67 \end{code}
68
69 #else
70
71 \begin{code}
72 import PrelPrim ( nh_getCPUtime
73                 , nh_getCPUprec
74                 , unsafePerformIO
75                 )
76
77 getCPUTime :: IO Integer
78 getCPUTime 
79    = do seconds <- nh_getCPUtime
80         return (round (seconds * 1.0e+12))
81
82 cpuTimePrecision :: Integer
83 cpuTimePrecision
84    = unsafePerformIO (
85         do resolution <- nh_getCPUprec
86            return (round (resolution * 1.0e+12))
87      )
88 \end{code} 
89 #endif