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__
37 #include "HsBaseConfig.h"
45 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
46 # if HAVE_SYS_RESOURCE_H
47 # include <sys/resource.h>
51 -- For FILETIME etc. on Windows
63 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
64 realToInteger :: Real a => a -> Integer
65 realToInteger ct = round (realToFrac ct :: Double)
66 -- CTime, CClock, CUShort etc are in Real but not Fractional,
67 -- so we must convert to Double before we can round it
70 #ifdef __GLASGOW_HASKELL__
71 -- -----------------------------------------------------------------------------
72 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
73 -- used by the current program. The precision of this result is
74 -- implementation-dependent.
76 getCPUTime :: IO Integer
79 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
80 -- getrusage() is right royal pain to deal with when targetting multiple
81 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
82 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
83 -- again in libucb in 2.6..)
85 -- Avoid the problem by resorting to times() instead.
87 #if defined(HAVE_GETRUSAGE) && ! irix_HOST_OS && ! solaris2_HOST_OS
88 allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
89 getrusage (#const RUSAGE_SELF) p_rusage
91 let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
92 let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
93 u_sec <- (#peek struct timeval,tv_sec) ru_utime :: IO CTime
94 u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
95 s_sec <- (#peek struct timeval,tv_sec) ru_stime :: IO CTime
96 s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
97 return ((realToInteger u_sec * 1000000 + realToInteger u_usec +
98 realToInteger s_sec * 1000000 + realToInteger s_usec)
102 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
104 # if defined(HAVE_TIMES)
105 allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
107 u_ticks <- (#peek struct tms,tms_utime) p_tms :: IO CClock
108 s_ticks <- (#peek struct tms,tms_stime) p_tms :: IO CClock
109 return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000)
110 `div` fromIntegral clockTicks)
113 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
115 ioException (IOError Nothing UnsupportedOperation
123 -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
124 -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
125 allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
126 allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
127 allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
128 allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
129 pid <- getCurrentProcess
130 ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
132 ut <- ft2psecs p_userTime
133 kt <- ft2psecs p_kernelTime
137 ft2psecs :: Ptr FILETIME -> IO Integer
139 high <- (#peek FILETIME,dwHighDateTime) ft :: IO Word32
140 low <- (#peek FILETIME,dwLowDateTime) ft :: IO Word32
141 -- Convert 100-ns units to picosecs (10^-12)
142 -- => multiply by 10^5.
143 return (((fromIntegral high) * (2^(32::Int)) + (fromIntegral low)) * 100000)
145 -- ToDo: pin down elapsed times to just the OS thread(s) that
146 -- are evaluating/managing Haskell code.
150 -- need proper Haskell names (initial lower-case character)
151 foreign import stdcall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
152 foreign import stdcall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
154 #endif /* not _WIN32 */
155 #endif /* __GLASGOW_HASKELL__ */
157 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
158 -- in CPU time that the implementation can record, and is given as an
159 -- integral number of picoseconds.
162 cpuTimePrecision :: Integer
163 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
166 #ifdef __GLASGOW_HASKELL__
172 unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
173 foreign import ccall unsafe sysconf :: CInt -> IO CLong
175 #endif /* __GLASGOW_HASKELL__ */