1#!/usr/bin/env python3 2# SPDX-License-Identifier: GPL-2.0-only 3# 4# Copyright (C) 2024 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org> 5# 6# This is a sample code about how to use timerlat's timer by any workload 7# so rtla can measure and provide auto-analysis for the overall latency (IOW 8# the response time) for a task. 9# 10# Before running it, you need to dispatch timerlat with -U option in a terminal. 11# Then # run this script pinned to a CPU on another terminal. For example: 12# 13# timerlat_load.py 1 -p 95 14# 15# The "Timerlat IRQ" is the IRQ latency, The thread latency is the latency 16# for the python process to get the CPU. The Ret from user Timer Latency is 17# the overall latency. In other words, it is the response time for that 18# activation. 19# 20# This is just an example, the load is reading 20MB of data from /dev/full 21# It is in python because it is easy to read :-) 22 23import argparse 24import sys 25import os 26 27parser = argparse.ArgumentParser(description='user-space timerlat thread in Python') 28parser.add_argument("cpu", type=int, help='CPU to run timerlat thread') 29parser.add_argument("-p", "--prio", type=int, help='FIFO priority') 30args = parser.parse_args() 31 32try: 33 affinity_mask = {args.cpu} 34except: 35 print("Invalid cpu: " + args.cpu) 36 exit(1) 37 38try: 39 os.sched_setaffinity(0, affinity_mask); 40except: 41 print("Error setting affinity") 42 exit(1) 43 44if (args.prio): 45 try: 46 param = os.sched_param(args.prio) 47 os.sched_setscheduler(0, os.SCHED_FIFO, param) 48 except: 49 print("Error setting priority") 50 exit(1) 51 52try: 53 timerlat_path = "/sys/kernel/tracing/osnoise/per_cpu/cpu" + args.cpu + "/timerlat_fd" 54 timerlat_fd = open(timerlat_path, 'r') 55except: 56 print("Error opening timerlat fd, did you run timerlat -U?") 57 exit(1) 58 59try: 60 data_fd = open("/dev/full", 'r'); 61except: 62 print("Error opening data fd") 63 64while True: 65 try: 66 timerlat_fd.read(1) 67 data_fd.read(20*1024*1024) 68 except: 69 print("Leaving") 70 break 71 72timerlat_fd.close() 73data_fd.close() 74