# SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2015, ARM Limited and contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import logging


class Workload(object):
    """
    Base class for Android related workloads
    """

    def __init__(self, test_env):
        """
        Initialized workloads available on the specified test environment

        test_env: target test environmen
        """
        self._te = test_env
        self._target = test_env.target
        self._log = logging.getLogger('Workload')

        # Set of data reported in output of each run
        self.trace_file = None
        self.nrg_report = None
        wloads = Workload.availables(self.target)
        self._log.info('Workloads available on target:')
        self._log.info('  %s', wloads)

    def _adb(self, cmd):
        return 'adb -s {} {}'.format(self._target.adb_name, cmd)


    def run(self, out_dir, collect='',
            **kwargs):
        raise RuntimeError('Not implemeted')

    def tracingStart(self):
        # Initialize energy meter results
        if 'energy' in self.collect and self._te.emeter:
            self._te.emeter.reset()
            self._log.info('Energy meter STARTED')

    def tracingStop(self):
        # Collect energy meter results
        if 'energy' in self.collect and self._te.emeter:
            self.nrg_report = self._te.emeter.report(self.out_dir)
            self._log.info('Energy meter STOPPED')
# vim :set tabstop=4 shiftwidth=4 expandtab

@@ -641,26 +641,57 @@
 ort 
-os%0Aimport logging%0A
+logging%0Aimport os%0Aimport re%0A%0Afrom . import System
 %0A%0Acl
@@ -1568,427 +1568,2183 @@
     
-# Initialize energy meter results%0A        if 'energy' in self.collect and self._te.emeter:%0A            self._te.emeter.reset()%0A            self._log.info('Energy meter STARTED')%0A%0A    def tracingStop(self):%0A        # Collect energy meter results%0A        if 'energy' in self.collect and self._te.emeter:%0A            self.nrg_report = self._te.emeter.report(self.out_dir)%0A            self._log.info('Energy meter STOPPED')
+if 'ftrace' in self.collect and 'systrace' in self.collect:%0A            msg = 'ftrace and systrace cannot be used at the same time'%0A            raise ValueError(msg)%0A        # Start FTrace%0A        if 'ftrace' in self.collect:%0A            self.trace_file = os.path.join(self.out_dir, 'trace.dat')%0A            self._log.info('FTrace START')%0A            self._te.ftrace.start()%0A        # Start Systrace (mutually exclusive with ftrace)%0A        elif 'systrace' in self.collect:%0A            self.trace_file = os.path.join(self.out_dir, 'trace.html')%0A            # Get the systrace time%0A            match = re.search(r'systrace_(%5B0-9%5D+)', self.collect)%0A            if match:%0A                self._trace_time = match.group(1)%0A            else:%0A                # TODO: must implement a CTRL+C based systrace stopping%0A                self._log.warning(%22Systrace time NOT defined, tracing for 10%5Bs%5D%22)%0A                self._trace_time = 10%0A            self._log.info('Systrace START')%0A            self._systrace_output = System.systrace_start(%0A                self._te, self.trace_file, self._trace_time)%0A        # Initialize energy meter results%0A        if 'energy' in self.collect and self._te.emeter:%0A            self._te.emeter.reset()%0A            self._log.info('Energy meter STARTED')%0A%0A    def tracingStop(self):%0A        # Collect energy meter results%0A        if 'energy' in self.collect and self._te.emeter:%0A            self.nrg_report = self._te.emeter.report(self.out_dir)%0A            self._log.info('Energy meter STOPPED')%0A        # Stop FTrace%0A        if 'ftrace' in self.collect:%0A            self._te.ftrace.stop()%0A            self._log.info('FTrace STOP')%0A            self._te.ftrace.get_trace(self.trace_file)%0A        # Stop Systrace (mutually exclusive with ftrace)%0A        elif 'systrace' in self.collect:%0A            if not self.systrace_output:%0A                self._log.warning('Systrace is not running!')%0A            else:%0A                self._log.info('Waiting systrace report %5B%25s%5D...',%0A                                 self.trace_file)%0A                self.systrace_output.wait()%0A        # Dump a platform description%0A        self._te.platform_dump(self.out_dir)%0A
 %0A# v

add support for tracing