#!/bin/bash
#
# Trace execution of Intel-kms function related to kms-pageflips
# which have to be serialized with rendering into dmabuf's, ie.
# have to wait for a dmabuf_fence being signalled on render-completion
# of an external producer into the new dmabuf framebuffer.
#
# Typical use case: Optimus/Prime config where the Intel gpu does
# the display and pageflip, an external offload gpu does the rendering,
# and intel has to wait for render-completion of the offload gpu into
# the dmabuf, e.g. a NVidia gpu.
#
# legacy page flip: intel_mmio_flip_work_func -> reservation_object_timeout_rcu
# (waits for dmabuf_fence aka offload gpu render completion) -> intel_do_mmio_flip
# executes vsync'ed flip at next vblank.
#
# atomic flip:
# intel_prepare_plane_fb -> reservation_object_wait_timeout_rcu (wait for render completion)
# -> intel_pin_and_fence_fb_obj finalize prep work for fb for a future atomic page flip.

#
# Usage:
# 1. sudo /bin/bash
# 2. Start script with ./i915_optimus_sync_tracing.sh
# 3. Run animation on DRI3/Present Optimus/Prime config with page flipped swaps.
# 4. Look at script output.

# Go to tracing director:
cd /sys/kernel/debug/tracing

# Select function_graph tracer:
echo function > current_tracer

#echo intel_mmio_flip_work_func > set_graph_function
echo > set_graph_function

echo global > trace_clock

# Select driver functions to trace:
#echo nouveau_gem_ioctl_pushbuf intel_crtc_page_flip intel_mmio_flip_work_func reservation_object_wait_timeout_rcu intel_pipe_update_start > set_ftrace_filter
echo radeon_cs_ioctl intel_crtc_page_flip intel_mmio_flip_work_func reservation_object_wait_timeout_rcu intel_pipe_update_start > set_ftrace_filter
#echo radeon_cs_ioctl nouveau_crtc_page_flip fence_wait_timeout > set_ftrace_filter
#echo amdgpu_cs_ioctl intel_crtc_page_flip intel_mmio_flip_work_func reservation_object_wait_timeout_rcu intel_pipe_update_start > set_ftrace_filter

#echo reservation_object_wait_timeout_rcu intel_prepare_plane_fb intel_pin_and_fence_fb_obj > set_ftrace_filter

# Reset trace buffer:
echo 0 > trace

# Start tracing, trace for 20 seconds, Stop tracing:
echo 1 > tracing_on ; sleep 20 ; echo 0 > tracing_on

# Show what you traced:
more trace
