diff options
author | Andrea Bastoni <bastoni@cs.unc.edu> | 2010-06-12 12:14:16 -0400 |
---|---|---|
committer | Bjoern B. Brandenburg <bbb@cs.unc.edu> | 2011-01-26 18:20:23 -0500 |
commit | 0a06230478a52a18d433735305c2cb6fc6de68da (patch) | |
tree | ff1324935555c7ec6e995a7251e7de0a361fdee4 /bin | |
parent | c3566e210d4e466e97d84d4a190760c4e91766ac (diff) |
[EDF-fm] Add support for sporadic edf-fm tasks and rtspin_edffm
Diffstat (limited to 'bin')
-rw-r--r-- | bin/rtspin_edffm.c | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/bin/rtspin_edffm.c b/bin/rtspin_edffm.c new file mode 100644 index 0000000..04dce0a --- /dev/null +++ b/bin/rtspin_edffm.c | |||
@@ -0,0 +1,263 @@ | |||
1 | #include <sys/time.h> | ||
2 | |||
3 | #include <stdio.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <unistd.h> | ||
6 | #include <time.h> | ||
7 | |||
8 | |||
9 | #include "litmus.h" | ||
10 | #include "common.h" | ||
11 | |||
12 | |||
13 | static double cputime() | ||
14 | { | ||
15 | struct timespec ts; | ||
16 | int err; | ||
17 | err = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); | ||
18 | if (err != 0) | ||
19 | perror("clock_gettime"); | ||
20 | return (ts.tv_sec + 1E-9 * ts.tv_nsec); | ||
21 | } | ||
22 | |||
23 | static double wctime() | ||
24 | { | ||
25 | struct timeval tv; | ||
26 | gettimeofday(&tv, NULL); | ||
27 | return (tv.tv_sec + 1E-6 * tv.tv_usec); | ||
28 | } | ||
29 | |||
30 | void usage(char *error) { | ||
31 | fprintf(stderr, "Error: %s\n", error); | ||
32 | fprintf(stderr, | ||
33 | "Usage: rt_spin [-w] [-p PARTITION] [-c CLASS] WCET PERIOD DURATION fracnum1 fracden2 fracnum2 fracden2 cpu1 cpu2\n" | ||
34 | " rt_spin -l\n"); | ||
35 | exit(1); | ||
36 | } | ||
37 | |||
38 | #define NUMS 4096 | ||
39 | static int num[NUMS]; | ||
40 | static double loop_length = 1.0; | ||
41 | static char* progname; | ||
42 | |||
43 | static int loop_once(void) | ||
44 | { | ||
45 | int i, j = 0; | ||
46 | for (i = 0; i < NUMS; i++) | ||
47 | j += num[i]++; | ||
48 | return j; | ||
49 | } | ||
50 | |||
51 | static int loop_for(double exec_time) | ||
52 | { | ||
53 | double t = 0; | ||
54 | int tmp = 0; | ||
55 | /* while (t + loop_length < exec_time) { | ||
56 | tmp += loop_once(); | ||
57 | t += loop_length; | ||
58 | } | ||
59 | */ | ||
60 | double start = cputime(); | ||
61 | double now = cputime(); | ||
62 | while (now + loop_length < start + exec_time) { | ||
63 | tmp += loop_once(); | ||
64 | t += loop_length; | ||
65 | now = cputime(); | ||
66 | } | ||
67 | |||
68 | return tmp; | ||
69 | } | ||
70 | |||
71 | static void fine_tune(double interval) | ||
72 | { | ||
73 | double start, end, delta; | ||
74 | |||
75 | start = wctime(); | ||
76 | loop_for(interval); | ||
77 | end = wctime(); | ||
78 | delta = (end - start - interval) / interval; | ||
79 | if (delta != 0) | ||
80 | loop_length = loop_length / (1 - delta); | ||
81 | } | ||
82 | |||
83 | static void configure_loop(void) | ||
84 | { | ||
85 | double start; | ||
86 | |||
87 | /* prime cache */ | ||
88 | loop_once(); | ||
89 | loop_once(); | ||
90 | loop_once(); | ||
91 | |||
92 | /* measure */ | ||
93 | start = wctime(); | ||
94 | loop_once(); /* hope we didn't get preempted */ | ||
95 | loop_length = wctime(); | ||
96 | loop_length -= start; | ||
97 | |||
98 | /* fine tune */ | ||
99 | fine_tune(0.1); | ||
100 | fine_tune(0.1); | ||
101 | fine_tune(0.1); | ||
102 | } | ||
103 | |||
104 | static void show_loop_length(void) | ||
105 | { | ||
106 | printf("%s/%d: loop_length=%f (%ldus)\n", | ||
107 | progname, getpid(), loop_length, | ||
108 | (long) (loop_length * 1000000)); | ||
109 | } | ||
110 | |||
111 | static void debug_delay_loop(void) | ||
112 | { | ||
113 | double start, end, delay; | ||
114 | show_loop_length(); | ||
115 | while (1) { | ||
116 | for (delay = 0.5; delay > 0.01; delay -= 0.01) { | ||
117 | start = wctime(); | ||
118 | loop_for(delay); | ||
119 | end = wctime(); | ||
120 | printf("%6.4fs: looped for %10.8fs, delta=%11.8fs, error=%7.4f%%\n", | ||
121 | delay, | ||
122 | end - start, | ||
123 | end - start - delay, | ||
124 | 100 * (end - start - delay) / delay); | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | static int job(double exec_time) | ||
130 | { | ||
131 | loop_for(exec_time); | ||
132 | sleep_next_period(); | ||
133 | return 0; | ||
134 | } | ||
135 | |||
136 | #define OPTSTR "p:c:wld:v" | ||
137 | |||
138 | int main(int argc, char** argv) | ||
139 | { | ||
140 | int ret; | ||
141 | lt_t wcet; | ||
142 | lt_t period; | ||
143 | /* [num,den] */ | ||
144 | lt_t frac1[2], frac2[2]; | ||
145 | int cpu1, cpu2; | ||
146 | double wcet_ms, period_ms; | ||
147 | int migrate = 0; | ||
148 | int cpu = 0; | ||
149 | int opt; | ||
150 | int wait = 0; | ||
151 | int test_loop = 0; | ||
152 | int skip_config = 0; | ||
153 | int verbose = 0; | ||
154 | double duration, start; | ||
155 | task_class_t class = RT_CLASS_HARD; | ||
156 | |||
157 | progname = argv[0]; | ||
158 | |||
159 | while ((opt = getopt(argc, argv, OPTSTR)) != -1) { | ||
160 | switch (opt) { | ||
161 | case 'w': | ||
162 | wait = 1; | ||
163 | break; | ||
164 | case 'p': | ||
165 | cpu = atoi(optarg); | ||
166 | migrate = 1; | ||
167 | break; | ||
168 | case 'c': | ||
169 | class = str2class(optarg); | ||
170 | if (class == -1) | ||
171 | usage("Unknown task class."); | ||
172 | break; | ||
173 | case 'l': | ||
174 | test_loop = 1; | ||
175 | break; | ||
176 | case 'd': | ||
177 | /* manually configure delay per loop iteration | ||
178 | * unit: microseconds */ | ||
179 | loop_length = atof(optarg) / 1000000; | ||
180 | skip_config = 1; | ||
181 | break; | ||
182 | case 'v': | ||
183 | verbose = 1; | ||
184 | break; | ||
185 | case ':': | ||
186 | usage("Argument missing."); | ||
187 | break; | ||
188 | case '?': | ||
189 | default: | ||
190 | usage("Bad argument."); | ||
191 | break; | ||
192 | } | ||
193 | } | ||
194 | |||
195 | |||
196 | if (!skip_config) | ||
197 | configure_loop(); | ||
198 | |||
199 | if (test_loop) { | ||
200 | debug_delay_loop(); | ||
201 | return 0; | ||
202 | } | ||
203 | |||
204 | if (argc - optind < 3) | ||
205 | usage("Arguments missing."); | ||
206 | wcet_ms = atof(argv[optind + 0]); | ||
207 | period_ms = atof(argv[optind + 1]); | ||
208 | duration = atof(argv[optind + 2]); | ||
209 | /* frac num, den = 0 means fixed task */ | ||
210 | frac1[0] = atoi(argv[optind + 3]); | ||
211 | frac1[1] = atoi(argv[optind + 4]); | ||
212 | frac2[0] = atoi(argv[optind + 5]); | ||
213 | frac2[1] = atoi(argv[optind + 6]); | ||
214 | cpu1 = atoi(argv[optind + 7]); | ||
215 | cpu2 = atoi(argv[optind + 8]); | ||
216 | wcet = wcet_ms * __NS_PER_MS; | ||
217 | period = period_ms * __NS_PER_MS; | ||
218 | if (wcet <= 0) | ||
219 | usage("The worst-case execution time must be a " | ||
220 | "positive number."); | ||
221 | if (period <= 0) | ||
222 | usage("The period must be a positive number."); | ||
223 | if (wcet > period) { | ||
224 | usage("The worst-case execution time must not " | ||
225 | "exceed the period."); | ||
226 | } | ||
227 | |||
228 | if (migrate) { | ||
229 | ret = be_migrate_to(cpu); | ||
230 | if (ret < 0) | ||
231 | bail_out("could not migrate to target partition"); | ||
232 | } | ||
233 | |||
234 | ret = sporadic_task_ns_edffm(wcet, period, 0, cpu, | ||
235 | frac1, frac2, cpu1, cpu2, | ||
236 | class, NO_ENFORCEMENT, migrate); | ||
237 | |||
238 | if (ret < 0) | ||
239 | bail_out("could not setup rt task params"); | ||
240 | |||
241 | if (verbose) | ||
242 | show_loop_length(); | ||
243 | |||
244 | init_litmus(); | ||
245 | |||
246 | ret = task_mode(LITMUS_RT_TASK); | ||
247 | if (ret != 0) | ||
248 | bail_out("could not become RT task"); | ||
249 | |||
250 | if (wait) { | ||
251 | ret = wait_for_ts_release(); | ||
252 | if (ret != 0) | ||
253 | bail_out("wait_for_ts_release()"); | ||
254 | } | ||
255 | |||
256 | start = wctime(); | ||
257 | |||
258 | while (start + duration > wctime()) { | ||
259 | job(wcet_ms * 0.0009); /* 90% wcet, in seconds */ | ||
260 | } | ||
261 | |||
262 | return 0; | ||
263 | } | ||