GetTime.c 2.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team 2005
 *
 * Machine-dependent time measurement functions
 *
 * ---------------------------------------------------------------------------*/

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

#include <windows.h>

14
15
16
17
#ifdef HAVE_TIME_H
# include <time.h>
#endif

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

21
22
static INLINE_ME Ticks
fileTimeToTicks(FILETIME ft)
23
{
24
25
26
27
28
    Ticks t;
    t = ((Ticks)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
    t = (t * TICKS_PER_SECOND) / HNS_PER_SEC;
    return t;
}    
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void
getProcessTimes(Ticks *user, Ticks *elapsed)
{
    *user    = getProcessCPUTime();
    *elapsed = getProcessElapsedTime();
}

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

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

    return fileTimeToTicks(userTime);
48
49
}

50
51
52
53
// getProcessElapsedTime relies on QueryPerformanceFrequency 
// which should be available on any Windows computer thay you
// would want to run Haskell on. Satnam Singh, 5 July 2010.

54
55
Ticks
getProcessElapsedTime(void)
56
{
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    // frequency represents the number of ticks per second
    // used by the QueryPerformanceFrequency implementaiton
    // and is represented by a 64-bit union type initially set to 0
    // and updated just once (hence use of static).
    static LARGE_INTEGER frequency = {.QuadPart = 0} ;  

    // system_time is a 64-bit union type used to represent the
    // tick count returned by QueryPerformanceCounter
    LARGE_INTEGER system_time ;

    // If this is the first time we are calling getProcessElapsedTime
    // then record the ticks per second used by QueryPerformanceCounter
    if (frequency.QuadPart == 0) {
      QueryPerformanceFrequency(&frequency);
    }
    
    // Get the tick count.
    QueryPerformanceCounter(&system_time) ;

    // Return the tick count as a millisecond value. 
    // Using double to compute the intermediate value, because a 64-bit
    // int would overflow when multiplied by TICKS_PER_SECOND in about 81 days.
    return (Ticks)((TICKS_PER_SECOND * (double)system_time.QuadPart) / (double)frequency.QuadPart) ;
80
81
}

82
83
Ticks
getThreadCPUTime(void)
84
{
85
86
87
88
89
90
91
92
    FILETIME creationTime, exitTime, userTime, kernelTime = {0,0};

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

    return fileTimeToTicks(userTime);
93
94
}

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