f8b21af4be
10_Wiki/Topics 대규모 정리: - 오류 캡처/미완성 stub 문서 227개 제거 - 교차폴더 중복 43클러스터 병합 (63파일 → redirect) - 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건 - 카테고리 MOC 6개 신규 생성 - Graph 섹션 미해결 related-keyword 링크 10,058건 제거 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
173 lines
5.5 KiB
Markdown
173 lines
5.5 KiB
Markdown
---
|
||
id: wiki-2026-0508-timestamp-queries
|
||
title: Timestamp Queries
|
||
category: 10_Wiki/Topics
|
||
status: verified
|
||
canonical_id: self
|
||
aliases: [GPU Timestamp Queries, WebGPU Timestamp]
|
||
duplicate_of: none
|
||
source_trust_level: A
|
||
confidence_score: 0.9
|
||
verification_status: applied
|
||
tags: [webgpu, profiling, gpu, performance]
|
||
raw_sources: []
|
||
last_reinforced: 2026-05-10
|
||
github_commit: pending
|
||
tech_stack:
|
||
language: javascript
|
||
framework: 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
|
||
```javascript
|
||
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
|
||
```javascript
|
||
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
|
||
```javascript
|
||
const pass = encoder.beginRenderPass({
|
||
colorAttachments: [...],
|
||
timestampWrites: {
|
||
querySet,
|
||
beginningOfPassWriteIndex: 0,
|
||
endOfPassWriteIndex: 1,
|
||
},
|
||
});
|
||
pass.setPipeline(...);
|
||
pass.draw(...);
|
||
pass.end();
|
||
```
|
||
|
||
### Resolve + readback
|
||
```javascript
|
||
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+)
|
||
```javascript
|
||
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
|
||
```javascript
|
||
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
|
||
```javascript
|
||
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
|
||
- 부모: [[WebGPU]]
|
||
- Adjacent: [[performance.now]]
|
||
|
||
## 🤖 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 |
|