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"
46 #define EXCL_ERROR "--{configure,deconfigure,disable,dispatch,enable}"
48 /* partial success, otherwise we return regular EXIT_{SUCCESS,FAILURE} */
49 #define CHCPU_EXIT_SOMEOK 64
51 #define _PATH_SYS_CPU "/sys/devices/system/cpu"
52 #define _PATH_SYS_CPU_ONLINE _PATH_SYS_CPU "/online"
53 #define _PATH_SYS_CPU_RESCAN _PATH_SYS_CPU "/rescan"
54 #define _PATH_SYS_CPU_DISPATCH _PATH_SYS_CPU "/dispatching"
56 static cpu_set_t
*onlinecpus
;
59 #define is_cpu_online(cpu) (CPU_ISSET_S((cpu), CPU_ALLOC_SIZE(maxcpus), onlinecpus))
60 #define num_online_cpus() (CPU_COUNT_S(CPU_ALLOC_SIZE(maxcpus), onlinecpus))
68 CMD_CPU_DISPATCH_HORIZONTAL
,
69 CMD_CPU_DISPATCH_VERTICAL
,
72 /* returns: 0 = success
74 * > 0 = partial success
76 static int cpu_enable(cpu_set_t
*cpu_set
, size_t setsize
, int enable
)
83 for (cpu
= 0; cpu
< setsize
; cpu
++) {
84 if (!CPU_ISSET(cpu
, cpu_set
))
86 if (!path_exist(_PATH_SYS_CPU
"/cpu%d", cpu
)) {
87 warnx(_("CPU %u does not exist"), cpu
);
91 if (!path_exist(_PATH_SYS_CPU
"/cpu%d/online", cpu
)) {
92 warnx(_("CPU %u is not hot pluggable"), cpu
);
96 online
= path_read_s32(_PATH_SYS_CPU
"/cpu%d/online", cpu
);
97 if ((online
== 1) && (enable
== 1)) {
98 printf(_("CPU %u is already enabled\n"), cpu
);
101 if ((online
== 0) && (enable
== 0)) {
102 printf(_("CPU %u is already disabled\n"), cpu
);
105 if (path_exist(_PATH_SYS_CPU
"/cpu%d/configure", cpu
))
106 configured
= path_read_s32(_PATH_SYS_CPU
"/cpu%d/configure", cpu
);
108 rc
= path_write_str("1", _PATH_SYS_CPU
"/cpu%d/online", cpu
);
109 if ((rc
== -1) && (configured
== 0)) {
110 warn(_("CPU %u enable failed (CPU is deconfigured)"), cpu
);
112 } else if (rc
== -1) {
113 warn(_("CPU %u enable failed"), cpu
);
116 printf(_("CPU %u enabled\n"), cpu
);
118 if (onlinecpus
&& num_online_cpus() == 1) {
119 warnx(_("CPU %u disable failed (last enabled CPU)"), cpu
);
123 rc
= path_write_str("0", _PATH_SYS_CPU
"/cpu%d/online", cpu
);
125 warn(_("CPU %u disable failed"), cpu
);
128 printf(_("CPU %u disabled\n"), cpu
);
130 CPU_CLR(cpu
, onlinecpus
);
135 return fails
== 0 ? 0 : fails
== setsize
? -1 : 1;
138 static int cpu_rescan(void)
140 if (!path_exist(_PATH_SYS_CPU_RESCAN
))
141 errx(EXIT_FAILURE
, _("This system does not support rescanning of CPUs"));
142 if (path_write_str("1", _PATH_SYS_CPU_RESCAN
) == -1)
143 err(EXIT_FAILURE
, _("Failed to trigger rescan of CPUs"));
144 printf(_("Triggered rescan of CPUs\n"));
148 static int cpu_set_dispatch(int mode
)
150 if (!path_exist(_PATH_SYS_CPU_DISPATCH
))
151 errx(EXIT_FAILURE
, _("This system does not support setting "
152 "the dispatching mode of CPUs"));
154 if (path_write_str("0", _PATH_SYS_CPU_DISPATCH
) == -1)
155 err(EXIT_FAILURE
, _("Failed to set horizontal dispatch mode"));
156 printf(_("Successfully set horizontal dispatching mode\n"));
158 if (path_write_str("1", _PATH_SYS_CPU_DISPATCH
) == -1)
159 err(EXIT_FAILURE
, _("Failed to set vertical dispatch mode"));
160 printf(_("Successfully set vertical dispatching mode\n"));
165 /* returns: 0 = success
167 * > 0 = partial success
169 static int cpu_configure(cpu_set_t
*cpu_set
, size_t setsize
, int configure
)
175 for (cpu
= 0; cpu
< setsize
; cpu
++) {
176 if (!CPU_ISSET(cpu
, cpu_set
))
178 if (!path_exist(_PATH_SYS_CPU
"/cpu%d", cpu
)) {
179 warnx(_("CPU %u does not exist"), cpu
);
183 if (!path_exist(_PATH_SYS_CPU
"/cpu%d/configure", cpu
)) {
184 warnx(_("CPU %u is not configurable"), cpu
);
188 current
= path_read_s32(_PATH_SYS_CPU
"/cpu%d/configure", cpu
);
189 if ((current
== 1) && (configure
== 1)) {
190 printf(_("CPU %u is already configured\n"), cpu
);
193 if ((current
== 0) && (configure
== 0)) {
194 printf(_("CPU %u is already deconfigured\n"), cpu
);
197 if ((current
== 1) && (configure
== 0) && onlinecpus
&&
198 is_cpu_online(cpu
)) {
199 warnx(_("CPU %u deconfigure failed (CPU is enabled)"), cpu
);
204 rc
= path_write_str("1", _PATH_SYS_CPU
"/cpu%d/configure", cpu
);
206 warn(_("CPU %u configure failed"), cpu
);
209 printf(_("CPU %u configured\n"), cpu
);
211 rc
= path_write_str("0", _PATH_SYS_CPU
"/cpu%d/configure", cpu
);
213 warn(_("CPU %u deconfigure failed"), cpu
);
216 printf(_("CPU %u deconfigured\n"), cpu
);
220 return fails
== 0 ? 0 : fails
== setsize
? -1 : 1;
223 static void cpu_parse(char *cpu_string
, cpu_set_t
*cpu_set
, size_t setsize
)
227 rc
= cpulist_parse(cpu_string
, cpu_set
, setsize
, 1);
231 errx(EXIT_FAILURE
, _("invalid CPU number in CPU list: %s"), cpu_string
);
232 errx(EXIT_FAILURE
, _("failed to parse CPU list: %s"), cpu_string
);
235 static void __attribute__((__noreturn__
)) usage(void)
240 " %s [options]\n"), program_invocation_short_name
);
242 fputs(USAGE_SEPARATOR
, out
);
243 fputs(_("Configure CPUs in a multi-processor system.\n"), out
);
245 fputs(USAGE_OPTIONS
, stdout
);
247 " -e, --enable <cpu-list> enable cpus\n"
248 " -d, --disable <cpu-list> disable cpus\n"
249 " -c, --configure <cpu-list> configure cpus\n"
250 " -g, --deconfigure <cpu-list> deconfigure cpus\n"
251 " -p, --dispatch <mode> set dispatching mode\n"
252 " -r, --rescan trigger rescan of cpus\n"
254 print_usage_help_options(31);
256 printf(USAGE_MAN_TAIL("chcpu(8)"));
260 int main(int argc
, char *argv
[])
267 static const struct option longopts
[] = {
268 { "configure", required_argument
, NULL
, 'c' },
269 { "deconfigure",required_argument
, NULL
, 'g' },
270 { "disable", required_argument
, NULL
, 'd' },
271 { "dispatch", required_argument
, NULL
, 'p' },
272 { "enable", required_argument
, NULL
, 'e' },
273 { "help", no_argument
, NULL
, 'h' },
274 { "rescan", no_argument
, NULL
, 'r' },
275 { "version", no_argument
, NULL
, 'V' },
279 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
280 { 'c','d','e','g','p' },
283 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
285 setlocale(LC_ALL
, "");
286 bindtextdomain(PACKAGE
, LOCALEDIR
);
288 atexit(close_stdout
);
290 maxcpus
= get_max_number_of_cpus();
292 errx(EXIT_FAILURE
, _("cannot determine NR_CPUS; aborting"));
293 if (path_exist(_PATH_SYS_CPU_ONLINE
))
294 onlinecpus
= path_read_cpulist(maxcpus
, _PATH_SYS_CPU_ONLINE
);
295 setsize
= CPU_ALLOC_SIZE(maxcpus
);
296 cpu_set
= CPU_ALLOC(maxcpus
);
298 err(EXIT_FAILURE
, _("cpuset_alloc failed"));
300 while ((c
= getopt_long(argc
, argv
, "c:d:e:g:hp:rV", longopts
, NULL
)) != -1) {
302 err_exclusive_options(c
, longopts
, excl
, excl_st
);
306 cmd
= CMD_CPU_CONFIGURE
;
307 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
310 cmd
= CMD_CPU_DISABLE
;
311 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
314 cmd
= CMD_CPU_ENABLE
;
315 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
318 cmd
= CMD_CPU_DECONFIGURE
;
319 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
324 if (strcmp("horizontal", argv
[optind
- 1]) == 0)
325 cmd
= CMD_CPU_DISPATCH_HORIZONTAL
;
326 else if (strcmp("vertical", argv
[optind
- 1]) == 0)
327 cmd
= CMD_CPU_DISPATCH_VERTICAL
;
329 errx(EXIT_FAILURE
, _("unsupported argument: %s"),
333 cmd
= CMD_CPU_RESCAN
;
336 printf(UTIL_LINUX_VERSION
);
339 errtryhelp(EXIT_FAILURE
);
343 if ((argc
== 1) || (argc
!= optind
)) {
344 warnx(_("bad usage"));
345 errtryhelp(EXIT_FAILURE
);
350 rc
= cpu_enable(cpu_set
, maxcpus
, 1);
352 case CMD_CPU_DISABLE
:
353 rc
= cpu_enable(cpu_set
, maxcpus
, 0);
355 case CMD_CPU_CONFIGURE
:
356 rc
= cpu_configure(cpu_set
, maxcpus
, 1);
358 case CMD_CPU_DECONFIGURE
:
359 rc
= cpu_configure(cpu_set
, maxcpus
, 0);
364 case CMD_CPU_DISPATCH_HORIZONTAL
:
365 rc
= cpu_set_dispatch(0);
367 case CMD_CPU_DISPATCH_VERTICAL
:
368 rc
= cpu_set_dispatch(1);
375 return rc
== 0 ? EXIT_SUCCESS
:
376 rc
< 0 ? EXIT_FAILURE
: CHCPU_EXIT_SOMEOK
;