PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/114-ParallelGC/src/Main.java

https://gitlab.com/androidopensourceproject/platform-art
Java | 142 lines | 82 code | 23 blank | 37 comment | 11 complexity | e449d5f319dfa741c77ea9176fea02b7 MD5 | raw file
  1. /*
  2. * Copyright (C) 2011 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. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.concurrent.atomic.AtomicInteger;
  19. import java.util.concurrent.CyclicBarrier;
  20. public class Main implements Runnable {
  21. // Timeout in minutes. Make it larger than the run-test timeout to get a native thread dump by
  22. // ART on timeout when running on the host.
  23. private final static long TIMEOUT_VALUE = 7;
  24. private final static long MAX_SIZE = 1000; // Maximum size of array-list to allocate.
  25. private final static int THREAD_COUNT = 16;
  26. // Use a couple of different forms of synchronizing to test some of these...
  27. private final static AtomicInteger counter = new AtomicInteger();
  28. private final static Object gate = new Object();
  29. private volatile static int waitCount = 0;
  30. public static void main(String[] args) throws Exception {
  31. Thread[] threads = new Thread[THREAD_COUNT];
  32. // This barrier is used to synchronize the threads starting to allocate.
  33. // Note: Even though a barrier is not allocation-free, this one is fine, as it will be used
  34. // before filling the heap.
  35. CyclicBarrier startBarrier = new CyclicBarrier(threads.length);
  36. for (int i = 0; i < threads.length; i++) {
  37. threads[i] = new Thread(new Main(startBarrier));
  38. threads[i].start();
  39. }
  40. // Wait for the threads to finish.
  41. for (Thread thread : threads) {
  42. thread.join();
  43. }
  44. // Allocate objects to definitely run GC before quitting.
  45. allocateObjectsToRunGc();
  46. new ArrayList<Object>(50);
  47. }
  48. private static void allocateObjectsToRunGc() {
  49. ArrayList<Object> l = new ArrayList<Object>();
  50. try {
  51. for (int i = 0; i < 100000; i++) {
  52. l.add(new ArrayList<Object>(i));
  53. }
  54. } catch (OutOfMemoryError oom) {
  55. }
  56. }
  57. private Main(CyclicBarrier startBarrier) {
  58. this.startBarrier = startBarrier;
  59. }
  60. private ArrayList<Object> store;
  61. private CyclicBarrier startBarrier;
  62. public void run() {
  63. try {
  64. work();
  65. } catch (Throwable t) {
  66. // Any exception or error getting here is bad.
  67. try {
  68. // May need allocations...
  69. t.printStackTrace(System.out);
  70. } catch (Throwable tInner) {
  71. }
  72. System.exit(1);
  73. }
  74. }
  75. private void work() throws Exception {
  76. // Any exceptions except an OOME in the allocation loop are bad and handed off to the
  77. // caller which should abort the whole runtime.
  78. ArrayList<Object> l = new ArrayList<Object>();
  79. store = l; // Keep it alive.
  80. // Wait for the start signal.
  81. startBarrier.await(TIMEOUT_VALUE, java.util.concurrent.TimeUnit.MINUTES);
  82. // Allocate.
  83. try {
  84. for (int i = 0; i < MAX_SIZE; i++) {
  85. l.add(new ArrayList<Object>(i));
  86. }
  87. } catch (OutOfMemoryError oome) {
  88. // Fine, we're done.
  89. }
  90. // Atomically increment the counter and check whether we were last.
  91. int number = counter.incrementAndGet();
  92. if (number < THREAD_COUNT) {
  93. // Not last.
  94. synchronized (gate) {
  95. // Increment the wait counter.
  96. waitCount++;
  97. gate.wait(TIMEOUT_VALUE * 1000 * 60);
  98. }
  99. } else {
  100. // Last. Wait until waitCount == THREAD_COUNT - 1.
  101. for (int loops = 0; ; loops++) {
  102. synchronized (gate) {
  103. if (waitCount == THREAD_COUNT - 1) {
  104. // OK, everyone's waiting. Notify and break out.
  105. gate.notifyAll();
  106. break;
  107. } else if (loops > 40) {
  108. // 1s wait, too many tries.
  109. System.out.println("Waited too long for the last thread.");
  110. System.exit(1);
  111. }
  112. }
  113. // Wait a bit.
  114. Thread.sleep(25);
  115. }
  116. }
  117. store = null; // Allow GC to reclaim it.
  118. }
  119. }