mirror of
https://github.com/thepigeongenerator/cpusetcores.git
synced 2025-12-16 21:45:46 +01:00
allow to list active cpu cores
also use nproc to get an accurate amount instead of guessing
This commit is contained in:
11
src/main.c
11
src/main.c
@@ -19,6 +19,10 @@ static inline bool cpu_setter(uint32_t id, bool nstate, uint8_t opts) {
|
||||
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) {
|
||||
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
|
||||
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)
|
||||
ncpus = mcpus - ncpus;
|
||||
|
||||
@@ -48,7 +57,7 @@ int32_t main(int32_t argc, char** argv) {
|
||||
}
|
||||
|
||||
if ((opts & OPT_LIST_CORES) || (opts & OPT_VERBOSE))
|
||||
printf("%i/%i cpus enabled\n", ncpus, mcpus);
|
||||
print_cpu_count(mcpus);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,10 @@ uint8_t getoptions(int32_t argc, char* const* argv, int32_t* ncpus) {
|
||||
if (optind < argc) {
|
||||
char* num = argv[optind];
|
||||
*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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user