{-# LINE 1 "libraries/base/System/CPUTime.hsc" #-} {-# LANGUAGE Trustworthy #-} {-# LINE 2 "libraries/base/System/CPUTime.hsc" #-} {-# LANGUAGE CPP, NondecreasingIndentation, ForeignFunctionInterface, CApiFFI #-} ----------------------------------------------------------------------------- -- | -- Module : System.CPUTime -- Copyright : (c) The University of Glasgow 2001 -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : provisional -- Portability : portable -- -- The standard CPUTime library. -- ----------------------------------------------------------------------------- {-# LINE 19 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 20 "libraries/base/System/CPUTime.hsc" #-} module System.CPUTime ( getCPUTime, -- :: IO Integer cpuTimePrecision -- :: Integer ) where import Prelude import Data.Ratio {-# LINE 34 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 38 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 40 "libraries/base/System/CPUTime.hsc" #-} import Foreign.Safe import Foreign.C import System.IO.Unsafe (unsafePerformIO) -- For _SC_CLK_TCK {-# LINE 46 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 47 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 48 "libraries/base/System/CPUTime.hsc" #-} -- For struct rusage {-# LINE 51 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 52 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 53 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 54 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 55 "libraries/base/System/CPUTime.hsc" #-} -- For FILETIME etc. on Windows {-# LINE 60 "libraries/base/System/CPUTime.hsc" #-} -- for CLK_TCK {-# LINE 63 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 64 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 65 "libraries/base/System/CPUTime.hsc" #-} -- for struct tms {-# LINE 68 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 69 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 70 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 72 "libraries/base/System/CPUTime.hsc" #-} #ifdef mingw32_HOST_OS # if defined(i386_HOST_ARCH) # define WINDOWS_CCONV stdcall # elif defined(x86_64_HOST_ARCH) # define WINDOWS_CCONV ccall # else # error Unknown mingw32 arch # endif #else #endif {-# LINE 85 "libraries/base/System/CPUTime.hsc" #-} realToInteger :: Real a => a -> Integer realToInteger ct = round (realToFrac ct :: Double) -- CTime, CClock, CUShort etc are in Real but not Fractional, -- so we must convert to Double before we can round it {-# LINE 90 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 92 "libraries/base/System/CPUTime.hsc" #-} -- ----------------------------------------------------------------------------- -- |Computation 'getCPUTime' returns the number of picoseconds CPU time -- used by the current program. The precision of this result is -- implementation-dependent. getCPUTime :: IO Integer getCPUTime = do {-# LINE 101 "libraries/base/System/CPUTime.hsc" #-} -- getrusage() is right royal pain to deal with when targetting multiple -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5), -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back -- again in libucb in 2.6..) -- -- Avoid the problem by resorting to times() instead. -- {-# LINE 125 "libraries/base/System/CPUTime.hsc" #-} allocaBytes (32) $ \ p_tms -> do {-# LINE 126 "libraries/base/System/CPUTime.hsc" #-} _ <- times p_tms u_ticks <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p_tms :: IO CClock {-# LINE 128 "libraries/base/System/CPUTime.hsc" #-} s_ticks <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p_tms :: IO CClock {-# LINE 129 "libraries/base/System/CPUTime.hsc" #-} return (( (realToInteger u_ticks + realToInteger s_ticks) * 1000000000000) `div` fromIntegral clockTicks) type CTms = () foreign import ccall unsafe times :: Ptr CTms -> IO CClock {-# LINE 140 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 174 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 175 "libraries/base/System/CPUTime.hsc" #-} -- |The 'cpuTimePrecision' constant is the smallest measurable difference -- in CPU time that the implementation can record, and is given as an -- integral number of picoseconds. {-# LINE 181 "libraries/base/System/CPUTime.hsc" #-} cpuTimePrecision :: Integer cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks)) {-# LINE 184 "libraries/base/System/CPUTime.hsc" #-} {-# LINE 186 "libraries/base/System/CPUTime.hsc" #-} clockTicks :: Int clockTicks = unsafePerformIO (sysconf (3) >>= return . fromIntegral) {-# LINE 189 "libraries/base/System/CPUTime.hsc" #-} foreign import ccall unsafe sysconf :: CInt -> IO CLong {-# LINE 191 "libraries/base/System/CPUTime.hsc" #-}