2018-06-08 15:24:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/taskset/taskset.c
|
|
|
|
*
|
2020-07-29 05:07:46 +02:00
|
|
|
* 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
|
2018-06-08 15:24:03 +02:00
|
|
|
*
|
2020-07-29 05:07:46 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-06-08 15:24:03 +02:00
|
|
|
*
|
2020-07-29 05:07:46 +02:00
|
|
|
* 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.
|
2018-06-08 15:24:03 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sched.h>
|
2021-05-20 05:32:57 +02:00
|
|
|
#include <assert.h>
|
2018-06-08 15:24:03 +02:00
|
|
|
#include <errno.h>
|
2019-02-25 20:38:54 +01:00
|
|
|
#include <string.h>
|
2018-06-08 15:24:03 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_usage
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void show_usage(FAR const char *progname, int exitcode)
|
|
|
|
{
|
2020-04-16 20:35:20 +02:00
|
|
|
printf("%s mask command ...\n", progname);
|
|
|
|
printf("%s -p [mask] pid\n", progname);
|
2018-06-08 15:24:03 +02:00
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_usage
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool get_cpuset(const char *arg, cpu_set_t *cpu_set)
|
|
|
|
{
|
2018-08-24 14:19:35 +02:00
|
|
|
DEBUGASSERT(NULL != arg);
|
2018-06-08 15:24:03 +02:00
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
int val = atoi(arg);
|
|
|
|
|
|
|
|
if (0 < val && val < (1 << CONFIG_SMP_NCPUS))
|
|
|
|
{
|
|
|
|
*cpu_set = (cpu_set_t)val;
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-16 20:35:20 +02:00
|
|
|
fprintf(stderr, "invalid cpuset %s\n", arg);
|
2018-06-08 15:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2019-02-25 20:38:54 +01:00
|
|
|
char command[CONFIG_NSH_LINELEN];
|
2018-06-08 15:24:03 +02:00
|
|
|
int exitcode;
|
|
|
|
int option;
|
|
|
|
int pid = -1;
|
|
|
|
cpu_set_t cpuset;
|
|
|
|
int rc;
|
2019-02-25 20:38:54 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(command, 0, sizeof(command));
|
2018-06-08 15:24:03 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-04-16 20:35:20 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"Err in sched_setaffinity() errno=%d\n", errno);
|
2018-06-08 15:24:03 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sched_getaffinity(pid, sizeof(cpu_set_t), &cpuset);
|
|
|
|
|
|
|
|
if (-1 == rc)
|
|
|
|
{
|
2020-04-16 20:35:20 +02:00
|
|
|
fprintf(stderr, "Err in sched_getaffinity() errno=%d\n", errno);
|
2018-06-08 15:24:03 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2020-04-16 20:35:20 +02:00
|
|
|
printf("pid %d's current affinity mask: %x\n", pid, cpuset);
|
2018-06-08 15:24:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (3 <= argc)
|
|
|
|
{
|
|
|
|
if (!get_cpuset(argv[1], &cpuset))
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2020-04-16 20:35:20 +02:00
|
|
|
/* Construct actual command with args
|
|
|
|
* NOTE: total length does not exceed CONFIG_NSH_LINELEN
|
|
|
|
*/
|
2019-02-25 20:38:54 +01:00
|
|
|
|
|
|
|
for (i = 0; i < argc - 2; i++)
|
|
|
|
{
|
|
|
|
strcat(command, argv[i + 2]);
|
|
|
|
strcat(command, " ");
|
|
|
|
}
|
|
|
|
|
2022-11-11 07:20:43 +01:00
|
|
|
sched_setaffinity(gettid(), sizeof(cpu_set_t), &cpuset);
|
2020-04-16 20:35:20 +02:00
|
|
|
system(command);
|
2018-06-08 15:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
errout_with_usage:
|
|
|
|
show_usage(argv[0], exitcode);
|
|
|
|
return exitcode; /* Not reachable */
|
|
|
|
}
|