Skip to content

Commit a23c05f

Browse files
author
Daniel Bristot de Oliveira
committed
tools/rtla: Add -U/--user-load option to timerlat
The timerlat tracer provides an interface for any application to wait for the timerlat's periodic wakeup. Currently, rtla timerlat uses it to dispatch its user-space workload (-u option). But as the tracer interface is generic, rtla timerlat can also be used to monitor any workload that uses it. For example, a user might place their own workload to wait on the tracer interface, and monitor the results with rtla timerlat. Add the -U option to rtla timerlat top and hist. With this option, rtla timerlat will not dispatch its workload but only setting up the system, waiting for a user to dispatch its workload. The sample code in this patch is an example of python application that loops in the timerlat tracer fd. To use it, dispatch: # rtla timerlat -U In a terminal, then run the python program on another terminal, specifying the CPU to run it. For example, setting on CPU 1: #./timerlat_load.py 1 Then rtla timerlat will start printing the statistics of the ./timerlat_load.py app. An interesting point is that the "Ret user Timer Latency" value is the overall response time of the load. The sample load does a memory copy to exemplify that. The stop tracing options on rtla timerlat works in this setup as well, including auto analysis. Link: https://lkml.kernel.org/r/36e6bcf18fe15c7601048fd4c65aeb193c502cc8.1707229706.git.bristot@kernel.org Cc: Jonathan Corbet <corbet@lwn.net> Cc: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Daniel Bristot de Oliveira <bristot@kernel.org>
1 parent 012e4e7 commit a23c05f

File tree

4 files changed

+101
-9
lines changed

4 files changed

+101
-9
lines changed

Documentation/tools/rtla/common_timerlat_options.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@
3333
to wait on the timerlat_fd. Once the workload is awakes, it goes to sleep again
3434
adding so the measurement for the kernel-to-user and user-to-kernel to the tracer
3535
output.
36+
37+
**-U**, **--user-load**
38+
39+
Set timerlat to run without workload, waiting for the user to dispatch a per-cpu
40+
task that waits for a new period on the tracing/osnoise/per_cpu/cpu$ID/timerlat_fd.
41+
See linux/tools/rtla/sample/timerlat_load.py for an example of user-load code.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
23+
import argparse
24+
import sys
25+
import os
26+
27+
parser = argparse.ArgumentParser(description='user-space timerlat thread in Python')
28+
parser.add_argument("cpu", help='CPU to run timerlat thread')
29+
parser.add_argument("-p", "--prio", help='FIFO priority')
30+
31+
args = parser.parse_args()
32+
33+
try:
34+
affinity_mask = { int(args.cpu) }
35+
except:
36+
print("Invalid cpu: " + args.cpu)
37+
exit(1)
38+
39+
try:
40+
os.sched_setaffinity(0, affinity_mask);
41+
except:
42+
print("Error setting affinity")
43+
exit(1)
44+
45+
if (args.prio):
46+
try:
47+
param = os.sched_param(int(args.prio))
48+
os.sched_setscheduler(0, os.SCHED_FIFO, param)
49+
except:
50+
print("Error setting priority")
51+
exit(1)
52+
53+
try:
54+
timerlat_path = "/sys/kernel/tracing/osnoise/per_cpu/cpu" + args.cpu + "/timerlat_fd"
55+
timerlat_fd = open(timerlat_path, 'r')
56+
except:
57+
print("Error opening timerlat fd, did you run timerlat -U?")
58+
exit(1)
59+
60+
try:
61+
data_fd = open("/dev/full", 'r');
62+
except:
63+
print("Error opening data fd")
64+
65+
while True:
66+
try:
67+
timerlat_fd.read(1)
68+
data_fd.read(20*1024*1024)
69+
except:
70+
print("Leaving")
71+
break
72+
73+
timerlat_fd.close()
74+
data_fd.close()

tools/tracing/rtla/src/timerlat_hist.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct timerlat_hist_params {
3939
int hk_cpus;
4040
int no_aa;
4141
int dump_tasks;
42+
int user_workload;
4243
int user_hist;
4344
cpu_set_t hk_cpu_set;
4445
struct sched_attr sched_param;
@@ -534,6 +535,7 @@ static void timerlat_hist_usage(char *usage)
534535
" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
535536
" in nanoseconds",
536537
" -u/--user-threads: use rtla user-space threads instead of in-kernel timerlat threads",
538+
" -U/--user-load: enable timerlat for user-defined user-space workload",
537539
NULL,
538540
};
539541

@@ -595,6 +597,7 @@ static struct timerlat_hist_params
595597
{"thread", required_argument, 0, 'T'},
596598
{"trace", optional_argument, 0, 't'},
597599
{"user-threads", no_argument, 0, 'u'},
600+
{"user-load", no_argument, 0, 'U'},
598601
{"event", required_argument, 0, 'e'},
599602
{"no-irq", no_argument, 0, '0'},
600603
{"no-thread", no_argument, 0, '1'},
@@ -613,7 +616,7 @@ static struct timerlat_hist_params
613616
/* getopt_long stores the option index here. */
614617
int option_index = 0;
615618

