[project @ 2002-08-30 13:27:42 by stolz]
[ghc-base.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 Foreign
24 import Foreign.C
25
26 import Data.Ratio
27
28 #include "HsBase.h"
29
30 -- -----------------------------------------------------------------------------
31 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
32 -- used by the current program.  The precision of this result is
33 -- implementation-dependent.
34
35 getCPUTime :: IO Integer
36 getCPUTime = do
37
38 #if !defined(mingw32_TARGET_OS) && !defined(cygwin32_TARGET_OS)
39 -- getrusage() is right royal pain to deal with when targetting multiple
40 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
41 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
42 -- again in libucb in 2.6..)
43 --
44 -- Avoid the problem by resorting to times() instead.
45 --
46 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS && ! solaris2_TARGET_OS
47     allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
48     getrusage (#const RUSAGE_SELF) p_rusage
49
50     let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
51     let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
52     u_sec  <- (#peek struct timeval,tv_sec)  ru_utime :: IO CTime
53     u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
54     s_sec  <- (#peek struct timeval,tv_sec)  ru_stime :: IO CTime
55     s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
56
57     return ((fromIntegral u_sec * 1000000 + fromIntegral u_usec + 
58              fromIntegral s_sec * 1000000 + fromIntegral s_usec) 
59                 * 1000000)
60
61 type CRUsage = ()
62 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
63 #else
64 # if defined(HAVE_TIMES)
65     allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
66     times p_tms
67     u_ticks  <- (#peek struct tms,tms_utime) p_tms :: IO CClock
68     s_ticks  <- (#peek struct tms,tms_stime) p_tms :: IO CClock
69     return (( (fromIntegral u_ticks + fromIntegral s_ticks) * 1000000000000) 
70                         `div` fromIntegral clockTicks)
71
72 type CTms = ()
73 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
74 # else
75     ioException (IOError Nothing UnsupportedOperation 
76                          "getCPUTime"
77                          "can't get CPU time"
78                          Nothing)
79 # endif
80 #endif
81
82 #else /* win32 */
83     allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
84     allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
85     allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
86     allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
87     pid <- getCurrentProcess
88     ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
89     if toBool ok then do
90       ut <- ft2psecs p_userTime
91       kt <- ft2psecs p_kernelTime
92       return (fromIntegral (ut + kt))
93      else return 0
94   where ft2psecs ft = do
95           high <- (#peek FILETIME,dwHighDateTime) ft :: IO CLong
96           low <- (#peek FILETIME,dwLowDateTime) ft :: IO CLong
97           return (((fromIntegral high) * (2^32) + (fromIntegral low)) * 100000)
98
99     -- ToDo: pin down elapsed times to just the OS thread(s) that
100     -- are evaluating/managing Haskell code.
101
102 type FILETIME = ()
103 type HANDLE = ()
104 -- need proper Haskell names (initial lower-case character)
105 foreign import ccall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
106 foreign import ccall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
107
108 #endif /* not _WIN32 */
109
110 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
111 -- in CPU time that the implementation can record, and is given as an
112 -- integral number of picoseconds.
113
114 cpuTimePrecision :: Integer
115 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
116
117 clockTicks :: Int
118 clockTicks =
119 #if defined(CLK_TCK)
120     (#const CLK_TCK)
121 #else
122     unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
123 foreign import ccall unsafe sysconf :: CInt -> IO CLong
124 #endif