Skip to content

Commit 0dd8a7e

Browse files
feat: report world state size on disk (#13706)
Closes #13200 Adds reporting for tracking the disk space currently used by the current world state.
1 parent 55b9a2b commit 0dd8a7e

File tree

8 files changed

+95
-7
lines changed

8 files changed

+95
-7
lines changed

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/lmdb_store/lmdb_tree_store.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const std::string& LMDBTreeStore::get_name() const
103103
void LMDBTreeStore::get_stats(TreeDBStats& stats, ReadTransaction& tx)
104104
{
105105
stats.mapSize = _environment->get_map_size();
106+
stats.physicalFileSize = _environment->get_data_file_size();
106107
stats.blocksDBStats = _blockDatabase->get_stats(tx);
107108
stats.leafPreimagesDBStats = _leafHashToPreImageDatabase->get_stats(tx);
108109
stats.leafIndicesDBStats = _leafKeyToIndexDatabase->get_stats(tx);

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/lmdb_store/lmdb_tree_store.test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,49 @@ TEST_F(LMDBTreeStoreTest, can_write_and_retrieve_block_numbers_with_duplicate_in
585585
EXPECT_EQ(readBack, 5);
586586
}
587587
}
588+
589+
TEST_F(LMDBTreeStoreTest, reports_physical_file_size)
590+
{
591+
LMDBTreeStore store(_directory, "DB1", _mapSize, _maxReaders);
592+
std::string dataDbPath = _directory + "/data.mdb";
593+
size_t previousFileSize = 0;
594+
595+
for (size_t i = 0; i < 3; i++) {
596+
{
597+
BlockPayload blockData;
598+
blockData.blockNumber = i;
599+
blockData.root = VALUES[i];
600+
blockData.size = 45 + (i * 97);
601+
602+
TreeMeta metaData;
603+
metaData.committedSize = blockData.size;
604+
metaData.size = blockData.size;
605+
metaData.root = blockData.root;
606+
metaData.depth = 32;
607+
metaData.unfinalisedBlockHeight = i;
608+
metaData.name = "NullifierTree";
609+
610+
// Write metadata and block data with different values each iteration
611+
LMDBWriteTransaction::Ptr transaction = store.create_write_transaction();
612+
store.write_meta_data(metaData, *transaction);
613+
store.write_block_data(i, blockData, *transaction);
614+
transaction->commit();
615+
}
616+
617+
{
618+
LMDBReadTransaction::Ptr transaction = store.create_read_transaction();
619+
TreeDBStats stats;
620+
store.get_stats(stats, *transaction);
621+
622+
EXPECT_TRUE(std::filesystem::exists(dataDbPath));
623+
624+
// Verify reported size matches actual file size
625+
EXPECT_EQ(stats.physicalFileSize, std::filesystem::file_size(dataDbPath));
626+
627+
// Verify size is increasing due to the new DB writes
628+
EXPECT_GT(stats.physicalFileSize, previousFileSize);
629+
630+
previousFileSize = stats.physicalFileSize;
631+
}
632+
}
633+
}

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/types.hpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,27 @@ const std::string BLOCK_INDICES_DB = "block indices";
6767

6868
struct TreeDBStats {
6969
uint64_t mapSize;
70+
uint64_t physicalFileSize;
7071
DBStats blocksDBStats;
7172
DBStats nodesDBStats;
7273
DBStats leafPreimagesDBStats;
7374
DBStats leafIndicesDBStats;
7475
DBStats blockIndicesDBStats;
7576

7677
TreeDBStats() = default;
77-
TreeDBStats(uint64_t mapSize)
78+
TreeDBStats(uint64_t mapSize, uint64_t physicalFileSize)
7879
: mapSize(mapSize)
80+
, physicalFileSize(physicalFileSize)
7981
{}
8082
TreeDBStats(uint64_t mapSize,
83+
uint64_t physicalFileSize,
8184
const DBStats& blockStats,
8285
const DBStats& nodesStats,
8386
const DBStats& leafPreimagesDBStats,
8487
const DBStats& leafIndicesStats,
8588
const DBStats& blockIndicesStats)
8689
: mapSize(mapSize)
90+
, physicalFileSize(physicalFileSize)
8791
, blocksDBStats(blockStats)
8892
, nodesDBStats(nodesStats)
8993
, leafPreimagesDBStats(leafPreimagesDBStats)
@@ -95,11 +99,18 @@ struct TreeDBStats {
9599

96100
~TreeDBStats() = default;
97101

98-
MSGPACK_FIELDS(mapSize, blocksDBStats, nodesDBStats, leafPreimagesDBStats, leafIndicesDBStats, blockIndicesDBStats)
102+
MSGPACK_FIELDS(mapSize,
103+
physicalFileSize,
104+
blocksDBStats,
105+
nodesDBStats,
106+
leafPreimagesDBStats,
107+
leafIndicesDBStats,
108+
blockIndicesDBStats)
99109

100110
bool operator==(const TreeDBStats& other) const
101111
{
102-
return mapSize == other.mapSize && blocksDBStats == other.blocksDBStats && nodesDBStats == other.nodesDBStats &&
112+
return mapSize == other.mapSize && physicalFileSize == other.physicalFileSize &&
113+
blocksDBStats == other.blocksDBStats && nodesDBStats == other.nodesDBStats &&
103114
leafPreimagesDBStats == other.leafPreimagesDBStats && leafIndicesDBStats == other.leafIndicesDBStats &&
104115
blockIndicesDBStats == other.blockIndicesDBStats;
105116
}
@@ -108,6 +119,7 @@ struct TreeDBStats {
108119
{
109120
if (this != &other) {
110121
mapSize = other.mapSize;
122+
physicalFileSize = other.physicalFileSize;
111123
blocksDBStats = std::move(other.blocksDBStats);
112124
nodesDBStats = std::move(other.nodesDBStats);
113125
leafPreimagesDBStats = std::move(other.leafPreimagesDBStats);
@@ -121,9 +133,10 @@ struct TreeDBStats {
121133

122134
friend std::ostream& operator<<(std::ostream& os, const TreeDBStats& stats)
123135
{
124-
os << "Map Size: " << stats.mapSize << " Blocks DB " << stats.blocksDBStats << ", Nodes DB "
125-
<< stats.nodesDBStats << ", Leaf Pre-images DB " << stats.leafPreimagesDBStats << ", Leaf Indices DB "
126-
<< stats.leafIndicesDBStats << ", Block Indices DB " << stats.blockIndicesDBStats;
136+
os << "Map Size: " << stats.mapSize << ", Physical File Size: " << stats.physicalFileSize << " Blocks DB "
137+
<< stats.blocksDBStats << ", Nodes DB " << stats.nodesDBStats << ", Leaf Pre-images DB "
138+
<< stats.leafPreimagesDBStats << ", Leaf Indices DB " << stats.leafIndicesDBStats << ", Block Indices DB "
139+
<< stats.blockIndicesDBStats;
127140
return os;
128141
}
129142
};

barretenberg/cpp/src/barretenberg/lmdblib/lmdb_environment.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "barretenberg/lmdblib/lmdb_helpers.hpp"
33
#include "lmdb.h"
44
#include <cstdint>
5+
#include <filesystem>
56
#include <stdexcept>
67
#include <sys/stat.h>
78

@@ -12,6 +13,7 @@ LMDBEnvironment::LMDBEnvironment(const std::string& directory,
1213
uint32_t maxNumDBs,
1314
uint32_t maxNumReaders)
1415
: _id(0)
16+
, _directory(directory)
1517
, _readGuard(maxNumReaders)
1618
, _writeGuard(1) // LMDB only permits one write transaction at a time
1719
{
@@ -71,4 +73,13 @@ uint64_t LMDBEnvironment::get_map_size() const
7173
call_lmdb_func(mdb_env_info, _mdbEnv, &info);
7274
return info.me_mapsize;
7375
}
76+
77+
uint64_t LMDBEnvironment::get_data_file_size() const
78+
{
79+
std::string dataPath = (std::filesystem::path(_directory) / "data.mdb").string();
80+
if (std::filesystem::exists(dataPath)) {
81+
return std::filesystem::file_size(dataPath);
82+
}
83+
return 0;
84+
}
7485
} // namespace bb::lmdblib

barretenberg/cpp/src/barretenberg/lmdblib/lmdb_environment.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ class LMDBEnvironment {
4848

4949
uint64_t get_map_size() const;
5050

51+
uint64_t get_data_file_size() const;
52+
5153
private:
5254
std::atomic_uint64_t _id;
55+
std::string _directory;
5356
MDB_env* _mdbEnv;
5457

5558
struct ResourceGuard {
@@ -82,4 +85,4 @@ class LMDBEnvironment {
8285
ResourceGuard _readGuard;
8386
ResourceGuard _writeGuard;
8487
};
85-
} // namespace bb::lmdblib
88+
} // namespace bb::lmdblib

yarn-project/telemetry-client/src/metrics.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export const WORLD_STATE_SYNC_DURATION = 'aztec.world_state.sync.duration';
150150
export const WORLD_STATE_MERKLE_TREE_SIZE = 'aztec.world_state.merkle_tree_size';
151151
export const WORLD_STATE_DB_SIZE = 'aztec.world_state.db_size';
152152
export const WORLD_STATE_DB_MAP_SIZE = 'aztec.world_state.db_map_size';
153+
export const WORLD_STATE_DB_PHYSICAL_SIZE = 'aztec.world_state.db_physical_size';
153154
export const WORLD_STATE_TREE_SIZE = 'aztec.world_state.tree_size';
154155
export const WORLD_STATE_UNFINALISED_HEIGHT = 'aztec.world_state.unfinalised_height';
155156
export const WORLD_STATE_FINALISED_HEIGHT = 'aztec.world_state.finalised_height';

yarn-project/world-state/src/instrumentation/instrumentation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const durationTrackDenylist = new Set<WorldStateMessageType>([
3131

3232
export class WorldStateInstrumentation {
3333
private dbMapSize: Gauge;
34+
private dbPhysicalSize: Gauge;
3435
private treeSize: Gauge;
3536
private unfinalisedHeight: Gauge;
3637
private finalisedHeight: Gauge;
@@ -50,6 +51,11 @@ export class WorldStateInstrumentation {
5051
valueType: ValueType.INT,
5152
});
5253

54+
this.dbPhysicalSize = meter.createGauge(Metrics.WORLD_STATE_DB_PHYSICAL_SIZE, {
55+
description: `The current physical disk space used for each database`,
56+
valueType: ValueType.INT,
57+
});
58+
5359
this.treeSize = meter.createGauge(Metrics.WORLD_STATE_TREE_SIZE, {
5460
description: `The current number of leaves in each merkle tree`,
5561
valueType: ValueType.INT,
@@ -96,6 +102,9 @@ export class WorldStateInstrumentation {
96102
this.dbMapSize.record(Number(treeDbStats.mapSize), {
97103
[Attributes.MERKLE_TREE_NAME]: MerkleTreeId[tree],
98104
});
105+
this.dbPhysicalSize.record(Number(treeDbStats.physicalFileSize), {
106+
[Attributes.MERKLE_TREE_NAME]: MerkleTreeId[tree],
107+
});
99108
this.treeSize.record(Number(treeMeta.size), {
100109
[Attributes.MERKLE_TREE_NAME]: MerkleTreeId[tree],
101110
});

yarn-project/world-state/src/native/message.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ export interface DBStats {
9696
export interface TreeDBStats {
9797
/** The configured max size of the DB mapping file (effectively the max possible size of the DB) */
9898
mapSize: bigint;
99+
/** The physical file size of the database on disk */
100+
physicalFileSize: bigint;
99101
/** Stats for the 'blocks' DB */
100102
blocksDBStats: DBStats;
101103
/** Stats for the 'nodes' DB */
@@ -151,6 +153,7 @@ export function buildEmptyDBStats() {
151153
export function buildEmptyTreeDBStats() {
152154
return {
153155
mapSize: 0n,
156+
physicalFileSize: 0n,
154157
blocksDBStats: buildEmptyDBStats(),
155158
nodesDBStats: buildEmptyDBStats(),
156159
leafIndicesDBStats: buildEmptyDBStats(),
@@ -242,6 +245,7 @@ export function sanitiseTreeDBStats(stats: TreeDBStats) {
242245
stats.blockIndicesDBStats = sanitiseDBStats(stats.blockIndicesDBStats);
243246
stats.nodesDBStats = sanitiseDBStats(stats.nodesDBStats);
244247
stats.mapSize = BigInt(stats.mapSize);
248+
stats.physicalFileSize = BigInt(stats.physicalFileSize);
245249
return stats;
246250
}
247251

0 commit comments

Comments
 (0)