Skip to content

Commit 78697c5

Browse files
committed
refactor: 使用 jemalloc-ctl 获取堆内存用
1 parent c1f6e9f commit 78697c5

6 files changed

Lines changed: 59 additions & 27 deletions

File tree

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ replay_pid*
2828

2929
*.iml
3030
.gradle
31-
/local.properties
32-
/.idea/*
31+
.idea
3332
.DS_Store
34-
/build
33+
build
3534
/captures
3635
.externalNativeBuild
3736
.cxx
37+
3838
local.properties
3939
signing.properties

.vscode/settings.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
{
2+
"rust-analyzer.linkedProjects": [
3+
"uniffi/Cargo.toml"
4+
],
25
"rust-analyzer.cargo.target": "aarch64-linux-android",
36
"rust-analyzer.check.allTargets": false,
4-
"rust-analyzer.checkOnSave.allTargets": false,
7+
// use `cargo ndk-env --json` to generate the below environment variables
58
"rust-analyzer.server.extraEnv": {
6-
"CARGO_BUILD_TARGET": "aarch64-linux-android"
9+
"CARGO_BUILD_TARGET": "aarch64-linux-android",
10+
"AR_aarch64-linux-android": "C:\\Workspace\\AndroidSDK\\ndk\\29.0.14206865\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\llvm-ar",
11+
"CARGO_NDK_SYSROOT_LIBS_PATH": "C:\\Workspace\\AndroidSDK\\ndk\\29.0.14206865\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\aarch64-linux-android",
12+
"CARGO_NDK_SYSROOT_PATH": "C:\\Workspace\\AndroidSDK\\ndk\\29.0.14206865\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot",
13+
"CARGO_NDK_SYSROOT_TARGET": "aarch64-linux-android",
14+
"CARGO_TARGET_AARCH64_LINUX_ANDROID_AR": "C:\\Workspace\\AndroidSDK\\ndk\\29.0.14206865\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\llvm-ar",
15+
"CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER": "\\\\?\\C:\\Users\\iHsin\\.cargo\\bin\\cargo-ndk",
16+
"CC_aarch64-linux-android": "C:\\Workspace\\AndroidSDK\\ndk\\29.0.14206865\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\clang",
17+
"CFLAGS_aarch64-linux-android": "--target=aarch64-linux-android21 --target=aarch64-linux-android21",
18+
"CXXFLAGS_aarch64-linux-android": "--target=aarch64-linux-android21 --target=aarch64-linux-android21",
19+
"CXX_aarch64-linux-android": "C:\\Workspace\\AndroidSDK\\ndk\\29.0.14206865\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\clang++",
20+
"RANLIB_aarch64-linux-android": "C:\\Workspace\\AndroidSDK\\ndk\\29.0.14206865\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\llvm-ranlib"
721
}
8-
}
22+
}

uniffi/.vscode/settings.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

uniffi/Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

uniffi/clash-android-ffi/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ edition.workspace = true
99
crate-type = ["cdylib"]
1010

1111
[features]
12+
default = []
1213
ffi-trace = ["uniffi/ffi-trace"]
13-
jemallocator = ["dep:tikv-jemallocator"]
14+
jemallocator = ["dep:tikv-jemallocator", "dep:jemalloc-ctl"]
1415

1516
[dependencies]
1617
uniffi.workspace = true
@@ -48,6 +49,7 @@ reqwest = { version = "0.13", default-features = false, features = ["rustls", "s
4849
tokio-stream = "0.1"
4950
# Memory allocator
5051
tikv-jemallocator = { version = "0.6", optional = true }
52+
jemalloc-ctl = { package = "tikv-jemalloc-ctl", optional = true, version = "0.6.1" }
5153
rustls-platform-verifier = "0.6.2" # sync with gradle/libs.versions.toml
5254

5355
[dependencies.clash-lib]

uniffi/clash-android-ffi/src/controller.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,30 @@ impl ClashController {
202202

203203
/// Get memory statistics
204204
pub async fn get_memory(&self) -> Result<MemoryResponse, EyreError> {
205-
self.request("GET", "/memory", None).await
205+
#[cfg(feature = "jemallocator")]
206+
{
207+
use jemalloc_ctl::{epoch, stats};
208+
// Advance the epoch to update statistics
209+
epoch::advance().context("Failed to advance jemalloc epoch")?;
210+
211+
// Read allocated memory in bytes
212+
let allocated = stats::allocated::read()
213+
.context("Failed to read allocated memory")?;
214+
215+
// Read resident memory in bytes
216+
let resident = stats::resident::read()
217+
.context("Failed to read resident memory")?;
218+
219+
Ok(MemoryResponse {
220+
inuse: allocated as i64,
221+
oslimit: resident as i64,
222+
})
223+
}
224+
225+
#[cfg(not(feature = "jemallocator"))]
226+
{
227+
self.request("GET", "/memory", None).await
228+
}
206229
}
207230

208231
/// Get active connections

0 commit comments

Comments
 (0)