aboutsummaryrefslogtreecommitdiff
path: root/summarize.py
blob: fe603972eb5c7307d17886874c786302b8c7f4ad (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python3

# Copyright 2018-2020 Florian Fischer <florian.fl.fischer@fau.de>
#
# This file is part of allocbench.
#
# allocbench is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# allocbench is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with allocbench.  If not, see <http://www.gnu.org/licenses/>.
"""Summarize the results of an allocbench run"""

import argparse
import os
import sys

from allocbench.directories import set_current_result_dir, get_current_result_dir
import allocbench.facter as facter
import allocbench.benchmark
import allocbench.util
from allocbench.util import print_status, set_verbosity, print_license_and_exit, get_logger

logger = get_logger(__file__)


def specific_summary(bench, sum_dir, allocators):
    """Summarize bench in sum_dir for allocators"""
    old_allocs = bench.results["allocators"]
    allocs_in_set = {k: v for k, v in old_allocs.items() if k in allocators}

    if not allocs_in_set:
        return

    # create and change to sum_dir
    os.mkdir(sum_dir)
    os.chdir(sum_dir)

    bench.results["allocators"] = allocs_in_set

    # set colors
    explicit_colors = [
        v["color"] for k, v in allocs_in_set.items() if v["color"] is not None
    ]
    logger.debug("Explicit colors: %s", explicit_colors)

    cycle_list = ["C" + str(i) for i in range(0, 10)]
    avail_colors = [
        color for color in cycle_list if color not in explicit_colors
    ]
    logger.debug("available colors: %s", avail_colors)

    for _, value in allocs_in_set.items():
        if value["color"] is None:
            value["color"] = avail_colors.pop()

    bench.summary()
    bench.results["allocators"] = old_allocs
    os.chdir("..")


def bench_sum(bench, exclude_allocators=None, sets=False):
    """Create a summary of bench for each set of allocators"""

    new_allocs = {
        an: a
        for an, a in bench.results["allocators"].items()
        if an not in (exclude_allocators or {})
    }
    bench.results["allocators"] = new_allocs

    os.makedirs(bench.name)
    os.chdir(bench.name)

    os.mkdir("all")
    os.chdir("all")
    bench.summary()
    os.chdir("..")

    if sets:
        sets = {
            "glibcs": [
                "glibc", "glibc-noThreadCache", "glibc-noFalsesharing",
                "glibc-noFalsesharingClever"
            ],
            "tcmalloc": ["TCMalloc", "TCMalloc-NoFalsesharing"],
            "nofs": [
                "glibc", "glibc-noFalsesharing", "glibc-noFalsesharingClever",
                "TCMalloc", "TCMalloc-NoFalsesharing"
            ],
            "ba": ["glibc", "TCMalloc", "jemalloc", "Hoard"],
            "industry": [
                "glibc", "llalloc", "TCMalloc", "jemalloc", "tbbmalloc",
                "mimalloc"
            ],
            "research":
            ["scalloc", "SuperMalloc", "Mesh", "Hoard", "snmalloc"]
        }
    else:
        sets = {}

    for set_name, set_allocators in sets.items():
        specific_summary(bench, set_name, set_allocators)

    os.chdir("..")


def summarize(benchmarks=None,
              exclude_benchmarks=None,
              exclude_allocators=None,
              sets=False):
    """summarize the benchmarks in the resdir"""

    cwd = os.getcwd()
    os.chdir(get_current_result_dir())

    for benchmark in allocbench.benchmark.AVAIL_BENCHMARKS:
        if benchmarks and not benchmark in benchmarks:
            continue
        if exclude_benchmarks and benchmark in exclude_benchmarks:
            continue

        try:
            bench = allocbench.benchmark.get_benchmark_object(benchmark)
        except Exception:  #pylint: disable=broad-except
            logger.error("Skipping %s. Loading failed", benchmark)
            continue

        try:
            bench.load()
        except FileNotFoundError:
            continue

        if not hasattr(bench, "summary"):
            continue

        print_status(f"Summarizing {bench.name} ...")
        try:
            bench_sum(bench, exclude_allocators=exclude_allocators, sets=sets)
        except FileExistsError as err:
            logger.error("%s", err)

    os.chdir(cwd)


def main():
    """Summarize the results of an allocbench run"""
    parser = argparse.ArgumentParser(
        description="Summarize allocbench results in allocator sets")
    parser.add_argument("results", help="path to results", type=str)
    parser.add_argument("-t",
                        "--file-ext",
                        help="file extension used for plots",
                        type=str)
    parser.add_argument("--license",
                        help="print license info and exit",
                        action='store_true')
    parser.add_argument("--version",
                        help="print version info and exit",
                        action='version',
                        version=f"allocbench {facter.allocbench_version()}")
    parser.add_argument("-v",
                        "--verbose",
                        help="more output",
                        action='count',
                        default=0)
    parser.add_argument("-b",
                        "--benchmarks",
                        help="benchmarks to summarize",
                        nargs='+')
    parser.add_argument("-x",
                        "--exclude-benchmarks",
                        help="benchmarks to exclude",
                        nargs='+')
    parser.add_argument("-xa",
                        "--exclude-allocators",
                        help="allocators to exclude",
                        nargs='+')
    parser.add_argument(
        "--latex-preamble",
        help="latex code to include in the preamble of generated standalones",
        type=str)
    parser.add_argument("-i",
                        "--interactive",
                        help="drop into repl after summarizing the results",
                        action='store_true')
    parser.add_argument("-s",
                        "--sets",
                        help="create summary for sets of allocators",
                        action='store_true')

    args = parser.parse_args()

    set_verbosity(args.verbose)

    if args.file_ext:
        allocbench.plots.summary_file_ext = args.file_ext

    if args.latex_preamble:
        allocbench.plots.latex_custom_preamble = args.latex_preamble

    if not os.path.isdir(args.results):
        logger.critical("%s is no directory", args.results)
        sys.exit(1)

    set_current_result_dir(args.results)

    # Load facts
    facter.load_facts(get_current_result_dir())

    summarize(benchmarks=args.benchmarks,
              exclude_benchmarks=args.exclude_benchmarks,
              exclude_allocators=args.exclude_allocators,
              sets=args.sets)

    if args.interactive:
        try:
            import IPython  # pylint: disable=import-outside-toplevel
            IPython.embed()
        except ModuleNotFoundError:
            import code  # pylint: disable=import-outside-toplevel
            code.InteractiveConsole(locals=globals()).interact()


if __name__ == "__main__":
    if "--license" in sys.argv:
        print_license_and_exit()

    main()