Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/cpuset/cpuset.c
Show All 33 Lines | |||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/time.h> | #include <sys/time.h> | ||||
#include <sys/resource.h> | #include <sys/resource.h> | ||||
#include <sys/cpuset.h> | #include <sys/cpuset.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <libutil.h> | |||||
#include <limits.h> | #include <limits.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <string.h> | #include <string.h> | ||||
static int Cflag; | static int Cflag; | ||||
Show All 12 Lines | |||||
static cpulevel_t level; | static cpulevel_t level; | ||||
static cpuwhich_t which; | static cpuwhich_t which; | ||||
static void usage(void); | static void usage(void); | ||||
static void printset(cpuset_t *mask); | static void printset(cpuset_t *mask); | ||||
static void | static void | ||||
parselist(char *list, cpuset_t *mask) | |||||
{ | |||||
enum { NONE, NUM, DASH } state; | |||||
int lastnum; | |||||
int curnum; | |||||
char *l; | |||||
if (strcasecmp(list, "all") == 0) { | |||||
if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1, | |||||
sizeof(*mask), mask) != 0) | |||||
err(EXIT_FAILURE, "getaffinity"); | |||||
return; | |||||
} | |||||
state = NONE; | |||||
curnum = lastnum = 0; | |||||
for (l = list; *l != '\0';) { | |||||
if (isdigit(*l)) { | |||||
curnum = atoi(l); | |||||
if (curnum > CPU_SETSIZE) | |||||
errx(EXIT_FAILURE, | |||||
"Only %d cpus supported", CPU_SETSIZE); | |||||
while (isdigit(*l)) | |||||
l++; | |||||
switch (state) { | |||||
case NONE: | |||||
lastnum = curnum; | |||||
state = NUM; | |||||
break; | |||||
case DASH: | |||||
for (; lastnum <= curnum; lastnum++) | |||||
CPU_SET(lastnum, mask); | |||||
state = NONE; | |||||
break; | |||||
case NUM: | |||||
default: | |||||
goto parserr; | |||||
} | |||||
continue; | |||||
} | |||||
switch (*l) { | |||||
case ',': | |||||
switch (state) { | |||||
case NONE: | |||||
break; | |||||
case NUM: | |||||
CPU_SET(curnum, mask); | |||||
state = NONE; | |||||
break; | |||||
case DASH: | |||||
goto parserr; | |||||
break; | |||||
} | |||||
break; | |||||
case '-': | |||||
if (state != NUM) | |||||
goto parserr; | |||||
state = DASH; | |||||
break; | |||||
default: | |||||
goto parserr; | |||||
} | |||||
l++; | |||||
} | |||||
switch (state) { | |||||
case NONE: | |||||
break; | |||||
case NUM: | |||||
CPU_SET(curnum, mask); | |||||
break; | |||||
case DASH: | |||||
goto parserr; | |||||
} | |||||
return; | |||||
parserr: | |||||
errx(EXIT_FAILURE, "Malformed cpu-list %s", list); | |||||
} | |||||
static void | |||||
printset(cpuset_t *mask) | printset(cpuset_t *mask) | ||||
{ | { | ||||
int once; | int once; | ||||
int cpu; | int cpu; | ||||
for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { | for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { | ||||
if (CPU_ISSET(cpu, mask)) { | if (CPU_ISSET(cpu, mask)) { | ||||
if (once == 0) { | if (once == 0) { | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | case 'i': | ||||
break; | break; | ||||
case 'j': | case 'j': | ||||
jflag = 1; | jflag = 1; | ||||
which = CPU_WHICH_JAIL; | which = CPU_WHICH_JAIL; | ||||
id = atoi(optarg); | id = atoi(optarg); | ||||
break; | break; | ||||
case 'l': | case 'l': | ||||
lflag = 1; | lflag = 1; | ||||
parselist(optarg, &mask); | switch (cpuset_parselist(optarg, &mask)) { | ||||
case CPUSET_PARSE_ERROR: | |||||
errx(EXIT_FAILURE, "Malformed cpu-list %s", | |||||
optarg); | |||||
break; | |||||
case CPUSET_PARSE_GETAFFINITY: | |||||
errx(EXIT_FAILURE, "getaffinity"); | |||||
break; | |||||
case CPUSET_PARSE_INVALID_CPU: | |||||
errx(EXIT_FAILURE, | |||||
"Only %d cpus supported", CPU_SETSIZE); | |||||
break; | |||||
} | |||||
break; | break; | ||||
case 'p': | case 'p': | ||||
pflag = 1; | pflag = 1; | ||||
which = CPU_WHICH_PID; | which = CPU_WHICH_PID; | ||||
id = pid = atoi(optarg); | id = pid = atoi(optarg); | ||||
break; | break; | ||||
case 'r': | case 'r': | ||||
level = CPU_LEVEL_ROOT; | level = CPU_LEVEL_ROOT; | ||||
▲ Show 20 Lines • Show All 119 Lines • Show Last 20 Lines |