reth_cli_commands/db/
stats.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::{common::CliNodeTypes, db::checksum::ChecksumViewer};
use clap::Parser;
use comfy_table::{Cell, Row, Table as ComfyTable};
use eyre::WrapErr;
use human_bytes::human_bytes;
use itertools::Itertools;
use reth_chainspec::EthereumHardforks;
use reth_db::{mdbx, static_file::iter_static_files, DatabaseEnv, TableViewer, Tables};
use reth_db_api::database::Database;
use reth_db_common::DbTool;
use reth_fs_util as fs;
use reth_node_builder::{NodePrimitives, NodeTypesWithDB, NodeTypesWithDBAdapter};
use reth_node_core::dirs::{ChainPath, DataDirPath};
use reth_provider::providers::{ProviderNodeTypes, StaticFileProvider};
use reth_static_file_types::SegmentRangeInclusive;
use std::{sync::Arc, time::Duration};

#[derive(Parser, Debug)]
/// The arguments for the `reth db stats` command
pub struct Command {
    /// Show only the total size for static files.
    #[arg(long, default_value_t = false)]
    detailed_sizes: bool,

    /// Show detailed information per static file segment.
    #[arg(long, default_value_t = false)]
    detailed_segments: bool,

    /// Show a checksum of each table in the database.
    ///
    /// WARNING: this option will take a long time to run, as it needs to traverse and hash the
    /// entire database.
    ///
    /// For individual table checksums, use the `reth db checksum` command.
    #[arg(long, default_value_t = false)]
    checksum: bool,
}

impl Command {
    /// Execute `db stats` command
    pub fn execute<N: CliNodeTypes<ChainSpec: EthereumHardforks>>(
        self,
        data_dir: ChainPath<DataDirPath>,
        tool: &DbTool<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>,
    ) -> eyre::Result<()> {
        if self.checksum {
            let checksum_report = self.checksum_report(tool)?;
            println!("{checksum_report}");
            println!("\n");
        }

        let static_files_stats_table = self.static_files_stats_table::<N::Primitives>(data_dir)?;
        println!("{static_files_stats_table}");

        println!("\n");

        let db_stats_table = self.db_stats_table(tool)?;
        println!("{db_stats_table}");

        Ok(())
    }

    fn db_stats_table<N: NodeTypesWithDB<DB = Arc<DatabaseEnv>>>(
        &self,
        tool: &DbTool<N>,
    ) -> eyre::Result<ComfyTable> {
        let mut table = ComfyTable::new();
        table.load_preset(comfy_table::presets::ASCII_MARKDOWN);
        table.set_header([
            "Table Name",
            "# Entries",
            "Branch Pages",
            "Leaf Pages",
            "Overflow Pages",
            "Total Size",
        ]);

        tool.provider_factory.db_ref().view(|tx| {
            let mut db_tables = Tables::ALL.iter().map(|table| table.name()).collect::<Vec<_>>();
            db_tables.sort();
            let mut total_size = 0;
            for db_table in db_tables {
                let table_db = tx.inner.open_db(Some(db_table)).wrap_err("Could not open db.")?;

                let stats = tx
                    .inner
                    .db_stat(&table_db)
                    .wrap_err(format!("Could not find table: {db_table}"))?;

                // Defaults to 16KB right now but we should
                // re-evaluate depending on the DB we end up using
                // (e.g. REDB does not have these options as configurable intentionally)
                let page_size = stats.page_size() as usize;
                let leaf_pages = stats.leaf_pages();
                let branch_pages = stats.branch_pages();
                let overflow_pages = stats.overflow_pages();
                let num_pages = leaf_pages + branch_pages + overflow_pages;
                let table_size = page_size * num_pages;

                total_size += table_size;
                let mut row = Row::new();
                row.add_cell(Cell::new(db_table))
                    .add_cell(Cell::new(stats.entries()))
                    .add_cell(Cell::new(branch_pages))
                    .add_cell(Cell::new(leaf_pages))
                    .add_cell(Cell::new(overflow_pages))
                    .add_cell(Cell::new(human_bytes(table_size as f64)));
                table.add_row(row);
            }

            let max_widths = table.column_max_content_widths();
            let mut separator = Row::new();
            for width in max_widths {
                separator.add_cell(Cell::new("-".repeat(width as usize)));
            }
            table.add_row(separator);

            let mut row = Row::new();
            row.add_cell(Cell::new("Tables"))
                .add_cell(Cell::new(""))
                .add_cell(Cell::new(""))
                .add_cell(Cell::new(""))
                .add_cell(Cell::new(""))
                .add_cell(Cell::new(human_bytes(total_size as f64)));
            table.add_row(row);

            let freelist = tx.inner.env().freelist()?;
            let pagesize = tx.inner.db_stat(&mdbx::Database::freelist_db())?.page_size() as usize;
            let freelist_size = freelist * pagesize;

            let mut row = Row::new();
            row.add_cell(Cell::new("Freelist"))
                .add_cell(Cell::new(freelist))
                .add_cell(Cell::new(""))
                .add_cell(Cell::new(""))
                .add_cell(Cell::new(""))
                .add_cell(Cell::new(human_bytes(freelist_size as f64)));
            table.add_row(row);

            Ok::<(), eyre::Report>(())
        })??;

        Ok(table)
    }

