mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Swap: split module code from memory.c
This commit is contained in:
@@ -296,6 +296,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/separator.c
|
||||
src/modules/shell.c
|
||||
src/modules/sound.c
|
||||
src/modules/swap.c
|
||||
src/modules/media.c
|
||||
src/modules/terminal.c
|
||||
src/modules/terminalfont.c
|
||||
|
||||
@@ -3,8 +3,14 @@
|
||||
#ifndef FF_INCLUDED_detection_memory_memory
|
||||
#define FF_INCLUDED_detection_memory_memory
|
||||
|
||||
#include "detection/storage.h"
|
||||
#include "fastfetch.h"
|
||||
|
||||
void ffDetectMemory(FFMemoryStorage* result);
|
||||
typedef struct FFMemoryResult
|
||||
{
|
||||
uint64_t bytesUsed;
|
||||
uint64_t bytesTotal;
|
||||
} FFMemoryResult;
|
||||
|
||||
const char* ffDetectMemory(FFMemoryResult* result);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,31 +3,25 @@
|
||||
#include <string.h>
|
||||
#include <mach/mach.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void ffDetectMemory(FFMemoryStorage* ram)
|
||||
const char* ffDetectMemory(FFMemoryResult* ram)
|
||||
{
|
||||
size_t length = sizeof(ram->bytesTotal);
|
||||
if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0))
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read hw.memsize");
|
||||
return;
|
||||
}
|
||||
return "Failed to read hw.memsize";
|
||||
|
||||
uint32_t pagesize;
|
||||
length = sizeof(pagesize);
|
||||
if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pagesize, &length, NULL, 0))
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
|
||||
return;
|
||||
}
|
||||
return "Failed to read hw.pagesize";
|
||||
|
||||
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
|
||||
vm_statistics64_data_t vmstat;
|
||||
if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read host_statistics64");
|
||||
return;
|
||||
}
|
||||
return "Failed to read host_statistics64";
|
||||
|
||||
ram->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
#include "memory.h"
|
||||
#include "common/sysctl.h"
|
||||
|
||||
void ffDetectMemory(FFMemoryStorage* ram)
|
||||
const char* ffDetectMemory(FFMemoryResult* ram)
|
||||
{
|
||||
uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
|
||||
if(pageSize == 0)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
|
||||
return;
|
||||
}
|
||||
uint32_t pageSize;
|
||||
uint64_t length = sizeof(pageSize);
|
||||
if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pageSize, &length, NULL, 0))
|
||||
return "Failed to read hw.pagesize";
|
||||
|
||||
ram->bytesTotal = (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_page_count", 0) * pageSize;
|
||||
if(ram->bytesTotal == 0)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read vm.stats.vm.v_page_count");
|
||||
return;
|
||||
}
|
||||
return "Failed to read vm.stats.vm.v_page_count";
|
||||
|
||||
ram->bytesUsed = ram->bytesTotal
|
||||
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_free_count", 0) * pageSize
|
||||
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_inactive_count", 0) * pageSize
|
||||
;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
#include "memory.h"
|
||||
#include "util/mallocHelper.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void ffDetectMemory(FFMemoryStorage* ram)
|
||||
const char* ffDetectMemory(FFMemoryResult* ram)
|
||||
{
|
||||
FILE* meminfo = fopen("/proc/meminfo", "r");
|
||||
if(meminfo == NULL)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to open /proc/meminfo");
|
||||
return;
|
||||
}
|
||||
return "Failed to open /proc/meminfo";
|
||||
|
||||
char* line = NULL;
|
||||
char* FF_AUTO_FREE line = NULL;
|
||||
size_t len = 0;
|
||||
|
||||
uint32_t memTotal = 0,
|
||||
@@ -32,14 +30,13 @@ void ffDetectMemory(FFMemoryStorage* ram)
|
||||
sscanf(line, "SReclaimable: %u", &sReclaimable);
|
||||
}
|
||||
|
||||
if(line != NULL)
|
||||
free(line);
|
||||
|
||||
fclose(meminfo);
|
||||
|
||||
ram->bytesTotal = memTotal * (uint64_t) 1024;
|
||||
if(ram->bytesTotal == 0)
|
||||
ffStrbufAppendS(&ram->error, "Failed to read MemTotal");
|
||||
return "Failed to read MemTotal";
|
||||
else
|
||||
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -2,16 +2,15 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
void ffDetectMemory(FFMemoryStorage* ram)
|
||||
const char* ffDetectMemory(FFMemoryResult* ram)
|
||||
{
|
||||
MEMORYSTATUSEX statex = {
|
||||
.dwLength = sizeof(statex),
|
||||
};
|
||||
if (!GlobalMemoryStatusEx(&statex))
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "GlobalMemoryStatusEx() failed");
|
||||
return;
|
||||
}
|
||||
return "GlobalMemoryStatusEx() failed";
|
||||
|
||||
ram->bytesTotal = statex.ullTotalPhys;
|
||||
ram->bytesUsed = statex.ullTotalPhys - statex.ullAvailPhys;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef FF_INCLUDED_detection_storage
|
||||
#define FF_INCLUDED_detection_storage
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
typedef struct FFMemoryStorage
|
||||
{
|
||||
FFstrbuf error;
|
||||
uint64_t bytesUsed;
|
||||
uint64_t bytesTotal;
|
||||
} FFMemoryStorage;
|
||||
|
||||
#endif
|
||||
@@ -3,8 +3,14 @@
|
||||
#ifndef FF_INCLUDED_detection_swap_swap
|
||||
#define FF_INCLUDED_detection_swap_swap
|
||||
|
||||
#include "detection/storage.h"
|
||||
#include "fastfetch.h"
|
||||
|
||||
void ffDetectSwap(FFMemoryStorage* result);
|
||||
typedef struct FFSwapResult
|
||||
{
|
||||
uint64_t bytesUsed;
|
||||
uint64_t bytesTotal;
|
||||
} FFSwapResult;
|
||||
|
||||
const char* ffDetectSwap(FFSwapResult* result);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,16 +3,14 @@
|
||||
#include <mach/mach.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
void ffDetectSwap(FFMemoryStorage* swap)
|
||||
const char* ffDetectSwap(FFSwapResult* swap)
|
||||
{
|
||||
struct xsw_usage xsw;
|
||||
size_t size = sizeof(xsw);
|
||||
if(sysctl((int[]){ CTL_VM, VM_SWAPUSAGE }, 2, &xsw, &size, NULL, 0) != 0)
|
||||
{
|
||||
ffStrbufAppendS(&swap->error, "Failed to read vm.swapusage");
|
||||
return;
|
||||
}
|
||||
return "Failed to read vm.swapusage";
|
||||
|
||||
swap->bytesTotal = xsw.xsu_total;
|
||||
swap->bytesUsed = xsw.xsu_used;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "swap.h"
|
||||
#include "common/sysctl.h"
|
||||
|
||||
void ffDetectSwap(FFMemoryStorage* swap)
|
||||
const char* ffDetectSwap(FFSwapResult* swap)
|
||||
{
|
||||
swap->bytesTotal = (uint64_t)ffSysctlGetInt64("vm.swap_total", 0);
|
||||
swap->bytesUsed = (uint64_t)ffSysctlGetInt64("vm.swap_reserved", 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
|
||||
#include <sys/sysinfo.h>
|
||||
|
||||
void ffDetectSwap(FFMemoryStorage* swap)
|
||||
const char* ffDetectSwap(FFSwapResult* swap)
|
||||
{
|
||||
struct sysinfo info;
|
||||
if(sysinfo(&info) != 0)
|
||||
ffStrbufAppendS(&swap->error, "sysinfo() failed");
|
||||
return "sysinfo() failed";
|
||||
|
||||
swap->bytesTotal = info.totalswap * (uint64_t) info.mem_unit;
|
||||
swap->bytesUsed = (info.totalswap - info.freeswap) * (uint64_t) info.mem_unit;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <ntstatus.h>
|
||||
#include <windows.h>
|
||||
|
||||
void ffDetectSwap(FFMemoryStorage* swap)
|
||||
const char* ffDetectSwap(FFSwapResult* swap)
|
||||
{
|
||||
SYSTEM_INFO sysInfo;
|
||||
GetNativeSystemInfo(&sysInfo);
|
||||
@@ -18,16 +18,10 @@ void ffDetectSwap(FFMemoryStorage* swap)
|
||||
if(status == STATUS_INFO_LENGTH_MISMATCH)
|
||||
{
|
||||
if(!(pstart = (SYSTEM_PAGEFILE_INFORMATION*)realloc(pstart, size)))
|
||||
{
|
||||
ffStrbufAppendF(&swap->error, "realloc(pstart, %lu) failed", size);
|
||||
return;
|
||||
}
|
||||
return "realloc(pstart, size) failed";
|
||||
}
|
||||
else if(!NT_SUCCESS(status))
|
||||
{
|
||||
ffStrbufAppendF(&swap->error, "NtQuerySystemInformation(SystemPagefileInformation, %lu) failed", size);
|
||||
return;
|
||||
}
|
||||
return "NtQuerySystemInformation(SystemPagefileInformation, size) failed";
|
||||
break;
|
||||
}
|
||||
swap->bytesUsed = (uint64_t)pstart->TotalUsed * sysInfo.dwPageSize;
|
||||
|
||||
+1
-1
@@ -326,7 +326,7 @@ void ffPrintCPU(FFinstance* instance);
|
||||
void ffPrintCPUUsage(FFinstance* instance);
|
||||
void ffPrintGPU(FFinstance* instance);
|
||||
void ffPrintMemory(FFinstance* instance);
|
||||
void ffPrintSwap(FFinstance* instance); //Also in modules/memory.c
|
||||
void ffPrintSwap(FFinstance* instance);
|
||||
void ffPrintDisk(FFinstance* instance);
|
||||
void ffPrintBattery(FFinstance* instance);
|
||||
void ffPrintPowerAdapter(FFinstance* instance);
|
||||
|
||||
+17
-45
@@ -3,47 +3,41 @@
|
||||
#include "common/parsing.h"
|
||||
#include "common/bar.h"
|
||||
#include "detection/memory/memory.h"
|
||||
#include "detection/swap/swap.h"
|
||||
|
||||
#define FF_MEMORY_MODULE_NAME "Memory"
|
||||
#define FF_SWAP_MODULE_NAME "Swap"
|
||||
|
||||
#define FF_MEMORY_NUM_FORMAT_ARGS 3
|
||||
|
||||
static uint8_t calculatePercentage(const FFMemoryStorage* storage)
|
||||
void ffPrintMemory(FFinstance* instance)
|
||||
{
|
||||
if(storage->error.length != 0 || storage->bytesTotal == 0)
|
||||
return 0;
|
||||
else
|
||||
return (uint8_t) (((long double) storage->bytesUsed / (long double) storage->bytesTotal) * 100.0);
|
||||
}
|
||||
FFMemoryResult storage;
|
||||
const char* error = ffDetectMemory(&storage);
|
||||
|
||||
static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage)
|
||||
{
|
||||
if(storage->error.length > 0)
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, name, 0, moduleArgs, "%s", storage->error.chars);
|
||||
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
FFstrbuf usedPretty;
|
||||
FF_STRBUF_AUTO_DESTROY usedPretty;
|
||||
ffStrbufInit(&usedPretty);
|
||||
ffParseSize(storage->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
|
||||
ffParseSize(storage.bytesUsed, instance->config.binaryPrefixType, &usedPretty);
|
||||
|
||||
FFstrbuf totalPretty;
|
||||
FF_STRBUF_AUTO_DESTROY totalPretty;
|
||||
ffStrbufInit(&totalPretty);
|
||||
ffParseSize(storage->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
|
||||
ffParseSize(storage.bytesTotal, instance->config.binaryPrefixType, &totalPretty);
|
||||
|
||||
uint8_t percentage = calculatePercentage(storage);
|
||||
uint8_t percentage = storage.bytesTotal == 0
|
||||
? 0
|
||||
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
|
||||
|
||||
if(moduleArgs->outputFormat.length == 0)
|
||||
if(instance->config.memory.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, name, 0, &moduleArgs->key);
|
||||
if (storage->bytesTotal == 0)
|
||||
ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key);
|
||||
if (storage.bytesTotal == 0)
|
||||
puts("Disabled");
|
||||
else
|
||||
{
|
||||
FFstrbuf str;
|
||||
FF_STRBUF_AUTO_DESTROY str;
|
||||
ffStrbufInit(&str);
|
||||
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
@@ -60,36 +54,14 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr
|
||||
|
||||
ffStrbufTrimRight(&str, ' ');
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
ffStrbufDestroy(&str);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, name, 0, moduleArgs, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
ffPrintFormat(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
|
||||
});
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&usedPretty);
|
||||
ffStrbufDestroy(&totalPretty);
|
||||
}
|
||||
|
||||
void ffPrintMemory(FFinstance* instance)
|
||||
{
|
||||
FFMemoryStorage result;
|
||||
ffStrbufInit(&result.error);
|
||||
ffDetectMemory(&result);
|
||||
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &result);
|
||||
ffStrbufDestroy(&result.error);
|
||||
}
|
||||
|
||||
void ffPrintSwap(FFinstance* instance)
|
||||
{
|
||||
FFMemoryStorage result;
|
||||
ffStrbufInit(&result.error);
|
||||
ffDetectSwap(&result);
|
||||
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &result);
|
||||
ffStrbufDestroy(&result.error);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/parsing.h"
|
||||
#include "common/bar.h"
|
||||
#include "detection/swap/swap.h"
|
||||
|
||||
#define FF_SWAP_MODULE_NAME "Swap"
|
||||
#define FF_SWAP_NUM_FORMAT_ARGS 3
|
||||
|
||||
void ffPrintSwap(FFinstance* instance)
|
||||
{
|
||||
FFSwapResult storage;
|
||||
const char* error = ffDetectSwap(&storage);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY usedPretty;
|
||||
ffStrbufInit(&usedPretty);
|
||||
ffParseSize(storage.bytesUsed, instance->config.binaryPrefixType, &usedPretty);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY totalPretty;
|
||||
ffStrbufInit(&totalPretty);
|
||||
ffParseSize(storage.bytesTotal, instance->config.binaryPrefixType, &totalPretty);
|
||||
|
||||
uint8_t percentage = storage.bytesTotal == 0
|
||||
? 0
|
||||
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
|
||||
|
||||
if(instance->config.swap.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap.key);
|
||||
if (storage.bytesTotal == 0)
|
||||
puts("Disabled");
|
||||
else
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY str;
|
||||
ffStrbufInit(&str);
|
||||
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(instance, &str, percentage, 0, 5, 8);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
|
||||
if(!(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
||||
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
||||
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
ffAppendPercentNum(instance, &str, (uint8_t) percentage, 50, 80, str.length > 0);
|
||||
|
||||
ffStrbufTrimRight(&str, ' ');
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap, FF_SWAP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user