get_gpuを最適化

このコミットが含まれているのは:
守矢諏訪子 2023-08-27 14:13:40 +09:00
コミット db01f3469c
2個のファイルの変更44行の追加53行の削除

ファイルの表示

@ -75,15 +75,25 @@
REMOVE("Quad-Core"), \ REMOVE("Quad-Core"), \
REMOVE("Six-Core"), \ REMOVE("Six-Core"), \
REMOVE("Eight-Core"), \ REMOVE("Eight-Core"), \
REMOVE("Core"), \ REMOVE("2-Core"), \
REMOVE("3-Core"), \
REMOVE("4-Core"), \
REMOVE("6-Core"), \
REMOVE("8-Core"), \
REMOVE("12-Core"), \
REMOVE("Processor"), \
REMOVE("CPU"), \ REMOVE("CPU"), \
} }
#define GPU_CONFIG \ #define GPU_CONFIG \
{ \ { \
REMOVE("Advanced Micro Devices, Inc. "), \ REMOVE("Advanced Micro Devices, Inc."), \
REPLACE(" [AMD/ATI]", "AMD/ATI"), \
REMOVE("Corporation"), \ REMOVE("Corporation"), \
REMOVE("Controller"), \ REMOVE("Controller"), \
REMOVE("Graphics"), \ REMOVE("Graphics"), \
REMOVE("Integrated"), \ REMOVE("Integrated"), \
REMOVE("Navi 14"), \
REMOVE("["), \
REMOVE("]"), \
} }

ファイルの表示

@ -412,6 +412,8 @@ static void get_terminal_wayland(char *terminal) {
return; return;
} }
printf("%s\n", terminal);
wl_display_disconnect(display); wl_display_disconnect(display);
} }
@ -474,49 +476,29 @@ static char *get_cpu() {
} }
char *cpu_model = malloc(BUF_SIZE / 2); char *cpu_model = malloc(BUF_SIZE / 2);
char *line = NULL; char *line = malloc(BUF_SIZE);
size_t len; size_t len = 0;
int num_cores = 0, cpu_freq, prec = 3; int num_cores = 0, cpu_freq;
double freq; double freq;
char freq_unit[] = "GHz"; char freq_unit[] = "GHz";
int prec = 3;
while (getline(&line, &len, cpuinfo) != -1) { while (getline(&line, &len, cpuinfo) != -1) {
num_cores += sscanf(line, "model name : %[^\n@]", cpu_model); num_cores += sscanf(line, "model name : %[^\n@]", cpu_model);
} }
free(line);
fclose(cpuinfo);
FILE *cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); fseek(cpuinfo, 0, SEEK_SET);
line = NULL;
if (cpufreq != NULL) { while (getline(&line, &len, cpuinfo) != -1) {
if (getline(&line, &len, cpufreq) != -1) { if (sscanf(line, "cpuMHz : %lf", &freq) > 0) {
sscanf(line, "%d", &cpu_freq); break;
cpu_freq /= 1000;
} else {
fclose(cpufreq);
free(line);
goto cpufreq_fallback;
} }
} else {
cpufreq_fallback:
cpufreq = fopen("/proc/cpuinfo", "r");
if (cpufreq == NULL) {
status = -1;
halt_and_catch_fire("Unable to open cpuinfo");
}
while (getline(&line, &len, cpufreq) != -1) {
if (sscanf(line, "cpu MHz : %lf", &freq) > 0) {
break;
}
}
cpu_freq = (int) freq;
} }
fclose(cpuinfo);
free(line); free(line);
fclose(cpufreq);
cpu_freq = (int) freq;
if (cpu_freq < 1000) { if (cpu_freq < 1000) {
freq = (double) cpu_freq; freq = (double) cpu_freq;
@ -552,53 +534,52 @@ cpufreq_fallback:
if (num_cores == 0) { if (num_cores == 0) {
*cpu = '\0'; *cpu = '\0';
} }
return cpu; return cpu;
} }
static char *find_gpu(int index) { static char *find_gpu(int index) {
char buffer[BUF_SIZE], *device_class, *gpu = malloc(BUF_SIZE); char buffer[BUF_SIZE], *device_class;
struct pci_access *pacc; struct pci_access *pacc;
struct pci_dev *dev; struct pci_dev *dev;
int gpu_index = 0; int gpu_index = 0;
bool found = false; bool found = false;
char *gpu = malloc(BUF_SIZE);
pacc = pci_alloc(); pacc = pci_alloc();
pci_init(pacc); pci_init(pacc);
pci_scan_bus(pacc); pci_scan_bus(pacc);
dev = pacc->devices;
while (dev != NULL) { for (dev = pacc->devices; dev != NULL; dev = dev->next) {
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS); pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
device_class = pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_CLASS, dev->device_class); device_class = pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_CLASS, dev->device_class);
if (strcmp("VGA compatible controller", device_class) == 0 || strcmp("3D controller", device_class) == 0) {
strncpy(gpu, pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_DEVICE | PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id), BUF_SIZE - 1); if (!strcmp("VGA compatible controller", device_class) || !strcmp("3D controller", device_class)) {
snprintf(gpu, BUF_SIZE, "%s", pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_DEVICE | PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id));
if (gpu_index == index) { if (gpu_index == index) {
found = true; found = true;
break; break;
} else {
gpu_index++;
} }
gpu_index++;
} }
dev = dev->next;
}
if (found == false) {
*gpu = '\0';
} }
pci_cleanup(pacc); pci_cleanup(pacc);
for (int i = 0; i < COUNT(gpu_config); ++i) { if (!found) {
if (gpu_config[i].repl_str == NULL) { *gpu = '\0';
remove_substring(gpu, gpu_config[i].substring, gpu_config[i].length); } else {
} else { for (int i = 0; i < COUNT(gpu_config); ++i) {
replace_substring(gpu, gpu_config[i].substring, gpu_config[i].repl_str, gpu_config[i].length, gpu_config[i].repl_len); if (gpu_config[i].repl_str == NULL) {
remove_substring(gpu, gpu_config[i].substring, gpu_config[i].length);
} else {
replace_substring(gpu, gpu_config[i].substring, gpu_config[i].repl_str, gpu_config[i].length, gpu_config[i].repl_len);
}
} }
truncate_spaces(gpu);
} }
truncate_spaces(gpu);
return gpu; return gpu;
} }