diff options
author | Aleksey Kladov <[email protected]> | 2019-06-30 11:30:17 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-06-30 11:30:17 +0100 |
commit | 18a1e092e9406c6670cd38d17997325bba7bbfdc (patch) | |
tree | 65284e826535842d1a28db8713881151000da874 | |
parent | e18389d2684a8da9937fe37f9598fabf67c65fee (diff) |
Move memory usage statistics to ra_prof
-rw-r--r-- | Cargo.lock | 4 | ||||
-rw-r--r-- | crates/ra_cli/src/analysis_stats.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/Cargo.toml | 6 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide_api/src/status.rs | 54 | ||||
-rw-r--r-- | crates/ra_lsp_server/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_prof/Cargo.toml | 6 | ||||
-rw-r--r-- | crates/ra_prof/src/lib.rs | 14 | ||||
-rw-r--r-- | crates/ra_prof/src/memory_usage.rs | 52 |
9 files changed, 78 insertions, 68 deletions
diff --git a/Cargo.lock b/Cargo.lock index 4f691f39e..b26e2454c 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1147,8 +1147,6 @@ dependencies = [ | |||
1147 | "fst 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", | 1147 | "fst 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", |
1148 | "insta 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", | 1148 | "insta 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", |
1149 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1149 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1150 | "jemalloc-ctl 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1151 | "jemallocator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1152 | "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", | 1150 | "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", |
1153 | "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", | 1151 | "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", |
1154 | "proptest 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)", | 1152 | "proptest 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)", |
@@ -1223,6 +1221,8 @@ dependencies = [ | |||
1223 | "backtrace 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", | 1221 | "backtrace 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", |
1224 | "cpuprofiler 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", | 1222 | "cpuprofiler 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", |
1225 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1223 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1224 | "jemalloc-ctl 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1225 | "jemallocator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1226 | "once_cell 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", | 1226 | "once_cell 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", |
1227 | ] | 1227 | ] |
1228 | 1228 | ||
diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index ed98fc7f6..2eaf784a6 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs | |||
@@ -112,6 +112,6 @@ pub fn run(verbose: bool, path: &Path, only: Option<&str>) -> Result<()> { | |||
112 | num_exprs_partially_unknown, | 112 | num_exprs_partially_unknown, |
113 | (num_exprs_partially_unknown * 100 / num_exprs) | 113 | (num_exprs_partially_unknown * 100 / num_exprs) |
114 | ); | 114 | ); |
115 | println!("Analysis: {:?}", analysis_time.elapsed()); | 115 | println!("Analysis: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage()); |
116 | Ok(()) | 116 | Ok(()) |
117 | } | 117 | } |
diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml index 9bf5dd6e6..5bd768817 100644 --- a/crates/ra_ide_api/Cargo.toml +++ b/crates/ra_ide_api/Cargo.toml | |||
@@ -16,9 +16,6 @@ unicase = "2.2.0" | |||
16 | superslice = "1.0.0" | 16 | superslice = "1.0.0" |
17 | rand = "0.6.5" | 17 | rand = "0.6.5" |
18 | 18 | ||
19 | jemallocator = { version = "0.1.9", optional = true } | ||
20 | jemalloc-ctl = { version = "0.2.0", optional = true } | ||
21 | |||
22 | ra_syntax = { path = "../ra_syntax" } | 19 | ra_syntax = { path = "../ra_syntax" } |
23 | ra_text_edit = { path = "../ra_text_edit" } | 20 | ra_text_edit = { path = "../ra_text_edit" } |
24 | ra_db = { path = "../ra_db" } | 21 | ra_db = { path = "../ra_db" } |
@@ -36,6 +33,3 @@ version = "0.9.0" | |||
36 | # Disable `fork` feature to allow compiling on webassembly | 33 | # Disable `fork` feature to allow compiling on webassembly |
37 | default-features = false | 34 | default-features = false |
38 | features = ["std", "bit-set", "break-dead-code"] | 35 | features = ["std", "bit-set", "break-dead-code"] |
39 | |||
40 | [features] | ||
41 | jemalloc = [ "jemallocator", "jemalloc-ctl" ] | ||
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index e61d5627e..28a74c003 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -74,12 +74,6 @@ pub use crate::{ | |||
74 | pub use ra_db::{Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, Edition}; | 74 | pub use ra_db::{Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, Edition}; |
75 | pub use hir::Documentation; | 75 | pub use hir::Documentation; |
76 | 76 | ||
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 | |||
83 | pub type Cancelable<T> = Result<T, Canceled>; | 77 | pub type Cancelable<T> = Result<T, Canceled>; |
84 | 78 | ||
85 | #[derive(Debug)] | 79 | #[derive(Debug)] |
diff --git a/crates/ra_ide_api/src/status.rs b/crates/ra_ide_api/src/status.rs index 0cdeb15eb..ce25f4a87 100644 --- a/crates/ra_ide_api/src/status.rs +++ b/crates/ra_ide_api/src/status.rs | |||
@@ -9,6 +9,7 @@ use ra_db::{ | |||
9 | FileTextQuery, SourceRootId, | 9 | FileTextQuery, SourceRootId, |
10 | salsa::{Database, debug::{DebugQueryTable, TableEntry}}, | 10 | salsa::{Database, debug::{DebugQueryTable, TableEntry}}, |
11 | }; | 11 | }; |
12 | use ra_prof::{Bytes, memory_usage}; | ||
12 | use hir::MacroFile; | 13 | use hir::MacroFile; |
13 | 14 | ||
14 | use crate::{ | 15 | use crate::{ |
@@ -34,7 +35,7 @@ pub(crate) fn status(db: &RootDatabase) -> String { | |||
34 | symbols_stats, | 35 | symbols_stats, |
35 | syntax_tree_stats, | 36 | syntax_tree_stats, |
36 | macro_syntax_tree_stats, | 37 | macro_syntax_tree_stats, |
37 | MemoryStats::current(), | 38 | memory_usage(), |
38 | db.last_gc.elapsed().as_secs(), | 39 | db.last_gc.elapsed().as_secs(), |
39 | ) | 40 | ) |
40 | } | 41 | } |
@@ -138,54 +139,3 @@ impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbols | |||
138 | res | 139 | res |
139 | } | 140 | } |
140 | } | 141 | } |
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 | } | ||
diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index a1fcf6cee..4491b8590 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml | |||
@@ -32,4 +32,4 @@ tempfile = "3" | |||
32 | test_utils = { path = "../test_utils" } | 32 | test_utils = { path = "../test_utils" } |
33 | 33 | ||
34 | [features] | 34 | [features] |
35 | jemalloc = [ "ra_ide_api/jemalloc" ] | 35 | jemalloc = [ "ra_prof/jemalloc" ] |
diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index 787e18385..e986019ca 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml | |||
@@ -10,3 +10,9 @@ once_cell = "0.2.0" | |||
10 | itertools = "0.8.0" | 10 | itertools = "0.8.0" |
11 | backtrace = "0.3.28" | 11 | backtrace = "0.3.28" |
12 | cpuprofiler = { version = "0.0.3", optional = true } | 12 | 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" ] | ||
diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index 1e8d780ab..6f7918745 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | mod memory_usage; | ||
2 | |||
1 | use std::{ | 3 | use std::{ |
2 | cell::RefCell, | 4 | cell::RefCell, |
3 | time::{Duration, Instant}, | 5 | time::{Duration, Instant}, |
@@ -11,6 +13,14 @@ use std::{ | |||
11 | use once_cell::sync::Lazy; | 13 | use once_cell::sync::Lazy; |
12 | use itertools::Itertools; | 14 | use itertools::Itertools; |
13 | 15 | ||
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 | |||
14 | /// Set profiling filter. It specifies descriptions allowed to profile. | 24 | /// Set profiling filter. It specifies descriptions allowed to profile. |
15 | /// This is helpful when call stack has too many nested profiling scopes. | 25 | /// This is helpful when call stack has too many nested profiling scopes. |
16 | /// Additionally filter can specify maximum depth of profiling scopes nesting. | 26 | /// Additionally filter can specify maximum depth of profiling scopes nesting. |
@@ -288,6 +298,10 @@ impl Drop for CpuProfiler { | |||
288 | } | 298 | } |
289 | } | 299 | } |
290 | 300 | ||
301 | pub fn memory_usage() -> MemoryUsage { | ||
302 | MemoryUsage::current() | ||
303 | } | ||
304 | |||
291 | #[cfg(test)] | 305 | #[cfg(test)] |
292 | mod tests { | 306 | mod tests { |
293 | use super::*; | 307 | use super::*; |
diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs new file mode 100644 index 000000000..2bde8fb5f --- /dev/null +++ b/crates/ra_prof/src/memory_usage.rs | |||
@@ -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 | } | ||