Affinity.xs

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 150 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Linux/Sys/CPU/Affinity.pm »
1 // PERL_NO_GET_CONTEXT is not used here, so it's OK to define it after inculding these files
2 #include "EXTERN.h"
3 #include "perl.h"
4  
5 // There are a lot of macro about threads: USE_ITHREADS, USE_5005THREADS, I_PTHREAD, I_MACH_CTHREADS, OLD_PTHREADS_API
6 // This symbol, if defined, indicates that Perl should be built to use the interpreter-based threading implementation.
7 #ifndef USE_ITHREADS
8 # define PERL_NO_GET_CONTEXT
9 #endif
10  
11 #include "XSUB.h"
12  
13 #ifdef I_PTHREAD
14 # include "pthread.h"
15 #endif
16  
17 #ifdef I_MACH_CTHREADS
18 # include "mach/cthreads.h"
19 #endif
20  
21  
22 #include <sched.h>
23 #include <errno.h>
24 #include <sys/sysinfo.h>
25 //#include <unistd.h>
26  
27 struct xs_state {
28 int max_cpus;
29 cpu_set_t *set;
30 size_t size;
31 char *class_name;
32 };
33  
34 typedef struct xs_state Linux_Sys_CPU_Affinity;
35  
36 0 static inline int get_available_nprocs (void) {
37  
38 static int available_cpus_cnt = 0;
39  
40 /* get available amount of CPU cores */
41 0 ▶ if (available_cpus_cnt == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (available_cpus_cnt == 0) 0 0 0
42 0 available_cpus_cnt = get_nprocs(); // sysconf(_SC_NPROCESSORS_ONLN);
43  
44 /* if we're failed, then use constant, mostly it equals to 1024 */
45 0 ▶ if (available_cpus_cnt == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (available_cpus_cnt == -1) 0 0 0
46 0 available_cpus_cnt = CPU_SETSIZE;
47  
48 0 return available_cpus_cnt;
49 }
50  
51 inline void init_set (Linux_Sys_CPU_Affinity *cpuset, AV *av) {
52  
53 if (cpuset->set != NULL)
54 CPU_FREE(cpuset->set);
55  
56 cpuset->size = CPU_ALLOC_SIZE(cpuset->max_cpus);
57 cpuset->set = CPU_ALLOC(cpuset->max_cpus);
58  
59 if (cpuset->set == NULL) {
60 SV *msg = sv_2mortal( newSVpvf("Failed to allocate memory for %i CPUs", cpuset->max_cpus) );
61 croak((char *) SvPV_nolen(msg));
62 }
63  
64 CPU_ZERO_S(cpuset->size, cpuset->set);
65  
66 if (av != NULL) {
67 SSize_t i;
68 SSize_t av_len = av_len(av) + 1;
69 for (i = 0; i < av_len; i++) {
70 size_t cpu = SvIV((SV*)*av_fetch(av, i, 0)); // SvIVX to coerce value into IV
71 //Perl_warner_nocontext(aTHX_ packWARN(WARN_QW), "value = %i\n", cpu);
72 CPU_SET_S(cpu, cpuset->size, cpuset->set);
73 }
74 }
75 }
76  
77 inline AV* _extract_and_validate_av(SV *sv) {
78  
79 if (!SvOK(sv))
80 croak("the CPU's list can't be undefined");
81  
82 if (SvTIED_mg(sv, PERL_MAGIC_tied))
83 croak("tied objects aren't supported");
84  
85 if (!SvROK(sv))
86 croak("the CPU's list must be an array reference");
87  
88 SV *ref = SvRV(sv);
89 AV *av;
90  
91 switch (SvTYPE(ref)) {
92 case SVt_PVAV: // $ref eq "ARRAY"
93 av = (AV *) ref;
94 break;
95 default: // $ref ne "ARRAY"
96 croak("the CPU's list must be an array reference");
97 }
98  
99 SSize_t i;
100 SSize_t bad_arg = -1;
101 // SV **arr = AvARRAY(av);
102  
103 for (i = av_len(av); i >= 0; i--) {
104 SV *val = (SV *) *av_fetch(av, i, 0);
105 // SV *val = arr[i];
106 if (!SvOK(val) || !SvIOK(val)) {
107 bad_arg = i;
108 break;
109 }
110 }
111  
112 if (bad_arg != -1) {
113 croak("Not an integer at position %i", bad_arg);
114 }
115  
116 return av;
117 }
118  
119 inline Linux_Sys_CPU_Affinity* make_from_object(Linux_Sys_CPU_Affinity *cpuset, int copy_set) {
120  
121 Linux_Sys_CPU_Affinity *new_cpuset = (Linux_Sys_CPU_Affinity *) safemalloc(sizeof(Linux_Sys_CPU_Affinity));
122 uint32_t cpu;
123  
124 new_cpuset->max_cpus = cpuset->max_cpus;
125 new_cpuset->class_name = cpuset->class_name;
126  
127 init_set(new_cpuset, NULL);
128  
129 if (copy_set != 0)
130 for (cpu = 0; cpu < new_cpuset->max_cpus; cpu++) {
131 int res = CPU_ISSET_S(cpu, cpuset->size, cpuset->set);
132 if (res != 0)
133 CPU_SET_S(cpu, new_cpuset->size, new_cpuset->set);
134 }
135  
136 return new_cpuset;
137 }
138  
139 inline AV* get_cpus_from_cpuset(Linux_Sys_CPU_Affinity *cpuset) {
140 AV* av = newAV();
141 uint32_t cpu;
142 for (cpu = 0; cpu < cpuset->max_cpus; cpu++) {
143 int res = CPU_ISSET_S(cpu, cpuset->size, cpuset->set);
144 if (res != 0)
145 av_push(av, newSVuv(cpu));
146 }
147 sv_2mortal((SV*) av);
148 return av;
149 }
150  
151 0 SV* get_sched_error_text(int error_code, int is_get_affinity) {
152 0 SV *error = sv_2mortal(newSV(0));
153 0 switch (error_code) {
154 0 case EFAULT:
155 0 sv_setpv(error, "A supplied memory address was invalid");
156 0 break;
157 0 case EINVAL:
158 0 ▶ if (is_get_affinity)
Branch: 1 branch
BranchTrueFalseTotal
if (is_get_affinity) 0 0 0
159 0 sv_setpv(error, "The cpusetsize is smaller than the size of the affinity mask used by the kernel");
160 else
161 0 sv_setpv(error, "The affinity bit mask mask contains no processors that are currently physically on the system and permitted to the thread");
162 0 break;
163 0 case EPERM:
164 0 sv_setpv(error, "The calling thread does not have appropriate privileges");
165 0 break;
166 0 case ESRCH:
167 0 sv_setpv(error, "The thread whose ID is pid could not be found");
168 0 break;
169 0 default:
170 0 sv_setpv(error, "Unknown error has occurred");
171 }
172 0 return error;
173 }
174  
175  
176 MODULE = Linux::Sys::CPU::Affinity PACKAGE = Linux::Sys::CPU::Affinity
177  
178 PROTOTYPES: DISABLE
179  
180 Linux_Sys_CPU_Affinity* new(class_name, sv = &PL_sv_undef)
181 char *class_name
182 SV *sv
183 PREINIT:
184 Linux_Sys_CPU_Affinity *cpuset;
185 0 ▶ AV* av = NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV* av = NULL; 0 0 0
186 CODE:
187  
188 0 ▶ if (SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv)) 0 0 0
189 0 av = _extract_and_validate_av(sv);
190  
191 0 cpuset = (Linux_Sys_CPU_Affinity *) safemalloc(sizeof(Linux_Sys_CPU_Affinity));
192  
193 0 cpuset->set = NULL;
194 0 cpuset->max_cpus = get_available_nprocs();
195 0 cpuset->class_name = class_name;
196  
197 0 init_set(cpuset, av);
198  
199 // ENTER; SAVETMPS; // {
200 // code
201 // FREETMPS; LEAVE; // }
202  
203 0 RETVAL = cpuset;
204 OUTPUT:
205 RETVAL
206  
207  
208 Linux_Sys_CPU_Affinity* clone (cpuset)
209 Linux_Sys_CPU_Affinity *cpuset
210 CODE:
211 0 char* class_name = cpuset->class_name;
212 0 RETVAL = make_from_object(cpuset, 1);
213 OUTPUT:
214 RETVAL
215  
216  
217 Linux_Sys_CPU_Affinity* cpu_and (cpusetA, cpusetB)
218 Linux_Sys_CPU_Affinity *cpusetA
219 Linux_Sys_CPU_Affinity *cpusetB
220 CODE:
221 0 char* class_name = cpusetA->class_name;
222  
223 0 ▶ if (cpusetA->size != cpusetB->size)
Branch: 1 branch
BranchTrueFalseTotal
if (cpusetA->size != cpusetB->size) 0 0 0
224 0 croak("The size of given cpusets are different");
225
226 0 Linux_Sys_CPU_Affinity *new_cpuset = make_from_object(cpusetA, 1);
227  
228 0 ▶ CPU_AND_S(cpusetA->size, new_cpuset->set, cpusetA->set, cpusetB->set);
Branch: 1 branch
BranchTrueFalseTotal
CPU_AND_S(cpusetA->size, new_cpuset->set, cpusetA->set, cpusetB->set); 0 0 0
229 0 RETVAL = new_cpuset;
230 OUTPUT:
231 RETVAL
232  
233  
234 Linux_Sys_CPU_Affinity* cpu_or (cpusetA, cpusetB)
235 Linux_Sys_CPU_Affinity *cpusetA
236 Linux_Sys_CPU_Affinity *cpusetB
237 CODE:
238 0 char* class_name = cpusetA->class_name;
239  
240 0 ▶ if (cpusetA->size != cpusetB->size)
Branch: 1 branch
BranchTrueFalseTotal
if (cpusetA->size != cpusetB->size) 0 0 0
241 0 croak("The size of given cpusets are different");
242
243 0 Linux_Sys_CPU_Affinity *new_cpuset = make_from_object(cpusetA, 1);
244  
245 0 ▶ CPU_OR_S(cpusetA->size, new_cpuset->set, cpusetA->set, cpusetB->set);
Branch: 1 branch
BranchTrueFalseTotal
CPU_OR_S(cpusetA->size, new_cpuset->set, cpusetA->set, cpusetB->set); 0 0 0
246 0 RETVAL = new_cpuset;
247 OUTPUT:
248 RETVAL
249  
250  
251 Linux_Sys_CPU_Affinity* cpu_xor (cpusetA, cpusetB)
252 Linux_Sys_CPU_Affinity *cpusetA
253 Linux_Sys_CPU_Affinity *cpusetB
254 CODE:
255 0 char* class_name = cpusetA->class_name;
256  
257 0 ▶ if (cpusetA->size != cpusetB->size)
Branch: 1 branch
BranchTrueFalseTotal
if (cpusetA->size != cpusetB->size) 0 0 0
258 0 croak("The size of given cpusets are different");
259
260 0 Linux_Sys_CPU_Affinity *new_cpuset = make_from_object(cpusetA, 1);
261  
262 0 ▶ CPU_XOR_S(cpusetA->size, new_cpuset->set, cpusetA->set, cpusetB->set);
Branch: 1 branch
BranchTrueFalseTotal
CPU_XOR_S(cpusetA->size, new_cpuset->set, cpusetA->set, cpusetB->set); 0 0 0
263 0 RETVAL = new_cpuset;
264 OUTPUT:
265 RETVAL
266  
267  
268 void cpu_zero(cpuset)
269 Linux_Sys_CPU_Affinity *cpuset
270 CODE:
271 0 init_set(cpuset, NULL);
272 0 XSRETURN_EMPTY;
273  
274  
275 void reset(cpuset, sv = &PL_sv_undef)
276 Linux_Sys_CPU_Affinity *cpuset
277 SV *sv
278 PREINIT:
279 0 AV* av = NULL;
280 CODE:
281 0 ▶ if (SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv)) 0 0 0
282 0 av = _extract_and_validate_av(sv);
283 0 init_set(cpuset, av);
284 0 XSRETURN_EMPTY;
285  
286  
287 IV cpu_isset (cpuset, cpu)
288 Linux_Sys_CPU_Affinity *cpuset
289 UV cpu
290 PPCODE:
291 0 ▶ int res = CPU_ISSET_S((uint32_t) cpu, cpuset->size, cpuset->set);
Branch: 2 branches
BranchTrueFalseTotal
int res = CPU_ISSET_S((uint32_t) cpu, cpuset->size, cpuset->set); 0 0 0
int res = CPU_ISSET_S((uint32_t) cpu, cpuset->size, cpuset->set); 0 0 0
292 0 ▶ mXPUSHu( res );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHu( res ); 0 0 0
293 0 XSRETURN(1);
294  
295  
296 void cpu_set (cpuset, cpu)
297 Linux_Sys_CPU_Affinity *cpuset
298 UV cpu
299 PPCODE:
300 0 ▶ CPU_SET_S((uint32_t) cpu, cpuset->size, cpuset->set);
Branch: 1 branch
BranchTrueFalseTotal
CPU_SET_S((uint32_t) cpu, cpuset->size, cpuset->set); 0 0 0
301 0 XSRETURN_EMPTY;
302  
303  
304 void cpu_clr (cpuset, cpu)
305 Linux_Sys_CPU_Affinity *cpuset
306 UV cpu
307 PPCODE:
308 0 ▶ CPU_CLR_S((uint32_t) cpu, cpuset->size, cpuset->set);
Branch: 1 branch
BranchTrueFalseTotal
CPU_CLR_S((uint32_t) cpu, cpuset->size, cpuset->set); 0 0 0
309 0 XSRETURN_EMPTY;
310  
311  
312 UV cpu_count(cpuset)
313 Linux_Sys_CPU_Affinity *cpuset
314 PPCODE:
315 0 int cpu_count = CPU_COUNT_S(cpuset->size, cpuset->set);
316 0 ▶ mXPUSHu( cpu_count ); // PUSHs(sv_2mortal(newSVuv(cpu_count)));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHu( cpu_count ); // PUSHs(sv_2mortal(newSVuv(cpu_count))); 0 0 0
317 0 XSRETURN(1);
318  
319  
320 IV cpu_equal (cpusetA, cpusetB)
321 Linux_Sys_CPU_Affinity *cpusetA
322 Linux_Sys_CPU_Affinity *cpusetB
323 PPCODE:
324 0 int res = 0;
325 0 ▶ if (cpusetA->size == cpusetB->size)
Branch: 1 branch
BranchTrueFalseTotal
if (cpusetA->size == cpusetB->size) 0 0 0
326 0 res = CPU_EQUAL_S(cpusetA->size, cpusetA->set, cpusetB->set);
327 0 ▶ mXPUSHu( res );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHu( res ); 0 0 0
328 0 XSRETURN(1);
329  
330  
331 AV* get_cpus(cpuset)
332 Linux_Sys_CPU_Affinity *cpuset
333 CODE:
334 0 RETVAL = get_cpus_from_cpuset(cpuset);
335 OUTPUT:
336 RETVAL
337  
338  
339 AV* get_affinity(cpuset, pid)
340 Linux_Sys_CPU_Affinity *cpuset
341 UV pid
342 CODE:
343 0 Linux_Sys_CPU_Affinity *new_cpuset = make_from_object(cpuset, 0);
344 0 int res = sched_getaffinity((pid_t) pid, new_cpuset->size, new_cpuset->set);
345 0 ▶ if (res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == -1) { 0 0 0
346 0 SV *error = get_sched_error_text(errno, 1);
347 0 croak((char *) SvPV_nolen(error));
348 }
349 0 RETVAL = get_cpus_from_cpuset(new_cpuset);
350 0 safefree(new_cpuset);
351 OUTPUT:
352 RETVAL
353  
354  
355 IV set_affinity(cpuset, pid)
356 Linux_Sys_CPU_Affinity *cpuset
357 UV pid
358 PPCODE:
359 0 int res = sched_setaffinity((pid_t) pid, cpuset->size, cpuset->set);
360 0 ▶ if (res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == -1) { 0 0 0
361 0 SV *error = get_sched_error_text(errno, 0);
362 0 croak((char *) SvPV_nolen(error));
363 }
364 0 ▶ mXPUSHi( res );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi( res ); 0 0 0
365 0 XSRETURN(1);
366  
367  
368 void DESTROY (cpuset)
369 PPCODE:
370 0 Linux_Sys_CPU_Affinity *self = (Linux_Sys_CPU_Affinity *) SvUV(SvRV(ST(0)));
371 0 ▶ if (PL_dirty)
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) 0 0 0
372 0 return;
373 0 CPU_FREE(self->set);
374 0 safefree(self);
375 0 XSRETURN_EMPTY;
376  
377  
378 IV get_nprocs ()
379 PPCODE:
380 0 int nprocs = get_available_nprocs();
381 0 ▶ mXPUSHi( nprocs );
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHi( nprocs ); 0 0 0
382 0 XSRETURN(1);
↑ Summary blib/lib/Linux/Sys/CPU/Affinity.pm »