summaryrefslogtreecommitdiff
path: root/rts/win32/GetTime.c
blob: 584b994d5378226d80b1f2ed632e7019260931cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team 2005
 *
 * Machine-dependent time measurement functions
 *
 * ---------------------------------------------------------------------------*/

#include "Rts.h"
#include "GetTime.h"

#include <windows.h>

#ifdef HAVE_TIME_H
# include <time.h>
#endif

#define HNS_PER_SEC 10000000LL /* FILETIMES are in units of 100ns */
/* Convert FILETIMEs into secs */

static INLINE_ME Ticks
fileTimeToTicks(FILETIME ft)
{
    Ticks t;
    t = ((Ticks)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
    t = (t * TICKS_PER_SECOND) / HNS_PER_SEC;
    return t;
}    

static int is_win9x = -1;

static INLINE_ME rtsBool
isWin9x(void)
{
    if (is_win9x < 0) {
	/* figure out whether we're on a Win9x box or not. */
	OSVERSIONINFO oi;
	BOOL b;
	
	/* Need to init the size field first.*/
	oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	b = GetVersionEx(&oi);
      
	is_win9x = ( (b && (oi.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS)) ? 1 : 0);
    }
    return is_win9x;
}


void
getProcessTimes(Ticks *user, Ticks *elapsed)
{
    *user    = getProcessCPUTime();
    *elapsed = getProcessElapsedTime();
}

Ticks
getProcessCPUTime(void)
{
    FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};

    if (isWin9x()) return getProcessElapsedTime();

    if (!GetProcessTimes(GetCurrentProcess(), &creationTime,
			 &exitTime, &kernelTime, &userTime)) {
	return 0;
    }

    return fileTimeToTicks(userTime);
}

Ticks
getProcessElapsedTime(void)
{
    FILETIME system_time;
    GetSystemTimeAsFileTime(&system_time);
    return fileTimeToTicks(system_time);
}

Ticks
getThreadCPUTime(void)
{
    FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};

    if (isWin9x()) return getProcessCPUTime();

    if (!GetThreadTimes(GetCurrentThread(), &creationTime,
			&exitTime, &kernelTime, &userTime)) {
	return 0;
    }

    return fileTimeToTicks(userTime);
}

nat
getPageFaults(void)
{
  /* ToDo (on NT): better, get this via the performance data
     that's stored in the registry. */
    return 0;
}