Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Created January 4, 2025 16:09
Show Gist options
  • Save offlinehacker/ce7d71b77edda466e01e660990fa14d4 to your computer and use it in GitHub Desktop.
Save offlinehacker/ce7d71b77edda466e01e660990fa14d4 to your computer and use it in GitHub Desktop.
Simple BCC script to check if hardware accelerated decoding with libva is being used
#!/usr/bin/python3
from bcc import BPF
# I just wanted to check if hardware acceleration for video decoding in firefox is
# actually being used. Thus I wrote this simple BCC script to check whether trace_va_render
# inside libva.so is being called.
# BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
#include <linux/string.h>
#define TASK_COMM_LEN 16
BPF_PERF_OUTPUT(events);
struct data_t {
u64 pid;
u64 timestamp;
char comm[TASK_COMM_LEN];
};
int trace_va_render(struct pt_regs *ctx) {
struct data_t data = {};
data.pid = bpf_get_current_pid_tgid();
data.timestamp = bpf_ktime_get_ns();
bpf_get_current_comm(&data.comm, sizeof(data.comm));
events.perf_submit(ctx, &data, sizeof(data));
return 0;
}
"""
# Initialize BPF
b = BPF(text=bpf_text)
# Attach the probe to vaRenderPicture in libva.so
b.attach_uprobe(name="/var/home/offlinehq/.local/share/flatpak/runtime/org.freedesktop.Platform/x86_64/24.08/fe5c0ca6b765a89629a24cedce80c20b7d15918e5bae4f35c8f698f4f85c8ecf/files/lib/x86_64-linux-gnu/libva.so.2.2200.0", sym="vaRenderPicture", fn_name="trace_va_render")
# Print header
print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "EVENT"))
# Process events
start = 0
def print_event(cpu, data, size):
global start
event = b["events"].event(data)
if start == 0:
start = event.timestamp
time_s = (float(event.timestamp - start)) / 1000000000
print("%-18.9f %-16s %-6d %s" % (time_s, event.comm.decode(),
event.pid, "vaRenderPicture"))
b["events"].open_perf_buffer(print_event)
print("Tracing vaRenderPicture calls... Hit Ctrl+C to end.")
# Loop and print events
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment