From aa23973457915dd2ce48793624ac543085cbaa52 Mon Sep 17 00:00:00 2001 From: sewardj Date: Mon, 22 Nov 1999 11:34:11 +0000 Subject: [PATCH] [project @ 1999-11-22 11:34:09 by sewardj] Implement CPUTime.getCPUTime, CPUTime.cpuTimePrecision. --- ghc/interpreter/lib/Prelude.hs | 4 ++++ ghc/interpreter/nHandle.c | 19 +++++++++++++++++++ ghc/lib/hugs/Prelude.hs | 4 ++++ ghc/lib/std/CPUTime.lhs | 40 +++++++++------------------------------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/ghc/interpreter/lib/Prelude.hs b/ghc/interpreter/lib/Prelude.hs index 965fbed..33145a9 100644 --- a/ghc/interpreter/lib/Prelude.hs +++ b/ghc/interpreter/lib/Prelude.hs @@ -119,6 +119,7 @@ module Prelude ( ,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, @@ -1739,6 +1740,9 @@ foreign import "nHandle" "nh_system" nh_system :: Addr -> IO Int 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 -> diff --git a/ghc/interpreter/nHandle.c b/ghc/interpreter/nHandle.c index 4f2b22a..c68ff3e 100644 --- a/ghc/interpreter/nHandle.c +++ b/ghc/interpreter/nHandle.c @@ -9,9 +9,28 @@ #include #include #include +#include +#include #include +#include #include +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(); diff --git a/ghc/lib/hugs/Prelude.hs b/ghc/lib/hugs/Prelude.hs index 965fbed..33145a9 100644 --- a/ghc/lib/hugs/Prelude.hs +++ b/ghc/lib/hugs/Prelude.hs @@ -119,6 +119,7 @@ module Prelude ( ,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, @@ -1739,6 +1740,9 @@ foreign import "nHandle" "nh_system" nh_system :: Addr -> IO Int 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 -> diff --git a/ghc/lib/std/CPUTime.lhs b/ghc/lib/std/CPUTime.lhs index 037460b..e808b2a 100644 --- a/ghc/lib/std/CPUTime.lhs +++ b/ghc/lib/std/CPUTime.lhs @@ -15,6 +15,7 @@ module CPUTime #ifndef __HUGS__ + \begin{code} import PrelBase import PrelArr ( ByteArray(..), newIntArray, unsafeFreezeByteArray ) @@ -37,30 +38,6 @@ in CPU time that the implementation can record, and is given as an 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)) @@ -77,14 +54,11 @@ getCPUTime = do 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 @@ -93,12 +67,16 @@ 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 -- 1.7.10.4