|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -pub struct HeapHistogram { |
18 |
| - jvmti: ::env::JvmTiEnv, |
| 17 | +use env::JvmTI; |
| 18 | +use std::io::Write; |
| 19 | +use std::io::stdout; |
| 20 | +use heap::tagger::Tagger; |
| 21 | +use heap::tagger::Tag; |
| 22 | +use heap::stats::Stats; |
| 23 | +use heap::stats::Record; |
| 24 | +use heap::stats::Print; |
| 25 | + |
| 26 | +pub struct HeapHistogram<T: JvmTI + Copy> { |
| 27 | + jvmti: T, |
19 | 28 | }
|
20 | 29 |
|
21 |
| -impl HeapHistogram { |
22 |
| - pub fn new(jvmti: ::env::JvmTiEnv) -> Result<Self, ::jvmti::jint> { |
| 30 | +impl<T: JvmTI + Copy> HeapHistogram<T> { |
| 31 | + pub fn new(mut jvmti: T) -> Result<Self, ::jvmti::jint> { |
| 32 | + jvmti.enable_object_tagging()?; |
23 | 33 | Ok(Self {
|
24 |
| - jvmti: jvmti |
| 34 | + jvmti: jvmti, |
25 | 35 | })
|
26 | 36 | }
|
| 37 | + |
| 38 | + 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 | + } |
27 | 55 | }
|
28 | 56 |
|
29 |
| -impl super::Action for HeapHistogram { |
30 |
| - fn on_oom(&self, jni_env: ::env::JniEnv, resource_exhaustion_flags: ::jvmti::jint) { |
| 57 | +impl<T: JvmTI + Copy> super::Action for HeapHistogram<T> { |
| 58 | + fn on_oom(&self, _: ::env::JniEnv, _: ::jvmti::jint) { |
| 59 | + self.print(&mut stdout()); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::HeapHistogram; |
| 66 | + use ::env::JvmTI; |
| 67 | + use ::env::FnResourceExhausted; |
| 68 | + use std::sync::Mutex; |
| 69 | + use ::env::RawMonitorId; |
| 70 | + use ::heap::tagger::Tag; |
| 71 | + |
| 72 | + const test_error_code: ::jvmti::jint = 54; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn new_calls_enable_object_tagging() { |
| 76 | + let mut mockJvmti = MockJvmti::new(); |
| 77 | + let hh = HeapHistogram::new(mockJvmti); |
| 78 | + assert!(hh.is_ok()); |
| 79 | + assert!((hh.unwrap().jvmti as MockJvmti).object_tagging_enabled); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn new_percolates_enable_object_tagging_failure() { |
| 84 | + let mut mockJvmti = MockJvmti::new(); |
| 85 | + mockJvmti.object_tagging_enabled_result = Err(test_error_code); |
| 86 | + let hh = HeapHistogram::new(mockJvmti); |
| 87 | + assert!(hh.is_err()); |
| 88 | + assert_eq!(hh.err().unwrap(), test_error_code); |
| 89 | + } |
| 90 | + |
| 91 | + #[derive(Clone, Copy)] |
| 92 | + struct MockJvmti { |
| 93 | + pub object_tagging_enabled_result: Result<(), ::jvmti::jint>, |
| 94 | + pub object_tagging_enabled: bool |
| 95 | + } |
| 96 | + |
| 97 | + impl MockJvmti { |
| 98 | + fn new() -> MockJvmti { |
| 99 | + MockJvmti { |
| 100 | + object_tagging_enabled_result: Ok(()), |
| 101 | + object_tagging_enabled: false |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + impl JvmTI for MockJvmti { |
| 107 | + fn create_raw_monitor(&mut self, name: String, monitor: &Mutex<RawMonitorId>) -> Result<(), ::jvmti::jint> { |
| 108 | + unimplemented!() |
| 109 | + } |
| 110 | + |
| 111 | + fn raw_monitor_enter(&mut self, monitor: &Mutex<RawMonitorId>) -> Result<(), ::jvmti::jint> { |
| 112 | + unimplemented!() |
| 113 | + } |
| 114 | + |
| 115 | + fn raw_monitor_exit(&mut self, monitor: &Mutex<RawMonitorId>) -> Result<(), ::jvmti::jint> { |
| 116 | + unimplemented!() |
| 117 | + } |
| 118 | + |
| 119 | + fn on_resource_exhausted(&mut self, callback: FnResourceExhausted) -> Result<(), ::jvmti::jint> { |
| 120 | + unimplemented!() |
| 121 | + } |
| 122 | + |
| 123 | + fn enable_object_tagging(&mut self) -> Result<(), ::jvmti::jint> { |
| 124 | + self.object_tagging_enabled = true; |
| 125 | + self.object_tagging_enabled_result |
| 126 | + } |
| 127 | + |
| 128 | + fn tag_loaded_classes(&self, tagger: &mut Tag) { |
| 129 | + unimplemented!() |
| 130 | + } |
| 131 | + |
| 132 | + fn traverse_live_heap<F>(&self, closure: F) where F: FnMut(::jvmti::jlong, ::jvmti::jlong) { |
| 133 | + unimplemented!() |
| 134 | + } |
31 | 135 | }
|
32 | 136 | }
|
0 commit comments