Windows: Unicode openFile and stat functions
[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/base/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 __NHC__
30 import CPUTime ( getCPUTime, cpuTimePrecision )
31 #endif
32
33 #ifdef __GLASGOW_HASKELL__
34 import Foreign
35 import Foreign.C
36
37 #include "HsBase.h"
38 #endif
39
40 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
41 realToInteger :: Real a => a -> Integer
42 realToInteger ct = round (realToFrac ct :: Double)
43   -- CTime, CClock, CUShort etc are in Real but not Fractional, 
44   -- so we must convert to Double before we can round it
45 #endif
46
47 #ifdef __GLASGOW_HASKELL__
48 -- -----------------------------------------------------------------------------
49 -- |Computation 'getCPUTime' returns the number of picoseconds CPU time
50 -- used by the current program.  The precision of this result is
51 -- implementation-dependent.
52
53 getCPUTime :: IO Integer
54 getCPUTime = do
55
56 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
57 -- getrusage() is right royal pain to deal with when targetting multiple
58 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
59 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
60 -- again in libucb in 2.6..)
61 --
62 -- Avoid the problem by resorting to times() instead.
63 --
64 #if defined(HAVE_GETRUSAGE) && ! irix_HOST_OS && ! solaris2_HOST_OS
65     allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
66     getrusage (#const RUSAGE_SELF) p_rusage
67
68     let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
69     let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
70     u_sec  <- (#peek struct timeval,tv_sec)  ru_utime :: IO CTime
71     u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CTime
72     s_sec  <- (#peek struct timeval,tv_sec)  ru_stime :: IO CTime
73     s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CTime
74     return ((realToInteger u_sec * 1000000 + realToInteger u_usec + 
75              realToInteger s_sec * 1000000 + realToInteger s_usec) 
76                 * 1000000)
77
78 type CRUsage = ()
79 foreign import ccall unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
80 #else
81 # if defined(HAVE_TIMES)
82     allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
83     times p_tms
84     u_ticks  <- (#peek struct tms,tms_utime) p_tms :: IO CClock
85     s_ticks  <- (#peek struct tms,tms_stime) p_tms :: IO CClock
86     return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000) 
87                         `div` fromIntegral clockTicks)
88
89 type CTms = ()
90 foreign import ccall unsafe times :: Ptr CTms -> IO CClock
91 # else
92     ioException (IOError Nothing UnsupportedOperation 
93                          "getCPUTime"
94                          "can't get CPU time"
95                          Nothing)
96 # endif
97 #endif
98
99 #else /* win32 */
100      -- NOTE: GetProcessTimes() is only supported on NT-based OSes.
101      -- The counts reported by GetProcessTimes() are in 100-ns (10^-7) units.
102     allocaBytes (#const sizeof(FILETIME)) $ \ p_creationTime -> do
103     allocaBytes (#const sizeof(FILETIME)) $ \ p_exitTime -> do
104     allocaBytes (#const sizeof(FILETIME)) $ \ p_kernelTime -> do
105     allocaBytes (#const sizeof(FILETIME)) $ \ p_userTime -> do
106     pid <- getCurrentProcess
107     ok <- getProcessTimes pid p_creationTime p_exitTime p_kernelTime p_userTime
108     if toBool ok then do
109       ut <- ft2psecs p_userTime
110       kt <- ft2psecs p_kernelTime
111       return (ut + kt)
112      else return 0
113   where 
114         ft2psecs :: Ptr FILETIME -> IO Integer
115         ft2psecs ft = do
116           high <- (#peek FILETIME,dwHighDateTime) ft :: IO Word32
117           low  <- (#peek FILETIME,dwLowDateTime)  ft :: IO Word32
118             -- Convert 100-ns units to picosecs (10^-12) 
119             -- => multiply by 10^5.
120           return (((fromIntegral high) * (2^(32::Int)) + (fromIntegral low)) * 100000)
121
122     -- ToDo: pin down elapsed times to just the OS thread(s) that
123     -- are evaluating/managing Haskell code.
124
125 type FILETIME = ()
126 type HANDLE = ()
127 -- need proper Haskell names (initial lower-case character)
128 foreign import stdcall unsafe "GetCurrentProcess" getCurrentProcess :: IO (Ptr HANDLE)
129 foreign import stdcall unsafe "GetProcessTimes" getProcessTimes :: Ptr HANDLE -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> Ptr FILETIME -> IO CInt
130
131 #endif /* not _WIN32 */
132 #endif /* __GLASGOW_HASKELL__ */
133
134 -- |The 'cpuTimePrecision' constant is the smallest measurable difference
135 -- in CPU time that the implementation can record, and is given as an
136 -- integral number of picoseconds.
137
138 #ifndef __NHC__
139 cpuTimePrecision :: Integer
140 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
141 #endif
142
143 #ifdef __GLASGOW_HASKELL__
144 clockTicks :: Int
145 clockTicks =
146 #if defined(CLK_TCK)
147     (#const CLK_TCK)
148 #else
149     unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
150 foreign import ccall unsafe sysconf :: CInt -> IO CLong
151 #endif
152 #endif /* __GLASGOW_HASKELL__ */