Mercurial > hg > toybox
comparison toys/taskset.c @ 645:ed4bbd756bc2
Refactoring, no code change.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 31 Jul 2012 06:01:12 -0500 |
parents | 8f22dbddf1e6 |
children | fb546cc2a022 |
comparison
equal
deleted
inserted
replaced
644:6a096902309d | 645:ed4bbd756bc2 |
---|---|
2 * | 2 * |
3 * taskset.c - Retrieve or set the CPU affinity of a process. | 3 * taskset.c - Retrieve or set the CPU affinity of a process. |
4 * | 4 * |
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> | 5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> |
6 * | 6 * |
7 * Not in SUSv4. | 7 * No standard. |
8 | 8 |
9 USE_TASKSET(NEWTOY(taskset, "<1pa", TOYFLAG_BIN|TOYFLAG_NEEDROOT)) | 9 USE_TASKSET(NEWTOY(taskset, "<1pa", TOYFLAG_BIN|TOYFLAG_NEEDROOT)) |
10 | 10 |
11 config TASKSET | 11 config TASKSET |
12 bool "taskset" | 12 bool "taskset" |
35 int cpu = 0; | 35 int cpu = 0; |
36 | 36 |
37 CPU_ZERO(set); | 37 CPU_ZERO(set); |
38 if (size > 1 && mask[0] == '0' && mask[1] == 'x') mask += 2; | 38 if (size > 1 && mask[0] == '0' && mask[1] == 'x') mask += 2; |
39 | 39 |
40 while(ptr >= mask) | 40 while(ptr >= mask) { |
41 { | |
42 char val = 0; | 41 char val = 0; |
42 | |
43 if ( *ptr >= '0' && *ptr <= '9') val = *ptr - '0'; | 43 if ( *ptr >= '0' && *ptr <= '9') val = *ptr - '0'; |
44 else if (*ptr >= 'a' && *ptr <= 'f') val = 10 + (*ptr - 'a'); | 44 else if (*ptr >= 'a' && *ptr <= 'f') val = 10 + (*ptr - 'a'); |
45 else return -1; | 45 else return -1; |
46 | 46 |
47 if (val & 1) CPU_SET(cpu, set); | 47 if (val & 1) CPU_SET(cpu, set); |
56 } | 56 } |
57 | 57 |
58 static char * cpu_set_to_str(cpu_set_t *set) | 58 static char * cpu_set_to_str(cpu_set_t *set) |
59 { | 59 { |
60 int cpu; | 60 int cpu; |
61 char * ptr = toybuf; | 61 char *ptr = toybuf; |
62 for (cpu=(8*sizeof(cpu_set_t) - 4); cpu >= 0; cpu -= 4) | 62 |
63 { | 63 for (cpu = (8*sizeof(cpu_set_t) - 4); cpu >= 0; cpu -= 4) { |
64 char val = 0; | 64 char val = 0; |
65 | |
65 if (CPU_ISSET(cpu, set)) val |= 1; | 66 if (CPU_ISSET(cpu, set)) val |= 1; |
66 if (CPU_ISSET(cpu + 1, set)) val |= 2; | 67 if (CPU_ISSET(cpu + 1, set)) val |= 2; |
67 if (CPU_ISSET(cpu + 2, set)) val |= 4; | 68 if (CPU_ISSET(cpu + 2, set)) val |= 4; |
68 if (CPU_ISSET(cpu + 3, set)) val |= 8; | 69 if (CPU_ISSET(cpu + 3, set)) val |= 8; |
69 if (ptr != toybuf || val != 0) | 70 if (ptr != toybuf || val != 0) { |
70 { | |
71 if (val < 10) *ptr = '0' + val; | 71 if (val < 10) *ptr = '0' + val; |
72 else *ptr = 'a' + (val - 10); | 72 else *ptr = 'a' + (val - 10); |
73 ptr++; | 73 ptr++; |
74 } | 74 } |
75 } | 75 } |
111 return 0; | 111 return 0; |
112 } | 112 } |
113 | 113 |
114 void taskset_main(void) | 114 void taskset_main(void) |
115 { | 115 { |
116 char * pidstr = (toys.optc==1)?toys.optargs[0]:toys.optargs[1]; | 116 char *pidstr = (toys.optc==1) ? toys.optargs[0] : toys.optargs[1]; |
117 | 117 |
118 if (!(toys.optflags & P_FLAG)) | 118 if (!(toys.optflags & P_FLAG)) { |
119 { | 119 if (toys.optc >= 2) { |
120 if (toys.optc >= 2) | |
121 { | |
122 do_taskset(getpid(),1); | 120 do_taskset(getpid(),1); |
123 xexec(&toys.optargs[1]); | 121 xexec(&toys.optargs[1]); |
124 } | 122 } else error_exit("Needs at least a mask and a command"); |
125 else error_exit("Needs at least a mask and a command"); | |
126 } | 123 } |
127 | 124 |
128 if (toys.optflags & A_FLAG) | 125 if (toys.optflags & A_FLAG) { |
129 { | |
130 sprintf(toybuf, "/proc/%s/task/", pidstr); | 126 sprintf(toybuf, "/proc/%s/task/", pidstr); |
131 dirtree_read(toybuf, task_cb); | 127 dirtree_read(toybuf, task_cb); |
132 } else | 128 } else do_taskset(atoi(pidstr), 0); |
133 do_taskset(atoi(pidstr), 0); | |
134 } | 129 } |