Unverified Commit 08e7e204 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add configurable sample series to query-trace

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 207b08b6
use std::{path::PathBuf, sync::Arc}; use std::{path::PathBuf, sync::Arc};
use napi::{Error, Result};
use napi_derive::napi; use napi_derive::napi;
use turbopack_trace_server::{ use turbopack_trace_server::{
QueryOptions, SortMode, query_spans, start_turbopack_trace_server, QueryOptions, SortMode, query_spans, start_turbopack_trace_server,
...@@ -45,6 +46,9 @@ pub struct TraceQueryOptions { ...@@ -45,6 +46,9 @@ pub struct TraceQueryOptions {
pub page: Option<u32>, pub page: Option<u32>,
/// Spans per page. Default `20`, capped at `500`. /// Spans per page. Default `20`, capped at `500`.
pub page_size: Option<u32>, pub page_size: Option<u32>,
/// Optional maximum values per sample series, including counts above 200.
/// Must be a nonnegative safe integer. Zero requests empty arrays.
pub samples: Option<f64>,
} }
/// Information about a single span or aggregated span group. /// Information about a single span or aggregated span group.
...@@ -146,10 +150,23 @@ pub struct TraceSpanInfo { ...@@ -146,10 +150,23 @@ pub struct TraceSpanInfo {
/// Unlike the allocation counters these are absolute live-heap readings, so /// Unlike the allocation counters these are absolute live-heap readings, so
/// `peak` is the figure to quote for memory actually in use. /// `peak` is the figure to quote for memory actually in use.
pub memory_summary: Option<TraceMemorySummary>, pub memory_summary: Option<TraceMemorySummary>,
/// Requested value arrays; absent unless `samples` was supplied.
/// MCP flattens this internal object and omits the legacy tuples above.
pub sample_series: Option<TraceSpanSampleSeries>,
/// Descendants of this span, populated only when `depth > 1`. /// Descendants of this span, populated only when `depth > 1`.
pub children: Vec<TraceSpanInfo>, pub children: Vec<TraceSpanInfo>,
} }
/// Requested process/global value series. Captured memory/pressure/workers
/// are grouped by recorded timestamps; concurrency uses equal-time segments.
#[napi(object)]
pub struct TraceSpanSampleSeries {
pub memory_samples: Vec<i64>,
pub memory_pressure_samples: Vec<u8>,
pub active_worker_threads_samples: Vec<i64>,
pub concurrency_samples: Vec<f64>,
}
/// Aggregate view of a span's TurboMalloc memory samples. /// Aggregate view of a span's TurboMalloc memory samples.
#[napi(object)] #[napi(object)]
pub struct TraceMemorySummary { pub struct TraceMemorySummary {
...@@ -239,6 +256,20 @@ fn convert_span(s: turbopack_trace_server::SpanInfo) -> TraceSpanInfo { ...@@ -239,6 +256,20 @@ fn convert_span(s: turbopack_trace_server::SpanInfo) -> TraceSpanInfo {
max_pressure: m.max_pressure, max_pressure: m.max_pressure,
max_footprint: m.max_footprint as i64, max_footprint: m.max_footprint as i64,
}), }),
sample_series: s.sample_series.map(|series| TraceSpanSampleSeries {
memory_samples: series
.memory_samples
.into_iter()
.map(|value| value as i64)
.collect(),
memory_pressure_samples: series.memory_pressure_samples,
active_worker_threads_samples: series
.active_worker_threads_samples
.into_iter()
.map(|value| value as i64)
.collect(),
concurrency_samples: series.concurrency_samples,
}),
children: s.children.into_iter().map(convert_span).collect(), children: s.children.into_iter().map(convert_span).collect(),
} }
} }
...@@ -248,7 +279,24 @@ fn convert_span(s: turbopack_trace_server::SpanInfo) -> TraceSpanInfo { ...@@ -248,7 +279,24 @@ fn convert_span(s: turbopack_trace_server::SpanInfo) -> TraceSpanInfo {
pub fn query_trace_spans( pub fn query_trace_spans(
handle: &TraceServerHandle, handle: &TraceServerHandle,
options: TraceQueryOptions, options: TraceQueryOptions,
) -> TraceQueryResult { ) -> Result<TraceQueryResult> {
// NAPI's integer conversions truncate; validate the original JS number instead.
let samples = options
.samples
.map(|count| {
if !count.is_finite()
|| count < 0.0
|| count.fract() != 0.0
|| count > 9_007_199_254_740_991.0
|| count > usize::MAX as f64
{
return Err(Error::from_reason(
"samples must be a nonnegative safe integer representable on this platform",
));
}
Ok(count as usize)
})
.transpose()?;
let result = query_spans( let result = query_spans(
&handle.store, &handle.store,
QueryOptions { QueryOptions {
...@@ -266,13 +314,14 @@ pub fn query_trace_spans( ...@@ -266,13 +314,14 @@ pub fn query_trace_spans(
depth: options.depth.unwrap_or(1), depth: options.depth.unwrap_or(1),
page: options.page.unwrap_or(1) as usize, page: options.page.unwrap_or(1) as usize,
page_size: options.page_size.map(|n| n as usize), page_size: options.page_size.map(|n| n as usize),
samples,
}, },
); );
TraceQueryResult { Ok(TraceQueryResult {
spans: result.spans.into_iter().map(convert_span).collect(), spans: result.spans.into_iter().map(convert_span).collect(),
page: result.page as u32, page: result.page as u32,
total_pages: result.total_pages as u32, total_pages: result.total_pages as u32,
total_count: result.total_count as u32, total_count: result.total_count as u32,
} })
} }
...@@ -36,6 +36,7 @@ import type { ...@@ -36,6 +36,7 @@ import type {
import type { NextBuildOptions } from '../cli/next-build.js' import type { NextBuildOptions } from '../cli/next-build.js'
import type { NextTypegenOptions } from '../cli/next-typegen.js' import type { NextTypegenOptions } from '../cli/next-typegen.js'
import type { NextPostBuildOptions } from '../cli/next-post-build.js' import type { NextPostBuildOptions } from '../cli/next-post-build.js'
import { parseTraceSampleCount } from '../cli/internal/query-trace'
import { ensureProfilesDir } from '../lib/profiles-dir' import { ensureProfilesDir } from '../lib/profiles-dir'
import type { NextRequestInsightsOptions } from '../cli/next-request-insights.js' import type { NextRequestInsightsOptions } from '../cli/next-request-insights.js'
...@@ -853,9 +854,15 @@ internal ...@@ -853,9 +854,15 @@ internal
'Spans per page (default 20, max 500).' 'Spans per page (default 20, max 500).'
).argParser(parseValidPositiveInteger) ).argParser(parseValidPositiveInteger)
) )
.addOption(
new Option(
'--samples <N>',
'Include up to N memory, pressure, active Tokio worker, and concurrency values per span. Omit for summaries only; 0 returns empty arrays.'
).argParser(parseTraceSampleCount)
)
.addHelpText('after', ({ command }) => { .addHelpText('after', ({ command }) => {
const port = (command.opts() as { port?: number }).port ?? 5748 const port = (command.opts() as { port?: number }).port ?? 5748
return `\nExample:\n next internal query-trace --port ${port} --parent <id>` return `\nExample:\n next internal query-trace --port ${port} --parent <id> --samples=200`
}) })
.action((options) => .action((options) =>
import('../cli/internal/query-trace.js').then((mod) => import('../cli/internal/query-trace.js').then((mod) =>
......
...@@ -838,6 +838,11 @@ export interface TraceQueryOptions { ...@@ -838,6 +838,11 @@ export interface TraceQueryOptions {
page?: number page?: number
/** Spans per page. Default `20`, capped at `500`. */ /** Spans per page. Default `20`, capped at `500`. */
pageSize?: number pageSize?: number
/**
* Optional maximum values per sample series, including counts above 200.
* Must be a nonnegative safe integer. Zero requests empty arrays.
*/
samples?: number
} }
/** The result of a `query_trace_spans` call. */ /** The result of a `query_trace_spans` call. */
...@@ -973,10 +978,26 @@ export interface TraceSpanInfo { ...@@ -973,10 +978,26 @@ export interface TraceSpanInfo {
* `peak` is the figure to quote for memory actually in use. * `peak` is the figure to quote for memory actually in use.
*/ */
memorySummary?: TraceMemorySummary memorySummary?: TraceMemorySummary
/**
* Requested value arrays; absent unless `samples` was supplied.
* MCP flattens this internal object and omits the legacy tuples above.
*/
sampleSeries?: TraceSpanSampleSeries
/** Descendants of this span, populated only when `depth > 1`. */ /** Descendants of this span, populated only when `depth > 1`. */
children: Array<TraceSpanInfo> children: Array<TraceSpanInfo>
} }
/**
* Requested process/global value series. Captured memory/pressure/workers
* are grouped by recorded timestamps; concurrency uses equal-time segments.
*/
export interface TraceSpanSampleSeries {
memorySamples: Array<number>
memoryPressureSamples: Array<number>
activeWorkerThreadsSamples: Array<number>
concurrencySamples: Array<number>
}
export declare function transform( export declare function transform(
src: string | Buffer | undefined, src: string | Buffer | undefined,
isModule: boolean, isModule: boolean,
......
...@@ -2,12 +2,26 @@ ...@@ -2,12 +2,26 @@
* CLI client for querying a running turbopack trace server via its MCP endpoint. * CLI client for querying a running turbopack trace server via its MCP endpoint.
* Sends a JSON-RPC `tools/call` request and prints the response to stdout. * Sends a JSON-RPC `tools/call` request and prints the response to stdout.
* Use --json for machine-readable JSON output (default: markdown). * Use --json for machine-readable JSON output (default: markdown).
* Use --samples=N to opt into memory, pressure, worker and concurrency values;
* otherwise only the existing summaries are reported.
* *
* Usage: next internal query-trace [options] * Usage: next internal query-trace [options]
*/ */
import { InvalidArgumentError } from 'next/dist/compiled/commander'
const DEFAULT_MCP_PORT = 5748 // Keep in sync with turbo-trace-server.ts const DEFAULT_MCP_PORT = 5748 // Keep in sync with turbo-trace-server.ts
export function parseTraceSampleCount(value: string): number {
const count = Number(value)
if (!/^\d+$/.test(value) || !Number.isSafeInteger(count) || count < 0) {
throw new InvalidArgumentError(
'Sample count must be a nonnegative safe integer.'
)
}
return count
}
interface QueryTraceOptions { interface QueryTraceOptions {
port: number | undefined port: number | undefined
parent: string | undefined parent: string | undefined
...@@ -19,6 +33,7 @@ interface QueryTraceOptions { ...@@ -19,6 +33,7 @@ interface QueryTraceOptions {
page: number | undefined page: number | undefined
pageSize: number | undefined pageSize: number | undefined
json: boolean | undefined json: boolean | undefined
samples?: number
} }
export async function queryTraceCli(options: QueryTraceOptions): Promise<void> { export async function queryTraceCli(options: QueryTraceOptions): Promise<void> {
...@@ -34,6 +49,7 @@ export async function queryTraceCli(options: QueryTraceOptions): Promise<void> { ...@@ -34,6 +49,7 @@ export async function queryTraceCli(options: QueryTraceOptions): Promise<void> {
if (options.depth !== undefined) args.depth = options.depth if (options.depth !== undefined) args.depth = options.depth
if (options.page !== undefined) args.page = options.page if (options.page !== undefined) args.page = options.page
if (options.pageSize !== undefined) args.pageSize = options.pageSize if (options.pageSize !== undefined) args.pageSize = options.pageSize
if (options.samples !== undefined) args.samples = options.samples
if (options.json) args.outputType = 'json' if (options.json) args.outputType = 'json'
const requestBody = JSON.stringify({ const requestBody = JSON.stringify({
......
import type {
TraceSpanInfo,
TraceSpanSampleSeries,
} from '../../build/swc/generated-native'
/** Public MCP shape: summaries always, value arrays only on explicit request. */
type JsonSpanInfo = Omit<
TraceSpanInfo,
'memorySamples' | 'sampleSeries' | 'children'
> &
Partial<TraceSpanSampleSeries> & { children: JsonSpanInfo[] }
export function serializeTraceSpan(span: TraceSpanInfo): JsonSpanInfo {
const { memorySamples: _summaryRows, sampleSeries, children, ...info } = span
return {
...info,
...sampleSeries,
children: children.map(serializeTraceSpan),
}
}
export function renderSampleSeriesMarkdown(
series: TraceSpanSampleSeries | undefined,
formatBytes: (bytes: number) => string
): string {
if (!series || Object.values(series).every((values) => values.length === 0)) {
return ''
}
let md =
'\n**Sample values:** Memory/pressure/workers use recorded sample groups; concurrency uses equal-time segments. Indices are not shared timestamps.\n'
const metrics: [string, number[], (value: number) => string][] = [
['Memory (TurboMalloc live bytes)', series.memorySamples, formatBytes],
['Memory pressure', series.memoryPressureSamples, (value) => `${value}%`],
['Active Tokio workers', series.activeWorkerThreadsSamples, String],
['Concurrency', series.concurrencySamples, (value) => value.toFixed(2)],
]
for (const [name, values, format] of metrics) {
md += `\n**${name}:**\n\n`
if (values.length === 0) {
md += '_No samples._\n'
continue
}
md += '| Sample | Value |\n| ---: | ---: |\n'
for (const [index, value] of values.entries()) {
md += `| ${index + 1} | ${format(value)} |\n`
}
}
return md
}
...@@ -2,6 +2,10 @@ import http from 'node:http' ...@@ -2,6 +2,10 @@ import http from 'node:http'
import z from 'next/dist/compiled/zod' import z from 'next/dist/compiled/zod'
import { loadBindings } from '../../build/swc' import { loadBindings } from '../../build/swc'
import type { TraceSpanInfo } from '../../build/swc/generated-native' import type { TraceSpanInfo } from '../../build/swc/generated-native'
import {
renderSampleSeriesMarkdown,
serializeTraceSpan,
} from './trace-query-result'
const { McpServer } = const { McpServer } =
require('next/dist/compiled/@modelcontextprotocol/sdk/server/mcp') as typeof import('next/dist/compiled/@modelcontextprotocol/sdk/server/mcp') require('next/dist/compiled/@modelcontextprotocol/sdk/server/mcp') as typeof import('next/dist/compiled/@modelcontextprotocol/sdk/server/mcp')
...@@ -160,6 +164,8 @@ function renderSpanMarkdown( ...@@ -160,6 +164,8 @@ function renderSpanMarkdown(
md += `\n**Process samples (TurboMalloc live bytes, memory pressure, memory footprint, active Tokio workers):** ${memSummary}\n` md += `\n**Process samples (TurboMalloc live bytes, memory pressure, memory footprint, active Tokio workers):** ${memSummary}\n`
} }
md += renderSampleSeriesMarkdown(span.sampleSeries, formatBytes)
if (span.children.length > 0) { if (span.children.length > 0) {
md += '\n' md += '\n'
for (const child of span.children) { for (const child of span.children) {
...@@ -224,15 +230,15 @@ export async function startTurboTraceServerCli( ...@@ -224,15 +230,15 @@ 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.', '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`; maxFootprint is the process RSS peak, 0 = not reported).', '`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 the legacy, at-most-200 max-memory rows, independent of requested sample details; maxFootprint is the process RSS peak, 0 = not reported).',
'', '',
"But live heap is process-wide: a span's samples are just the global series sliced to its time range, so concurrent spans report identical memory however much each allocated. Rank concurrent work by the allocation fields, never by memory.", "But live heap is process-wide: a span's samples are just the global series sliced to its time range, so concurrent spans report identical memory however much each allocated. Rank concurrent work by the allocation fields, never by memory.",
'', '',
"Frees are charged to whichever span was on the stack at free time, not the one that allocated. A child with large `selfAllocations` under a parent with large `selfDeallocations` means the parent drops the child's arena — bounded, not leaking. Large `selfPersistentAllocations` with no such counterpart above it is the shape worth suspecting.", "Frees are charged to whichever span was on the stack at free time, not the one that allocated. A child with large `selfAllocations` under a parent with large `selfDeallocations` means the parent drops the child's arena — bounded, not leaking. Large `selfPersistentAllocations` with no such counterpart above it is the shape worth suspecting.",
'', '',
'For aggregated groups every allocation field is a group total, while `cpuDuration`, `correctedDuration` and `memorySamples` describe the example span only. `firstSpanId` is first in execution order; use `heaviestSpanId` to reach the member holding the most bytes.', 'For aggregated groups every allocation field is a group total, while `cpuDuration`, `correctedDuration`, `memorySummary` and requested samples describe the example span only. `firstSpanId` is first in execution order; use `heaviestSpanId` to reach the member holding the most bytes.',
'', '',
'Set `outputType: "json"` for full precision and the raw `memorySamples` entries `[tsOffsetTicks, bytes, pressure, footprint, active_worker_threads]` (footprint is the process RSS in bytes, 0 = not reported; active_worker_threads excludes parked and blocking-pool threads); markdown is a human summary.', "Set `samples: N` (a nonnegative safe integer, including values above 200) to include up to N values per series: `memorySamples` (TurboMalloc live bytes, not RSS), `memoryPressureSamples` (percent), `activeWorkerThreadsSamples` (non-parked scheduler workers, excluding blocking-pool threads), and `concurrencySamples` (global average self-time concurrency, rounded to hundredths; blocking=true spans do not contribute their own time). Without `samples`, JSON omits all four fields; `samples: 0` includes four empty arrays. Memory/worker rows use each recorded group's peak-memory sample; pressure uses each group's maximum. Concurrency uses equal-time segments, so indices are not shared timestamps. JSON returns number arrays (not the previous timestamped tuples); markdown includes formatted sample tables. `memorySummary.count` still counts its independent at-most-200 summary rows.",
].join('\n'), ].join('\n'),
inputSchema: { inputSchema: {
parent: z parent: z
...@@ -276,11 +282,20 @@ export async function startTurboTraceServerCli( ...@@ -276,11 +282,20 @@ export async function startTurboTraceServerCli(
.number() .number()
.optional() .optional()
.describe('Spans per page. Default 20, capped at 500.'), .describe('Spans per page. Default 20, capped at 500.'),
samples: z
.number()
.int()
.nonnegative()
.max(Number.MAX_SAFE_INTEGER)
.optional()
.describe(
'Maximum values per memory, pressure, active-worker and concurrency series per span. Omit for summaries only; 0 returns empty arrays. Supports counts above 200. Larger values increase response/query cost.'
),
outputType: z outputType: z
.enum(['markdown', 'json']) .enum(['markdown', 'json'])
.optional() .optional()
.describe( .describe(
'Output format. "markdown" (default) returns human-readable markdown. "json" returns structured JSON with all span fields.' 'Output format. "markdown" (default) returns human-readable markdown. "json" returns structured timing, allocation and summary fields; sample arrays are included only when samples is supplied.'
), ),
}, },
}, },
...@@ -294,6 +309,7 @@ export async function startTurboTraceServerCli( ...@@ -294,6 +309,7 @@ export async function startTurboTraceServerCli(
depth: args.depth, depth: args.depth,
page: args.page ?? 1, page: args.page ?? 1,
pageSize: args.pageSize, pageSize: args.pageSize,
samples: args.samples,
}) })
const { spans, page, totalPages, totalCount } = result const { spans, page, totalPages, totalCount } = result
...@@ -303,7 +319,12 @@ export async function startTurboTraceServerCli( ...@@ -303,7 +319,12 @@ export async function startTurboTraceServerCli(
content: [ content: [
{ {
type: 'text', type: 'text',
text: JSON.stringify({ spans, page, totalPages, totalCount }), text: JSON.stringify({
spans: spans.map(serializeTraceSpan),
page,
totalPages,
totalCount,
}),
}, },
], ],
} }
......
import type { TraceSpanInfo } from '../../packages/next/src/build/swc/generated-native'
import {
parseTraceSampleCount,
queryTraceCli,
} from '../../packages/next/src/cli/internal/query-trace'
import {
renderSampleSeriesMarkdown,
serializeTraceSpan,
} from '../../packages/next/src/cli/internal/trace-query-result'
const series = {
memorySamples: [1024, 2048],
memoryPressureSamples: [7, 80],
activeWorkerThreadsSamples: [2, 1],
concurrencySamples: [1.25, 0],
}
function span(overrides: Partial<TraceSpanInfo> = {}): TraceSpanInfo {
return {
id: '1',
name: 'work',
cpuDuration: 100,
correctedDuration: 100,
startRelativeToParent: 0,
endRelativeToParent: 100,
args: [],
isAggregated: false,
allocations: 0,
deallocations: 0,
persistentAllocations: 0,
allocationCount: 0,
selfAllocations: 0,
selfDeallocations: 0,
selfPersistentAllocations: 0,
selfAllocationCount: 0,
memorySamples: [[0, 1024, 7, 2]],
memorySummary: {
count: 1,
start: 1024,
end: 1024,
min: 1024,
peak: 1024,
maxPressure: 7,
},
children: [],
...overrides,
}
}
const emptySeries = {
memorySamples: [],
memoryPressureSamples: [],
activeWorkerThreadsSamples: [],
concurrencySamples: [],
}
const options = {
port: 1234,
parent: undefined,
aggregated: undefined,
sort: undefined,
search: undefined,
maxDepth: undefined,
depth: undefined,
page: undefined,
pageSize: undefined,
json: true,
}
describe('query-trace samples', () => {
const originalFetch = global.fetch
afterEach(() => {
jest.restoreAllMocks()
global.fetch = originalFetch
})
it.each(['0', '1', '300', String(Number.MAX_SAFE_INTEGER)])(
'accepts count %s',
(value) => {
expect(parseTraceSampleCount(value)).toBe(Number(value))
}
)
it.each([
'-1',
'1.5',
'NaN',
'Infinity',
'',
'2x',
'1e2',
' 2 ',
'9007199254740992',
])('rejects count %s', (value) => {
expect(() => parseTraceSampleCount(value)).toThrow(
'nonnegative safe integer'
)
})
it.each([undefined, 0, 1, 300])(
'forwards explicit samples %s without a default',
async (samples) => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
text: async () =>
'data: {"result":{"content":[{"type":"text","text":"{}"}]}}\n',
})
jest.spyOn(process.stdout, 'write').mockImplementation(() => true)
await queryTraceCli({ ...options, samples })
const request = JSON.parse(
jest.mocked(global.fetch).mock.calls[0][1]!.body as string
)
expect(request.params.arguments.outputType).toBe('json')
if (samples === undefined) {
expect(request.params.arguments).not.toHaveProperty('samples')
} else {
expect(request.params.arguments.samples).toBe(samples)
}
}
)
it('omits all sample fields by default, including descendants, without dropping summaries', () => {
const result = serializeTraceSpan(span({ children: [span({ id: '1-2' })] }))
for (const entry of [result, result.children[0]]) {
for (const key of Object.keys(series))
expect(entry).not.toHaveProperty(key)
expect(entry).not.toHaveProperty('sampleSeries')
expect(entry.memorySummary?.peak).toBe(1024)
expect(entry.cpuDuration).toBe(100)
}
})
it('flattens requested value arrays recursively rather than exposing legacy tuples', () => {
const result = serializeTraceSpan(
span({
sampleSeries: series,
isAggregated: true,
count: 2,
firstSpanId: '1',
children: [span({ id: '1-2', sampleSeries: series })],
})
)
for (const entry of [result, result.children[0]]) {
for (const [key, values] of Object.entries(series))
expect(entry).toHaveProperty(key, values)
expect(entry).not.toHaveProperty('sampleSeries')
expect(entry.memorySummary?.count).toBe(1)
}
expect(result.count).toBe(2)
expect(result.firstSpanId).toBe('1')
})
it('returns four empty arrays for zero while keeping the memory summary', () => {
expect(
serializeTraceSpan(span({ sampleSeries: emptySeries }))
).toMatchObject({
...emptySeries,
memorySummary: { peak: 1024 },
})
})
it('prints readable units and explains independent sample grids', () => {
const md = renderSampleSeriesMarkdown(
series,
(value) => `${value / 1024} KB`
)
expect(md).toContain('| 1 | 1 KB |')
expect(md).toContain('| 2 | 80% |')
expect(md).toContain('| 1 | 2 |')
expect(md).toContain('| 1 | 1.25 |')
expect(md).toContain('| 2 | 0.00 |')
expect(md).toContain('Indices are not shared timestamps')
expect(md).toContain('Active Tokio workers')
})
it('does not print sample tables for omitted or zero details', () => {
expect(renderSampleSeriesMarkdown(undefined, String)).toBe('')
expect(renderSampleSeriesMarkdown(emptySeries, String)).toBe('')
})
})
...@@ -74,6 +74,7 @@ fn reads_active_workers_and_exposes_them_in_span_queries() { ...@@ -74,6 +74,7 @@ fn reads_active_workers_and_exposes_them_in_span_queries() {
depth: 1, depth: 1,
page: 1, page: 1,
page_size: None, page_size: None,
samples: None,
}, },
); );
assert_eq!(result.spans.len(), 1); assert_eq!(result.spans.len(), 1);
......
...@@ -410,19 +410,32 @@ impl Store { ...@@ -410,19 +410,32 @@ impl Store {
start: Timestamp, start: Timestamp,
end: Timestamp, end: Timestamp,
) -> Vec<MemorySample> { ) -> Vec<MemorySample> {
self.memory_samples_for_range_with_ts_limit(start, end, MAX_MEMORY_SAMPLES)
}
/// Query-specific limit; the viewer retains its fixed sample cap.
pub fn memory_samples_for_range_with_ts_limit(
&self,
start: Timestamp,
end: Timestamp,
limit: usize,
) -> Vec<MemorySample> {
if limit == 0 {
return Vec::new();
}
let slice = self.memory_samples_slice(start, end); let slice = self.memory_samples_slice(start, end);
let count = slice.len(); let count = slice.len();
if count == 0 { if count == 0 {
return Vec::new(); return Vec::new();
} }
if count <= MAX_MEMORY_SAMPLES { if count <= limit {
return slice.to_vec(); return slice.to_vec();
} }
// Merge groups of N samples: timestamp and memory come from the // Merge groups of N samples: timestamp and memory come from the
// max-memory sample, pressure, footprint and workers are column maxes. // max-memory sample, pressure, footprint and workers are column maxes.
let n = count.div_ceil(MAX_MEMORY_SAMPLES); let n = count.div_ceil(limit);
slice slice
.chunks(n) .chunks(n)
.map(|chunk| { .map(|chunk| {
...@@ -458,17 +471,30 @@ impl Store { ...@@ -458,17 +471,30 @@ impl Store {
/// taking the maximum pressure value, independently of which sample had /// taking the maximum pressure value, independently of which sample had
/// the max memory (matching [`Self::memory_samples_for_range_with_ts`]). /// the max memory (matching [`Self::memory_samples_for_range_with_ts`]).
pub fn memory_pressure_samples_for_range(&self, start: Timestamp, end: Timestamp) -> Vec<u8> { pub fn memory_pressure_samples_for_range(&self, start: Timestamp, end: Timestamp) -> Vec<u8> {
self.memory_pressure_samples_for_range_limit(start, end, MAX_MEMORY_SAMPLES)
}
/// Peak pressure per group, with the same boundaries as the limited memory series.
pub fn memory_pressure_samples_for_range_limit(
&self,
start: Timestamp,
end: Timestamp,
limit: usize,
) -> Vec<u8> {
if limit == 0 {
return Vec::new();
}
let slice = self.memory_samples_slice(start, end); let slice = self.memory_samples_slice(start, end);
let count = slice.len(); let count = slice.len();
if count == 0 { if count == 0 {
return Vec::new(); return Vec::new();
} }
if count <= MAX_MEMORY_SAMPLES { if count <= limit {
return slice.iter().map(|(_, _, p, ..)| *p).collect(); return slice.iter().map(|(_, _, p, ..)| *p).collect();
} }
let n = count.div_ceil(MAX_MEMORY_SAMPLES); let n = count.div_ceil(limit);
slice slice
.chunks(n) .chunks(n)
.map(|chunk| chunk.iter().map(|(_, _, p, ..)| *p).max().unwrap()) .map(|chunk| chunk.iter().map(|(_, _, p, ..)| *p).max().unwrap())
...@@ -505,8 +531,18 @@ impl Store { ...@@ -505,8 +531,18 @@ impl Store {
/// of `[start, end)`. When corrected-time indexing is disabled, there is /// of `[start, end)`. When corrected-time indexing is disabled, there is
/// no tree to query and the series is omitted. /// no tree to query and the series is omitted.
pub fn concurrency_samples_for_range(&self, start: Timestamp, end: Timestamp) -> Vec<f64> { pub fn concurrency_samples_for_range(&self, start: Timestamp, end: Timestamp) -> Vec<f64> {
self.concurrency_samples_for_range_limit(start, end, MAX_CONCURRENCY_SAMPLES)
}
/// Query-specific segment count, independent of recorded memory sample timestamps.
pub fn concurrency_samples_for_range_limit(
&self,
start: Timestamp,
end: Timestamp,
limit: usize,
) -> Vec<f64> {
self.self_time_tree.as_ref().map_or_else(Vec::new, |tree| { self.self_time_tree.as_ref().map_or_else(Vec::new, |tree| {
tree.lookup_range_concurrency_samples(start, end, MAX_CONCURRENCY_SAMPLES) tree.lookup_range_concurrency_samples(start, end, limit)
}) })
} }
...@@ -656,6 +692,96 @@ mod tests { ...@@ -656,6 +692,96 @@ mod tests {
); );
} }
#[test]
fn query_sample_limit_is_not_the_viewer_cap() {
let mut store = Store::new();
let start = Timestamp::ZERO;
let end = Timestamp::from_value(599);
for i in 0..600 {
store.add_memory_sample(Timestamp::from_value(i), i, (i % 100) as u8, 0, i % 8);
}
for limit in [0, 1, 200, 300, 1000] {
let samples = store.memory_samples_for_range_with_ts_limit(start, end, limit);
let pressure = store.memory_pressure_samples_for_range_limit(start, end, limit);
assert!(samples.len() <= limit);
assert_eq!(pressure.len(), samples.len());
if limit > 0 {
assert_eq!(samples.last().unwrap().1, 599);
assert_eq!(samples.last().unwrap().4, 599 % 8);
}
}
assert_eq!(
store
.memory_samples_for_range_with_ts_limit(start, end, 300)
.len(),
300
);
assert_eq!(
store
.memory_samples_for_range_with_ts_limit(start, end, 1000)
.len(),
600
);
assert_eq!(
store.memory_samples_for_range_with_ts_limit(start, end, 1)[0].2,
99
);
assert_eq!(
store.memory_pressure_samples_for_range_limit(start, end, 1),
vec![99]
);
assert!(store.memory_samples_for_range_with_ts(start, end).len() <= MAX_MEMORY_SAMPLES);
assert!(
store
.memory_samples_for_range_with_ts_limit(Timestamp::from_value(600), end, 1)
.is_empty()
);
}
#[test]
fn pressure_and_workers_keep_the_viewer_reduction_semantics() {
let mut store = Store::new();
let start = Timestamp::ZERO;
let end = Timestamp::from_value(10);
store.add_memory_sample(start, 100, 80, 1000, 7);
store.add_memory_sample(end, 900, 2, 200, 1);
let samples = store.memory_samples_for_range_with_ts_limit(start, end, 1);
assert_eq!(samples, vec![(end, 900, 80, 1000, 7)]);
assert_eq!(
store.memory_pressure_samples_for_range_limit(start, end, 1),
vec![80]
);
let mut outdated = FxHashSet::default();
let span = store.add_span(
None,
start,
rcstr!("test"),
rcstr!("work"),
SpanArgs::new(),
&mut outdated,
);
store.add_self_time(span, start, Timestamp::from_value(1000), &mut outdated);
assert_eq!(
store.concurrency_samples_for_range_limit(start, Timestamp::from_value(1000), 300),
vec![1.0; 300]
);
assert!(
store
.concurrency_samples_for_range_limit(start, end, 0)
.is_empty()
);
assert_eq!(
store.concurrency_samples_for_range_limit(start, Timestamp::from_value(2), 300),
vec![1.0; 2]
);
store.self_time_tree = None;
assert!(
store
.concurrency_samples_for_range_limit(start, end, 300)
.is_empty()
);
}
#[test] #[test]
fn downsampled_worker_counts_align_with_memory_groups() { fn downsampled_worker_counts_align_with_memory_groups() {
let mut store = Store::new(); let mut store = Store::new();
......
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