-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime.c
600 lines (424 loc) · 13.6 KB
/
realtime.c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
Copyright (C) 2012, 2013 Christian Klauer
This file is part of OpenRTDynamics, the Real-Time Dynamics Framework
OpenRTDynamics is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenRTDynamics 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with OpenRTDynamics. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Steps to port ORTD to a new target:
*
* 1) Edit the Makefile & and add a new section for your target
* 2) Configure your new target in target.conf
* 3) Add an #ifdef / #endif combination for your target in this
* file to implement the neccessary platform specific functions.
* (see below for a list)
*
*
*
* Currently, possible targets are:
* __ORTD_TARGET_LINUX, __ORTD_TARGET_ANDROID, __ORTD_TARGET_RTAI (only partial support)
*
* The following functions must be defined for each target
*
* int ortd_rt_SetThreadProperties(int *par, int Npar)
* Set the properties of the calling thread
*
* int ortd_rt_ChangePriority(unsigned int flags, int priority)
* The obsolete version of ortd_rt_SetThreadProperties
* may be a dummy function.
*
* long int ortd_mu_time()
* return the system time in microseconds
*
*
* Please note: ORTD relies on the pthread library for creating threads.
* Of the target OS / RTOS doesn't support this library,
* an emulation of a small subset of the pthread library
* may help. I guess Xenomai has support for pthreads.
*
* In the long term however, all dependencies on the specific
* operating systems will be in this file.
*
*
* Currently the following functions of libpthread are used:
*
* pthread_cancel
* pthread_cond_destroy
* pthread_cond_init
* pthread_cond_signal
* pthread_cond_wait
* pthread_create
* pthread_exit
* pthread_join
* pthread_kill
* pthread_cancel // workaround for Android
* pthread_mutex_destroy
* pthread_mutex_init
* pthread_mutex_lock
* pthread_mutex_trylock
* pthread_mutex_unlock
* pthread_self
* pthread_setaffinity_np
*
*
*
*/
//
// Headers for the individual target systems
//
//
// lINUX
//
#if defined(__ORTD_TARGET_LINUX) || defined(__ORTD_TARGET_ANDROID)
// normal linux with optional rt_preempt
#define _GNU_SOURCE
#include <sched.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#include <signal.h>
#endif
//
// RTAI.
//
#ifdef __ORTD_TARGET_RTAI
#define _GNU_SOURCE
#include <sched.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#include <signal.h>
#endif
//
// MACOSX
//
#ifdef __ORTD_TARGET_MACOSX
#include <sched.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <pthread.h>
#include <signal.h>
#endif
// Add other targets
// as needed here
// #ifdef __ORTD_TARGET_ANDROID
// #include <time.h>
// #include <sched.h>
// #include <sys/mman.h>
// #endif
#include "realtime.h"
//#include <binders.h>
//#if defined(__ORTD_TARGET_LINUX) || defined(__ORTD_TARGET_ANDROID)
#if defined(__SYSTEMAPI_LINUX) || defined(__SYSTEMAPI_ANDROID)
// normal linux with optional rt_preempt OR ANDROID
#define MAX_SAFE_STACK (8*1024) /* The maximum stack size which is
guaranteed safe to access without
faulting */
#define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
long int ortd_mu_time()
{
struct timeval mytime;
struct timezone myzone;
gettimeofday(&mytime, &myzone);
return (1000000*mytime.tv_sec+mytime.tv_usec);
} /* mu_time */
int ortd_rt_SetThreadProperties2(struct TaskPriority_t TaskPriority)
{
return ortd_rt_SetThreadProperties(TaskPriority.par, TaskPriority.Npar);
}
// #ifdef __ORTD_TARGET_ANDROID
// The signal handler for the threads
void ortd_thread_exit_handler(int sig)
{
#ifdef DEBUG
printf("ortd_thread_exit_handler: this signal is %d \n", sig);
#endif
pthread_exit(0);
}
void ortd_InstallThreadSignalHandler() {
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = ortd_thread_exit_handler;
int rc = sigaction(SIGUSR1,&actions,NULL);
}
// #endif
void ortd_rt_stack_prefault(void) {
unsigned char dummy[MAX_SAFE_STACK];
memset(dummy, 0, MAX_SAFE_STACK);
return;
}
int ortd_rt_SetThreadProperties(int *par, int Npar)
{
// set signal handlers
ortd_InstallThreadSignalHandler();
// set priorities and cpu affinity
if (Npar >= 3) {
// ok got prio
#ifdef DEBUG
fprintf(stderr, "Task Prio1 (flags) would be %d (1 means ORTD_RT_REALTIMETASK)\n", par[0]);
fprintf(stderr, "Task Prio2 would be %d\n", par[1]);
fprintf(stderr, "Task CPU would be %d\n", par[2]);
#endif
// Set CPU
ortd_rt_SetCore(par[2]);
// set the tasks priority
ortd_rt_ChangePriority(par[0], par[1]);
ortd_rt_stack_prefault();
#ifdef DEBUG
fprintf(stderr, "realtime.c: Successfully set the task properties\n");
#endif
return 0;
}
#ifdef DEBUG
fprintf(stderr, "realtime.c: no task properties were given\n");
#endif
return -1;
}
int ortd_rt_SetCore(int core_id) {
// #ifdef __ORTD_TARGET_ANDROID
#ifdef __SYSTEMAPI_ANDROID
// http://stackoverflow.com/questions/16319725/android-set-thread-affinity
#define CPU_SETSIZE 1024
#define __NCPUBITS (8 * sizeof (unsigned long))
typedef struct
{
unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;
#define CPU_SET(cpu, cpusetp) \
((cpusetp)->__bits[(cpu)/__NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
#define CPU_ZERO(cpusetp) \
memset((cpusetp), 0, sizeof(cpu_set_t))
fprintf(stderr, "WARNING: CPU affinity not supported on Android by now!\n");
return -1;
#else
if (core_id < 0)
return;
int num_cores = get_nprocs();
// int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
if (core_id >= num_cores) {
fprintf(stderr, "WARNING: Cannot run on CPU%d because only %d CPU(s) are available!\n", core_id, num_cores);
return -1;
}
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
pthread_t current_thread = pthread_self();
int ret = pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
if (ret != 0) {
fprintf(stderr, "WARNING: Cannot run on CPU%d, call to pthread_setaffinity_np failed!\n", core_id);
// handle_error_en(ret, "pthread_setaffinity_np");
} else {
fprintf(stderr, "Running on CPU%d out of %d CPU(s)\n", core_id, num_cores);
}
return ret;
#endif
}
int ortd_rt_ChangePriority(unsigned int flags, int priority)
{
// printf("********************************************\n");
if (flags & ORTD_RT_REALTIMETASK) {
/* Declare ourself as a real-time task */
struct sched_param param;
param.sched_priority = priority;
if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
perror("sched_setscheduler failed");
return(-1);
}
/* Lock memory */
//#ifdef __ORTD_TARGET_ANDROID
#ifdef __SYSTEMAPI_ANDROID
fprintf(stderr, "WARNING: mlockall is not provided by Android\n"); // Android does not provide mlockall
#else
if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
perror("mlockall failed");
return(-2);
}
#endif
/* Pre-fault our stack */
ortd_rt_stack_prefault();
fprintf(stderr, "realtime.c: initialised a real-time thread\n");
} else {
struct sched_param param;
param.sched_priority = priority;
if(sched_setscheduler(0, SCHED_OTHER, ¶m) == -1) {
perror("sched_setscheduler failed");
return -1;
}
#ifdef DEBUG
fprintf(stderr, "realtime.c: initialised a non real-time thread\n");
#endif
}
}
int ortd_pthread_cancel(pthread_t thread) {
// #ifdef __ORTD_TARGET_ANDROID
#ifdef __SYSTEMAPI_ANDROID
int status;
// workaround for the missing pthread_cancel in the android NDK
// http://stackoverflow.com/questions/4610086/pthread-cancel-alternatives-in-android-ndk
if ( (status = pthread_kill(thread, SIGUSR1)) != 0)
{
// fprintf(stderr, "Error cancelling thread %d, error = %d (%s)\n", thread, status, strerror status);
}
return status;
#else
return pthread_cancel(thread);
#endif
}
#endif
#ifdef __ORTD_TARGET_RTAI
// RTAI.
// These are dummy entries by now
int ortd_rt_ChangePriority(unsigned int flags, int priority)
{
printf("realtime.c: ortd_rt_ChangePriority not implemented by now for this target\n");
}
int ortd_rt_SetThreadProperties2(struct TaskPriority_t TaskPriority)
{
return ortd_rt_SetThreadProperties(TaskPriority.par, TaskPriority.Npar);
}
int ortd_rt_SetThreadProperties(int *par, int Npar)
{
printf("realtime.c: ortd_rt_SetThreadProperties not implemented by now for this target\n");
}
void ortd_InstallThreadSignalHandler() {
}
int ortd_pthread_cancel(pthread_t thread) {
return pthread_cancel(thread);
}
long int ortd_mu_time()
{
struct timeval mytime;
struct timezone myzone;
gettimeofday(&mytime, &myzone);
return (1000000*mytime.tv_sec+mytime.tv_usec);
} /* mu_time */
#endif
// #ifdef __ORTD_TARGET_MACOSX
#ifdef __SYSTEMAPI_MACOSX
#include <mach/mach.h>
#include <mach/mach_time.h>
int move_pthread_to_realtime_scheduling_class(double Tcomputation, double Tconstraint)
{
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
const uint64_t NANOS_PER_MSEC = 1000000ULL;
double clock2abs = ((double)timebase_info.denom / (double)timebase_info.numer) * NANOS_PER_MSEC;
thread_time_constraint_policy_data_t policy;
policy.period = 0;
policy.computation = (uint32_t)(Tcomputation * clock2abs); // 5 ms of work
policy.constraint = (uint32_t)(Tconstraint * clock2abs);
policy.preemptible = FALSE;
int kr = thread_policy_set(pthread_mach_thread_np(pthread_self()),
THREAD_TIME_CONSTRAINT_POLICY,
(thread_policy_t)&policy,
THREAD_TIME_CONSTRAINT_POLICY_COUNT);
if (kr != KERN_SUCCESS) {
mach_error("thread_policy_set:", kr);
fprintf(stderr, "failed to enter Mach real-time scheduling class\n");
return -1;
}
#ifdef DEBUG
fprintf(stderr, "Entered Mach real-time scheduling class\n");
#endif
return 0;
}
// The signal handler for the threads
void ortd_thread_exit_handler(int sig)
{
#ifdef DEBUG
printf("ortd_thread_exit_handler: this signal is %d \n", sig);
#endif
pthread_exit(0);
}
void ortd_InstallThreadSignalHandler() {
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = ortd_thread_exit_handler;
int rc = sigaction(SIGUSR1,&actions,NULL);
}
// These are dummy entries by now
int ortd_rt_ChangePriority(unsigned int flags, int priority)
{
// printf("realtime.c: ortd_rt_ChangePriority not implemented by now for this target\n");
if (flags & ORTD_RT_REALTIMETASK) {
return move_pthread_to_realtime_scheduling_class(5.0, 10.0); // TODO variable params here
} else {
// normal user task
return 0;
}
}
int ortd_rt_SetThreadProperties2(struct TaskPriority_t TaskPriority)
{
return ortd_rt_SetThreadProperties(TaskPriority.par, TaskPriority.Npar);
}
int ortd_rt_SetThreadProperties(int *par, int Npar)
{
// printf("realtime.c: ortd_rt_SetThreadProperties not implemented by now for this target\n");
// set signal handlers
ortd_InstallThreadSignalHandler();
// set priorities and cpu affinity
if (Npar >= 3) {
// ok got prio
#ifdef DEBUG
fprintf(stderr, "Task Prio1 (flags) would be %d (1 means ORTD_RT_REALTIMETASK)\n", par[0]);
fprintf(stderr, "Task Prio2 would be %d\n", par[1]);
fprintf(stderr, "Task CPU would be %d\n", par[2]);
#endif
// Set CPU
// ortd_rt_SetCore(par[2]);
// set the tasks priority
ortd_rt_ChangePriority(par[0], par[1]);
// ortd_rt_stack_prefault();
#ifdef DEBUG
fprintf(stderr, "realtime.c: Successfully set the task properties\n");
#endif
return 0;
}
#ifdef DEBUG
fprintf(stderr, "realtime.c: no task properties were given\n");
#endif
return -1;
}
int ortd_pthread_cancel(pthread_t thread) {
return pthread_cancel(thread);
}
long int ortd_mu_time()
{
struct timeval mytime;
struct timezone myzone;
gettimeofday(&mytime, &myzone);
return (1000000*mytime.tv_sec+mytime.tv_usec);
} /* mu_time */
#endif