From 701e6adf7a7b86ee4bd826c27370e6613a326c4b Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Mon, 16 Nov 2015 17:40:46 +0000
Subject: [PATCH 1/2] cpufreq: add method to trace current frequencies

This patch add a method which allows to inject into a trace a "cpu_frequency"
event for each CPU reporting the current frequency the CPU is running at.
Such a method could be useful to force report CPUs frequency into a trace
file, for example at tracing start and stop, thus allowing to know always
at which frequency an experiment has been executed.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/module/cpufreq.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/devlib/module/cpufreq.py b/devlib/module/cpufreq.py
index f4386ad..5a55213 100644
--- a/devlib/module/cpufreq.py
+++ b/devlib/module/cpufreq.py
@@ -363,3 +363,16 @@ class CpufreqModule(Module):
                 "done"\
                 .format(governor), as_root=True)
 
+    def trace_frequencies(self):
+        """
+        Report current frequencies on trace file
+        """
+        self.target.execute(
+                'FREQS=$(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq); '
+                'CPU=0; for F in $FREQS; do '
+                '   echo "cpu_frequency:        state=$F cpu_id=$CPU" > /sys/kernel/debug/tracing/trace_marker; '
+                '   let CPU++; '
+                'done',
+                as_root=True
+        )
+

From a7cfd28bd0f6cdf5097e52a52262c06f4cb1015a Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Mon, 16 Nov 2015 17:45:33 +0000
Subject: [PATCH 2/2] ftrace: force report CPU frequencies at trace start and
 stop

When the cpufreq module is loaded, quite likely we want to run experiments
which involve an analysis of frequencies transitions. However, if during
an experiment there are not cpu_frequency events, the generated trace
does not allows to know at which frequency the experiment has been executed.
This happens for example when we are running at constant frequency or just
because the workload is not generating a variable capacity request in the
system.

This path ensure the a "cpu_frequency" events is always generated at the
beginning and at the end of a collected trace. This support is required
for example to properly plot CPUs frequencies event when there are not
CPUFreq generated changes of OPP.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/trace/ftrace.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/devlib/trace/ftrace.py b/devlib/trace/ftrace.py
index 871cc89..2973e0b 100644
--- a/devlib/trace/ftrace.py
+++ b/devlib/trace/ftrace.py
@@ -101,8 +101,14 @@ class FtraceCollector(TraceCollector):
         if self.automark:
             self.mark_start()
         self.target.execute('{} start {}'.format(self.target_binary, self.event_string), as_root=True)
+        if 'cpufreq' in self.target.modules:
+            self.logger.debug('Trace CPUFreq frequencies')
+            self.target.cpufreq.trace_frequencies()
 
     def stop(self):
+        if 'cpufreq' in self.target.modules:
+            self.logger.debug('Trace CPUFreq frequencies')
+            self.target.cpufreq.trace_frequencies()
         self.stop_time = time.time()
         if self.automark:
             self.mark_stop()