Skip to content

Commit 47bb237

Browse files
committed
Cleanup: Split out posix
1 parent 2f4d4a7 commit 47bb237

7 files changed

Lines changed: 351 additions & 336 deletions

File tree

src/rtapi/Submakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ifeq ($(BUILD_SYS),uspace)
3434
RTAPI_APP_SRCS := \
3535
rtapi/uspace_rtapi.cc \
3636
rtapi/uspace_rtapi_app.cc \
37+
rtapi/uspace_rtapi_posix.cc \
3738
rtapi/uspace_rtapi_parport.cc \
3839
rtapi/uspace_rtapi_string.c \
3940
rtapi/rtapi_pci.cc

src/rtapi/uspace_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <sys/time.h>
2222
#include <time.h>
2323
#include <stdio.h>
24+
#include <stdlib.h>
2425
#include <sys/utsname.h>
2526
#include <string.h>
2627
#include <unistd.h>

src/rtapi/uspace_rtapi.cc

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "rtapi.h"
22
#include "uspace_rtapi.hh"
33

4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
48
std::atomic_int WithRoot::level;
59
uid_t euid, ruid;
610

@@ -161,4 +165,94 @@ long RtapiApp::clock_set_period(long nsecs)
161165
}
162166
period = nsecs;
163167
return period;
168+
}
169+
170+
//parse_cpu_list from https://gitlab.com/Xenomai/xenomai4/libevl/-/blob/11e6a1fb183a315ae861762e7650fd5e10d83ff5/tests/helpers.c
171+
//License: MIT
172+
static void parse_cpu_list(const char *path, cpu_set_t *cpuset)
173+
{
174+
char *p, *range, *range_p = NULL, *id, *id_r;
175+
int start, end, cpu;
176+
char buf[BUFSIZ];
177+
FILE *fp;
178+
179+
CPU_ZERO(cpuset);
180+
181+
fp = fopen(path, "r");
182+
if (fp == NULL)
183+
return;
184+
185+
if (!fgets(buf, sizeof(buf), fp))
186+
goto out;
187+
188+
p = buf;
189+
while ((range = strtok_r(p, ",", &range_p)) != NULL) {
190+
if (*range == '\0' || *range == '\n')
191+
goto next;
192+
end = -1;
193+
id = strtok_r(range, "-", &id_r);
194+
if (id) {
195+
start = atoi(id);
196+
id = strtok_r(NULL, "-", &id_r);
197+
if (id)
198+
end = atoi(id);
199+
else if (end < 0)
200+
end = start;
201+
for (cpu = start; cpu <= end; cpu++)
202+
CPU_SET(cpu, cpuset);
203+
}
204+
next:
205+
p = NULL;
206+
}
207+
out:
208+
fclose(fp);
209+
}
210+
211+
int find_rt_cpu_number() {
212+
if(getenv("RTAPI_CPU_NUMBER")) return atoi(getenv("RTAPI_CPU_NUMBER"));
213+
214+
#ifdef __linux__
215+
const char* isolated_file="/sys/devices/system/cpu/isolated";
216+
cpu_set_t cpuset;
217+
218+
parse_cpu_list(isolated_file, &cpuset);
219+
220+
//Print list
221+
rtapi_print_msg(RTAPI_MSG_INFO, "cpuset isolated ");
222+
for(int i=0; i<CPU_SETSIZE; i++) {
223+
if(CPU_ISSET(i, &cpuset)){
224+
rtapi_print_msg(RTAPI_MSG_INFO, "%i ", i);
225+
}
226+
}
227+
rtapi_print_msg(RTAPI_MSG_INFO, "\n");
228+
229+
int top = -1;
230+
for(int i=0; i<CPU_SETSIZE; i++) {
231+
if(CPU_ISSET(i, &cpuset)) top = i;
232+
}
233+
if(top == -1){
234+
rtapi_print_msg(RTAPI_MSG_ERR, "No isolated CPU's found, expect some latency or set RTAPI_CPU_NUMBER to select CPU\n");
235+
}
236+
return top;
237+
#else
238+
return (-1);
239+
#endif
240+
}
241+
242+
void set_namef(const char *fmt, ...) {
243+
char *buf = NULL;
244+
va_list ap;
245+
246+
va_start(ap, fmt);
247+
if (vasprintf(&buf, fmt, ap) < 0) {
248+
va_end(ap);
249+
return;
250+
}
251+
va_end(ap);
252+
253+
int res = pthread_setname_np(pthread_self(), buf);
254+
if (res) {
255+
fprintf(stderr, "pthread_setname_np() failed for %s: %d\n", buf, res);
256+
}
257+
free(buf);
164258
}

src/rtapi/uspace_rtapi.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ T *rtapi_get_task(int task_id) {
108108
}
109109

110110
int find_rt_cpu_number();
111+
void set_namef(const char *fmt, ...);
111112

112113
#define MAX_TASKS 64
113114
#define TASK_MAGIC 21979 /* random numbers used as signatures */

0 commit comments

Comments
 (0)