[project @ 2002-12-19 15:23:29 by ross]
[haskell-directory.git] / System / CPUTime.hsc
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  System.CPUTime
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/core/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  portable
10 --
11 -- The standard CPUTime library.
12 --
13 -----------------------------------------------------------------------------
14
15 module System.CPUTime 
16         (
17          getCPUTime,       -- :: IO Integer
18          cpuTimePrecision  -- :: Integer
19         ) where
20
21 import Prelude
22
23 import Data.Ratio
24
25 #ifdef __HUGS__
26 import Hugs.Time ( getCPUTime, clockTicks )
27 #endif
28
29 #ifdef __GLASGOW_HASKELL__
30 import Foreign
31 import Foreign.C
32
33 #include "HsBase.h"
34 #endif
35
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.
41
42 getCPUTime :: IO Integer
43 getCPUTime = do
44
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..)
50 --
51 -- Avoid the problem by resorting to times() instead.
52 --
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
56
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
63
64     return ((fromIntegral u_sec * 1000000 + fromIntegral u_usec + 
65              fromIntegral s_sec * 1000000 + fromIntegral s_usec) 
66                 * 1000000)
67
68 type CRUsage = ()
69 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
70 #else
71 # if defined(HAVE_TIMES)
72     allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
73     times p_tms
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)
78
79 type CTms = ()
80 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
81 # else
82     ioException (IOError Nothing UnsupportedOperation 
83                          "getCPUTime"
84                          "can't get CPU time"
85                          Nothing)
86 # endif
87 #endif
88
89 #else /* win32 */
90     allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
91     allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
92     allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
93     allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
94     pid <- getCurrentProcess
95     ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
96     if toBool ok then do
97       ut <- ft2psecs p_userTime
98       kt <- ft2psecs p_kernelTime
99       return (fromIntegral (ut + kt))
100      else return 0
101   where ft2psecs ft = do
102           high <- (#peek FILETIME,dwHighDateTime) ft :: IO CLong
103           low <- (#peek FILETIME,dwLowDateTime) ft :: IO CLong
104           return (((fromIntegral high) * (2^32) + (fromIntegral low)) * 100000)
105
106     -- ToDo: pin down elapsed times to just the OS thread(s) that
107     -- are evaluating/managing Haskell code.
108
109 type FILETIME = ()
110 type HANDLE = ()
111 -- need proper Haskell names (initial lower-case character)
112 foreign import ccall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
113 foreign import ccall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
114
115 #endif /* not _WIN32 */
116 #endif /* __GLASGOW_HASKELL__ */
117
118 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
119 -- in CPU time that the implementation can record, and is given as an
120 -- integral number of picoseconds.
121
122 cpuTimePrecision :: Integer
123 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
124
125 #ifdef __GLASGOW_HASKELL__
126 clockTicks :: Int
127 clockTicks =
128 #if defined(CLK_TCK)
129     (#const CLK_TCK)
130 #else
131     unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
132 foreign import ccall unsafe sysconf :: CInt -> IO CLong
133 #endif
134 #endif /* __GLASGOW_HASKELL__ */