    fn static_files_stats_table<N: NodePrimitives>(
        &self,
        data_dir: ChainPath<DataDirPath>,
    ) -> eyre::Result<ComfyTable> {
        let mut table = ComfyTable::new();
        table.load_preset(comfy_table::presets::ASCII_MARKDOWN);

        if self.detailed_sizes {
            table.set_header([
                "Segment",
                "Block Range",
                "Transaction Range",
                "Shape (columns x rows)",
                "Data Size",
                "Index Size",
                "Offsets Size",
                "Config Size",
                "Total Size",
            ]);
        } else {
            table.set_header([
                "Segment",
                "Block Range",
                "Transaction Range",
                "Shape (columns x rows)",
                "Size",
            ]);
        }

        let static_files = iter_static_files(data_dir.static_files())?;
        let static_file_provider =
            StaticFileProvider::<N>::read_only(data_dir.static_files(), false)?;

        let mut total_data_size = 0;
        let mut total_index_size = 0;
        let mut total_offsets_size = 0;
        let mut total_config_size = 0;

        for (segment, ranges) in static_files.into_iter().sorted_by_key(|(segment, _)| *segment) {
            let (
                mut segment_columns,
                mut segment_rows,
                mut segment_data_size,
                mut segment_index_size,
                mut segment_offsets_size,
                mut segment_config_size,
            ) = (0, 0, 0, 0, 0, 0);

            for (block_range, tx_range) in &ranges {
                let fixed_block_range = static_file_provider.find_fixed_range(block_range.start());
                let jar_provider = static_file_provider
                    .get_segment_provider(segment, || Some(fixed_block_range), None)?
                    .ok_or_else(|| {
                        eyre::eyre!("Failed to get segment provider for segment: {}", segment)
                    })?;

                let columns = jar_provider.columns();
                let rows = jar_provider.rows();

                let data_size = fs::metadata(jar_provider.data_path())
                    .map(|metadata| metadata.len())
                    .unwrap_or_default();
                let index_size = fs::metadata(jar_provider.index_path())
                    .map(|metadata| metadata.len())
                    .unwrap_or_default();
                let offsets_size = fs::metadata(jar_provider.offsets_path())
                    .map(|metadata| metadata.len())
                    .unwrap_or_default();
                let config_size = fs::metadata(jar_provider.config_path())
                    .map(|metadata| metadata.len())
                    .unwrap_or_default();

                if self.detailed_segments {
                    let mut row = Row::new();
                    row.add_cell(Cell::new(segment))
                        .add_cell(Cell::new(format!("{block_range}")))
                        .add_cell(Cell::new(
                            tx_range.map_or("N/A".to_string(), |tx_range| format!("{tx_range}")),
                        ))
                        .add_cell(Cell::new(format!("{columns} x {rows}")));
                    if self.detailed_sizes {
                        row.add_cell(Cell::new(human_bytes(data_size as f64)))
                            .add_cell(Cell::new(human_bytes(index_size as f64)))
                            .add_cell(Cell::new(human_bytes(offsets_size as f64)))
                            .add_cell(Cell::new(human_bytes(config_size as f64)));
                    }
                    row.add_cell(Cell::new(human_bytes(
                        (data_size + index_size + offsets_size + config_size) as f64,
                    )));
                    table.add_row(row);
                } else {
                    if segment_columns > 0 {
                        assert_eq!(segment_columns, columns);
                    } else {
                        segment_columns = columns;
                    }
                    segment_rows += rows;
                    segment_data_size += data_size;
                    segment_index_size += index_size;
                    segment_offsets_size += offsets_size;
                    segment_config_size += config_size;
                }

                total_data_size += data_size;
                total_index_size += index_size;
                total_offsets_size += offsets_size;
                total_config_size += config_size;

                // Manually drop provider, otherwise removal from cache will deadlock.
                drop(jar_provider);

                // Removes from cache, since if we have many files, it may hit ulimit limits
                static_file_provider.remove_cached_provider(segment, fixed_block_range.end());
            }

            if !self.detailed_segments {
                let first_ranges = ranges.first().expect("not empty list of ranges");
                let last_ranges = ranges.last().expect("not empty list of ranges");

                let block_range =
                    SegmentRangeInclusive::new(first_ranges.0.start(), last_ranges.0.end());

                // Transaction ranges can be empty, so we need to find the first and last which are
                // not.
                let tx_range = {
                    let start = ranges
                        .iter()
                        .find_map(|(_, tx_range)| tx_range.map(|r| r.start()))
                        .unwrap_or_default();
                    let end =
                        ranges.iter().rev().find_map(|(_, tx_range)| tx_range.map(|r| r.end()));
                    end.map(|end| SegmentRangeInclusive::new(start, end))
                };

                let mut row = Row::new();
                row.add_cell(Cell::new(segment))
                    .add_cell(Cell::new(format!("{block_range}")))
                    .add_cell(Cell::new(
                        tx_range.map_or("N/A".to_string(), |tx_range| format!("{tx_range}")),
                    ))
                    .add_cell(Cell::new(format!("{segment_columns} x {segment_rows}")));
                if self.detailed_sizes {
                    row.add_cell(Cell::new(human_bytes(segment_data_size as f64)))
                        .add_cell(Cell::new(human_bytes(segment_index_size as f64)))
                        .add_cell(Cell::new(human_bytes(segment_offsets_size as f64)))
                        .add_cell(Cell::new(human_bytes(segment_config_size as f64)));
                }
                row.add_cell(Cell::new(human_bytes(
                    (segment_data_size +
                        segment_index_size +
                        segment_offsets_size +
                        segment_config_size) as f64,
                )));
                table.add_row(row);
            }
        }

        let max_widths = table.column_max_content_widths();
        let mut separator = Row::new();
        for width in max_widths {
            separator.add_cell(Cell::new("-".repeat(width as usize)));
        }
        table.add_row(separator);

        let mut row = Row::new();
        row.add_cell(Cell::new("Total"))
            .add_cell(Cell::new(""))
            .add_cell(Cell::new(""))
            .add_cell(Cell::new(""));
        if self.detailed_sizes {
            row.add_cell(Cell::new(human_bytes(total_data_size as f64)))
                .add_cell(Cell::new(human_bytes(total_index_size as f64)))
                .add_cell(Cell::new(human_bytes(total_offsets_size as f64)))
                .add_cell(Cell::new(human_bytes(total_config_size as f64)));
        }
        row.add_cell(Cell::new(human_bytes(
            (total_data_size + total_index_size + total_offsets_size + total_config_size) as f64,
        )));
        table.add_row(row);

        Ok(table)
    }

