mirror of
https://github.com/thepigeongenerator/cpusetcores.git
synced 2025-12-17 05:55:46 +01:00
add cpu handling functions
add the functions which handle the /sys/devices/cpu/cpuX/online files
This commit is contained in:
34
src/main.c
34
src/main.c
@@ -1,5 +1,6 @@
|
|||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -14,6 +15,8 @@ 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
|
||||||
};
|
};
|
||||||
|
|
||||||
|
char const* const cpu_path = "/sys/devices/system/cpu/cpu%i/online";
|
||||||
|
|
||||||
noreturn void fatal(char const* fmt, ...) {
|
noreturn void fatal(char const* fmt, ...) {
|
||||||
char buf[128];
|
char buf[128];
|
||||||
va_list args;
|
va_list args;
|
||||||
@@ -61,6 +64,37 @@ uint8_t getoptions(int32_t argc, char* const* argv, int32_t* ncpus) {
|
|||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool getcore(uint32_t id) {
|
||||||
|
// get the file path
|
||||||
|
char path[64]; // contains the file path (max length is 64 due to the path and a bunch of extra wiggle room)
|
||||||
|
snprintf(path, 64, cpu_path, id); // writes the path using the id
|
||||||
|
|
||||||
|
// if the file doesn't exist; return true
|
||||||
|
if (access(path, R_OK) != 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// read a character from the file, store in state
|
||||||
|
char state = '\0';
|
||||||
|
FILE* f = fopen(path, "r");
|
||||||
|
fread(&state, 1, 1, f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
// return whether state is truthy
|
||||||
|
return !!state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setcore(uint32_t id, bool state) {
|
||||||
|
char path[64];
|
||||||
|
snprintf(path, 64, cpu_path, id);
|
||||||
|
|
||||||
|
char s = 0x30 + (char)state; // convert the state to a character
|
||||||
|
|
||||||
|
// write the state to the file (creates file if it doesn't exist)
|
||||||
|
FILE* f = fopen(path, "w");
|
||||||
|
fwrite(&s, 1, 1, f);
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
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!");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user