Fix the build when HAVE_KQUEUE but not HAVE_KEVENT64
[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 "HsBaseConfig.h"
38
39 -- For _SC_CLK_TCK
40 #if HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 -- For struct rusage
45 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
46 # if HAVE_SYS_RESOURCE_H
47 #  include <sys/resource.h>
48 # endif
49 #endif
50
51 -- For FILETIME etc. on Windows
52 #if HAVE_WINDOWS_H
53 #include <windows.h>
54 #endif
55
56 -- for CLK_TCK
57 #if HAVE_TIME_H
58 #include <time.h>
59 #endif
60
61 -- for struct tms
62 #if HAVE_SYS_TIMES_H
63 #include <sys/times.h>
64 #endif
65
66 #endif
67
68 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
69 realToInteger :: Real a => a -> Integer
70 realToInteger ct = round (realToFrac ct :: Double)
71   -- CTime, CClock, CUShort etc are in Real but not Fractional, 
72   -- so we must convert to Double before we can round it
73 #endif
74
75 #ifdef __GLASGOW_HASKELL__
76 -- -----------------------------------------------------------------------------
77 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
78 -- used by the current program.  The precision of this result is
79 -- implementation-dependent.
80
81 getCPUTime :: IO Integer
82 getCPUTime = do
83
84 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
85 -- getrusage() is right royal pain to deal with when targetting multiple
86 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
87 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
88 -- again in libucb in 2.6..)
89 --
90 -- Avoid the problem by resorting to times() instead.
91 --
92 #if defined(HAVE_GETRUSAGE) && ! irix_HOST_OS && ! solaris2_HOST_OS
93     allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
94     throwErrnoIfMinus1_ "getrusage" $ getrusage (#const RUSAGE_SELF) p_rusage
95
96     let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
97     let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
98     u_sec  <- (#peek struct timeval,tv_sec)  ru_utime :: IO CTime
99     u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
100     s_sec  <- (#peek struct timeval,tv_sec)  ru_stime :: IO CTime
101     s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
102     return ((realToInteger u_sec * 1000000 + realToInteger u_usec + 
103              realToInteger s_sec * 1000000 + realToInteger s_usec) 
104                 * 1000000)
105
106 type CRUsage = ()
107 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
108 #else
109 # if defined(HAVE_TIMES)
110     allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
111     _ <- times p_tms
112     u_ticks  <- (#peek struct tms,tms_utime) p_tms :: IO CClock
113     s_ticks  <- (#peek struct tms,tms_stime) p_tms :: IO CClock
114     return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000) 
115                         `div` fromIntegral clockTicks)
116
117 type CTms = ()
118 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
119 # else
120     ioException (IOError Nothing UnsupportedOperation 
121                          "getCPUTime"
122                          "can't get CPU time"
123                          Nothing)
124 # endif
125 #endif
126
127 #else /* win32 */
128      -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
129      -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
130     allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
131     allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
132     allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
133     allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
134     pid <- getCurrentProcess
135     ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
136     if toBool ok then do
137       ut <- ft2psecs p_userTime
138       kt <- ft2psecs p_kernelTime
139       return (ut + kt)
140      else return 0
141   where 
142         ft2psecs :: Ptr FILETIME -> IO Integer
143         ft2psecs ft = do
144           high <- (#peek FILETIME,dwHighDateTime) ft :: IO Word32
145           low  <- (#peek FILETIME,dwLowDateTime)  ft :: IO Word32
146             -- Convert 100-ns units to picosecs (10^-12) 
147             -- => multiply by 10^5.
148           return (((fromIntegral high) * (2^(32::Int)) + (fromIntegral low)) * 100000)
149
150     -- ToDo: pin down elapsed times to just the OS thread(s) that
151     -- are evaluating/managing Haskell code.
152
153 type FILETIME = ()
154 type HANDLE = ()
155 -- need proper Haskell names (initial lower-case character)
156 foreign import stdcall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
157 foreign import stdcall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
158
159 #endif /* not _WIN32 */
160 #endif /* __GLASGOW_HASKELL__ */
161
162 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
163 -- in CPU time that the implementation can record, and is given as an
164 -- integral number of picoseconds.
165
166 #ifndef __NHC__
167 cpuTimePrecision :: Integer
168 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
169 #endif
170
171 #ifdef __GLASGOW_HASKELL__
172 clockTicks :: Int
173 clockTicks =
174 #if defined(CLK_TCK)
175     (#const CLK_TCK)
176 #else
177     unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
178 foreign import ccall unsafe sysconf :: CInt -> IO CLong
179 #endif
180 #endif /* __GLASGOW_HASKELL__ */