2026-01-06 09:48:38 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
|
|
|
|
#include "common/percent.h"
|
|
|
|
|
#include "common/size.h"
|
|
|
|
|
#include "common/stringUtils.h"
|
2022-09-05 10:57:09 +02:00
|
|
|
#include "detection/memory/memory.h"
|
2023-05-11 21:07:04 +08:00
|
|
|
#include "modules/memory/memory.h"
|
2022-06-18 14:25:31 +02:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
bool ffPrintMemory(FFMemoryOptions* options) {
|
2024-10-04 15:58:17 +08:00
|
|
|
FFMemoryResult storage = {};
|
2023-05-11 20:39:54 +08:00
|
|
|
const char* error = ffDetectMemory(&storage);
|
2022-10-18 23:46:42 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2021-04-15 18:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
|
2025-07-30 14:30:36 +08:00
|
|
|
ffSizeAppendNum(storage.bytesUsed, &usedPretty);
|
2022-08-26 16:02:04 +02:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
|
2025-07-30 14:30:36 +08:00
|
|
|
ffSizeAppendNum(storage.bytesTotal, &totalPretty);
|
2022-08-26 16:02:04 +02:00
|
|
|
|
2023-08-16 16:46:21 +08:00
|
|
|
double percentage = storage.bytesTotal == 0
|
2023-05-11 20:39:54 +08:00
|
|
|
? 0
|
2023-08-16 16:46:21 +08:00
|
|
|
: (double) storage.bytesUsed / (double) storage.bytesTotal * 100.0;
|
2022-10-18 23:46:42 +08:00
|
|
|
|
2024-11-14 15:33:43 +08:00
|
|
|
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0) {
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (storage.bytesTotal == 0) {
|
2022-09-21 15:21:51 +08:00
|
|
|
puts("Disabled");
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
2022-10-18 22:55:28 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendBar(&str, percentage, options->percent, &options->moduleArgs);
|
2022-10-18 22:55:28 +08:00
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) {
|
2023-01-12 22:41:11 +08:00
|
|
|
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2022-10-18 22:55:28 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
|
2024-05-08 15:25:59 +08:00
|
|
|
ffPercentAppendNum(&str, percentage, options->percent, str.length > 0, &options->moduleArgs);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2022-10-18 22:55:28 +08:00
|
|
|
|
2023-01-12 22:41:11 +08:00
|
|
|
ffStrbufTrimRight(&str, ' ');
|
2022-10-18 22:55:28 +08:00
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
}
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY percentageNum = ffStrbufCreate();
|
2026-03-29 09:28:49 +08:00
|
|
|
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
|
2024-11-14 15:33:43 +08:00
|
|
|
ffPercentAppendNum(&percentageNum, percentage, options->percent, false, &options->moduleArgs);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-07-08 15:21:12 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY percentageBar = ffStrbufCreate();
|
2026-03-29 09:28:49 +08:00
|
|
|
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
|
2024-11-14 15:33:43 +08:00
|
|
|
ffPercentAppendBar(&percentageBar, percentage, options->percent, &options->moduleArgs);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-11-14 15:33:43 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
|
|
|
|
FF_ARG(usedPretty, "used"),
|
|
|
|
|
FF_ARG(totalPretty, "total"),
|
|
|
|
|
FF_ARG(percentageNum, "percentage"),
|
|
|
|
|
FF_ARG(percentageBar, "percentage-bar"),
|
|
|
|
|
}));
|
2021-03-19 20:57:07 +01:00
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2022-09-08 14:51:31 +02:00
|
|
|
}
|
2023-05-11 21:07:04 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffParseMemoryJsonObject(FFMemoryOptions* options, yyjson_val* module) {
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2023-08-17 16:09:41 +08:00
|
|
|
size_t idx, max;
|
2026-03-29 09:28:49 +08:00
|
|
|
yyjson_obj_foreach (module, idx, max, key, val) {
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-05-11 21:07:04 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (ffPercentParseJsonObject(key, val, &options->percent)) {
|
2024-01-20 18:40:12 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-01-20 18:40:12 +08:00
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2023-05-11 21:07:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-06 15:58:43 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffGenerateMemoryJsonConfig(FFMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2023-10-24 14:41:29 +08:00
|
|
|
|
2025-08-04 16:46:28 +08:00
|
|
|
ffPercentGenerateJsonConfig(doc, module, options->percent);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 16:20:59 +08:00
|
|
|
bool ffGenerateMemoryJsonResult(FF_A_UNUSED FFMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-05-08 23:17:22 +08:00
|
|
|
FFMemoryResult storage = {};
|
2023-09-06 15:58:43 +08:00
|
|
|
const char* error = ffDetectMemory(&storage);
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2023-09-06 15:58:43 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-09-06 15:58:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "total", storage.bytesTotal);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "used", storage.bytesUsed);
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2023-09-06 15:58:43 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitMemoryOptions(FFMemoryOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2026-03-30 10:27:23 +08:00
|
|
|
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
|
2025-08-04 14:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyMemoryOptions(FFMemoryOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffMemoryModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_MEMORY_MODULE_NAME,
|
2026-04-16 18:27:11 +08:00
|
|
|
.description = "Print system memory usage information",
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitMemoryOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyMemoryOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParseMemoryJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintMemory,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateMemoryJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateMemoryJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
2026-03-30 10:27:23 +08:00
|
|
|
{ "Used size", "used" },
|
|
|
|
|
{ "Total size", "total" },
|
|
|
|
|
{ "Percentage used (num)", "percentage" },
|
|
|
|
|
{ "Percentage used (bar)", "percentage-bar" },
|
2026-03-30 10:17:45 +08:00
|
|
|
}))
|
|
|
|
|
};
|