static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable) { unsigned int cpu; int online, rc; int configured = -1; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { printf(_("CPU %d does not exist\n"), cpu); continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/online", cpu)) { printf(_("CPU %d is not hot pluggable\n"), cpu); continue; } online = path_read_s32(_PATH_SYS_CPU "/cpu%d/online", cpu); if ((online == 1) && (enable == 1)) { printf(_("CPU %d is already enabled\n"), cpu); continue; } if ((online == 0) && (enable == 0)) { printf(_("CPU %d is already disabled\n"), cpu); continue; } if (path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) configured = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu); if (enable) { rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/online", cpu); if ((rc == -1) && (configured == 0)) printf(_("CPU %d enable failed " "(CPU is deconfigured)\n"), cpu); else if (rc == -1) printf(_("CPU %d enable failed (%m)\n"), cpu); else printf(_("CPU %d enabled\n"), cpu); } else { if (onlinecpus && num_online_cpus() == 1) { printf(_("CPU %d disable failed " "(last enabled CPU)\n"), cpu); continue; } rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/online", cpu); if (rc == -1) printf(_("CPU %d disable failed (%m)\n"), cpu); else { printf(_("CPU %d disabled\n"), cpu); if (onlinecpus) CPU_CLR(cpu, onlinecpus); } } } return EXIT_SUCCESS; }
/* returns: 0 = success * < 0 = failure * > 0 = partial success */ static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure) { unsigned int cpu; int rc, current; size_t fails = 0; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { warnx(_("CPU %u does not exist"), cpu); fails++; continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) { warnx(_("CPU %u is not configurable"), cpu); fails++; continue; } current = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu); if ((current == 1) && (configure == 1)) { printf(_("CPU %u is already configured\n"), cpu); continue; } if ((current == 0) && (configure == 0)) { printf(_("CPU %u is already deconfigured\n"), cpu); continue; } if ((current == 1) && (configure == 0) && onlinecpus && is_cpu_online(cpu)) { warnx(_("CPU %u deconfigure failed (CPU is enabled)"), cpu); fails++; continue; } if (configure) { rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) { warn(_("CPU %u configure failed"), cpu); fails++; } else printf(_("CPU %u configured\n"), cpu); } else { rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) { warn(_("CPU %u deconfigure failed"), cpu); fails++; } else printf(_("CPU %u deconfigured\n"), cpu); } } return fails == 0 ? 0 : fails == setsize ? -1 : 1; }
static int cpu_set_dispatch(int mode) { if (!path_exist(_PATH_SYS_CPU_DISPATCH)) errx(EXIT_FAILURE, _("This system does not support setting " "the dispatching mode of CPUs")); if (mode == 0) { if (path_write_str("0", _PATH_SYS_CPU_DISPATCH) == -1) err(EXIT_FAILURE, _("Failed to set horizontal dispatch mode")); printf(_("Successfully set horizontal dispatching mode\n")); } else { if (path_write_str("1", _PATH_SYS_CPU_DISPATCH) == -1) err(EXIT_FAILURE, _("Failed to set vertical dispatch mode")); printf(_("Successfully set vertical dispatching mode\n")); } return EXIT_SUCCESS; }
static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure) { unsigned int cpu; int rc, current; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { printf(_("CPU %d does not exist\n"), cpu); continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) { printf(_("CPU %d is not configurable\n"), cpu); continue; } current = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu); if ((current == 1) && (configure == 1)) { printf(_("CPU %d is already configured\n"), cpu); continue; } if ((current == 0) && (configure == 0)) { printf(_("CPU %d is already deconfigured\n"), cpu); continue; } if ((current == 1) && (configure == 0) && onlinecpus && is_cpu_online(cpu)) { printf(_("CPU %d deconfigure failed " "(CPU is enabled)\n"), cpu); continue; } if (configure) { rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) printf(_("CPU %d configure failed (%m)\n"), cpu); else printf(_("CPU %d configured\n"), cpu); } else { rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) printf(_("CPU %d deconfigure failed (%m)\n"), cpu); else printf(_("CPU %d deconfigured\n"), cpu); } } return EXIT_SUCCESS; }
static int cpu_rescan(void) { if (!path_exist(_PATH_SYS_CPU_RESCAN)) errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs")); if (path_write_str("1", _PATH_SYS_CPU_RESCAN) == -1) err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs")); printf(_("Triggered rescan of CPUs\n")); return EXIT_SUCCESS; }
static int set_score_adj(const pid_t pid, int adj) { char buf[sizeof(stringify_value(INT_MAX))]; snprintf(buf, sizeof(buf), "%d", adj); if (path_write_str(buf, "/proc/%d/oom_score_adj", (int) pid) < 0) return -1; return 0; }
/* returns: 0 = success * < 0 = failure * > 0 = partial success */ static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable) { unsigned int cpu; int online, rc; int configured = -1; size_t fails = 0; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { warnx(_("CPU %u does not exist"), cpu); fails++; continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/online", cpu)) { warnx(_("CPU %u is not hot pluggable"), cpu); fails++; continue; } online = path_read_s32(_PATH_SYS_CPU "/cpu%d/online", cpu); if ((online == 1) && (enable == 1)) { printf(_("CPU %u is already enabled\n"), cpu); continue; } if ((online == 0) && (enable == 0)) { printf(_("CPU %u is already disabled\n"), cpu); continue; } if (path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) configured = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu); if (enable) { rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/online", cpu); if ((rc == -1) && (configured == 0)) { warn(_("CPU %u enable failed (CPU is deconfigured)"), cpu); fails++; } else if (rc == -1) { warn(_("CPU %u enable failed"), cpu); fails++; } else printf(_("CPU %u enabled\n"), cpu); } else { if (onlinecpus && num_online_cpus() == 1) { warnx(_("CPU %u disable failed (last enabled CPU)"), cpu); fails++; continue; } rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/online", cpu); if (rc == -1) { warn(_("CPU %u disable failed"), cpu); fails++; } else { printf(_("CPU %u disabled\n"), cpu); if (onlinecpus) CPU_CLR(cpu, onlinecpus); } } } return fails == 0 ? 0 : fails == setsize ? -1 : 1; }