gpu: nvgpu: add rwsem unit test

Add unit tests for rwsem unit.

Jira NVGPU-2698

Change-Id: Id8c6f336b3cc2c458f42a8c21a9bace3a7711e05
Signed-off-by: ajesh <akv@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/2208425
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
This commit is contained in:
ajesh
2019-09-30 10:25:58 +05:30
committed by Alex Waterman
parent df5924cf57
commit c46a68c231
8 changed files with 631 additions and 0 deletions

View File

@@ -46,6 +46,7 @@ NV_REPOSITORY_COMPONENTS += userspace/units/posix/thread
NV_REPOSITORY_COMPONENTS += userspace/units/posix/cond
NV_REPOSITORY_COMPONENTS += userspace/units/posix/timers
NV_REPOSITORY_COMPONENTS += userspace/units/posix/kmem
NV_REPOSITORY_COMPONENTS += userspace/units/posix/rwsem
NV_REPOSITORY_COMPONENTS += userspace/units/interface/bsearch
NV_REPOSITORY_COMPONENTS += userspace/units/interface/lock
NV_REPOSITORY_COMPONENTS += userspace/units/interface/atomic

View File

@@ -419,6 +419,10 @@ nvgpu_readl_impl
nvgpu_request_firmware
nvgpu_runlist_construct_locked
nvgpu_rwsem_init
nvgpu_rwsem_down_read
nvgpu_rwsem_down_write
nvgpu_rwsem_up_read
nvgpu_rwsem_up_write
nvgpu_timeout_init
nvgpu_timeout_peek_expired
nvgpu_tsg_abort

View File

@@ -53,6 +53,7 @@ UNITS := \
$(UNIT_SRC)/posix/cond \
$(UNIT_SRC)/posix/timers \
$(UNIT_SRC)/posix/kmem \
$(UNIT_SRC)/posix/rwsem \
$(UNIT_SRC)/pramin \
$(UNIT_SRC)/ptimer \
$(UNIT_SRC)/init \

View File

@@ -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 = posix-rwsem.o
MODULE = posix-rwsem
include ../../Makefile.units

View File

@@ -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=posix-rwsem
include $(NV_COMPONENT_DIR)/../../Makefile.units.common.interface.tmk
# Local Variables:
# indent-tabs-mode: t
# tab-width: 8
# End:
# vi: set tabstop=8 noexpandtab:

View File

@@ -0,0 +1,39 @@
################################### 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=posix-rwsem
ifneq ($(NV_BUILD_CONFIGURATION_OS_IS_QNX),1)
NVGPU_UNIT_SHARED_LIBRARIES += pthread
endif
include $(NV_COMPONENT_DIR)/../../Makefile.units.common.tmk
# Local Variables:
# indent-tabs-mode: t
# tab-width: 8
# End:
# vi: set tabstop=8 noexpandtab:

View File

