From e9c6fa0497cef87371b4e3b47e3e775dbd71c3cc Mon Sep 17 00:00:00 2001 From: ligd Date: Mon, 29 Aug 2022 21:35:47 +0800 Subject: [PATCH] cpuload: add cpulad test case Signed-off-by: ligd --- testing/cpuload/Kconfig | 8 +++ testing/cpuload/Make.defs | 23 +++++++ testing/cpuload/Makefile | 30 +++++++++ testing/cpuload/cpuload_main.c | 114 +++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 testing/cpuload/Kconfig create mode 100644 testing/cpuload/Make.defs create mode 100644 testing/cpuload/Makefile create mode 100644 testing/cpuload/cpuload_main.c diff --git a/testing/cpuload/Kconfig b/testing/cpuload/Kconfig new file mode 100644 index 000000000..aab20e614 --- /dev/null +++ b/testing/cpuload/Kconfig @@ -0,0 +1,8 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_CPULOAD + tristate "cpuload test" + default n diff --git a/testing/cpuload/Make.defs b/testing/cpuload/Make.defs new file mode 100644 index 000000000..a8b0cd6ee --- /dev/null +++ b/testing/cpuload/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/testing/cpuload/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_CPULOAD),) +CONFIGURED_APPS += $(APPDIR)/testing/cpuload +endif diff --git a/testing/cpuload/Makefile b/testing/cpuload/Makefile new file mode 100644 index 000000000..09899c571 --- /dev/null +++ b/testing/cpuload/Makefile @@ -0,0 +1,30 @@ +############################################################################ +# apps/testing/cpuload/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. +# +############################################################################ + +include $(APPDIR)/Make.defs + +PROGNAME = cpuload +PRIORITY = 100 +STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE) +MODULE = $(CONFIG_TESTING_CPULOAD) + +MAINSRC = cpuload_main.c + +include $(APPDIR)/Application.mk diff --git a/testing/cpuload/cpuload_main.c b/testing/cpuload/cpuload_main.c new file mode 100644 index 000000000..70f4335e5 --- /dev/null +++ b/testing/cpuload/cpuload_main.c @@ -0,0 +1,114 @@ +/**************************************************************************** + * apps/testing/cpuload/cpuload_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 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define CPULOAD_US (USEC_PER_SEC / CONFIG_SCHED_CPULOAD_TICKSPERSEC) +#define CPULOAD_DELAY (10 * CPULOAD_US) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void show_usage(FAR const char *progname, int exitcode) +{ + optind = 0; + + printf("\nUsage: %s [-c cpu] -p percent\n", progname); + printf("\nWhere:\n"); + printf(" -c bind to specific CPU, don't bind CPU if no this option\n"); + printf(" -p process percent[1-100], exectime / (exectime + idletime)\n"); + exit(exitcode); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * cpuload_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + FAR char *endptr; + int option; + int cpu = -1; + int per = 50; + + while ((option = getopt(argc, argv, "c:p:")) != ERROR) + { + if (option == 'c') + { + cpu = strtol(optarg, &endptr, 10); + } + else if (option == 'p') + { + per = strtol(optarg, &endptr, 10); + } + else + { + printf("Unrecognized option: '%c'\n", option); + show_usage(argv[0], EXIT_FAILURE); + } + } + + /* There should be two parameters remaining on the command line */ + + if (per < 1 || per > 100 || optind > argc) + { + printf("Missing required arguments\n"); + show_usage(argv[0], EXIT_FAILURE); + } + +#ifdef CONFIG_SMP + if (cpu > 0 && cpu < CONFIG_SMP_NCPUS) + { + cpu_set_t cpu_mask; + + CPU_ZERO(&cpu_mask); + CPU_SET(cpu, &cpu_mask); + + pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_mask); + } +#endif + + while (1) + { + up_udelay(per * CPULOAD_DELAY / 100); + usleep((100 - per) * CPULOAD_DELAY / 100); + } + + return 0; +}