blob: a4d41ac9c3f716d3116fe5cfec33df68c4c41bc6 [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
9#include "busybox.h"
10#include <sched.h>
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000011#include <getopt.h> /* optind */
12
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
Denis Vlasenko3bba5452006-12-30 17:57:03 +000042#define OPT_p 1
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000043
44int taskset_main(int argc, char** argv)
45{
46 cpu_set_t mask, new_mask;
47 pid_t pid = 0;
Denis Vlasenko3bba5452006-12-30 17:57:03 +000048 unsigned opt;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000049 const char *state = "current\0new";
50 char *p_opt = NULL, *aff = NULL;
51
Denis Vlasenko3bba5452006-12-30 17:57:03 +000052 opt = getopt32(argc, argv, "+p:", &p_opt);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000053
Denis Vlasenko3bba5452006-12-30 17:57:03 +000054 if (opt & OPT_p) {
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000055 if (argc == optind+1) { /* -p <aff> <pid> */
56 aff = p_opt;
57 p_opt = argv[optind];
58 }
59 argv += optind; /* me -p <arg> */
60 pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
61 } else
62 aff = *++argv; /* <aff> <cmd...> */
63 if (aff) {
64 unsigned i = 0;
65 unsigned long l = xstrtol_range(aff, 16, 1, ULONG_MAX);
66
67 CPU_ZERO(&new_mask);
68 while (i < CPU_SETSIZE && l >= (1<<i)) {
69 if ((1<<i) & l)
70 CPU_SET(i, &new_mask);
71 ++i;
72 }
73 }
74
Denis Vlasenko3bba5452006-12-30 17:57:03 +000075 if (opt & OPT_p) {
76 print_aff:
77 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
78 bb_perror_msg_and_die("failed to %cet pid %d's affinity", 'g', pid);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000079 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
80 pid, state, from_cpuset(mask));
81 if (!*argv) /* no new affinity given or we did print already, done. */
82 return EXIT_SUCCESS;
83 }
84
Denis Vlasenko3bba5452006-12-30 17:57:03 +000085 if (sched_setaffinity(pid, sizeof(new_mask), &new_mask))
86 bb_perror_msg_and_die("failed to %cet pid %d's affinity", 's', pid);
87 if (opt & OPT_p) {
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000088 state += 8;
89 ++argv;
90 goto print_aff;
91 }
92 ++argv;
93 execvp(*argv, argv);
94 bb_perror_msg_and_die("%s", *argv);
95}
Denis Vlasenko3bba5452006-12-30 17:57:03 +000096#undef OPT_p
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000097#undef TASKSET_PRINTF_MASK
98#undef from_cpuset