<commit_before># $Copyright:
# ----------------------------------------------------------------
# This confidential and proprietary software may be used only as
# authorised by a licensing agreement from ARM Limited
#  (C) COPYRIGHT 2015 ARM Limited
#       ALL RIGHTS RESERVED
# The entire notice above must be reproduced on all authorised
# copies and copies may only be made to the extent permitted
# by a licensing agreement from ARM Limited.
# ----------------------------------------------------------------
# File:        SchedConf.py
# ----------------------------------------------------------------
# $
#
"""Scheduler specific Functionality for the
stats framework
"""

import numpy as np
from cr2.stats.Trigger import Trigger

WINDOW_SIZE = 0.0001

# Trigger Values
SCHED_SWITCH_IN = 1
SCHED_SWITCH_OUT = -1
NO_EVENT = 0

# Field Names
CPU_FIELD = "__cpu"
NEXT_PID_FIELD = "next_pid"
PREV_PID_FIELD = "prev_pid"
TASK_RUNNING = 1
TASK_NOT_RUNNING = 0

def csum(series, window=None, filter_gaps=False):
    """The following actions are done on the
    input series if filter_gaps is set as True

        * A sched_out of duration < WindowSize
          is filtered out
        * The resultant series is summed cumulatively
          and returned

        Args:
            series (pandas.Series)
            window (tuple)
            filter_gaps (boolean)
        Returns:
            pandas.Series
    """

    if filter_gaps:
        series = filter_small_gaps(series)

    series = series.cumsum()
    return select_window(series, window)

def filter_small_gaps(series):
    start = None
    for index, value in series.iteritems():

        if value == SCHED_SWITCH_IN:
            if start == None:
                continue

            if index - start < WINDOW_SIZE:
                series[start] = NO_EVENT
                series[index] = NO_EVENT
            start = None

        if value == SCHED_SWITCH_OUT:
            start = index

    return series


def select_window(series, window):
    """Library Function to select a portion of
       pandas time series
    """

    if not window:
        return series

    start, stop = window
    ix = series.index
    selector = ((ix >= start) & (ix <= stop))
    window_series = series[selector]
    return window_series

def residency_sum(series, window=None):
    """The input series is processed for
    intervals between a 1 and -1 in order
    to track additive residency of a task


        Args:
            series (pandas.Series)
            window (start, stop): A start stop
                tuple to process only a section of the
                series
        Returns:
            float (scalar)
    """

    series = select_window(series, window)
    duration = 0
    start = None
    for index, value in series.iteritems():
        if value == SCHED_SWITCH_IN:
            start = index

        if value == SCHED_SWITCH_OUT:
            if start is not None:
                duration += index - start

            start = None


    return duration

def total_duration(series):
    """Aggregator function that returns the
    total execution duration
    """

    index = series.index.values
    return index[-1] - index[0]

def binary_correlate(series_x, series_y):
    """Function to Correlate binary Data"""

    if len(series_x) != len(series_y):
        raise ValueError("Cannot compute binary correlation for \
                          unequal vectors")

    agree = len(series_x[series_x == series_y])
    disagree = len(series_x[series_x != series_y])

    return (agree - disagree) / float(len(series_x))

def get_pids_for_process(run, execname, cls=None):
    """Returns the pids for a given process

    Args:
        run (cr2.Run): A cr2.Run object with a sched_switch
            event
        execname (str): The name of the process
        cls (cr2.Base): The SchedSwitch event class

    Returns:
        The list of pids (unique) for the execname

    """

    if not cls:
        try:
            df = run.sched_switch.data_frame
        except AttributeError:
            raise ValueError("SchedSwitch event not found in run")
    else:
        event = getattr(run, cls.name)
        df = event.data_frame

    mask = df["__comm"].apply(lambda x : True if x.startswith(execname) else False)
    return list(np.unique(df[mask]["__pid"].values))


