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 )
28 import PrelNum ( Num(..), Integral(..) ) -- To get fromInt/toInt
32 #define cat2(x,y) x/**/y
33 #define CCALL(fun) cat2(prim_,fun)
35 #define sizeof_int64 8
37 #define CCALL(fun) _ccall_ fun
38 #define const_BUFSIZ ``BUFSIZ''
39 #define primPackString
44 Computation @getCPUTime@ returns the number of picoseconds CPU time
45 used by the current program. The precision of this result is
46 implementation-dependent.
48 The @cpuTimePrecision@ constant is the smallest measurable difference
49 in CPU time that the implementation can record, and is given as an
50 integral number of picoseconds.
55 getCPUTime :: IO Integer
57 marr <- primNewByteArray (sizeof_int * 4)
58 ptr <- CCALL(getCPUTime) marr
59 if (ptr /= nullAddr) then do
60 x0 <- primReadIntArray marr 0
61 x1 <- primReadIntArray marr 1
62 x2 <- primReadIntArray marr 2
63 x3 <- primReadIntArray marr 3
64 return ((fromIntegral x0 * 1000000000 + fromIntegral x1 +
65 fromIntegral x2 * 1000000000 + fromIntegral x3)
68 ioError (IOError Nothing UnsupportedOperation
74 getCPUTime :: IO Integer
76 stToIO (newIntArray ((0::Int),3)) >>= \ marr ->
77 stToIO (unsafeFreezeByteArray marr) >>= \ barr@(ByteArray _ frozen#) ->
78 _ccall_ getCPUTime barr >>= \ ptr ->
79 if (ptr::Addr) /= ``NULL'' then
80 return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 +
81 fromIntegral (I# (indexIntArray# frozen# 1#)) +
82 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 +
83 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
85 ioError (IOError Nothing UnsupportedOperation
91 cpuTimePrecision :: Integer
92 cpuTimePrecision = round ((1000000000000::Integer) %
93 fromInt (unsafePerformIO (CCALL(clockTicks) )))
101 foreign import stdcall "libHS_cbits.so" "getCPUTime" prim_getCPUTime :: Bytes -> IO Addr
102 foreign import stdcall "libHS_cbits.so" "clockTicks" prim_clockTicks :: IO Int