feature: spinlock benchmark

Use multi-threads to measure spinlock.
You can get related setting with make menuconfig.

Signed-off-by: TaiJu Wu <tjwu1217@gmail.com>
Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>
Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com>
This commit is contained in:
TaiJu Wu 2023-10-30 00:44:48 +00:00 committed by Xiang Xiao
parent 57f98ee20e
commit 388256b7d7
5 changed files with 272 additions and 0 deletions

View File

@ -0,0 +1,55 @@
# ##############################################################################
# apps/benchmarks/spinlock_bench/CMakeLists.txt
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################
if(CONFIG_BENCHMARK_SPINLOCK)
# ############################################################################
# Config and Fetch Coremark application
# ############################################################################
set(SPINLOCKAPP_DIR ${CMAKE_CURRENT_LIST_DIR}/spinlock_bench)
# ############################################################################
# Sources
# ############################################################################
set(CSRCS ${SPINLOCKAPP_DIR}/spinlock_bench.c)
# ############################################################################
# Applications Configuration
# ############################################################################
nuttx_add_application(
NAME
spinlock_bench
PRIORITY
${CONFIG_SPINLOCK_PRIORITY}
STACKSIZE
${CONFIG_SPINLOCK_STACKSIZE}
MODULE
${CONFIG_BENCHMARK_SPINLOCK}
COMPILE_FLAGS
${CFLAGS}
SRCS
${CSRCS}
INCLUDE_DIRECTORIES
${INCDIR})
endif()

View File

@ -0,0 +1,45 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
menuconfig BENCHMARK_SPINLOCK
bool "Spinlock Benchmark"
depends on BUILD_FLAT
default n
---help---
Enable the Spinlock benchmark application.
if BENCHMARK_SPINLOCK
config SPINLOCK_PROGNAME
string "spinlock benchmark"
default "spinlock_bench"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.
config SPINLOCK_PRIORITY
int "Spinlock task priority"
default 100
config SPINLOCK_STACKSIZE
int "Spinlock task stack size"
default 4096
config SPINLOCK_MULTITHREAD
int "Number of threads"
default 40
---help---
Override the default number of threads to be executed.
The default value is 40.
config SPINLOCK_ITERATIONS
int "Number of iterations"
default 100
---help---
Default number of iterations for the benchmark on each thread.
The default value is 100.
endif # BENCHMARK_SPINLOCK

View File

@ -0,0 +1,23 @@
############################################################################
# apps/benchmarks/spinlock_bench/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
ifneq ($(CONFIG_BENCHMARK_SPINLOCK),)
CONFIGURED_APPS += $(APPDIR)/benchmarks/spinlock_bench
endif

View File

@ -0,0 +1,39 @@
############################################################################
# apps/benchmarks/spinlock_bench/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(APPDIR)/Make.defs
# spinlock_bench application
############################################################################
# Applications Configuration
############################################################################
MODULE = $(CONFIG_BENCHMARK_SPINLOCK)
PROGNAME += $(CONFIG_SPINLOCK_PROGNAME)
PRIORITY += $(CONFIG_SPINLOCK_PRIORITY)
STACKSIZE += $(CONFIG_SPINLOCK_STACKSIZE)
MAINSRC += spinlock_bench.c
# Build with WebAssembly when CONFIG_INTERPRETERS_WAMR is enabled
include $(APPDIR)/Application.mk

View File

@ -0,0 +1,110 @@
/****************************************************************************
* apps/benchmarks/spinlock_bench/spinlock_bench.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/spinlock.h>
#include <time.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define TEST_NUM CONFIG_SPINLOCK_MULTITHREAD
#define THREAD_NUM CONFIG_SPINLOCK_MULTITHREAD
/****************************************************************************
* Private Type
****************************************************************************/
struct thread_parmeter_s
{
FAR int *result;
FAR spinlock_t *lock;
};
/****************************************************************************
* Private Functions
****************************************************************************/
static FAR void *thread_spinlock(FAR void *parameter)
{
FAR int *result = ((FAR struct thread_parmeter_s *)parameter)->result;
FAR spinlock_t *lock = ((FAR struct thread_parmeter_s *)parameter)->lock;
int i;
for (i = 0; i < TEST_NUM; i++)
{
spin_lock(lock);
(*result)++;
spin_unlock(lock);
}
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
void main(void)
{
spinlock_t lock = SP_UNLOCKED;
int result = 0;
pthread_t thread[THREAD_NUM];
struct thread_parmeter_s para;
clock_t start;
clock_t end;
int status;
int i;
para.result = &result;
para.lock = &lock;
start = perf_gettime();
for (i = 0; i < THREAD_NUM; ++i)
{
status = pthread_create(&thread[i], NULL,
thread_spinlock, &para);
if (status != 0)
{
printf("spinlock_test: ERROR pthread_create failed, status=%d\n",
status);
ASSERT(false);
}
}
for (i = 0; i < THREAD_NUM; ++i)
{
pthread_join(thread[i], NULL);
}
end = perf_gettime();
assert(result == THREAD_NUM * TEST_NUM);
printf("total_time: %lu\n", end - start);
}