Hi, I am trying to use a timestamp counter heap, but it always seems to report timestamp zero. Consider this example program:
#include <Metal/Metal.h>
#include <assert.h>
int main(int argc, char *argv[]) {
auto device = MTLCreateSystemDefaultDevice();
assert(device);
auto descriptor = [MTL4CounterHeapDescriptor new];
[descriptor setType:MTL4CounterHeapTypeTimestamp];
[descriptor setCount:1];
auto heap = [device newCounterHeapWithDescriptor:descriptor error:nullptr];
assert(heap);
[heap invalidateCounterRange:NSMakeRange(0, 1)];
auto command_buffer = [device newCommandBuffer];
assert(command_buffer);
auto allocator = [device newCommandAllocator];
assert(allocator);
[command_buffer beginCommandBufferWithAllocator:allocator];
auto encoder = [command_buffer computeCommandEncoder];
assert(encoder);
[encoder writeTimestampWithGranularity:MTL4TimestampGranularityPrecise intoHeap:heap atIndex:0];
[encoder endEncoding];
[command_buffer endCommandBuffer];
auto queue = [device newMTL4CommandQueue];
assert(queue);
auto event = [device newSharedEvent];
assert(event);
[queue commit:&command_buffer count:1];
[queue signalEvent:event value:1];
[event waitUntilSignaledValue:1 timeoutMS:UINT64_MAX];
auto data = [heap resolveCounterRange:NSMakeRange(0, 1)];
printf("size %lu: %llu\n", data.length, *(uint64_t*)data.bytes);
return 0;
}
Trying to compile and run:
% clang++ -g -O0 -o test test.mm -framework Metal -framework Foundation && MTL_DEBUG_LAYER=1 ./test
2026-06-23 14:44:48.006 test[26472:1588857] Metal API Validation Enabled
size 8: 0
I would have expected to receive
size 8: [some random non-zero number]
that number being a GPU timestamp of when the command was executed, but I always get zero. Does anybody have an idea of what I am doing wrong?