1 -- -----------------------------------------------------------------------------
2 -- $Id: CPUTime.hsc,v 1.6 2001/06/22 12:36:33 rrt Exp $
4 -- (c) The University of Glasgow, 1995-2001
9 getCPUTime, -- :: IO Integer
10 cpuTimePrecision -- :: Integer
13 import PrelMarshalAlloc
14 import PrelMarshalUtils ( toBool )
20 import PrelBase ( Int(..) )
21 import PrelByteArr ( ByteArray(..), newIntArray )
22 import PrelArrExtra ( unsafeFreezeByteArray )
23 import PrelIOBase ( IOException(..) )
26 #include "cbits/HsStd.h"
28 -- -----------------------------------------------------------------------------
29 -- Computation `getCPUTime' returns the number of picoseconds CPU time
30 -- used by the current program. The precision of this result is
31 -- implementation-dependent.
33 -- The `cpuTimePrecision' constant is the smallest measurable difference
34 -- in CPU time that the implementation can record, and is given as an
35 -- integral number of picoseconds.
37 getCPUTime :: IO Integer
41 -- getrusage() is right royal pain to deal with when targetting multiple
42 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
43 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
44 -- again in libucb in 2.6..)
46 -- Avoid the problem by resorting to times() instead.
48 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS && ! solaris2_TARGET_OS
49 allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
50 getrusage (#const RUSAGE_SELF) p_rusage
52 let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
53 let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
54 u_sec <- (#peek struct timeval,tv_sec) ru_utime :: IO CLong
55 u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CLong
56 s_sec <- (#peek struct timeval,tv_sec) ru_stime :: IO CLong
57 s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CLong
59 return ((fromIntegral u_sec * 1000000 + fromIntegral u_usec +
60 fromIntegral s_sec * 1000000 + fromIntegral s_usec)
64 foreign import unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
66 # if defined(HAVE_TIMES)
67 allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
69 u_ticks <- (#peek struct tms,tms_utime) p_tms :: IO CClock
70 s_ticks <- (#peek struct tms,tms_stime) p_tms :: IO CClock
71 return (( (fromIntegral u_ticks + fromIntegral s_ticks) * 1000000000000)
75 foreign import unsafe times :: Ptr CTms -> CClock
77 ioException (IOError Nothing UnsupportedOperation
85 allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
86 allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
87 allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
88 allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
89 pid <- getCurrentProcess
90 ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
92 ut <- ft2usecs p_userTime
93 kt <- ft2usecs p_kernelTime
94 return (fromIntegral (ut + kt))
96 where ft2usecs ft = do
97 high <- (#peek FILETIME,dwHighDateTime) ft :: IO CLong
98 low <- (#peek FILETIME,dwLowDateTime) ft :: IO CLong
99 return (high * (2^32) + low)
101 -- ToDo: pin down elapsed times to just the OS thread(s) that
102 -- are evaluating/managing Haskell code.
106 -- need proper Haskell names (initial lower-case character)
107 foreign import "GetCurrentProcess" unsafe getCurrentProcess :: IO (Ptr HANDLE)
108 foreign import "GetProcessTimes" unsafe getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
110 #endif /* not _WIN32 */
112 cpuTimePrecision :: Integer
113 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
120 unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
121 foreign import unsafe sysconf :: CInt -> IO CLong