Create new library helper: tst_getconf
v2 cover Create new library helper: tst_getconf
Hello everyone,
This is a V2 of my series that handles the fact that "getconf" may not available in some embedded systems.
I updated the patches according to Li Wang's review:
- handle invalid arguments in tst_getconf
- correct arguments in mkswap01.sh and numa01.sh
If you have any review/remark, do not hesitate!
Thank you in advance,
Mylène
Mylène Josserand (2):
testcase: lib: Create tst_getconf to replace "getconf"
testcases: Replace "getconf" by "tst_getconf"
doc/test-writing-guidelines.txt | 14 +++++ testcases/commands/du/du01.sh | 2 +- testcases/commands/mkswap/mkswap01.sh | 2 +- .../controllers/cpuset/cpuset_regression_test.sh | 2 +- .../memcg/control/memcg_control_test.sh | 2 +- .../controllers/memcg/functional/memcg_lib.sh | 4 +- testcases/kernel/numa/numa01.sh | 2 +- testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 3 +- testcases/lib/test.sh | 4 +- testcases/lib/tst_getconf.c | 66 ++++++++++++++++++++++ 11 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 testcases/lib/tst_getconf.c
Signature · 1 line
2.11.0
PATCH v2 1/2 testcase: lib: Create tst_getconf to replace "getconf"
In some system, "getconf" application may not be installed. Some tests are using it to retrieve some variables such as the page size (PAGESIZE).
Create a tst_getconf binary that use sysconf() function to retrieve these variables instead of relying on "getconf" application that may not be available.
Add also this new helper in the documentation.
Example:
pagesize=tst_getconf PAGESIZE
--- doc/test-writing-guidelines.txt | 14 +++++++++ testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 3 +- testcases/lib/tst_getconf.c | 66 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 testcases/lib/tst_getconf.c
doc/test-writing-guidelines.txt +14 -0
@@ -1630,6 +1630,20 @@ passed directly to the script in '$1', '$2', ..., '$n'. 2.3.4 Usefull library functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Retrieving configuration variables +++++++++++++++++++++++++++++++++++ + +You may need to retrieve some variable configuration such as PAGESIZE. +There is "getconf" application that allows that but as some system may not +have it, a "tst_getconf" library allows to retrieve these variables by using +"sysconf()" function. + +[source,sh] +------------------------------------------------------------------------------- +# retrieve PAGESIZE +pagesize=`tst_getconf PAGESIZE` +------------------------------------------------------------------------------- + Sleeping for subsecond intervals ++++++++++++++++++++++++++++++++
testcases/lib/.gitignore +1 -0
@@ -7,3 +7,4 @@ /tst_net_iface_prefix /tst_net_ip_prefix /tst_net_vars +/tst_getconf
testcases/lib/Makefile +2 -1
@@ -27,6 +27,7 @@ include $(top_srcdir)/include/mk/testcases.mk INSTALL_TARGETS := *.sh MAKE_TARGETS := tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\ - tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars + tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\ + tst_getconf include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_getconf.c new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v2 1/2 testcase: lib: Create tst_getconf to replace "getconf"
Hi Mylene,
Quoted 9 lines · Mylène Josserand
In some system, "getconf" application may not be installed. Some tests are using it to retrieve some variables such as the page size (PAGESIZE).
Create a
tst_getconfbinary that usesysconf()function to retrieve these variables instead of relying on "getconf" application that may not be available.
Add also this new helper in the documentation.
Quoted 4 lines
Example:
pagesize=tst_getconf PAGESIZESigned-off-by Mylène Josserand <[email protected]>
Quoted 115 lines
--- doc/test-writing-guidelines.txt | 14 +++++++++ testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 3 +- testcases/lib/tst_getconf.c | 66 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 testcases/lib/tst_getconf.cdoc/test-writing-guidelines.txt +14
-0@@ -1630,6 +1630,20 @@ passed directly to the script in '$1', '$2', ..., '$n'. 2.3.4 Usefull library functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Retrieving configuration variables +++++++++++++++++++++++++++++++++++ + +You may need to retrieve some variable configuration such as PAGESIZE. +There is "getconf" application that allows that but as some system may not +have it, a "tst_getconf" library allows to retrieve these variables by using +"sysconf()" function. + +[source,sh] +------------------------------------------------------------------------------- +# retrieve PAGESIZE +pagesize=`tst_getconf PAGESIZE` +------------------------------------------------------------------------------- + Sleeping for subsecond intervals ++++++++++++++++++++++++++++++++testcases/lib/.gitignore +1
-0@@ -7,3 +7,4 @@ /tst_net_iface_prefix /tst_net_ip_prefix /tst_net_vars +/tst_getconftestcases/lib/Makefile +2
-1@@ -27,6 +27,7 @@ include $(top_srcdir)/include/mk/testcases.mk INSTALL_TARGETS := *.sh MAKE_TARGETS := tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\ - tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars + tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\ + tst_getconf include $(top_srcdir)/include/mk/generic_leaf_target.mktestcases/lib/tst_getconf.c new file +53
-0@@ -0,0 +1,66 @@ +/* +* Copyright (c) 2018 Mylène Josserand <[email protected]> +* +* This program 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 program is distributed in the hope that it would 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. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <stdio.h> +#include <unistd.h> +#include <string.h> + +static void print_help (void ) +{ +printf ("Usage: tst_getconf variable\n\n" ); +printf (" variable: can be PAGESIZE or _NPROCESSORS_ONLN (for the moment)\n" ); +printf ("example: tst_getconf PAGESIZE\n" ); +} + +int main (int argc,char *argv[]) +{ +int opt; + +while ((opt =getopt (argc, argv,":h" )) !=-1 ) { +switch (opt) { +case 'h' : +print_help (); +return 0 ; +default : +print_help (); +return 1 ; + } + } + +if (argc !=2 ) { +print_help (); +return 1 ; + } + +if (optind >= argc) { +fprintf (stderr,"ERROR: Expected variable argument\n\n" ); +print_help (); +return 1 ;
testcases/lib/tst_getconf.c line 63
Quoted 10 lines
testcases/lib/tst_getconf.c new file +10
-0+ } + +if (!strcmp (argv[optind],"_NPROCESSORS_ONLN" )) +printf ("%ld" ,sysconf (_SC_NPROCESSORS_ONLN)); +else if (!strcmp (argv[optind],"PAGESIZE" )) +printf ("%ld" ,sysconf (_SC_PAGE_SIZE)); +else { +printf ("Invalid argument" ); +return -1 ; + }
To be honest, I prefer to make it more like getconf behavior especially for the neline or error warning. Which diff with you patch as following, but without this alignment it would be also accessible I think.
testcases/lib/tst_getconf.c +0 -1
@@ -23,7 +23,7 @@static void print_help (void ) {printf ("Usage: tst_getconf variable\n\n" ); - printf(" variable: can be PAGESIZE or _NPROCESSORS_ONLN
(for the moment)\n");
+ printf(" variable: can be PAGESIZE/PAGE_SIZE or
_NPROCESSORS_ONLN (for the moment)\n");
printf("example: tst_getconf PAGESIZE\n");}
@@ -54,11 +54,11 @@ int main(int argc, char *argv[])
}
if (!strcmp(argv[optind], "_NPROCESSORS_ONLN"))- printf("%ld", sysconf(_SC_NPROCESSORS_ONLN));
- else if (!strcmp(argv[optind],"PAGESIZE"))
- printf("%ld", sysconf(_SC_PAGE_SIZE));
+ printf("%ld\n", sysconf(_SC_NPROCESSORS_ONLN));
+ else if (!strcmp(argv[optind], "PAGESIZE") ||
!strcmp(argv[optind], "PAGE_SIZE"))
+ printf("%ld\n", sysconf(_SC_PAGE_SIZE));
else {
- printf("Invalid argument");
+ printf("tst_getconf: Unrecognized variable \'%s\'\n",
argv[optind]);
return -1;
}Quoted 9 lines
testcases/lib/tst_getconf.c new file +3
-1+ +return 0 ; +} --2.11.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Regards, Li Wang
PATCH v2 1/2 testcase: lib: Create tst_getconf to replace "getconf"
In reply to Mylène JosserandHi!
doc/test-writing-guidelines.txt passed directly to the script in '$1', '$2', ..., '$n'. · line 1639
Quoted 4 lines
doc/test-writing-guidelines.txt +4
-0+You may need to retrieve some variable configuration such as PAGESIZE. +There is "getconf" application that allows that but as some system may not +have it, a "tst_getconf" library allows to retrieve these variables by using +"sysconf()" function.
Shouldn't we stress here that we should use the tst_getconf instead of getconf here?
Something as:
You may need to retrieve configuration values such as PAGESIZE, there is 'getconf' but as some system may not have it, you are advised to use 'tst_getconf' instead. Note that it implements subset of 'getconf' system variables used by the testcases only.
Quoted 82 lines
+[source,sh]
+-------------------------------------------------------------------------------doc/test-writing-guidelines.txt +4
-0+# retrieve PAGESIZE +pagesize=`tst_getconf PAGESIZE` +------------------------------------------------------------------------------- + Sleeping for subsecond intervals ++++++++++++++++++++++++++++++++testcases/lib/.gitignore +1
-0@@ -7,3 +7,4 @@ /tst_net_iface_prefix /tst_net_ip_prefix /tst_net_vars +/tst_getconftestcases/lib/Makefile +2
-1@@ -27,6 +27,7 @@ include $(top_srcdir)/include/mk/testcases.mk INSTALL_TARGETS := *.sh MAKE_TARGETS := tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\ - tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars + tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\ + tst_getconf include $(top_srcdir)/include/mk/generic_leaf_target.mktestcases/lib/tst_getconf.c new file +44
-0@@ -0,0 +1,66 @@ +/* +* Copyright (c) 2018 Myl??ne Josserand <[email protected]> +* +* This program 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 program is distributed in the hope that it would 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. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write the Free Software Foundation, +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <stdio.h> +#include <unistd.h> +#include <string.h> + +static void print_help (void ) +{ +printf ("Usage: tst_getconf variable\n\n" ); +printf (" variable: can be PAGESIZE or _NPROCESSORS_ONLN (for the moment)\n" ); +printf ("example: tst_getconf PAGESIZE\n" ); +} + +int main (int argc,char *argv[]) +{ +int opt; + +while ((opt =getopt (argc, argv,":h" )) !=-1 ) { +switch (opt) { +case 'h' : +print_help (); +return 0 ; +default : +print_help (); +return 1 ; + } + } +
testcases/lib/tst_getconf.c line 54
Quoted 10 lines
testcases/lib/tst_getconf.c new file +10
-0+if (argc !=2 ) { +print_help (); +return 1 ; + } + +if (optind >= argc) { +fprintf (stderr,"ERROR: Expected variable argument\n\n" ); +print_help (); +return 1 ; + }
I doubt that this will ever be true, we do return from main once there are any options passed, so optind here should be set to 1 and argc == 2 because we checked earlier.
testcases/lib/tst_getconf.c line 61
Quoted 6 lines
testcases/lib/tst_getconf.c new file +6
-0+if (!strcmp (argv[optind],"_NPROCESSORS_ONLN" )) +printf ("%ld" ,sysconf (_SC_NPROCESSORS_ONLN)); +else if (!strcmp (argv[optind],"PAGESIZE" )) +printf ("%ld" ,sysconf (_SC_PAGE_SIZE)); +else { +printf ("Invalid argument" );
^ Missing newline
testcases/lib/tst_getconf.c line 62
Quoted 2 lines
testcases/lib/tst_getconf.c new file +2
-0+return -1 ; + }
LKML coding style prefers to have curly braces around both (all) blocks if they are around one of them, hence this should rather be:
if (...) {
} else if (...) {
} else {
}
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 2/2 testcases: Replace "getconf" by "tst_getconf"
In reply to Mylène JosserandNow that tst_getconf is available to use "sysconf()" function instead of relying on the application "getconf", let's convert all the tests to it.
--- testcases/commands/du/du01.sh | 2 +- testcases/commands/mkswap/mkswap01.sh | 2 +- testcases/kernel/controllers/cpuset/cpuset_regression_test.sh | 2 +- testcases/kernel/controllers/memcg/control/memcg_control_test.sh | 2 +- testcases/kernel/controllers/memcg/functional/memcg_lib.sh | 4 ++-- testcases/kernel/numa/numa01.sh | 2 +- testcases/lib/test.sh | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-)
testcases/commands/du/du01.sh +1 -1
@@ -73,7 +73,7 @@ du_test() } block_size=512 -page_size=$(getconf PAGESIZE) +page_size=$(tst_getconf PAGESIZE) if [ "$page_size" -lt 1024 ]; then tst_brk TBROK "Page size < 1024" fi
testcases/commands/mkswap/mkswap01.sh +1 -1
@@ -29,7 +29,7 @@ setup() { UUID=`uuidgen` - PAGE_SIZE=`getconf PAGE_SIZE` + PAGE_SIZE=`tst_getconf PAGESIZE` # Here get the size of the device and align it down to be the # multiple of $PAGE_SIZE and use that as the size for testing.
testcases/kernel/controllers/cpuset/cpuset_regression_test.sh +1 -1
@@ -34,7 +34,7 @@ setup() tst_brkm TCONF "Test must be run with kernel 3.18.0 or newer" fi - local cpu_num=$(getconf _NPROCESSORS_ONLN) + local cpu_num=$(tst_getconf _NPROCESSORS_ONLN) if [ $cpu_num -lt 2 ]; then tst_brkm TCONF "We need 2 cpus@least to have test" fi
testcases/kernel/controllers/memcg/control/memcg_control_test.sh +1 -1
@@ -41,7 +41,7 @@ export TST_COUNT=0 export TMP=${TMP:-/tmp} cd $TMP -PAGE_SIZE=$(getconf PAGESIZE) +PAGE_SIZE=$(tst_getconf PAGESIZE) TOT_MEM_LIMIT=$PAGE_SIZE ACTIVE_MEM_LIMIT=$PAGE_SIZE
testcases/kernel/controllers/memcg/functional/memcg_lib.sh +2 -2
@@ -29,9 +29,9 @@ if [ "x$(grep -w memory /proc/cgroups | cut -f4)" != "x1" ]; then tst_brkm TCONF "Kernel does not support the memory resource controller" fi -PAGESIZE=$(getconf PAGESIZE) +PAGESIZE=$(tst_getconf PAGESIZE) if [ $? -ne 0 ]; then - tst_brkm TBROK "getconf PAGESIZE failed" + tst_brkm TBROK "tst_getconf PAGESIZE failed" fi HUGEPAGESIZE=$(awk '/Hugepagesize/ {print $2}' /proc/meminfo)
testcases/kernel/numa/numa01.sh +1 -1
@@ -83,7 +83,7 @@ wait_for_support_numa() setup() { export MB=$((1024*1024)) - export PAGE_SIZE=$(getconf PAGE_SIZE) + export PAGE_SIZE=$(tst_getconf PAGESIZE) export HPAGE_SIZE=$(awk '/Hugepagesize:/ {print $2}' /proc/meminfo) total_nodes=0
testcases/lib/test.sh +2 -2
@@ -421,9 +421,9 @@ fi if [ "$TST_NEEDS_CHECKPOINTS" = "1" ]; then LTP_IPC_PATH="/dev/shm/ltp_${TCID}_$$" - LTP_IPC_SIZE=$(getconf PAGESIZE) + LTP_IPC_SIZE=$(tst_getconf PAGESIZE) if [ $? -ne 0 ]; then - tst_brkm TBROK "getconf PAGESIZE failed" + tst_brkm TBROK "tst_getconf PAGESIZE failed" fi ROD_SILENT dd if=/dev/zero of="$LTP_IPC_PATH" bs="$LTP_IPC_SIZE" count=1
Signature · 1 line
2.11.0
PATCH v2 2/2 testcases: Replace "getconf" by "tst_getconf"
Quoted 5 lines · Mylène Josserand
Now that
tst_getconfis available to use "sysconf()" function instead of relying on the application "getconf", let's convert all the tests to it.Signed-off-by Mylène Josserand <[email protected]>
Quoted 113 lines
--- testcases/commands/du/du01.sh | 2 +- testcases/commands/mkswap/mkswap01.sh | 2 +- testcases/kernel/controllers/cpuset/cpuset_regression_test.sh | 2 +- testcases/kernel/controllers/memcg/control/memcg_control_test.sh | 2 +- testcases/kernel/controllers/memcg/functional/memcg_lib.sh | 4 ++-- testcases/kernel/numa/numa01.sh | 2 +- testcases/lib/test.sh | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-)testcases/commands/du/du01.sh +1
-1@@ -73,7 +73,7 @@ du_test() } block_size=512 -page_size=$(getconf PAGESIZE) +page_size=$(tst_getconf PAGESIZE) if [ "$page_size" -lt 1024 ]; then tst_brk TBROK "Page size < 1024" fitestcases/commands/mkswap/mkswap01.sh +1
-1@@ -29,7 +29,7 @@ setup() { UUID=`uuidgen` - PAGE_SIZE=`getconf PAGE_SIZE` + PAGE_SIZE=`tst_getconf PAGESIZE` # Here get the size of the device and align it down to be the # multiple of $PAGE_SIZE and use that as the size for testing.testcases/kernel/controllers/cpuset/cpuset_regression_test.sh +1
-1@@ -34,7 +34,7 @@ setup() tst_brkm TCONF "Test must be run with kernel 3.18.0 or newer" fi - local cpu_num=$(getconf _NPROCESSORS_ONLN) + local cpu_num=$(tst_getconf _NPROCESSORS_ONLN) if [ $cpu_num -lt 2 ]; then tst_brkm TCONF "We need 2 cpus at least to have test" fitestcases/kernel/controllers/memcg/control/memcg_control_test.sh +1
-1@@ -41,7 +41,7 @@ export TST_COUNT=0 export TMP=${TMP:-/tmp} cd $TMP -PAGE_SIZE=$(getconf PAGESIZE) +PAGE_SIZE=$(tst_getconf PAGESIZE) TOT_MEM_LIMIT=$PAGE_SIZE ACTIVE_MEM_LIMIT=$PAGE_SIZEtestcases/kernel/controllers/memcg/functional/memcg_lib.sh +2
-2@@ -29,9 +29,9 @@ if [ "x$(grep -w memory /proc/cgroups | cut -f4)" != "x1" ]; then tst_brkm TCONF "Kernel does not support the memory resource controller" fi -PAGESIZE=$(getconf PAGESIZE) +PAGESIZE=$(tst_getconf PAGESIZE) if [ $? -ne 0 ]; then - tst_brkm TBROK "getconf PAGESIZE failed" + tst_brkm TBROK "tst_getconf PAGESIZE failed" fi HUGEPAGESIZE=$(awk '/Hugepagesize/ {print $2}' /proc/meminfo)testcases/kernel/numa/numa01.sh +1
-1@@ -83,7 +83,7 @@ wait_for_support_numa() setup() { export MB=$((1024*1024)) - export PAGE_SIZE=$(getconf PAGE_SIZE) + export PAGE_SIZE=$(tst_getconf PAGESIZE) export HPAGE_SIZE=$(awk '/Hugepagesize:/ {print $2}' /proc/meminfo) total_nodes=0testcases/lib/test.sh +2
-3@@ -421,9 +421,9 @@ fi if [ "$TST_NEEDS_CHECKPOINTS" = "1" ]; then LTP_IPC_PATH="/dev/shm/ltp_${TCID}_$$" - LTP_IPC_SIZE=$(getconf PAGESIZE) + LTP_IPC_SIZE=$(tst_getconf PAGESIZE) if [ $? -ne 0 ]; then - tst_brkm TBROK "getconf PAGESIZE failed" + tst_brkm TBROK "tst_getconf PAGESIZE failed" fi ROD_SILENT dd if=/dev/zero of="$LTP_IPC_PATH" bs="$LTP_IPC_SIZE" count=1 --2.11.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Regards, Li Wang