From 125b6a9191a1b09dbeaa1a068e7b0a966fca03fa Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 24 May 2010 14:13:33 +0200 Subject: [PATCH] tests: add cpuset regression test Signed-off-by: Karel Zak --- lib/Makefile.am | 3 +- lib/cpuset.c | 70 ++++++++++++++++++++++++++++++++ tests/commands.sh.in | 1 + tests/expected/schedutils/cpuset | 24 +++++++++++ tests/ts/schedutils/cpuset | 57 ++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tests/expected/schedutils/cpuset create mode 100755 tests/ts/schedutils/cpuset diff --git a/lib/Makefile.am b/lib/Makefile.am index 0f008b30..93b79a5a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,13 +3,14 @@ include $(top_srcdir)/config/include-Makefile.am AM_CPPFLAGS += -DTEST_PROGRAM noinst_PROGRAMS = test_blkdev test_ismounted test_wholedisk test_mangle \ - test_strtosize + test_strtosize test_cpuset test_blkdev_SOURCES = blkdev.c test_ismounted_SOURCES = ismounted.c test_wholedisk_SOURCES = wholedisk.c test_mangle_SOURCES = mangle.c test_strtosize_SOURCES = strtosize.c +test_cpuset_SOURCES = cpuset.c if LINUX test_blkdev_SOURCES += linux_version.c diff --git a/lib/cpuset.c b/lib/cpuset.c index abdd4090..92ed7304 100644 --- a/lib/cpuset.c +++ b/lib/cpuset.c @@ -239,3 +239,73 @@ int cstr_to_cpuset(struct bitmask *mask, const char* str) return 0; } + +#ifdef TEST_PROGRAM + +#include +#include + +int main(int argc, char *argv[]) +{ + struct bitmask *set; + char *buf, *mask = NULL, *range = NULL; + int ncpus = 2048, rc, c; + + struct option longopts[] = { + { "ncpus", 1, 0, 'n' }, + { "mask", 1, 0, 'm' }, + { "range", 1, 0, 'r' }, + { NULL, 0, 0, 0 } + }; + + while ((c = getopt_long(argc, argv, "n:m:r:", longopts, NULL)) != -1) { + switch(c) { + case 'n': + ncpus = atoi(optarg); + break; + case 'm': + mask = strdup(optarg); + break; + case 'r': + range = strdup(optarg); + break; + default: + goto usage_err; + } + } + + if (!mask && !range) + goto usage_err; + + set = bitmask_alloc(ncpus); + if (!set) + err(EXIT_FAILURE, "failed to allocate cpu set"); + + buf = malloc(7 * ncpus); + if (!buf) + err(EXIT_FAILURE, "failed to allocate cpu set buffer"); + + if (mask) + rc = str_to_cpuset(set, mask); + else + rc = cstr_to_cpuset(set, range); + + if (rc) + errx(EXIT_FAILURE, "failed to parse string: %s", mask ? : range); + + printf("%-15s = %15s ", mask ? : range, cpuset_to_str(set, buf)); + printf("[%s]\n", cpuset_to_cstr(set, buf)); + + free(buf); + free(set->maskp); + free(set); + + return EXIT_SUCCESS; + +usage_err: + fprintf(stderr, + "usage: %s [--ncpus ] --mask | --range ", + program_invocation_short_name); + exit(EXIT_FAILURE); +} +#endif diff --git a/tests/commands.sh.in b/tests/commands.sh.in index f52a6868..94f6521e 100644 --- a/tests/commands.sh.in +++ b/tests/commands.sh.in @@ -11,6 +11,7 @@ TS_HELPER_MD5="$TS_TOPDIR/helpers/test_md5" TS_HELPER_ISMOUNTED="$TOPDIR/lib/test_ismounted" TS_HELPER_STRTOSIZE="$TOPDIR/lib/test_strtosize" +TS_HELPER_CPUSET="$TOPDIR/lib/test_cpuset" # TODO: use partx TS_HELPER_PARTITIONS="$TOPDIR/shlibs/blkid/samples/partitions" diff --git a/tests/expected/schedutils/cpuset b/tests/expected/schedutils/cpuset new file mode 100644 index 00000000..10e17606 --- /dev/null +++ b/tests/expected/schedutils/cpuset @@ -0,0 +1,24 @@ +masks: +0x00000001 = 1 [0] +0x00000002 = 2 [1] +0x00000003 = 3 [0,1] +0x00000004 = 4 [2] +0x00000005 = 5 [0,2] +0x00000006 = 6 [1,2] +0x00000007 = 7 [0-2] +0x00000008 = 8 [3] +0x00000009 = 9 [0,3] +0x00005555 = 5555 [0,2,4,6,8,10,12,14] +0x00007777 = 7777 [0-2,4-6,8-10,12-14] +strings: +0 = 1 [0] +1 = 2 [1] +0,1 = 3 [0,1] +2 = 4 [2] +0,2 = 5 [0,2] +1,2 = 6 [1,2] +0-2 = 7 [0-2] +3 = 8 [3] +0,3 = 9 [0,3] +0,2,4,6,8,10,12,14 = 5555 [0,2,4,6,8,10,12,14] +0-2,4-6,8-10,12-14 = 7777 [0-2,4-6,8-10,12-14] diff --git a/tests/ts/schedutils/cpuset b/tests/ts/schedutils/cpuset new file mode 100755 index 00000000..6ad240dc --- /dev/null +++ b/tests/ts/schedutils/cpuset @@ -0,0 +1,57 @@ +#!/bin/bash + +# +# This file is part of util-linux-ng. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +TS_TOPDIR="$(dirname $0)/../.." +TS_DESC="cpuset" + +. $TS_TOPDIR/functions.sh +ts_init "$*" + +MASKS=" 0x00000001 \ + 0x00000002 \ + 0x00000003 \ + 0x00000004 \ + 0x00000005 \ + 0x00000006 \ + 0x00000007 \ + 0x00000008 \ + 0x00000009 \ + 0x00005555 \ + 0x00007777" + +RANGES="0 \ + 1 \ + 0,1 \ + 2 \ + 0,2 \ + 1,2 \ + 0-2 \ + 3 \ + 0,3 \ + 0,2,4,6,8,10,12,14 \ + 0-2,4-6,8-10,12-14" + +ts_log "masks:" +for i in $MASKS; do + $TS_HELPER_CPUSET --mask $i >> $TS_OUTPUT +done + +ts_log "strings:" +for i in $RANGES; do + $TS_HELPER_CPUSET --range $i >> $TS_OUTPUT +done + +ts_finalize -- 2.39.5