# 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 logging
import os

from test import LisaTest

import trappy
from bart.common.Analyzer import Analyzer

TESTS_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
TESTS_CONF = os.path.join(TESTS_DIRECTORY, "smoke_test_ramp.config")

class STune(LisaTest):
    """
    Goal
    ====

    Verify that a task in a SchedTune cgroup is boosted

    Detailed Description
    ====================

    The test runs a ramp task that has increasing load as time passes.
    The load increases from 5% to 60% over 1 second.  It is run in
    four different configurations: no boost, 15% boost, 30% boost and
    60% boost.

    Expected Behaviour
    ==================

    The margin of the task should match the formula

    .. math::

          (sched\_load\_scale - util) \\times boost

    for all configurations

    """


    @classmethod
    def setUpClass(cls, *args, **kwargs):
        super(STune, cls)._init(TESTS_CONF, *args, **kwargs)

    def test_boosted_utilization_signal(self):
        """Tasks in stune groups are boosted"""

        for tc in self.conf["confs"]:
            test_id = tc["tag"]

            wload_idx = self.conf["wloads"].keys()[0]
            run_dir = os.path.join(self.te.res_dir,
                                   "rtapp:{}:{}".format(test_id, wload_idx),
                                   "1")

            ftrace_events = ["sched_boost_task"]
            ftrace = trappy.FTrace(run_dir, scope="custom",
                                   events=ftrace_events)

            first_task_params = self.conf["wloads"][wload_idx]["conf"]["params"]
            first_task_name = first_task_params.keys()[0]
            rta_task_name = "task_{}".format(first_task_name)

            sbt_dfr = ftrace.sched_boost_task.data_frame
            boost_task_rtapp = sbt_dfr[sbt_dfr.comm == rta_task_name]
            ftrace.add_parsed_event("boost_task_rtapp", boost_task_rtapp)

            # Avoid the first period as the task starts with a very
            # high load and it overutilizes the CPU
            rtapp_period = first_task_params[first_task_name]["params"]["period_ms"]
            task_start = boost_task_rtapp.index[0]
            after_first_period = task_start + (rtapp_period / 1000.)

            boost = tc["cgroups"]["conf"]["schedtune"]["/stune"]["boost"]
            analyzer_const = {
                "SCHED_LOAD_SCALE": 1024,
                "BOOST": boost,
            }
            analyzer = Analyzer(ftrace, analyzer_const,
                                window=(after_first_period, None))
            statement = "(((SCHED_LOAD_SCALE - boost_task_rtapp:util) * BOOST) // 100) == boost_task_rtapp:margin"
            error_msg = "task was not boosted to the expected margin: {:.2f}"\
                        .format(boost / 100.)
            self.assertTrue(analyzer.assertStatement(statement), msg=error_msg)

# vim :set tabstop=4 shiftwidth=4 expandtab

@@ -2345,210 +2345,8 @@
 e)%0A%0A
-            sbt_dfr = ftrace.sched_boost_task.data_frame%0A            boost_task_rtapp = sbt_dfr%5Bsbt_dfr.comm == rta_task_name%5D%0A            ftrace.add_parsed_event(%22boost_task_rtapp%22, boost_task_rtapp)%0A%0A
     
@@ -2409,16 +2409,16 @@
  a very%0A
+
         
@@ -2562,37 +2562,116 @@
     
-task_start = boost
+sbt_dfr = ftrace.sched_boost_task.data_frame%0A            task_start = sbt_dfr%5Bsbt_dfr.comm == rta
 _task_
-rtapp
+name%5D
 .ind
@@ -3060,16 +3060,81 @@
 d, None)
+,%0A                                filters=%7B%22comm%22: rta_task_name%7D
 )%0A      
@@ -3174,16 +3174,22 @@
 SCALE - 
+sched_
 boost_ta
@@ -3190,22 +3190,16 @@
 ost_task
-_rtapp
 :util) *
@@ -3217,16 +3217,22 @@
 100) == 
+sched_
 boost_ta
@@ -3237,14 +3237,8 @@
 task
-_rtapp
 :mar

simplify stune's boosted utilization signal test