2006-12-06 17:26:54 -06:00
|
|
|
/*
|
2010-05-27 09:29:56 -05:00
|
|
|
* taskset.c - command-line utility for setting and retrieving
|
|
|
|
* a task's CPU affinity
|
2006-12-06 17:26:54 -06:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License, v2, as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Robert Love
|
2010-05-27 09:29:56 -05:00
|
|
|
* Copyright (C) 2010 Karel Zak <kzak@redhat.com>
|
2006-12-06 17:26:54 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
2010-05-27 09:29:56 -05:00
|
|
|
#include <err.h>
|
2007-06-11 15:24:45 -05:00
|
|
|
|
2010-05-24 05:44:39 -05:00
|
|
|
#include "cpuset.h"
|
2010-05-27 09:29:56 -05:00
|
|
|
#include "nls.h"
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2010-11-16 07:47:35 -06:00
|
|
|
#include "strutils.h"
|
2010-09-30 16:27:42 -05:00
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(FILE *out)
|
2006-12-06 17:26:54 -06:00
|
|
|
{
|
2010-05-27 09:29:56 -05:00
|
|
|
fprintf(out,
|
|
|
|
_("Usage: %s [options] [mask | cpu-list] [pid|cmd [args...]]\n\n"),
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
fprintf(out, _(
|
|
|
|
"Options:\n"
|
|
|
|
" -p, --pid operate on existing given pid\n"
|
|
|
|
" -c, --cpu-list display and specify cpus in list format\n"
|
|
|
|
" -h, --help display this help\n"
|
|
|
|
" -V, --version output version information\n\n"));
|
|
|
|
|
|
|
|
fprintf(out, _(
|
|
|
|
"The default behavior is to run a new command:\n"
|
|
|
|
" %1$s 03 sshd -b 1024\n"
|
|
|
|
"You can retrieve the mask of an existing task:\n"
|
|
|
|
" %1$s -p 700\n"
|
|
|
|
"Or set it:\n"
|
|
|
|
" %1$s -p 03 700\n"
|
|
|
|
"List format uses a comma-separated list instead of a mask:\n"
|
|
|
|
" %1$s -pc 0,3,7-11 700\n"
|
|
|
|
"Ranges in list format can take a stride argument:\n"
|
|
|
|
" e.g. 0-31:2 is equivalent to mask 0x55555555\n"),
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
fprintf(out, _("\nFor more information see taskset(1).\n"));
|
|
|
|
|
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-05-27 04:32:50 -05:00
|
|
|
cpu_set_t *new_set, *cur_set;
|
2006-12-06 17:26:54 -06:00
|
|
|
pid_t pid = 0;
|
2010-05-27 09:29:56 -05:00
|
|
|
int opt, c_opt = 0, rc;
|
2010-05-27 04:32:50 -05:00
|
|
|
char *buf;
|
|
|
|
unsigned int ncpus;
|
|
|
|
size_t new_setsize, cur_setsize, cur_nbits, buflen;
|
2006-12-06 17:26:54 -06:00
|
|
|
|
|
|
|
struct option longopts[] = {
|
|
|
|
{ "pid", 0, NULL, 'p' },
|
|
|
|
{ "cpu-list", 0, NULL, 'c' },
|
|
|
|
{ "help", 0, NULL, 'h' },
|
|
|
|
{ "version", 0, NULL, 'V' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
while ((opt = getopt_long(argc, argv, "+pchV", longopts, NULL)) != -1) {
|
2006-12-06 17:26:54 -06:00
|
|
|
switch (opt) {
|
|
|
|
case 'p':
|
2010-11-16 07:47:35 -06:00
|
|
|
pid = strtol_or_err(argv[argc - 1], _("failed to parse pid"));
|
2006-12-06 17:26:54 -06:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
c_opt = 1;
|
|
|
|
break;
|
|
|
|
case 'V':
|
2006-12-06 17:26:58 -06:00
|
|
|
printf("taskset (%s)\n", PACKAGE_STRING);
|
2010-05-27 09:29:56 -05:00
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:26:54 -06:00
|
|
|
case 'h':
|
2010-05-27 09:29:56 -05:00
|
|
|
usage(stdout);
|
|
|
|
break;
|
2006-12-06 17:26:54 -06:00
|
|
|
default:
|
2010-05-27 09:29:56 -05:00
|
|
|
usage(stderr);
|
|
|
|
break;
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!pid && argc - optind < 2)
|
2010-05-27 09:29:56 -05:00
|
|
|
|| (pid && (argc - optind < 1 || argc - optind > 2)))
|
|
|
|
usage(stderr);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2010-05-28 04:08:39 -05:00
|
|
|
ncpus = get_max_number_of_cpus();
|
|
|
|
if (ncpus <= 0)
|
|
|
|
errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting"));
|
2007-06-11 15:24:45 -05:00
|
|
|
|
|
|
|
/*
|
2010-05-27 09:29:56 -05:00
|
|
|
* cur_set is always used for the sched_getaffinity call
|
2007-06-11 15:24:45 -05:00
|
|
|
* On the sched_getaffinity the kernel demands a user mask of
|
|
|
|
* at least the size of its own cpumask_t.
|
|
|
|
*/
|
2010-05-27 04:32:50 -05:00
|
|
|
cur_set = cpuset_alloc(ncpus, &cur_setsize, &cur_nbits);
|
2010-05-27 09:29:56 -05:00
|
|
|
if (!cur_set)
|
|
|
|
err(EXIT_FAILURE, _("cpuset_alloc failed"));
|
|
|
|
|
2010-05-27 04:32:50 -05:00
|
|
|
buflen = 7 * cur_nbits;
|
|
|
|
buf = malloc(buflen);
|
2010-05-27 09:29:56 -05:00
|
|
|
if (!buf)
|
|
|
|
err(EXIT_FAILURE, _("malloc failed"));
|
2007-06-11 15:24:45 -05:00
|
|
|
|
|
|
|
/*
|
2010-05-27 09:29:56 -05:00
|
|
|
* new_set is always used for the sched_setaffinity call
|
2007-06-11 15:24:45 -05:00
|
|
|
* On the sched_setaffinity the kernel will zero-fill its
|
|
|
|
* cpumask_t if the user's mask is shorter.
|
|
|
|
*/
|
2010-05-27 04:32:50 -05:00
|
|
|
new_set = cpuset_alloc(ncpus, &new_setsize, NULL);
|
2010-05-27 09:29:56 -05:00
|
|
|
if (!new_set)
|
|
|
|
err(EXIT_FAILURE, _("cpuset_alloc failed"));
|
2007-06-11 15:24:45 -05:00
|
|
|
|
2006-12-06 17:26:54 -06:00
|
|
|
if (pid) {
|
2010-05-27 09:29:56 -05:00
|
|
|
if (sched_getaffinity(pid, cur_setsize, cur_set) < 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to get pid %d's affinity"), pid);
|
|
|
|
|
2006-12-06 17:26:54 -06:00
|
|
|
if (c_opt)
|
2010-05-27 09:29:56 -05:00
|
|
|
printf(_("pid %d's current affinity list: %s\n"), pid,
|
2010-05-27 04:32:50 -05:00
|
|
|
cpulist_create(buf, buflen, cur_set, cur_setsize));
|
2006-12-06 17:26:54 -06:00
|
|
|
else
|
2010-05-27 09:29:56 -05:00
|
|
|
printf(_("pid %d's current affinity mask: %s\n"), pid,
|
2010-05-27 04:32:50 -05:00
|
|
|
cpumask_create(buf, buflen, cur_set, cur_setsize));
|
2006-12-06 17:26:54 -06:00
|
|
|
|
|
|
|
if (argc - optind == 1)
|
2010-05-27 09:29:56 -05:00
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
rc = c_opt ? cpulist_parse(argv[optind], new_set, new_setsize) :
|
|
|
|
cpumask_parse(argv[optind], new_set, new_setsize);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
if (rc)
|
|
|
|
errx(EXIT_FAILURE, _("failed to parse %s %s"),
|
|
|
|
c_opt ? _("CPU list") : _("CPU mask"),
|
2006-12-06 17:26:54 -06:00
|
|
|
argv[optind]);
|
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
if (sched_setaffinity(pid, new_setsize, new_set) < 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to set pid %d's affinity"), pid);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
if (sched_getaffinity(pid, cur_setsize, cur_set) < 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to get pid %d's affinity"), pid);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
|
|
|
if (pid) {
|
|
|
|
if (c_opt)
|
2010-05-27 09:29:56 -05:00
|
|
|
printf(_("pid %d's new affinity list: %s\n"), pid,
|
2010-05-27 04:32:50 -05:00
|
|
|
cpulist_create(buf, buflen, cur_set, cur_setsize));
|
2006-12-06 17:26:54 -06:00
|
|
|
else
|
2010-05-27 09:29:56 -05:00
|
|
|
printf(_("pid %d's new affinity mask: %s\n"), pid,
|
2010-05-27 04:32:50 -05:00
|
|
|
cpumask_create(buf, buflen, cur_set, cur_setsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
cpuset_free(cur_set);
|
|
|
|
cpuset_free(new_set);
|
|
|
|
|
|
|
|
if (!pid) {
|
2006-12-06 17:26:54 -06:00
|
|
|
argv += optind + 1;
|
|
|
|
execvp(argv[0], argv);
|
2010-05-27 09:29:56 -05:00
|
|
|
err(EXIT_FAILURE, _("executing %s failed"), argv[0]);
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
2010-05-27 09:29:56 -05:00
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|