module System.CPUTime.Posix.ClockGetTime
( getCPUTime
, getCpuTimePrecision
) where
import Foreign
import Foreign.C
import System.CPUTime.Utils
getCPUTime :: IO Integer
getCPUTime = fmap snd $ withTimespec $ \ts ->
throwErrnoIfMinus1_ "clock_gettime"
$ clock_gettime (2) ts
getCpuTimePrecision :: IO Integer
getCpuTimePrecision = fmap snd $ withTimespec $ \ts ->
throwErrnoIfMinus1_ "clock_getres"
$ clock_getres (2) ts
data Timespec
withTimespec :: (Ptr Timespec -> IO a) -> IO (a, Integer)
withTimespec action =
allocaBytes (16) $ \p_ts -> do
r <- action p_ts
u_sec <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p_ts :: IO CTime
u_nsec <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p_ts :: IO CLong
return (r, cTimeToInteger u_sec * 1e12 + fromIntegral u_nsec * 1e3)
foreign import capi unsafe "time.h clock_getres" clock_getres :: CInt -> Ptr Timespec -> IO CInt
foreign import capi unsafe "time.h clock_gettime" clock_gettime :: CInt -> Ptr Timespec -> IO CInt