master ohmu/fastfetch-openbsd / src / modules / cpu / cpu.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/parsing.h"
  4#include "common/temps.h"
  5#include "common/frequency.h"
  6#include "detection/cpu/cpu.h"
  7#include "modules/cpu/cpu.h"
  8
  9static int sortCores(const FFCPUCore* a, const FFCPUCore* b) {
 10    return (int) b->freq - (int) a->freq;
 11}
 12
 13bool ffPrintCPU(FFCPUOptions* options) {
 14    bool success = false;
 15    FFCPUResult cpu = {
 16        .temperature = FF_CPU_TEMP_UNSET,
 17        .frequencyMax = 0,
 18        .frequencyBase = 0,
 19        .name = ffStrbufCreate(),
 20        .vendor = ffStrbufCreate(),
 21    };
 22
 23    const char* error = ffDetectCPU(options, &cpu);
 24
 25    if (error) {
 26        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(CPU), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 27    } else if (cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1) {
 28        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(CPU), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No CPU detected");
 29    } else {
 30        FF_STRBUF_AUTO_DESTROY coreTypes = ffStrbufCreate();
 31        if (options->showPeCoreCount) {
 32            uint32_t typeCount = 0;
 33            while (cpu.coreTypes[typeCount].count != 0 && typeCount < ARRAY_SIZE(cpu.coreTypes)) {
 34                typeCount++;
 35            }
 36            if (typeCount > 0) {
 37                qsort(cpu.coreTypes, typeCount, sizeof(cpu.coreTypes[0]), (void*) sortCores);
 38
 39                for (uint32_t i = 0; i < typeCount; i++) {
 40                    ffStrbufAppendF(&coreTypes, "%s%u", i == 0 ? "" : "+", cpu.coreTypes[i].count);
 41                }
 42            }
 43        }
 44
 45        if (options->moduleArgs.outputFormat.length == 0) {
 46            ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(CPU), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 47
 48            FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
 49
 50            if (cpu.name.length > 0) {
 51                ffStrbufAppend(&str, &cpu.name);
 52            } else if (cpu.vendor.length > 0) {
 53                ffStrbufAppend(&str, &cpu.vendor);
 54                ffStrbufAppendS(&str, " CPU");
 55            } else {
 56                ffStrbufAppendS(&str, "Unknown");
 57            }
 58
 59            if (coreTypes.length > 0) {
 60                ffStrbufAppendF(&str, " (%s)", coreTypes.chars);
 61            } else if (cpu.coresOnline > 1) {
 62                ffStrbufAppendF(&str, " (%u)", cpu.coresOnline);
 63            }
 64
 65            uint32_t freq = cpu.frequencyMax;
 66            if (freq == 0) {
 67                freq = cpu.frequencyBase;
 68            }
 69            if (freq > 0) {
 70                ffStrbufAppendS(&str, " @ ");
 71                ffFreqAppendNum(freq, &str);
 72            }
 73
 74            if (cpu.temperature != FF_CPU_TEMP_UNSET) {
 75                ffStrbufAppendS(&str, " - ");
 76                ffTempsAppendNum(cpu.temperature, &str, options->tempConfig, &options->moduleArgs);
 77            }
 78
 79            ffStrbufPutTo(&str, stdout);
 80        } else {
 81            FF_STRBUF_AUTO_DESTROY freqBase = ffStrbufCreate();
 82            ffFreqAppendNum(cpu.frequencyBase, &freqBase);
 83            FF_STRBUF_AUTO_DESTROY freqMax = ffStrbufCreate();
 84            ffFreqAppendNum(cpu.frequencyMax, &freqMax);
 85
 86            FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
 87            ffTempsAppendNum(cpu.temperature, &tempStr, options->tempConfig, &options->moduleArgs);
 88            FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(CPU), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 89                                                                                                            FF_ARG(cpu.name, "name"),
 90                                                                                                            FF_ARG(cpu.vendor, "vendor"),
 91                                                                                                            FF_ARG(cpu.coresPhysical, "cores-physical"),
 92                                                                                                            FF_ARG(cpu.coresLogical, "cores-logical"),
 93                                                                                                            FF_ARG(cpu.coresOnline, "cores-online"),
 94                                                                                                            FF_ARG(freqBase, "freq-base"),
 95                                                                                                            FF_ARG(freqMax, "freq-max"),
 96                                                                                                            FF_ARG(tempStr, "temperature"),
 97                                                                                                            FF_ARG(coreTypes, "core-types"),
 98                                                                                                            FF_ARG(cpu.packages, "packages"),
 99                                                                                                            FF_ARG(cpu.march, "march"),
100                                                                                                            FF_ARG(cpu.numaNodes, "numa-nodes"),
101                                                                                                            #if __i386__ || __x86_64__
102                                                                                                            FF_ARG(cpu.codeName, "code-name"),
103                                                                                                            FF_ARG(cpu.technology, "technology"),
104                                                                                                            #endif
105                                                                                                        }));
106        }
107        success = true;
108    }
109
110    ffStrbufDestroy(&cpu.name);
111    ffStrbufDestroy(&cpu.vendor);
112
113    return success;
114}
115
116void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module) {
117    yyjson_val *key, *val;
118    size_t idx, max;
119    yyjson_obj_foreach (module, idx, max, key, val) {
120        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
121            continue;
122        }
123
124        if (ffTempsParseJsonObject(key, val, &options->temp, &options->tempConfig)) {
125            continue;
126        }
127
128        if (unsafe_yyjson_equals_str(key, "tempSensor")) {
129            ffStrbufSetJsonVal(&options->tempSensor, val);
130            continue;
131        }
132
133        if (unsafe_yyjson_equals_str(key, "freqNdigits")) {
134            ffPrintError(FF_MODULE_GET_DISPLAY_NAME(CPU), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "modules.CPU.freqNdigits has been moved to display.freq.ndigits");
135            continue;
136        }
137
138        if (unsafe_yyjson_equals_str(key, "showPeCoreCount")) {
139            options->showPeCoreCount = yyjson_get_bool(val);
140            continue;
141        }
142
143        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(CPU), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
144    }
145}
146
147[[gnu::cold]]
148void ffGenerateCPUJsonConfig(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
149    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
150
151    ffTempsGenerateJsonConfig(doc, module, options->temp, options->tempConfig);
152
153    yyjson_mut_obj_add_bool(doc, module, "showPeCoreCount", options->showPeCoreCount);
154
155    yyjson_mut_obj_add_strbuf(doc, module, "tempSensor", &options->tempSensor);
156}
157
158bool ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
159    bool success = false;
160    FFCPUResult cpu = {
161        .temperature = FF_CPU_TEMP_UNSET,
162        .frequencyMax = 0,
163        .frequencyBase = 0,
164        .name = ffStrbufCreate(),
165        .vendor = ffStrbufCreate(),
166    };
167
168    const char* error = ffDetectCPU(options, &cpu);
169
170    if (error) {
171        yyjson_mut_obj_add_str(doc, module, "error", error);
172    } else if (cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1) {
173        yyjson_mut_obj_add_str(doc, module, "error", "No CPU detected");
174    } else {
175        yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
176        yyjson_mut_obj_add_strbuf(doc, obj, "cpu", &cpu.name);
177        yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &cpu.vendor);
178        if (cpu.packages == 0) {
179            yyjson_mut_obj_add_null(doc, obj, "packages");
180        } else {
181            yyjson_mut_obj_add_uint(doc, obj, "packages", cpu.packages);
182        }
183
184        yyjson_mut_val* cores = yyjson_mut_obj_add_obj(doc, obj, "cores");
185        yyjson_mut_obj_add_uint(doc, cores, "physical", cpu.coresPhysical);
186        yyjson_mut_obj_add_uint(doc, cores, "logical", cpu.coresLogical);
187        yyjson_mut_obj_add_uint(doc, cores, "online", cpu.coresOnline);
188
189        yyjson_mut_val* frequency = yyjson_mut_obj_add_obj(doc, obj, "frequency");
190        yyjson_mut_obj_add_uint(doc, frequency, "base", cpu.frequencyBase);
191        yyjson_mut_obj_add_uint(doc, frequency, "max", cpu.frequencyMax);
192
193        yyjson_mut_val* coreTypes = yyjson_mut_obj_add_arr(doc, obj, "coreTypes");
194        for (uint32_t i = 0; i < ARRAY_SIZE(cpu.coreTypes) && cpu.coreTypes[i].count > 0; i++) {
195            yyjson_mut_val* core = yyjson_mut_arr_add_obj(doc, coreTypes);
196            yyjson_mut_obj_add_uint(doc, core, "count", cpu.coreTypes[i].count);
197            yyjson_mut_obj_add_uint(doc, core, "freq", cpu.coreTypes[i].freq);
198        }
199
200        if (cpu.temperature != FF_CPU_TEMP_UNSET) {
201            yyjson_mut_obj_add_real(doc, obj, "temperature", cpu.temperature);
202        } else {
203            yyjson_mut_obj_add_null(doc, obj, "temperature");
204        }
205
206        if (cpu.march) {
207            yyjson_mut_obj_add_str(doc, obj, "march", cpu.march);
208        } else {
209            yyjson_mut_obj_add_null(doc, obj, "march");
210        }
211
212        if (cpu.numaNodes > 0) {
213            yyjson_mut_obj_add_uint(doc, obj, "numaNodes", cpu.numaNodes);
214        } else {
215            yyjson_mut_obj_add_null(doc, obj, "numaNodes");
216        }
217
218        #if __i386__ || __x86_64__
219        if (cpu.codeName) {
220            yyjson_mut_obj_add_str(doc, obj, "codeName", cpu.codeName);
221        } else {
222            yyjson_mut_obj_add_null(doc, obj, "codeName");
223        }
224
225        if (cpu.technology) {
226            yyjson_mut_obj_add_str(doc, obj, "technology", cpu.technology);
227        } else {
228            yyjson_mut_obj_add_null(doc, obj, "technology");
229        }
230        #endif
231
232        success = true;
233    }
234
235    ffStrbufDestroy(&cpu.name);
236    ffStrbufDestroy(&cpu.vendor);
237
238    return success;
239}
240
241void ffInitCPUOptions(FFCPUOptions* options) {
242    ffOptionInitModuleArg(&options->moduleArgs, "");
243    ffStrbufInit(&options->tempSensor);
244    options->temp = false;
245    options->tempConfig = (FFColorRangeConfig) { 60, 80 };
246    options->showPeCoreCount = true;
247}
248
249void ffDestroyCPUOptions(FFCPUOptions* options) {
250    ffOptionDestroyModuleArg(&options->moduleArgs);
251    ffStrbufDestroy(&options->tempSensor);
252}
253
254FFModuleBaseInfo ffCPUModuleInfo = {
255    .name = "CPU",
256    .description = "Print CPU name, frequency, etc.",
257    .displayName = {
258        .en = "CPU",
259        .ar = "المعالج",
260        .cs = "CPU",
261        .de = "CPU",
262        .es = "CPU",
263        .fi = "Suoritin",
264        .fr = "CPU",
265        .gl = "CPU",
266        .he = "מעבד",
267        .id = "CPU",
268        .it = "CPU",
269        .ja = "CPU",
270        .ko = "CPU",
271        .pl = "CPU",
272        .pt = "CPU",
273        .ru = "Процессор",
274        .tr = "CPU",
275        .uk = "CPU",
276        .vi = "CPU",
277        .zh_CN = "CPU",
278        .zh_TW = "CPU",
279    },
280    .initOptions = (void*) ffInitCPUOptions,
281    .destroyOptions = (void*) ffDestroyCPUOptions,
282    .parseJsonObject = (void*) ffParseCPUJsonObject,
283    .printModule = (void*) ffPrintCPU,
284    .generateJsonResult = (void*) ffGenerateCPUJsonResult,
285    .generateJsonConfig = (void*) ffGenerateCPUJsonConfig,
286    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
287        { "Name", "name" },
288        { "Vendor", "vendor" },
289        { "Physical core count", "cores-physical" },
290        { "Logical core count", "cores-logical" },
291        { "Online core count", "cores-online" },
292        { "Base frequency (formatted)", "freq-base" },
293        { "Max frequency (formatted)", "freq-max" },
294        { "Temperature (formatted)", "temperature" },
295        { "Logical core count grouped by frequency", "core-types" },
296        { "Processor package count", "packages" },
297        { "CPU microarchitecture", "march" },
298        { "NUMA node count", "numa-nodes" },
299        #if __i386__ || __x86_64__
300        { "CPU code name", "code-name" },
301        { "CPU technology", "technology" },
302        #endif
303    })),
304    .defaultOrder = 33,
305};