File: thread_time.c

package info (click to toggle)
rtlinux 3.1pre3-2
  • links: PTS
  • area: non-free
  • in suites: sarge, woody
  • size: 4,892 kB
  • ctags: 4,228
  • sloc: ansic: 26,204; sh: 2,069; makefile: 1,414; perl: 855; tcl: 489; asm: 380; cpp: 42
file content (136 lines) | stat: -rw-r--r-- 2,947 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
/*
 * (C) Finite State Machine Labs Inc. 2000 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 <rtl.h>
#include <pthread.h>
#include <rtl_sched.h>
#include <rtl_time.h>
#include <rtl_fifo.h>

#define MODULE_NAME	"thread_time"

MODULE_AUTHOR("Nathan Paul Simons <npsimons@fsmlabs.com>");
MODULE_DESCRIPTION("RTLinux thread wait test kernel module");

MODULE_PARM(times, "i");
MODULE_PARM_DESC(times, "Number of times to test.");
MODULE_PARM(fifo, "i");
MODULE_PARM_DESC(fifo, "RTL-FIFO number to create (ie, 0 -> /dev/rtf0)");
MODULE_PARM(wait, "i");
MODULE_PARM_DESC(wait,
		 "Amount of time to wait (in nanoseconds) for each test");

int times;
int fifo = 0;
int wait;

int all_times_sz;

pthread_t time_thread;
pthread_t fifo_thread;

char *rtl_strerr(int thiserr);

void *time_stats(void *arg)
{
	struct sched_param my_sparam;
	int retval;
	int i;
	hrtime_t all_times[times];

	my_sparam.sched_priority = 1;

	if (
	    (retval =
	     pthread_setschedparam(pthread_self(), SCHED_FIFO,
				   &my_sparam)) != 0) {
		rtl_printf
		    ("%s: pthread_setschedparam (): %s\n", MODULE_NAME,
		     rtl_strerr(retval));
		return (NULL);
	}

	all_times[0] = clock_gethrtime(CLOCK_REALTIME);
	pthread_make_periodic_np(pthread_self(), all_times[0], wait);

	for (i = 0; i <= times; i++) {
		pthread_wait_np();
		all_times[i] = clock_gethrtime(CLOCK_REALTIME);
	}

	if ((retval = rtf_put(fifo, all_times, all_times_sz)) <
	    all_times_sz) {
		rtl_printf("%s: rtf_put (%d, all_times, %d): %s\n",
			   MODULE_NAME, fifo, all_times_sz,
			   rtl_strerr(retval));
		return (NULL);
	}

	return (NULL);
}

int init_module(void)
{
	int retval;

	fifo = 0;
	all_times_sz = (sizeof(hrtime_t) * (times + 1));

	if ((retval = rtf_create(fifo, all_times_sz + 1)) != 0) {
		rtl_printf("%s: rtf_create (0, %d): %s\n", MODULE_NAME,
			   all_times_sz, rtl_strerr(retval));
		return (retval);
	}

	if ((retval = pthread_create(&time_thread, NULL, time_stats, 0)) !=
	    0) {
		rtl_printf
		    ("%s: pthread_create (&time_thread, NULL, time_stats, 0): %s\n",
		     MODULE_NAME, rtl_strerr(retval));
		return (retval);
	}

	return (0);
}

void cleanup_module(void)
{
	pthread_cancel(time_thread);
	pthread_join(time_thread, NULL);

	rtf_destroy(fifo);
}

char *rtl_strerr(int thiserr)
{
	switch (thiserr) {
	case -ENODEV:
		return ("-ENODEV");
	case -EINVAL:
		return ("-EINVAL");
	case -EPERM:
		return ("-EPERM");
	case -ESRCH:
		return ("-ESRCH");
	case -EFAULT:
		return ("-EFAULT");
	case -EBUSY:
		return ("-EBUSY");
	case -ENOMEM:
		return ("-ENOMEM");
	case -ENOSPC:
		return ("-ENOSPC");
	case -EAGAIN:
		return ("-EAGAIN");
	default:
		return ("unknown error");
	}
}