Files
2nd/10_Wiki/Topics/Frontend/Timestamp Queries.md
T
2026-05-10 22:08:15 +09:00

5.7 KiB
Raw Blame History

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-timestamp-queries Timestamp Queries 10_Wiki/Topics verified self
GPU Timestamp Queries
WebGPU Timestamp
none A 0.9 applied
webgpu
profiling
gpu
performance
2026-05-10 pending
language framework
javascript webgpu

Timestamp Queries

매 한 줄

"매 nanosecond-precision GPU timing". WebGPU timestamp-query feature 의 GPU command buffer 안 의 timestamp 의 record 의 pass-level / draw-level latency 의 measure. 매 CPU performance.now() 의 GPU work 의 invisible 의 problem 의 solve, 매 2026 production profiling 의 standard.

매 핵심

매 동작 원리

  • timestamp query type 의 query set 의 allocate.
  • pass.writeTimestamp(querySet, index) 의 command buffer 의 inject.
  • commandEncoder.resolveQuerySet() 의 GPU buffer 의 result 의 write.
  • mapAsync(GPUMapMode.READ) 의 CPU 의 read — 매 BigInt64Array 의 nanosecond.

매 caveat

  • 매 feature flag 의 requiredFeatures: ['timestamp-query'] 의 device 의 request 의 require.
  • 매 timestamp resolution 의 driver-dependent — 매 Chrome 의 100ns granularity 의 quantize (privacy).
  • 매 GPU clock 의 CPU clock 의 unsynced — 매 absolute time 의 X.
  • 매 query result 의 readback 의 1+ frame 의 lag — 매 real-time 의 X 의 best-effort.

매 응용

  1. Render pass / compute pass 의 cost breakdown.
  2. Shader optimization 의 before/after 의 measure.
  3. Adaptive quality (timestamp 의 budget exceed 의 case 의 LOD drop).
  4. Production telemetry 의 GPU bottleneck 의 detect.

💻 패턴

Request feature

const adapter = await navigator.gpu.requestAdapter();
if (!adapter.features.has('timestamp-query')) {
  throw new Error('timestamp-query unsupported');
}
const device = await adapter.requestDevice({
  requiredFeatures: ['timestamp-query'],
});

Create query set + buffer

const querySet = device.createQuerySet({ type: 'timestamp', count: 2 });
const queryBuffer = device.createBuffer({
  size: 16, // 2 × u64
  usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
});
const readBuffer = device.createBuffer({
  size: 16,
  usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});

Inject into render pass

const pass = encoder.beginRenderPass({
  colorAttachments: [...],
  timestampWrites: {
    querySet,
    beginningOfPassWriteIndex: 0,
    endOfPassWriteIndex: 1,
  },
});
pass.setPipeline(...);
pass.draw(...);
pass.end();

Resolve + readback

encoder.resolveQuerySet(querySet, 0, 2, queryBuffer, 0);
encoder.copyBufferToBuffer(queryBuffer, 0, readBuffer, 0, 16);
device.queue.submit([encoder.finish()]);

await readBuffer.mapAsync(GPUMapMode.READ);
const times = new BigInt64Array(readBuffer.getMappedRange());
const deltaNs = Number(times[1] - times[0]);
console.log(`Pass: ${(deltaNs / 1e6).toFixed(2)} ms`);
readBuffer.unmap();

Three.js helper (r170+)

const renderer = new THREE.WebGPURenderer({ trackTimestamp: true });
await renderer.init();

await renderer.renderAsync(scene, camera);
const renderTime = await renderer.resolveTimestampsAsync(THREE.TimestampQuery.RENDER);
console.log(`Render GPU: ${renderTime} ms`);

Multiple pass labels

const querySet = device.createQuerySet({ type: 'timestamp', count: 6 });
// pass A: index 0, 1
// pass B: index 2, 3
// pass C: index 4, 5
// 매 single resolve 의 batch

Ring buffer 의 frame-over-frame

class GpuProfiler {
  constructor(device, frames = 4) {
    this.queries = Array.from({ length: frames }, () => ({
      set: device.createQuerySet({ type: 'timestamp', count: 2 }),
      buf: device.createBuffer({ size: 16, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST }),
      pending: false,
    }));
    this.frame = 0;
  }
  acquire() {
    const slot = this.queries[this.frame % this.queries.length];
    this.frame++;
    return slot;
  }
}

매 결정 기준

상황 Approach
Coarse CPU+GPU 의 frame time performance.now() + requestAnimationFrame
Per-pass GPU breakdown timestamp-query
Shader instruction-level Chrome WebGPU Inspector / RenderDoc
Production telemetry timestamp-query + sampling (1% session)
WebGL fallback EXT_disjoint_timer_query (degraded, deprecated)

기본값: 매 WebGPU app 의 production 의 ring-buffered timestamp-query — 매 1-frame lag 의 accept.

🔗 Graph

🤖 LLM 활용

언제: 매 GPU bottleneck 의 suspect, 매 pass-level cost 의 attribute 의 want. 언제 X: 매 CPU-bound problem (e.g. JS hot loop) — 매 DevTools profiler 의 use.

안티패턴

  • Sync mapAsync await 의 매 frame: 매 pipeline stall — 매 ring buffer 의 use.
  • Single timestamp 의 absolute time 의 interpret: 매 GPU clock 의 CPU 의 unsync.
  • 100ns 의 below 의 reliance: 매 browser 의 quantize — 매 noise.
  • Feature 의 unrequest 의 use: 매 device.createQuerySet 의 throw.
  • Profiler 의 production 의 always-on: 매 overhead minor 의 X 의 budget — 매 sample.

🧪 검증 / 중복

  • Verified (W3C WebGPU spec §22 Queries, Chrome WebGPU release notes).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — query set, ring buffer, Three.js integration