-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreads.c
332 lines (301 loc) · 10.6 KB
/
threads.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
/* threads.c
*
* allow to change thread priorities and affinities
*
* Copyright (C) 2018 Dirk Zimoch
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "epicsStdioRedirect.h"
#include "epicsString.h"
#include "epicsThread.h"
#include "iocsh.h"
#include "errlog.h"
#include "epicsExport.h"
#ifndef CPU_SETSIZE
typedef unsigned int cpu_set_t;
#define CPU_SETSIZE (8*sizeof(cpu_set_t))
#define CPU_ZERO(set) (*(set)=0)
#define CPU_SET(cpu, set) (*(set)|=(1<<cpu))
#define CPU_CLR(cpu, set) (*(set)&=~(1<<cpu))
#define CPU_ISSET(cpu, set) (!!(*(set)&(1<<cpu)))
#endif
#ifndef __GLIBC_PREREQ
#define __GLIBC_PREREQ(a,b) 0
#endif
#if !__GLIBC_PREREQ(2,4)
#define pthread_t unsigned int
#define pthread_getaffinity_np(id, size, set) ENOSYS
#define pthread_setaffinity_np(id, size, set) ENOSYS
#define pthread_self() 0
#define epicsThreadGetPosixThreadId(id) 0
#endif
/* Original epicsThreadPriorityMax 99 is wrong */
#undef epicsThreadPriorityMax
#define epicsThreadPriorityMax (100)
static const iocshArg epicsThreadSetPriorityArg0 = { "thread", iocshArgString };
static const iocshArg epicsThreadSetPriorityArg1 = { "priority", iocshArgString };
static const iocshArg epicsThreadSetPriorityArg2 = { "[+/-diff]", iocshArgInt };
static const iocshArg * const epicsThreadSetPriorityArgs[3] = { &epicsThreadSetPriorityArg0, &epicsThreadSetPriorityArg1, &epicsThreadSetPriorityArg2};
static const iocshFuncDef epicsThreadSetPriorityDef = { "epicsThreadSetPriority", 3, epicsThreadSetPriorityArgs };
static epicsThreadId epicsThreadGetIdFromNameOrNumber(const char* threadname)
{
epicsThreadId id;
char* end;
#if defined(__LP64__) || defined(_WIN64)
id = (epicsThreadId)(size_t) strtoull(threadname, &end, 0);
#else
id = (epicsThreadId)(size_t) strtoul(threadname, &end, 0);
#endif
if (*end || !id)
{
id = epicsThreadGetId(threadname);
if (!id)
{
fprintf(stderr, "Thread %s not found.\n", threadname);
return NULL;
}
}
return id;
}
static int epicsThreadStrToPrio(const char* priostr, int diff)
{
int priority;
char* end;
priority = strtol(priostr, &end, 0);
if (!*end)
{
if (priority < epicsThreadPriorityMin ||
priority > epicsThreadPriorityMax)
{
fprintf(stderr, "Priority %s out of range %d-%d.\n",
priostr, epicsThreadPriorityMin, epicsThreadPriorityMax);
return -1;
}
}
else
{
if (epicsStrCaseCmp(priostr, "Min") == 0)
priority = epicsThreadPriorityMin;
else if (epicsStrCaseCmp(priostr, "Max") == 0)
priority = epicsThreadPriorityMax;
else if (epicsStrCaseCmp(priostr, "Low") == 0)
priority = epicsThreadPriorityLow;
else if (epicsStrCaseCmp(priostr, "Medium") == 0)
priority = epicsThreadPriorityMedium;
else if (epicsStrCaseCmp(priostr, "High") == 0)
priority = epicsThreadPriorityHigh;
else if (epicsStrCaseCmp(priostr, "CAServerLow") == 0)
priority = epicsThreadPriorityCAServerLow;
else if (epicsStrCaseCmp(priostr, "CAServerHigh") == 0)
priority = epicsThreadPriorityCAServerHigh;
else if (epicsStrCaseCmp(priostr, "ScanLow") == 0)
priority = epicsThreadPriorityScanLow;
else if (epicsStrCaseCmp(priostr, "ScanHigh") == 0)
priority = epicsThreadPriorityScanHigh;
else if (epicsStrCaseCmp(priostr, "Iocsh") == 0)
priority = epicsThreadPriorityIocsh;
else if (epicsStrCaseCmp(priostr, "BaseMax") == 0)
priority = epicsThreadPriorityBaseMax;
else {
epicsThreadId otherthread = epicsThreadGetId(priostr);
if (otherthread)
priority = epicsThreadGetPriority(otherthread);
else
{
fprintf(stderr, "Unknown priority %s.\n", priostr);
return -1;
}
}
while (diff++ < 0 && priority > epicsThreadPriorityMin)
epicsThreadHighestPriorityLevelBelow(priority, (unsigned int *)&priority);
while (diff-- > 0 && priority < 100)
epicsThreadLowestPriorityLevelAbove(priority, (unsigned int *)&priority);
}
return priority;
}
static void epicsThreadSetPriorityFunc(const iocshArgBuf *args)
{
const char* threadname = args[0].sval;
const char* priostr = args[1].sval;
int diff = args[2].ival;
epicsThreadId id;
int priority;
int old_errVerbose = errVerbose;
if (!threadname || !*threadname || !priostr || !*priostr)
{
fprintf(stderr, "usage: epicsThreadSetPriority thread, priority\n");
return;
}
id = epicsThreadGetIdFromNameOrNumber(threadname);
if (!id) return;
priority = epicsThreadStrToPrio(priostr, diff);
if (priority < 0) return;
errVerbose = 1;
epicsThreadSetPriority(id, priority);
errVerbose = old_errVerbose;
}
void epicsThreadPrintAffinityList(cpu_set_t* cpuset)
{
int cpu, first = -2;
for (cpu = 0; cpu <= CPU_SETSIZE; cpu++)
{
if (cpu < CPU_SETSIZE && CPU_ISSET(cpu, cpuset))
{
if (first < 0)
{
if (first == -1) printf(",");
printf("%u", cpu);
first = cpu;
}
}
else
{
if (first >= 0)
{
if (first < cpu-1) printf("-%u", cpu-1);
first = -1;
}
}
}
printf("\n");
}
static int epicsThreadParseAffinityList(const char* cpulist, cpu_set_t* cpuset)
{
int n = 0;
unsigned int first, last, cpu;
char sign = 0, c;
if (sscanf(cpulist, " %[+-]%n", &sign, &n)) cpulist+=n;
if (sign == 0) CPU_ZERO(cpuset);
do {
if (n = 0, sscanf(cpulist, " %[+-]%n", &sign, &n)) cpulist+=n;
if (n = 0, sscanf(cpulist, "%u %n", &first, &n))
{
cpulist+=n;
last = first;
if (n = 0, sscanf(cpulist, "-%u %n", &last, &n)) cpulist+=n;
for (cpu = first; cpu <= last; cpu++)
{
if (cpu >= CPU_SETSIZE) break;
if (sign == '-') CPU_CLR(cpu, cpuset);
else CPU_SET(cpu, cpuset);
}
}
c = 0;
if (n = 0, sscanf(cpulist, "%[,;] %n", &c, &n)) cpulist+=n;
} while (c);
if (cpulist[0])
fprintf(stderr, "rubbish at end of list: \"%s\"\n", cpulist);
return 0;
}
int epicsThreadGetAffinity(epicsThreadId id, cpu_set_t* cpuset)
{
int status = ENOSYS;
pthread_t tid = 0;
if (id) tid = epicsThreadGetPosixThreadId(id);
else tid = pthread_self();
if (tid) status = pthread_getaffinity_np(tid, sizeof(cpu_set_t), cpuset);
switch (status)
{
case 0:
break;
case ESRCH:
fprintf(stderr, "epicsThreadGetAffinity: invalid thread\n");
break;
case ENOSYS:
fprintf(stderr, "epicsThreadGetAffinity: function not implemented\n");
break;
default:
fprintf(stderr, "epicsThreadGetAffinity: unknown error\n");
}
return status;
}
static const iocshArg epicsThreadGetAffinityArg0 = { "thread", iocshArgString };
static const iocshArg * const epicsThreadGetAffinityArgs[1] = { &epicsThreadGetAffinityArg0};
static const iocshFuncDef epicsThreadGetAffinityDef = { "epicsThreadGetAffinity", 1, epicsThreadGetAffinityArgs };
static void epicsThreadGetAffinityFunc(const iocshArgBuf *args)
{
const char* threadname = args[0].sval;
cpu_set_t cpuset;
epicsThreadId id;
if (!threadname || !*threadname)
{
fprintf(stderr, "usage: epicsThreadGetAffinity thread\n");
return;
}
id = epicsThreadGetIdFromNameOrNumber(threadname);
if (epicsThreadGetAffinity(id, &cpuset) != 0) return;
epicsThreadPrintAffinityList(&cpuset);
}
int epicsThreadSetAffinity(epicsThreadId id, cpu_set_t* cpuset)
{
int status = ENOSYS;
pthread_t tid = 0;
if (id) tid = epicsThreadGetPosixThreadId(id);
else tid = pthread_self();
if (tid) status = pthread_setaffinity_np(tid, sizeof(cpu_set_t), cpuset);
switch (status)
{
case 0:
break;
case EINVAL:
fprintf(stderr, "epicsThreadSetAffinity: no cpu left to execute\n");
break;
case ESRCH:
fprintf(stderr, "epicsThreadSetAffinity: invalid thread\n");
break;
case ENOSYS:
fprintf(stderr, "epicsThreadSetAffinity: function not implemented\n");
break;
default:
fprintf(stderr, "epicsThreadSetAffinity: unknown error\n");
}
return status;
}
static const iocshArg epicsThreadSetAffinityArg0 = { "thread", iocshArgString };
static const iocshArg epicsThreadSetAffinityArg1 = { "cpulist", iocshArgString };
static const iocshArg * const epicsThreadSetAffinityArgs[2] = { &epicsThreadSetAffinityArg0, &epicsThreadSetAffinityArg1};
static const iocshFuncDef epicsThreadSetAffinityDef = { "epicsThreadSetAffinity", 2, epicsThreadSetAffinityArgs };
static void epicsThreadSetAffinityFunc(const iocshArgBuf *args)
{
const char* threadname = args[0].sval;
const char* cpulist = args[1].sval;
cpu_set_t cpuset;
epicsThreadId id;
if (!threadname || !*threadname || !cpulist || !*cpulist)
{
fprintf(stderr, "usage: epicsThreadSetAffinity thread, affinitylist\n");
return;
}
id = epicsThreadGetIdFromNameOrNumber(threadname);
if (epicsThreadGetAffinity(id, &cpuset) != 0) return;
epicsThreadParseAffinityList(cpulist, &cpuset);
if (epicsThreadSetAffinity(id, &cpuset) != 0) return;
if (epicsThreadGetAffinity(id, &cpuset) != 0) return;
epicsThreadPrintAffinityList(&cpuset);
}
static void
threadsRegister(void)
{
iocshRegister(&epicsThreadSetPriorityDef, epicsThreadSetPriorityFunc);
iocshRegister(&epicsThreadGetAffinityDef, epicsThreadGetAffinityFunc);
iocshRegister(&epicsThreadSetAffinityDef, epicsThreadSetAffinityFunc);
}
epicsExportRegistrar(threadsRegister);