Unverified Commit 5834f7fe authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

and more

parent 8e93ea13
......@@ -165,7 +165,9 @@ pub struct TraceSpanInfo {
/// (0 = no pressure, higher = more pressure); `footprint` is the process
/// memory footprint (RSS) in bytes (0 = not reported by the platform); and
/// `active_worker_threads` counts non-parked Tokio scheduler workers.
/// `100 ticks = 1 µs`. Capped and downsampled by the store.
/// `100 ticks = 1 µs`. Capped at 200 entries: beyond that, each group of
/// consecutive samples is represented by its sample with the most
/// committed bytes, kept whole, so a row's columns come from one sample.
pub memory_samples: Vec<Vec<i64>>,
/// Summary of `memorySamples`; absent when the span's range holds none.
/// Unlike the allocation counters these are absolute readings of committed
......@@ -185,7 +187,8 @@ pub struct TraceSpanInfo {
/// Aggregate view of a span's memory samples.
#[napi(object)]
pub struct TraceMemorySummary {
/// Number of samples in the span's range, after downsampling.
/// Number of samples in the span's range. Every figure here is computed
/// from all of them, before `memorySamples` is downsampled.
pub count: u32,
/// Committed bytes at the first sample in the range.
pub start: i64,
......@@ -346,9 +349,10 @@ pub struct TraceTimelinePhase {
pub corrected_duration: i64,
pub memory_at_start: Option<TraceMemoryPoint>,
pub memory_at_end: Option<TraceMemoryPoint>,
/// Largest committed-bytes sample during the phase.
/// Largest committed bytes during the phase, over the samples inside it
/// and the interpolated readings at its edges, so short phases have one.
pub peak_bytes: Option<i64>,
/// Largest footprint sample during the phase.
/// Largest footprint during the phase, like `peakBytes`.
pub peak_footprint: Option<i64>,
}
......
......@@ -866,7 +866,10 @@ export interface TraceMemoryReading {
/** Aggregate view of a span's memory samples. */
export interface TraceMemorySummary {
/** Number of samples in the span's range, after downsampling. */
/**
* Number of samples in the span's range. Every figure here is computed
* from all of them, before `memorySamples` is downsampled.
*/
count: number
/** Committed bytes at the first sample in the range. */
start: number
......@@ -1175,7 +1178,9 @@ export interface TraceSpanInfo {
* (0 = no pressure, higher = more pressure); `footprint` is the process
* memory footprint (RSS) in bytes (0 = not reported by the platform); and
* `active_worker_threads` counts non-parked Tokio scheduler workers.
* `100 ticks = 1 µs`. Capped and downsampled by the store.
* `100 ticks = 1 µs`. Capped at 200 entries: beyond that, each group of
* consecutive samples is represented by its sample with the most
* committed bytes, kept whole, so a row's columns come from one sample.
*/
memorySamples: Array<Array<number>>
/**
......@@ -1213,9 +1218,12 @@ export interface TraceTimelinePhase {
correctedDuration: number
memoryAtStart?: TraceMemoryPoint
memoryAtEnd?: TraceMemoryPoint
/** Largest committed-bytes sample during the phase. */
/**
* Largest committed bytes during the phase, over the samples inside it
* and the interpolated readings at its edges, so short phases have one.
*/
peakBytes?: number
/** Largest footprint sample during the phase. */
/** Largest footprint during the phase, like `peakBytes`. */
peakFootprint?: number
}
......
......@@ -303,7 +303,7 @@ function renderMemoryTimelineMarkdown(
const gap = timeline.maxGap.footprint - timeline.maxGap.bytes
md += `- **Largest RSS − committed gap:** ${gap >= 0 ? '' : '-'}${formatBytes(Math.abs(gap))} ${reading(timeline.maxGap)}\n`
}
md += `- **Samples:** ${timeline.samples.length} shown of ${timeline.rawSampleCount} (downsampled groups keep their max)\n`
md += `- **Samples:** ${timeline.samples.length} shown of ${timeline.rawSampleCount} (each row is a whole sample: the one with the most committed bytes in its group, so its columns belong together; the peaks above use every sample)\n`
if (timeline.phases.length > 0) {
md += `\n### Phases (${timeline.phases.length} with the largest corrected duration, in start order)\n\n`
......@@ -486,7 +486,7 @@ export async function startTurboTraceServerCli(
'',
'Allocations: `allocations` / `deallocations` / `allocationCount` and `persistentAllocations`, each with a `self*` counterpart excluding children. Comparing a total to its `self` shows whether a span allocates directly or only through descendants.',
'',
'`persistentAllocations` ranks allocators; it is NOT retained memory. It is allocated-minus-freed per TurboMalloc counters, which never see turbo-tasks cell or cache drops, so a total far above real peak RSS is expected rather than a leak. For absolute memory use `memorySummary` (count/start/end/min/peak/maxPressure/maxFootprint, precomputed from `memorySamples`) or `memoryAtStart` / `memoryAtEnd`, which are interpolated between samples and so are present even for spans too short to contain one (outside the sampled range they are the nearest sample). For the memory curve of a whole build, use `memory_timeline`.',
'`persistentAllocations` ranks allocators; it is NOT retained memory. It is allocated-minus-freed per TurboMalloc counters, which never see turbo-tasks cell or cache drops, so a total far above real peak RSS is expected rather than a leak. For absolute memory use `memorySummary` (count/start/end/min/peak/maxPressure/maxFootprint, computed from every sample in the span time range; `memorySamples` is downsampled to at most 200 whole samples, the one with the most committed bytes per group) or `memoryAtStart` / `memoryAtEnd`, which are interpolated between samples and so are present even for spans too short to contain one (outside the sampled range they are the nearest sample). For the memory curve of a whole build, use `memory_timeline`.',
'',
MEMORY_DESCRIPTION,
'',
......@@ -537,7 +537,7 @@ export async function startTurboTraceServerCli(
.number()
.optional()
.describe(
"Levels of descendants to include inline in each span's `children`. Default 1 (no nesting), capped at 32."
"Levels of descendants to include inline in each span's `children`. Default 1 (no nesting), capped at 32. With `fields`, `children` is added to them when this is greater than 1."
),
page: z.number().optional().describe('1-based page number. Default 1.'),
pageSize: z
......@@ -556,7 +556,14 @@ export async function startTurboTraceServerCli(
},
},
(args) => {
const fields = args.fields
// Nesting is what `depth` asks for, so don't let a mask without
// `children` silently drop it.
const fields =
args.fields &&
(args.depth ?? 1) > 1 &&
!args.fields.includes('children')
? [...args.fields, 'children' as const]
: args.fields
const result = bindings.turbo.queryTraceSpans(handle, {
parent: args.parent,
aggregated: args.aggregated ?? true,
......@@ -714,7 +721,7 @@ export async function startTurboTraceServerCli(
.number()
.optional()
.describe(
'Samples to downsample to. Default 100, capped at 2000. Each downsampled group keeps its max.'
'Samples to downsample to. Default 100, capped at 2000. Each downsampled group is represented by its sample with the most committed bytes, kept whole, so the RSS and gap in a row belong to that sample.'
),
maxPhases: z
.number()
......
......@@ -426,11 +426,19 @@ describe('turbopack-trace-server', () => {
maxPressure: number
}
expect(summary).toBeDefined()
expect(summary.count).toBe(samples.length)
expect(summary.start).toBe(samples[0][1])
expect(summary.end).toBe(samples[samples.length - 1][1])
// The summary covers every sample, the series at most 200 of them, each
// the one with the most committed bytes in its group, so the peak
// survives downsampling.
expect(summary.count).toBeGreaterThanOrEqual(samples.length)
expect(summary.peak).toBe(Math.max(...samples.map((x) => x[1])))
expect(summary.min).toBe(Math.min(...samples.map((x) => x[1])))
expect(summary.min).toBeLessThanOrEqual(
Math.min(...samples.map((x) => x[1]))
)
if (summary.count === samples.length) {
expect(summary.start).toBe(samples[0][1])
expect(summary.end).toBe(samples[samples.length - 1][1])
expect(summary.min).toBe(Math.min(...samples.map((x) => x[1])))
}
})
// ─── recursive search ────────────────────────────────────────────────────
......@@ -674,6 +682,19 @@ describe('turbopack-trace-server', () => {
)
})
it('should keep nesting when fields omits children but depth asks for it', async () => {
const nested = await querySpansJson(mcpPort, {
sort: 'value',
depth: 2,
fields: ['name'],
})
const top = nested.spans[0]
expect(Array.isArray(top.children)).toBe(true)
for (const child of top.children as SpanData[]) {
expect(Object.keys(child).sort()).toEqual(['children', 'id', 'name'])
}
})
// ─── flat profile ────────────────────────────────────────────────────────
it('should return a flat profile grouped by name', async () => {
......@@ -734,6 +755,27 @@ describe('turbopack-trace-server', () => {
expect(md).toContain('## Memory timeline of whole trace')
})
it('should downsample the memory timeline to whole samples', async () => {
const all = await callToolJson<MemoryTimelineResult>(
mcpPort,
'memory_timeline',
{ maxSamples: 2000 }
)
if (all.rawSampleCount > 2000 || all.rawSampleCount < 4) return
const few = await callToolJson<MemoryTimelineResult>(
mcpPort,
'memory_timeline',
// The same range: in dev the trace keeps growing with new samples.
{ maxSamples: 3, start: all.start, end: all.end }
)
expect(few.samples.length).toBeLessThanOrEqual(3)
// Every downsampled row is one real sample, not columns from several.
const real = new Set(all.samples.map((row) => row.join(',')))
for (const row of few.samples) {
expect(real.has(row.join(','))).toBe(true)
}
})
// ─── CLI tests ───────────────────────────────────────────────────────────
it('CLI: should return root-level spans', async () => {
......
......@@ -481,7 +481,8 @@ fn memory_timeline_reports_peaks_gap_and_phases() {
assert_eq!(
phases,
[
("task", 0, ticks(10), Some(100)),
// Memory rises after the sample at 0 µs: 180 interpolated at 10 µs.
("task", 0, ticks(10), Some(180)),
("task", ticks(10), ticks(90), Some(500)),
]
);
......@@ -492,6 +493,23 @@ fn memory_timeline_reports_peaks_gap_and_phases() {
format!("{build}-{}", spans.task_a.get())
);
// c.js runs from 20 to 30 µs, between the samples at 0 and 50 µs, so its
// peak comes from the readings interpolated at its edges.
let short = memory_timeline(
&store,
MemoryTimelineOptions {
parent: Some(spans.analyze_b.get().to_string()),
start: None,
end: None,
max_samples: None,
max_phases: None,
},
);
assert_eq!(short.raw_sample_count, 1, "only the sample at 50 µs");
let c = &short.phases[0];
assert_eq!((c.start, c.end), (ticks(20), ticks(30)));
assert_eq!((c.peak_bytes, c.peak_footprint), (Some(340), Some(1600)));
// Phases are the ones with the largest corrected duration.
let longest = memory_timeline(
&store,
......
......@@ -271,10 +271,11 @@ pub struct SpanInfo {
/// `active_worker_threads` counts non-parked Tokio scheduler workers.
/// `100 ticks = 1 µs`.
///
/// The store caps the series at `MAX_MEMORY_SAMPLES`; when more samples
/// exist in the range, consecutive groups are merged: the timestamp and
/// value come from the group's max-memory sample, while pressure,
/// footprint and worker count are each the max over the group.
/// The series is capped at `MAX_MEMORY_SAMPLES`; when more samples exist
/// in the range, each group of consecutive ones is represented by its
/// sample with the most committed bytes, kept whole, so the columns of a
/// row always come from the same sample. Peaks of the other columns can
/// fall in rows that were dropped, so take those from `memory_summary`.
pub memory_samples: Vec<(u64, u64, u8, u64, u64)>,
/// Summary of `memory_samples`. `None` when the span's range holds none.
pub memory_summary: Option<MemorySummary>,
......@@ -297,7 +298,8 @@ pub struct SpanInfo {
/// actually in use.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemorySummary {
/// Number of samples in the span's range (after the store's downsampling).
/// Number of samples in the span's range. Every figure here is computed
/// from all of them, before `memory_samples` is downsampled.
pub count: usize,
/// Committed bytes at the first sample in the range.
pub start: u64,
......@@ -316,7 +318,7 @@ pub struct MemorySummary {
impl MemorySummary {
/// Summarize a sample series, or `None` if it is empty.
fn from_samples(samples: &[(u64, u64, u8, u64, u64)]) -> Option<Self> {
fn from_samples(samples: &[store::MemorySample]) -> Option<Self> {
let (_, first_bytes, first_pressure, first_footprint, _) = *samples.first()?;
let mut summary = MemorySummary {
count: samples.len(),
......@@ -713,17 +715,22 @@ struct SpanMemory {
}
impl SpanMemory {
/// `include_samples` keeps the series; the summary is computed either way.
/// `include_samples` keeps the series; the summary is computed either way,
/// from every sample rather than the downsampled series.
fn of(store: &store::Store, span: &SpanRef<'_>, include_samples: bool) -> Self {
let mut samples: Vec<_> = store
.memory_samples_for_range_with_ts(span.start(), span.end())
.into_iter()
.map(|(ts, mem, pressure, footprint, workers)| (*ts, mem, pressure, footprint, workers))
.collect();
let summary = MemorySummary::from_samples(&samples);
if !include_samples {
samples = Vec::new();
}
let summary =
MemorySummary::from_samples(store.memory_samples_slice(span.start(), span.end()));
let samples = if include_samples {
store
.memory_sample_rows_for_range(span.start(), span.end(), store::MAX_MEMORY_SAMPLES)
.into_iter()
.map(|(ts, mem, pressure, footprint, workers)| {
(*ts, mem, pressure, footprint, workers)
})
.collect()
} else {
Vec::new()
};
SpanMemory {
summary,
samples,
......@@ -1542,9 +1549,9 @@ mod tests {
#[test]
fn memory_summary_reports_peak_not_last() {
let samples = [
(0u64, 100u64, 0u8, 5000u64, 2u64),
(1, 900, 3, 4000, 1),
(2, 200, 1, 7000, 3),
(Timestamp::from_value(0), 100u64, 0u8, 5000u64, 2u64),
(Timestamp::from_value(1), 900, 3, 4000, 1),
(Timestamp::from_value(2), 200, 1, 7000, 3),
];
let summary = MemorySummary::from_samples(&samples).expect("samples present");
assert_eq!(summary.count, 3);
......
......@@ -62,9 +62,11 @@ pub struct TimelinePhase {
pub memory_at_start: Option<MemoryPoint>,
/// Interpolated memory at `end`.
pub memory_at_end: Option<MemoryPoint>,
/// Largest committed-bytes sample between `start` and `end`.
/// Largest committed bytes during the phase: of the samples inside it and
/// the interpolated readings at its edges, so short phases have one too.
/// `None` only without any samples.
pub peak_bytes: Option<u64>,
/// Largest footprint sample between `start` and `end`.
/// Largest footprint during the phase, like `peak_bytes`.
pub peak_footprint: Option<u64>,
}
......@@ -138,7 +140,7 @@ pub fn memory_timeline(store: &StoreContainer, options: MemoryTimelineOptions) -
.map(reading);
let samples = store_ref
.memory_samples_for_range_downsampled(start, end, max_samples)
.memory_sample_rows_for_range(start, end, max_samples)
.into_iter()
.map(|(ts, mem, pressure, footprint, workers)| (*ts, mem, pressure, footprint, workers))
.collect();
......@@ -158,17 +160,31 @@ pub fn memory_timeline(store: &StoreContainer, options: MemoryTimelineOptions) -
.map(|span| {
let (cat, title) = span.nice_name();
let (phase_start, phase_end) = (span.start(), span.end());
let in_phase = store_ref.memory_samples_slice(phase_start, phase_end);
let memory_at_start = MemoryPoint::at(store_ref, phase_start);
let memory_at_end = MemoryPoint::at(store_ref, phase_end);
// The samples inside the phase plus its interpolated edges, so a
// phase too short to contain a sample still gets a peak.
let readings: Vec<(u64, u64)> = store_ref
.memory_samples_slice(phase_start, phase_end)
.iter()
.map(|&(_, bytes, _, footprint, _)| (bytes, footprint))
.chain(
[memory_at_start, memory_at_end]
.into_iter()
.flatten()
.map(|point| (point.bytes, point.footprint)),
)
.collect();
TimelinePhase {
id: build_span_id(options.parent.as_deref(), &span.index.to_string()),
name: format_span_name(cat, title),
start: *phase_start,
end: *phase_end,
corrected_duration: *span.corrected_total_time(),
memory_at_start: MemoryPoint::at(store_ref, phase_start),
memory_at_end: MemoryPoint::at(store_ref, phase_end),
peak_bytes: in_phase.iter().map(|sample| sample.1).max(),
peak_footprint: in_phase.iter().map(|sample| sample.3).max(),
memory_at_start,
memory_at_end,
peak_bytes: readings.iter().map(|&(bytes, _)| bytes).max(),
peak_footprint: readings.iter().map(|&(_, footprint)| footprint).max(),
}
})
.collect();
......
......@@ -32,7 +32,7 @@ const CUT_OFF_DEPTH: u32 = 80;
pub(crate) type MemorySample = (Timestamp, u64, u8, u64, u64);
/// Maximum number of memory samples returned in a query result.
const MAX_MEMORY_SAMPLES: usize = 200;
pub(crate) const MAX_MEMORY_SAMPLES: usize = 200;
/// Maximum number of equal-duration concurrency segments in a span query.
const MAX_CONCURRENCY_SAMPLES: usize = 200;
......@@ -410,31 +410,19 @@ impl Store {
start: Timestamp,
end: Timestamp,
) -> Vec<MemorySample> {
self.memory_samples_for_range_downsampled(start, end, MAX_MEMORY_SAMPLES)
}
/// Like [`Self::memory_samples_for_range_with_ts`], but downsamples to at
/// most `max_samples` instead of `MAX_MEMORY_SAMPLES`.
pub fn memory_samples_for_range_downsampled(
&self,
start: Timestamp,
end: Timestamp,
max_samples: usize,
) -> Vec<MemorySample> {
let max_samples = max_samples.max(1);
let slice = self.memory_samples_slice(start, end);
let count = slice.len();
if count == 0 {
return Vec::new();
}
if count <= max_samples {
if count <= MAX_MEMORY_SAMPLES {
return slice.to_vec();
}
// Merge groups of N samples: timestamp and memory come from the
// max-memory sample, pressure, footprint and workers are column maxes.
let n = count.div_ceil(max_samples);
let n = count.div_ceil(MAX_MEMORY_SAMPLES);
slice
.chunks(n)
.map(|chunk| {
......@@ -447,6 +435,36 @@ impl Store {
.collect()
}
/// Up to `max_samples` samples in `[start, end]`. When there are more,
/// consecutive groups are merged into the sample with the most memory in
/// each, kept whole.
///
/// Unlike [`Self::memory_samples_for_range_with_ts`], which takes each
/// column's max separately for the viewer's per-signal series, every row
/// here is a real sample, so its columns can be compared with each other
/// (e.g. footprint minus memory). A group's own peak footprint can be in a
/// different row, so compute peaks from [`Self::memory_samples_slice`].
#[allow(
dead_code,
reason = "only used by the query API in lib.rs, not the binary"
)]
pub fn memory_sample_rows_for_range(
&self,
start: Timestamp,
end: Timestamp,
max_samples: usize,
) -> Vec<MemorySample> {
let slice = self.memory_samples_slice(start, end);
let max_samples = max_samples.max(1);
if slice.len() <= max_samples {
return slice.to_vec();
}
slice
.chunks(slice.len().div_ceil(max_samples))
.map(|chunk| *chunk.iter().max_by_key(|(_, mem, ..)| *mem).unwrap())
.collect()
}
/// Returns up to `MAX_MEMORY_SAMPLES` active worker thread counts in the
/// range `[start, end]`, with the same length and group boundaries as
/// [`Self::memory_samples_for_range`]. Each group is downsampled by taking
......@@ -788,6 +806,31 @@ mod tests {
);
}
#[test]
fn downsampled_rows_are_whole_samples() {
let mut store = Store::new();
// In each pair, one sample has the most memory and the other the
// largest footprint.
for i in 0..10u64 {
let (mem, footprint) = if i % 2 == 0 {
(100 + i, 1)
} else {
(1, 500 + i)
};
store.add_memory_sample(Timestamp::from_micros(i), mem, 0, footprint, 1);
}
let rows =
store.memory_sample_rows_for_range(Timestamp::ZERO, Timestamp::from_micros(9), 5);
assert_eq!(rows.len(), 5);
for (i, &(ts, mem, _, footprint, _)) in rows.iter().enumerate() {
let i = 2 * i as u64;
assert_eq!(
(ts, mem, footprint),
(Timestamp::from_micros(i), 100 + i, 1)
);
}
}
fn ts(micros: u64) -> Timestamp {
Timestamp::from_micros(micros)
}
......
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