1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 import java.util.concurrent.CyclicBarrier;
  27 
  28 import static java.lang.System.currentTimeMillis;
  29 
  30 public class MetaspaceTestOneArenaManyThreads extends MetaspaceTestWithThreads {
  31 
  32     // Several threads allocate from a single arena.
  33     // This mimicks several threads loading classes via the same class loader.
  34 
  35     public MetaspaceTestOneArenaManyThreads(MetaspaceTestContext context, long testAllocationCeiling, int numThreads, int seconds) {
  36         super(context, testAllocationCeiling, numThreads, seconds);
  37     }
  38 
  39     public void runTest() throws Exception {
  40 
  41         long t_start = currentTimeMillis();
  42         long t_stop = t_start + (seconds * 1000);
  43 
  44         // We create a single arena, and n threads which will allocate from that single arena.
  45 
  46         MetaspaceTestArena arena = context.createArena(RandomHelper.fiftyfifty(), testAllocationCeiling);
  47         CyclicBarrier gate = new CyclicBarrier(numThreads + 1);
  48 
  49         for (int i = 0; i < numThreads; i ++) {
  50             RandomAllocator allocator = new RandomAllocator(arena);
  51             RandomAllocatorThread thread = new RandomAllocatorThread(gate, allocator, i);
  52             threads[i] = thread;
  53             thread.start();
  54         }
  55 
  56         gate.await();
  57 
  58         while (System.currentTimeMillis() < t_stop) {
  59             Thread.sleep(200);
  60         }
  61 
  62         stopAllThreads();
  63 
  64         context.updateTotals();
  65         System.out.println("  ## Finished: " + context);
  66 
  67         context.checkStatistics();
  68 
  69         context.destroyArena(arena);
  70 
  71         context.purge();
  72 
  73         context.destroy();
  74 
  75         System.out.println("This took " + (System.currentTimeMillis() - t_start) + "ms");
  76 
  77     }
  78 
  79 }
  80