File: switch_app.c

package info (click to toggle)
rtlinux 3.1pre3-3
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 4,896 kB
  • ctags: 4,228
  • sloc: ansic: 26,204; sh: 2,069; makefile: 1,414; perl: 855; tcl: 489; asm: 380; cpp: 42
file content (236 lines) | stat: -rw-r--r-- 5,016 bytes parent folder | download | duplicates (2)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * (C) Finite State Machine Labs Inc. 2001 business@fsmlabs.com
 *
 * Released under the terms of GPL 2.
 * Open RTLinux makes use of a patented process described in
 * US Patent 5,995,745. Use of this process is governed
 * by the Open RTLinux Patent License which can be obtained from
 * www.fsmlabs.com/PATENT or by sending email to
 * licensequestions@fsmlabs.com
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

#include "switch_test.h"

#ifndef	hrtime_t
typedef long long int hrtime_t;
#endif				/* hrtime_t */

/* FIFO number to use */
#define	FIFO_NR	1
/* FIFO size to use */
#define	FIFO_SZ	16384

/* module unload and loading commands */
#define	MODULE_LOAD	"/sbin/insmod regression/switch_time.o fifo_nr=%d fifo_sz=%d"
#define MODULE_UNLOAD	"/sbin/rmmod switch_time"

int unload_module()
{
	if ((system(MODULE_UNLOAD)) != 0) {
		fprintf(stderr, "system(%s): %s\n", MODULE_UNLOAD,
			strerror(errno));
		return errno;
	}

	return 0;
}

void my_sahandler(int whatever)
{
	fprintf(stderr, "my_sahandler: received signal %d\n", whatever);
	unload_module();
	exit(whatever);
}

int sig_handler_setup(void)
{
	struct sigaction *my_action;
	struct sigaction *old_action;

	if (
	    (my_action =
	     (struct sigaction *) calloc(1,
					 sizeof(struct sigaction))) ==
	    NULL) {
		fprintf(stderr, "calloc(1, %d): %s\n",
			(int) sizeof(struct sigaction), strerror(errno));
		return errno;
	}

	my_action->sa_handler = &my_sahandler;

	if (
	    (old_action =
	     (struct sigaction *) calloc(1,
					 sizeof(struct sigaction))) ==
	    NULL) {
		fprintf(stderr, "calloc(1, %d): %s\n",
			(int) sizeof(struct sigaction), strerror(errno));
		return errno;
	}

	sigaction(SIGHUP, my_action, old_action);
	sigaction(SIGINT, my_action, old_action);
	sigaction(SIGQUIT, my_action, old_action);

	return 0;
}

int load_module(int fifo_nr, int fifo_sz)
{
	int i = strlen(MODULE_LOAD) + 32;
	char command[i];

	if ((snprintf(command, i, MODULE_LOAD, fifo_nr, fifo_sz)) < 0) {
		fprintf(stderr, "snprintf(): %s", strerror(errno));
		return errno;
	}

	if ((system(command)) != 0) {
		fprintf(stderr, "system(%s): %s\n", command,
			strerror(errno));
		return errno;
	}

	return 0;
}

char *construct_filename(int i)
{
	char *filename;

	if ((filename = (char *) calloc(11, sizeof(char))) == NULL) {
		fprintf(stderr, "calloc(11, sizeof(char)): %s\n",
			strerror(errno));
		return NULL;
	}

	if ((snprintf(filename, 11, "/dev/rtf%d", i)) < 0) {
		fprintf(stderr, "snprintf(filename, 11, /dev/rtf%d): %s",
			i, strerror(errno));
		free(filename);
		return NULL;
	}

	return filename;
}

int stat_test(const char *filename)
{
	struct stat file_stats;

	if (stat(filename, &file_stats) != 0) {
		fprintf(stderr, "stat(%s, &file_stats): %s\n", filename,
			strerror(errno));
		return errno;
	}

	if (!(S_ISCHR(file_stats.st_mode))) {
		fprintf(stderr, "%s is not a character device.\n",
			filename);
		return -1;
	}

	return 0;
}

int main(void)
{
	int retval, filedes, i, copy_size;
	hrtime_t time1[num_tests], time2[num_tests], worst, best, current;
	double average;
	char *filename, *tmp_buf;
	struct pollfd fifo_poll;

	average = 0;
	worst = 0;
	best = 99999999;
	copy_size = (sizeof(hrtime_t) * (num_tests));

	unload_module();

	sig_handler_setup();

	if ((tmp_buf = (char *) calloc(FIFO_SZ, 1)) == NULL) {
		fprintf(stderr, "calloc(FIFO_SZ, 1): %s\n",
			strerror(errno));
		return errno;
	}

	if ((retval = load_module(FIFO_NR, FIFO_SZ)) < 0) {
		return retval;
	}

	if ((filename = construct_filename(FIFO_NR)) == NULL) {
		unload_module();
		return -1;
	}

	if ((retval = stat_test(filename)) < 0) {
		unload_module();
		free(filename);
		return retval;
	}

	if ((filedes = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
		fprintf(stderr, "open(%s, O_RDONLY): %s\n", filename,
			strerror(errno));
		unload_module();
		return errno;
	}

	fifo_poll.fd = filedes;
	fifo_poll.events = POLLIN;

	if ((poll(&fifo_poll, 1, 1000000 * num_tests)) < 0) {
		fprintf(stderr, "poll(&fifo_poll, 1, 1): %s\n",
			strerror(errno));
		close(filedes);
		unload_module();
		return errno;
	}

	if ((retval = read(filedes, tmp_buf, FIFO_SZ)) < 0) {
		fprintf(stderr, "read(%d, tmp_buf, FIFO_SZ): %s\n",
			filedes, strerror(errno));
		close(filedes);
		unload_module();
		return errno;
	}

	if (retval > 0) {
		memcpy(&time1, tmp_buf, copy_size);
		memcpy(&time2, tmp_buf + copy_size, copy_size);
	}

	for (i = 0; i < num_tests; i++) {
		current = time2[i] - time1[i];
		average += current;
		worst = (worst < current) ? current : worst;
		best = (best > current) ? current : best;
	}

	average /= num_tests;

	fprintf(stderr,
		"average: %f us\tworst: %d.%02d us\tbest: %d.%02d us\n",
		average / 1000, (int) worst / 1000,
		(int) (worst % 1000) / 10, (int) best / 1000,
		(int) (best % 1000) / 10);

	close(filedes);
	free(filename);
	unload_module();
	return 0;
}