Skip to content

Commit a51a3fb

Browse files
linux/memory OK
1 parent fab38d6 commit a51a3fb

20 files changed

Lines changed: 956 additions & 15 deletions

base/system/cpu/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
cmake_minimum_required(VERSION 3.16)
33

44
add_library(cfbase_cpu OBJECT)
5+
set_target_properties(cfbase_cpu PROPERTIES
6+
POSITION_INDEPENDENT_CODE ON
7+
)
58
target_include_directories(cfbase_cpu
69
PUBLIC
710
${CMAKE_CURRENT_SOURCE_DIR}/../../include

base/system/memory/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
cmake_minimum_required(VERSION 3.16)
33

44
add_library(cfbase_memory OBJECT)
5+
set_target_properties(cfbase_memory PROPERTIES
6+
POSITION_INDEPENDENT_CODE ON
7+
)
58
target_include_directories(cfbase_memory
69
PUBLIC
710
${CMAKE_CURRENT_SOURCE_DIR}/../../include
@@ -23,5 +26,10 @@ if(WIN32)
2326
elseif(UNIX)
2427
target_sources(cfbase_memory PRIVATE
2528
private/linux_impl/memory_info.cpp
29+
private/linux_impl/physical_memory.cpp
30+
private/linux_impl/swap_memory.cpp
31+
private/linux_impl/process_memory.cpp
32+
private/linux_impl/cached_memory.cpp
33+
private/linux_impl/dimm_info.cpp
2634
)
2735
endif()

base/system/memory/memory_info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void getSystemMemoryInfo(MemoryInfo& info) {
1313
#ifdef CFDESKTOP_OS_WINDOWS
1414
win_impl::getSystemMemoryInfo(info);
1515
#elif defined(CFDESKTOP_OS_LINUX)
16-
# error "Waiting Implemented..."
16+
linux_impl::getSystemMemoryInfo(info);
1717
#endif
1818
}
1919

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @file cached_memory.cpp
3+
* @author Charliechen114514 (chengh1922@mails.jlu.edu.cn)
4+
* @brief Linux-specific Cached Memory Query Implementation
5+
* @version 0.1
6+
* @date 2026-02-27
7+
*
8+
* @copyright Copyright (c) 2026
9+
*
10+
*/
11+
#include "cached_memory.h"
12+
13+
#include <cstdio>
14+
#include <cstdint>
15+
#include <cstring>
16+
17+
namespace cf {
18+
namespace linux_impl {
19+
20+
namespace {
21+
22+
bool parseMemInfoLine(const char* line, const char* fieldName, uint64_t& outKb) {
23+
size_t fieldNameLen = strlen(fieldName);
24+
if (strncmp(line, fieldName, fieldNameLen) != 0) {
25+
return false;
26+
}
27+
28+
const char* p = line + fieldNameLen;
29+
while (*p == ':' || *p == ' ' || *p == '\t') {
30+
p++;
31+
}
32+
33+
if (*p == '\0') {
34+
return false;
35+
}
36+
37+
char* end;
38+
unsigned long value = strtoul(p, &end, 10);
39+
if (end == p) {
40+
return false;
41+
}
42+
43+
outKb = static_cast<uint64_t>(value);
44+
return true;
45+
}
46+
47+
} // anonymous namespace
48+
49+
void queryCachedMemory(CachedMemory& cached) {
50+
FILE* fp = fopen("/proc/meminfo", "r");
51+
if (!fp) {
52+
cached.buffers_bytes = 0;
53+
cached.cached_bytes = 0;
54+
cached.shared_bytes = 0;
55+
cached.slab_bytes = 0;
56+
return;
57+
}
58+
59+
uint64_t buffers = 0;
60+
uint64_t cachedMem = 0;
61+
uint64_t shmem = 0;
62+
uint64_t slab = 0;
63+
64+
char line[256];
65+
while (fgets(line, sizeof(line), fp)) {
66+
uint64_t value;
67+
if (parseMemInfoLine(line, "Buffers", value)) {
68+
buffers = value;
69+
} else if (parseMemInfoLine(line, "Cached", value)) {
70+
// Note: There may also be "SReclaimable:" which is similar to Cached
71+
// For simplicity, we only get the main Cached value
72+
cachedMem = value;
73+
} else if (parseMemInfoLine(line, "Shmem", value)) {
74+
shmem = value;
75+
} else if (parseMemInfoLine(line, "Slab", value)) {
76+
slab = value;
77+
}
78+
79+
if (buffers > 0 && cachedMem > 0 && shmem > 0 && slab > 0) {
80+
break;
81+
}
82+
}
83+
84+
fclose(fp);
85+
86+
// Convert KB to bytes
87+
cached.buffers_bytes = buffers * 1024;
88+
cached.cached_bytes = cachedMem * 1024;
89+
cached.shared_bytes = shmem * 1024;
90+
cached.slab_bytes = slab * 1024;
91+
}
92+
93+
} // namespace linux_impl
94+
} // namespace cf
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @file cached_memory.h
3+
* @author Charliechen114514 (chengh1922@mails.jlu.edu.cn)
4+
* @brief Linux-specific Cached Memory Query
5+
* @version 0.1
6+
* @date 2026-02-27
7+
*
8+
* @copyright Copyright (c) 2026
9+
*
10+
*/
11+
#pragma once
12+
13+
#include "system/memory/memory_info.h"
14+
15+
namespace cf {
16+
namespace linux_impl {
17+
18+
/**
19+
* @brief Query Linux-specific cached memory information using /proc/meminfo.
20+
*
21+
* @param[out] cached Output parameter for cached memory statistics.
22+
*
23+
* @throws None.
24+
*
25+
* @note Reads /proc/meminfo to get Buffers, Cached, Shmem, and Slab.
26+
*
27+
* @warning None.
28+
*
29+
* @since 0.1
30+
* @ingroup system_memory
31+
*/
32+
void queryCachedMemory(CachedMemory& cached);
33+
34+
} // namespace linux_impl
35+
} // namespace cf

0 commit comments

Comments
 (0)