summaryrefslogtreecommitdiff
path: root/src/timing.c
blob: cf729eab00760e62759c8c910f6e45d0ff809803 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "timing.h"

#ifdef GIT_WIN32

#define TICKS_PER_SECOND	1000

int git_stopwatch_start(git_stopwatch *s)
{
	/* Try first to use the high-performance counter */
	if (!QueryPerformanceFrequency(&s->freq) ||
		!QueryPerformanceCounter(&s->start)) {
		/* Fall back to GetTickCount */
		s->freq.QuadPart = 0;
		s->start.LowPart = GetTickCount();
	}

	s->running = 1;

	return 0;
}

int git_stopwatch_query(double *out_elapsed_seconds, git_stopwatch *s)
{
	LARGE_INTEGER end;
	double elapsed_seconds;

	if (!s->running) {
		giterr_set(GITERR_INVALID, "Stopwatch is not running");
		return -1;
	}

	if (s->freq.QuadPart) {
		if (!QueryPerformanceCounter(&end)) {
			giterr_set(GITERR_OS, "Unable to read performance counter");
			return -1;
		}

		elapsed_seconds = (end.QuadPart - s->start.QuadPart) / (double)s->freq.QuadPart;
	} else {
		DWORD end_ticks = GetTickCount();

		/* Unsigned subtraction handles tick count wraparound */
		elapsed_seconds = (end_ticks - s->start.LowPart) / (double)TICKS_PER_SECOND;
	}

	/* Just to be on the safe side */
	if (elapsed_seconds < 0)
		elapsed_seconds = 0;

	*out_elapsed_seconds = elapsed_seconds;

	return 0;
}

#elif __APPLE__

int git_stopwatch_start(git_stopwatch *s)
{
	(void)mach_timebase_info(&s->info);
	s->start = mach_absolute_time();

	s->running = 1;

	return 0;
}

int git_stopwatch_query(double *out_elapsed_seconds, git_stopwatch *s)
{
	uint64_t end;
	double elapsed_seconds;

	if (!s->running) {
		giterr_set(GITERR_INVALID, "Stopwatch is not running");
		return -1;
	}

	end = mach_absolute_time();
	elapsed_seconds = 0;

	if (end > start) {
		elapsed_seconds = (double)s->info.numer * (end - start);
		elapsed_seconds /= (double)s->info.denom * 1E9L;
	}

	*out_elapsed_seconds = elapsed_seconds;

	return 0;
}

#else

int git_stopwatch_start(git_stopwatch *s)
{
	if (clock_gettime(CLOCK_MONOTONIC, &s->start)) {
		giterr_set(GITERR_OS, "Unable to get the monotonic timer");
		return -1;
	}

	s->running = 1;

	return 0;
}

int git_stopwatch_query(double *out_elapsed_seconds, git_stopwatch *s)
{
	struct timespec end, elapsed;
	double elapsed_seconds;

	if (!s->running) {
		giterr_set(GITERR_INVALID, "Stopwatch is not running");
		return -1;
	}

	if (clock_gettime(CLOCK_MONOTONIC, &end)) {
		giterr_set(GITERR_OS, "Unable to get the monotonic timer");
		return -1;
	}

	elapsed_seconds = 0;

	/* If end < start, then we consider the elapsed time to be zero */
	if (end.tv_sec > s->start.tv_sec ||
		(end.tv_sec == s->start.tv_sec && end.tv_nsec > s->start.tv_nsec)) {
		elapsed.tv_sec = end.tv_sec - s->start.tv_sec;
		elapsed.tv_nsec = end.tv_nsec - s->start.tv_nsec;

		if (elapsed.tv_nsec < 0) {
			elapsed.tv_sec--;
			elapsed.tv_nsec += 1E9L;
		}

		elapsed_seconds = (double)elapsed.tv_sec + elapsed.tv_nsec / (double)1E9;
	}

	*out_elapsed_seconds = elapsed_seconds;

	return 0;
}

#endif