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