Skip to content

Commit

Permalink
Heap histogram - WIP
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
glyn committed Jun 16, 2017
1 parent dbda939 commit 1459c76
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 87 deletions.
7 changes: 4 additions & 3 deletions src/agentcontroller/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

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

unsafe extern "C" fn test_get_env(vm: *mut ::jvmti::JavaVM,
penv: *mut *mut ::std::os::raw::c_void,
version: ::jvmti::jint)
unsafe extern "C" fn test_get_env(_: *mut ::jvmti::JavaVM,
_: *mut *mut ::std::os::raw::c_void,
_: ::jvmti::jint)
-> ::jvmti::jint {
0
}
Expand Down
35 changes: 32 additions & 3 deletions src/agentcontroller/heaphistogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,48 @@
* limitations under the License.
*/

use env::JvmTI;
use std::io::Write;
use std::io::stdout;
use heap::Tagger;
use heap::Tag;
use heap::Stats;
use heap::Record;
use heap::Print;

pub struct HeapHistogram {
jvmti: ::env::JvmTiEnv,
}

impl HeapHistogram {
pub fn new(jvmti: ::env::JvmTiEnv) -> Result<Self, ::jvmti::jint> {
pub fn new(mut jvmti: ::env::JvmTiEnv) -> Result<Self, ::jvmti::jint> {
jvmti.enable_object_tagging()?;
Ok(Self {
jvmti: jvmti
jvmti: jvmti,
})
}

pub fn print(&self, writer: &mut Write) {
let mut tagger = Tagger::new();

// Tag all loaded classes so we can determine each object's class signature during heap traversal.
self.jvmti.tag_loaded_classes(&mut tagger);

let mut heap_stats = Stats::new();

// Traverse the live heap and add objects to the heap stats.
self.jvmti.traverse_live_heap(|class_tag: ::jvmti::jlong, size: ::jvmti::jlong| {
if let Some(sig) = tagger.class_signature(class_tag) {
heap_stats.recordObject(sig, size);
}
});

heap_stats.print(writer);
}
}

impl super::Action for HeapHistogram {
fn on_oom(&self, jni_env: ::env::JniEnv, resource_exhaustion_flags: ::jvmti::jint) {
fn on_oom(&self, _: ::env::JniEnv, _: ::jvmti::jint) {
self.print(&mut stdout());
}
}
3 changes: 2 additions & 1 deletion src/agentcontroller/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl Kill {
}
}

#[cfg(test)]
pub fn setSignal(&mut self, signal: c_int) {
self.signal = signal;
}
Expand Down Expand Up @@ -93,7 +94,7 @@ mod tests {
signal::SaFlags::empty(),
signal::SigSet::empty());
unsafe {
signal::sigaction(signal::SIGUSR1, &sig_action);
signal::sigaction(signal::SIGUSR1, &sig_action).unwrap();
}
}
}
Loading

0 comments on commit 1459c76

Please sign in to comment.