Massive patch for the first months work adding System FC to GHC #35
[ghc-hetmet.git] / 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 #define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
19 /* Convert FILETIMEs into secs */
20
21 static INLINE_ME Ticks
22 fileTimeToTicks(FILETIME ft)
23 {
24     Ticks t;
25     t = ((Ticks)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
26     t = (t * TICKS_PER_SECOND) / HNS_PER_SEC;
27     return t;
28 }    
29
30 static int is_win9x = -1;
31
32 static INLINE_ME rtsBool
33 isWin9x(void)
34 {
35     if (is_win9x < 0) {
36         /* figure out whether we're on a Win9x box or not. */
37         OSVERSIONINFO oi;
38         BOOL b;
39         
40         /* Need to init the size field first.*/
41         oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
42         b = GetVersionEx(&oi);
43       
44         is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
45     }
46     return is_win9x;
47 }
48
49
50 void
51 getProcessTimes(Ticks *user, Ticks *elapsed)
52 {
53     *user    = getProcessCPUTime();
54     *elapsed = getProcessElapsedTime();
55 }
56
57 Ticks
58 getProcessCPUTime(void)
59 {
60     FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
61
62     if (isWin9x()) return getProcessElapsedTime();
63
64     if (!GetProcessTimes(GetCurrentProcess(), &creationTime,
65                          &exitTime, &kernelTime, &userTime)) {
66         return 0;
67     }
68
69     return fileTimeToTicks(userTime);
70 }
71
72 Ticks
73 getProcessElapsedTime(void)
74 {
75     FILETIME system_time;
76     GetSystemTimeAsFileTime(&system_time);
77     return fileTimeToTicks(system_time);
78 }
79
80 Ticks
81 getThreadCPUTime(void)
82 {
83     FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};
84
85     if (isWin9x()) return getProcessCPUTime();
86
87     if (!GetThreadTimes(GetCurrentThread(), &creationTime,
88                         &exitTime, &kernelTime, &userTime)) {
89         return 0;
90     }
91
92     return fileTimeToTicks(userTime);
93 }
94
95 nat
96 getPageFaults(void)
97 {
98   /* ToDo (on NT): better, get this via the performance data
99      that's stored in the registry. */
100     return 0;
101 }