@@ -0,0 +1,300 @@
/*
* 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 <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <unit/io.h>
#include <unit/unit.h>
#include "posix-rwsem.h"
struct unit_test_rwsem_data {
unsigned int read_pattern;
unsigned int write_pattern;
bool read1_locked;
bool read2_locked;
bool write1_locked;
bool write2_locked;
bool wrlock_err;
bool wrpattern_err;
bool write_read_test;
bool read_lock_fail;
};
static struct unit_test_rwsem_data test_data;
static struct nvgpu_rwsem test_rwsem;
static void *test_rwsem_read1_thread(void *args)
{
int ret;
struct unit_test_rwsem_data *data;
data = (struct unit_test_rwsem_data *)args;
if (data->write_read_test == true) {
while (data->write1_locked == false) {
usleep(1);
}
ret = pthread_rwlock_tryrdlock(&test_rwsem.rw_sem);
if (ret != 0) {
data->read_lock_fail = true;
usleep(2);
}
}
nvgpu_rwsem_down_read(&test_rwsem);
data->read1_locked = true;
while (data->read2_locked == false) {
if (data->write1_locked) {
if (data->write_pattern != 0xCCCCCCCC) {
data->wrpattern_err = true;
}
break;
}
usleep(1);
}
nvgpu_rwsem_up_read(&test_rwsem);
return NULL;
}
static void *test_rwsem_read2_thread(void *args)
{
struct unit_test_rwsem_data *data;
data = (struct unit_test_rwsem_data *)args;
while (data->read1_locked == false) {
usleep(1);
}
nvgpu_rwsem_down_read(&test_rwsem);
data->read2_locked = true;
nvgpu_rwsem_up_read(&test_rwsem);
return NULL;
}
static void *test_rwsem_write1_thread(void *args)
{
struct unit_test_rwsem_data *data;
data = (struct unit_test_rwsem_data *)args;
nvgpu_rwsem_down_write(&test_rwsem);
data->write_pattern = 0xCCCCCCCC;
data->write1_locked = true;
usleep(5);
if (data->write_read_test == true) {
while (data->read_lock_fail == false) {
usleep(1);
}
}
if ((data->write2_locked == true) || (data->read1_locked == true)) {
data->wrlock_err = true;
}
nvgpu_rwsem_up_write(&test_rwsem);
return NULL;
}
static void *test_rwsem_write2_thread(void *args)
{
struct unit_test_rwsem_data *data;
data = (struct unit_test_rwsem_data *)args;
while (data->write1_locked == false) {
usleep(1);
}
nvgpu_rwsem_down_write(&test_rwsem);
data->write2_locked = true;
if (data->write_pattern != 0xCCCCCCCC) {
data->wrpattern_err = true;
}
data->write_pattern = 0xDDDDDDDD;
nvgpu_rwsem_up_write(&test_rwsem);
return NULL;
}
int test_rwsem_init(struct unit_module *m, struct gk20a *g, void *args)
{
nvgpu_rwsem_init(&test_rwsem);
usleep(1);
pthread_rwlock_destroy(&test_rwsem.rw_sem);
return UNIT_SUCCESS;
}
int test_rwsem_read(struct unit_module *m, struct gk20a *g, void *args)
{
int ret;
pthread_t thread_read1, thread_read2;
(void) memset(&test_data, 0, sizeof(struct unit_test_rwsem_data));
nvgpu_rwsem_init(&test_rwsem);
ret = pthread_create(&thread_read1, NULL,
&test_rwsem_read1_thread, &test_data);
if (ret != 0) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Read1 thread creation failed\n");
}
ret = pthread_create(&thread_read2, NULL,
&test_rwsem_read2_thread, &test_data);
if (ret != 0) {
pthread_cancel(thread_read1);
pthread_join(thread_read1, NULL);
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Read2 thread creation failed\n");
}
pthread_join(thread_read1, NULL);
pthread_join(thread_read2, NULL);
pthread_rwlock_destroy(&test_rwsem.rw_sem);
return UNIT_SUCCESS;
}
int test_rwsem_write(struct unit_module *m, struct gk20a *g, void *args)
{
int ret;
pthread_t thread_write1, thread_write2;
(void) memset(&test_data, 0, sizeof(struct unit_test_rwsem_data));
nvgpu_rwsem_init(&test_rwsem);
test_data.write_pattern = 0xABABABAB;
ret = pthread_create(&thread_write1, NULL,
&test_rwsem_write1_thread, &test_data);
if (ret != 0) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Write1 thread creation failed\n");
}
ret = pthread_create(&thread_write2, NULL,
&test_rwsem_write2_thread, &test_data);
if (ret != 0) {
pthread_cancel(thread_write1);
pthread_join(thread_write1, NULL);
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Write2 thread creation failed\n");
}
pthread_join(thread_write1, NULL);
pthread_join(thread_write2, NULL);
if (test_data.wrpattern_err == true) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Write pattern from write1 mismatch\n");
} else if (test_data.wrlock_err == true) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Lock error observed by write1 thread\n");
} else if (test_data.write_pattern != 0xDDDDDDDD) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Write pattern from write2 mismatch\n");
}
pthread_rwlock_destroy(&test_rwsem.rw_sem);
return UNIT_SUCCESS;
}
int test_rwsem_write_read(struct unit_module *m, struct gk20a *g, void *args)
{
int ret;
pthread_t thread_write, thread_read;
(void) memset(&test_data, 0, sizeof(struct unit_test_rwsem_data));
nvgpu_rwsem_init(&test_rwsem);
test_data.write_pattern = 0xABABABAB;
test_data.write_read_test = true;
ret = pthread_create(&thread_write, NULL,
&test_rwsem_write1_thread, &test_data);
if (ret != 0) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Write thread creation failed\n");
}
ret = pthread_create(&thread_read, NULL,
&test_rwsem_read1_thread, &test_data);
if (ret != 0) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
pthread_cancel(thread_write);
pthread_join(thread_write, NULL);
unit_return_fail(m, "Read thread creation failed\n");
}
pthread_join(thread_write, NULL);
pthread_join(thread_read, NULL);
if (test_data.wrpattern_err == true) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Write pattern mismatch\n");
} else if (test_data.wrlock_err == true) {
pthread_rwlock_destroy(&test_rwsem.rw_sem);
unit_return_fail(m, "Lock error observed by write thread\n");
}
pthread_rwlock_destroy(&test_rwsem.rw_sem);
return UNIT_SUCCESS;
}
struct unit_module_test posix_rwsem_tests[] = {
UNIT_TEST(init, test_rwsem_init, NULL, 0),
UNIT_TEST(read, test_rwsem_read, NULL, 0),
UNIT_TEST(write, test_rwsem_write, NULL, 0),
UNIT_TEST(write_read, test_rwsem_write_read, NULL, 0),
};
UNIT_MODULE(posix_rwsem, posix_rwsem_tests, UNIT_PRIO_POSIX_TEST);

View File

@@ -0,0 +1,225 @@
/*
* 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.
*/
/**
* @addtogroup SWUTS-posix-rwsem
* @{
*
* Software Unit Test Specification for posix-rwsem
*/
#ifndef __UNIT_POSIX_RWSEM_H__
#define __UNIT_POSIX_RWSEM_H__
#include <nvgpu/rwsem.h>
/**
* Test specification for test_rwsem_init.
*
* Description: Initialisation of rwsem.
*
* Test Type: Feature based.
*
* Inputs:
* 1) Global nvgpu_rwsem instance.
*
* Steps:
* 1) Call nvgpu API to initialise the rwsem.
* 2) Sleep for some time and destroy the rwsem.
* 3) Return success.
*
* Output:
* Returns success if the rwsem is initialised.
*
*/
int test_rwsem_init(struct unit_module *m,
struct gk20a *g, void *args);
/**
* Test specification for test_rwsem_read.
*
* Description: Testing the locking of a rwlock by multiple read threads.
*
* Test Type: Feature based.
*
* Inputs:
* 1) Global nvgpu_rwsem instance.
* 2) Global test_data instance.
*
* Steps:
* There are three threads involved in this test, a main thread which creates
* multiple reader threads and waits for the reader threads to return.
*
* Main thread
* 1) Initialize the global nvgpu_rwsem instance.
* 2) Create a thread for first reader.
* 3) Check for the return status of thread creation. If thread creation
* fails, destroy the rwsem instance and return failure.
* 4) Create a thread for second reader.
* 5) Check for the return status of thread creation. If thread creation
* fails, cancel the first reader thread, destroy the rwsem and return
* failure.
* 6) Use pthread_join to wait for both the reader threads to return.
* 7) Destroy the rwsem and return success.
*
* Reader Thread 1
* 1) Acquires the read lock on rwsem.
* 2) Set the flag read1_locked as true.
* 3) Wait on read2_locked flag till it is true.
* 4) Release the rwsem once read2_locked is true.
* 5) Return from the thread handler.
*
* Reader thread 2
* 1) Wait on read1_locked to be true, this ensures that the first reader
* thread has acquired the read lock.
* 2) Acquire the read lock on rwsem.
* 3) Set read2_locked as true.
* 4) Release the rwsem.
* 5) Return from the thread handler.
*
* Output:
* Returns success if both the threads are able to acquire read locks.
* If any of the thread hangs without acquiring the lock, the test should fail
* after the global timeout.
*
*/
int test_rwsem_read(struct unit_module *m,
struct gk20a *g, void *args);
/**
* Test specification for test_rwsem_write.
*
* Description: Testing the locking of a rwlock by multiple write threads.
*
* Test Type: Feature based.
*
* Inputs:
* 1) Global nvgpu_rwsem instance.
* 2) Global test_data instance.
*
* Steps:
* There are three threads involved in this test, a main thread which
* creates multiple write threads and waits for the write threads to return.
*
* Main thread
* 1) Initialize the global test_data structure.
* 2) Initialize the global nvgpu_rwsem instance.
* 3) Update the write pattern in test_data structure.
* 4) Create a thread for first writer.
* 5) Check for the return status of thread creation. If thread creation
* fails, destroy the rwsem instance and return failure.
* 6) Create a thread for second writer.
* 7) Check for the return status of thread creation. If thread creation
* fails, cancel the first thread, destroy the rwsem and return
* failure.
* 6) Use pthread_join to wait for both the reader threads to return.
* 7) Check if the write pattern in test_data matches with the data written
* by second thread. Return FAIL if it doesn't match.
* 8) Destroy the rwsem and return success.
*
* Writer Thread 1
* 1) Acquires the write lock on rwsem.
* 2) Set the flag write1_locked as true.
* 3) Delay the execution using sleep to let the second write thread try
* acquire the lock.
* 4) Update the write pattern.
* 5) Check if second write thread status indicates an acquired write lock,
* and populate error status if so.
* 4) Release the rwsem.
* 5) Return from the thread handler.
*
* Writer thread 2
* 1) Wait on write1_locked to be true, this ensures that the first writer
* thread has acquired the write lock.
* 2) Try to acquire the write lock on rwsem, this should put the thread in
* inactive state waiting for the lock to be available.
* 3) Set write2_locked as true once the thread is woken up.
* 4) Check for the write pattern if it matches with what thread 1 has written.
* 5) Update the write pattern.
* 4) Release the rwsem.
* 5) Return from the thread handler.
*
* Output:
* Return success if the second write thread is able to lock rwsem only after
* the first write thread releases it, else return failure.
*/
int test_rwsem_write(struct unit_module *m,
struct gk20a *g, void *args);
/**
* Test specification for test_rwsem_write_read.
*
* Description: Testing the locking of a rwlock by write and read threads.
*
* Test Type: Feature based.
*
* Inputs:
* 1) Global nvgpu_rwsem instance.
* 2) Global test_data instance.
*
* Steps:
* There are three threads involved in this test, a main thread which
* creates a write thread and a read thread and waits for the threads to
* return.
*
* Main thread
* 1) Initialize the global test_data structure.
* 2) Initialize the global nvgpu_rwsem instance.
* 3) Update the write pattern in test_data structure.
* 4) Create a thread for writer.
* 5) Check for the return status of thread creation. If thread creation
* fails, destroy the rwsem instance and return failure.
* 6) Create a thread for reader.
* 7) Check for the return status of thread creation. If thread creation
* fails, cancel the first thread, destroy the rwsem and return
* failure.
* 6) Use pthread_join to wait for both the reader threads to return.
* 7) Destroy the rwsem and return success.
*
* Writer Thread
* 1) Acquires the write lock on rwsem.
* 2) Set the flag write1_locked as true.
* 3) Delay the execution using sleep to let the second thread try
* acquire the lock.
* 4) Update the write pattern.
* 5) Check if second thread status indicates an acquired lock,
* and populate error status if so.
* 4) Release the rwsem.
* 5) Return from the thread handler.
*
* Reader thread
* 1) Try to acquire the lock on rwsem, this should put the thread in
* inactive state waiting for the lock to be available.
* 2) Set read1_locked as true once the thread is woken up.
* 3) Check for the write pattern if it matches with what thread 1 has written.
* Update error status if the pattern does not match.
* 4) Release the rwsem.
* 5) Return from the thread handler.
*
*
* Output:
* Returns success if the read thread is able to lock rwsem only after
* write thread releases it, else returns failure.
*
*/
int test_rwsem_write_read(struct unit_module *m,
struct gk20a *g, void *args);
#endif /* __UNIT_POSIX_RWSEM_H__ */