Path: blob/master/tools/tracing/rtla/sample/timerlat_load.py
26285 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.0-only2#3# Copyright (C) 2024 Red Hat, Inc. Daniel Bristot de Oliveira <[email protected]>4#5# This is a sample code about how to use timerlat's timer by any workload6# so rtla can measure and provide auto-analysis for the overall latency (IOW7# the response time) for a task.8#9# Before running it, you need to dispatch timerlat with -U option in a terminal.10# Then # run this script pinned to a CPU on another terminal. For example:11#12# timerlat_load.py 1 -p 9513#14# The "Timerlat IRQ" is the IRQ latency, The thread latency is the latency15# for the python process to get the CPU. The Ret from user Timer Latency is16# the overall latency. In other words, it is the response time for that17# activation.18#19# This is just an example, the load is reading 20MB of data from /dev/full20# It is in python because it is easy to read :-)2122import argparse23import sys24import os2526parser = argparse.ArgumentParser(description='user-space timerlat thread in Python')27parser.add_argument("cpu", type=int, help='CPU to run timerlat thread')28parser.add_argument("-p", "--prio", type=int, help='FIFO priority')29args = parser.parse_args()3031try:32affinity_mask = {args.cpu}33os.sched_setaffinity(0, affinity_mask)34except Exception as e:35print(f"Error setting affinity: {e}")36sys.exit(1)3738if args.prio:39try:40param = os.sched_param(args.prio)41os.sched_setscheduler(0, os.SCHED_FIFO, param)42except Exception as e:43print(f"Error setting priority: {e}")44sys.exit(1)4546try:47timerlat_path = f"/sys/kernel/tracing/osnoise/per_cpu/cpu{args.cpu}/timerlat_fd"48timerlat_fd = open(timerlat_path, 'r')49except PermissionError:50print("Permission denied. Please check your access rights.")51sys.exit(1)52except OSError:53print("Error opening timerlat fd, did you run timerlat -U?")54sys.exit(1)5556try:57data_fd = open("/dev/full", 'r')58except Exception as e:59print(f"Error opening data fd: {e}")60sys.exit(1)6162while True:63try:64timerlat_fd.read(1)65data_fd.read(20 * 1024 * 1024)66except KeyboardInterrupt:67print("Leaving")68break69except IOError as e:70print(f"I/O error occurred: {e}")71break72except Exception as e:73print(f"Unexpected error: {e}")74break7576timerlat_fd.close()77data_fd.close()787980