<commit_before># SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2019, 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 re
import glob
from collections import namedtuple

import pandas as pd

from lisa.analysis.base import AnalysisHelpers, TraceAnalysisBase
from lisa.datautils import df_filter_task_ids
from lisa.trace import TaskID, requires_events
from lisa.utils import memoized

class RTAEventsAnalysis(TraceAnalysisBase):
    """
    Support for RTA events analysis.

    :param trace: input Trace object
    :type trace: lisa.trace.Trace
    """

    name = 'rta'

    RefTime = namedtuple("RefTime", ['kernel', 'user'])
    """
    Named tuple to synchronize kernel and userspace (``rt-app``) timestamps.
    """

    PhaseWindow = namedtuple("PhaseWindow", ['id', 'start', 'end'])
    """
    Named tuple with fields:

        * ``id``: integer ID of the phase
        * ``start``: timestamp of the start of the phase
        * ``end``: timestamp of the end of the phase

    """

    def _task_filtered(self, df, task=None):
        if not task:
            return df
        if type(task) is not TaskID:
            task = self.trace.get_task_id(task)
        if task not in self.rtapp_tasks:
            raise ValueError("Task [{}] is not an rt-app task: {}"
                             .format(task, self.rtapp_tasks))

        return df_filter_task_ids(df, [task],
                                  pid_col='__pid', comm_col='__comm')

    @memoized
    def _get_rtapp_tasks(self):
        task_ids = set()
        for evt in self.trace.available_events:
            if not evt.startswith('rtapp_'):
                continue
            df = self.trace.df_events(evt)
            for pid,name in df[['__pid', '__comm']].drop_duplicates().values:
                task_ids.add(TaskID(pid, name))
        return sorted(task_ids)

    @property
    def rtapp_tasks(self):
        """
        List of :class:`lisa.trace.TaskID` of the ``rt-app`` tasks present in
        the trace.
        """
        return self._get_rtapp_tasks()


###############################################################################
# DataFrame Getter Methods<commit_msg>lisa.analysis.rta: Remove unnecessary type check<commit_after># SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2019, 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 re
import glob
from collections import namedtuple

import pandas as pd

from lisa.analysis.base import AnalysisHelpers, TraceAnalysisBase
from lisa.datautils import df_filter_task_ids
from lisa.trace import TaskID, requires_events
from lisa.utils import memoized

class RTAEventsAnalysis(TraceAnalysisBase):
    """
    Support for RTA events analysis.

    :param trace: input Trace object
    :type trace: lisa.trace.Trace
    """

    name = 'rta'

    RefTime = namedtuple("RefTime", ['kernel', 'user'])
    """
    Named tuple to synchronize kernel and userspace (``rt-app``) timestamps.
    """

    PhaseWindow = namedtuple("PhaseWindow", ['id', 'start', 'end'])
    """
    Named tuple with fields:

        * ``id``: integer ID of the phase
        * ``start``: timestamp of the start of the phase
        * ``end``: timestamp of the end of the phase

    """

    def _task_filtered(self, df, task=None):
        if not task:
            return df

        task = self.trace.get_task_id(task)

        if task not in self.rtapp_tasks:
            raise ValueError("Task [{}] is not an rt-app task: {}"
                             .format(task, self.rtapp_tasks))

        return df_filter_task_ids(df, [task],
                                  pid_col='__pid', comm_col='__comm')

    @memoized
    def _get_rtapp_tasks(self):
        task_ids = set()
        for evt in self.trace.available_events:
            if not evt.startswith('rtapp_'):
                continue
            df = self.trace.df_events(evt)
            for pid,name in df[['__pid', '__comm']].drop_duplicates().values:
                task_ids.add(TaskID(pid, name))
        return sorted(task_ids)

    @property
    def rtapp_tasks(self):
        """
        List of :class:`lisa.trace.TaskID` of the ``rt-app`` tasks present in
        the trace.
        """
        return self._get_rtapp_tasks()


###############################################################################
# DataFrame Getter Methods
{'fraction_non_alphanumeric': 0.12624584717607973, 'fraction_numerical': 0.0036913990402362494, 'mean_word_length': 2.794817927170868, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6773920', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1513, 'n_words': 516}