Add WiVRn-native Perfetto via Perfetto SDK

Optional server profiling built on the Perfetto SDK. Adds CPU spans
(compositor, encoder, network), GPU encode/copy slices from Vulkan timestamp
queries (Vulkan/NVENC/VAAPI), and headset feedback markers from dump_time.

WIVRN_TRACING enables tracing and selects the backend (system, inprocess, or
both); WIVRN_USE_PERFETTO gates it at build time and compiles it out otherwise.
WIVRN_TRACE_MONADO additionally drives Monado's percetto tracing from the same
switch so WiVRn and Monado land on one timeline.

Tooling: server-tracing[-monado] presets and a CI build, two Perfetto configs
(WiVRn-only and full-system), a wivrn_capture.py capture wrapper, a pftrace
summary script, and docs/profiling.md.
This commit is contained in:
Jonathan Steffan
2026-07-01 21:04:08 +02:00
committed by xytovl
parent b11655fdee
commit b9c8a54d92
34 changed files with 1968 additions and 58 deletions
@@ -20,6 +20,7 @@
#include "video_encoder_ffmpeg.h"
#include "util/u_logging.h"
#include "utils/wivrn_trace.h"
extern "C"
{
@@ -83,9 +84,13 @@ std::optional<wivrn::video_encoder::data> video_encoder_ffmpeg::encode(uint8_t s
bool is_idr = idr_handler.get_type(frame_index) == default_idr_handler::frame_type::i;
push_frame(is_idr, slot);
std::shared_ptr<AVPacket> enc_pkt(av_packet_alloc(), [](AVPacket * d) { av_packet_free(&d); });
int err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt.get());
int err;
{
wivrn::trace::scope trace_send_recv(wivrn::trace::cpu_track::encoder, stream_idx, frame_index, "avcodec_send+receive");
push_frame(is_idr, slot);
err = avcodec_receive_packet(encoder_ctx.get(), enc_pkt.get());
}
if (err == 0)
{
return data{
+20 -1
View File
@@ -23,7 +23,9 @@
#include "encoder/encoder_settings.h"
#include "os/os_time.h"
#include "util/u_logging.h"
#include "utils/wivrn_trace.h"
#include "utils/wivrn_vk_bundle.h"
#include <drm_fourcc.h>
@@ -430,9 +432,11 @@ video_encoder_va::video_encoder_va(wivrn::vk_bundle & vk,
vk.device.bindImageMemory2(bind_info);
}
}
ts_pool = gpu_timestamp_pool(vk, vk.queue.family_index, num_slots, std::format("va encoder {} pixel copy", stream_idx));
}
void video_encoder_va::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo, uint8_t slot, uint64_t)
void video_encoder_va::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo, uint8_t slot, uint64_t frame_index)
{
if (vk.device.waitForFences(*in[slot].fence, true, 1'000'000'000) == vk::Result::eTimeout)
{
@@ -468,6 +472,8 @@ void video_encoder_va::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo,
auto & cmd = in[slot].cmd;
cmd.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
ts_pool.cmd_begin(cmd, slot, frame_index, vk::PipelineStageFlagBits2::eTopOfPipe);
cmd.pipelineBarrier(
vk::PipelineStageFlagBits::eAllCommands,
vk::PipelineStageFlagBits::eTransfer,
@@ -534,6 +540,8 @@ void video_encoder_va::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo,
nullptr,
im_barriers);
ts_pool.cmd_end(cmd, slot, vk::PipelineStageFlagBits2::eAllTransfer);
cmd.end();
std::unique_lock lock(vk.queue.mutex);
@@ -553,6 +561,17 @@ void video_encoder_va::push_frame(bool idr, uint8_t slot)
{
if (vk.device.waitForFences(*in[slot].fence, true, 1'000'000'000) == vk::Result::eTimeout)
throw std::runtime_error("timeout");
if (auto s = ts_pool.collect(slot))
{
wivrn::trace::gpu_slice(wivrn::trace::gpu_track::va_copy,
"vk_copy_luma_chroma",
s->begin_ns,
s->end_ns,
s->frame_index,
stream_idx);
}
auto & va_frame = in[slot].va_frame;
va_frame->pict_type = idr ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
int err = avcodec_send_frame(encoder_ctx.get(), va_frame.get());
+3
View File
@@ -20,6 +20,7 @@
#pragma once
#include "ffmpeg_helper.h"
#include "utils/gpu_timestamp_pool.h"
#include "video_encoder_ffmpeg.h"
#include <array>
@@ -47,6 +48,8 @@ class video_encoder_va : public video_encoder_ffmpeg
av_buffer_ptr drm_frame_ctx;
std::array<in_t, num_slots> in;
gpu_timestamp_pool ts_pool;
public:
video_encoder_va(wivrn::vk_bundle &, const wivrn::encoder_settings & settings, uint8_t stream_index);
+13 -5
View File
@@ -24,6 +24,7 @@
#include "encoder_settings.h"
#include "os/os_time.h"
#include "utils/wivrn_trace.h"
#include "wivrn_config.h"
#include <string>
@@ -234,6 +235,7 @@ void video_encoder::set_framerate(float framerate)
void video_encoder::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo info, uint64_t frame_index)
{
wivrn::trace::scope trace_present(wivrn::trace::cpu_track::encoder, stream_idx, frame_index, "present_image");
// Wait for encoder to be done
present_slot = (present_slot + 1) % num_slots;
state[present_slot].wait(busy);
@@ -271,6 +273,7 @@ void video_encoder::encode(wivrn_session & cnx,
this->cnx = &cnx;
clock = cnx.get_offset();
wivrn::trace::scope trace_encode(wivrn::trace::cpu_track::encoder, stream_idx, frame_index, "encode");
auto encode_begin = os_monotonic_get_ns();
timing_info = {
.encode_begin = clock.to_headset(encode_begin),
@@ -297,6 +300,13 @@ void video_encoder::encode(wivrn_session & cnx,
void video_encoder::SendData(std::span<uint8_t> data, bool end_of_frame, bool control)
{
std::lock_guard lock(mutex);
if (shard.shard_idx == 0)
{
// One SendData call per NAL; span the whole frame, not each call.
wivrn::trace::cpu_begin(wivrn::trace::cpu_track::network, stream_idx, shard.frame_idx, "SendData");
cnx->dump_time("send_begin", shard.frame_idx, os_monotonic_get_ns(), stream_idx);
timing_info.send_begin = clock.to_headset(os_monotonic_get_ns());
}
if (end_of_frame)
{
timing_info.send_end = clock.to_headset(os_monotonic_get_ns());
@@ -305,11 +315,6 @@ void video_encoder::SendData(std::span<uint8_t> data, bool end_of_frame, bool co
}
if (video_dump)
video_dump.write((char *)data.data(), data.size());
if (shard.shard_idx == 0)
{
cnx->dump_time("send_begin", shard.frame_idx, os_monotonic_get_ns(), stream_idx);
timing_info.send_begin = clock.to_headset(os_monotonic_get_ns());
}
ssize_t max_payload_size = (cnx->has_stream() and not control) ? to_headset::video_stream_data_shard::max_payload_size : std::numeric_limits<uint32_t>::max();
@@ -341,7 +346,10 @@ void video_encoder::SendData(std::span<uint8_t> data, bool end_of_frame, bool co
begin = next;
}
if (end_of_frame)
{
cnx->dump_time("send_end", shard.frame_idx, os_monotonic_get_ns(), stream_idx);
wivrn::trace::cpu_end(wivrn::trace::cpu_track::network, stream_idx, shard.frame_idx, "SendData");
}
}
} // namespace wivrn
+23 -4
View File
@@ -20,7 +20,9 @@
#include "video_encoder_nvenc.h"
#include "encoder_settings.h"
#include "os/os_time.h"
#include "util/u_logging.h"
#include "utils/wivrn_trace.h"
#include "utils/wivrn_vk_bundle.h"
bool operator==(const GUID & l, const GUID & r)
@@ -410,6 +412,8 @@ video_encoder_nvenc::video_encoder_nvenc(
i.nvenc_resource = resource_params.registeredResource;
}
CU_CHECK(shared_state->cuda_fn->cuCtxPopCurrent(NULL));
ts_pool = gpu_timestamp_pool(vk, vk.queue.family_index, num_slots, std::format("nvenc encoder {} pixel copy", stream_idx));
}
video_encoder_nvenc::~video_encoder_nvenc()
@@ -418,7 +422,7 @@ video_encoder_nvenc::~video_encoder_nvenc()
shared_state->fn.nvEncDestroyEncoder(session_handle);
}
void video_encoder_nvenc::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo, uint8_t slot, uint64_t)
void video_encoder_nvenc::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo, uint8_t slot, uint64_t frame_index)
{
if (vk.device.waitForFences(*in[slot].fence, true, 1'000'000'000) == vk::Result::eTimeout)
{
@@ -428,6 +432,8 @@ void video_encoder_nvenc::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInf
auto & cmd = in[slot].cmd;
cmd.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
ts_pool.cmd_begin(cmd, slot, frame_index, vk::PipelineStageFlagBits2::eTopOfPipe);
cmd.copyImageToBuffer(
y_cbcr,
vk::ImageLayout::eGeneral,
@@ -459,6 +465,7 @@ void video_encoder_nvenc::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInf
.depth = 1,
},
}});
ts_pool.cmd_end(cmd, slot, vk::PipelineStageFlagBits2::eAllTransfer);
cmd.end();
std::unique_lock lock(vk.queue.mutex);
@@ -482,6 +489,16 @@ std::optional<video_encoder::data> video_encoder_nvenc::encode(uint8_t slot, uin
return {};
}
if (auto s = ts_pool.collect(slot))
{
wivrn::trace::gpu_slice(wivrn::trace::gpu_track::nvenc_copy,
"vk_copy_image_to_buffer",
s->begin_ns,
s->end_ns,
s->frame_index,
stream_idx);
}
CU_CHECK(shared_state->cuda_fn->cuCtxPushCurrent(shared_state->cuda));
auto & idr_handler = ((default_idr_handler &)*idr);
@@ -557,14 +574,16 @@ std::optional<video_encoder::data> video_encoder_nvenc::encode(uint8_t slot, uin
frame_params.pictureType = NV_ENC_PIC_TYPE_UNKNOWN;
break;
}
NVENC_CHECK(shared_state->fn.nvEncEncodePicture(session_handle, &frame_params));
NV_ENC_LOCK_BITSTREAM buf_lock_params{
.version = NV_ENC_LOCK_BITSTREAM_VER,
.doNotWait = 0,
.outputBitstream = outputBuffer,
};
NVENC_CHECK(shared_state->fn.nvEncLockBitstream(session_handle, &buf_lock_params));
{
wivrn::trace::scope trace_nvenc(wivrn::trace::cpu_track::encoder, stream_idx, frame_index, "nvEncEncodePicture+Lock");
NVENC_CHECK(shared_state->fn.nvEncEncodePicture(session_handle, &frame_params));
NVENC_CHECK(shared_state->fn.nvEncLockBitstream(session_handle, &buf_lock_params));
}
if (buf_lock_params.pictureType == NV_ENC_PIC_TYPE_NONREF_P)
idr_handler.set_non_ref(frame_index);
+3
View File
@@ -19,6 +19,7 @@
#pragma once
#include "utils/gpu_timestamp_pool.h"
#include "video_encoder.h"
#include "video_encoder_nvenc_shared_state.h"
#include <array>
@@ -53,6 +54,8 @@ private:
};
std::array<in_t, num_slots> in;
gpu_timestamp_pool ts_pool;
float fps;
uint64_t bitrate;
int bytesPerPixel = 1;
+59
View File
@@ -20,7 +20,9 @@
#include "encoder/encoder_settings.h"
#include "inplace_vector.hpp"
#include "os/os_time.h"
#include "util/u_logging.h"
#include "utils/wivrn_trace.h"
#include "utils/wivrn_vk_bundle.h"
#include <iostream>
@@ -565,6 +567,15 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
vk.name(query_pool, std::format("vulkan encoder {} query pool", stream_idx));
}
// Each pool is bound to the queue family that records its timestamps; pools
// whose corresponding code path never runs stay dormant.
ts_pool = gpu_timestamp_pool(vk, encode_queue.family_index, num_slots, std::format("vulkan encoder {} encode", stream_idx));
if (tmp_image)
ts_pool_image_copy = gpu_timestamp_pool(vk, encode_queue.family_index, num_slots, std::format("vulkan encoder {} image-copy", stream_idx));
const uint32_t host_copy_family = vk.transfer_queue ? vk.transfer_queue.family_index : encode_queue.family_index;
ts_pool_host_copy = gpu_timestamp_pool(vk, host_copy_family, num_slots, std::format("vulkan encoder {} host-copy", stream_idx));
ts_pool_host_copy_overflow = gpu_timestamp_pool(vk, host_copy_family, num_slots, std::format("vulkan encoder {} host-copy-overflow", stream_idx));
// command pools
{
video_command_pool = vk.device.createCommandPool(
@@ -685,6 +696,7 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.pImageMemoryBarriers = im_barriers.data(),
});
ts_pool_image_copy.cmd_begin(video_cmd_buf, encode_slot, frame_index, vk::PipelineStageFlagBits2::eCopy);
video_cmd_buf.copyImage(
y_cbcr,
target_layout,
@@ -726,6 +738,7 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
},
},
});
ts_pool_image_copy.cmd_end(video_cmd_buf, encode_slot, vk::PipelineStageFlagBits2::eCopy);
vk::ImageMemoryBarrier2 tmp_image_barrier{
.srcStageMask = vk::PipelineStageFlagBits2KHR::eTransfer,
@@ -911,7 +924,9 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
encode_info.setReferenceSlots(ref_slot->info);
video_cmd_buf.beginQuery(*query_pool, encode_slot, {});
ts_pool.cmd_begin(video_cmd_buf, encode_slot, frame_index, vk::PipelineStageFlagBits2::eVideoEncodeKHR);
video_cmd_buf.encodeVideoKHR(encode_info);
ts_pool.cmd_end(video_cmd_buf, encode_slot, vk::PipelineStageFlagBits2::eVideoEncodeKHR);
video_cmd_buf.endQuery(*query_pool, encode_slot);
video_cmd_buf.endVideoCodingKHR(vk::VideoEndCodingInfoKHR{});
@@ -943,12 +958,14 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
cmd.pipelineBarrier2(dep_info);
}
ts_pool_host_copy.cmd_begin(cmd, encode_slot, frame_index, vk::PipelineStageFlagBits2::eCopy);
cmd.copyBuffer(
slot_item.output_buffer,
slot_item.host_buffer,
vk::BufferCopy{
.size = slot_item.copy_size,
});
ts_pool_host_copy.cmd_end(cmd, encode_slot, vk::PipelineStageFlagBits2::eCopy);
cmd.end();
}
else
@@ -964,12 +981,14 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.memoryBarrierCount = 1,
.pMemoryBarriers = &mem_barrier,
});
ts_pool_host_copy.cmd_begin(video_cmd_buf, encode_slot, frame_index, vk::PipelineStageFlagBits2::eCopy);
video_cmd_buf.copyBuffer(
slot_item.output_buffer,
slot_item.host_buffer,
vk::BufferCopy{
.size = slot_item.copy_size,
});
ts_pool_host_copy.cmd_end(video_cmd_buf, encode_slot, vk::PipelineStageFlagBits2::eCopy);
}
}
@@ -1043,6 +1062,34 @@ std::optional<wivrn::video_encoder::data> wivrn::video_encoder_vulkan::encode(ui
std::cerr << "device.getQueryPoolResults: " << vk::to_string(res) << std::endl;
}
if (auto s = ts_pool.collect(encode_slot))
{
wivrn::trace::gpu_slice(wivrn::trace::gpu_track::vulkan_encode,
"encodeVideoKHR",
s->begin_ns,
s->end_ns,
s->frame_index,
stream_idx);
}
if (auto s = ts_pool_image_copy.collect(encode_slot))
{
wivrn::trace::gpu_slice(wivrn::trace::gpu_track::vulkan_image_copy,
"vk_copy_image_tmp",
s->begin_ns,
s->end_ns,
s->frame_index,
stream_idx);
}
if (auto s = ts_pool_host_copy.collect(encode_slot))
{
wivrn::trace::gpu_slice(wivrn::trace::gpu_track::vulkan_host_copy,
"vk_copy_to_host",
s->begin_ns,
s->end_ns,
s->frame_index,
stream_idx);
}
// We don't copy the whole buffer, but an estimate of how much we'll need
// If that wasn't enough, we have to issue a second copy command for the rest
if (slot_item.host_buffer and size > slot_item.copy_size)
@@ -1054,6 +1101,7 @@ std::optional<wivrn::video_encoder::data> wivrn::video_encoder_vulkan::encode(ui
cmd.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
ts_pool_host_copy_overflow.cmd_begin(cmd, encode_slot, frame_index, vk::PipelineStageFlagBits2::eCopy);
cmd.copyBuffer(
slot_item.output_buffer,
slot_item.host_buffer,
@@ -1062,6 +1110,7 @@ std::optional<wivrn::video_encoder::data> wivrn::video_encoder_vulkan::encode(ui
.dstOffset = slot_item.copy_size,
.size = size - slot_item.copy_size,
});
ts_pool_host_copy_overflow.cmd_end(cmd, encode_slot, vk::PipelineStageFlagBits2::eCopy);
cmd.end();
@@ -1086,6 +1135,16 @@ std::optional<wivrn::video_encoder::data> wivrn::video_encoder_vulkan::encode(ui
U_LOG_E("Timeout on stream %d", stream_idx);
return {};
}
if (auto s = ts_pool_host_copy_overflow.collect(encode_slot))
{
wivrn::trace::gpu_slice(wivrn::trace::gpu_track::vulkan_host_copy_overflow,
"vk_copy_to_host_overflow",
s->begin_ns,
s->end_ns,
s->frame_index,
stream_idx);
}
}
void * mapped = slot_item.host_buffer ? slot_item.host_buffer.map() : slot_item.output_buffer.map();
+6
View File
@@ -22,6 +22,7 @@
#include <vulkan/vulkan_raii.hpp>
#include <vulkan/vulkan_structs.hpp>
#include "utils/gpu_timestamp_pool.h"
#include "utils/wivrn_vk_bundle.h"
#include "video_encoder.h"
#include "vk/allocation.h"
@@ -44,6 +45,11 @@ class video_encoder_vulkan : public video_encoder
uint64_t sem_value = 0;
vk::raii::QueryPool query_pool = nullptr;
gpu_timestamp_pool ts_pool;
// Optional per-op GPU timestamp pools; created only when their path runs.
gpu_timestamp_pool ts_pool_image_copy;
gpu_timestamp_pool ts_pool_host_copy;
gpu_timestamp_pool ts_pool_host_copy_overflow;
vk::raii::CommandPool transfer_command_pool = nullptr;
vk::raii::CommandPool video_command_pool = nullptr;