From 791133becbc1ba3e1c536fa32b3da9714467b157 Mon Sep 17 00:00:00 2001 From: hujun5 Date: Thu, 14 Nov 2024 15:59:24 +0800 Subject: [PATCH] testing: add iob test Signed-off-by: hujun5 --- testing/iob/CMakeLists.txt | 33 ++++ testing/iob/Kconfig | 30 +++ testing/iob/Make.defs | 23 +++ testing/iob/Makefile | 36 ++++ testing/iob/iob_main.c | 391 +++++++++++++++++++++++++++++++++++++ 5 files changed, 513 insertions(+) create mode 100644 testing/iob/CMakeLists.txt create mode 100644 testing/iob/Kconfig create mode 100644 testing/iob/Make.defs create mode 100644 testing/iob/Makefile create mode 100644 testing/iob/iob_main.c diff --git a/testing/iob/CMakeLists.txt b/testing/iob/CMakeLists.txt new file mode 100644 index 0000000000..ca423c42a9 --- /dev/null +++ b/testing/iob/CMakeLists.txt @@ -0,0 +1,33 @@ +# ############################################################################## +# apps/testing/iob/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_TESTING_IOB) + nuttx_add_application( + NAME + ${CONFIG_TESTING_IOB_PROGNAME} + PRIORITY + ${CONFIG_TESTING_IOB_PRIORITY} + STACKSIZE + ${CONFIG_TESTING_IOB_STACKSIZE} + MODULE + ${CONFIG_TESTING_IOB} + SRCS + iob_main.c) +endif() diff --git a/testing/iob/Kconfig b/testing/iob/Kconfig new file mode 100644 index 0000000000..584eb44a2a --- /dev/null +++ b/testing/iob/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_IOB + tristate "\"Test iob\" testing" + default n + depends on MM_IOB + ---help--- + Enable the \"Test iob!\" testing. + +if TESTING_IOB + +config TESTING_IOB_PROGNAME + string "Program name" + default "iob" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is iob. + +config TESTING_IOB_PRIORITY + int "Iob task priority" + default 100 + +config TESTING_IOB_STACKSIZE + int "Iob stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/testing/iob/Make.defs b/testing/iob/Make.defs new file mode 100644 index 0000000000..8cd27f4502 --- /dev/null +++ b/testing/iob/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/testing/iob/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_TESTING_IOB),) +CONFIGURED_APPS += $(APPDIR)/testing/iob +endif diff --git a/testing/iob/Makefile b/testing/iob/Makefile new file mode 100644 index 0000000000..4ba4252b5e --- /dev/null +++ b/testing/iob/Makefile @@ -0,0 +1,36 @@ +############################################################################ +# apps/testing/iob/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 + +# Iob ! built-in application info + +PROGNAME = $(CONFIG_TESTING_IOB_PROGNAME) +PRIORITY = $(CONFIG_TESTING_IOB_PRIORITY) +STACKSIZE = $(CONFIG_TESTING_IOB_STACKSIZE) +MODULE = $(CONFIG_TESTING_IOB) + +# Iob! Example + +MAINSRC = iob_main.c + +CFLAGS += -I$(TOPDIR)/mm/iob + +include $(APPDIR)/Application.mk diff --git a/testing/iob/iob_main.c b/testing/iob/iob_main.c new file mode 100644 index 0000000000..236a0e19d4 --- /dev/null +++ b/testing/iob/iob_main.c @@ -0,0 +1,391 @@ +/**************************************************************************** + * apps/testing/iob/iob_main.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 +#include + +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAGIC_NUMBER1 47 +#define MAGIC_NUMBER2 49 +#define MAGIC_NUMBER3 37 + +#define IOB_TOTAL_BUFFER_SIZE (CONFIG_IOB_BUFSIZE * CONFIG_IOB_NBUFFERS) +#define IOB_TEST_BUFFER_SIZE (IOB_TOTAL_BUFFER_SIZE - MAGIC_NUMBER1) + +#define TEST_COUNT 10000 + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static uint8_t g_buffer1[IOB_TOTAL_BUFFER_SIZE]; +static uint8_t g_buffer2[IOB_TOTAL_BUFFER_SIZE]; +static uint8_t g_iob_buffer1[IOB_TOTAL_BUFFER_SIZE]; +static uint8_t g_iob_buffer2[IOB_TOTAL_BUFFER_SIZE]; +static int failed_count[2]; +static int success_count[2]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +FAR static void *thread_nothrottled(FAR void *arg) +{ + int idx = (int)(uintptr_t)arg; + int nbytes; + int size; + uint8_t *inbuffer; + uint8_t *outbuffer; + + printf("thread_nothrottled %d\n", idx); + inbuffer = ((idx == 0) ? g_buffer1 : g_buffer2); + outbuffer = ((idx == 0) ? g_iob_buffer1 : g_iob_buffer2); + size = CONFIG_IOB_BUFSIZE * CONFIG_IOB_NBUFFERS; + + for (int i = 0; i < TEST_COUNT; i++) + { + struct iob_s *iob; + + iob = iob_alloc(false); + for (int j = 0; j < IOB_TOTAL_BUFFER_SIZE; j++) + { + inbuffer[j] = (uint8_t)(i & j & 0xff); + } + + if (iob_trycopyin(iob, inbuffer, size, 0, false) != size) + { + failed_count[idx]++; + iob_free_chain(iob); + continue; + } + + success_count[idx]++; + nbytes = iob_copyout(outbuffer, iob, IOB_TOTAL_BUFFER_SIZE, 0); + ASSERT(size == nbytes); + + if (memcmp(inbuffer, outbuffer, nbytes) != 0) + { + printf("thread_nothrottled inbuffer does not match outbuffer\n"); + ASSERT(false); + } + + iob_free_chain(iob); + } + + return NULL; +} + +FAR static void *thread_throttled(FAR void *arg) +{ + int idx = (int)(uintptr_t)arg; + int nbytes; + int size; + uint8_t *inbuffer; + uint8_t *outbuffer; + + printf("thread_throttled %d\n", idx); + inbuffer = ((idx == 0) ? g_buffer1 : g_buffer2); + outbuffer = ((idx == 0) ? g_iob_buffer1 : g_iob_buffer2); + size = CONFIG_IOB_BUFSIZE * (CONFIG_IOB_NBUFFERS - CONFIG_IOB_THROTTLE); + + for (int i = 0; i < TEST_COUNT; i++) + { + struct iob_s *iob; + + iob = iob_alloc(true); + for (int j = 0; j < IOB_TOTAL_BUFFER_SIZE; j++) + { + inbuffer[j] = (uint8_t)(i & j & 0xff); + } + + if (iob_trycopyin(iob, inbuffer, size, 0, true) != size) + { + failed_count[idx]++; + iob_free_chain(iob); + continue; + } + + success_count[idx]++; + nbytes = iob_copyout(outbuffer, iob, IOB_TOTAL_BUFFER_SIZE, 0); + ASSERT(size == nbytes); + + if (memcmp(inbuffer, outbuffer, nbytes) != 0) + { + printf("thread_throttled inbuffer does not match outbuffer\n"); + ASSERT(false); + } + + iob_free_chain(iob); + } + + return NULL; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * iob_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + printf("iob_test!!\n"); + + int ret; + cpu_set_t cpuset; + pthread_attr_t attr; + pthread_t thread[2]; + + struct iob_s *iob; + int nbytes; + int i; + + iob = iob_alloc(false); + + for (i = 0; i < IOB_TEST_BUFFER_SIZE; i++) + { + g_buffer1[i] = (uint8_t)(i & 0xff); + } + + memset(g_buffer2, 0xff, IOB_TEST_BUFFER_SIZE); + + iob_copyin(iob, g_buffer2, MAGIC_NUMBER1, 0, false); + printf("Copy IN: MAGIC_NUMBER1, offset 0\n"); + ASSERT(iob->io_pktlen == MAGIC_NUMBER1); + ASSERT(iob->io_offset == 0); + ASSERT(iob->io_len == MAGIC_NUMBER1); + + iob_copyin(iob, g_buffer1, IOB_TEST_BUFFER_SIZE, MAGIC_NUMBER1, false); + printf("Copy IN: IOB_TEST_BUFFER_SIZE, offset 0\n"); + ASSERT(iob->io_pktlen == IOB_TOTAL_BUFFER_SIZE); + ASSERT(iob->io_offset == 0); + ASSERT(iob->io_len == (CONFIG_IOB_BUFSIZE -iob->io_offset)); + + nbytes = iob_copyout(g_buffer2, iob, IOB_TEST_BUFFER_SIZE, MAGIC_NUMBER1); + printf("Copy OUT: %d, offset 0\n", nbytes); + ASSERT(nbytes == IOB_TEST_BUFFER_SIZE); + ASSERT(iob->io_pktlen == IOB_TOTAL_BUFFER_SIZE); + ASSERT(iob->io_offset == 0); + ASSERT(iob->io_len == (CONFIG_IOB_BUFSIZE -iob->io_offset)); + + if (memcmp(g_buffer1, g_buffer2, nbytes) != 0) + { + printf("Buffer1 does not match g_buffer2\n"); + ASSERT(false); + } + + iob = iob_trimhead(iob, MAGIC_NUMBER1); + printf("Trim: MAGIC_NUMBER1 from the beginning of the list\n"); + ASSERT(iob->io_pktlen == (IOB_TOTAL_BUFFER_SIZE - MAGIC_NUMBER1)); + ASSERT(iob->io_offset == MAGIC_NUMBER1); + ASSERT(iob->io_len == (CONFIG_IOB_BUFSIZE -iob->io_offset)); + + iob = iob_trimtail(iob, MAGIC_NUMBER2); + printf("Trim: MAGIC_NUMBER2 from the end of the list\n"); + ASSERT(iob->io_pktlen == (IOB_TOTAL_BUFFER_SIZE - MAGIC_NUMBER1 - + MAGIC_NUMBER2)); + ASSERT(iob->io_offset == MAGIC_NUMBER1); + ASSERT(iob->io_len == (CONFIG_IOB_BUFSIZE -iob->io_offset)); + + nbytes = iob_copyout(g_buffer2, iob, IOB_TEST_BUFFER_SIZE, 0); + printf("Copy OUT: %d, offset MAGIC_NUMBER1\n", nbytes); + ASSERT(nbytes == iob->io_pktlen); + ASSERT(iob->io_pktlen == (IOB_TOTAL_BUFFER_SIZE - MAGIC_NUMBER1 - + MAGIC_NUMBER2)); + ASSERT(iob->io_offset == MAGIC_NUMBER1); + ASSERT(iob->io_len == (CONFIG_IOB_BUFSIZE -iob->io_offset)); + + if (memcmp(g_buffer1, g_buffer2, nbytes) != 0) + { + printf("Buffer1 does not match g_buffer2\n"); + ASSERT(false); + } + + iob = iob_trimhead(iob, MAGIC_NUMBER2); + printf("Trim: MAGIC_NUMBER2 from the beginning of the list\n"); + ASSERT(iob->io_pktlen == (IOB_TOTAL_BUFFER_SIZE - MAGIC_NUMBER1 - + MAGIC_NUMBER2 - MAGIC_NUMBER2)); + + nbytes = iob_copyout(g_buffer2, iob, IOB_TEST_BUFFER_SIZE, 0); + printf("Copy OUT: %d, offset xxx\n", nbytes); + ASSERT(nbytes == iob->io_pktlen); + ASSERT(iob->io_pktlen == (IOB_TOTAL_BUFFER_SIZE - MAGIC_NUMBER1 - + MAGIC_NUMBER2 - MAGIC_NUMBER2)); + + if (memcmp(&g_buffer1[MAGIC_NUMBER2], g_buffer2, nbytes) != 0) + { + printf("Buffer1 does not match g_buffer2\n"); + ASSERT(false); + } + + iob = iob_pack(iob); + printf("Packed\n"); + ASSERT(iob->io_pktlen == (IOB_TOTAL_BUFFER_SIZE - MAGIC_NUMBER1 - + MAGIC_NUMBER2 - MAGIC_NUMBER2)); + ASSERT(iob->io_offset == 0); + + nbytes = iob_copyout(g_buffer2, iob, IOB_TEST_BUFFER_SIZE, 0); + printf("Copy OUT: %d, offset 0\n", nbytes); + ASSERT(nbytes == iob->io_pktlen); + ASSERT(iob->io_offset == 0); + + if (memcmp(&g_buffer1[MAGIC_NUMBER2], g_buffer2, nbytes) != 0) + { + printf("Buffer1 does not match g_buffer2\n"); + ASSERT(false); + } + + nbytes = iob->io_pktlen; + + iob_reserve(iob, 55); + printf("Reserve: adjust offset to 55\n"); + + if (iob->io_offset != 55 || iob->io_pktlen + 55 != nbytes) + { + printf("Offset or packet length wrong\n"); + ASSERT(false); + } + + iob_reserve(iob, 28); + printf("Reserve: adjust offset to 28\n"); + + if (iob->io_offset != 28 || iob->io_pktlen + 28 != nbytes) + { + printf("Offset or packet length wrong\n"); + ASSERT(false); + } + + iob_free_chain(iob); + + /* test race conditon between nothrottled and nothrottled */ + + failed_count[0] = 0; + failed_count[1] = 0; + success_count[0] = 0; + success_count[1] = 0; + + CPU_ZERO(&cpuset); + CPU_SET(0, &cpuset); + pthread_attr_init(&attr); +#ifdef CONFIG_SMP + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset); +#endif + ret = pthread_create(&thread[0], &attr, thread_nothrottled, (void *)0); + ASSERT(ret == 0); + + CPU_ZERO(&cpuset); + CPU_SET(1, &cpuset); + pthread_attr_init(&attr); +#ifdef CONFIG_SMP + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset); +#endif + ret = pthread_create(&thread[1], &attr, thread_nothrottled, (void *)1); + ASSERT(ret == 0); + + pthread_join(thread[0], NULL); + pthread_join(thread[1], NULL); + printf("nothrottled thread 0 statistc failed_count %d success_count %d\n", + failed_count[0], success_count[0]); + printf("nothrottled thread 1 statistc failed_count %d success_count %d\n", + failed_count[1], success_count[1]); + + /* test race conditon between throttled and nothrottled */ + + failed_count[0] = 0; + failed_count[1] = 0; + success_count[0] = 0; + success_count[1] = 0; + + CPU_ZERO(&cpuset); + CPU_SET(0, &cpuset); + pthread_attr_init(&attr); +#ifdef CONFIG_SMP + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset); +#endif + ret = pthread_create(&thread[0], &attr, thread_nothrottled, (void *)0); + ASSERT(ret == 0); + + CPU_ZERO(&cpuset); + CPU_SET(1, &cpuset); + pthread_attr_init(&attr); +#ifdef CONFIG_SMP + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset); +#endif + ret = pthread_create(&thread[1], &attr, thread_throttled, (void *)1); + ASSERT(ret == 0); + + pthread_join(thread[0], NULL); + pthread_join(thread[1], NULL); + printf("nothrottled thread 0 statistc failed_count %d success_count %d\n", + failed_count[0], success_count[0]); + printf("throttled thread 1 statistc failed_count %d success_count %d\n", + failed_count[1], success_count[1]); + + /* test race conditon between throttled and throttled */ + + failed_count[0] = 0; + failed_count[1] = 0; + success_count[0] = 0; + success_count[1] = 0; + + CPU_ZERO(&cpuset); + CPU_SET(0, &cpuset); + pthread_attr_init(&attr); +#ifdef CONFIG_SMP + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset); +#endif + ret = pthread_create(&thread[0], &attr, thread_throttled, (void *)0); + ASSERT(ret == 0); + + CPU_ZERO(&cpuset); + CPU_SET(1, &cpuset); + pthread_attr_init(&attr); +#ifdef CONFIG_SMP + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset); +#endif + ret = pthread_create(&thread[1], &attr, thread_throttled, (void *)1); + ASSERT(ret == 0); + + pthread_join(thread[0], NULL); + pthread_join(thread[1], NULL); + printf("throttled thread 0 statistc failed_count %d success_count %d\n", + failed_count[0], success_count[0]); + printf("throttled thread 1 statistc failed_count %d success_count %d\n", + failed_count[1], success_count[1]); + + return 0; +}