blob: 6247aa869f4d930547d52c8bd03058afd4d1e76e [file] [log] [blame]
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00001/* vi: set sw=4 ts=4: */
2/*
3 * taskset - retrieve or set a processes' CPU affinity
4 * Copyright (c) 2006 Bernhard Fischer
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
8
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00009#include <sched.h>
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000010#include <getopt.h> /* optind */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000012
13#if ENABLE_FEATURE_TASKSET_FANCY
14#define TASKSET_PRINTF_MASK "%s"
15#define from_cpuset(x) __from_cpuset(&x)
16/* craft a string from the mask */
Denis Vlasenko3bba5452006-12-30 17:57:03 +000017static char *__from_cpuset(cpu_set_t *mask)
18{
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000019 int i;
20 char *ret = 0, *str = xzalloc(9);
21
22 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
23 char val = 0;
24 int off;
25 for (off = 0; off <= 3; ++off)
26 if (CPU_ISSET(i+off, mask))
27 val |= 1<<off;
28
29 if (!ret && val)
30 ret = str;
31 *str++ = (val-'0'<=9) ? (val+48) : (val+87);
32 }
33 return ret;
34}
35#else
36#define TASKSET_PRINTF_MASK "%x"
Denis Vlasenko3bba5452006-12-30 17:57:03 +000037/* (void*) cast is for battling gcc: */
38/* "dereferencing type-punned pointer will break strict-aliasing rules" */
39#define from_cpuset(mask) (*(unsigned*)(void*)&(mask))
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000040#endif
41
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000042
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000043int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000044int taskset_main(int argc ATTRIBUTE_UNUSED, char **argv)
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000045{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000046 cpu_set_t mask;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000047 pid_t pid = 0;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000048 unsigned opt_p;
49 const char *current_new;
50 char *pid_str;
51 char *aff = aff; /* for compiler */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000052
Denis Vlasenkob44c7902008-03-17 09:29:43 +000053 opt_complementary = "-1"; /* at least 1 arg */
54 opt_p = getopt32(argv, "+p");
55 argv += optind;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000056
Denis Vlasenkob44c7902008-03-17 09:29:43 +000057 if (opt_p) {
58 pid_str = *argv++;
59 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
60 aff = pid_str;
61 pid_str = *argv; /* NB: *argv != NULL in this case */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000062 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000063 /* else it was just "-p <pid>", and *argv == NULL */
64 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
65 } else {
66 aff = *argv++; /* <aff> <cmd...> */
67 if (!*argv)
68 bb_show_usage();
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000069 }
70
Denis Vlasenkob44c7902008-03-17 09:29:43 +000071 current_new = "current\0new";
72 if (opt_p) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +000073 print_aff:
74 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000075 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000076 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +000077 pid, current_new, from_cpuset(mask));
78 if (!*argv) {
79 /* Either it was just "-p <pid>",
80 * or it was "-p <aff> <pid>" and we came here
81 * for the second time (see goto below) */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000082 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000083 }
84 *argv = NULL;
85 current_new += 8; /* "new" */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000086 }
87
Denis Vlasenkob44c7902008-03-17 09:29:43 +000088 { /* Affinity was specified, translate it into cpu_set_t */
89 unsigned i;
90 /* Do not allow zero mask: */
91 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
92 CPU_ZERO(&mask);
93 for (i = 0; i < CPU_SETSIZE; i++) {
94 unsigned long long bit = (1ULL << i);
95 if (bit & m)
96 CPU_SET(i, &mask);
97 }
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000098 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000099
100 /* Set pid's or our own (pid==0) affinity */
101 if (sched_setaffinity(pid, sizeof(mask), &mask))
102 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
103
104 if (!*argv) /* "-p <aff> <pid> [...ignored...]" */
105 goto print_aff; /* print new affinity and exit */
106
Denis Vlasenko1d76f432007-02-06 01:20:12 +0000107 BB_EXECVP(*argv, argv);
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000108 bb_simple_perror_msg_and_die(*argv);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000109}