    fn checksum_report<N: ProviderNodeTypes>(&self, tool: &DbTool<N>) -> eyre::Result<ComfyTable> {
        let mut table = ComfyTable::new();
        table.load_preset(comfy_table::presets::ASCII_MARKDOWN);
        table.set_header(vec![Cell::new("Table"), Cell::new("Checksum"), Cell::new("Elapsed")]);

        let db_tables = Tables::ALL;
        let mut total_elapsed = Duration::default();

        for &db_table in db_tables {
            let (checksum, elapsed) = ChecksumViewer::new(tool).view_rt(db_table).unwrap();

            // increment duration for final report
            total_elapsed += elapsed;

            // add rows containing checksums to the table
            let mut row = Row::new();
            row.add_cell(Cell::new(db_table));
            row.add_cell(Cell::new(format!("{:x}", checksum)));
            row.add_cell(Cell::new(format!("{:?}", elapsed)));
            table.add_row(row);
        }

        // add a separator for the final report
        let max_widths = table.column_max_content_widths();
        let mut separator = Row::new();
        for width in max_widths {
            separator.add_cell(Cell::new("-".repeat(width as usize)));
        }
        table.add_row(separator);

        // add the final report
        let mut row = Row::new();
        row.add_cell(Cell::new("Total elapsed"));
        row.add_cell(Cell::new(""));
        row.add_cell(Cell::new(format!("{:?}", total_elapsed)));
        table.add_row(row);

        Ok(table)
    }
}