307b2a2ddb796ccf58083555516f23913196ab5e
[ghc-base.git] / System / CPUTime.hsc
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  System.CPUTime
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  portable
10 --
11 -- The standard CPUTime library.
12 --
13 -----------------------------------------------------------------------------
14
15 module System.CPUTime 
16         (
17          getCPUTime,       -- :: IO Integer
18          cpuTimePrecision  -- :: Integer
19         ) where
20
21 import Prelude
22
23 import Data.Ratio
24
25 #ifdef __HUGS__
26 import Hugs.Time ( getCPUTime, clockTicks )
27 #endif
28
29 #ifdef __NHC__
30 import CPUTime ( getCPUTime, cpuTimePrecision )
31 #endif
32
33 #ifdef __GLASGOW_HASKELL__
34 import Foreign
35 import Foreign.C
36
37 #include "HsBase.h"
38 #endif
39
40 realToInteger :: Real a => a -> Integer
41 realToInteger ct = round (realToFrac ct :: Double)
42   -- CTime, CClock, CUShort etc are in Real but not Fractional, 
43   -- so we must convert to Double before we can round it
44
45 #ifdef __GLASGOW_HASKELL__
46 -- -----------------------------------------------------------------------------
47 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
48 -- used by the current program.  The precision of this result is
49 -- implementation-dependent.
50
51 getCPUTime :: IO Integer
52 getCPUTime = do
53
54 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
55 -- getrusage() is right royal pain to deal with when targetting multiple
56 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
57 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
58 -- again in libucb in 2.6..)
59 --
60 -- Avoid the problem by resorting to times() instead.
61 --
62 #if defined(HAVE_GETRUSAGE) && ! irix_HOST_OS && ! solaris2_HOST_OS
63     allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
64     getrusage (#const RUSAGE_SELF) p_rusage
65
66     let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
67     let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
68     u_sec  <- (#peek struct timeval,tv_sec)  ru_utime :: IO CTime
69     u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
70     s_sec  <- (#peek struct timeval,tv_sec)  ru_stime :: IO CTime
71     s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
72     return ((realToInteger u_sec * 1000000 + realToInteger u_usec + 
73              realToInteger s_sec * 1000000 + realToInteger s_usec) 
74                 * 1000000)
75
76 type CRUsage = ()
77 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
78 #else
79 # if defined(HAVE_TIMES)
80     allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
81     times p_tms
82     u_ticks  <- (#peek struct tms,tms_utime) p_tms :: IO CClock
83     s_ticks  <- (#peek struct tms,tms_stime) p_tms :: IO CClock
84     return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000) 
85                         `div` fromIntegral clockTicks)
86
87 type CTms = ()
88 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
89 # else
90     ioException (IOError Nothing UnsupportedOperation 
91                          "getCPUTime"
92                          "can't get CPU time"
93                          Nothing)
94 # endif
95 #endif
96
97 #else /* win32 */
98      -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
99      -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
100     allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
101     allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
102     allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
103     allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
104     pid <- getCurrentProcess
105     ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
106     if toBool ok then do
107       ut <- ft2psecs p_userTime
108       kt <- ft2psecs p_kernelTime
109       return (ut + kt)
110      else return 0
111   where 
112         ft2psecs :: Ptr FILETIME -> IO Integer
113         ft2psecs ft = do
114           high <- (#peek FILETIME,dwHighDateTime) ft :: IO Word32
115           low  <- (#peek FILETIME,dwLowDateTime)  ft :: IO Word32
116             -- Convert 100-ns units to picosecs (10^-12) 
117             -- => multiply by 10^5.
118           return (((fromIntegral high) * (2^32) + (fromIntegral low)) * 100000)
119
120     -- ToDo: pin down elapsed times to just the OS thread(s) that
121     -- are evaluating/managing Haskell code.
122
123 type FILETIME = ()
124 type HANDLE = ()
125 -- need proper Haskell names (initial lower-case character)
126 foreign import stdcall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
127 foreign import stdcall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
128
129 #endif /* not _WIN32 */
130 #endif /* __GLASGOW_HASKELL__ */
131
132 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
133 -- in CPU time that the implementation can record, and is given as an
134 -- integral number of picoseconds.
135
136 #ifndef __NHC__
137 cpuTimePrecision :: Integer
138 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
139 #endif
140
141 #ifdef __GLASGOW_HASKELL__
142 clockTicks :: Int
143 clockTicks =
144 #if defined(CLK_TCK)
145     (#const CLK_TCK)
146 #else
147     unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
148 foreign import ccall unsafe sysconf :: CInt -> IO CLong
149 #endif
150 #endif /* __GLASGOW_HASKELL__ */