ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
/**
 * A test driver for JIT compiling methods and looking for JIT
 * cache issues.
 */
public class JitCacheChurnTest {
  /* The name of methods to JIT */
  private static final String JITTED_METHOD = "$noinline$Call";
 
  /* The number of cores to oversubscribe load by. */
  private static final int OVERSUBSCRIBED_CORES = 1;
 
  /* The number of concurrent executions of methods to be JIT compiled. */
  private static final int CONCURRENCY =
      Runtime.getRuntime().availableProcessors() + OVERSUBSCRIBED_CORES;
 
  /* The number of times the methods to be JIT compiled should be executed per thread. */
  private static final int METHOD_ITERATIONS = 10;
 
  /* Number of test iterations JIT methods and removing methods from JIT cache. */
  private static final int TEST_ITERATIONS = 512;
 
  /* Tasks to run and generate compiled code of various sizes */
  private static final BaseTask [] TASKS = {
    new TaskOne(), new TaskTwo(), new TaskThree(), new TaskFour(), new TaskFive(), new TaskSix(),
    new TaskSeven(), new TaskEight(), new TaskNine(), new TaskTen()
  };
  private static final int TASK_BITMASK = (1 << TASKS.length) - 1;
 
  private final ExecutorService executorService;
  private int runMask = 0;
 
  private JitCacheChurnTest() {
    this.executorService = new ThreadPoolExecutor(CONCURRENCY, CONCURRENCY, 5000,
        TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>());
  }
 
  private void shutdown() {
    this.executorService.shutdown();
  }
 
  private void runTasks(Callable<Integer> task) {
    // Force JIT compilation of tasks method.
    ensureJitCompiled(task.getClass(), JITTED_METHOD);
 
    // Launch worker threads to run JIT compiled method.
    try {
      ArrayList<Callable<Integer>> tasks = new ArrayList<>(CONCURRENCY);
      for (int i = 0; i < CONCURRENCY; ++i) {
        tasks.add(i, task);
      }
 
      List<Future<Integer>> results = executorService.invokeAll(tasks);
      for (Future<?> result : results) {
        result.get();
      }
    } catch (InterruptedException | ExecutionException e) {
      System.err.println(e);
      System.exit(-1);
    }
  }
 
  private static abstract class BaseTask implements Callable<Integer> {
    private static CyclicBarrier barrier = new CyclicBarrier(CONCURRENCY);
 
    public Integer call() throws Exception {
      barrier.await();
      int iterations = METHOD_ITERATIONS + 1;
      for (int i = 0; i < iterations; ++i) {
        $noinline$Call();
      }
      return $noinline$Call();
    }
 
    protected abstract Integer $noinline$Call();
  }
 
  private static class TaskOne extends BaseTask {
    @Override
    protected Integer $noinline$Call() {
      return null;
    }
  }
 
  private static class TaskTwo extends BaseTask {
    @Override
    protected Integer $noinline$Call() {
      return 0;
    }
  }
 
  private static class TaskThree extends BaseTask {
    @Override
    protected Integer $noinline$Call() {
      int sum = 0;
      for (int i = 0; i < 3; ++i) {
        sum = i * (i + 1);
      }
      return sum;
    }
  }
 
  private static class TaskFour extends BaseTask {
    @Override
    protected Integer $noinline$Call() {
      int sum = 0;
      for (int i = 0; i < 10; ++i) {
        int bits = i;
        bits = ((bits >>> 1) & 0x55555555) | ((bits << 1) & 0x55555555);
        bits = ((bits >>> 2) & 0x33333333) | ((bits << 2) & 0x33333333);
        bits = ((bits >>> 4) & 0x0f0f0f0f) | ((bits << 4) & 0x0f0f0f0f);
        bits = ((bits >>> 8) & 0x00ff00ff) | ((bits << 8) & 0x00ff00ff);
        bits = (bits >>> 16) | (bits << 16);
        sum += bits;
      }
      return sum;
    }
  }
 
  private static class TaskFive extends BaseTask {
    static final AtomicInteger instances = new AtomicInteger(0);
    int instance;
    TaskFive() {
      instance = instances.getAndIncrement();
    }
    protected Integer $noinline$Call() {
      return instance;
    }
  }
 
  private static class TaskSix extends TaskFive {
    protected Integer $noinline$Call() {
      return instance + 1;
    }
  }
 
  private static class TaskSeven extends TaskFive {
    protected Integer $noinline$Call() {
      return 2 * instance + 1;
    }
  }
 
  private static class TaskEight extends TaskFive {
    protected Integer $noinline$Call() {
      double a = Math.cosh(2.22 * instance);
      double b = a / 2;
      double c = b * 3;
      double d = a + b + c;
      if (d > 42) {
        d *= Math.max(Math.sin(d), Math.sinh(d));
        d *= Math.max(1.33, 0.17 * Math.sinh(d));
        d *= Math.max(1.34, 0.21 * Math.sinh(d));
        d *= Math.max(1.35, 0.32 * Math.sinh(d));
        d *= Math.max(1.36, 0.41 * Math.sinh(d));
        d *= Math.max(1.37, 0.57 * Math.sinh(d));
        d *= Math.max(1.38, 0.61 * Math.sinh(d));
        d *= Math.max(1.39, 0.79 * Math.sinh(d));
        d += Double.parseDouble("3.711e23");
      }
 
      if (d > 3) {
        return (int) a;
      } else {
        return (int) b;
      }
    }
  }
 
  private static class TaskNine extends TaskFive {
    private final String [] numbers = { "One", "Two", "Three", "Four", "Five", "Six" };
 
    protected Integer $noinline$Call() {
      String number = numbers[instance % numbers.length];
      return number.length();
    }
  }
 
  private static class TaskTen extends TaskFive {
    private final String [] numbers = { "12345", "23451", "34512", "78901", "89012" };
 
    protected Integer $noinline$Call() {
      int odd = 0;
      String number = numbers[instance % numbers.length];
      for (int i = 0; i < number.length(); i += 2) {
        odd += Integer.parseInt(numbers[i]);
      }
      odd *= 3;
 
      int even = 0;
      for (int i = 1; i < number.length(); i += 2) {
        even += Integer.parseInt(numbers[i]);
      }
      return (odd + even) % 10;
    }
  }
 
  private void runAndJitMethods(int mask) {
    runMask |= mask;
    for (int index = 0; mask != 0; mask >>= 1, index++) {
      if ((mask & 1) == 1) {
        runTasks(TASKS[index]);
      }
    }
  }
 
  private static void ensureJitCompiled(Class<?> klass, String name) {
    Main.ensureJitCompiled(klass, name);
  }
 
  private void removeJittedMethod(Class<?> klass, String name) {
    Method method = null;
    try {
      method = klass.getDeclaredMethod(name);
    } catch (NoSuchMethodException e) {
      System.err.println(e);
      System.exit(-1);
    }
    removeJitCompiledMethod(method, false);
  }
 
  private void removeJittedMethods(int mask) {
    mask = mask & runMask;
    runMask ^= mask;
    for (int index = 0; mask != 0; mask >>= 1, index++) {
      if ((mask & 1) == 1) {
        removeJittedMethod(TASKS[index].getClass(), JITTED_METHOD);
      }
    }
  }
 
  private static int getMethodsAsMask(Random rng) {
    return rng.nextInt(TASK_BITMASK) + 1;
  }
 
  public static void run() {
    JitCacheChurnTest concurrentExecution = new JitCacheChurnTest();
    Random invokeMethodGenerator = new Random(5);
    Random removeMethodGenerator = new Random(7);
    try {
      for (int i = 0; i < TEST_ITERATIONS; ++i) {
        concurrentExecution.runAndJitMethods(getMethodsAsMask(invokeMethodGenerator));
        concurrentExecution.removeJittedMethods(getMethodsAsMask(removeMethodGenerator));
      }
    } finally {
      concurrentExecution.shutdown();
    }
  }
 
  private static native void removeJitCompiledMethod(Method method, boolean releaseMemory);
}