---
title: "sampleCounters(sampleBuffer:sampleIndex:barrier:)"
framework: metal
role: symbol
role_heading: Instance Method
path: "metal/mtlcomputecommandencoder/samplecounters(samplebuffer:sampleindex:barrier:)"
---

# sampleCounters(sampleBuffer:sampleIndex:barrier:)

Encodes a command to sample hardware counters, providing performance information.

## Declaration

```swift
func sampleCounters(sampleBuffer: any MTLCounterSampleBuffer, sampleIndex: Int, barrier: Bool)
```

## Parameters

- `sampleBuffer`: An doc://com.apple.metal/documentation/Metal/MTLCounterSampleBuffer instance that stores the GPU hardware data.
- `sampleIndex`: An index within sampleBuffer the command stores the data to.
- `barrier`: Whether or not the command inserts a barrier before sampling the counter’s data. A barrier ensures that the commands you encode before this one complete before the GPU samples the hardware counters, but can negatively impact runtime performance. Running this command without a barrier means the GPU can sample counters concurrently with other commands from the encoder. The barrier parameter for the command has no impact on sampling commands from other passes.

## Mentioned in

Sampling GPU data into counter sample buffers

## Discussion

Discussion important: To use a sample buffer, it needs to be part of the sampleBufferAttachments on the compute pass descriptor. See GPU counters and counter sample buffers, Sampling GPU data into counter sample buffers, and MTLCounter for more information.
