From a9c5c2892c0cda2b36c5c1a88d5c766e00c91e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 7 Dec 2023 16:30:10 +0800 Subject: [PATCH 1/4] Memory (Linux): Use `MemAvailable` if available --- src/detection/memory/memory_linux.c | 42 ++++++++++++++++++----------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/detection/memory/memory_linux.c b/src/detection/memory/memory_linux.c index 3da3e7930..6f9e87a0f 100644 --- a/src/detection/memory/memory_linux.c +++ b/src/detection/memory/memory_linux.c @@ -13,33 +13,43 @@ const char* ffDetectMemory(FFMemoryResult* ram) buf[nRead] = '\0'; uint64_t memTotal = 0, + memAvailable = 0, shmem = 0, memFree = 0, buffers = 0, cached = 0, sReclaimable = 0; - + char *token = NULL; if((token = strstr(buf, "MemTotal:")) != NULL) memTotal = strtoul(token + strlen("MemTotal:"), NULL, 10); + else + return "MemTotal not found in /proc/meminfo"; + + if((token = strstr(buf, "MemAvailable:")) != NULL) + memAvailable = strtoul(token + strlen("MemAvailable:"), NULL, 10); + else + { + if((token = strstr(buf, "MemFree:")) != NULL) + memFree = strtoul(token + strlen("MemFree:"), NULL, 10); + + if((token = strstr(buf, "Buffers:")) != NULL) + buffers = strtoul(token + strlen("Buffers:"), NULL, 10); + + if((token = strstr(buf, "Cached:")) != NULL) + cached = strtoul(token + strlen("Cached:"), NULL, 10); + + if((token = strstr(buf, "Shmem:")) != NULL) + shmem = strtoul(token + strlen("Shmem:"), NULL, 10); + + if((token = strstr(buf, "SReclaimable:")) != NULL) + sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10); - if((token = strstr(buf, "MemFree:")) != NULL) - memFree = strtoul(token + strlen("MemFree:"), NULL, 10); - - if((token = strstr(buf, "Buffers:")) != NULL) - buffers = strtoul(token + strlen("Buffers:"), NULL, 10); - - if((token = strstr(buf, "Cached:")) != NULL) - cached = strtoul(token + strlen("Cached:"), NULL, 10); - - if((token = strstr(buf, "Shmem:")) != NULL) - shmem = strtoul(token + strlen("Shmem:"), NULL, 10); - - if((token = strstr(buf, "SReclaimable:")) != NULL) - sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10); + memAvailable = memFree + buffers + cached + sReclaimable - shmem; + } ram->bytesTotal = memTotal * 1024lu; - ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * 1024lu; + ram->bytesUsed = (memTotal - memAvailable) * 1024lu; return NULL; } From 1504e26e38d5e9a3257f2308e50174a398a27f35 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Wed, 6 Dec 2023 19:06:44 +0900 Subject: [PATCH 2/4] optimize(Packages): optimize reading large files Using less syscalls and processing data in memory makes getNumStringsImpl ~300% faster. The code is much simpler. --- src/detection/packages/packages_linux.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/detection/packages/packages_linux.c b/src/detection/packages/packages_linux.c index 9351b420f..ef5d1162d 100644 --- a/src/detection/packages/packages_linux.c +++ b/src/detection/packages/packages_linux.c @@ -42,26 +42,19 @@ static uint32_t getNumElements(FFstrbuf* baseDir, const char* dirname, unsigned static uint32_t getNumStringsImpl(const char* filename, const char* needle) { - FILE* file = fopen(filename, "r"); - if(file == NULL) + FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate(); + if (!ffReadFileBuffer(filename, &content)) return 0; uint32_t count = 0; - - char* line = NULL; - size_t len = 0; - - while(getline(&line, &len, file) != EOF) + char *iter = content.chars; + size_t needleLength = strlen(needle); + while ((iter = memmem(iter, content.length - (size_t)(iter - content.chars), needle, needleLength)) != NULL) { - if(strstr(line, needle) != NULL) - ++count; + ++count; + iter += needleLength; } - if(line != NULL) - free(line); - - fclose(file); - return count; } From 91e4e94aaaca5ff15781aed28267a24cb1cb458f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 7 Dec 2023 23:51:56 +0800 Subject: [PATCH 3/4] CMake: fix `--help` doesn't work when building without python3 --- CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 20a65e01f..e4dc90511 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -221,12 +221,15 @@ endfunction(fastfetch_load_text) find_package(Python) if(Python_FOUND) - # Minify JSON string. `io.open(0,encoding='utf-8')` is needed to avoid encoding issues on Windows execute_process(COMMAND ${Python_EXECUTABLE} -c "import json,sys;json.dump(json.load(sys.stdin),sys.stdout,separators=(',',':'))" INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/src/data/help.json" OUTPUT_VARIABLE DATATEXT_JSON_HELP) + if(DATATEXT_JSON_HELP STREQUAL "") + message(ERROR "DATATEXT_JSON_HELP is empty, which should not happen!") + endif() else() - file(READ "src/data/help.json" TEMP) + message(STATUS "Python3 is not found, 'help.json' will not be minified") + file(READ "src/data/help.json" DATATEXT_JSON_HELP) endif() fastfetch_encode_c_string("${DATATEXT_JSON_HELP}" DATATEXT_JSON_HELP) From dcbda6c0d50fdd7cce75ca07703fa06ee3ed0f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 8 Dec 2023 00:04:38 +0800 Subject: [PATCH 4/4] Release: v2.3.4 --- CHANGELOG.md | 9 +++++++++ CMakeLists.txt | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03212cd25..ff02b1cac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# 2.3.4 + +Bugfixes: +* Fix `--help` doesn't work when built without python + +Features: +* Use `MemAvailable` if available (Memory, Linux) +* Improve performance of detecting dpkg package count (Packages, Linux) + # 2.3.3 Bugfixes: diff --git a/CMakeLists.txt b/CMakeLists.txt index e4dc90511..12807993e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.12.0) # target_link_libraries with OBJECT libs & project homepage url project(fastfetch - VERSION 2.3.3 + VERSION 2.3.4 LANGUAGES C DESCRIPTION "Fast neofetch-like system information tool" HOMEPAGE_URL "https://github.com/fastfetch-cli/fastfetch"