objectdump.rs 4.77 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2
use utils::Address;
use utils::ByteSize;
3 4 5 6 7 8
use utils::POINTER_SIZE;
use common::gctype::*;

use MY_GC;
use objectmodel;

qinsoon's avatar
qinsoon committed
9
use std::collections::HashMap;
10
use std::sync::Arc;
qinsoon's avatar
qinsoon committed
11 12 13 14 15 16 17 18 19 20 21 22

pub struct HeapDump {
    pub objects: HashMap<Address, ObjectDump>,
    pub relocatable_refs: HashMap<Address, String>
}

pub struct ObjectDump {
    pub reference_addr: Address,

    pub mem_start: Address,
    pub mem_size : ByteSize,
    pub reference_offsets: Vec<ByteSize>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}

impl HeapDump {
    pub fn from_roots(roots: Vec<Address>) -> HeapDump {
        let mut work_queue : Vec<Address> = roots;
        let mut heap : HeapDump = HeapDump {
            objects: HashMap::new(),
            relocatable_refs: HashMap::new()
        };

        while !work_queue.is_empty() {
            let obj = work_queue.pop().unwrap();

            if !heap.objects.contains_key(&obj) {
                // add this object to heap dump
                let obj_dump = heap.persist_object(obj);
                heap.objects.insert(obj, obj_dump);

                heap.keep_tracing(heap.objects.get(&obj).unwrap(), &mut work_queue);
            }
        }

        heap.label_relocatable_refs();

        heap
    }

    fn persist_object(&self, obj: Address) -> ObjectDump {
        let hdr_addr = obj.offset(objectmodel::OBJECT_HEADER_OFFSET);
        let hdr = unsafe {hdr_addr.load::<u64>()};

        if objectmodel::header_is_fix_size(hdr) {
            // fix sized type
            if objectmodel::header_has_ref_map(hdr) {
                // has ref map
                let ref_map = objectmodel::header_get_ref_map(hdr);

                let mut offsets = vec![];
                let mut i = 0;
                while i < objectmodel::REF_MAP_LENGTH {
                    let has_ref : bool = ((ref_map >> i) & 1) == 1;

                    if has_ref {
                        offsets.push(i * POINTER_SIZE);
                    }

                    i += 1;
                }

                ObjectDump {
                    reference_addr   : obj,
                    mem_start        : hdr_addr,
                    mem_size         : objectmodel::header_get_object_size(hdr) as usize + objectmodel::OBJECT_HEADER_SIZE,
                    reference_offsets: offsets
                }
            } else {
                // by type ID
                let gctype_id = objectmodel::header_get_gctype_id(hdr);

                let gc_lock = MY_GC.read().unwrap();
                let gctype : Arc<GCType> = gc_lock.as_ref().unwrap().gc_types[gctype_id as usize].clone();

                ObjectDump {
                    reference_addr: obj,
                    mem_start     : hdr_addr,
                    mem_size      : gctype.size,
                    reference_offsets: gctype.gen_ref_offsets()
                }
            }
        } else {
            // hybrids - same as above
            let gctype_id = objectmodel::header_get_gctype_id(hdr);

            let gc_lock = MY_GC.read().unwrap();
            let gctype : Arc<GCType> = gc_lock.as_ref().unwrap().gc_types[gctype_id as usize].clone();

            ObjectDump {
                reference_addr: obj,
                mem_start     : hdr_addr,
                mem_size      : gctype.size,
                reference_offsets: gctype.gen_ref_offsets()
            }
        }
    }

    fn keep_tracing(&self, obj_dump: &ObjectDump, work_queue: &mut Vec<Address>) {
        let base = obj_dump.reference_addr;

        for offset in obj_dump.reference_offsets.iter() {
            let field_addr = base.plus(*offset);
            let edge = unsafe {field_addr.load::<Address>()};

            if !edge.is_zero() && !self.objects.contains_key(&edge) {
                work_queue.push(edge);
            }
        }
    }

    fn label_relocatable_refs(&mut self) {
        let mut count = 0;

        for addr in self.objects.keys() {
            let label = format!("GCDUMP_{}_{}", count, addr);
            self.relocatable_refs.insert(*addr, label);

            count += 1;
        }
    }
}

use std::fmt;

impl fmt::Debug for ObjectDump {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "PersistedObject({}, {} bytes from {}, offsets at {:?})",
               self.reference_addr, self.mem_size, self.mem_start, self.reference_offsets
        )
    }
}

impl fmt::Debug for HeapDump {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Heap Dump\n").unwrap();

        write!(f, "---{} objects---\n", self.objects.len()).unwrap();
        for obj in self.objects.iter() {
            write!(f, "{:?}\n", obj).unwrap();
        }

        write!(f, "---{} ref labels---\n", self.relocatable_refs.len()).unwrap();
        for (addr, label) in self.relocatable_refs.iter() {
            write!(f, "{} = {}\n", addr, label).unwrap()
        }

        Ok(())
    }
qinsoon's avatar
qinsoon committed
160
}