summaryrefslogtreecommitdiff
path: root/tools/tracing/rtla/sample/timerlat_load.py
blob: 8cc5eb2d2e69e56ce284a7ba6b54b09d17b6d3c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2024 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
#
# This is a sample code about how to use timerlat's timer by any workload
# so rtla can measure and provide auto-analysis for the overall latency (IOW
# the response time) for a task.
#
# Before running it, you need to dispatch timerlat with -U option in a terminal.
# Then # run this script pinned to a CPU on another terminal. For example:
#
# timerlat_load.py 1 -p 95
#
# The "Timerlat IRQ" is the IRQ latency, The thread latency is the latency
# for the python process to get the CPU. The Ret from user Timer Latency is
# the overall latency. In other words, it is the response time for that
# activation.
#
# This is just an example, the load is reading 20MB of data from /dev/full
# It is in python because it is easy to read :-)

import argparse
import sys
import os

parser = argparse.ArgumentParser(description='user-space timerlat thread in Python')
parser.add_argument("cpu", help='CPU to run timerlat thread')
parser.add_argument("-p", "--prio", help='FIFO priority')

args = parser.parse_args()

try:
    affinity_mask = { int(args.cpu) }
except:
    print("Invalid cpu: " + args.cpu)
    exit(1)

try:
    os.sched_setaffinity(0, affinity_mask);
except:
    print("Error setting affinity")
    exit(1)

if (args.prio):
    try:
        param = os.sched_param(int(args.prio))
        os.sched_setscheduler(0, os.SCHED_FIFO, param)
    except:
        print("Error setting priority")
        exit(1)

try:
    timerlat_path = "/sys/kernel/tracing/osnoise/per_cpu/cpu" + args.cpu + "/timerlat_fd"
    timerlat_fd = open(timerlat_path, 'r')
except:
    print("Error opening timerlat fd, did you run timerlat -U?")
    exit(1)

try:
    data_fd = open("/dev/full", 'r');
except:
    print("Error opening data fd")

while True:
    try:
        timerlat_fd.read(1)
        data_fd.read(20*1024*1024)
    except:
        print("Leaving")
        break

timerlat_fd.close()
data_fd.close()