This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 388256b7d feature: spinlock benchmark
388256b7d is described below

commit 388256b7d7d880d2c9396459b87a9a46b4a2fe5f
Author: TaiJu Wu <tjwu1...@gmail.com>
AuthorDate: Mon Oct 30 00:44:48 2023 +0000

    feature: spinlock benchmark
    
    Use multi-threads to measure spinlock.
    You can get related setting with make menuconfig.
    
    Signed-off-by: TaiJu Wu <tjwu1...@gmail.com>
    Co-authored-by: Petro Karashchenko <petro.karashche...@gmail.com>
    Co-authored-by: Xiang Xiao <xiaoxiang781...@gmail.com>
---
 benchmarks/spinlock_bench/CMakeLists.txt   |  55 +++++++++++++++
 benchmarks/spinlock_bench/Kconfig          |  45 ++++++++++++
 benchmarks/spinlock_bench/Make.defs        |  23 ++++++
 benchmarks/spinlock_bench/Makefile         |  39 ++++++++++
 benchmarks/spinlock_bench/spinlock_bench.c | 110 +++++++++++++++++++++++++++++
 5 files changed, 272 insertions(+)

diff --git a/benchmarks/spinlock_bench/CMakeLists.txt 
b/benchmarks/spinlock_bench/CMakeLists.txt
new file mode 100644
index 000000000..8cf14f37c
--- /dev/null
+++ b/benchmarks/spinlock_bench/CMakeLists.txt
@@ -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()
diff --git a/benchmarks/spinlock_bench/Kconfig 
b/benchmarks/spinlock_bench/Kconfig
new file mode 100644
index 000000000..b872f5e84
--- /dev/null
+++ b/benchmarks/spinlock_bench/Kconfig
@@ -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
diff --git a/benchmarks/spinlock_bench/Make.defs 
b/benchmarks/spinlock_bench/Make.defs
new file mode 100644
index 000000000..e82177760
--- /dev/null
+++ b/benchmarks/spinlock_bench/Make.defs
@@ -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
diff --git a/benchmarks/spinlock_bench/Makefile 
b/benchmarks/spinlock_bench/Makefile
new file mode 100644
index 000000000..fada2fb61
--- /dev/null
+++ b/benchmarks/spinlock_bench/Makefile
@@ -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
diff --git a/benchmarks/spinlock_bench/spinlock_bench.c 
b/benchmarks/spinlock_bench/spinlock_bench.c
new file mode 100644
index 000000000..47a33786e
--- /dev/null
+++ b/benchmarks/spinlock_bench/spinlock_bench.c
@@ -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);
+}

Reply via email to