{-# LINE 1 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
{-# LANGUAGE CPP, CApiFFI, NumDecimals #-}
{-# LINE 6 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
{-# LINE 9 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
module System.CPUTime.Posix.ClockGetTime
( getCPUTime
, getCpuTimePrecision
) where
{-# LINE 16 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
import Foreign
import Foreign.C
import System.CPUTime.Utils
getCPUTime :: IO Integer
getCPUTime :: IO Integer
getCPUTime = (((), Integer) -> Integer) -> IO ((), Integer) -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((), Integer) -> Integer
forall a b. (a, b) -> b
snd (IO ((), Integer) -> IO Integer) -> IO ((), Integer) -> IO Integer
forall a b. (a -> b) -> a -> b
$ (Ptr Timespec -> IO ()) -> IO ((), Integer)
forall a. (Ptr Timespec -> IO a) -> IO (a, Integer)
withTimespec ((Ptr Timespec -> IO ()) -> IO ((), Integer))
-> (Ptr Timespec -> IO ()) -> IO ((), Integer)
forall a b. (a -> b) -> a -> b
$ \ts :: Ptr Timespec
ts ->
String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ "clock_gettime"
(IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Timespec -> IO CInt
clock_gettime (2) Ptr Timespec
ts
{-# LINE 25 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
getCpuTimePrecision :: IO Integer
getCpuTimePrecision :: IO Integer
getCpuTimePrecision = (((), Integer) -> Integer) -> IO ((), Integer) -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((), Integer) -> Integer
forall a b. (a, b) -> b
snd (IO ((), Integer) -> IO Integer) -> IO ((), Integer) -> IO Integer
forall a b. (a -> b) -> a -> b
$ (Ptr Timespec -> IO ()) -> IO ((), Integer)
forall a. (Ptr Timespec -> IO a) -> IO (a, Integer)
withTimespec ((Ptr Timespec -> IO ()) -> IO ((), Integer))
-> (Ptr Timespec -> IO ()) -> IO ((), Integer)
forall a b. (a -> b) -> a -> b
$ \ts :: Ptr Timespec
ts ->
String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ "clock_getres"
(IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr Timespec -> IO CInt
clock_getres (2) Ptr Timespec
ts
{-# LINE 30 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
data Timespec
withTimespec :: (Ptr Timespec -> IO a) -> IO (a, Integer)
withTimespec :: (Ptr Timespec -> IO a) -> IO (a, Integer)
withTimespec action :: Ptr Timespec -> IO a
action =
Int -> (Ptr Timespec -> IO (a, Integer)) -> IO (a, Integer)
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes (16) ((Ptr Timespec -> IO (a, Integer)) -> IO (a, Integer))
-> (Ptr Timespec -> IO (a, Integer)) -> IO (a, Integer)
forall a b. (a -> b) -> a -> b
$ \p_ts :: Ptr Timespec
p_ts -> do
{-# LINE 38 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
a
r <- Ptr Timespec -> IO a
action Ptr Timespec
p_ts
CTime
u_sec <- ((\hsc_ptr :: Ptr Timespec
hsc_ptr -> Ptr Timespec -> Int -> IO CTime
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Timespec
hsc_ptr 0)) Ptr Timespec
p_ts :: IO CTime
{-# LINE 40 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
CLong
u_nsec <- ((\hsc_ptr :: Ptr Timespec
hsc_ptr -> Ptr Timespec -> Int -> IO CLong
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Timespec
hsc_ptr 8)) Ptr Timespec
p_ts :: IO CLong
{-# LINE 41 "System/CPUTime/Posix/ClockGetTime.hsc" #-}
(a, Integer) -> IO (a, Integer)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
r, CTime -> Integer
cTimeToInteger CTime
u_sec Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* 1e12 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ CLong -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral CLong
u_nsec Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* 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
{-# LINE 56 "System/CPUTime/Posix/ClockGetTime.hsc" #-}