_core_rustc_static/
allocations_table.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use heapdump_vmo::allocations_table_v1::{AllocationsTableWriter, ResourceKey};
use zx::{self as zx, AsHandleRef, HandleBased};

/// We cap the size of our backing VMO at 2 GiB, then preallocate it and map it entirely.
/// Actual memory for each page will only be committed when we first write to that page.
const VMO_SIZE: usize = 1 << 31;

const VMO_NAME: zx::Name = zx::Name::new_lossy("heapdump-allocations");

/// Tracks live allocations by storing their metadata in a dedicated VMO.
pub struct AllocationsTable {
    vmo: zx::Vmo,
    writer: AllocationsTableWriter,
}

impl Default for AllocationsTable {
    fn default() -> AllocationsTable {
        let vmo = zx::Vmo::create(VMO_SIZE as u64).expect("failed to create allocations VMO");
        vmo.set_name(&VMO_NAME).expect("failed to set VMO name");

        let writer = AllocationsTableWriter::new(&vmo).expect("failed to create writer");
        AllocationsTable { vmo, writer }
    }
}

impl AllocationsTable {
    /// Duplicate the handle to the underlying VMO.
    pub fn share_vmo(&self) -> zx::Vmo {
        self.vmo.duplicate_handle(zx::Rights::SAME_RIGHTS).expect("failed to share allocations VMO")
    }

    // Take a snapshot of the underlying VMO.
    pub fn snapshot_vmo(&self) -> zx::Vmo {
        self.vmo
            .create_child(
                zx::VmoChildOptions::SNAPSHOT | zx::VmoChildOptions::NO_WRITE,
                0,
                VMO_SIZE as u64,
            )
            .expect("failed to snapshot allocations VMO")
    }

    pub fn try_record_allocation(
        &mut self,
        address: u64,
        size: u64,
        thread_info_key: ResourceKey,
        stack_trace_key: ResourceKey,
        timestamp: zx::MonotonicInstant,
    ) -> bool {
        self.writer
            .insert_allocation(address, size, thread_info_key, stack_trace_key, timestamp)
            .expect("out of space")
    }

    pub fn update_allocation(
        &mut self,
        address: u64,
        size: u64,
        thread_info_key: ResourceKey,
        stack_trace_key: ResourceKey,
        timestamp: zx::MonotonicInstant,
    ) -> u64 {
        if let Some(old_size) = self
            .writer
            .replace_allocation(address, size, thread_info_key, stack_trace_key, timestamp)
            .expect("out of space")
        {
            old_size
        } else {
            panic!("Block 0x{:x} was not allocated", address);
        }
    }

    pub fn forget_allocation(&mut self, address: u64) -> u64 {
        if let Some(size) = self.writer.erase_allocation(address) {
            size
        } else {
            panic!("Block 0x{:x} was not allocated", address);
        }
    }
}