Skip to content

Commit c5b9083

Browse files
committed
Heap histogram - WIP
Also: * Avoid unhelpful compiler warnings * Capture common code in a macro Note: it would have been nice to alias the closure type, but rust-lang/rfcs#1733 is not yet implemented and macros can't cope (rust-lang/rust#24010).
1 parent dbda939 commit c5b9083

File tree

6 files changed

+320
-87
lines changed

6 files changed

+320
-87
lines changed

src/agentcontroller/controller.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
pub struct AgentController<'a> {
18+
#[allow(dead_code)] // TODO: revisit this once port is complete
1819
jvmti: ::env::JvmTiEnv,
1920
heuristic: Box<super::Heuristic + 'a>,
2021
actions: Vec<Box<super::Action>>
@@ -126,9 +127,9 @@ mod tests {
126127
ac.on_oom(dummy_jni_env(), 0);
127128
}
128129

129-
unsafe extern "C" fn test_get_env(vm: *mut ::jvmti::JavaVM,
130-
penv: *mut *mut ::std::os::raw::c_void,
131-
version: ::jvmti::jint)
130+
unsafe extern "C" fn test_get_env(_: *mut ::jvmti::JavaVM,
131+
_: *mut *mut ::std::os::raw::c_void,
132+
_: ::jvmti::jint)
132133
-> ::jvmti::jint {
133134
0
134135
}

src/agentcontroller/heaphistogram.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,48 @@
1414
* limitations under the License.
1515
*/
1616

17+
use env::JvmTI;
18+
use std::io::Write;
19+
use std::io::stdout;
20+
use heap::Tagger;
21+
use heap::Tag;
22+
use heap::Stats;
23+
use heap::Record;
24+
use heap::Print;
25+
1726
pub struct HeapHistogram {
1827
jvmti: ::env::JvmTiEnv,
1928
}
2029

2130
impl HeapHistogram {
22-
pub fn new(jvmti: ::env::JvmTiEnv) -> Result<Self, ::jvmti::jint> {
31+
pub fn new(mut jvmti: ::env::JvmTiEnv) -> Result<Self, ::jvmti::jint> {
32+
jvmti.enable_object_tagging()?;
2333
Ok(Self {
24-
jvmti: jvmti
34+
jvmti: jvmti,
2535
})
2636
}
37+
38+
pub fn print(&self, writer: &mut Write) {
39+
let mut tagger = Tagger::new();
40+
41+
// Tag all loaded classes so we can determine each object's class signature during heap traversal.
42+
self.jvmti.tag_loaded_classes(&mut tagger);
43+
44+
let mut heap_stats = Stats::new();
45+
46+
// Traverse the live heap and add objects to the heap stats.
47+
self.jvmti.traverse_live_heap(|class_tag: ::jvmti::jlong, size: ::jvmti::jlong| {
48+
if let Some(sig) = tagger.class_signature(class_tag) {
49+
heap_stats.recordObject(sig, size);
50+
}
51+
});
52+
53+
heap_stats.print(writer);
54+
}
2755
}
2856

2957
impl super::Action for HeapHistogram {
30-
fn on_oom(&self, jni_env: ::env::JniEnv, resource_exhaustion_flags: ::jvmti::jint) {
58+
fn on_oom(&self, _: ::env::JniEnv, _: ::jvmti::jint) {
59+
self.print(&mut stdout());
3160
}
3261
}

src/agentcontroller/kill.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl Kill {
3030
}
3131
}
3232

33+
#[cfg(test)]
3334
pub fn setSignal(&mut self, signal: c_int) {
3435
self.signal = signal;
3536
}
@@ -93,7 +94,7 @@ mod tests {
9394
signal::SaFlags::empty(),
9495
signal::SigSet::empty());
9596
unsafe {
96-
signal::sigaction(signal::SIGUSR1, &sig_action);
97+
signal::sigaction(signal::SIGUSR1, &sig_action).unwrap();
9798
}
9899
}
99100
}

0 commit comments

Comments
 (0)