11#include " rtapi.h"
22#include " uspace_rtapi.hh"
33
4+ #include < stdio.h>
5+ #include < stdlib.h>
6+ #include < string.h>
7+
48std::atomic_int WithRoot::level;
59uid_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}
0 commit comments