2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1997
4 \section[CPUTime]{Haskell 1.4 CPU Time Library}
7 {-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
11 getCPUTime, -- :: IO Integer
12 cpuTimePrecision -- :: Integer
19 import PrelArr ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
31 #define cat2(x,y) x/**/y
32 #define CCALL(fun) cat2(prim_,fun)
34 #define sizeof_int64 8
36 #define CCALL(fun) _ccall_ fun
37 #define const_BUFSIZ ``BUFSIZ''
38 #define primPackString
43 Computation @getCPUTime@ returns the number of picoseconds CPU time
44 used by the current program. The precision of this result is
45 implementation-dependent.
47 The @cpuTimePrecision@ constant is the smallest measurable difference
48 in CPU time that the implementation can record, and is given as an
49 integral number of picoseconds.
54 getCPUTime :: IO Integer
56 marr <- primNewByteArray (sizeof_int * 4)
57 ptr <- CCALL(getCPUTime) marr
58 if (ptr /= nullAddr) then do
59 x0 <- primReadIntArray marr 0
60 x1 <- primReadIntArray marr 1
61 x2 <- primReadIntArray marr 2
62 x3 <- primReadIntArray marr 3
63 return ((fromIntegral x0 * 1000000000 + fromIntegral x1 +
64 fromIntegral x2 * 1000000000 + fromIntegral x3)
67 ioError (IOError Nothing UnsupportedOperation
73 getCPUTime :: IO Integer
75 stToIO (newIntArray ((0::Int),3)) >>= \ marr ->
76 stToIO (unsafeFreezeByteArray marr) >>= \ barr@(ByteArray _ frozen#) ->
77 _ccall_ getCPUTime barr >>= \ ptr ->
78 if (ptr::Addr) /= ``NULL'' then
79 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
80 fromIntegral (I# (indexIntArray# frozen# 1#)) +
81 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
82 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
84 ioError (IOError Nothing UnsupportedOperation
90 cpuTimePrecision :: Integer
91 cpuTimePrecision = round ((1000000000000::Integer) %
92 fromInt (unsafePerformIO (CCALL(clockTicks) )))
100 foreign import stdcall "libHS_cbits.so" "getCPUTime" prim_getCPUTime :: Bytes -> IO Addr
101 foreign import stdcall "libHS_cbits.so" "clockTicks" prim_clockTicks :: IO Int