Commit d4a0d13b authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Improve latency code (graphviz, filtering, reformat).

parent 2ffc0736
Loading
Loading
Loading
Loading
+66 −68
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ from xlap.analyse.util import get_outlier_threshold, extract_durations, box
from scipy.stats.stats import pearsonr
import numpy as np
import sys
import graphviz


def _get_thread_for_event(config, e):
@@ -14,6 +15,7 @@ def _get_thread_for_event(config, e):
        print("Cannot find %s".format(name), file=sys.stderr)
        return None


def _happens_before(df, a, b, config):
    """
    check if a happens-before b in the trace
@@ -35,12 +37,14 @@ def _happens_before(df, a, b, config):
    # If in doubt, a and b are concurrent.
    return not (df[a] >= df[b]).any()


def _fast_happens_before(df, a, b, hb):
    """
    check if a happens-before b, using a pre-computed relation
    """
    return any(r['Start'] == str(a) and r['End'] == str(b) for r in hb)


def _happens_directly_before(df, a, b, hb):
    """
    check if a happens-directly-before b in the trace
@@ -55,6 +59,7 @@ def _happens_directly_before(df, a, b, hb):
            return False
    return True


def _locally_happens_directly_before(df, a, b, hb, config):
    """
    check if a happens-directly-before b in the trace but, be a little bit more
@@ -101,20 +106,19 @@ def _locally_happens_directly_before(df, a, b, hb, config):
    else:
        return _happens_directly_before(df, a, b, hb)


def _plot_controlflow_graph(df, hdb):
    """
    print the control flow graph in a format that dot understands
    generate the control flow graph using dot
    """
    print("Digraph G {")
    for event1 in df:
        if not str(event1).endswith("_T"):
            continue
        print("\t_node__"+str(event1) + "[label=\""+str(event1)[:-2]+"\"]")
    t_columns = [x for x in df.columns if x.endswith("_T")]
    d = graphviz.Digraph(filename="graph", format="pdf")
    for event1 in df[t_columns]:
        d.node(str(event1)[:-2])
    for edge in hdb:
        print("\t_node__"+edge['Start'] + " -> _node__"+edge['End'] + ";")
    #for edge in hdb:
    #    print("\t_node__"+edge['Start'] + " -> _node__"+edge['End'] + "[label=\""+str(edge['Correlation'])+"\"];")
    print("}")
        d.edge(edge["Start"][:-2], edge["End"][:-2])
    d.render()  # saves to graph.pdf in local folder
    return d


# Taken from: http://composition.al/blog/2015/11/29/a-better-way-to-add-labels-to-bar-charts-with-matplotlib/
@@ -160,22 +164,17 @@ def _plot_critical_regions(df,hdb):
        rects = ax.barh(x, correlations, align="center", tick_label="")
        autolabel(rects, ax, ticks)

    # TODO: find a more elegant solution for the label text
        plt.tight_layout()
        plt.savefig("latency-criticality.pdf")
        plt.close()



    def analyse(df, config):
        hb = []
    for event1 in df:
        if not str(event1).endswith("_T"):
            continue
        print(str(event1) + " ...", file=sys.stderr)
        for event2 in df:
            if not str(event2).endswith("_T"):
                continue

        events = [column for column in df.columns if column.endswith("_T")]

        for event1 in df[events]:
            for event2 in df[events]:
                if str(event1) == str(event2):
                    continue
                if _happens_before(df, event1, event2, config):
@@ -183,12 +182,8 @@ def analyse(df, config):

        hdb = []
        e2e = list(df['EndToEnd_D'])
    for event1 in df:
        if not str(event1).endswith("_T"):
            continue
        for event2 in df:
            if not str(event2).endswith("_T"):
                continue
        for event1 in df[events]:
            for event2 in df[events]:
                if str(event1) == str(event2):
                    continue
                # if _locally_happens_directly_before(df, event1, event2, hb, config):
@@ -202,5 +197,8 @@ def analyse(df, config):

                    hdb += [{'Start': str(event1), 'End': str(event2), 'Correlation': correlation}]

    _plot_controlflow_graph(df, hdb)
        cfg = _plot_controlflow_graph(df, hdb)
        _plot_critical_regions(df, hdb)
        return {
            "cfg": cfg
        }