1 {-# LANGUAGE CPP, NondecreasingIndentation, ForeignFunctionInterface #-}
3 -----------------------------------------------------------------------------
5 -- Module : System.CPUTime
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
13 -- The standard CPUTime library.
15 -----------------------------------------------------------------------------
19 getCPUTime, -- :: IO Integer
20 cpuTimePrecision -- :: Integer
28 import Hugs.Time ( getCPUTime, clockTicks )
32 import CPUTime ( getCPUTime, cpuTimePrecision )
35 #ifdef __GLASGOW_HASKELL__
36 import Foreign hiding (unsafePerformIO)
39 import System.IO.Unsafe (unsafePerformIO)
42 #include "HsBaseConfig.h"
50 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
51 # if HAVE_SYS_RESOURCE_H
52 # include <sys/resource.h>
56 -- For FILETIME etc. on Windows
68 #include <sys/times.h>
73 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
74 realToInteger :: Real a => a -> Integer
75 realToInteger ct = round (realToFrac ct :: Double)
76 -- CTime, CClock, CUShort etc are in Real but not Fractional,
77 -- so we must convert to Double before we can round it
80 #ifdef __GLASGOW_HASKELL__
81 -- -----------------------------------------------------------------------------
82 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
83 -- used by the current program. The precision of this result is
84 -- implementation-dependent.
86 getCPUTime :: IO Integer
89 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
90 -- getrusage() is right royal pain to deal with when targetting multiple
91 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
92 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
93 -- again in libucb in 2.6..)
95 -- Avoid the problem by resorting to times() instead.
97 #if defined(HAVE_GETRUSAGE) && ! irix_HOST_OS && ! solaris2_HOST_OS
98 allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
99 throwErrnoIfMinus1_ "getrusage" $ getrusage (#const RUSAGE_SELF) p_rusage
101 let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
102 let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
103 u_sec <- (#peek struct timeval,tv_sec) ru_utime :: IO CTime
104 u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
105 s_sec <- (#peek struct timeval,tv_sec) ru_stime :: IO CTime
106 s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
107 return ((realToInteger u_sec * 1000000 + realToInteger u_usec +
108 realToInteger s_sec * 1000000 + realToInteger s_usec)
112 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
114 # if defined(HAVE_TIMES)
115 allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
117 u_ticks <- (#peek struct tms,tms_utime) p_tms :: IO CClock
118 s_ticks <- (#peek struct tms,tms_stime) p_tms :: IO CClock
119 return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000)
120 `div` fromIntegral clockTicks)
123 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
125 ioException (IOError Nothing UnsupportedOperation
133 -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
134 -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
135 allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
136 allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
137 allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
138 allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
139 pid <- getCurrentProcess
140 ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
142 ut <- ft2psecs p_userTime
143 kt <- ft2psecs p_kernelTime
147 ft2psecs :: Ptr FILETIME -> IO Integer
149 high <- (#peek FILETIME,dwHighDateTime) ft :: IO Word32
150 low <- (#peek FILETIME,dwLowDateTime) ft :: IO Word32
151 -- Convert 100-ns units to picosecs (10^-12)
152 -- => multiply by 10^5.
153 return (((fromIntegral high) * (2^(32::Int)) + (fromIntegral low)) * 100000)
155 -- ToDo: pin down elapsed times to just the OS thread(s) that
156 -- are evaluating/managing Haskell code.
160 -- need proper Haskell names (initial lower-case character)
161 foreign import stdcall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
162 foreign import stdcall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
164 #endif /* not _WIN32 */
165 #endif /* __GLASGOW_HASKELL__ */
167 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
168 -- in CPU time that the implementation can record, and is given as an
169 -- integral number of picoseconds.
172 cpuTimePrecision :: Integer
173 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
176 #ifdef __GLASGOW_HASKELL__
182 unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
183 foreign import ccall unsafe sysconf :: CInt -> IO CLong
185 #endif /* __GLASGOW_HASKELL__ */