suwako / farfetch アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
farfetch / src / cpu.c
3.8 KB、137行 生 Blame 履歴
1#include "cpu.h"
2#include "common.h"
3#include <stdio.h>
4#include <stdlib.h>
5
6#if defined(__OpenBSD__)
7#include <string.h>
8#include <sys/types.h>
9#include <sys/sysctl.h>
10
11void rm_junk(char *str, const char *sub) {
12 char *pos;
13 size_t len = strlen(sub);
14
15 while ((pos = strstr(str, sub)) != NULL)
16 memmove(pos, pos + len, strlen(pos + len) + 1);
17}
18
19const char *display_cpu() {
20 char *out = NULL;
21 char *model = NULL;
22 model = malloc(1024);
23 if (!model) return NULL;
24 long long ncpu = 0;
25
26 int mib[2];
27 size_t len = 1024;
28
29 // hw.model
30 mib[0] = CTL_HW;
31 mib[1] = HW_MODEL;
32 if (sysctl(mib, 2, model, &len, NULL, 0) != 0) {
33 perror("sysctl");
34 free(model);
35 return NULL;
36 }
37
38 // ゴミ文字の消し
39 rm_junk(model, "CPU ");
40 rm_junk(model, "Processor ");
41 rm_junk(model, "(R)");
42 rm_junk(model, "(TM)");
43
44 // hw.ncpu
45 mib[1] = HW_NCPU;
46 len = sizeof(ncpu);
47 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) {
48 perror("sysctl");
49 free(model);
50 return NULL;
51 }
52
53 out = malloc(strlen(model) + 20);
54 if (!out) {
55 free(model);
56 return NULL;
57 }
58
59 snprintf(out, strlen(model) + 20, "%s (%lld core)", model, ncpu);
60 free(model);
61
62 return out;
63}
64#else
65const char *display_cpu() {
66#if !defined(__HAIKU__)
67 const char *out = from_cache("/tmp/farfetch/cpu");
68 if (out) return out;
69#else
70 const char *out = NULL;
71#endif
72 char *cmd = NULL;
73
74#if defined(__NetBSD__)
75 cmd = malloc(256);
76 if (!cmd) return NULL;
77 snprintf(cmd, 256, "sysctl -n machdep.cpu_brand | sed 's/(R)//' | "
78 "sed 's/(TM)//' | sed 's/CPU //' | sed 's/Processor//' && "
79 "echo \" (\" && sysctl -n hw.ncpu && echo \" core)\"");
80#elif defined(__FreeBSD__)
81 cmd = malloc(1024);
82 if (!cmd) return NULL;
83 snprintf(cmd, 1024, "sysctl -n hw.model | sed 's/(R)//' | "
84 "sed 's/(TM)//' | sed 's/CPU //' | sed 's/ Processor//' && "
85 "echo \" (\" && sysctl -n hw.ncpu && echo \" core)\"");
86#elif defined(__sun)
87 cmd = malloc(1024);
88 if (!cmd) return NULL;
89 snprintf(cmd, 1024, "psrinfo -pv | tail -1 | "
90 "sed 's/(r)//g; s/ CPU//; s/^ *//; s/ $//' | awk '{$1=$1};1' && "
91 "echo \" (\" && psrinfo -p && echo \" core)\"");
92#elif defined(__linux__)
93 cmd = "cat /proc/cpuinfo | awk -F '\\\\s*: | @' "
94 "'/model name|Hardware|Processor|^cpu model|chip type|^cpu type/ { "
95 "cpu=$2; if ($1 == \"Hardware\") exit } END { print cpu }' | "
96 "sed 's/(R)//' | sed 's/(TM)//' | sed 's/CPU //' | sed 's/ Processor//' | "
97 "sed 's/ [0-9]-Core//' && echo \" @ \" && "
98 "cat /proc/cpuinfo | grep \"cpu MHz\" | sort -nr | head -1 | "
99 "sed 's/^.*: //' | awk '{printf \"%.2f\", $1/1000}' && "
100 "echo \"GHz (\" && nproc && echo \" core)\"";
101#elif defined(__HAIKU__)
102 const char *cpuname = run_command_s("sysinfo | grep \"CPU #0:\" | "
103 "sed 's/CPU #0: \"'// | sed 's/\"//' | sed 's/(R)//' | sed 's/(TM)//' | "
104 "sed 's/^.* Gen //' | sed 's/CPU //' | sed 's/ Processor//'");
105 const char *freq = run_command_s("sysinfo | grep -A1 \"CPU #0\" | tail -1 | "
106 "sed 's/^.*: //' | awk '{ printf \"%.2f\", $1 / 1000 }'");
107 long long int proc = run_command_lld("nproc");
108
109 cmd = (char *)malloc(128 * sizeof(char));
110 if (!cmd) return NULL;
111
112 snprintf(cmd, 128, "%s @ %sGHz (%lld core)", cpuname, freq, proc);
113
114 free((void *)cpuname);
115 free((void *)freq);
116#elif defined(__APPLE__)
117 cmd = malloc(1024);
118 if (!cmd) return NULL;
119 snprintf(cmd, 1024, "sysctl -n machdep.cpu.brand_string | sed 's/(R)//' | "
120 "sed 's/(TM)//' | sed 's/CPU //' | sed 's/ Processor//' && "
121 "echo \" (\" && sysctl -n hw.logicalcpu_max && echo \" core)\"");
122#else
123 return NULL;
124#endif
125
126 if (!cmd) return NULL;
127 out = run_command_s(cmd);
128#if !defined(__linux__)
129 free(cmd);
130#endif
131#if !defined(__HAIKU__)
132 to_cache("/tmp/farfetch/cpu", out);
133#endif
134
135 return out;
136}
137#endif