controller_sample.c 7.89 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18
19
20
21
22
/*
	This module starts and maintains the sample thread.
	It also provides access to sample timing information.
	
 */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <limits.h>
#include <time.h>
#include <string.h>
#include <sys/mman.h>
31
#include <errno.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
32
33
34

#include "controller_block.h"
#include "controller_sample.h"
35
36
#include "log.h"
#include "shell.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
37
38
39
40


static int nsec_interval;
static int controller_sampleoverruns = 0;
41
static double controller_frequency = 1.0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
42

43
static pthread_t controller_sample_thread;
44
static bool controller_sample_thread_running = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
45
46
47
48
49
50
51
52
53

struct sample_timing {
	double min;
	double max;
	double avg;
	long long n;
	char *name;
};

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
static struct sample_timing st_start, st_io, st_end, st_sample;

struct controller_sample_start_hook {
	void (*func)(void *arg);
	void *arg;
};

static struct controller_sample_start_hook *controller_sample_start_hooks = NULL;
static int controller_sample_start_hooks_nr = 0;



static inline void tsnorm(struct timespec *ts)
{
	while (ts->tv_nsec >= 1000000000) {
		ts->tv_nsec -= 1000000000;
		ts->tv_sec++;
	}
}


Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
static void sample_timing_init(struct sample_timing *st, char *name)
{
	memset(st, 0, sizeof(struct sample_timing));
	st->name = name;
}

static void sample_timing_add(struct sample_timing *st, 
    struct timespec *t1, struct timespec *t2)
{
	double t;
	
	t = t2->tv_nsec * 1.0 - t1->tv_nsec * 1.0;
	t += (t2->tv_sec - t1->tv_sec) * 1000000000;

	if (st->n == 0) {
		st->min = t;
		st->max = t;
		st->avg = t;
		st->n = 1;
		return;
	}
	
	if (t > st->max)
		st->max = t;
	if (t < st->min)
		st->min = t;
	st->avg = st->avg * st->n + t;
	st->n++;
	st->avg /= st->n * 1.0;

	return;
}

static int sample_timing_diff_nsec(struct timespec *t1, struct timespec *t2)
{
	int diff;
	
	diff = t2->tv_nsec - t1->tv_nsec;
	diff += (t2->tv_sec - t1->tv_sec) * 1000000000;
	
	return diff;
}


static ssize_t sample_timing_snprintf(struct sample_timing *st,
    char *dest, size_t n)
{
122
	return snprintf(dest, n, "%s\t%.3f\t%.3f\t%.3f",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	    st->name,
	    st->min / 1000.0, st->avg / 1000.0, st->max / 1000.0);
}


/*
	This thread should be realtime....
 */
static void *sample_thread(void *arg)
{
	struct timespec t;
	struct sched_param param;
	struct timespec t_start;
	struct timespec t_io;
	struct timespec t_end;
	param.sched_priority = 99;
	
140
	log_send(LOG_T_DEBUG, "Starting sample thread");
141
	controller_sample_thread_running = true;
142

Jeroen Vreeken's avatar
Jeroen Vreeken committed
143
	if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
144
145
		log_send(LOG_T_WARNING, "sched_setscheduler() failed: %s",
		    strerror(errno));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
146
147
148
	}

	if (mlockall(MCL_CURRENT | MCL_FUTURE )) {
149
150
		log_send(LOG_T_WARNING, "mlockall failed: %s",
		    strerror(errno));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
194
195
196
	}

	clock_gettime(0, &t);
	t.tv_nsec = 0;
	t.tv_sec++;

	while (1) {
		/* Pre sample stuff: */
		controller_samplenr++;
		
		t.tv_nsec += nsec_interval;
		tsnorm(&t);
		
		controller_time_nseconds = t.tv_sec * 1000000000;
		controller_time_nseconds += t.tv_nsec;
		controller_time_seconds = t.tv_sec;
		controller_time_samplenr = t.tv_nsec / nsec_interval;

		/* Wait for the right moment... 
		   (Internal timer or external interrupt) */
		clock_nanosleep(0, TIMER_ABSTIME, &t, NULL);

		clock_gettime(0, &t_start);

		/* Do sample stuff */
		controller_block_calculate();
		
		clock_gettime(0, &t_io);
		
		controller_block_trace();
		controller_block_param_handle();
		
		clock_gettime(0, &t_end);
		
		sample_timing_add(&st_start, &t, &t_start);
		sample_timing_add(&st_io, &t_start, &t_io);
		sample_timing_add(&st_end, &t, &t_end);
		sample_timing_add(&st_sample, &t_start, &t_end);
		
		if (sample_timing_diff_nsec(&t, &t_end) > nsec_interval) {
			controller_sampleoverruns++;
		}
	}
	return NULL;
}

