2 * chcpu - CPU configuration tool
4 * Copyright IBM Corp. 2011
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/utsname.h>
33 #include <sys/types.h>
43 #include "closestream.h"
45 #define _PATH_SYS_CPU "/sys/devices/system/cpu"
46 #define _PATH_SYS_CPU_ONLINE _PATH_SYS_CPU "/online"
47 #define _PATH_SYS_CPU_RESCAN _PATH_SYS_CPU "/rescan"
48 #define _PATH_SYS_CPU_DISPATCH _PATH_SYS_CPU "/dispatching"
50 static cpu_set_t
*onlinecpus
;
53 #define is_cpu_online(cpu) (CPU_ISSET_S((cpu), CPU_ALLOC_SIZE(maxcpus), onlinecpus))
54 #define num_online_cpus() (CPU_COUNT_S(CPU_ALLOC_SIZE(maxcpus), onlinecpus))
62 CMD_CPU_DISPATCH_HORIZONTAL
,
63 CMD_CPU_DISPATCH_VERTICAL
,
66 static int cpu_enable(cpu_set_t
*cpu_set
, size_t setsize
, int enable
)
72 for (cpu
= 0; cpu
< setsize
; cpu
++) {
73 if (!CPU_ISSET(cpu
, cpu_set
))
75 if (!path_exist(_PATH_SYS_CPU
"/cpu%d", cpu
)) {
76 printf(_("CPU %d does not exist\n"), cpu
);
79 if (!path_exist(_PATH_SYS_CPU
"/cpu%d/online", cpu
)) {
80 printf(_("CPU %d is not hot pluggable\n"), cpu
);
83 online
= path_getnum(_PATH_SYS_CPU
"/cpu%d/online", cpu
);
84 if ((online
== 1) && (enable
== 1)) {
85 printf(_("CPU %d is already enabled\n"), cpu
);
88 if ((online
== 0) && (enable
== 0)) {
89 printf(_("CPU %d is already disabled\n"), cpu
);
92 if (path_exist(_PATH_SYS_CPU
"/cpu%d/configure", cpu
))
93 configured
= path_getnum(_PATH_SYS_CPU
"/cpu%d/configure", cpu
);
95 rc
= path_writestr("1", _PATH_SYS_CPU
"/cpu%d/online", cpu
);
96 if ((rc
== -1) && (configured
== 0))
97 printf(_("CPU %d enable failed "
98 "(CPU is deconfigured)\n"), cpu
);
100 printf(_("CPU %d enable failed (%m)\n"), cpu
);
102 printf(_("CPU %d enabled\n"), cpu
);
104 if (onlinecpus
&& num_online_cpus() == 1) {
105 printf(_("CPU %d disable failed "
106 "(last enabled CPU)\n"), cpu
);
109 rc
= path_writestr("0", _PATH_SYS_CPU
"/cpu%d/online", cpu
);
111 printf(_("CPU %d disable failed (%m)\n"), cpu
);
113 printf(_("CPU %d disabled\n"), cpu
);
115 CPU_CLR(cpu
, onlinecpus
);
122 static int cpu_rescan(void)
124 if (!path_exist(_PATH_SYS_CPU_RESCAN
))
125 errx(EXIT_FAILURE
, _("This system does not support rescanning of CPUs"));
126 if (path_writestr("1", _PATH_SYS_CPU_RESCAN
) == -1)
127 err(EXIT_FAILURE
, _("Failed to trigger rescan of CPUs"));
128 printf(_("Triggered rescan of CPUs\n"));
132 static int cpu_set_dispatch(int mode
)
134 if (!path_exist(_PATH_SYS_CPU_DISPATCH
))
135 errx(EXIT_FAILURE
, _("This system does not support setting "
136 "the dispatching mode of CPUs"));
138 if (path_writestr("0", _PATH_SYS_CPU_DISPATCH
) == -1)
139 err(EXIT_FAILURE
, _("Failed to set horizontal dispatch mode"));
140 printf(_("Succesfully set horizontal dispatching mode\n"));
142 if (path_writestr("1", _PATH_SYS_CPU_DISPATCH
) == -1)
143 err(EXIT_FAILURE
, _("Failed to set vertical dispatch mode"));
144 printf(_("Succesfully set vertical dispatching mode\n"));
149 static int cpu_configure(cpu_set_t
*cpu_set
, size_t setsize
, int configure
)
154 for (cpu
= 0; cpu
< setsize
; cpu
++) {
155 if (!CPU_ISSET(cpu
, cpu_set
))
157 if (!path_exist(_PATH_SYS_CPU
"/cpu%d", cpu
)) {
158 printf(_("CPU %d does not exist\n"), cpu
);
161 if (!path_exist(_PATH_SYS_CPU
"/cpu%d/configure", cpu
)) {
162 printf(_("CPU %d is not configurable\n"), cpu
);
165 current
= path_getnum(_PATH_SYS_CPU
"/cpu%d/configure", cpu
);
166 if ((current
== 1) && (configure
== 1)) {
167 printf(_("CPU %d is already configured\n"), cpu
);
170 if ((current
== 0) && (configure
== 0)) {
171 printf(_("CPU %d is already deconfigured\n"), cpu
);
174 if ((current
== 1) && (configure
== 0) && onlinecpus
&&
175 is_cpu_online(cpu
)) {
176 printf(_("CPU %d deconfigure failed "
177 "(CPU is enabled)\n"), cpu
);
181 rc
= path_writestr("1", _PATH_SYS_CPU
"/cpu%d/configure", cpu
);
183 printf(_("CPU %d configure failed (%m)\n"), cpu
);
185 printf(_("CPU %d configured\n"), cpu
);
187 rc
= path_writestr("0", _PATH_SYS_CPU
"/cpu%d/configure", cpu
);
189 printf(_("CPU %d deconfigure failed (%m)\n"), cpu
);
191 printf(_("CPU %d deconfigured\n"), cpu
);
197 static void cpu_parse(char *cpu_string
, cpu_set_t
*cpu_set
, size_t setsize
)
201 rc
= cpulist_parse(cpu_string
, cpu_set
, setsize
, 1);
205 errx(EXIT_FAILURE
, _("invalid CPU number in CPU list: %s"), cpu_string
);
206 errx(EXIT_FAILURE
, _("failed to parse CPU list: %s"), cpu_string
);
209 static void __attribute__((__noreturn__
)) usage(FILE *out
)
213 " %s [options]\n"), program_invocation_short_name
);
215 puts(_( "\nOptions:\n"
216 " -h, --help print this help\n"
217 " -e, --enable <cpu-list> enable cpus\n"
218 " -d, --disable <cpu-list> disable cpus\n"
219 " -c, --configure <cpu-list> configure cpus\n"
220 " -g, --deconfigure <cpu-list> deconfigure cpus\n"
221 " -p, --dispatch <mode> set dispatching mode\n"
222 " -r, --rescan trigger rescan of cpus\n"
223 " -V, --version output version information and exit\n"));
225 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
228 int main(int argc
, char *argv
[])
235 static const struct option longopts
[] = {
236 { "configure", required_argument
, 0, 'c' },
237 { "deconfigure",required_argument
, 0, 'g' },
238 { "disable", required_argument
, 0, 'd' },
239 { "dispatch", required_argument
, 0, 'p' },
240 { "enable", required_argument
, 0, 'e' },
241 { "help", no_argument
, 0, 'h' },
242 { "rescan", no_argument
, 0, 'r' },
243 { "version", no_argument
, 0, 'V' },
247 setlocale(LC_ALL
, "");
248 bindtextdomain(PACKAGE
, LOCALEDIR
);
250 atexit(close_stdout
);
252 maxcpus
= get_max_number_of_cpus();
254 errx(EXIT_FAILURE
, _("cannot determine NR_CPUS; aborting"));
255 if (path_exist(_PATH_SYS_CPU_ONLINE
))
256 onlinecpus
= path_cpulist(maxcpus
, _PATH_SYS_CPU_ONLINE
);
257 setsize
= CPU_ALLOC_SIZE(maxcpus
);
258 cpu_set
= CPU_ALLOC(maxcpus
);
260 err(EXIT_FAILURE
, _("cpuset_alloc failed"));
262 while ((c
= getopt_long(argc
, argv
, "c:d:e:g:hp:rV", longopts
, NULL
)) != -1) {
263 if (cmd
!= -1 && strchr("cdegpr", c
))
265 _("configure, deconfigure, disable, dispatch, enable "
266 "and rescan are mutually exclusive"));
269 cmd
= CMD_CPU_CONFIGURE
;
270 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
273 cmd
= CMD_CPU_DISABLE
;
274 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
277 cmd
= CMD_CPU_ENABLE
;
278 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
281 cmd
= CMD_CPU_DECONFIGURE
;
282 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
287 if (strcmp("horizontal", argv
[optind
- 1]) == 0)
288 cmd
= CMD_CPU_DISPATCH_HORIZONTAL
;
289 else if (strcmp("vertical", argv
[optind
- 1]) == 0)
290 cmd
= CMD_CPU_DISPATCH_VERTICAL
;
292 errx(EXIT_FAILURE
, _("unsupported argument: %s"),
296 cmd
= CMD_CPU_RESCAN
;
299 printf(_("%s from %s\n"), program_invocation_short_name
,
307 if ((argc
== 1) || (argc
!= optind
))
312 return cpu_enable(cpu_set
, maxcpus
, 1);
313 case CMD_CPU_DISABLE
:
314 return cpu_enable(cpu_set
, maxcpus
, 0);
315 case CMD_CPU_CONFIGURE
:
316 return cpu_configure(cpu_set
, maxcpus
, 1);
317 case CMD_CPU_DECONFIGURE
:
318 return cpu_configure(cpu_set
, maxcpus
, 0);
321 case CMD_CPU_DISPATCH_HORIZONTAL
:
322 return cpu_set_dispatch(0);
323 case CMD_CPU_DISPATCH_VERTICAL
:
324 return cpu_set_dispatch(1);