summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCheng Shao <astrohavoc@gmail.com>2022-10-24 06:42:26 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-11-11 00:26:55 -0500
commit7f59b0f351d2376eee1cb797338d3b0a987d0d72 (patch)
tree89683b33b1e56eb788c64700ee256cc83864f3d9
parentd7b33982df0dfbb5b66888037dc9baf59c8af339 (diff)
downloadhaskell-7f59b0f351d2376eee1cb797338d3b0a987d0d72.tar.gz
base: fall back to using monotonic clock to emulate cputime on wasm32
On wasm32, we have to fall back to using monotonic clock to emulate cputime, since there's no native support for cputime as a clock id.
-rw-r--r--libraries/base/System/CPUTime/Posix/ClockGetTime.hsc5
-rw-r--r--libraries/base/System/CPUTime/Posix/RUsage.hsc12
-rw-r--r--libraries/base/System/CPUTime/Posix/Times.hsc12
-rw-r--r--libraries/base/configure.ac1
4 files changed, 30 insertions, 0 deletions
diff --git a/libraries/base/System/CPUTime/Posix/ClockGetTime.hsc b/libraries/base/System/CPUTime/Posix/ClockGetTime.hsc
index 259cee8226..22423926e1 100644
--- a/libraries/base/System/CPUTime/Posix/ClockGetTime.hsc
+++ b/libraries/base/System/CPUTime/Posix/ClockGetTime.hsc
@@ -40,7 +40,12 @@ withTimespec action =
foreign import capi unsafe "time.h clock_getres" clock_getres :: CUIntPtr -> Ptr Timespec -> IO CInt
foreign import capi unsafe "time.h clock_gettime" clock_gettime :: CUIntPtr -> Ptr Timespec -> IO CInt
+
+#if HAVE_DECL_CLOCK_PROCESS_CPUTIME_ID
foreign import capi unsafe "time.h value CLOCK_PROCESS_CPUTIME_ID" cLOCK_PROCESS_CPUTIME_ID :: CUIntPtr
+#else
+foreign import capi unsafe "time.h value CLOCK_MONOTONIC" cLOCK_PROCESS_CPUTIME_ID :: CUIntPtr
+#endif // HAVE_DECL_CLOCK_PROCESS_CPUTIME_ID
#else
diff --git a/libraries/base/System/CPUTime/Posix/RUsage.hsc b/libraries/base/System/CPUTime/Posix/RUsage.hsc
index b59422709a..4828a894c1 100644
--- a/libraries/base/System/CPUTime/Posix/RUsage.hsc
+++ b/libraries/base/System/CPUTime/Posix/RUsage.hsc
@@ -18,6 +18,8 @@ import System.CPUTime.Utils
#include <sys/resource.h>
#endif
+#if HAVE_GETRUSAGE
+
getCPUTime :: IO Integer
getCPUTime = allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
throwErrnoIfMinus1_ "getrusage" $ getrusage (#const RUSAGE_SELF) p_rusage
@@ -40,3 +42,13 @@ getCpuTimePrecision =
return $ round ((1e12::Integer) % fromIntegral clk_tck)
foreign import ccall unsafe clk_tck :: CLong
+
+#else
+
+getCPUTime :: IO Integer
+getCPUTime = fail "System.CPUTime.Posix.RUsage.getCPUTime"
+
+getCpuTimePrecision :: IO Integer
+getCpuTimePrecision = fail "System.CPUTime.Posix.RUsage.getCpuTimePrecision"
+
+#endif
diff --git a/libraries/base/System/CPUTime/Posix/Times.hsc b/libraries/base/System/CPUTime/Posix/Times.hsc
index c703863584..12410c40ff 100644
--- a/libraries/base/System/CPUTime/Posix/Times.hsc
+++ b/libraries/base/System/CPUTime/Posix/Times.hsc
@@ -18,6 +18,8 @@ import System.CPUTime.Utils
#include <sys/times.h>
#endif
+#if HAVE_TIMES
+
getCPUTime :: IO Integer
getCPUTime = allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
_ <- times p_tms
@@ -37,3 +39,13 @@ foreign import ccall unsafe clk_tck :: CLong
clockTicks :: Integer
clockTicks = fromIntegral clk_tck
+
+#else
+
+getCPUTime :: IO Integer
+getCPUTime = fail "System.CPUTime.Posix.Times.getCPUTime"
+
+getCpuTimePrecision :: IO Integer
+getCpuTimePrecision = fail "System.CPUTime.Posix.Times.getCpuTimePrecision"
+
+#endif
diff --git a/libraries/base/configure.ac b/libraries/base/configure.ac
index 31bb7e6a0f..8935fd9a9b 100644
--- a/libraries/base/configure.ac
+++ b/libraries/base/configure.ac
@@ -37,6 +37,7 @@ AC_CHECK_HEADERS([wctype.h], [AC_CHECK_FUNCS(iswspace)])
AC_CHECK_FUNCS([lstat])
AC_CHECK_LIB([rt], [clock_gettime])
AC_CHECK_FUNCS([clock_gettime])
+AC_CHECK_DECLS([CLOCK_PROCESS_CPUTIME_ID], [], [], [[#include <time.h>]])
AC_CHECK_FUNCS([getclock getrusage times])
AC_CHECK_FUNCS([_chsize ftruncate])