Skip to content

Commit 9a77956

Browse files
feat: memory detection
1 parent aa6c05e commit 9a77956

39 files changed

Lines changed: 1020 additions & 783 deletions

base/CMakeLists.txt

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,38 @@
11
# Base library - fundamental utilities and platform abstractions
22
cmake_minimum_required(VERSION 3.16)
33

4-
# Create interface library for headers-only components
5-
add_library(cfbase_interface INTERFACE)
4+
# Include subdirectory CMakeLists
5+
add_subdirectory(system/cpu)
6+
add_subdirectory(system/memory)
67

7-
# Target for CPU info module
8-
add_library(cfbase_cpu STATIC
9-
system/cpu/cfcpu.cpp
10-
system/cpu/cfcpu_profile.cpp
11-
system/cpu/cfcpu_bonus.cpp
12-
)
8+
# ============================================================
9+
# Unified Base Library
10+
# ============================================================
1311

14-
target_include_directories(cfbase_cpu
15-
PUBLIC
16-
${CMAKE_CURRENT_SOURCE_DIR}/include
17-
PRIVATE
18-
${CMAKE_CURRENT_SOURCE_DIR}/system
19-
)
12+
# Create interface library that merges all sub-libraries
13+
add_library(cfbase INTERFACE)
2014

21-
target_link_libraries(cfbase_cpu
22-
PUBLIC
23-
Qt6::Core
15+
# Link all CPU and memory modules
16+
target_link_libraries(cfbase INTERFACE
17+
cfbase_cpu_info
18+
cfbase_cpu_profile
19+
cfbase_cpu_bonus
20+
cfbase_cpu_features
21+
cfbase_memory_info
22+
Qt6::Core
2423
)
2524

26-
# Platform-specific sources
25+
# Platform-specific link for unified library
2726
if(WIN32)
28-
# Windows specific implementations
29-
target_sources(cfbase_cpu PRIVATE
30-
system/cpu/private/win_impl/cpu_profile.cpp
31-
system/cpu/private/win_impl/cpu_bonus.cpp
32-
system/cpu/private/win_impl/cpu_features.cpp
33-
system/cpu/private/win_impl/cpu_info.cpp
34-
)
35-
target_include_directories(cfbase_cpu PRIVATE
36-
${CMAKE_CURRENT_SOURCE_DIR}/base/windows
37-
)
38-
# Link Windows WMI libraries for CPU info
39-
target_link_libraries(cfbase_cpu PUBLIC wbemuuid PowrProf pdh)
40-
elseif(UNIX)
41-
# Linux/Unix specific implementations
42-
target_sources(cfbase_cpu PRIVATE
43-
system/cpu/private/linux_impl/cpu_profile.cpp
44-
system/cpu/private/linux_impl/cpu_bonus.cpp
45-
system/cpu/private/linux_impl/cpu_features.cpp
46-
system/cpu/private/linux_impl/cpu_info.cpp
47-
utils/linux/proc_parser.cpp
27+
target_link_libraries(cfbase INTERFACE
28+
wbemuuid
29+
PowrProf
30+
pdh
31+
psapi
4832
)
4933
endif()
5034

51-
# Alias for convenience
52-
add_library(CFDesktop::base_cpu ALIAS cfbase_cpu)
53-
add_library(CFDesktop::base_interface ALIAS cfbase_interface)
54-
55-
# Group sources for IDEs
56-
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "base" FILES
57-
system/cpu/cfcpu.cpp
58-
system/cpu/private/win_impl/cpu.cpp
59-
base/windows/co_helper.cpp
60-
)
35+
# ============================================================
36+
# Aliases
37+
# ============================================================
38+
add_library(CFDesktop::base ALIAS cfbase)

base/include/base/macro/system_judge.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@
1212
* @ingroup base_macros
1313
*/
1414
#pragma once
15-
#include <QtGlobal>
1615

17-
#ifdef Q_OS_WIN
16+
/* ==================== Operating System Detection ==================== */
17+
18+
// Windows platform detection
19+
#if defined(_WIN32) || defined(_WIN64)
1820
# define CFDESKTOP_OS_WINDOWS
1921
#endif
2022

2123
// Linux platform detection
22-
#ifdef Q_OS_LINUX
24+
#if defined(__linux__)
2325
# define CFDESKTOP_OS_LINUX
2426
#endif
2527

26-
#ifdef Q_PROCESSOR_X86_64
28+
/* ==================== Architecture Detection ==================== */
29+
30+
// x86_64 (AMD64) detection
31+
#if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__)
2732
# define CFDESKTOP_ARCH_X86_64
2833
#endif
29-
#ifdef Q_PROCESSOR_ARM_64
34+
35+
// ARM64 detection
36+
#if defined(__aarch64__) || defined(_M_ARM64)
3037
# define CFDESKTOP_ARCH_ARM64
3138
#endif
3239

33-
#ifdef Q_PROCESSOR_ARM_32
40+
// ARM32 detection
41+
#if defined(__arm__) || defined(_M_ARM)
3442
# define CFDESKTOP_ARCH_ARM32
3543
#endif
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @file memory_info.h
3+
* @author Charliechen114514 (chengh1922@mails.jlu.edu.cn)
4+
* @brief Memory Detections Using for System Detection
5+
* @version 0.1
6+
* @date 2026-02-23
7+
*
8+
* @copyright Copyright (c) 2026
9+
*
10+
*/
11+
#pragma once
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
namespace cf {
17+
18+
/**
19+
* @brief Memory type enumeration.
20+
*/
21+
enum class MemoryType : uint8_t {
22+
UNKNOWN = 0,
23+
DDR3,
24+
DDR4,
25+
DDR5,
26+
LPDDR3,
27+
LPDDR4,
28+
LPDDR4X,
29+
LPDDR5,
30+
DDR2,
31+
SDRAM
32+
};
33+
34+
/**
35+
* @brief DIMM (Memory Module) information.
36+
*/
37+
struct DimmInfo {
38+
uint64_t capacity_bytes; ///< Capacity in bytes.
39+
MemoryType type; ///< Memory type (DDR3/DDR4/LPDDR4, etc.).
40+
uint32_t frequency_mhz; ///< Operating frequency in MHz.
41+
std::string manufacturer; ///< Manufacturer name (optional, empty if unavailable).
42+
std::string part_number; ///< Part number (optional, empty if unavailable).
43+
std::string serial_number; ///< Serial number (optional, empty if unavailable).
44+
uint8_t channel; ///< Memory channel number (0-based).
45+
uint8_t slot; ///< Slot number on the channel (0-based).
46+
};
47+
48+
/**
49+
* @brief Physical memory information.
50+
*/
51+
struct PhysicalMemory {
52+
uint64_t total_bytes; ///< Total physical memory in bytes.
53+
uint64_t available_bytes; ///< Available physical memory in bytes.
54+
uint64_t free_bytes; ///< Free physical memory in bytes.
55+
};
56+
57+
/**
58+
* @brief Virtual memory / Swap space information.
59+
*/
60+
struct SwapMemory {
61+
uint64_t total_bytes; ///< Total swap space in bytes.
62+
uint64_t free_bytes; ///< Free swap space in bytes.
63+
};
64+
65+
/**
66+
* @brief Linux-specific cached memory information.
67+
*/
68+
struct CachedMemory {
69+
uint64_t buffers_bytes; ///< Memory used for buffers (in bytes).
70+
uint64_t cached_bytes; ///< Memory used for page cache (in bytes).
71+
uint64_t shared_bytes; ///< Memory used for shared memory (Shmem, in bytes).
72+
uint64_t slab_bytes; ///< Memory used for kernel data structures (in bytes).
73+
};
74+
75+
/**
76+
* @brief Process memory usage information.
77+
*/
78+
struct ProcessMemory {
79+
uint64_t vm_rss_bytes; ///< Resident Set Size - actual physical memory used.
80+
uint64_t vm_size_bytes; ///< Virtual memory size (total virtual memory allocated).
81+
uint64_t vm_peak_bytes; ///< Peak virtual memory size.
82+
};
83+
84+
/**
85+
* @brief Comprehensive memory information for system detection.
86+
*/
87+
struct MemoryInfo {
88+
PhysicalMemory physical; ///< Physical memory statistics.
89+
SwapMemory swap; ///< Swap space statistics.
90+
CachedMemory cached; ///< Cached/buffered memory (mainly Linux).
91+
ProcessMemory process; ///< Current process memory usage.
92+
std::vector<DimmInfo> dimms; ///< List of memory modules (DIMM info).
93+
};
94+
95+
void getSystemMemoryInfo(MemoryInfo& info);
96+
97+
} // namespace cf

base/system/cpu/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# CPU Module Library
2+
cmake_minimum_required(VERSION 3.16)
3+
4+
add_library(cfbase_cpu STATIC)
5+
target_include_directories(cfbase_cpu
6+
PUBLIC
7+
${CMAKE_CURRENT_SOURCE_DIR}/../../include
8+
PRIVATE
9+
${CMAKE_CURRENT_SOURCE_DIR}/../..
10+
)
11+
target_sources(cfbase_cpu PRIVATE
12+
cfcpu.cpp
13+
cfcpu_profile.cpp
14+
cfcpu_bonus.cpp
15+
)
16+
17+
if(WIN32)
18+
target_sources(cfbase_cpu PRIVATE
19+
private/win_impl/cpu_profile.cpp
20+
private/win_impl/cpu_bonus.cpp
21+
private/win_impl/cpu_features.cpp
22+
private/win_impl/cpu_info.cpp
23+
)
24+
target_include_directories(cfbase_cpu PRIVATE
25+
${CMAKE_CURRENT_SOURCE_DIR}/private
26+
)
27+
target_link_libraries(cfbase_cpu PUBLIC wbemuuid PowrProf pdh)
28+
elseif(UNIX)
29+
target_sources(cfbase_cpu PRIVATE
30+
private/linux_impl/cpu_profile.cpp
31+
private/linux_impl/cpu_bonus.cpp
32+
private/linux_impl/cpu_features.cpp
33+
private/linux_impl/cpu_info.cpp
34+
../../utils/linux/proc_parser.cpp
35+
)
36+
endif()
37+
38+
add_library(CFDesktop::base_cpu ALIAS cfbase_cpu)

base/system/cpu/private/win_impl/cpu_bonus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "cpu_bonus.h"
2-
#include "cpu/private/cpu_host.h"
2+
#include "cpu_host.h"
33
#include "cpu_features.h"
44
#include <windows.h>
55

base/system/cpu/private/win_impl/cpu_info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111
#include "base/scope_guard/scope_guard.hpp"
1212
#include "base/windows/co_helper.hpp"
13-
#include "cpu/private/cpu_host.h"
1413
#include "system/cpu/cfcpu.h"
14+
#include "system/cpu/private/cpu_host.h"
1515
#include <Wbemidl.h>
1616
#include <Windows.h>
1717
#include <comdef.h>

base/system/cpu/private/win_impl/cpu_profile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ float getCpuUsage() {
3535
PDH_HCOUNTER counter;
3636

3737
PdhOpenQuery(nullptr, 0, &query);
38-
PdhAddEnglishCounter(query, L"\\Processor(_Total)\\% Processor Time", 0, &counter);
38+
PdhAddEnglishCounterW(query, L"\\Processor(_Total)\\% Processor Time", 0, &counter);
3939
PdhCollectQueryData(query);
4040

4141
Sleep(100);

base/system/memory/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Memory Module Library
2+
cmake_minimum_required(VERSION 3.16)
3+
4+
add_library(cfbase_memory STATIC)
5+
target_include_directories(cfbase_memory
6+
PUBLIC
7+
${CMAKE_CURRENT_SOURCE_DIR}/../../include
8+
PRIVATE
9+
${CMAKE_CURRENT_SOURCE_DIR}/../..
10+
)
11+
target_sources(cfbase_memory PRIVATE
12+
memory_info.cpp
13+
)
14+
15+
if(WIN32)
16+
target_sources(cfbase_memory PRIVATE
17+
private/win_impl/memory_info.cpp
18+
private/win_impl/physical_memory.cpp
19+
private/win_impl/swap_memory.cpp
20+
private/win_impl/process_memory.cpp
21+
private/win_impl/dimm_info.cpp
22+
)
23+
target_link_libraries(cfbase_memory PUBLIC psapi)
24+
elseif(UNIX)
25+
target_sources(cfbase_memory PRIVATE
26+
private/linux_impl/memory_info.cpp
27+
)
28+
endif()
29+
30+
# Link Qt Core for QtGlobal header
31+
find_package(Qt6 COMPONENTS Core REQUIRED)
32+
target_link_libraries(cfbase_memory PRIVATE Qt6::Core)
33+
34+
add_library(CFDesktop::base_memory ALIAS cfbase_memory)

base/system/memory/memory_info.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "system/memory/memory_info.h"
2+
#include "base/macro/system_judge.h"
3+
4+
#ifdef CFDESKTOP_OS_WINDOWS
5+
# include "private/win_impl/memory_info.h"
6+
#elif defined(CFDESKTOP_OS_LINUX)
7+
# include "private/linux_impl/memory_info.h"
8+
#endif
9+
10+
namespace cf {
11+
12+
void getSystemMemoryInfo(MemoryInfo& info) {
13+
#ifdef CFDESKTOP_OS_WINDOWS
14+
win_impl::getSystemMemoryInfo(info);
15+
#elif defined(CFDESKTOP_OS_LINUX)
16+
# error "Waiting Implemented..."
17+
#endif
18+
}
19+
20+
} // namespace cf

base/system/memory/private/linux_impl/linux_impl.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)