616-
c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:np:P:s:t::T:u0123456:7:8:9\1",
619+
c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:np:P:s:t::T:uU0123456:7:8:9\1",
617620
long_options, &option_index);
618621

619622
/* detect the end of the options. */
@@ -724,6 +727,9 @@ static struct timerlat_hist_params
724727
params->trace_output = "timerlat_trace.txt";
725728
break;
726729
case 'u':
730+
params->user_workload = 1;
731+
/* fallback: -u implies in -U */
732+
case 'U':
727733
params->user_hist = 1;
728734
break;
729735
case '0': /* no irq */
@@ -985,7 +991,7 @@ int timerlat_hist_main(int argc, char *argv[])
985991
}
986992
}
987993

988-
if (params->cgroup && !params->user_hist) {
994+
if (params->cgroup && !params->user_workload) {
989995
retval = set_comm_cgroup("timerlat/", params->cgroup_name);
990996
if (!retval) {
991997
err_msg("Failed to move threads to cgroup\n");
@@ -1049,7 +1055,7 @@ int timerlat_hist_main(int argc, char *argv[])
10491055
tool->start_time = time(NULL);
10501056
timerlat_hist_set_signals(params);
10511057

1052-
if (params->user_hist) {
1058+
if (params->user_workload) {
10531059
/* rtla asked to stop */
10541060
params_u.should_run = 1;
10551061
/* all threads left */
@@ -1086,14 +1092,14 @@ int timerlat_hist_main(int argc, char *argv[])
10861092
break;
10871093

10881094
/* is there still any user-threads ? */
1089-
if (params->user_hist) {
1095+
if (params->user_workload) {
10901096
if (params_u.stopped_running) {
10911097
debug_msg("timerlat user-space threads stopped!\n");
10921098
break;
10931099
}
10941100
}
10951101
}
1096-
if (params->user_hist && !params_u.stopped_running) {
1102+
if (params->user_workload && !params_u.stopped_running) {
10971103
params_u.should_run = 0;
10981104
sleep(1);
10991105
}

tools/tracing/rtla/src/timerlat_top.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct timerlat_top_params {
4343
int cgroup;
4444
int hk_cpus;
4545
int user_top;
46+
int user_workload;
4647
cpu_set_t hk_cpu_set;
4748
struct sched_attr sched_param;
4849
struct trace_events *events;
@@ -364,6 +365,7 @@ static void timerlat_top_usage(char *usage)
364365
" d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
365366
" in nanoseconds",
366367
" -u/--user-threads: use rtla user-space threads instead of in-kernel timerlat threads",
368+
" -U/--user-load: enable timerlat for user-defined user-space workload",
367369
NULL,
368370
};
369371

@@ -423,6 +425,7 @@ static struct timerlat_top_params
423425
{"thread", required_argument, 0, 'T'},
424426
{"trace", optional_argument, 0, 't'},
425427
{"user-threads", no_argument, 0, 'u'},
428+
{"user-load", no_argument, 0, 'U'},
426429
{"trigger", required_argument, 0, '0'},
427430
{"filter", required_argument, 0, '1'},
428431
{"dma-latency", required_argument, 0, '2'},
@@ -435,7 +438,7 @@ static struct timerlat_top_params
435438
/* getopt_long stores the option index here. */
436439
int option_index = 0;
437440

438-
c = getopt_long(argc, argv, "a:c:C::d:De:hH:i:np:P:qs:t::T:u0:1:2:345:",
441+
c = getopt_long(argc, argv, "a:c:C::d:De:hH:i:np:P:qs:t::T:uU0:1:2:345:",
439442
long_options, &option_index);
440443

441444
/* detect the end of the options. */
@@ -552,6 +555,9 @@ static struct timerlat_top_params
552555

553556
break;
554557
case 'u':
558+
params->user_workload = true;
559+
/* fallback: -u implies -U */
560+
case 'U':
555561
params->user_top = true;
556562
break;
557563
case '0': /* trigger */
@@ -869,7 +875,7 @@ int timerlat_top_main(int argc, char *argv[])
869875
top->start_time = time(NULL);
870876
timerlat_top_set_signals(params);
871877

872-
if (params->user_top) {
878+
if (params->user_workload) {
873879
/* rtla asked to stop */
874880
params_u.should_run = 1;
875881
/* all threads left */
@@ -912,15 +918,15 @@ int timerlat_top_main(int argc, char *argv[])
912918
break;
913919

914920
/* is there still any user-threads ? */
915-
if (params->user_top) {
921+
if (params->user_workload) {
916922
if (params_u.stopped_running) {
917923
debug_msg("timerlat user space threads stopped!\n");
918924
break;
919925
}
920926
}
921927
}
922928

923-
if (params->user_top && !params_u.stopped_running) {
929+
if (params->user_workload && !params_u.stopped_running) {
924930
params_u.should_run = 0;
925931
sleep(1);
926932
}

0 commit comments

Comments
 (0)