clock 0.4.1.3 → 0.4.2.0
raw patch · 7 files changed
+353/−360 lines, 7 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ System.Clock: diffTimeSpec :: TimeSpec -> TimeSpec -> TimeSpec
+ System.Clock: instance Num TimeSpec
- System.Clock: TimeSpec :: {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> TimeSpec
+ System.Clock: TimeSpec :: {-# UNPACK #-} !Int64 -> {-# UNPACK #-} !Int64 -> TimeSpec
- System.Clock: nsec :: TimeSpec -> {-# UNPACK #-} !Int
+ System.Clock: nsec :: TimeSpec -> {-# UNPACK #-} !Int64
- System.Clock: sec :: TimeSpec -> {-# UNPACK #-} !Int
+ System.Clock: sec :: TimeSpec -> {-# UNPACK #-} !Int64
Files
- System/Clock.hs +0/−112
- System/Clock.hsc +200/−0
- cbits/hs_clock_darwin.c +29/−0
- cbits/hs_clock_win32.c +108/−0
- clock.cabal +16/−8
- csec/clock.c +0/−231
- csec/clock.h +0/−9
− System/Clock.hs
@@ -1,112 +0,0 @@--- | High-resolution, realtime clock and timer functions for Posix--- systems. This module is being developed according to IEEE Std--- 1003.1-2008: <http://www.opengroup.org/onlinepubs/9699919799/>,--- <http://www.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html#>--module System.Clock (-- Clock (Monotonic, Realtime, ProcessCPUTime, ThreadCPUTime),- TimeSpec (TimeSpec),-- getTime,- getRes,-- sec,- nsec--) where--import Foreign.Ptr-import Foreign.Storable-import Foreign.Marshal.Alloc-import Control.Applicative-import Data.Typeable (Typeable)-import GHC.Generics (Generic)---- | Clock types.--- A clock may be system-wide (that is, visible to all processes)--- or per-process (measuring time that is meaningful only within--- a process). All implementations shall support CLOCK_REALTIME.-data Clock = Monotonic -- ^ The identifier for the system-wide monotonic clock, which is defined as a clock measuring real time, whose value cannot be set via clock_settime and which cannot have negative clock jumps. The maximum possible clock jump shall be implementation-defined. For this clock, the value returned by 'getTime' represents the amount of time (in seconds and nanoseconds) since an unspecified point in the past (for example, system start-up time, or the Epoch). This point does not change after system start-up time. Note that the absolute value of the monotonic clock is meaningless (because its origin is arbitrary), and thus there is no need to set it. Furthermore, realtime applications can rely on the fact that the value of this clock is never set.- | Realtime -- ^ The identifier of the system-wide clock measuring real time. For this clock, the value returned by getTime represents the amount of time (in seconds and nanoseconds) since the Epoch.- | ProcessCPUTime -- ^ The identifier of the CPU-time clock associated with the calling process. For this clock, the value returned by getTime represents the amount of execution time of the current process.- | ThreadCPUTime -- ^ The identifier of the CPU-time clock associated with the calling OS thread. For this clock, the value returned by getTime represents the amount of execution time of the current OS thread.- deriving (Eq, Enum, Generic, Read, Show, Typeable)---- | TimeSpec structure-data TimeSpec = TimeSpec { sec :: {-# UNPACK #-} !Int, -- ^ seconds- nsec :: {-# UNPACK #-} !Int -- ^ nanoseconds- } deriving (Eq, Generic, Read, Show, Typeable)--instance Storable TimeSpec where- sizeOf _ = sizeOf (0 :: Int) * 2- alignment _ = 1- poke t v = do- let i :: Ptr Int = castPtr t- i |^ 0 $! sec v- i |^ 1 $! nsec v- peek t = do- let i :: Ptr Int = castPtr t- TimeSpec <$> i |. 0 <*> i |. 1--instance Ord TimeSpec where- compare (TimeSpec xs xn) (TimeSpec ys yn) = - if xs > ys then GT- else if xs < ys then LT- else if xn > yn then GT- else if xn < yn then LT- else EQ---- | The 'getTime' function shall return the current value for the--- specified clock.-getTime :: Clock -> IO TimeSpec-getTime = call . time---- | The 'getRes' function shall return the resolution of any clock.--- Clock resolutions are implementation-defined and cannot be set--- by a process.-getRes :: Clock -> IO TimeSpec-getRes = call . res--------------------------------------------------- Reader function-type ReaderFunc = Ptr TimeSpec -> IO ()---- Readers-foreign import ccall clock_readtime_monotonic :: ReaderFunc-foreign import ccall clock_readtime_realtime :: ReaderFunc-foreign import ccall clock_readtime_processtime :: ReaderFunc-foreign import ccall clock_readtime_threadtime :: ReaderFunc--foreign import ccall clock_readres_monotonic :: ReaderFunc-foreign import ccall clock_readres_realtime :: ReaderFunc-foreign import ccall clock_readres_processtime :: ReaderFunc-foreign import ccall clock_readres_threadtime :: ReaderFunc---- Clock-to-time reading-time :: Clock -> ReaderFunc-time Monotonic = clock_readtime_monotonic-time Realtime = clock_readtime_realtime-time ProcessCPUTime = clock_readtime_processtime-time ThreadCPUTime = clock_readtime_threadtime---- Clock-to-res reading-res :: Clock -> ReaderFunc-res Monotonic = clock_readres_monotonic-res Realtime = clock_readres_realtime-res ProcessCPUTime = clock_readres_processtime-res ThreadCPUTime = clock_readres_threadtime---- Marshalling-call :: ReaderFunc -> IO TimeSpec-call read_ = do- x <- malloc- read_ x- t <- peek x- free x- return t---- Allocation and pointer operations-{-# INLINE (|.) #-}; (|.)::Storable a=>Ptr a -> Int -> IO a ; (|.) a i = peekElemOff a i-{-# INLINE (|^) #-}; (|^)::Storable a=>Ptr a -> Int -> a -> IO (); (|^) a i v = pokeElemOff a i v
+ System/Clock.hsc view
@@ -0,0 +1,200 @@+-- | High-resolution, realtime clock and timer functions for Posix+-- systems. This module is being developed according to IEEE Std+-- 1003.1-2008: <http://www.opengroup.org/onlinepubs/9699919799/>,+-- <http://www.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html#>++{-# OPTIONS_GHC -fno-warn-type-defaults #-}++module System.Clock+ ( Clock(..)+ , TimeSpec(..)+ , getTime+ , getRes+ , diffTimeSpec+ ) where++import Control.Applicative+import Data.Int+import Data.Typeable (Typeable)+import Foreign.Ptr+import Foreign.Storable+import Foreign.Marshal.Alloc+import GHC.Generics (Generic)++#if defined(_WIN32)+# include "hs_clock_win32.c"+#elif defined(__MACH__)+# include "hs_clock_darwin.c"+#else+# include <time.h>+-- Due to missing define in FreeBSD 9.0 and 9.1+-- (http://lists.freebsd.org/pipermail/freebsd-stable/2013-September/075095.html).+# ifndef CLOCK_PROCESS_CPUTIME_ID+# define CLOCK_PROCESS_CPUTIME_ID 15+# endif+#endif++#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)++-- | Clock types. A clock may be system-wide (that is, visible to all processes)+-- or per-process (measuring time that is meaningful only within a process).+-- All implementations shall support CLOCK_REALTIME.+data Clock+ -- | The identifier for the system-wide monotonic clock, which is defined as+ -- a clock measuring real time, whose value cannot be set via+ -- @clock_settime@ and which cannot have negative clock jumps. The maximum+ -- possible clock jump shall be implementation defined. For this clock,+ -- the value returned by 'getTime' represents the amount of time (in+ -- seconds and nanoseconds) since an unspecified point in the past (for+ -- example, system start-up time, or the Epoch). This point does not+ -- change after system start-up time. Note that the absolute value of the+ -- monotonic clock is meaningless (because its origin is arbitrary), and+ -- thus there is no need to set it. Furthermore, realtime applications can+ -- rely on the fact that the value of this clock is never set.+ = Monotonic+ -- | The identifier of the system-wide clock measuring real time. For this+ -- clock, the value returned by getTime represents the amount of time (in+ -- seconds and nanoseconds) since the Epoch.+ | Realtime+ -- | The identifier of the CPU-time clock associated with the calling+ -- process. For this clock, the value returned by getTime represents the+ -- amount of execution time of the current process.+ | ProcessCPUTime+ -- | The identifier of the CPU-time clock associated with the calling OS+ -- thread. For this clock, the value returned by getTime represents the+ -- amount of execution time of the current OS thread.+ | ThreadCPUTime+ deriving (Eq, Enum, Generic, Read, Show, Typeable)++#if defined(_WIN32)+foreign import ccall hs_clock_win32_gettime_monotonic :: Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_win32_gettime_realtime :: Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_win32_gettime_processtime :: Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_win32_gettime_threadtime :: Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_win32_getres_monotonic :: Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_win32_getres_realtime :: Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_win32_getres_processtime :: Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_win32_getres_threadtime :: Ptr TimeSpec -> IO ()+#elif defined(__MACH__)+foreign import ccall hs_clock_darwin_gettime :: #{type clock_id_t} -> Ptr TimeSpec -> IO ()+foreign import ccall hs_clock_darwin_getres :: #{type clock_id_t} -> Ptr TimeSpec -> IO ()+#else+foreign import ccall clock_gettime :: #{type clockid_t} -> Ptr TimeSpec -> IO ()+foreign import ccall clock_getres :: #{type clockid_t} -> Ptr TimeSpec -> IO ()+#endif++#if defined(_WIN32)+#elif defined(__MACH__)+clockToConst :: Clock -> #{type clock_id_t}+clockToConst Monotonic = #const SYSTEM_CLOCK+clockToConst Realtime = #const CALENDAR_CLOCK+clockToConst ProcessCPUTime = #const SYSTEM_CLOCK+clockToConst ThreadCPUTime = #const SYSTEM_CLOCK+#else+clockToConst :: Clock -> #{type clockid_t}+clockToConst Monotonic = #const CLOCK_MONOTONIC+clockToConst Realtime = #const CLOCK_REALTIME+clockToConst ProcessCPUTime = #const CLOCK_PROCESS_CPUTIME_ID+clockToConst ThreadCPUTime = #const CLOCK_THREAD_CPUTIME_ID+#endif++allocaAndPeek :: Storable a => (Ptr a -> IO ()) -> IO a+allocaAndPeek f = alloca $ \ptr -> f ptr >> peek ptr++-- | The 'getTime' function shall return the current value for the+-- specified clock.+getTime :: Clock -> IO TimeSpec++-- | The 'getRes' function shall return the resolution of any clock.+-- Clock resolutions are implementation-defined and cannot be set+-- by a process.+getRes :: Clock -> IO TimeSpec++#if defined(_WIN32)+getTime Monotonic = allocaAndPeek hs_clock_win32_gettime_monotonic+getTime Realtime = allocaAndPeek hs_clock_win32_gettime_realtime+getTime ProcessCPUTime = allocaAndPeek hs_clock_win32_gettime_processtime+getTime ThreadCPUTime = allocaAndPeek hs_clock_win32_gettime_threadtime+#elif defined(__MACH__)+getTime clk = allocaAndPeek $ hs_clock_darwin_gettime $ clockToConst clk+#else+getTime clk = allocaAndPeek $ clock_gettime $ clockToConst clk+#endif++#if defined(_WIN32)+getRes Monotonic = allocaAndPeek hs_clock_win32_getres_monotonic+getRes Realtime = allocaAndPeek hs_clock_win32_getres_realtime+getRes ProcessCPUTime = allocaAndPeek hs_clock_win32_getres_processtime+getRes ThreadCPUTime = allocaAndPeek hs_clock_win32_getres_threadtime+#elif defined(__MACH__)+getRes clk = allocaAndPeek $ hs_clock_darwin_getres $ clockToConst clk+#else+getRes clk = allocaAndPeek $ clock_getres $ clockToConst clk+#endif++-- | TimeSpec structure+data TimeSpec = TimeSpec+ { sec :: {-# UNPACK #-} !Int64 -- ^ seconds+ , nsec :: {-# UNPACK #-} !Int64 -- ^ nanoseconds+ } deriving (Eq, Generic, Read, Show, Typeable)++#if defined(_WIN32)+instance Storable TimeSpec where+ sizeOf _ = sizeOf (undefined :: Int64) * 2+ alignment _ = alignment (undefined :: Int64)+ poke ptr ts = do+ pokeByteOff ptr 0 (sec ts)+ pokeByteOff ptr (sizeOf (undefined :: Int64)) (nsec ts)+ peek ptr = do+ TimeSpec+ <$> peekByteOff ptr 0+ <*> peekByteOff ptr (sizeOf (undefined :: Int64))+#else+instance Storable TimeSpec where+ sizeOf _ = #{size struct timespec}+ alignment _ = #{alignment struct timespec}+ poke ptr ts = do+ let xs :: #{type time_t} = fromIntegral $ sec ts+ xn :: #{type long} = fromIntegral $ nsec ts+ #{poke struct timespec, tv_sec} ptr (xs)+ #{poke struct timespec, tv_nsec} ptr (xn)+ peek ptr = do+ xs :: #{type time_t} <- #{peek struct timespec, tv_sec} ptr+ xn :: #{type long} <- #{peek struct timespec, tv_nsec} ptr+ return $ TimeSpec (fromIntegral xs) (fromIntegral xn)+#endif++normalize :: TimeSpec -> TimeSpec+normalize (TimeSpec xs xn) =+ let (q, r) = xn `divMod` (10^9)+ in TimeSpec (xs + q) r++instance Num TimeSpec where+ (TimeSpec xs xn) + (TimeSpec ys yn) =+ normalize $ TimeSpec (xs + ys) (xn + yn)+ (TimeSpec xs xn) - (TimeSpec ys yn) =+ normalize $ TimeSpec (xs - ys) (xn - yn)+ (TimeSpec xs xn) * (TimeSpec ys yn) =+ normalize $ TimeSpec (xs * ys) (xn * yn)+ negate (TimeSpec xs xn) =+ normalize $ TimeSpec (negate xs) (negate xn)+ abs (TimeSpec xs xn) =+ normalize $ TimeSpec (abs xs) (signum xs * xn)+ signum (normalize -> TimeSpec xs yn)+ | signum xs == 0 = TimeSpec 0 (signum yn)+ | otherwise = TimeSpec 0 (signum xs)+ fromInteger x =+ -- For range, compute div, mod over integers, not any bounded type.+ let (q, r) = x `divMod` (10^9)+ in TimeSpec (fromInteger q) (fromInteger r)++instance Ord TimeSpec where+ compare (TimeSpec xs xn) (TimeSpec ys yn)+ | EQ == ordering = compare xn yn+ | otherwise = ordering+ where+ ordering = compare xs ys++-- | Compute the absolute difference.+diffTimeSpec :: TimeSpec -> TimeSpec -> TimeSpec+diffTimeSpec ts1 ts2 = abs (ts1 - ts2)
+ cbits/hs_clock_darwin.c view
@@ -0,0 +1,29 @@+#ifdef __MACH__+#include <time.h>+#include <mach/clock.h>+#include <mach/mach.h>++void hs_clock_darwin_gettime(clock_id_t clock, struct timespec *ts)+{+ // OS X does not have clock_gettime, use clock_get_time+ // see http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx+ clock_serv_t cclock;+ mach_timespec_t mts;+ host_get_clock_service(mach_host_self(), clock, &cclock);+ clock_get_time(cclock, &mts);+ mach_port_deallocate(mach_task_self(), cclock);+ ts->tv_sec = mts.tv_sec;+ ts->tv_nsec = mts.tv_nsec;+}++void hs_clock_darwin_getres(clock_id_t clock, struct timespec *ts)+{+ clock_serv_t cclock;+ int nsecs;+ mach_msg_type_number_t count;+ host_get_clock_service(mach_host_self(), clock, &cclock);+ clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);+ mach_port_deallocate(mach_task_self(), cclock);+}++#endif /* __MACH__ */
+ cbits/hs_clock_win32.c view
@@ -0,0 +1,108 @@+#ifdef _WIN32+#include <windows.h>++#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)+ #define U64(x) x##Ui64+#else+ #define U64(x) x##ULL+#endif++#define DELTA_EPOCH_IN_100NS U64(116444736000000000)++static long ticks_to_nanos(LONGLONG subsecond_time, LONGLONG frequency)+{+ return (long)((1e9 * subsecond_time) / frequency);+}++static ULONGLONG to_quad_100ns(FILETIME ft)+{+ ULARGE_INTEGER li;+ li.LowPart = ft.dwLowDateTime;+ li.HighPart = ft.dwHighDateTime;+ return li.QuadPart;+}++static void to_timespec_from_100ns(ULONGLONG t_100ns, long long *t)+{+ t[0] = (long)(t_100ns / 10000000UL);+ t[1] = 100*(long)(t_100ns % 10000000UL);+}++void hs_clock_win32_gettime_monotonic(long long* t)+{+ LARGE_INTEGER time;+ LARGE_INTEGER frequency;+ QueryPerformanceCounter(&time);+ QueryPerformanceFrequency(&frequency);+ // seconds+ t[0] = time.QuadPart / frequency.QuadPart;+ // nanos =+ t[1] = ticks_to_nanos(time.QuadPart % frequency.QuadPart, frequency.QuadPart);+}++void hs_clock_win32_gettime_realtime(long long* t)+{+ FILETIME ft;+ ULONGLONG tmp;++ GetSystemTimeAsFileTime(&ft);++ tmp = to_quad_100ns(ft);+ tmp -= DELTA_EPOCH_IN_100NS;++ to_timespec_from_100ns(tmp, t);+}++void hs_clock_win32_gettime_processtime(long long* t)+{+ FILETIME creation_time, exit_time, kernel_time, user_time;+ ULONGLONG time;++ GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time);+ // Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117++ time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);+ to_timespec_from_100ns(time, t);+}++void hs_clock_win32_gettime_threadtime(long long* t)+{+ FILETIME creation_time, exit_time, kernel_time, user_time;+ ULONGLONG time;++ GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &kernel_time, &user_time);+ // Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117++ time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);+ to_timespec_from_100ns(time, t);+}++void hs_clock_win32_getres_monotonic(long long* t)+{+ LARGE_INTEGER frequency;+ QueryPerformanceFrequency(&frequency);++ ULONGLONG resolution = U64(1000000000)/frequency.QuadPart;+ t[0] = resolution / U64(1000000000);+ t[1] = resolution % U64(1000000000);+}++void hs_clock_win32_getres_realtime(long long* t)+{+ t[0] = 0;+ t[1] = 100;+}++void hs_clock_win32_getres_processtime(long long* t)+{+ t[0] = 0;+ t[1] = 100;+}++void hs_clock_win32_getres_threadtime(long long* t)+{+ t[0] = 0;+ t[1] = 100;+}++#endif /* _WIN32 */
clock.cabal view
@@ -1,5 +1,5 @@ name: clock-version: 0.4.1.3+version: 0.4.2.0 stability: stable synopsis: High-resolution clock functions: monotonic, realtime, cputime. description: A package for convenient access to high-resolution clock and@@ -15,14 +15,16 @@ . Derived @Generic@, @Typeable@ and other instances for @Clock@ and @TimeSpec@ was contributed by Mathieu Boespflug on 2014-09-17. .- Corrected dependency listing for @GHC < 7.6@ was contributed by Brian McKenna on 2014-09-30, fixed by Mihaly Barasz on 2014-10-02.+ Corrected dependency listing for @GHC < 7.6@ was contributed by Brian McKenna on 2014-09-30. .+ Windows code corrected by Dimitri Sabadie on 2015-02-09.+ . [Version Scheme] Major-@/R/@-ewrite . New-@/F/@-unctionality . @/I/@-mprovementAndBugFixes . @/P/@-ackagingOnly . * @PackagingOnly@ changes are made for quality assurance reasons. -copyright: Copyright © Cetin Sert 2009-2013, Eugene Kirpichov 2010, Finn Espen Gundersen 2013, Gerolf Seitz 2013, Mathieu Boespflug 2014+copyright: Copyright © Cetin Sert 2009-2013, Eugene Kirpichov 2010, Finn Espen Gundersen 2013, Gerolf Seitz 2013, Mathieu Boespflug 2014, Dimitri Sabadie 2015 license: BSD3 license-file: LICENSE author: Cetin Sert <cetin@corsis.eu>, Corsis Research@@ -46,13 +48,19 @@ library if impl (ghc < 7.6)- build-depends: base >= 2 && <= 5, ghc-prim+ build-depends: base >= 4.4 && <= 5, ghc-prim build-depends: base >= 2 && <= 5 exposed-modules: System.Clock- extensions: DeriveGeneric DeriveDataTypeable ForeignFunctionInterface ScopedTypeVariables- c-sources: csec/clock.c- include-dirs: csec- install-includes: clock.h+ extensions: DeriveGeneric+ DeriveDataTypeable+ ForeignFunctionInterface+ ScopedTypeVariables+ ViewPatterns+ if os(darwin)+ c-sources: cbits/hs_clock_darwin.c+ if os(windows)+ c-sources: cbits/hs_clock_win32.c+ include-dirs: cbits ghc-options: -O2 -Wall if flag(llvm)
− csec/clock.c
@@ -1,231 +0,0 @@-#include "clock.h"--#ifdef _WIN32--// ***********************-// ******** WIN32 ********-// ***********************--#include <windows.h>--#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)- #define U64(x) x##Ui64-#else- #define U64(x) x##ULL-#endif--#define DELTA_EPOCH_IN_100NS U64(116444736000000000)--long ticks_to_nanos(LONGLONG subsecond_time, LONGLONG frequency)-{- return (long)((1e9 * subsecond_time) / frequency);-}--ULONGLONG to_quad_100ns(FILETIME ft)-{- ULARGE_INTEGER li;- li.LowPart = ft.dwLowDateTime;- li.HighPart = ft.dwHighDateTime;- return li.QuadPart;-}--void to_timespec_from_100ns(ULONGLONG t_100ns, long *t)-{- t[0] = (long)(t_100ns / 10000000UL);- t[1] = 100*(long)(t_100ns % 10000000UL);-}--void clock_readtime_monotonic(long* t)-{- LARGE_INTEGER time;- LARGE_INTEGER frequency;- QueryPerformanceCounter(&time);- QueryPerformanceFrequency(&frequency);- // seconds- t[0] = time.QuadPart / frequency.QuadPart;- // nanos = - t[1] = ticks_to_nanos(time.QuadPart % frequency.QuadPart, frequency.QuadPart);-}--void clock_readtime_realtime(long* t)-{- FILETIME ft;- ULONGLONG tmp;-- GetSystemTimeAsFileTime(&ft);- - tmp = to_quad_100ns(ft);- tmp -= DELTA_EPOCH_IN_100NS; -- to_timespec_from_100ns(tmp, t);-}--void clock_readtime_processtime(long* t)-{- FILETIME creation_time, exit_time, kernel_time, user_time;- ULONGLONG time;-- GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time);- // Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117-- time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);- to_timespec_from_100ns(time, t);-}--void clock_readtime_threadtime(long* t)-{- FILETIME creation_time, exit_time, kernel_time, user_time;- ULONGLONG time;-- GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &kernel_time, &user_time);- // Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117-- time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);- to_timespec_from_100ns(time, t);-}--void clock_readres_monotonic(long* t)-{- LARGE_INTEGER frequency;- QueryPerformanceFrequency(&frequency);-- ULONGLONG resolution = U64(1000000000)/frequency.QuadPart;- t[0] = resolution / U64(1000000000);- t[1] = resolution % U64(1000000000);-}--void clock_readres_realtime(long* t)-{- t[0] = 0;- t[1] = 100;-}--void clock_readres_processtime(long* t)-{- t[0] = 0;- t[1] = 100;-}--void clock_readres_threadtime(long* t)-{- t[0] = 0;- t[1] = 100;-}--#else // Not _WIN32--// ***********************-// ******** POSIX ********-// ***********************--#include <time.h>-#ifdef __MACH__- #include <mach/clock.h>- #include <mach/mach.h>- #define CLOCK_ID_T clock_id_t- #define CLOCK_MONOTONIC SYSTEM_CLOCK- #define CLOCK_REALTIME CALENDAR_CLOCK- #define CLOCK_PROCESS_CPUTIME_ID SYSTEM_CLOCK- #define CLOCK_THREAD_CPUTIME_ID SYSTEM_CLOCK-#else- #define CLOCK_ID_T clockid_t-#endif--// due to missing define in FreeBSD 9.0 and 9.1 (http://lists.freebsd.org/pipermail/freebsd-stable/2013-September/075095.html)-#ifndef CLOCK_PROCESS_CPUTIME_ID- #define CLOCK_PROCESS_CPUTIME_ID 15-#endif--void time_(CLOCK_ID_T clock, long* t)-{- #ifdef __MACH__- // OS X does not have clock_gettime, use clock_get_time- // see http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx- clock_serv_t cclock;- mach_timespec_t mts;- struct timespec* ts;- host_get_clock_service(mach_host_self(), clock, &cclock);- clock_get_time(cclock, &mts);- mach_port_deallocate(mach_task_self(), cclock);- ts = (struct timespec*)t;- ts->tv_sec = mts.tv_sec;- ts->tv_nsec = mts.tv_nsec;- #else- clock_gettime(clock, (struct timespec*)t);- #endif----/*struct timespec a;-- clock_gettime(clock, &a);-- t[0] = a.tv_sec;- t[1] = a.tv_nsec;*/--}--void clock_readtime_monotonic(long* t)-{- time_(CLOCK_MONOTONIC, t);-}--void clock_readtime_realtime(long* t)-{- time_(CLOCK_REALTIME, t);-}--void clock_readtime_processtime(long* t)-{- time_(CLOCK_PROCESS_CPUTIME_ID , t);-}--void clock_readtime_threadtime(long* t)-{- time_(CLOCK_THREAD_CPUTIME_ID , t);-}---void res_(CLOCK_ID_T clock, long* t)-{- #ifdef __MACH__- clock_serv_t cclock;- int nsecs;- mach_msg_type_number_t count;- host_get_clock_service(mach_host_self(), clock, &cclock);- clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);- mach_port_deallocate(mach_task_self(), cclock);- #else- clock_getres(clock, (struct timespec*)t);- #endif--/*struct timespec a;-- clock_getres(clock, &a);-- t[0] = a.tv_sec;- t[1] = a.tv_nsec;*/--}--void clock_readres_monotonic(long* t)-{- res_(CLOCK_MONOTONIC, t);-}--void clock_readres_realtime(long* t)-{- res_(CLOCK_REALTIME, t);-}--void clock_readres_processtime(long* t)-{- res_(CLOCK_PROCESS_CPUTIME_ID , t);-}--void clock_readres_threadtime(long* t)-{- res_(CLOCK_THREAD_CPUTIME_ID , t);-}--#endif
− csec/clock.h
@@ -1,9 +0,0 @@-void clock_readtime_monotonic(long* t);-void clock_readtime_realtime(long* t);-void clock_readtime_processtime(long* t);-void clock_readtime_threadtime(long* t);--void clock_readres_monotonic(long* t);-void clock_readres_realtime(long* t);-void clock_readres_processtime(long* t);-void clock_readres_threadtime(long* t);