197
198
199
200
201
bool controller_sample_context(void)
{
	return pthread_equal(controller_sample_thread, pthread_self());
}

202
203
204
205
206
bool controller_sample_running(void)
{
	return false;
}

207

Jeroen Vreeken's avatar
Jeroen Vreeken committed
208
209
210
211
212
213
static void *sample_monitor(void *arg)
{
	char linebuf[201];
	
	linebuf[200] = 0;
	while(1) {
214
215
		log_send(LOG_T_DEBUG, "Samplenr: %d\tOverruns: %d",
		    controller_samplenr, controller_sampleoverruns);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
216
		sample_timing_snprintf(&st_start, linebuf, 200);
217
		log_send(LOG_T_DEBUG, linebuf);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
218
		sample_timing_snprintf(&st_end, linebuf, 200);
219
		log_send(LOG_T_DEBUG, linebuf);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
220
		sample_timing_snprintf(&st_io, linebuf, 200);
221
		log_send(LOG_T_DEBUG, linebuf);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
222
		sample_timing_snprintf(&st_sample, linebuf, 200);
223
		log_send(LOG_T_DEBUG, linebuf);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
224
225
226
227
228
		sleep(1);
	}
	return NULL;
}

229
230
231
232
233
/********************************************
 *	Shell interface:
 */

static void controller_sample_timing_reset(void)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
234
235
236
237
238
239
240
241
{
	sample_timing_init(&st_start, "start");
	sample_timing_init(&st_end, "end");
	sample_timing_init(&st_io, "io");
	sample_timing_init(&st_sample, "sample");
	controller_sampleoverruns = 0;
}

242
static ssize_t controller_sample_timing_snprintf(char *dest, size_t n)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
243
244
245
246
{
	ssize_t used = 0;
	
	used += sample_timing_snprintf(&st_start, dest, n);
247
	used += sprintf(dest + used, "\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
248
	used += sample_timing_snprintf(&st_end, dest + used, n - used);
249
	used += sprintf(dest + used, "\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
250
	used += sample_timing_snprintf(&st_io, dest + used, n - used);
251
	used += sprintf(dest + used, "\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
252
	used += sample_timing_snprintf(&st_sample, dest + used, n  - used);
253
	used += sprintf(dest + used, "\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
254
255
256
257

	return used;
}

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
static int controller_sample_timing(char *args, char *out, int *outlen)
{
	if (args && !strcmp(args, "print")) {
		*outlen = sprintf(out, "Sample timing:\n");
		*outlen += controller_sample_timing_snprintf(out + *outlen, 2000);
	} else if (args && !strcmp(args, "reset")) {
		controller_sample_timing_reset();
		*outlen = sprintf(out, "Sample timing has been cleared.\n");
	} else {
		*outlen = sprintf(out,
		     "Valid arguments are: 'reset' and 'print'\n");
	}

	return *outlen;
}


275
276
277
278
static struct shell_cmd sample_shell_cmd = {
	"timing", "reset/print sample timing", controller_sample_timing
};

279
280
int controller_sample_shell_add(void)
{
281
	return shell_cmd_add(&sample_shell_cmd);
282
283
284
}


Jeroen Vreeken's avatar
Jeroen Vreeken committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

double controller_sample_period(void)
{
	return 1.0 / controller_frequency;
}

void controller_sample_frequency_set(double frequency)
{
	controller_frequency = frequency;
}


void controller_sample_start_hook(void (*func)(void *arg), void *arg)
{
	controller_sample_start_hooks = realloc(
	    controller_sample_start_hooks,
	    sizeof(struct controller_sample_start_hook)*(controller_sample_start_hooks_nr+1));
	controller_sample_start_hooks[controller_sample_start_hooks_nr].func = func;
	controller_sample_start_hooks[controller_sample_start_hooks_nr].arg = arg;

	controller_sample_start_hooks_nr++;
}

int controller_sample_start(void)
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int i;

	nsec_interval = 1000000000.0 / controller_frequency;

316
317
	log_send(LOG_T_DEBUG, "nsec_interval: %d %f", 
	    nsec_interval, controller_frequency);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
318
319
320
321
322
323
324
325
326
327
328
329

	sample_timing_init(&st_start, "start");
	sample_timing_init(&st_end, "end");
	sample_timing_init(&st_io, "io");
	sample_timing_init(&st_sample, "sample");

	for (i = 0; i < controller_sample_start_hooks_nr; i++)
		controller_sample_start_hooks[i].func(
		    controller_sample_start_hooks[i].arg);

	pthread_attr_init(&attr);
	pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN * 2);
330
	pthread_create(&controller_sample_thread, &attr, sample_thread, NULL);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
331
332
333
334
	pthread_create(&thread_id, &attr, sample_monitor, NULL);

	return 0;
}