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 #define _PATH_SYS_CPU "/sys/devices/system/cpu"
49 #define _PATH_SYS_CPU_ONLINE _PATH_SYS_CPU "/online"
50 #define _PATH_SYS_CPU_RESCAN _PATH_SYS_CPU "/rescan"
51 #define _PATH_SYS_CPU_DISPATCH _PATH_SYS_CPU "/dispatching"
53 static cpu_set_t
*onlinecpus
;
56 #define is_cpu_online(cpu) (CPU_ISSET_S((cpu), CPU_ALLOC_SIZE(maxcpus), onlinecpus))
57 #define num_online_cpus() (CPU_COUNT_S(CPU_ALLOC_SIZE(maxcpus), onlinecpus))
65 CMD_CPU_DISPATCH_HORIZONTAL
,
66 CMD_CPU_DISPATCH_VERTICAL
,
69 static int cpu_enable(cpu_set_t
*cpu_set
, size_t setsize
, int enable
)
75 for (cpu
= 0; cpu
< setsize
; cpu
++) {
76 if (!CPU_ISSET(cpu
, cpu_set
))
78 if (!path_exist(_PATH_SYS_CPU
"/cpu%d", cpu
)) {
79 printf(_("CPU %d does not exist\n"), cpu
);
82 if (!path_exist(_PATH_SYS_CPU
"/cpu%d/online", cpu
)) {
83 printf(_("CPU %d is not hot pluggable\n"), cpu
);
86 online
= path_read_s32(_PATH_SYS_CPU
"/cpu%d/online", cpu
);
87 if ((online
== 1) && (enable
== 1)) {
88 printf(_("CPU %d is already enabled\n"), cpu
);
91 if ((online
== 0) && (enable
== 0)) {
92 printf(_("CPU %d is already disabled\n"), cpu
);
95 if (path_exist(_PATH_SYS_CPU
"/cpu%d/configure", cpu
))
96 configured
= path_read_s32(_PATH_SYS_CPU
"/cpu%d/configure", cpu
);
98 rc
= path_write_str("1", _PATH_SYS_CPU
"/cpu%d/online", cpu
);
99 if ((rc
== -1) && (configured
== 0))
100 printf(_("CPU %d enable failed "
101 "(CPU is deconfigured)\n"), cpu
);
103 printf(_("CPU %d enable failed (%m)\n"), cpu
);
105 printf(_("CPU %d enabled\n"), cpu
);
107 if (onlinecpus
&& num_online_cpus() == 1) {
108 printf(_("CPU %d disable failed "
109 "(last enabled CPU)\n"), cpu
);
112 rc
= path_write_str("0", _PATH_SYS_CPU
"/cpu%d/online", cpu
);
114 printf(_("CPU %d disable failed (%m)\n"), cpu
);
116 printf(_("CPU %d disabled\n"), cpu
);
118 CPU_CLR(cpu
, onlinecpus
);
125 static int cpu_rescan(void)
127 if (!path_exist(_PATH_SYS_CPU_RESCAN
))
128 errx(EXIT_FAILURE
, _("This system does not support rescanning of CPUs"));
129 if (path_write_str("1", _PATH_SYS_CPU_RESCAN
) == -1)
130 err(EXIT_FAILURE
, _("Failed to trigger rescan of CPUs"));
131 printf(_("Triggered rescan of CPUs\n"));
135 static int cpu_set_dispatch(int mode
)
137 if (!path_exist(_PATH_SYS_CPU_DISPATCH
))
138 errx(EXIT_FAILURE
, _("This system does not support setting "
139 "the dispatching mode of CPUs"));
141 if (path_write_str("0", _PATH_SYS_CPU_DISPATCH
) == -1)
142 err(EXIT_FAILURE
, _("Failed to set horizontal dispatch mode"));
143 printf(_("Successfully set horizontal dispatching mode\n"));
145 if (path_write_str("1", _PATH_SYS_CPU_DISPATCH
) == -1)
146 err(EXIT_FAILURE
, _("Failed to set vertical dispatch mode"));
147 printf(_("Successfully set vertical dispatching mode\n"));
152 static int cpu_configure(cpu_set_t
*cpu_set
, size_t setsize
, int configure
)
157 for (cpu
= 0; cpu
< setsize
; cpu
++) {
158 if (!CPU_ISSET(cpu
, cpu_set
))
160 if (!path_exist(_PATH_SYS_CPU
"/cpu%d", cpu
)) {
161 printf(_("CPU %d does not exist\n"), cpu
);
164 if (!path_exist(_PATH_SYS_CPU
"/cpu%d/configure", cpu
)) {
165 printf(_("CPU %d is not configurable\n"), cpu
);
168 current
= path_read_s32(_PATH_SYS_CPU
"/cpu%d/configure", cpu
);
169 if ((current
== 1) && (configure
== 1)) {
170 printf(_("CPU %d is already configured\n"), cpu
);
173 if ((current
== 0) && (configure
== 0)) {
174 printf(_("CPU %d is already deconfigured\n"), cpu
);
177 if ((current
== 1) && (configure
== 0) && onlinecpus
&&
178 is_cpu_online(cpu
)) {
179 printf(_("CPU %d deconfigure failed "
180 "(CPU is enabled)\n"), cpu
);
184 rc
= path_write_str("1", _PATH_SYS_CPU
"/cpu%d/configure", cpu
);
186 printf(_("CPU %d configure failed (%m)\n"), cpu
);
188 printf(_("CPU %d configured\n"), cpu
);
190 rc
= path_write_str("0", _PATH_SYS_CPU
"/cpu%d/configure", cpu
);
192 printf(_("CPU %d deconfigure failed (%m)\n"), cpu
);
194 printf(_("CPU %d deconfigured\n"), cpu
);
200 static void cpu_parse(char *cpu_string
, cpu_set_t
*cpu_set
, size_t setsize
)
204 rc
= cpulist_parse(cpu_string
, cpu_set
, setsize
, 1);
208 errx(EXIT_FAILURE
, _("invalid CPU number in CPU list: %s"), cpu_string
);
209 errx(EXIT_FAILURE
, _("failed to parse CPU list: %s"), cpu_string
);
212 static void __attribute__((__noreturn__
)) usage(FILE *out
)
216 " %s [options]\n"), program_invocation_short_name
);
218 puts(_( "\nOptions:\n"
219 " -h, --help print this help\n"
220 " -e, --enable <cpu-list> enable cpus\n"
221 " -d, --disable <cpu-list> disable cpus\n"
222 " -c, --configure <cpu-list> configure cpus\n"
223 " -g, --deconfigure <cpu-list> deconfigure cpus\n"
224 " -p, --dispatch <mode> set dispatching mode\n"
225 " -r, --rescan trigger rescan of cpus\n"
226 " -V, --version output version information and exit\n"));
228 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
231 int main(int argc
, char *argv
[])
238 static const struct option longopts
[] = {
239 { "configure", required_argument
, 0, 'c' },
240 { "deconfigure",required_argument
, 0, 'g' },
241 { "disable", required_argument
, 0, 'd' },
242 { "dispatch", required_argument
, 0, 'p' },
243 { "enable", required_argument
, 0, 'e' },
244 { "help", no_argument
, 0, 'h' },
245 { "rescan", no_argument
, 0, 'r' },
246 { "version", no_argument
, 0, 'V' },
250 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
251 { 'c','d','e','g','p' },
254 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
256 setlocale(LC_ALL
, "");
257 bindtextdomain(PACKAGE
, LOCALEDIR
);
259 atexit(close_stdout
);
261 maxcpus
= get_max_number_of_cpus();
263 errx(EXIT_FAILURE
, _("cannot determine NR_CPUS; aborting"));
264 if (path_exist(_PATH_SYS_CPU_ONLINE
))
265 onlinecpus
= path_read_cpulist(maxcpus
, _PATH_SYS_CPU_ONLINE
);
266 setsize
= CPU_ALLOC_SIZE(maxcpus
);
267 cpu_set
= CPU_ALLOC(maxcpus
);
269 err(EXIT_FAILURE
, _("cpuset_alloc failed"));
271 while ((c
= getopt_long(argc
, argv
, "c:d:e:g:hp:rV", longopts
, NULL
)) != -1) {
273 err_exclusive_options(c
, longopts
, excl
, excl_st
);
277 cmd
= CMD_CPU_CONFIGURE
;
278 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
281 cmd
= CMD_CPU_DISABLE
;
282 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
285 cmd
= CMD_CPU_ENABLE
;
286 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
289 cmd
= CMD_CPU_DECONFIGURE
;
290 cpu_parse(argv
[optind
- 1], cpu_set
, setsize
);
295 if (strcmp("horizontal", argv
[optind
- 1]) == 0)
296 cmd
= CMD_CPU_DISPATCH_HORIZONTAL
;
297 else if (strcmp("vertical", argv
[optind
- 1]) == 0)
298 cmd
= CMD_CPU_DISPATCH_VERTICAL
;
300 errx(EXIT_FAILURE
, _("unsupported argument: %s"),
304 cmd
= CMD_CPU_RESCAN
;
307 printf(_("%s from %s\n"), program_invocation_short_name
,
315 if ((argc
== 1) || (argc
!= optind
))
320 return cpu_enable(cpu_set
, maxcpus
, 1);
321 case CMD_CPU_DISABLE
:
322 return cpu_enable(cpu_set
, maxcpus
, 0);
323 case CMD_CPU_CONFIGURE
:
324 return cpu_configure(cpu_set
, maxcpus
, 1);
325 case CMD_CPU_DECONFIGURE
:
326 return cpu_configure(cpu_set
, maxcpus
, 0);
329 case CMD_CPU_DISPATCH_HORIZONTAL
:
330 return cpu_set_dispatch(0);
331 case CMD_CPU_DISPATCH_VERTICAL
:
332 return cpu_set_dispatch(1);