From 7f5a150b4f691d8d2972f24c16c911bc4f66727a Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Tue, 26 Jan 2016 15:33:21 +0000
Subject: [PATCH] ftrace: add support to collect function stats

Function stats can be collected while events are accumulated into a trace.

This function adds the required support to start and stop functions
profiling across a trace collection.

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

diff --git a/devlib/trace/ftrace.py b/devlib/trace/ftrace.py
index 9cd428c..c521b00 100644
--- a/devlib/trace/ftrace.py
+++ b/devlib/trace/ftrace.py
@@ -75,8 +75,10 @@ class FtraceCollector(TraceCollector):
         # Setup tracing paths
         self.available_functions_file = self.target.path.join(self.tracing_path, 'available_filter_functions')
         self.buffer_size_file         = self.target.path.join(self.tracing_path, 'buffer_size_kb')
+        self.current_tracer_file      = self.target.path.join(self.tracing_path, 'current_tracer')
         self.function_profile_file    = self.target.path.join(self.tracing_path, 'function_profile_enabled')
         self.marker_file              = self.target.path.join(self.tracing_path, 'trace_marker')
+        self.ftrace_filter_file       = self.target.path.join(self.tracing_path, 'set_ftrace_filter')
 
         self.host_binary = which('trace-cmd')
         self.kernelshark = which('kernelshark')
@@ -123,8 +125,17 @@ class FtraceCollector(TraceCollector):
         if 'cpufreq' in self.target.modules:
             self.logger.debug('Trace CPUFreq frequencies')
             self.target.cpufreq.trace_frequencies()
+        # Enable kernel function profiling
+        if self.functions:
+            self.target.execute('echo nop > {}'.format(self.current_tracer_file))
+            self.target.execute('echo 0 > {}'.format(self.function_profile_file))
+            self.target.execute('echo {} > {}'.format(self.function_string, self.ftrace_filter_file))
+            self.target.execute('echo 1 > {}'.format(self.function_profile_file))
 
     def stop(self):
+        # Disable kernel function profiling
+        if self.functions:
+            self.target.execute('echo 1 > {}'.format(self.function_profile_file))
         if 'cpufreq' in self.target.modules:
             self.logger.debug('Trace CPUFreq frequencies')
             self.target.cpufreq.trace_frequencies()