taskset: implement stride argument

function                                             old     new   delta
taskset_main                                         925     986     +61

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2019-11-09 17:05:14 +01:00
parent 2f57b5139e
commit a82fb1b9d8

View File

@ -121,10 +121,8 @@ static unsigned long *get_aff(int pid, unsigned *sz)
* Parse the CPU list and set the mask accordingly. * Parse the CPU list and set the mask accordingly.
* *
* The list element can be either a CPU index or a range of CPU indices. * The list element can be either a CPU index or a range of CPU indices.
* Example: "1,3,5-7". * Example: "1,3,5-7". Stride can be specified: "0-7:2" is "0,2,4,6".
* * Note: leading and trailing whitespace is not allowed.
* note1: stride (e.g. 0-255:2) is not supported
* note2: leading and trailing whitespace is not allowed
* util-linux 2.31 allows leading and sometimes trailing whitespace: * util-linux 2.31 allows leading and sometimes trailing whitespace:
* ok: taskset -c ' 1, 2' * ok: taskset -c ' 1, 2'
* ok: taskset -c ' 1 , 2' * ok: taskset -c ' 1 , 2'
@ -137,21 +135,28 @@ static void parse_cpulist(ul *mask, unsigned max, char *s)
char *aff = s; char *aff = s;
for (;;) { for (;;) {
unsigned bit, end; unsigned bit, end;
unsigned stride = 1;
bit = end = bb_strtou(s, &s, 10); bit = end = bb_strtou(s, &s, 10);
if (*s == '-') { if (*s == '-') {
s++; s++;
end = bb_strtou(s, &s, 10); end = bb_strtou(s, &s, 10);
if (*s == ':') {
s++;
stride = bb_strtou(s, &s, 10);
}
} }
if ((*s != ',' && *s != '\0') if ((*s != ',' && *s != '\0')
|| bit > end || bit > end
|| end == UINT_MAX /* bb_strtou returns this on malformed / ERANGE numbers */ || end == UINT_MAX /* bb_strtou returns this on malformed / ERANGE numbers */
|| stride == 0
|| stride == UINT_MAX
) { ) {
bb_error_msg_and_die("bad affinity '%s'", aff); bb_error_msg_and_die("bad affinity '%s'", aff);
} }
while (bit <= end && bit < max) { while (bit <= end && bit < max) {
mask[bit / BITS_UL] |= (1UL << (bit & MASK_UL)); mask[bit / BITS_UL] |= (1UL << (bit & MASK_UL));
bit++; bit += stride;
} }
if (*s == '\0') if (*s == '\0')
break; break;