CpuCache: move to a new module; add linux impl

This commit is contained in:
Carter Li
2024-06-05 11:02:26 +08:00
committed by 李通洲
parent f1eaeddfe1
commit df9144ea31
15 changed files with 381 additions and 89 deletions
-37
View File
@@ -23,11 +23,6 @@ void ffPrintCPU(FFCPUOptions* options)
.frequencyBiosLimit = 0.0/0.0,
.name = ffStrbufCreate(),
.vendor = ffStrbufCreate(),
.caches = {
ffListCreate(sizeof (FFCPUCache)),
ffListCreate(sizeof (FFCPUCache)),
ffListCreate(sizeof (FFCPUCache)),
},
};
const char* error = ffDetectCPU(options, &cpu);
@@ -128,9 +123,6 @@ void ffPrintCPU(FFCPUOptions* options)
ffStrbufDestroy(&cpu.name);
ffStrbufDestroy(&cpu.vendor);
ffListDestroy(&cpu.caches[0]);
ffListDestroy(&cpu.caches[1]);
ffListDestroy(&cpu.caches[2]);
}
bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value)
@@ -216,11 +208,6 @@ void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_
.frequencyBiosLimit = 0.0/0.0,
.name = ffStrbufCreate(),
.vendor = ffStrbufCreate(),
.caches = {
ffListCreate(sizeof (FFCPUCache)),
ffListCreate(sizeof (FFCPUCache)),
ffListCreate(sizeof (FFCPUCache)),
},
};
const char* error = ffDetectCPU(options, &cpu);
@@ -250,27 +237,6 @@ void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_
yyjson_mut_obj_add_real(doc, frequency, "min", cpu.frequencyMin);
yyjson_mut_obj_add_real(doc, frequency, "biosLimit", cpu.frequencyBiosLimit);
yyjson_mut_val* caches = yyjson_mut_obj_add_obj(doc, obj, "cache");
for (uint32_t i = 0; i < sizeof (cpu.caches) / sizeof (cpu.caches[0]) && cpu.caches[i].length > 0; i++)
{
yyjson_mut_val* level = yyjson_mut_obj_add_arr(doc, caches, i == 0 ? "L1" : (i == 1 ? "L2" : "L3"));
FF_LIST_FOR_EACH(FFCPUCache, src, cpu.caches[i])
{
yyjson_mut_val* item = yyjson_mut_arr_add_obj(doc, level);
yyjson_mut_obj_add_uint(doc, item, "size", src->size);
yyjson_mut_obj_add_uint(doc, item, "num", src->num);
const char* typeStr = "unknown";
switch (src->type)
{
case FF_CPU_CACHE_TYPE_DATA: typeStr = "data"; break;
case FF_CPU_CACHE_TYPE_INSTRUCTION: typeStr = "instruction"; break;
case FF_CPU_CACHE_TYPE_UNIFIED: typeStr = "unified"; break;
case FF_CPU_CACHE_TYPE_TRACE: typeStr = "trace"; break;
}
yyjson_mut_obj_add_str(doc, item, "type", typeStr);
}
}
yyjson_mut_val* coreTypes = yyjson_mut_obj_add_arr(doc, obj, "coreTypes");
for (uint32_t i = 0; i < sizeof (cpu.coreTypes) / sizeof (cpu.coreTypes[0]) && cpu.coreTypes[i].count > 0; i++)
{
@@ -284,9 +250,6 @@ void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_
ffStrbufDestroy(&cpu.name);
ffStrbufDestroy(&cpu.vendor);
ffListDestroy(&cpu.caches[0]);
ffListDestroy(&cpu.caches[1]);
ffListDestroy(&cpu.caches[2]);
}
void ffPrintCPUHelpFormat(void)
+165
View File
@@ -0,0 +1,165 @@
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "detection/cpucache/cpucache.h"
#include "modules/cpucache/cpucache.h"
#include "util/stringUtils.h"
#define FF_CPUCACHE_NUM_FORMAT_ARGS 4
void ffPrintCPUCache(FFCPUCacheOptions* options)
{
FFCPUCacheResult result = {
.caches = {
ffListCreate(sizeof(FFCPUCache)),
ffListCreate(sizeof(FFCPUCache)),
ffListCreate(sizeof(FFCPUCache)),
ffListCreate(sizeof(FFCPUCache)),
},
};
const char* error = ffDetectCPUCache(&result);
if(error)
{
ffPrintError(FF_CPUCACHE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
goto exit;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_CPUCACHE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
// ffStrbufWriteTo(&result.type, stdout);
// if (result.version.length)
// printf(" (%s)", result.version.chars);
putchar('\n');
}
else
{
// FF_PRINT_FORMAT_CHECKED(FF_CPUCACHE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_CPUCACHE_NUM_FORMAT_ARGS, ((FFformatarg[]) {
// {FF_FORMAT_ARG_TYPE_STRBUF, &result.type, "type"},
// {FF_FORMAT_ARG_TYPE_STRBUF, &result.vendor, "vendor"},
// {FF_FORMAT_ARG_TYPE_STRBUF, &result.version, "version"},
// {FF_FORMAT_ARG_TYPE_STRBUF, &result.serial, "serial"},
// }));
}
exit:
ffListDestroy(&result.caches[0]);
ffListDestroy(&result.caches[1]);
ffListDestroy(&result.caches[2]);
ffListDestroy(&result.caches[3]);
}
bool ffParseCPUCacheCommandOptions(FFCPUCacheOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_CPUCACHE_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffParseCPUCacheJsonObject(FFCPUCacheOptions* options, yyjson_val* module)
{
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
{
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
ffPrintError(FF_CPUCACHE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
}
}
void ffGenerateCPUCacheJsonConfig(FFCPUCacheOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyCPUCacheOptions))) FFCPUCacheOptions defaultOptions;
ffInitCPUCacheOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
void ffGenerateCPUCacheJsonResult(FF_MAYBE_UNUSED FFCPUCacheOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
FFCPUCacheResult result = {
.caches = {
ffListCreate(sizeof(FFCPUCache)),
ffListCreate(sizeof(FFCPUCache)),
ffListCreate(sizeof(FFCPUCache)),
ffListCreate(sizeof(FFCPUCache)),
},
};
const char* error = ffDetectCPUCache(&result);
if (error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
goto exit;
}
yyjson_mut_val* caches = yyjson_mut_obj_add_obj(doc, module, "result");
for (uint32_t i = 0; i < sizeof (result.caches) / sizeof (result.caches[0]) && result.caches[i].length > 0; i++)
{
yyjson_mut_val* level = yyjson_mut_obj_add_arr(doc, caches, i == 0 ? "L1" : (i == 1 ? "L2" : "L3"));
FF_LIST_FOR_EACH(FFCPUCache, src, result.caches[i])
{
yyjson_mut_val* item = yyjson_mut_arr_add_obj(doc, level);
yyjson_mut_obj_add_uint(doc, item, "size", src->size);
yyjson_mut_obj_add_uint(doc, item, "num", src->num);
const char* typeStr = "unknown";
switch (src->type)
{
case FF_CPU_CACHE_TYPE_DATA: typeStr = "data"; break;
case FF_CPU_CACHE_TYPE_INSTRUCTION: typeStr = "instruction"; break;
case FF_CPU_CACHE_TYPE_UNIFIED: typeStr = "unified"; break;
case FF_CPU_CACHE_TYPE_TRACE: typeStr = "trace"; break;
}
yyjson_mut_obj_add_str(doc, item, "type", typeStr);
}
}
exit:
ffListDestroy(&result.caches[0]);
ffListDestroy(&result.caches[1]);
ffListDestroy(&result.caches[2]);
ffListDestroy(&result.caches[3]);
}
void ffPrintCPUCacheHelpFormat(void)
{
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_CPUCACHE_MODULE_NAME, "{1}", FF_CPUCACHE_NUM_FORMAT_ARGS, ((const char* []) {
"cpucache type - type",
"cpucache vendor - vendor",
"cpucache version - version",
"cpucache serial number - serial",
}));
}
void ffInitCPUCacheOptions(FFCPUCacheOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_CPUCACHE_MODULE_NAME,
"Print CPU cache sizes",
ffParseCPUCacheCommandOptions,
ffParseCPUCacheJsonObject,
ffPrintCPUCache,
ffGenerateCPUCacheJsonResult,
ffPrintCPUCacheHelpFormat,
ffGenerateCPUCacheJsonConfig
);
ffOptionInitModuleArg(&options->moduleArgs);
}
void ffDestroyCPUCacheOptions(FFCPUCacheOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "fastfetch.h"
#define FF_CPUCACHE_MODULE_NAME "CPUCache"
void ffPrintCPUCache(FFCPUCacheOptions* options);
void ffInitCPUCacheOptions(FFCPUCacheOptions* options);
void ffDestroyCPUCacheOptions(FFCPUCacheOptions* options);
+11
View File
@@ -0,0 +1,11 @@
#pragma once
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
#include "common/option.h"
typedef struct FFCPUCacheOptions
{
FFModuleBaseInfo moduleInfo;
FFModuleArgs moduleArgs;
} FFCPUCacheOptions;
+1
View File
@@ -12,6 +12,7 @@
#include "modules/camera/camera.h"
#include "modules/chassis/chassis.h"
#include "modules/cpu/cpu.h"
#include "modules/cpucache/cpucache.h"
#include "modules/cpuusage/cpuusage.h"
#include "modules/command/command.h"
#include "modules/colors/colors.h"
+1
View File
@@ -12,6 +12,7 @@
#include "modules/camera/option.h"
#include "modules/chassis/option.h"
#include "modules/cpu/option.h"
#include "modules/cpucache/option.h"
#include "modules/cpuusage/option.h"
#include "modules/colors/option.h"
#include "modules/cursor/option.h"