From f3c58a3923ac467ee3c5057da6799a926aa3fce1 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 20 Mar 2025 13:59:10 +0100 Subject: [PATCH] implement activation / deactivation --- src/main.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index b352667..6e2cfc5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,24 @@ -#include -#include #include -#include +#include #include #include +#include "cpu.h" #include "error.h" #include "opts.h" +static inline bool cpu_setter(uint32_t id, bool nstate, uint8_t opts) { + bool cstate = !nstate; + if (!(opts & OPT_SET_ALL)) + cstate = getcpu(id); + + if (cstate != nstate) { + setcpu(id, nstate); + return true; + } + return false; +} + int32_t main(int32_t argc, char** argv) { if (geteuid() != 0) fatal("must be executed as the root user!"); @@ -18,7 +29,26 @@ int32_t main(int32_t argc, char** argv) { if (opts & OPT_INVERT) ncpus = mcpus - ncpus; + // validate arguments if (ncpus > mcpus) fatal("%i exeeds the maximum numbers of cpus available (%i)", ncpus, mcpus); if (ncpus < 1) fatal("may not keep less than 1 cpu enabled, requested to enable %i", ncpus); + + char const* const cpu_set_log = "set cpu %i to %hi\n"; + for (int32_t id = 1; id < mcpus; id++) { // start at CPU 1, as CPU 0 is not writeable + if (id <= ncpus) { + if (cpu_setter(id, true, opts) && (opts & OPT_VERBOSE)) + printf(cpu_set_log, id, 1); + continue; + } + + if (cpu_setter(id, false, opts) && (opts & OPT_VERBOSE)) + printf(cpu_set_log, id, 0); + else if (!(opts & OPT_SET_ALL)) + break; // break if OPT_SET_ALL is not set (assuming the rest of the CPUs are already off) + } + + if ((opts & OPT_LIST_CORES) || (opts & OPT_VERBOSE)) + printf("%i/%i cpus enabled\n", ncpus, mcpus); + return 0; }