File: linux-sched_setaffinity.xs

package info (click to toggle)
libsys-cpuaffinity-perl 1.13~03-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: perl: 1,261; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download | duplicates (2)
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
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>

#include <linux/unistd.h>
#include <sched.h>

=pod

linux-sched_setaffinity.xs: update CPU affinity with the Linux
sched_setaffinity(2)  system call. See also:
linux-sched_getaffinity.xs .

=cut




MODULE = Sys::CpuAffinity        PACKAGE = Sys::CpuAffinity

int
xs_sched_setaffinity_set_affinity(pid,mask,debug_flag)
    int pid
    AV *mask
    int debug_flag
  CODE:
    static cpu_set_t cpumask;
    int i,r;
    
    CPU_ZERO(&cpumask);
    for (i=0; i <= av_len(mask); i++) {
        int c = SvIV(*av_fetch(mask,i,0));
        if (debug_flag) fprintf(stderr,"sched_setaffinity%d = %d\n", i, c);
        CPU_SET(c, &cpumask);
    }
    r = sched_setaffinity(pid, sizeof(cpu_set_t), &cpumask);
if (debug_flag) fprintf(stderr,"sched_setaffinity(%d,%ld,...) = %d\n", pid, (long unsigned int) sizeof(cpu_set_t), r);
    if (r != 0) {
      fprintf(stderr,"result: %d %d %s\n", r, errno,
            errno==EFAULT ? "EFAULT" /* a supplied memory address was invalid */
          : errno==EINVAL ? "EINVAL" /* the affinity bitmask contains no
                                        processors that are physically on the
                                        system, or _cpusetsize_ is smaller than
                                        the size of the affinity mask used by
                                        the kernel */
          : errno==EPERM ? "EPERM"   /* the calling process does not have
                                        appropriate privilieges. The process
                                        calling *sched_setaffinity()* needs an
                                        effective user ID equal to the user ID
                                        or effective user ID of the process
                                        identified by _pid_, or it must possess
                                        the _CAP_SYS_NICE_ capability. */
          : errno==ESRCH ? "ESRCH"   /* the process whose ID is _pid_ could not
                                        be found */
              :"E_WTF");
    }
    RETVAL = !r;
  OUTPUT:
    RETVAL