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