forked from alk/measure-sched-delays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure-sched-delays.c
193 lines (159 loc) · 4.01 KB
/
measure-sched-delays.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#define _GNU_SOURCE
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sched.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/mman.h>
#include <pthread.h>
#include <assert.h>
int64_t target_nanos;
timer_t timerid;
#define TIMER_SIG SIGRTMIN
#define BUFSIZE (1024*1024)
static int current_buffer;
static int writer_current_buffer;
static int bufpos[2];
static char buffers[2][BUFSIZE];
static long long max_diff = 0;
static
int64_t read_nanos(int type)
{
struct timespec ts;
int rv = clock_gettime(type, &ts);
if (rv < 0) {
perror("clock_gettime");
abort();
}
return (int64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec;
}
static
int int_min(int a, int b)
{
return a > b ? b : a;
}
static
void handle_timer_tick(void)
{
int64_t nanos = read_nanos(CLOCK_MONOTONIC);
int current = current_buffer;
int pos = bufpos[current];
if (pos > BUFSIZE - 1024) {
fprintf(stderr, "%lld exceeded bufsize!\n", (long long)nanos);
return;
}
long long diff = (long long)(nanos - target_nanos);
if (diff > max_diff) {
max_diff = diff;
}
int srv = snprintf(buffers[current] + pos, BUFSIZE - pos,
"%.9f ns:%lld, us:%lld, ms:%lld "
"(max ns:%lld, us:%lld, ms:%lld)\n",
read_nanos(CLOCK_REALTIME) * (double)1E-9,
diff, diff/1000, diff/1000/1000,
max_diff, max_diff/1000, max_diff/1000/1000);
bufpos[current] = int_min(pos + srv, BUFSIZE);
target_nanos += 1000000000LL;
}
static pthread_mutex_t current_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t have_data_cond = PTHREAD_COND_INITIALIZER;
void *rt_thread(void *dummy)
{
struct sched_param sp;
sigset_t ss;
int rv;
sigemptyset(&ss);
sigaddset(&ss, TIMER_SIG);
while (1) {
int signo;
sigwait(&ss, &signo);
assert(signo == TIMER_SIG);
pthread_mutex_lock(¤t_buffer_lock);
handle_timer_tick();
pthread_cond_signal(&have_data_cond);
pthread_mutex_unlock(¤t_buffer_lock);
}
}
int main()
{
struct sigevent sev;
struct itimerspec its;
struct sigaction sa;
pthread_t threadid;
struct sched_param sp;
sigset_t ss;
int rv;
sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
rv = sched_setscheduler(getpid(), SCHED_FIFO, &sp);
if (rv < 0) {
perror("sched_setscheduler");
exit(1);
}
/* sets smaller buffer size. We're fine with passing those
* writes to kernel sooner */
setvbuf(stdout, 0, _IONBF, 0);
/* ruby effing crap sets them to ignore which childs inherit
* (WAT!) */
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = SIG_DFL;
sigaction(SIGHUP, &sa, 0);
sigaction(SIGINT, &sa, 0);
sigaction(SIGQUIT, &sa, 0);
memset(&sp, 0, sizeof(sp));
rv = mlockall(MCL_CURRENT | MCL_FUTURE);
if (rv < 0) {
perror("mlockall");
exit(1);
}
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = TIMER_SIG;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1) {
perror("timer_create");
exit(1);
}
sigemptyset(&ss);
sigaddset(&ss, TIMER_SIG);
sigprocmask(SIG_BLOCK, &ss, 0);
rv = pthread_create(&threadid, 0, rt_thread, 0);
if (rv) {
errno = rv;
perror("pthread_create");
exit(1);
}
rv = clock_gettime(CLOCK_MONOTONIC, &its.it_value);
if (rv < 0) {
perror("clock_gettime");
abort();
}
its.it_value.tv_sec += 2;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 1;
its.it_interval.tv_nsec = 0;
target_nanos = 1000000000LL * its.it_value.tv_sec + its.it_value.tv_nsec;
rv = timer_settime(timerid, TIMER_ABSTIME, &its, 0);
if (rv < 0) {
perror("timer_settime");
exit(1);
}
pthread_mutex_lock(¤t_buffer_lock);
while (1) {
int current = current_buffer;
int pos;
if (!bufpos[current]) {
pthread_cond_wait(&have_data_cond, ¤t_buffer_lock);
continue;
}
current_buffer = current ^ 1;
pos = bufpos[current];
pthread_mutex_unlock(¤t_buffer_lock);
fwrite(buffers[current], 1, pos, stdout);
bufpos[current] = 0;
pthread_mutex_lock(¤t_buffer_lock);
}
}