1 -- -----------------------------------------------------------------------------
2 -- $Id: CPUTime.hsc,v 1.4 2001/05/21 14:04:15 simonmar Exp $
4 -- (c) The University of Glasgow, 1995-2001
9 getCPUTime, -- :: IO Integer
10 cpuTimePrecision -- :: Integer
13 import PrelMarshalAlloc
19 import PrelBase ( Int(..) )
20 import PrelByteArr ( ByteArray(..), newIntArray )
21 import PrelArrExtra ( unsafeFreezeByteArray )
22 import PrelIOBase ( IOException(..) )
27 -- -----------------------------------------------------------------------------
28 -- Computation `getCPUTime' returns the number of picoseconds CPU time
29 -- used by the current program. The precision of this result is
30 -- implementation-dependent.
32 -- The `cpuTimePrecision' constant is the smallest measurable difference
33 -- in CPU time that the implementation can record, and is given as an
34 -- integral number of picoseconds.
36 getCPUTime :: IO Integer
40 -- getrusage() is right royal pain to deal with when targetting multiple
41 -- versions of Solaris, since some versions supply it in libc (2.3 and 2.5),
42 -- while 2.4 has got it in libucb (I wouldn't be too surprised if it was back
43 -- again in libucb in 2.6..)
45 -- Avoid the problem by resorting to times() instead.
47 #if defined(HAVE_GETRUSAGE) && ! irix_TARGET_OS && ! solaris2_TARGET_OS
48 allocaBytes (#const sizeof(struct rusage)) $ \ p_rusage -> do
49 getrusage (#const RUSAGE_SELF) p_rusage
51 let ru_utime = (#ptr struct rusage, ru_utime) p_rusage
52 let ru_stime = (#ptr struct rusage, ru_stime) p_rusage
53 u_sec <- (#peek struct timeval,tv_sec) ru_utime :: IO CLong
54 u_usec <- (#peek struct timeval,tv_usec) ru_utime :: IO CLong
55 s_sec <- (#peek struct timeval,tv_sec) ru_stime :: IO CLong
56 s_usec <- (#peek struct timeval,tv_usec) ru_stime :: IO CLong
58 return ((fromIntegral u_sec * 1000000 + fromIntegral u_usec +
59 fromIntegral s_sec * 1000000 + fromIntegral s_usec)
63 foreign import unsafe getrusage :: CInt -> Ptr CRUsage -> IO CInt
65 # if defined(HAVE_TIMES)
66 allocaBytes (#const sizeof(struct tms)) $ \ p_tms -> do
68 u_ticks <- (#peek struct tms,tms_utime) p_tms :: IO CClock
69 s_ticks <- (#peek struct tms,tms_stime) p_tms :: IO CClock
70 return (( (fromIntegral u_ticks + fromIntegral s_ticks) * 1000000000000)
74 foreign import unsafe times :: Ptr CTms -> CClock
76 ioException (IOError Nothing UnsupportedOperation
88 /* 100ns units per sec, really */
89 #define NS_PER_SEC 10000000LL
90 #define FT2usecs(ll,ft) \
91 (ll)=(ft).dwHighDateTime; \
93 (ll) |= (ft).dwLowDateTime;
97 /* cygwin32 or mingw32 version */
99 getCPUTime(StgByteArray cpuStruct)
101 FILETIME creationTime, exitTime, kernelTime, userTime;
102 StgInt *cpu=(StgInt *)cpuStruct;
103 unsigned long long uT, kT;
105 /* ToDo: pin down elapsed times to just the OS thread(s) that
106 are evaluating/managing Haskell code.
108 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
109 &exitTime, &kernelTime, &userTime)) {
110 /* Probably on a Win95 box..*/
118 FT2usecs(uT, userTime);
119 FT2usecs(kT, kernelTime);
121 cpu[0] = (unsigned int)(uT / NS_PER_SEC);
122 cpu[1] = (unsigned int)((uT - cpu[0] * NS_PER_SEC) * 100);
123 cpu[2] = (unsigned int)(kT / NS_PER_SEC);
124 cpu[3] = (unsigned int)((kT - cpu[2] * NS_PER_SEC) * 100);
129 cpuTimePrecision :: Integer
130 cpuTimePrecision = round ((1000000000000::Integer) % fromIntegral (clockTicks))
137 unsafePerformIO (sysconf (#const _SC_CLK_TCK) >>= return . fromIntegral)
138 foreign import unsafe sysconf :: CInt -> IO CLong