[project @ 2005-11-03 16:20:38 by simonmar]
[ghc-hetmet.git] / ghc / rts / win32 / GetTime.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 2005
4  *
5  * Machine-dependent time measurement functions
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #include "Rts.h"
10 #include "GetTime.h"
11
12 #include <windows.h>
13
14 #ifdef HAVE_TIME_H
15 # include <time.h>
16 #endif
17
18 /* elapsedtime() -- The current elapsed time in seconds */
19
20 #define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
21 /* Convert FILETIMEs into secs */
22 #define FT2longlong(ll,ft)    \
23     (ll)=(ft).dwHighDateTime; \
24     (ll) <<= 32;              \
25     (ll) |= (ft).dwLowDateTime; \
26     (ll) /= (unsigned long long) (HNS_PER_SEC / CLOCKS_PER_SEC)
27
28 /* cygwin32 or mingw32 version */
29 void
30 getProcessTimes( Ticks *user, Ticks *elapsed )
31 {
32     static int is_win9x = -1;
33     static Ticks elapsed_start = 0;
34
35     FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
36     long long int kT, uT;
37     
38     if (is_win9x < 0) {
39       /* figure out whether we're on a Win9x box or not. */
40       OSVERSIONINFO oi;
41       BOOL b;
42
43       /* Need to init the size field first.*/
44       oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
45       b = GetVersionEx(&oi);
46       
47       is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
48     }
49  
50     if (is_win9x) {
51       /* On Win9x, just attribute all running time to the user. */
52       SYSTEMTIME st;
53
54       GetSystemTime(&st);
55       SystemTimeToFileTime(&st,&userTime);
56     } else {
57       /* ToDo: pin down elapsed times to just the OS thread(s) that
58          are evaluating/managing Haskell code.
59       */
60       if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
61                           &exitTime, &kernelTime, &userTime)) {
62         /* Probably on a Win95 box..*/
63         *elapsed = 0;
64         *user = 0;
65         return;
66       }
67     }
68
69     FT2longlong(kT,kernelTime);
70     FT2longlong(uT,userTime);
71     *elapsed = uT + kT;
72     *user = uT;
73
74     if (is_win9x) {
75         /* User time is assumed to start at zero, so adjust for the fact
76            that we're using system time & not process time on Win9x.  */
77         if (elapsed_start == 0) {
78             elapsed_start = *elapsed;
79         }
80         *user -= elapsed_start;
81     }
82 }
83
84 Ticks getProcessCPUTime(void)
85 {
86     Ticks user, elapsed;
87     getProcessTimes(&user,&elapsed);
88     return user;
89 }
90
91 Ticks getProcessElapsedTime(void)
92 {
93     Ticks user, elapsed;
94     getProcessTimes(&user,&elapsed);
95     return elapsed;
96 }
97
98 nat getPageFaults(void)
99 {
100   /* ToDo (on NT): better, get this via the performance data
101      that's stored in the registry. */
102     return 0;
103 }