Implement CPUTime.getCPUTime, CPUTime.cpuTimePrecision.
,unsafeInterleaveIO,nh_write,primCharToInt,
nullAddr, incAddr, isNullAddr,
nh_filesize, nh_iseof, nh_system, nh_exitwith, nh_getPID,
+ nh_getCPUtime, nh_getCPUprec,
Word,
primGtWord, primGeWord, primEqWord, primNeWord,
foreign import "nHandle" "nh_exitwith" nh_exitwith :: Int -> IO ()
foreign import "nHandle" "nh_getPID" nh_getPID :: IO Int
+foreign import "nHandle" "nh_getCPUtime" nh_getCPUtime :: IO Double
+foreign import "nHandle" "nh_getCPUprec" nh_getCPUprec :: IO Double
+
copy_String_to_cstring :: String -> IO Addr
copy_String_to_cstring s
= nh_malloc (1 + length s) >>= \ptr0 ->
#include <malloc.h>
#include <stdlib.h>
#include <ctype.h>
+#include <sys/times.h>
+#include <sys/resource.h>
#include <sys/stat.h>
+#include <time.h>
#include <unistd.h>
+double nh_getCPUtime ( void )
+{
+ double usertime;
+ struct rusage usage;
+ getrusage ( RUSAGE_SELF, &usage );
+ usertime = (double)usage.ru_utime.tv_sec +
+ (double)usage.ru_utime.tv_usec / 1000000.0;
+ return usertime;
+}
+
+double nh_getCPUprec ( void )
+{
+ /* or perhaps CLOCKS_PER_SEC ? */
+ return 1.0 / (double)(CLK_TCK);
+}
+
int nh_getPID ( void )
{
return (int) getpid();
,unsafeInterleaveIO,nh_write,primCharToInt,
nullAddr, incAddr, isNullAddr,
nh_filesize, nh_iseof, nh_system, nh_exitwith, nh_getPID,
+ nh_getCPUtime, nh_getCPUprec,
Word,
primGtWord, primGeWord, primEqWord, primNeWord,
foreign import "nHandle" "nh_exitwith" nh_exitwith :: Int -> IO ()
foreign import "nHandle" "nh_getPID" nh_getPID :: IO Int
+foreign import "nHandle" "nh_getCPUtime" nh_getCPUtime :: IO Double
+foreign import "nHandle" "nh_getCPUprec" nh_getCPUprec :: IO Double
+
copy_String_to_cstring :: String -> IO Addr
copy_String_to_cstring s
= nh_malloc (1 + length s) >>= \ptr0 ->
#ifndef __HUGS__
+
\begin{code}
import PrelBase
import PrelArr ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
integral number of picoseconds.
\begin{code}
-#ifdef __HUGS__
-
-sizeof_int :: Int
-sizeof_int = 4
-
-getCPUTime :: IO Integer
-getCPUTime = do
- marr <- primNewByteArray (sizeof_int * 4)
- rc <- primGetCPUTime marr
- if rc /= 0 then do
- x0 <- primReadIntArray marr 0
- x1 <- primReadIntArray marr 1
- x2 <- primReadIntArray marr 2
- x3 <- primReadIntArray marr 3
- return ((fromIntegral x0 * 1000000000 + fromIntegral x1 +
- fromIntegral x2 * 1000000000 + fromIntegral x3)
- * 1000)
- else
- ioError (IOError Nothing UnsupportedOperation
- "getCPUTime"
- "can't get CPU time")
-
-#else
-
getCPUTime :: IO Integer
getCPUTime = do
marr <- stToIO (newIntArray ((0::Int),3))
ioError (IOError Nothing UnsupportedOperation
"getCPUTime"
"can't get CPU time")
-#endif
cpuTimePrecision :: Integer
cpuTimePrecision = round ((1000000000000::Integer) %
fromInt (unsafePerformIO clockTicks))
-\end{code}
-\begin{code}
foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
#else
\begin{code}
--- TODO: Hugs/getCPUTime
getCPUTime :: IO Integer
-getCPUTime = return 0
+getCPUTime
+ = do seconds <- nh_getCPUtime
+ return (round (seconds * 1.0e+12))
--- TODO: Hugs/cpuTimePrecision
cpuTimePrecision :: Integer
-cpuTimePrecision = 1
+cpuTimePrecision
+ = primRunST (
+ do resolution <- nh_getCPUprec
+ return (round (resolution * 1.0e+12))
+ )
\end{code}
#endif