From 63f1fcb9cf0569f27d8c285b6afc4379322ea65e Mon Sep 17 00:00:00 2001 From: Nicolas Benech Date: Thu, 21 Mar 2019 13:28:53 -0400 Subject: [PATCH] gpu: nvgpu: unit: bsearch unit test Unit test to target the interface.bsearch unit. The goal here is to provide line coverage for the unit. Considering that the underlying implementation is using POSIX's bsearch, there is no need to verify the behavior of bsearch in depth. JIRA NVGPU-2265 Change-Id: I93ec3193bb0f93aaa47aa0c6c44eca09320893ca Signed-off-by: Nicolas Benech Reviewed-on: https://git-master.nvidia.com/r/2078427 Reviewed-by: mobile promotions Tested-by: mobile promotions --- Makefile.umbrella.tmk | 3 +- drivers/gpu/nvgpu/libnvgpu-drv.export | 1 + userspace/Makefile.sources | 8 +- userspace/required_tests.json | 4 + userspace/units/interface/bsearch/Makefile | 26 ++++++ .../interface/bsearch/Makefile.interface.tmk | 35 ++++++++ .../units/interface/bsearch/Makefile.tmk | 35 ++++++++ userspace/units/interface/bsearch/bsearch.c | 85 +++++++++++++++++++ 8 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 userspace/units/interface/bsearch/Makefile create mode 100644 userspace/units/interface/bsearch/Makefile.interface.tmk create mode 100644 userspace/units/interface/bsearch/Makefile.tmk create mode 100644 userspace/units/interface/bsearch/bsearch.c diff --git a/Makefile.umbrella.tmk b/Makefile.umbrella.tmk index 89aa90c38..8b55dcd3b 100644 --- a/Makefile.umbrella.tmk +++ b/Makefile.umbrella.tmk @@ -39,6 +39,8 @@ NV_REPOSITORY_COMPONENTS += userspace/units/posix/env NV_REPOSITORY_COMPONENTS += userspace/units/posix/mockio NV_REPOSITORY_COMPONENTS += userspace/units/posix/fault-injection NV_REPOSITORY_COMPONENTS += userspace/units/interface/atomic +NV_REPOSITORY_COMPONENTS += userspace/units/interface/bsearch +NV_REPOSITORY_COMPONENTS += userspace/units/interface/lock NV_REPOSITORY_COMPONENTS += userspace/units/pramin NV_REPOSITORY_COMPONENTS += userspace/units/mm/nvgpu_sgt NV_REPOSITORY_COMPONENTS += userspace/units/mm/nvgpu_mem @@ -51,7 +53,6 @@ NV_REPOSITORY_COMPONENTS += userspace/units/fifo/runlist NV_REPOSITORY_COMPONENTS += userspace/units/fuse NV_REPOSITORY_COMPONENTS += userspace/units/list NV_REPOSITORY_COMPONENTS += userspace/units/enabled -NV_REPOSITORY_COMPONENTS += userspace/units/interface/lock endif # Local Variables: diff --git a/drivers/gpu/nvgpu/libnvgpu-drv.export b/drivers/gpu/nvgpu/libnvgpu-drv.export index afed58b5e..3d61a5f65 100644 --- a/drivers/gpu/nvgpu/libnvgpu-drv.export +++ b/drivers/gpu/nvgpu/libnvgpu-drv.export @@ -71,6 +71,7 @@ nvgpu_alloc_space nvgpu_aperture_mask nvgpu_bar1_readl nvgpu_bar1_writel +nvgpu_bsearch nvgpu_dma_alloc nvgpu_dma_alloc_get_fault_injection nvgpu_dma_alloc_vid_at diff --git a/userspace/Makefile.sources b/userspace/Makefile.sources index eb5e46b2d..e7c98d38c 100644 --- a/userspace/Makefile.sources +++ b/userspace/Makefile.sources @@ -54,6 +54,8 @@ UNITS := \ $(UNIT_SRC)/pramin \ $(UNIT_SRC)/fuse \ $(UNIT_SRC)/interface/atomic \ + $(UNIT_SRC)/interface/bsearch \ + $(UNIT_SRC)/interface/lock \ $(UNIT_SRC)/mm/nvgpu_sgt \ $(UNIT_SRC)/mm/allocators/nvgpu_allocator \ $(UNIT_SRC)/mm/gmmu/pd_cache \ @@ -63,8 +65,4 @@ UNITS := \ $(UNIT_SRC)/mm/vm \ $(UNIT_SRC)/fifo/runlist \ $(UNIT_SRC)/list \ - $(UNIT_SRC)/enabled \ - $(UNIT_SRC)/interface/lock - -# A test unit. Not really needed any more... -# $(UNIT_SRC)/test + $(UNIT_SRC)/enabled diff --git a/userspace/required_tests.json b/userspace/required_tests.json index 33673dd73..2ef9ae533 100644 --- a/userspace/required_tests.json +++ b/userspace/required_tests.json @@ -307,6 +307,10 @@ "test": "fuse_gv100_vin_cal_slope_intercept", "unit": "fuse" }, + { + "test": "test_bsearch", + "unit": "interface_bsearch" + }, { "test": "mutex_acquire_release", "unit": "interface_lock" diff --git a/userspace/units/interface/bsearch/Makefile b/userspace/units/interface/bsearch/Makefile new file mode 100644 index 000000000..e4ce036d6 --- /dev/null +++ b/userspace/units/interface/bsearch/Makefile @@ -0,0 +1,26 @@ +# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +.SUFFIXES: + +OBJS = bsearch.o +MODULE = bsearch + +include ../../Makefile.units diff --git a/userspace/units/interface/bsearch/Makefile.interface.tmk b/userspace/units/interface/bsearch/Makefile.interface.tmk new file mode 100644 index 000000000..8d441944f --- /dev/null +++ b/userspace/units/interface/bsearch/Makefile.interface.tmk @@ -0,0 +1,35 @@ +################################### tell Emacs this is a -*- makefile-gmake -*- +# +# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# +# tmake for SW Mobile component makefile +# +############################################################################### + +NVGPU_UNIT_NAME=bsearch + +include $(NV_COMPONENT_DIR)/../../Makefile.units.common.interface.tmk + +# Local Variables: +# indent-tabs-mode: t +# tab-width: 8 +# End: +# vi: set tabstop=8 noexpandtab: diff --git a/userspace/units/interface/bsearch/Makefile.tmk b/userspace/units/interface/bsearch/Makefile.tmk new file mode 100644 index 000000000..3d18402c7 --- /dev/null +++ b/userspace/units/interface/bsearch/Makefile.tmk @@ -0,0 +1,35 @@ +################################### tell Emacs this is a -*- makefile-gmake -*- +# +# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# +# tmake for SW Mobile component makefile +# +############################################################################### + +NVGPU_UNIT_NAME=bsearch + +include $(NV_COMPONENT_DIR)/../../Makefile.units.common.tmk + +# Local Variables: +# indent-tabs-mode: t +# tab-width: 8 +# End: +# vi: set tabstop=8 noexpandtab: diff --git a/userspace/units/interface/bsearch/bsearch.c b/userspace/units/interface/bsearch/bsearch.c new file mode 100644 index 000000000..c9c7b1732 --- /dev/null +++ b/userspace/units/interface/bsearch/bsearch.c @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include +#include + +/* The test will create a table of size TABLE_SIZE with ordered values from 0 + * to TABLE_SIZE-1. Then the test will bsearch the SEARCH_FOR value. + * So obviously, this condition must be met: 0 <= SEARCH_FOR < TABLE_SIZE-1 + */ +#define TABLE_SIZE 1000 +#define SEARCH_FOR 727 + +u32 num_iterations = 0; + +/* + * Simple comparator function for ints. + * Returns 0 if values are equal, or the delta if not. + */ +static int int_compare(const void *a, const void *b) +{ + num_iterations++; + return (*((const int *)a) - *((const int *)b)); +} + +/* + * Simple test for bsearch. The goal here is code coverage, the underlying + * implementation of bsearch is provided by the stdlib. + */ +static int test_bsearch(struct unit_module *m, struct gk20a *g, + void *args) +{ + int sorted_table[TABLE_SIZE]; + int i; + int key = SEARCH_FOR; + + num_iterations = 0; + + /* Create a sorted table by having consecutive, incrementing values */ + for (i = 0; i < TABLE_SIZE; i++) { + sorted_table[i] = i; + } + + /* Run the binary search */ + int *node_ptr = (int *) nvgpu_bsearch((void *)(&key), + (void *)&sorted_table, TABLE_SIZE, sizeof(int), int_compare); + + if (node_ptr != NULL) { + if (*node_ptr == SEARCH_FOR) { + unit_info(m, "Found correct key after %d iterations\n", + num_iterations); + return UNIT_SUCCESS; + } + unit_return_fail(m, "Found incorrect value %d\n", *node_ptr); + } + unit_return_fail(m, "Key not found in array\n"); +} + +struct unit_module_test interface_bsearch_tests[] = { + UNIT_TEST(test_bsearch, test_bsearch, NULL), +}; + +UNIT_MODULE(interface_bsearch, interface_bsearch_tests, UNIT_PRIO_NVGPU_TEST);