-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the check for cpu being in cpuset range
Also add a testcase to make sure that the new function is correct. In order to share the cpuset range checking code with with the test, move it into cpuset.c. Not sure whether we want that in a utils.c instead. Signed-off-by: Serge Hallyn <[email protected]>
- Loading branch information
Showing
5 changed files
with
104 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,6 @@ stamp-h1 | |
lxcfs.1 | ||
share/00-lxcfs.conf | ||
share/lxc.mount.hook | ||
tests/cpusetrange | ||
tests/test-read | ||
cpuset.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
* Helper functions for cpuset_in-set | ||
*/ | ||
static char *cpuset_nexttok(const char *c) | ||
{ | ||
char *r = strchr(c+1, ','); | ||
if (r) | ||
return r+1; | ||
return NULL; | ||
} | ||
|
||
static int cpuset_getrange(const char *c, int *a, int *b) | ||
{ | ||
int ret; | ||
|
||
ret = sscanf(c, "%d-%d", a, b); | ||
return ret; | ||
} | ||
|
||
/* | ||
* cpusets are in format "1,2-3,4" | ||
* iow, comma-delimited ranges | ||
*/ | ||
bool cpu_in_cpuset(int cpu, const char *cpuset) | ||
{ | ||
const char *c; | ||
|
||
for (c = cpuset; c; c = cpuset_nexttok(c)) { | ||
int a, b, ret; | ||
|
||
ret = cpuset_getrange(c, &a, &b); | ||
if (ret == 1 && cpu == a) // "1" or "1,6" | ||
return true; | ||
else if (ret == 2 && cpu >= a && cpu <= b) // range match | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
* cpusets are in format "1,2-3,4" | ||
* iow, comma-delimited ranges | ||
*/ | ||
extern bool cpu_in_cpuset(int cpu, const char *cpuset); | ||
|
||
void verify(bool condition) { | ||
if (condition) { | ||
printf(" PASS\n"); | ||
} else { | ||
printf(" FAIL!\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
int main() { | ||
char *a = "1,2"; | ||
char *b = "1-3,5"; | ||
char *c = "1,4-5"; | ||
char *d = ""; | ||
char *e = "\n"; | ||
|
||
printf("1 in %s", a); | ||
verify(cpu_in_cpuset(1, a)); | ||
printf("2 in %s", a); | ||
verify(cpu_in_cpuset(2, a)); | ||
printf("NOT 4 in %s", a); | ||
verify(!cpu_in_cpuset(4, a)); | ||
printf("1 in %s", b); | ||
verify(cpu_in_cpuset(1, b)); | ||
printf("NOT 4 in %s", b); | ||
verify(!cpu_in_cpuset(4, b)); | ||
printf("5 in %s", b); | ||
verify(cpu_in_cpuset(5, b)); | ||
printf("1 in %s", c); | ||
verify(cpu_in_cpuset(1, c)); | ||
printf("5 in %s", c); | ||
verify(cpu_in_cpuset(5, c)); | ||
printf("NOT 6 in %s", c); | ||
verify(!cpu_in_cpuset(6, c)); | ||
printf("NOT 6 in empty set"); | ||
verify(!cpu_in_cpuset(6, d)); | ||
printf("NOT 6 in empty set(2)"); | ||
verify(!cpu_in_cpuset(6, e)); | ||
} |