1 -----------------------------------------------------------------------------
3 -- Module : System.CPUTime
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/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 )
29 #ifdef __GLASGOW_HASKELL__
36 #ifdef __GLASGOW_HASKELL__
37 -- -----------------------------------------------------------------------------
38 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
39 -- used by the current program. The precision of this result is
40 -- implementation-dependent.
42 getCPUTime :: IO Integer
45 #if !defined(mingw32_TARGET_OS) && !defined(cygwin32_TARGET_OS)
46 -- getrusage() is right royal pain to deal with when targetting multiple
47 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
48 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
49 -- again in libucb in 2.6..)
51 -- Avoid the problem by resorting to times() instead.
53 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS && ! solaris2_TARGET_OS
54 allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
55 getrusage (#const RUSAGE_SELF) p_rusage
57 let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
58 let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
59 u_sec <- (#peek struct timeval,tv_sec) ru_utime :: IO CTime
60 u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
61 s_sec <- (#peek struct timeval,tv_sec) ru_stime :: IO CTime
62 s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
64 return ((fromIntegral u_sec * 1000000 + fromIntegral u_usec +
65 fromIntegral s_sec * 1000000 + fromIntegral s_usec)
69 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
71 # if defined(HAVE_TIMES)
72 allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
74 u_ticks <- (#peek struct tms,tms_utime) p_tms :: IO CClock
75 s_ticks <- (#peek struct tms,tms_stime) p_tms :: IO CClock
76 return (( (fromIntegral u_ticks + fromIntegral s_ticks) * 1000000000000)
77 `div` fromIntegral clockTicks)
80 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
82 ioException (IOError Nothing UnsupportedOperation
90 -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
91 -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
92 allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
93 allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
94 allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
95 allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
96 pid <- getCurrentProcess
97 ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
99 ut <- ft2psecs p_userTime
100 kt <- ft2psecs p_kernelTime
104 ft2psecs :: Ptr FILETIME -> IO Integer
106 high <- (#peek FILETIME,dwHighDateTime) ft :: IO CLong
107 low <- (#peek FILETIME,dwLowDateTime) ft :: IO CLong
108 -- Convert 100-ns units to picosecs (10^-12)
109 -- => multiply by 10^5.
110 return (((fromIntegral high) * (2^32) + (fromIntegral low)) * 100000)
112 -- ToDo: pin down elapsed times to just the OS thread(s) that
113 -- are evaluating/managing Haskell code.
117 -- need proper Haskell names (initial lower-case character)
118 foreign import ccall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
119 foreign import ccall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
121 #endif /* not _WIN32 */
122 #endif /* __GLASGOW_HASKELL__ */
124 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
125 -- in CPU time that the implementation can record, and is given as an
126 -- integral number of picoseconds.
128 cpuTimePrecision :: Integer
129 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
131 #ifdef __GLASGOW_HASKELL__
137 unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
138 foreign import ccall unsafe sysconf :: CInt -> IO CLong
140 #endif /* __GLASGOW_HASKELL__ */