Subject: [PATCH] rtla: Set /dev/cpu_dma_latency to avoid exit from idle latencies

Set /dev/cpu_dma_latency to 0 to avoid having exit from idle
states latencies influencing in the analysis.

Cc: Daniel Bristot de Oliveira <[email protected]>
Cc: Steven Rostedt <[email protected]>
Signed-off-by: Daniel Bristot de Oliveira <[email protected]>
---
tools/tracing/rtla/src/timerlat_hist.c | 10 ++++++++
tools/tracing/rtla/src/timerlat_top.c | 10 ++++++++
tools/tracing/rtla/src/utils.c | 33 ++++++++++++++++++++++++++
tools/tracing/rtla/src/utils.h | 1 +
4 files changed, 54 insertions(+)

diff --git a/tools/tracing/rtla/src/timerlat_hist.c b/tools/tracing/rtla/src/timerlat_hist.c
index 237e1735afa7..3c0108ff63ab 100644
--- a/tools/tracing/rtla/src/timerlat_hist.c
+++ b/tools/tracing/rtla/src/timerlat_hist.c
@@ -732,6 +732,7 @@ int timerlat_hist_main(int argc, char *argv[])
struct osnoise_tool *record = NULL;
struct osnoise_tool *tool = NULL;
struct trace_instance *trace;
+ int dma_latency_fd = -1;
int return_value = 1;
int retval;

@@ -767,6 +768,13 @@ int timerlat_hist_main(int argc, char *argv[])
}
}

+ /*
+ * Avoid going into deep idle states.
+ */
+ dma_latency_fd = set_cpu_dma_latency(0);
+ if (dma_latency_fd < 0)
+ err_msg("Could not set /dev/cpu_dma_latency to 0. Long IRQ latencies expected\n");
+
trace_instance_start(trace);

if (params->trace_output) {
@@ -812,6 +820,8 @@ int timerlat_hist_main(int argc, char *argv[])
}

out_hist:
+ if (dma_latency_fd >= 0)
+ close(dma_latency_fd);
timerlat_free_histogram(tool->data);
osnoise_destroy_tool(record);
osnoise_destroy_tool(tool);
diff --git a/tools/tracing/rtla/src/timerlat_top.c b/tools/tracing/rtla/src/timerlat_top.c
index d4187f6534ed..f4bca0ad5de3 100644
--- a/tools/tracing/rtla/src/timerlat_top.c
+++ b/tools/tracing/rtla/src/timerlat_top.c
@@ -524,6 +524,7 @@ int timerlat_top_main(int argc, char *argv[])
struct osnoise_tool *record = NULL;
struct osnoise_tool *top = NULL;
struct trace_instance *trace;
+ int dma_latency_fd = -1;
int return_value = 1;
int retval;

@@ -559,6 +560,13 @@ int timerlat_top_main(int argc, char *argv[])
}
}

+ /*
+ * Avoid going into deep idle states.
+ */
+ dma_latency_fd = set_cpu_dma_latency(0);
+ if (dma_latency_fd < 0)
+ err_msg("Could not set /dev/cpu_dma_latency. Long IRQ latencies expected.\n");
+
trace_instance_start(trace);

if (params->trace_output) {
@@ -608,6 +616,8 @@ int timerlat_top_main(int argc, char *argv[])
}

out_top:
+ if (dma_latency_fd >= 0)
+ close(dma_latency_fd);
timerlat_free_top(top->data);
osnoise_destroy_tool(record);
osnoise_destroy_tool(top);
diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c
index ffaf8ec84001..da2b590edaed 100644
--- a/tools/tracing/rtla/src/utils.c
+++ b/tools/tracing/rtla/src/utils.c
@@ -10,6 +10,7 @@
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
#include <sched.h>
#include <stdio.h>

@@ -431,3 +432,35 @@ int parse_prio(char *arg, struct sched_attr *sched_param)
}
return 0;
}
+
+/*
+ * set_cpu_dma_latency - set the /dev/cpu_dma_latecy
+ *
+ * This is used to reduce the exit from idle latency. The value
+ * will be reset once the file descriptor of /dev/cpu_dma_latecy
+ * is closed.
+ *
+ * Return: the /dev/cpu_dma_latecy file descriptor
+ */
+int set_cpu_dma_latency(int32_t latency)
+{
+ int retval;
+ int fd;
+
+ fd = open("/dev/cpu_dma_latency", O_RDWR);
+ if (fd < 0) {
+ err_msg("Error opening /dev/cpu_dma_latency\n");
+ return -1;
+ }
+
+ retval = write(fd, &latency, 4);
+ if (retval < 1) {
+ err_msg("Error setting /dev/cpu_dma_latency\n");
+ close(fd);
+ return -1;
+ }
+
+ debug_msg("Set /dev/cpu_dma_latency to %d\n", latency);
+
+ return fd;
+}
diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h
index 9aa962319ca2..fa08e374870a 100644
--- a/tools/tracing/rtla/src/utils.h
+++ b/tools/tracing/rtla/src/utils.h
@@ -54,3 +54,4 @@ struct sched_attr {

int parse_prio(char *arg, struct sched_attr *sched_param);
int set_comm_sched_attr(const char *comm, struct sched_attr *attr);
+int set_cpu_dma_latency(int32_t latency);
--
2.34.1


Subject: Re: [PATCH] rtla: Set /dev/cpu_dma_latency to avoid exit from idle latencies

On 2/23/22 19:37, Daniel Bristot de Oliveira wrote:
> Set /dev/cpu_dma_latency to 0 to avoid having exit from idle
> states latencies influencing in the analysis.
>

Sending a v2 of this patch together with a new version of rtla features patch
series.

-- Daniel