From 67e6e330a81ab120c5659b497d3518856024b42e Mon Sep 17 00:00:00 2001 From: Masayuki Ishikawa Date: Fri, 8 Jun 2018 13:24:03 +0000 Subject: [PATCH] Merged in masayuki2009/nuttx.apps/taskset_command (pull request #140) apps/system/taskset: Add taskset command for SMP systems This command can be used to retrieve or set a process's CPU affinity. For example, nsh> taskset -p 4 pid 4's current affinity mask: 2 nsh> taskset -p 3 4 pid 4's current affinity mask: 3 nsh> taskset -p 1 busyloop & Signed-off-by: Masayuki Ishikawa Approved-by: Gregory Nutt --- system/taskset/.gitignore | 11 +++ system/taskset/Kconfig | 30 ++++++ system/taskset/Make.defs | 39 ++++++++ system/taskset/Makefile | 147 +++++++++++++++++++++++++++++ system/taskset/taskset.c | 188 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 415 insertions(+) create mode 100644 system/taskset/.gitignore create mode 100644 system/taskset/Kconfig create mode 100644 system/taskset/Make.defs create mode 100644 system/taskset/Makefile create mode 100644 system/taskset/taskset.c diff --git a/system/taskset/.gitignore b/system/taskset/.gitignore new file mode 100644 index 000000000..83bd7b811 --- /dev/null +++ b/system/taskset/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src +/*.obj diff --git a/system/taskset/Kconfig b/system/taskset/Kconfig new file mode 100644 index 000000000..63a7e8f29 --- /dev/null +++ b/system/taskset/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 SYSTEM_TASKSET + bool "Taskset Command" + default n + depends on SMP && NSH_LIBRARY && SCHED_WAITPID + ---help--- + Enable support for the taskset command. + +if SYSTEM_TASKSET +config SYSTEM_TASKSET_PROGNAME + string "Taskset program name" + default "taskset" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config SYSTEM_TASKSET_PRIORITY + int "Taskset task priority" + default 50 + +config SYSTEM_TASKSET_STACKSIZE + int "Taskset stack size" + default 2048 + +endif diff --git a/system/taskset/Make.defs b/system/taskset/Make.defs new file mode 100644 index 000000000..ec5f41d67 --- /dev/null +++ b/system/taskset/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/system/taskset/Make.defs +# Adds selected applications to apps/ build +# +# Copyright 2018 Sony Video & Sound Products Inc. +# Author: Masayuki Ishikawa +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +ifeq ($(CONFIG_SYSTEM_TASKSET),y) +CONFIGURED_APPS += system/taskset +endif diff --git a/system/taskset/Makefile b/system/taskset/Makefile new file mode 100644 index 000000000..9a1513f35 --- /dev/null +++ b/system/taskset/Makefile @@ -0,0 +1,147 @@ +############################################################################ +# apps/system/taskset/Makefile +# +# Copyright 2018 Sony Video & Sound Products Inc. +# Author: Masayuki Ishikawa +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# taskset command + +CONFIG_SYSTEM_TASKSET_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_SYSTEM_TASKSET_STACKSIZE ?= 2048 + +APPNAME = taskset +PRIORITY = $(CONFIG_SYSTEM_TASKSET_PRIORITY) +STACKSIZE = $(CONFIG_SYSTEM_TASKSET_STACKSIZE) + +CONFIG_SYSTEM_TASKSET_PROGNAME ?= taskset$(EXEEXT) +PROGNAME = $(CONFIG_SYSTEM_TASKSET_PROGNAME) + +# Files + +ASRCS = +CSRCS = +MAINSRC = taskset.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) +MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) $(MAINSRC) +OBJS = $(AOBJS) $(COBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) + OBJS += $(MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ifeq ($(WINTOOL),y) + INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}" +else + INSTALL_DIR = $(BIN_DIR) +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean preconfig +.PRECIOUS: ../../libapps$(LIBEXT) + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + $(Q) touch .built + +ifeq ($(CONFIG_BUILD_KERNEL),y) +$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ) + @echo "LD: $(PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) + +install: $(BIN_DIR)$(DELIM)$(PROGNAME) + +else +install: + +endif + +# Register application + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat +else +context: +endif + +# Create dependencies + +.depend: Makefile $(SRCS) + $(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + $(Q) touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +preconfig: + +-include Make.dep diff --git a/system/taskset/taskset.c b/system/taskset/taskset.c new file mode 100644 index 000000000..f086bf75e --- /dev/null +++ b/system/taskset/taskset.c @@ -0,0 +1,188 @@ +/**************************************************************************** + * apps/system/taskset/taskset.c + * + * Copyright 2018 Sony Video & Sound Products Inc. + * Author: Masayuki Ishikawa + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include "nshlib/nshlib.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: show_usage + ****************************************************************************/ + +static void show_usage(FAR const char *progname, int exitcode) noreturn_function; +static void show_usage(FAR const char *progname, int exitcode) +{ + printf("%s mask command ... \n", progname); + printf("%s -p [mask] pid \n", progname); + exit(exitcode); +} + +/**************************************************************************** + * Name: show_usage + ****************************************************************************/ + +static bool get_cpuset(const char *arg, cpu_set_t *cpu_set) +{ + ASSERT(NULL != arg); + + bool ret = false; + int val = atoi(arg); + + if (0 < val && val < (1 << CONFIG_SMP_NCPUS)) + { + *cpu_set = (cpu_set_t)val; + ret = true; + } + else + { + fprintf(stderr, "invalid cpuset %s \n", arg); + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int taskset_main(int argc, char **argv) +#endif +{ + FAR char *nshargv[2]; + int exitcode; + int option; + int pid = -1; + cpu_set_t cpuset; + int rc; + + CPU_ZERO(&cpuset); + + /* Parse command line options */ + + exitcode = EXIT_FAILURE; + + while ((option = getopt(argc, argv, ":p:h")) != ERROR) + { + switch (option) + { + case 'p': + { + pid = (int)atoi(argv[argc -1]); + } + break; + + case 'h': + exitcode = EXIT_SUCCESS; + goto errout_with_usage; + } + } + + if (-1 != pid) + { + if (4 == argc) + { + if (!get_cpuset(argv[2], &cpuset)) + { + goto errout; + } + + rc = sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset); + + if (-1 == rc) + { + fprintf(stderr, "Err in sched_setaffinity() errno=%d \n", errno); + goto errout; + } + } + + rc = sched_getaffinity(pid, sizeof(cpu_set_t), &cpuset); + + if (-1 == rc) + { + fprintf(stderr, "Err in sched_getaffinity() errno=%d \n", errno); + goto errout; + } + + printf("pid %d's current affinity mask: %x \n", pid, cpuset); + } + else + { + if (3 <= argc) + { + if (!get_cpuset(argv[1], &cpuset)) + { + goto errout; + } + + nshargv[0] = argv[2]; + nshargv[1] = NULL; + + sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset); + usleep(10 * 1000); + + pid = task_create("system", CONFIG_SYSTEM_TASKSET_PRIORITY, + CONFIG_SYSTEM_TASKSET_STACKSIZE, nsh_system, + (FAR char * const *)nshargv); + + (void)waitpid(pid, &rc, 0); + } + } + +errout: + return EXIT_SUCCESS; +errout_with_usage: + show_usage(argv[0], exitcode); + return exitcode; /* Not reachable */ +}