Skip to content

Commit 2ad8220

Browse files
bors[bot]matklad
andcommitted
Merge #1462
1462: Move memory usage statistics to ra_prof r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents bb70d18 + 18a1e09 commit 2ad8220

File tree

9 files changed

+78
-68
lines changed

9 files changed

+78
-68
lines changed

Cargo.lock

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

crates/ra_cli/src/analysis_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ pub fn run(verbose: bool, path: &Path, only: Option<&str>) -> Result<()> {
112112
num_exprs_partially_unknown,
113113
(num_exprs_partially_unknown * 100 / num_exprs)
114114
);
115-
println!("Analysis: {:?}", analysis_time.elapsed());
115+
println!("Analysis: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage());
116116
Ok(())
117117
}

crates/ra_ide_api/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ unicase = "2.2.0"
1616
superslice = "1.0.0"
1717
rand = "0.6.5"
1818

19-
jemallocator = { version = "0.1.9", optional = true }
20-
jemalloc-ctl = { version = "0.2.0", optional = true }
21-
2219
ra_syntax = { path = "../ra_syntax" }
2320
ra_text_edit = { path = "../ra_text_edit" }
2421
ra_db = { path = "../ra_db" }
@@ -36,6 +33,3 @@ version = "0.9.0"
3633
# Disable `fork` feature to allow compiling on webassembly
3734
default-features = false
3835
features = ["std", "bit-set", "break-dead-code"]
39-
40-
[features]
41-
jemalloc = [ "jemallocator", "jemalloc-ctl" ]

crates/ra_ide_api/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ pub use crate::{
7474
pub use ra_db::{Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, Edition};
7575
pub use hir::Documentation;
7676

77-
// We use jemalloc mainly to get heap usage statistics, actual performance
78-
// difference is not measures.
79-
#[cfg(feature = "jemalloc")]
80-
#[global_allocator]
81-
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
82-
8377
pub type Cancelable<T> = Result<T, Canceled>;
8478

8579
#[derive(Debug)]

crates/ra_ide_api/src/status.rs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ra_db::{
99
FileTextQuery, SourceRootId,
1010
salsa::{Database, debug::{DebugQueryTable, TableEntry}},
1111
};
12+
use ra_prof::{Bytes, memory_usage};
1213
use hir::MacroFile;
1314

1415
use crate::{
@@ -34,7 +35,7 @@ pub(crate) fn status(db: &RootDatabase) -> String {
3435
symbols_stats,
3536
syntax_tree_stats,
3637
macro_syntax_tree_stats,
37-
MemoryStats::current(),
38+
memory_usage(),
3839
db.last_gc.elapsed().as_secs(),
3940
)
4041
}
@@ -138,54 +139,3 @@ impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbols
138139
res
139140
}
140141
}
141-
142-
struct MemoryStats {
143-
allocated: Bytes,
144-
resident: Bytes,
145-
}
146-
147-
impl MemoryStats {
148-
#[cfg(feature = "jemalloc")]
149-
fn current() -> MemoryStats {
150-
jemalloc_ctl::epoch().unwrap();
151-
MemoryStats {
152-
allocated: Bytes(jemalloc_ctl::stats::allocated().unwrap()),
153-
resident: Bytes(jemalloc_ctl::stats::resident().unwrap()),
154-
}
155-
}
156-
157-
#[cfg(not(feature = "jemalloc"))]
158-
fn current() -> MemoryStats {
159-
MemoryStats { allocated: Bytes(0), resident: Bytes(0) }
160-
}
161-
}
162-
163-
impl fmt::Display for MemoryStats {
164-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
165-
write!(fmt, "{} allocated {} resident", self.allocated, self.resident,)
166-
}
167-
}
168-
169-
#[derive(Default)]
170-
struct Bytes(usize);
171-
172-
impl fmt::Display for Bytes {
173-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
174-
let bytes = self.0;
175-
if bytes < 4096 {
176-
return write!(f, "{} bytes", bytes);
177-
}
178-
let kb = bytes / 1024;
179-
if kb < 4096 {
180-
return write!(f, "{}kb", kb);
181-
}
182-
let mb = kb / 1024;
183-
write!(f, "{}mb", mb)
184-
}
185-
}
186-
187-
impl std::ops::AddAssign<usize> for Bytes {
188-
fn add_assign(&mut self, x: usize) {
189-
self.0 += x;
190-
}
191-
}