def sched_triggers(run, pid, sched_switch_class):
    """Returns the list of sched_switch triggers


    Args:
        run (cr2.Run): A run object with SchedSwitch event
        pid (int): pid of the associated task
        sched_switch_class (cr2.Base): The SchedSwitch class

    Returns:
        Lits of triggers
        [0] = switch_in_trigger<commit_msg>stats: SchedConf: Add get_task_name<commit_after># $Copyright:
# ----------------------------------------------------------------
# This confidential and proprietary software may be used only as
# authorised by a licensing agreement from ARM Limited
#  (C) COPYRIGHT 2015 ARM Limited
#       ALL RIGHTS RESERVED
# The entire notice above must be reproduced on all authorised
# copies and copies may only be made to the extent permitted
# by a licensing agreement from ARM Limited.
# ----------------------------------------------------------------
# File:        SchedConf.py
# ----------------------------------------------------------------
# $
#
"""Scheduler specific Functionality for the
stats framework
"""

import numpy as np
from cr2.stats.Trigger import Trigger

WINDOW_SIZE = 0.0001

# Trigger Values
SCHED_SWITCH_IN = 1
SCHED_SWITCH_OUT = -1
NO_EVENT = 0

# Field Names
CPU_FIELD = "__cpu"
NEXT_PID_FIELD = "next_pid"
PREV_PID_FIELD = "prev_pid"
TASK_RUNNING = 1
TASK_NOT_RUNNING = 0

def csum(series, window=None, filter_gaps=False):
    """The following actions are done on the
    input series if filter_gaps is set as True

        * A sched_out of duration < WindowSize
          is filtered out
        * The resultant series is summed cumulatively
          and returned

        Args:
            series (pandas.Series)
            window (tuple)
            filter_gaps (boolean)
        Returns:
            pandas.Series
    """

    if filter_gaps:
        series = filter_small_gaps(series)

    series = series.cumsum()
    return select_window(series, window)

def filter_small_gaps(series):
    start = None
    for index, value in series.iteritems():

        if value == SCHED_SWITCH_IN:
            if start == None:
                continue

            if index - start < WINDOW_SIZE:
                series[start] = NO_EVENT
                series[index] = NO_EVENT
            start = None

        if value == SCHED_SWITCH_OUT:
            start = index

    return series


def select_window(series, window):
    """Library Function to select a portion of
       pandas time series
    """

    if not window:
        return series

    start, stop = window
    ix = series.index
    selector = ((ix >= start) & (ix <= stop))
    window_series = series[selector]
    return window_series

def residency_sum(series, window=None):
    """The input series is processed for
    intervals between a 1 and -1 in order
    to track additive residency of a task


        Args:
            series (pandas.Series)
            window (start, stop): A start stop
                tuple to process only a section of the
                series
        Returns:
            float (scalar)
    """

    series = select_window(series, window)
    duration = 0
    start = None
    for index, value in series.iteritems():
        if value == SCHED_SWITCH_IN:
            start = index

        if value == SCHED_SWITCH_OUT:
            if start is not None:
                duration += index - start

            start = None


    return duration

def total_duration(series):
    """Aggregator function that returns the
    total execution duration
    """

    index = series.index.values
    return index[-1] - index[0]

def binary_correlate(series_x, series_y):
    """Function to Correlate binary Data"""

    if len(series_x) != len(series_y):
        raise ValueError("Cannot compute binary correlation for \
                          unequal vectors")

    agree = len(series_x[series_x == series_y])
    disagree = len(series_x[series_x != series_y])

    return (agree - disagree) / float(len(series_x))

def get_pids_for_process(run, execname, cls=None):
    """Returns the pids for a given process

    Args:
        run (cr2.Run): A cr2.Run object with a sched_switch
            event
        execname (str): The name of the process
        cls (cr2.Base): The SchedSwitch event class

    Returns:
        The list of pids (unique) for the execname

    """

    if not cls:
        try:
            df = run.sched_switch.data_frame
        except AttributeError:
            raise ValueError("SchedSwitch event not found in run")
    else:
        event = getattr(run, cls.name)
        df = event.data_frame

    mask = df["__comm"].apply(lambda x : True if x.startswith(execname) else False)
    return list(np.unique(df[mask]["__pid"].values))

def get_task_name(run, pid, cls=None):
    """Returns the execname for pid

    Args:
        run (cr2.Run): A cr2.Run object with a sched_switch
            event
        pid (str): The name of the process
        cls (cr2.Base): The SchedSwitch event class

    Returns:
        The execname for the PID

    """

    if not cls:
        try:
            df = run.sched_switch.data_frame
        except AttributeError:
           raise ValueError("SchedSwitch event not found in run")
    else:
        event = getattr(run, cls.name)
        df = event.data_frame

    df = df[df["__pid"] == pid]
    if not len(df):
        return ""
    else:
        return df["__comm"].values[0]

def sched_triggers(run, pid, sched_switch_class):
    """Returns the list of sched_switch triggers


    Args:
        run (cr2.Run): A run object with SchedSwitch event
        pid (int): pid of the associated task
        sched_switch_class (cr2.Base): The SchedSwitch class

    Returns:
        Lits of triggers
        [0] = switch_in_trigger
{'fraction_non_alphanumeric': 0.11380025684085746, 'fraction_numerical': 0.005531956929763904, 'mean_word_length': 2.011302795954789, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7621844', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2764, 'n_words': 961}