2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2011 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
9 /* Process RT scheduling */
21 static inline const char *next_token(const char *q
, int sep
)
31 int set_cpu_affinity(const char *str
, int inverted
)
35 cpu_set_t cpu_bitmask
;
38 cpus
= get_number_cpus();
39 CPU_ZERO(&cpu_bitmask
);
41 for (i
= 0; inverted
&& i
< cpus
; ++i
)
42 CPU_SET(i
, &cpu_bitmask
);
44 while (p
= q
, q
= next_token(q
, ','), p
) {
45 unsigned int a
; /* Beginning of range */
46 unsigned int b
; /* End of range */
47 unsigned int s
; /* Stride */
50 if (sscanf(p
, "%u", &a
) < 1)
56 c1
= next_token(p
, '-');
57 c2
= next_token(p
, ',');
59 if (c1
!= NULL
&& (c2
== NULL
|| c1
< c2
)) {
60 if (sscanf(c1
, "%u", &b
) < 1)
62 c1
= next_token(c1
, ':');
63 if (c1
!= NULL
&& (c2
== NULL
|| c1
< c2
))
64 if (sscanf(c1
, "%u", &s
) < 1)
73 CPU_CLR(a
, &cpu_bitmask
);
75 CPU_SET(a
, &cpu_bitmask
);
80 ret
= sched_setaffinity(getpid(), sizeof(cpu_bitmask
),
83 panic("Can't set this cpu affinity!\n");
87 char *get_cpu_affinity(char *cpu_string
, size_t len
)
90 cpu_set_t cpu_bitmask
;
92 if (len
!= get_number_cpus() + 1)
94 CPU_ZERO(&cpu_bitmask
);
96 ret
= sched_getaffinity(getpid(), sizeof(cpu_bitmask
),
99 whine("Can't fetch cpu affinity!\n");
103 for (i
= 0, cpu_string
[len
- 1] = 0; i
< len
- 1; ++i
) {
104 cpu
= CPU_ISSET(i
, &cpu_bitmask
);
105 cpu_string
[i
] = (cpu
? '1' : '0');
111 int set_proc_prio(int priority
)
114 * setpriority() is clever, even if you put a nice value which
115 * is out of range it corrects it to the closest valid nice value
117 int ret
= setpriority(PRIO_PROCESS
, getpid(), priority
);
119 panic("Can't set nice val to %i!\n", priority
);
123 int set_sched_status(int policy
, int priority
)
125 int ret
, min_prio
, max_prio
;
126 struct sched_param sp
;
128 max_prio
= sched_get_priority_max(policy
);
129 min_prio
= sched_get_priority_min(policy
);
131 if (max_prio
== -1 || min_prio
== -1)
132 whine("Cannot determine scheduler prio limits!\n");
133 else if (priority
< min_prio
)
135 else if (priority
> max_prio
)
138 memset(&sp
, 0, sizeof(sp
));
139 sp
.sched_priority
= priority
;
141 ret
= sched_setscheduler(getpid(), policy
, &sp
);
143 whine("Cannot set scheduler policy!\n");
147 ret
= sched_setparam(getpid(), &sp
);
149 whine("Cannot set scheduler prio!\n");