crates/ra_lsp_server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ tempfile = "3"
3232
test_utils = { path = "../test_utils" }
3333

3434
[features]
35-
jemalloc = [ "ra_ide_api/jemalloc" ]
35+
jemalloc = [ "ra_prof/jemalloc" ]

crates/ra_prof/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ once_cell = "0.2.0"
1010
itertools = "0.8.0"
1111
backtrace = "0.3.28"
1212
cpuprofiler = { version = "0.0.3", optional = true }
13+
jemallocator = { version = "0.1.9", optional = true }
14+
jemalloc-ctl = { version = "0.2.0", optional = true }
15+
16+
17+
[features]
18+
jemalloc = [ "jemallocator", "jemalloc-ctl" ]

crates/ra_prof/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod memory_usage;
2+
13
use std::{
24
cell::RefCell,
35
time::{Duration, Instant},
@@ -11,6 +13,14 @@ use std::{
1113
use once_cell::sync::Lazy;
1214
use itertools::Itertools;
1315

16+
pub use crate::memory_usage::{MemoryUsage, Bytes};
17+
18+
// We use jemalloc mainly to get heap usage statistics, actual performance
19+
// difference is not measures.
20+
#[cfg(feature = "jemalloc")]
21+
#[global_allocator]
22+
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
23+
1424
/// Set profiling filter. It specifies descriptions allowed to profile.
1525
/// This is helpful when call stack has too many nested profiling scopes.
1626
/// Additionally filter can specify maximum depth of profiling scopes nesting.
@@ -288,6 +298,10 @@ impl Drop for CpuProfiler {
288298
}
289299
}
290300

301+
pub fn memory_usage() -> MemoryUsage {
302+
MemoryUsage::current()
303+
}
304+
291305
#[cfg(test)]
292306
mod tests {
293307
use super::*;

crates/ra_prof/src/memory_usage.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::fmt;
2+
3+
pub struct MemoryUsage {
4+
pub allocated: Bytes,
5+
pub resident: Bytes,
6+
}
7+
8+
impl MemoryUsage {
9+
#[cfg(feature = "jemalloc")]
10+
pub fn current() -> MemoryUsage {
11+
jemalloc_ctl::epoch().unwrap();
12+
MemoryUsage {
13+
allocated: Bytes(jemalloc_ctl::stats::allocated().unwrap()),
14+
resident: Bytes(jemalloc_ctl::stats::resident().unwrap()),
15+
}
16+
}
17+
18+
#[cfg(not(feature = "jemalloc"))]
19+
pub fn current() -> MemoryUsage {
20+
MemoryUsage { allocated: Bytes(0), resident: Bytes(0) }
21+
}
22+
}
23+
24+
impl fmt::Display for MemoryUsage {
25+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
26+
write!(fmt, "{} allocated {} resident", self.allocated, self.resident,)
27+
}
28+
}
29+
30+
#[derive(Default)]
31+
pub struct Bytes(usize);
32+
33+
impl fmt::Display for Bytes {
34+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35+
let bytes = self.0;
36+
if bytes < 4096 {
37+
return write!(f, "{} bytes", bytes);
38+
}
39+
let kb = bytes / 1024;
40+
if kb < 4096 {
41+
return write!(f, "{}kb", kb);
42+
}
43+
let mb = kb / 1024;
44+
write!(f, "{}mb", mb)
45+
}
46+
}
47+
48+
impl std::ops::AddAssign<usize> for Bytes {
49+
fn add_assign(&mut self, x: usize) {
50+
self.0 += x;
51+
}
52+
}

0 commit comments

Comments
 (0)