1 -----------------------------------------------------------------------------
3 -- Module : System.CPUTime
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- The standard CPUTime library.
13 -----------------------------------------------------------------------------
17 getCPUTime, -- :: IO Integer
18 cpuTimePrecision -- :: Integer
26 import Hugs.Time ( getCPUTime, clockTicks )
30 import CPUTime ( getCPUTime, cpuTimePrecision )
33 #ifdef __GLASGOW_HASKELL__
34 import Foreign hiding (unsafePerformIO)
37 import System.IO.Unsafe (unsafePerformIO)
40 #include "HsBaseConfig.h"
48 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
49 # if HAVE_SYS_RESOURCE_H
50 # include <sys/resource.h>
54 -- For FILETIME etc. on Windows
66 #include <sys/times.h>
71 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
72 realToInteger :: Real a => a -> Integer
73 realToInteger ct = round (realToFrac ct :: Double)
74 -- CTime, CClock, CUShort etc are in Real but not Fractional,
75 -- so we must convert to Double before we can round it
78 #ifdef __GLASGOW_HASKELL__
79 -- -----------------------------------------------------------------------------
80 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
81 -- used by the current program. The precision of this result is
82 -- implementation-dependent.
84 getCPUTime :: IO Integer
87 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
88 -- getrusage() is right royal pain to deal with when targetting multiple
89 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
90 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
91 -- again in libucb in 2.6..)
93 -- Avoid the problem by resorting to times() instead.
95 #if defined(HAVE_GETRUSAGE) && ! irix_HOST_OS && ! solaris2_HOST_OS
96 allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
97 throwErrnoIfMinus1_ "getrusage" $ getrusage (#const RUSAGE_SELF) p_rusage
99 let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
100 let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
101 u_sec <- (#peek struct timeval,tv_sec) ru_utime :: IO CTime
102 u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
103 s_sec <- (#peek struct timeval,tv_sec) ru_stime :: IO CTime
104 s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
105 return ((realToInteger u_sec * 1000000 + realToInteger u_usec +
106 realToInteger s_sec * 1000000 + realToInteger s_usec)
110 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
112 # if defined(HAVE_TIMES)
113 allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
115 u_ticks <- (#peek struct tms,tms_utime) p_tms :: IO CClock
116 s_ticks <- (#peek struct tms,tms_stime) p_tms :: IO CClock
117 return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000)
118 `div` fromIntegral clockTicks)
121 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
123 ioException (IOError Nothing UnsupportedOperation
131 -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
132 -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
133 allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
134 allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
135 allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
136 allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
137 pid <- getCurrentProcess
138 ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
140 ut <- ft2psecs p_userTime
141 kt <- ft2psecs p_kernelTime
145 ft2psecs :: Ptr FILETIME -> IO Integer
147 high <- (#peek FILETIME,dwHighDateTime) ft :: IO Word32
148 low <- (#peek FILETIME,dwLowDateTime) ft :: IO Word32
149 -- Convert 100-ns units to picosecs (10^-12)
150 -- => multiply by 10^5.
151 return (((fromIntegral high) * (2^(32::Int)) + (fromIntegral low)) * 100000)
153 -- ToDo: pin down elapsed times to just the OS thread(s) that
154 -- are evaluating/managing Haskell code.
158 -- need proper Haskell names (initial lower-case character)
159 foreign import stdcall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
160 foreign import stdcall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
162 #endif /* not _WIN32 */
163 #endif /* __GLASGOW_HASKELL__ */
165 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
166 -- in CPU time that the implementation can record, and is given as an
167 -- integral number of picoseconds.
170 cpuTimePrecision :: Integer
171 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
174 #ifdef __GLASGOW_HASKELL__
180 unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
181 foreign import ccall unsafe sysconf :: CInt -> IO CLong
183 #endif /* __GLASGOW_HASKELL__ */