File: another_way.c

package info (click to toggle)
rtlinux 2.0rel-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,068 kB
  • ctags: 1,178
  • sloc: ansic: 7,169; makefile: 779; sh: 89
file content (175 lines) | stat: -rw-r--r-- 3,807 bytes parent folder | download
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
/*
 * RTL scheduling accuracy measuring example
 *
 * (C) Michael Barabanov, 1997
 *  (C) FSMLabs  1999. baraban@fsmlabs.com
 *  Released under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991
 *  Any use of this code must include this notice.
 */

#include <rtl.h>
#include <rtl_fifo.h>
#include <time.h>
#include <rtl_sched.h>
#include <rtl_sync.h>
#include <pthread.h>
#include <unistd.h>
#include <rtl_debug.h>
#include <errno.h>
#include "common.h"

int ntests=500;
int period=1000000;
int mode=0;
int fifo_size=4000;
int advance=0;

MODULE_PARM(period,"i");
MODULE_PARM(ntests,"i");
MODULE_PARM(mode,"i");
MODULE_PARM(advance,"i");

pthread_t thread;
int fd_fifo;


void *thread_code(void *param) {

	hrtime_t start_time;
	hrtime_t last_time = 0;
	hrtime_t diff;
	hrtime_t now;
	hrtime_t min_diff;
	hrtime_t max_diff;
	struct sample samp;
	int i;
	int cnt = 0;
	DECLARE_CPUID(cpu_id);

	rtl_printf ("Measurement task starts on CPU %d\n", cpu_id);
	if (mode) {
		int ret = rtl_setclockmode (rtl_getschedclock(), RTL_CLOCK_MODE_PERIODIC, period);

		if (ret != 0) {
			conpr("Setting periodic mode failed\n");
			mode = 0;
		}
	}

	if (mode) {
		struct timespec resolution;

		/* need to round up the period; should this be done
		 * in pthread_make_periodic? */
		clock_getres (rtl_getschedclock(), &resolution);
		period = timespec_to_ns (&resolution);
	} else {

		rtl_setclockmode (rtl_getschedclock(), RTL_CLOCK_MODE_ONESHOT, 0);
	}

	start_time = clock_gethrtime(rtl_getschedclock()) + 2 * period;

	if (advance) {
		pthread_make_periodic_np (pthread_self(), start_time - advance, period);
	} else {
		pthread_make_periodic_np (pthread_self(), start_time, period);
	}


	fd_fifo = open("/dev/rtf0", O_NONBLOCK);
	if (fd_fifo < 0) {
		rtl_printf("/dev/rtf0 open returned %d\n", fd_fifo);
		return (void *) -1;
	}

	if (advance) {
		rtl_stop_interrupts(); /* Be careful with this! The task won't be preempted by anything else. This is probably only appropriate for small high-priority tasks. */
	}

	do {
		min_diff = 2000000000;
		max_diff = -2000000000;

		for (i = 0; i < ntests; i++) {
			++cnt;
			pthread_wait_np();
			start_time += period;

			now = clock_gethrtime(CLOCK_UST);
			if (advance && !mode) {
				if (now < start_time) {
					rtl_delay (start_time - now);
				}
				now = clock_gethrtime(CLOCK_UST);
			}
			if (!last_time) {
				last_time = now;
				continue;
			}
			diff = now - last_time - period;
			if (diff < 0) {
				diff = -diff;
			}
			if (diff < min_diff) {
				min_diff = diff;
			}
			if (diff > max_diff) {
				max_diff = diff;
			}

			last_time = now;
		}

		samp.min = min_diff;
		samp.max = max_diff;
		write (fd_fifo, &samp, sizeof(samp));
	} while (1);
	return 0;
}



int init_module(void)
{
	pthread_attr_t attr;
	struct sched_param sched_param;
	int thread_status;
        int fifo_status;

	rtf_destroy(0);
	fifo_status = rtf_create(0, fifo_size);
	if (fifo_status) {
		rtl_printf("RTL measurement test fail. fifo_status=%d\n",fifo_status);
		return -1;
	}


	rtl_printf("RTL measurement module on CPU %d\n",rtl_getcpuid());
	pthread_attr_init (&attr);
	pthread_attr_setcpu_np(&attr, 0 /*!rtl_getcpuid()*/);
	sched_param.sched_priority = 1;
	pthread_attr_setschedparam (&attr, &sched_param);
	rtl_printf("About to thread create\n");
	thread_status = pthread_create (&thread,  &attr, thread_code, (void *)1);
	if (thread_status != 0) {
		rtl_printf("failed to create RT-thread; errno=\n", errno);
		return -1;
	} else {
/* 		rtl_printf("created RT-thread\n"); */
	}

	return 0;
}


void cleanup_module(void)
{
        rtl_printf ("Removing module on CPU %d\n", rtl_getcpuid());
	pthread_delete_np (thread);
	close(fd_fifo);
/* should be:	pthread_cancel(thread); pthread_join(thread); */
/* or:  	rtl_pthread_kill_other_threads(); */
	rtf_destroy(0);
}