dj_trace.py 6.4 KB
Newer Older
Florian Fischer's avatar
Florian Fischer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import matplotlib.pyplot as plt
import multiprocessing
import numpy as np
import os
from urllib.request import urlretrieve
import sys
import re
import shutil

from benchmark import Benchmark

comma_sep_number_re = "(?:\d*(?:,\d*)?)*"
rss_re = "(?P<rss>" + comma_sep_number_re + ")"
time_re = "(?P<time>" + comma_sep_number_re + ")"
calls_re = "(?P<calls>" + comma_sep_number_re + ")"

max_rss_re = re.compile("^{} Kb Max RSS".format(rss_re))
ideal_rss_re = re.compile("^{} Kb Max Ideal RSS".format(rss_re))

malloc_re = re.compile("^Avg malloc time:\s*{} in\s*{} calls$".format(time_re, calls_re))
calloc_re = re.compile("^Avg calloc time:\s*{} in\s*{} calls$".format(time_re, calls_re))
realloc_re = re.compile("^Avg realloc time:\s*{} in\s*{} calls$".format(time_re, calls_re))
free_re = re.compile("^Avg free time:\s*{} in\s*{} calls$".format(time_re, calls_re))

class Benchmark_DJ_Trace( Benchmark ):
    def __init__(self):
        self.name = "dj_trace"
        self.descrition = """This benchmark uses the workload simulator written
                             by DJ Delorie to simulate workloads provided by him
                             under https://delorie.com/malloc. Those workloads
                             are generated from traces of real aaplications and are
                             also used by delorie to measure improvements in the
                             glibc allocator.""",

        self.cmd = "build/trace_run{binary_suffix} dj_workloads/{workload}.wl"

        self.args = {
                        "workload" : [
                                        "389-ds-2",
                                        "dj",
                                        "dj2",
                                        "mt_test_one_alloc",
                                        "oocalc",
                                        "qemu-virtio",
                                        "qemu-win7",
                                        "proprietary-1",
                                        "proprietary-2",
                                      ]
                    }

        self.requirements = ["build/trace_run"]
        super().__init__()

    def prepare(self, verbose=False):
        super().prepare(verbose=verbose)

        def reporthook(blocknum, blocksize, totalsize):
            readsofar = blocknum * blocksize
            if totalsize > 0:
                percent = readsofar * 1e2 / totalsize
                s = "\r%5.1f%% %*d / %d" % (
                percent, len(str(totalsize)), readsofar, totalsize)
                sys.stderr.write(s)
            else: # total size is unknown
                sys.stderr.write("\rdownloaded %d" % (readsofar,))

        if not os.path.isdir("dj_workloads"):
            os.mkdir("dj_workloads")

        for wl in self.args["workload"]:
            file_name = wl + ".wl"
            file_path = os.path.join("dj_workloads", file_name)
            if not os.path.isfile(file_path):
                if input("want to download " + wl + " [Y/n] ") in ["", "Y", "y"]:
                    url = "http://www.delorie.com/malloc/" + file_name
                    urlretrieve(url, file_path, reporthook)
                    sys.stderr.write("\n")
        return True

    def process_stdout(self, result, stdout, verbose):
        def to_int(s):
            return int(s.replace(',', ""))

        for i, l in enumerate(stdout.splitlines()):
            if i == 3:
                result["Max_RSS"] = to_int(max_rss_re.match(l).group("rss"))
            elif i == 4:
                result["Ideal_RSS"] = to_int(ideal_rss_re.match(l).group("rss"))
            elif i == 7:
                result["avg_malloc"] = to_int(malloc_re.match(l).group("time"))
            elif i == 8:
                result["avg_calloc"] = to_int(calloc_re.match(l).group("time"))
            elif i == 9:
                result["avg_realloc"] = to_int(realloc_re.match(l).group("time"))
            elif i == 10:
                result["avg_free"] = to_int(free_re.match(l).group("time"))
        

    def summary(self, sd=None):
        args = self.results["args"]
        targets = self.results["targets"]

        sd = sd or ""

        # Total times
        for perm in self.iterate_args():
            for i, target in enumerate(targets):
                d = [float(x["task-clock"]) for x in self.results[target][perm]]
                y_val = np.mean(d)
                plt.bar([i], y_val, label=target, color=targets[target]["color"])

            plt.legend(loc="lower right")
            plt.ylabel("Time in ms")
            plt.title("Runtime of " + perm.workload + ":")
            plt.savefig(os.path.join(sd, ".".join([self.name, perm.workload, "runtime", "png"])))
            plt.clf()

        # Function Times
        for perm in self.iterate_args():
            for i, target in enumerate(targets):
                x_vals = [x-i/8 for x in range(0,4)]
                y_vals = [0] * 4
                y_vals[0] = np.mean([x["avg_malloc"] for x in self.results[target][perm]])
                y_vals[1] = np.mean([x["avg_calloc"] for x in self.results[target][perm]])
                y_vals[2] = np.mean([x["avg_realloc"] for x in self.results[target][perm]])
                y_vals[3] = np.mean([x["avg_free"] for x in self.results[target][perm]])
                plt.bar(x_vals, y_vals, width=0.2, align="center",
                        label=target, color=targets[target]["color"])

            plt.legend(loc="best")
            plt.xticks(range(0,4), ["malloc", "calloc", "realloc", "free"])
            plt.ylabel("Avg time in ms")
            plt.title("Avg API call times " + perm.workload + ":")
            plt.savefig(os.path.join(sd, ".".join([self.name, perm.workload, "apitimes", "png"])))
            plt.clf()

        # Memusage
        for perm in self.iterate_args():
            for i, target in enumerate(targets):
                d = [x["Max_RSS"] for x in self.results[target][perm]]
                y_val = np.mean(d)
                plt.bar([i], y_val, label=target, color=targets[target]["color"])

            # add ideal rss
            y_val = self.results[list(targets.keys())[0]][perm][0]["Ideal_RSS"]
            plt.bar([len(targets)], y_val, label="Ideal RSS")

            plt.legend(loc="best")
            plt.ylabel("Max RSS in Kb")
            plt.title("Max RSS " + perm.workload + ":")
            plt.savefig(os.path.join(sd, ".".join([self.name, perm.workload, "rss", "png"])))
            plt.clf()

dj_trace = Benchmark_DJ_Trace()