allow to list active cpu cores

also use nproc to get an accurate amount instead of guessing
This commit is contained in:
2025-03-20 15:23:13 +01:00
parent 4812cbd3c0
commit 0a60702f9d
2 changed files with 14 additions and 2 deletions

View File

@@ -19,6 +19,10 @@ static inline bool cpu_setter(uint32_t id, bool nstate, uint8_t opts) {
return false; return false;
} }
static inline void print_cpu_count(int32_t mcpus) {
printf("%i/%i cpus enabled.\n", get_nprocs(), mcpus); // get the number of available processors
}
int32_t main(int32_t argc, char** argv) { int32_t main(int32_t argc, char** argv) {
if (geteuid() != 0) fatal("must be executed as the root user!"); if (geteuid() != 0) fatal("must be executed as the root user!");
@@ -26,6 +30,11 @@ int32_t main(int32_t argc, char** argv) {
uint8_t opts = getoptions(argc, argv, &ncpus); // the options to use uint8_t opts = getoptions(argc, argv, &ncpus); // the options to use
int32_t mcpus = get_nprocs_conf(); // the max number of CPUs that are available int32_t mcpus = get_nprocs_conf(); // the max number of CPUs that are available
if (opts & OPT_LIST_CORES && ncpus < 0) {
print_cpu_count(mcpus);
return 0;
}
if (opts & OPT_INVERT) if (opts & OPT_INVERT)
ncpus = mcpus - ncpus; ncpus = mcpus - ncpus;
@@ -48,7 +57,7 @@ int32_t main(int32_t argc, char** argv) {
} }
if ((opts & OPT_LIST_CORES) || (opts & OPT_VERBOSE)) if ((opts & OPT_LIST_CORES) || (opts & OPT_VERBOSE))
printf("%i/%i cpus enabled\n", ncpus, mcpus); print_cpu_count(mcpus);
return 0; return 0;
} }

View File

@@ -37,7 +37,10 @@ uint8_t getoptions(int32_t argc, char* const* argv, int32_t* ncpus) {
if (optind < argc) { if (optind < argc) {
char* num = argv[optind]; char* num = argv[optind];
*ncpus = atoi(num); *ncpus = atoi(num);
} else fatal("you must provide a number!"); } else if (opts & OPT_LIST_CORES) {
*ncpus = -1;
} else
fatal("must execute with either a number or [-l]");
return opts; return opts;
} }