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__
40 #ifdef __GLASGOW_HASKELL__
41 -- -----------------------------------------------------------------------------
42 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
43 -- used by the current program. The precision of this result is
44 -- implementation-dependent.
46 getCPUTime :: IO Integer
49 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
50 -- getrusage() is right royal pain to deal with when targetting multiple
51 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
52 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
53 -- again in libucb in 2.6..)
55 -- Avoid the problem by resorting to times() instead.
57 #if defined(HAVE_GETRUSAGE) && ! irix_HOST_OS && ! solaris2_HOST_OS
58 allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
59 getrusage (#const RUSAGE_SELF) p_rusage
61 let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
62 let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
63 u_sec <- (#peek struct timeval,tv_sec) ru_utime :: IO CTime
64 u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
65 s_sec <- (#peek struct timeval,tv_sec) ru_stime :: IO CTime
66 s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
67 let realToInteger = round . realToFrac :: Real a => a -> Integer
68 return ((realToInteger u_sec * 1000000 + realToInteger u_usec +
69 realToInteger s_sec * 1000000 + realToInteger s_usec)
73 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
75 # if defined(HAVE_TIMES)
76 allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
78 u_ticks <- (#peek struct tms,tms_utime) p_tms :: IO CClock
79 s_ticks <- (#peek struct tms,tms_stime) p_tms :: IO CClock
80 let realToInteger = round . realToFrac :: Real a => a -> Integer
81 return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000)
82 `div` fromIntegral clockTicks)
85 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
87 ioException (IOError Nothing UnsupportedOperation
95 -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
96 -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
97 allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
98 allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
99 allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
100 allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
101 pid <- getCurrentProcess
102 ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
104 ut <- ft2psecs p_userTime
105 kt <- ft2psecs p_kernelTime
109 ft2psecs :: Ptr FILETIME -> IO Integer
111 high <- (#peek FILETIME,dwHighDateTime) ft :: IO Word32
112 low <- (#peek FILETIME,dwLowDateTime) ft :: IO Word32
113 -- Convert 100-ns units to picosecs (10^-12)
114 -- => multiply by 10^5.
115 return (((fromIntegral high) * (2^32) + (fromIntegral low)) * 100000)
117 -- ToDo: pin down elapsed times to just the OS thread(s) that
118 -- are evaluating/managing Haskell code.
122 -- need proper Haskell names (initial lower-case character)
123 foreign import stdcall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
124 foreign import stdcall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
126 #endif /* not _WIN32 */
127 #endif /* __GLASGOW_HASKELL__ */
129 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
130 -- in CPU time that the implementation can record, and is given as an
131 -- integral number of picoseconds.
134 cpuTimePrecision :: Integer
135 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
138 #ifdef __GLASGOW_HASKELL__
144 unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
145 foreign import ccall unsafe sysconf :: CInt -> IO CLong
147 #endif /* __GLASGOW_HASKELL__ */