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 
  36     public MetaspaceTestOneArenaManyThreads(MetaspaceTestContext context, long testAllocationCeiling, int numThreads, int seconds) {
  37         super(context, testAllocationCeiling, numThreads, seconds);
  38     }
  39 
  40     public void runTest() throws Exception {
  41 
  42         long t_start = currentTimeMillis();
  43         long t_stop = t_start + (seconds * 1000);
  44 
  45         // We create a single arena, and n threads which will allocate from that single arena.
  46 
  47         MetaspaceTestArena arena = context.createArena(RandomHelper.fiftyfifty(), testAllocationCeiling);
  48         CyclicBarrier gate = new CyclicBarrier(numThreads + 1);
  49 
  50         for (int i = 0; i < numThreads; i ++) {
  51             RandomAllocator allocator = new RandomAllocator(arena);
  52             RandomAllocatorThread thread = new RandomAllocatorThread(gate, allocator, i);
  53             threads[i] = thread;
  54             thread.start();
  55         }
  56 
  57         gate.await();
  58 
  59         while (System.currentTimeMillis() < t_stop) {
  60             Thread.sleep(200);
  61         }
  62 
  63         stopAllThreads();
  64 
  65         context.updateTotals();
  66         System.out.println("  ## Finished: " + context);
  67 
  68         context.checkStatistics();
  69 
  70         context.destroyArena(arena);
  71 
  72         context.purge();
  73 
  74         context.destroy();
  75 
  76         System.out.println("This took " + (System.currentTimeMillis() - t_start) + "ms");
  77 
  78     }
  79 
  80 }
  81