Skip to content

Commit

Permalink
Fix the check for cpu being in cpuset range
Browse files Browse the repository at this point in the history
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
hallyn committed Apr 30, 2015
1 parent c0adec8 commit fa47bb5
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ stamp-h1
lxcfs.1
share/00-lxcfs.conf
share/lxc.mount.hook
tests/cpusetrange
tests/test-read
cpuset.o
7 changes: 5 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AM_LDFLAGS = $(DBUS_LIBS) $(NIH_LIBS) $(NIH_DBUS_LIBS) $(CGMANAGER_LIBS) $(FUSE_

bin_PROGRAMS = lxcfs

lxcfs_SOURCES = lxcfs.c cgmanager.c cgmanager.h
lxcfs_SOURCES = lxcfs.c cgmanager.c cgmanager.h cpuset.c

EXTRA_DIST = \
lxcfs.man.add
Expand All @@ -28,7 +28,10 @@ endif
TEST_READ: tests/test-read.c
$(CC) -o tests/test-read tests/test-read.c

tests: TEST_READ
TEST_CPUSET: tests/cpusetrange.c cpuset.c
$(CC) -o tests/cpusetrange tests/cpusetrange.c cpuset.c

tests: TEST_READ TEST_CPUSET

distclean:
rm -rf .deps/ \
Expand Down
45 changes: 45 additions & 0 deletions cpuset.c
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;
}

42 changes: 1 addition & 41 deletions lxcfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1684,47 +1684,7 @@ static char *get_cpuset(const char *cg)
return answer;
}

/*
* Helper functions for cpuset_in-set
*/
char *cpuset_nexttok(const char *c)
{
char *r = strchr(c+1, ',');
if (r)
return r+1;
return NULL;
}

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
*/
static 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)
return true;
if (ret != 2) // bad cpuset!
return false;
if (cpu >= a && cpu <= b)
return true;
}

return false;
}
bool cpu_in_cpuset(int cpu, const char *cpuset);

static bool cpuline_in_cpuset(const char *line, const char *cpuset)
{
Expand Down
50 changes: 50 additions & 0 deletions tests/cpusetrange.c
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));
}

0 comments on commit fa47bb5

Please sign in to comment.