Unverified Commit 91b3570e authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Record size metrics to our compaction and persistence spans (#94977)

Add data about persistence and compaction size to our spans

This will show up in trace data collected by `NEXT_TURBOPACK_TRACING`
and in the default trace spans collected by next.js
parent 6dc3a5a7
...@@ -30,7 +30,7 @@ mod tests; ...@@ -30,7 +30,7 @@ mod tests;
pub use arc_bytes::ArcBytes; pub use arc_bytes::ArcBytes;
pub use compression::checksum_block; pub use compression::checksum_block;
pub use db::{CompactConfig, MetaFileEntryInfo, MetaFileInfo, TurboPersistence}; pub use db::{CommitStats, CompactConfig, MetaFileEntryInfo, MetaFileInfo, TurboPersistence};
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FamilyKind { pub enum FamilyKind {
......
...@@ -379,6 +379,11 @@ impl MetaFile { ...@@ -379,6 +379,11 @@ impl MetaFile {
self.family self.family
} }
/// The on-disk size of this meta file in bytes (the length of its memory map).
pub fn byte_size(&self) -> u64 {
self.mmap.len() as u64
}
pub fn entries(&self) -> &[MetaEntry] { pub fn entries(&self) -> &[MetaEntry] {
&self.entries &self.entries
} }
......
...@@ -44,14 +44,16 @@ impl<'a> MetaFileBuilder<'a> { ...@@ -44,14 +44,16 @@ impl<'a> MetaFileBuilder<'a> {
} }
#[tracing::instrument(level = "trace", skip_all)] #[tracing::instrument(level = "trace", skip_all)]
pub fn write(self, db_path: &Path, seq: u32) -> Result<File> { pub fn write(self, db_path: &Path, seq: u32) -> Result<(File, u64)> {
let file = db_path.join(format!("{seq:08}.meta")); let file = db_path.join(format!("{seq:08}.meta"));
self.write_internal(&file) self.write_internal(&file)
.with_context(|| format!("Unable to write meta file {seq:08}.meta")) .with_context(|| format!("Unable to write meta file {seq:08}.meta"))
} }
fn write_internal(mut self, file: &Path) -> io::Result<File> { fn write_internal(mut self, file: &Path) -> io::Result<(File, u64)> {
let mut file = BufWriter::new(File::create(file)?); // Wrap the writer to count the bytes written, so callers can accumulate written-byte totals
// without stat'ing the file afterwards.
let mut file = CountingWriter::new(BufWriter::new(File::create(file)?));
file.write_u32::<BE>(0xFE4ADA4A)?; // Magic number file.write_u32::<BE>(0xFE4ADA4A)?; // Magic number
file.write_u32::<BE>(self.family)?; file.write_u32::<BE>(self.family)?;
...@@ -93,6 +95,44 @@ impl<'a> MetaFileBuilder<'a> { ...@@ -93,6 +95,44 @@ impl<'a> MetaFileBuilder<'a> {
if let Some(bytes) = &serialized_used_key_hashes { if let Some(bytes) = &serialized_used_key_hashes {
file.write_all(bytes)?; file.write_all(bytes)?;
} }
Ok(file.into_inner()?) let bytes_written = file.bytes_written();
let file = file.into_inner().into_inner()?;
Ok((file, bytes_written))
}
}
/// A [`Write`] adapter that counts the total number of bytes written through it, so writers can
/// report their on-disk size without an extra `stat`/`stream_position` syscall.
struct CountingWriter<W> {
inner: W,
bytes_written: u64,
}
impl<W> CountingWriter<W> {
fn new(inner: W) -> Self {
Self {
inner,
bytes_written: 0,
}
}
fn bytes_written(&self) -> u64 {
self.bytes_written
}
fn into_inner(self) -> W {
self.inner
}
}
impl<W: Write> Write for CountingWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let n = self.inner.write(buf)?;
self.bytes_written += n as u64;
Ok(n)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
} }
} }
...@@ -28,6 +28,15 @@ use crate::{ ...@@ -28,6 +28,15 @@ use crate::{
static_sorted_file_builder::{StaticSortedFileBuilderMeta, write_static_stored_file}, static_sorted_file_builder::{StaticSortedFileBuilderMeta, write_static_stored_file},
}; };
/// A newly created database file (meta, SST, or blob), carrying its on-disk size so commit can sum
/// written bytes without stat'ing the files afterwards.
pub(crate) struct NewFile {
pub(crate) seq: u32,
pub(crate) file: File,
/// On-disk size in bytes.
pub(crate) size: u64,
}
/// The thread local state of a `WriteBatch`. `FAMILIES` should fit within a `u32`. /// The thread local state of a `WriteBatch`. `FAMILIES` should fit within a `u32`.
// //
// NOTE: This type *must* use `usize`, even though the real type used in storage is `u32` because // NOTE: This type *must* use `usize`, even though the real type used in storage is `u32` because
...@@ -37,8 +46,7 @@ struct ThreadLocalState<K: StoreKey + Send, const FAMILIES: usize> { ...@@ -37,8 +46,7 @@ struct ThreadLocalState<K: StoreKey + Send, const FAMILIES: usize> {
/// The collectors for each family. /// The collectors for each family.
collectors: [Option<Collector<K, THREAD_LOCAL_SIZE_SHIFT>>; FAMILIES], collectors: [Option<Collector<K, THREAD_LOCAL_SIZE_SHIFT>>; FAMILIES],
/// The list of new blob files that have been created. /// The list of new blob files that have been created.
/// Tuple of (sequence number, file). new_blob_files: Vec<NewFile>,
new_blob_files: Vec<(u32, File)>,
} }
const COLLECTOR_SHARDS: usize = 4; const COLLECTOR_SHARDS: usize = 4;
...@@ -48,12 +56,9 @@ const COLLECTOR_SHARD_SHIFT: usize = ...@@ -48,12 +56,9 @@ const COLLECTOR_SHARD_SHIFT: usize =
/// The result of a `WriteBatch::finish` operation. /// The result of a `WriteBatch::finish` operation.
pub(crate) struct FinishResult { pub(crate) struct FinishResult {
pub(crate) sequence_number: u32, pub(crate) sequence_number: u32,
/// Tuple of (sequence number, file). pub(crate) new_meta_files: Vec<NewFile>,
pub(crate) new_meta_files: Vec<(u32, File)>, pub(crate) new_sst_files: Vec<NewFile>,
/// Tuple of (sequence number, file). pub(crate) new_blob_files: Vec<NewFile>,
pub(crate) new_sst_files: Vec<(u32, File)>,
/// Tuple of (sequence number, file).
pub(crate) new_blob_files: Vec<(u32, File)>,
/// Number of keys written in this batch. /// Number of keys written in this batch.
pub(crate) keys_written: u64, pub(crate) keys_written: u64,
} }
...@@ -86,8 +91,7 @@ pub struct WriteBatch<'db, K: StoreKey + Send, S: ParallelScheduler, const FAMIL ...@@ -86,8 +91,7 @@ pub struct WriteBatch<'db, K: StoreKey + Send, S: ParallelScheduler, const FAMIL
/// Meta file builders for each family. /// Meta file builders for each family.
meta_collectors: [Mutex<Vec<(u32, StaticSortedFileBuilderMeta<'static>)>>; FAMILIES], meta_collectors: [Mutex<Vec<(u32, StaticSortedFileBuilderMeta<'static>)>>; FAMILIES],
/// The list of new SST files that have been created. /// The list of new SST files that have been created.
/// Tuple of (sequence number, file). new_sst_files: Mutex<Vec<NewFile>>,
new_sst_files: Mutex<Vec<(u32, File)>>,
} }
impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize> impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize>
...@@ -234,9 +238,9 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -234,9 +238,9 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
if value.len() <= MAX_MEDIUM_VALUE_SIZE { if value.len() <= MAX_MEDIUM_VALUE_SIZE {
collector.put(key, value); collector.put(key, value);
} else { } else {
let (blob, file) = self.create_blob(&value)?; let blob = self.create_blob(&value)?;
collector.put_blob(key, blob); collector.put_blob(key, blob.seq);
state.new_blob_files.push((blob, file)); state.new_blob_files.push(blob);
} }
Ok(()) Ok(())
} }
...@@ -425,8 +429,8 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -425,8 +429,8 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
let accessed_key_hashes = get_accessed_key_hashes(family); let accessed_key_hashes = get_accessed_key_hashes(family);
builder.set_used_key_hashes_amqf(accessed_key_hashes); builder.set_used_key_hashes_amqf(accessed_key_hashes);
let seq = self.current_sequence_number.fetch_add(1, Ordering::SeqCst) + 1; let seq = self.current_sequence_number.fetch_add(1, Ordering::SeqCst) + 1;
let file = builder.write(&self.db_path, seq)?; let (file, size) = builder.write(&self.db_path, seq)?;
Ok((seq, file)) Ok(NewFile { seq, file, size })
}, },
)?; )?;
...@@ -442,9 +446,8 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -442,9 +446,8 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
} }
/// Creates a new blob file with the given value. /// Creates a new blob file with the given value.
/// Returns a tuple of (sequence number, file).
#[tracing::instrument(level = "trace", skip(self, value), fields(value_len = value.len()))] #[tracing::instrument(level = "trace", skip(self, value), fields(value_len = value.len()))]
fn create_blob(&self, value: &[u8]) -> Result<(u32, File)> { fn create_blob(&self, value: &[u8]) -> Result<NewFile> {
let seq = self.current_sequence_number.fetch_add(1, Ordering::SeqCst) + 1; let seq = self.current_sequence_number.fetch_add(1, Ordering::SeqCst) + 1;
let mut compressed = Vec::new(); let mut compressed = Vec::new();
compress_into_buffer(value, &mut compressed) compress_into_buffer(value, &mut compressed)
...@@ -455,22 +458,22 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -455,22 +458,22 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
buffer.write_u32::<BE>(checksum_block(&compressed))?; buffer.write_u32::<BE>(checksum_block(&compressed))?;
buffer.extend_from_slice(&compressed); buffer.extend_from_slice(&compressed);
let size = buffer.len() as u64;
let file = self.db_path.join(format!("{seq:08}.blob")); let file = self.db_path.join(format!("{seq:08}.blob"));
let mut file = File::create(&file).context("Unable to create blob file")?; let mut file = File::create(&file).context("Unable to create blob file")?;
file.write_all(&buffer) file.write_all(&buffer)
.context("Unable to write blob file")?; .context("Unable to write blob file")?;
file.flush().context("Unable to flush blob file")?; file.flush().context("Unable to flush blob file")?;
Ok((seq, file)) Ok(NewFile { seq, file, size })
} }
/// Creates a new SST file with the given collector data. /// Creates a new SST file with the given collector data.
/// Returns a tuple of (sequence number, file).
#[tracing::instrument(level = "trace", skip(self, collector_data), fields(family_name = self.family_configs[usize_from_u32(family)].name))] #[tracing::instrument(level = "trace", skip(self, collector_data), fields(family_name = self.family_configs[usize_from_u32(family)].name))]
fn create_sst_file( fn create_sst_file(
&self, &self,
family: u32, family: u32,
collector_data: (&[CollectorEntry<K>], usize), collector_data: (&[CollectorEntry<K>], usize),
) -> Result<(u32, File)> { ) -> Result<NewFile> {
let (entries, _total_key_size) = collector_data; let (entries, _total_key_size) = collector_data;
let seq = self.current_sequence_number.fetch_add(1, Ordering::SeqCst) + 1; let seq = self.current_sequence_number.fetch_add(1, Ordering::SeqCst) + 1;
...@@ -568,11 +571,12 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize ...@@ -568,11 +571,12 @@ impl<'db, K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize
} }
} }
let size = meta.size;
self.meta_collectors[usize_from_u32(family)] self.meta_collectors[usize_from_u32(family)]
.lock() .lock()
.push((seq, meta)); .push((seq, meta));
Ok((seq, file)) Ok(NewFile { seq, file, size })
} }
} }
......
...@@ -59,6 +59,10 @@ pub struct SnapshotMeta { ...@@ -59,6 +59,10 @@ pub struct SnapshotMeta {
pub data_items: usize, pub data_items: usize,
pub meta_items: usize, pub meta_items: usize,
pub task_cache_items: usize, pub task_cache_items: usize,
/// Physical on-disk bytes written by the commit.
pub bytes_written: u64,
/// Physical on-disk bytes of files removed by the commit.
pub bytes_deleted: u64,
pub max_next_task_id: u32, pub max_next_task_id: u32,
} }
...@@ -69,7 +73,28 @@ impl SnapshotMeta { ...@@ -69,7 +73,28 @@ impl SnapshotMeta {
data_items: self.data_items + rhs.data_items, data_items: self.data_items + rhs.data_items,
meta_items: self.meta_items + rhs.meta_items, meta_items: self.meta_items + rhs.meta_items,
task_cache_items: self.task_cache_items + rhs.task_cache_items, task_cache_items: self.task_cache_items + rhs.task_cache_items,
bytes_written: self.bytes_written + rhs.bytes_written,
bytes_deleted: self.bytes_deleted + rhs.bytes_deleted,
max_next_task_id: max(self.max_next_task_id, rhs.max_next_task_id), max_next_task_id: max(self.max_next_task_id, rhs.max_next_task_id),
} }
} }
} }
impl std::fmt::Display for SnapshotMeta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let SnapshotMeta {
data_items,
meta_items,
task_cache_items,
bytes_written,
bytes_deleted,
max_next_task_id,
} = self;
write!(
f,
"data_items={data_items} meta_items={meta_items} task_cache_items={task_cache_items} \
bytes_written={bytes_written} bytes_deleted={bytes_deleted} \
next_task_id={max_next_task_id}"
)
}
}
...@@ -8,7 +8,8 @@ use std::{ ...@@ -8,7 +8,8 @@ use std::{
use anyhow::{Ok, Result}; use anyhow::{Ok, Result};
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_persistence::{ use turbo_persistence::{
ArcBytes, CompactConfig, DbConfig, KeyBase, StoreKey, TurboPersistence, ValueBuffer, ArcBytes, CommitStats, CompactConfig, DbConfig, KeyBase, StoreKey, TurboPersistence,
ValueBuffer,
}; };
use turbo_tasks::{ use turbo_tasks::{
message_queue::{TimingEvent, TraceEvent}, message_queue::{TimingEvent, TraceEvent},
...@@ -125,11 +126,11 @@ impl TurboKeyValueDatabase { ...@@ -125,11 +126,11 @@ impl TurboKeyValueDatabase {
/// Triggers compaction of the database. /// Triggers compaction of the database.
/// ///
/// Returns `Ok(true)` if compaction actually merged files, `Ok(false)` if there was nothing /// Returns `Ok(Some(stats))` with the bytes written/deleted if compaction actually merged
/// to compact. /// files, `Ok(None)` if there was nothing to compact.
pub fn compact(&self) -> Result<bool> { pub fn compact(&self) -> Result<Option<CommitStats>> {
if self.is_short_session || self.db.is_empty() { if self.is_short_session || self.db.is_empty() {
return Ok(false); return Ok(None);
} }
do_compact( do_compact(
&self.db, &self.db,
...@@ -169,16 +170,16 @@ fn do_compact( ...@@ -169,16 +170,16 @@ fn do_compact(
db: &TurboPersistence<TurboTasksParallelScheduler, FAMILIES>, db: &TurboPersistence<TurboTasksParallelScheduler, FAMILIES>,
message: &'static str, message: &'static str,
max_merge_segment_count: usize, max_merge_segment_count: usize,
) -> Result<bool> { ) -> Result<Option<CommitStats>> {
let start = Instant::now(); let start = Instant::now();
// SystemTime for wall-clock timestamps in trace events (Instant has no // SystemTime for wall-clock timestamps in trace events (Instant has no
// defined epoch so it can't be used for cross-process trace correlation). // defined epoch so it can't be used for cross-process trace correlation).
let wall_start = SystemTime::now(); let wall_start = SystemTime::now();
let ran = db.compact(&CompactConfig { let stats = db.compact(&CompactConfig {
max_merge_segment_count, max_merge_segment_count,
..COMPACT_CONFIG ..COMPACT_CONFIG
})?; })?;
if ran { if let Some(stats) = stats {
let elapsed = start.elapsed(); let elapsed = start.elapsed();
// avoid spamming the event queue with information about fast operations // avoid spamming the event queue with information about fast operations
if elapsed > Duration::from_secs(10) { if elapsed > Duration::from_secs(10) {
...@@ -195,10 +196,13 @@ fn do_compact( ...@@ -195,10 +196,13 @@ fn do_compact(
"turbopack-compaction", "turbopack-compaction",
wall_start_ms, wall_start_ms,
wall_end_ms, wall_end_ms,
vec![], serde_json::json!([
["bytes_written", stats.bytes_written],
["bytes_deleted", stats.bytes_deleted],
]),
))); )));
} }
Ok(ran) Ok(stats)
} }
pub struct TurboWriteBatch<'a> { pub struct TurboWriteBatch<'a> {
...@@ -216,9 +220,8 @@ impl<'a> TurboWriteBatch<'a> { ...@@ -216,9 +220,8 @@ impl<'a> TurboWriteBatch<'a> {
self.db.get(key_space as usize, &key) self.db.get(key_space as usize, &key)
} }
pub fn commit(self) -> Result<()> { pub fn commit(self) -> Result<CommitStats> {
self.db.commit_write_batch(self.batch)?; self.db.commit_write_batch(self.batch)
Ok(())
} }
pub fn put( pub fn put(
......
...@@ -8,6 +8,7 @@ use std::{ ...@@ -8,6 +8,7 @@ use std::{
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_bincode::{new_turbo_bincode_decoder, turbo_bincode_decode, turbo_bincode_encode}; use turbo_bincode::{new_turbo_bincode_decoder, turbo_bincode_decode, turbo_bincode_encode};
use turbo_persistence::CommitStats;
use turbo_tasks::{ use turbo_tasks::{
DynTaskInputs, RawVc, TaskId, DynTaskInputs, RawVc, TaskId,
macro_helpers::NativeFunction, macro_helpers::NativeFunction,
...@@ -236,7 +237,7 @@ impl TurboBackingStorage { ...@@ -236,7 +237,7 @@ impl TurboBackingStorage {
{ {
let _span = tracing::trace_span!("update task data").entered(); let _span = tracing::trace_span!("update task data").entered();
let snapshot_meta = let mut snapshot_meta =
parallel::map_collect_owned::<_, _, Result<Vec<_>>>(snapshots, |shard: I| { parallel::map_collect_owned::<_, _, Result<Vec<_>>>(snapshots, |shard: I| {
let mut max_new_task_id = 0; let mut max_new_task_id = 0;
let mut data_items = 0; let mut data_items = 0;
...@@ -282,6 +283,10 @@ impl TurboBackingStorage { ...@@ -282,6 +283,10 @@ impl TurboBackingStorage {
data_items, data_items,
meta_items, meta_items,
task_cache_items, task_cache_items,
// The on-disk byte totals aren't known until the batch is committed below;
// they're filled in from `CommitStats` after `batch.commit()`.
bytes_written: 0,
bytes_deleted: 0,
max_next_task_id: max_new_task_id, max_next_task_id: max_new_task_id,
}) })
})? })?
...@@ -306,7 +311,11 @@ impl TurboBackingStorage { ...@@ -306,7 +311,11 @@ impl TurboBackingStorage {
save_infra(&batch, next_task_id, operations)?; save_infra(&batch, next_task_id, operations)?;
{ {
let _span = tracing::trace_span!("commit").entered(); let _span = tracing::trace_span!("commit").entered();
batch.commit().context("Unable to commit operations")?; // Byte totals are the physical on-disk bytes (post-compression, including .sst /
// .blob / .meta files) produced and removed by the commit.
let stats = batch.commit().context("Unable to commit operations")?;
snapshot_meta.bytes_written = stats.bytes_written;
snapshot_meta.bytes_deleted = stats.bytes_deleted;
} }
Ok(snapshot_meta) Ok(snapshot_meta)
} }
...@@ -395,7 +404,7 @@ impl TurboBackingStorage { ...@@ -395,7 +404,7 @@ impl TurboBackingStorage {
.collect::<Result<Vec<_>>>() .collect::<Result<Vec<_>>>()
} }
pub(crate) fn compact(&self) -> Result<bool> { pub(crate) fn compact(&self) -> Result<Option<CommitStats>> {
self.inner.database.compact() self.inner.database.compact()
} }
......
...@@ -244,7 +244,8 @@ pub struct TraceEvent { ...@@ -244,7 +244,8 @@ pub struct TraceEvent {
pub name: &'static str, pub name: &'static str,
pub start_time_ms: f64, pub start_time_ms: f64,
pub end_time_ms: f64, pub end_time_ms: f64,
pub attributes: Vec<(&'static str, serde_json::Value)>, /// Should be an array of key value pairs
pub attributes: serde_json::Value,
} }
impl TraceEvent { impl TraceEvent {
...@@ -252,8 +253,10 @@ impl TraceEvent { ...@@ -252,8 +253,10 @@ impl TraceEvent {
name: &'static str, name: &'static str,
start_time_ms: f64, start_time_ms: f64,
end_time_ms: f64, end_time_ms: f64,
attributes: Vec<(&'static str, serde_json::Value)>, attributes: serde_json::Value,
) -> Self { ) -> Self {
// basic sanity test
debug_assert!(matches!(attributes, serde_json::Value::Array(_)));
Self { Self {
name, name,
start_time_ms, start_time_ms,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment