1#
2# Copyright 2016 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import sys
18
19max_conflict_depth = 20 # In practice does not go above 20 for reasonable IMT sizes
20try:
21    imt_size = int(sys.argv[1])
22except (IndexError, ValueError):
23    print("Usage: python ImtConflictBenchmarkGen.py <IMT_SIZE>")
24    sys.exit(1)
25
26license = """\
27/*
28 * Copyright 2016 The Android Open Source Project
29 *
30 * Licensed under the Apache License, Version 2.0 (the "License");
31 * you may not use this file except in compliance with the License.
32 * You may obtain a copy of the License at
33 *
34 *      http://www.apache.org/licenses/LICENSE-2.0
35 *
36 * Unless required by applicable law or agreed to in writing, software
37 * distributed under the License is distributed on an "AS IS" BASIS,
38 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 * See the License for the specific language governing permissions and
40 * limitations under the License.
41 */
42"""
43description = """
44/**
45 * This file is script-generated by ImtConflictBenchmarkGen.py.
46 * It measures the performance impact of conflicts in interface method tables.
47 * Run `python ImtConflictBenchmarkGen.py > ImtConflictBenchmark.java` to regenerate.
48 *
49 * Each interface has 64 methods, which is the current size of an IMT. C0 implements
50 * one interface, C1 implements two, C2 implements three, and so on. The intent
51 * is that C0 has no conflicts in its IMT, C1 has depth-2 conflicts in
52 * its IMT, C2 has depth-3 conflicts, etc. This is currently guaranteed by
53 * the fact that we hash interface methods by taking their method index modulo 64.
54 * (Note that a "conflict depth" of 1 means no conflict at all.)
55 */\
56"""
57
58print(license)
59print("package benchmarks;")
60print("import com.google.caliper.BeforeExperiment;")
61print(description)
62
63print("public class ImtConflictBenchmark {")
64
65# Warm up interface method tables
66print("    @BeforeExperiment")
67print("    public void setup() {")
68for i in xrange(max_conflict_depth):
69    print("        C{0} c{0} = new C{0}();".format(i))
70    for j in xrange(i+1):
71        print("        callF{}(c{});".format(imt_size * j, i))
72print("    }")
73
74# Print test cases--one for each conflict depth
75for i in xrange(max_conflict_depth):
76    print("    public void timeConflictDepth{:02d}(int nreps) {{".format(i+1))
77    print("        C{0} c{0} = new C{0}();".format(i))
78    print("        for (int i = 0; i < nreps; i++) {")
79    # Cycle through each interface method in an IMT entry in order
80    # to test all conflict resolution possibilities
81    for j in xrange(max_conflict_depth):
82        print("            callF{}(c{});".format(imt_size * (j % (i + 1)), i))
83    print("        }")
84    print("    }")
85
86# Make calls through the IMTs
87for i in xrange(max_conflict_depth):
88    print("    public void callF{0}(I{1} i) {{ i.f{0}(); }}".format(imt_size*i, i))
89
90# Class definitions, implementing varying amounts of interfaces
91for i in xrange(max_conflict_depth):
92    interfaces = ", ".join(["I{}".format(j) for j in xrange(i+1)])
93    print("    static class C{} implements {} {{}}".format(i, interfaces))
94
95# Interface definitions, each with enough methods to fill an entire IMT
96for i in xrange(max_conflict_depth):
97    print("    static interface I{} {{".format(i))
98    for j in xrange(imt_size):
99        print("        default void f{}() {{}}".format(i*imt_size + j))
100    print("    }")
101
102print "}"
103