[project @ 1999-11-22 11:34:09 by sewardj]
authorsewardj <unknown>
Mon, 22 Nov 1999 11:34:11 +0000 (11:34 +0000)
committersewardj <unknown>
Mon, 22 Nov 1999 11:34:11 +0000 (11:34 +0000)
Implement CPUTime.getCPUTime, CPUTime.cpuTimePrecision.

ghc/interpreter/lib/Prelude.hs
ghc/interpreter/nHandle.c
ghc/lib/hugs/Prelude.hs
ghc/lib/std/CPUTime.lhs

index 965fbed..33145a9 100644 (file)
@@ -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 -> 
index 4f2b22a..c68ff3e 100644 (file)
@@ -9,9 +9,28 @@
 #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();
index 965fbed..33145a9 100644 (file)
@@ -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 -> 
index 037460b..e808b2a 100644 (file)
@@ -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