7 Commits

3 changed files with 44 additions and 24 deletions

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
# cpusetcores
This is a utility application for managing CPU cores / threads on Linux machines.
It allows you to enable/disable threads (via reading/writing to `/sys/devices/system/cpu/cpuX/online`).
The plan is to eventually through deamons or some kernel magic to manage the amount of cores automatically, in hopes of saving power.
How well this'll work and how much power is gained from it is still researched and the project is not actively being worked on.
So for now it just serves as something faster than a shell script.
I assume the kernel likely already has things for this, or it is being worked on.
I just haven't looked into it that much yet, since I haven't really started the project yet.

View File

@@ -1,44 +1,49 @@
#include "opts.h" #include "opts.h"
#include <errno.h>
#include <getopt.h> #include <getopt.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include "error.h" #include "error.h"
#include "util/intdef.h"
uint8_t getoptions(int32_t argc, char *const *argv, int32_t *ncpus) { u8 getoptions(int argc, char *const *argv, int *ncpus) {
*ncpus = -1;
uint8_t opts = 0; uint8_t opts = 0;
char opt; char opt;
while ((opt = getopt(argc, argv, "lavi")) != -1) { while ((opt = getopt(argc, argv, "lavih")) != -1) {
switch (opt) { switch (opt) {
case 'l': case 'l': opts |= OPT_LIST_CORES; break;
if (opts & OPT_LIST_CORES) fatal("-%c has already been set", 'l'); case 'a': opts |= OPT_SET_ALL; break;
opts |= OPT_LIST_CORES; case 'v': opts |= OPT_VERBOSE | OPT_LIST_CORES; break;
break; case 'i': opts |= OPT_INVERT; break;
case 'a':
if (opts & OPT_SET_ALL) fatal("-%c has already been set", 'a');
opts |= OPT_SET_ALL;
break;
case 'v':
if (opts & OPT_VERBOSE) fatal("-%c has already been set", 'v');
opts |= OPT_VERBOSE | OPT_LIST_CORES;
break;
case 'i':
if (opts & OPT_INVERT) fatal("-%c has already been set", 'i');
opts |= OPT_INVERT;
break;
case '?': case '?':
fatal("usage: %s [number] [-l] [-a] [-v] [-i]", argv[0]); printf("specify -h for help\n");
exit(EXIT_FAILURE);
case 'h':
printf(
"%s: a tool for turning on/off CPU threads\n"
"USAGE: %s [integer] [-l] [-a] [-v] [-i] [-h]\n"
"ARGUMENTS:\n"
"\t-l : List cores after executing the command. May be specified without integer.\n"
"\t-a : Writes to all cores, regardless of their state.\n"
"\t-v : Print the cores that are written. (implies -l)\n"
"\t-i : Instead of the given number.\n"
"\t-h : Shows this dialogue.\n",
argv[0], argv[0]);
exit(EXIT_SUCCESS);
} }
} }
if (optind < argc) { if (optind < argc) {
char *end;
char *num = argv[optind]; char *num = argv[optind];
*ncpus = atoi(num); *ncpus = strtol(num, &end, 0);
} else if (opts & OPT_LIST_CORES) { if (errno || end == num || *end != '\0')
*ncpus = -1; fatal("failed to parse numeric value from string '%s'", num);
} else } else if (!(opts & OPT_LIST_CORES))
fatal("must execute with either a number or [-l]"); fatal("must execute with either a number or [-l]");
return opts; return opts;

View File

@@ -1,6 +1,8 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include "util/intdef.h"
enum opt { enum opt {
OPT_LIST_CORES = 1, // option that lists the total amount of cores that are set OPT_LIST_CORES = 1, // option that lists the total amount of cores that are set
OPT_SET_ALL = 2, // option to set all cores, regardless of their assumed state OPT_SET_ALL = 2, // option to set all cores, regardless of their assumed state
@@ -8,4 +10,7 @@ enum opt {
OPT_INVERT = 8, // 'num' now represents the amount of cores to disable OPT_INVERT = 8, // 'num' now represents the amount of cores to disable
}; };
uint8_t getoptions(int32_t, char *const *, int32_t *); /* acquires the options given by the user through arguments.
* returns the bitmap of these options.
* `ncpus` is set to `-1`, or the amount of CPUs that the user requested to enable.*/
u8 getoptions(int argc, char *const *argv, int *ncpus);