Files
fastfetch/src/modules/memory.c
T

77 lines
2.4 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include "common/printing.h"
2022-08-26 16:02:04 +02:00
#include "common/parsing.h"
2021-02-18 22:25:36 +01:00
2022-06-18 14:25:31 +02:00
#include <stdlib.h>
2021-04-15 18:59:26 +02:00
#define FF_MEMORY_MODULE_NAME "Memory"
2022-08-26 16:02:04 +02:00
#define FF_MEMORY_NUM_FORMAT_ARGS 3
2021-04-15 18:59:26 +02:00
2021-04-09 14:07:05 +02:00
// Impl inspired by: https://github.com/sam-barr/paleofetch/blob/b7c58a52c0de39b53c9b5f417889a5886d324bfa/paleofetch.c#L544
2021-02-27 18:30:05 +01:00
void ffPrintMemory(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2022-05-23 20:01:18 +02:00
FILE* meminfo = fopen("/proc/meminfo", "r");
2021-02-18 22:25:36 +01:00
if(meminfo == NULL) {
2022-06-17 18:15:36 +02:00
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "fopen(\"""/proc/meminfo\", \"r\") == NULL");
2021-02-18 22:25:36 +01:00
return;
}
2021-02-19 10:07:00 +01:00
char* line = NULL;
2021-04-03 21:51:50 +02:00
size_t len = 0;
2021-02-18 22:25:36 +01:00
uint32_t total = 0,
shared = 0,
memfree = 0,
buffers = 0,
cached = 0,
reclaimable = 0;
2021-02-18 22:25:36 +01:00
while (getline(&line, &len, meminfo) != -1) {
2021-02-19 10:07:00 +01:00
sscanf(line, "MemTotal: %u", &total);
sscanf(line, "Shmem: %u", &shared);
sscanf(line, "MemFree: %u", &memfree);
sscanf(line, "Buffers: %u", &buffers);
sscanf(line, "Cached: %u", &cached);
sscanf(line, "SReclaimable: %u", &reclaimable);
2021-02-18 22:25:36 +01:00
}
2021-04-03 21:51:50 +02:00
if(line != NULL)
free(line);
2021-02-18 22:25:36 +01:00
fclose(meminfo);
2022-08-23 13:53:12 +02:00
uint32_t used = total + shared - memfree - buffers - cached - reclaimable;
uint8_t percentage = (uint8_t) ((used / (double) total) * 100);
if(used == 0 && total == 0)
2021-04-15 18:59:26 +02:00
{
2022-06-17 18:15:36 +02:00
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "/proc/meminfo could't be parsed");
2021-04-15 18:59:26 +02:00
return;
}
2022-08-26 16:02:04 +02:00
FFstrbuf usedPretty;
ffStrbufInit(&usedPretty);
ffParseSize(used * (uint64_t) 1024, instance->config.binaryPrefixType, &usedPretty);
FFstrbuf totalPretty;
ffStrbufInit(&totalPretty);
ffParseSize(total * (uint64_t) 1024, instance->config.binaryPrefixType, &totalPretty);
2022-06-17 18:15:36 +02:00
if(instance->config.memory.outputFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
2022-06-17 18:15:36 +02:00
ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key);
2022-08-26 16:02:04 +02:00
printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, percentage);
2021-03-19 20:57:07 +01:00
}
else
{
2022-06-17 18:15:36 +02:00
ffPrintFormat(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
2022-08-26 16:02:04 +02:00
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
2022-08-23 13:53:12 +02:00
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
2021-04-15 18:59:26 +02:00
});
2021-03-19 20:57:07 +01:00
}
2022-08-26 16:02:04 +02:00
ffStrbufDestroy(&usedPretty);
ffStrbufDestroy(&totalPretty);
2021-03-27 18:14:19 +01:00
}