objectdump.rs 5.24 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

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

pub struct ObjectDump {
    pub mem_start: Address,
    pub mem_size : ByteSize,
20 21 22

    pub reference_addr: Address,
    pub reference_offsets: Vec<ByteSize> // based on reference_addr
23 24 25 26
}

impl HeapDump {
    pub fn from_roots(roots: Vec<Address>) -> HeapDump {
27
        trace!("dump heap from {:?}", roots);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        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 {
52
        trace!("dump object: {}", obj);
53 54 55 56 57 58 59 60 61
        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);

62 63
                trace!("fix sized, ref map as {:b}", ref_map);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                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);

86 87
                trace!("fix size, type id as {}", gctype_id);

88 89 90 91 92 93
                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,
qinsoon's avatar
qinsoon committed
94
                    mem_size      : gctype.size() + objectmodel::OBJECT_HEADER_SIZE,
95 96 97 98 99
                    reference_offsets: gctype.gen_ref_offsets()
                }
            }
        } else {
            // hybrids - same as above
qinsoon's avatar
qinsoon committed
100 101
            let gctype_id  = objectmodel::header_get_gctype_id(hdr);
            let var_length = objectmodel::header_get_hybrid_length(hdr);
102

103 104
            trace!("var sized, type id as {}", gctype_id);

105 106 107 108 109 110
            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,
qinsoon's avatar
qinsoon committed
111 112
                mem_size      : gctype.size_hybrid(var_length) + objectmodel::OBJECT_HEADER_SIZE,
                reference_offsets: gctype.gen_hybrid_ref_offsets(var_length)
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 160 161 162 163 164 165 166 167 168
            }
        }
    }

    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
169
}