-
Notifications
You must be signed in to change notification settings - Fork 2
/
measure-sched-delays-win.c
174 lines (135 loc) · 3.33 KB
/
measure-sched-delays-win.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#define _WIN32_WINNT _WIN32_WINNT_LONGHORN
#define WINVER _WIN32_WINNT_LONGHORN
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <windows.h>
#include <process.h>
int64_t target_nanos;
#define BUFSIZE (1024*1024)
static int current_buffer;
static int bufpos[2];
static char buffers[2][BUFSIZE];
static CRITICAL_SECTION current_buffer_lock;
static CONDITION_VARIABLE have_data_cond;
static double perf_counts_per_nsec_inv;
static
int64_t read_nanos(void)
{
LARGE_INTEGER li;
BOOL rv = QueryPerformanceCounter(&li);
if (!rv) {
abort();
}
return (int64_t)((double)(li.QuadPart) * perf_counts_per_nsec_inv);
}
static
int int_min(int a, int b)
{
return a > b ? b : a;
}
unsigned WINAPI rt_thread(void *_dummy)
{
HANDLE timer;
BOOL rv;
LARGE_INTEGER due_time;
char *stuff;
timer = CreateWaitableTimer(NULL, FALSE, NULL);
if (timer == NULL) {
abort();
}
due_time.QuadPart = -10000000;
rv = SetWaitableTimer(timer, &due_time, 1000, NULL, NULL, FALSE);
if (!rv) {
abort();
}
rv = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
if (!rv) {
abort();
}
while (1) {
DWORD wrv = WaitForSingleObject(timer, INFINITE);
int64_t nanos_now;
int64_t nanos_after;
if (wrv != WAIT_OBJECT_0) {
abort();
}
nanos_now = read_nanos();
if (target_nanos == 0) {
target_nanos = nanos_now + 1000000000;
continue;
}
stuff = (char *)VirtualAlloc(NULL, 4096, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
if (stuff == NULL) {
abort();
}
stuff[0] ^= 1;
nanos_after = read_nanos();
VirtualFree(stuff, 4096, MEM_RELEASE);
EnterCriticalSection(¤t_buffer_lock);
{
long long diff = (long long)(nanos_now - target_nanos);
long long diff2 = (long long)(nanos_after - nanos_now);
int current = current_buffer;
int pos = bufpos[current];
int srv;
if (pos > BUFSIZE - 1024) {
fprintf(stderr, "%f: exceeded bufsize!\n", (double)nanos_now);
goto out;
}
srv = _snprintf(buffers[current] + pos, BUFSIZE - pos,
"%.9f %ld %ld\n",
nanos_now * (double)1E-9, (long)diff, (long)diff2);
bufpos[current] = int_min(pos + srv, BUFSIZE);
target_nanos = nanos_now + 1000000000LL;
}
out:
LeaveCriticalSection(¤t_buffer_lock);
WakeConditionVariable(&have_data_cond);
}
}
static
void setup_perf_freq(void)
{
LARGE_INTEGER li;
BOOL rv;
rv = QueryPerformanceFrequency(&li);
if (!rv) {
abort();
}
perf_counts_per_nsec_inv = 1E9 / (double)(li.QuadPart);
}
int main()
{
int rv;
setup_perf_freq();
timeBeginPeriod(10);
/* sets smaller buffer size. We're fine with passing those
* writes to kernel sooner */
setvbuf(stdout, 0, _IONBF, 0);
InitializeConditionVariable(&have_data_cond);
InitializeCriticalSection(¤t_buffer_lock);
rv = (int)_beginthreadex(NULL, 0, rt_thread, NULL, 0, NULL);
if (rv == 0) {
abort();
}
EnterCriticalSection(¤t_buffer_lock);
while (1) {
int current = current_buffer;
int pos;
if (!bufpos[current]) {
SleepConditionVariableCS(&have_data_cond, ¤t_buffer_lock, INFINITE);
continue;
}
current_buffer = current ^ 1;
pos = bufpos[current];
LeaveCriticalSection(¤t_buffer_lock);
fwrite(buffers[current], 1, pos, stdout);
bufpos[current] = 0;
EnterCriticalSection(¤t_buffer_lock);
}
}