< prev index next >

test/hotspot/jtreg/vmTestbase/vm/gc/concurrent/Concurrent.java

Print this page
rev 59093 : [mq]: randomness-code_vmTestbase_gc


   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package vm.gc.concurrent;
  24 
  25 import java.lang.management.ManagementFactory;
  26 import java.lang.management.MemoryMXBean;
  27 import java.lang.management.MemoryUsage;
  28 import java.util.Random;
  29 import java.util.concurrent.atomic.AtomicInteger;
  30 import java.util.concurrent.locks.Lock;
  31 import java.util.concurrent.locks.ReentrantLock;
  32 import nsk.share.TestFailure;
  33 import nsk.share.gc.GC;
  34 import nsk.share.gc.Memory;
  35 import nsk.share.gc.ThreadedGCTest;
  36 import nsk.share.gc.gp.GarbageProducer;
  37 import nsk.share.gc.gp.GarbageProducer1Aware;
  38 import nsk.share.gc.gp.GarbageProducerAware;
  39 import nsk.share.gc.gp.MemoryStrategy;
  40 import nsk.share.gc.gp.MemoryStrategyAware;
  41 import nsk.share.gc.tree.*;
  42 import nsk.share.log.Log;
  43 import nsk.share.test.ExecutionController;
  44 
  45 
  46 class Forest {
  47 
  48     // the actual size of TreeNode in bytes in the memory calculated as occupied memory / count of nodes
  49     static int nodeSize;
  50 
  51     static long treeSize;
  52 
  53     private static long allNodesCount;
  54 
  55     /* log from test */
  56     static Log log;
  57 
  58 
  59     static int treeHeight;
  60 
  61     static long actuallyMut = 0;
  62     private static Forest instance = new Forest();
  63     private Tree[] trees;
  64     private Lock[] locks;
  65     private static Random rnd = new Random();
  66 
  67     private int nodeGarbageSize;
  68 
  69     private GarbageProducer gp;
  70     /*
  71      * Create array of trees occupyng given percent of heap
  72      */
  73     static Forest createForest(long percent, int heightToSizeRatio, int nodeGarbageSize, GarbageProducer gp, Log _log) {
  74         log = _log;
  75 
  76         long size = Runtime.getRuntime().maxMemory() * percent / 100;
  77         treeHeight = Memory.balancedTreeHeightFromMemory(size, (int) new TreeNode(nodeGarbageSize).getTotalSize());
  78         int ntrees = 0;
  79         while (treeHeight * heightToSizeRatio > ntrees) {
  80             ntrees++;
  81             treeHeight = Memory.balancedTreeHeightFromMemory(size / ntrees, (int) new TreeNode(nodeGarbageSize).getTotalSize());
  82         }
  83 
  84         log.debug("The expected forest paramteres: tree height = " + treeHeight  + " number of trees = " + ntrees
  85                 + " size = " +  new TreeNode(nodeGarbageSize).getTotalSize());


 160                 tn2 = tn2.getRight();
 161             }
 162             path >>= 1;
 163         }
 164         TreeNode tmp;
 165         if ((path & 1) == 0) {
 166             tmp = tn1.getLeft();
 167             tn1.setLeft(tn2.getLeft());
 168             tn2.setLeft(tmp);
 169         } else {
 170             tmp = tn1.getRight();
 171             tn1.setRight(tn2.getRight());
 172             tn2.setLeft(tmp);
 173         }
 174     }
 175 
 176 
 177     // Interchanges two randomly selected subtrees (of same size and depth) several times
 178     void swapSubtrees(long count) {
 179         for (int i = 0; i < count; i++) {
 180             int index1 = rnd.nextInt(trees.length);
 181             int index2 = rnd.nextInt(trees.length);
 182             int depth = rnd.nextInt(treeHeight);
 183             int path = rnd.nextInt();
 184             locks[index1].lock();
 185             // Skip the round to avoid deadlocks
 186             if (locks[index2].tryLock()) {
 187                 swapSubtrees(trees[index1], trees[index2], depth, path);
 188                 actuallyMut += 2;
 189                 locks[index2].unlock();
 190             }
 191             locks[index1].unlock();
 192 
 193         }
 194 
 195     }
 196 
 197 
 198     static class AtomicCycleInteger extends AtomicInteger {
 199         private int max;
 200         public AtomicCycleInteger(int cycleLength) {
 201             super();
 202             this.max = cycleLength - 1;
 203         }




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package vm.gc.concurrent;
  24 
  25 import java.lang.management.ManagementFactory;
  26 import java.lang.management.MemoryMXBean;
  27 import java.lang.management.MemoryUsage;

  28 import java.util.concurrent.atomic.AtomicInteger;
  29 import java.util.concurrent.locks.Lock;
  30 import java.util.concurrent.locks.ReentrantLock;
  31 import nsk.share.TestFailure;
  32 import nsk.share.gc.GC;
  33 import nsk.share.gc.Memory;
  34 import nsk.share.gc.ThreadedGCTest;
  35 import nsk.share.gc.gp.GarbageProducer;
  36 import nsk.share.gc.gp.GarbageProducer1Aware;
  37 import nsk.share.gc.gp.GarbageProducerAware;
  38 import nsk.share.gc.gp.MemoryStrategy;
  39 import nsk.share.gc.gp.MemoryStrategyAware;
  40 import nsk.share.gc.tree.*;
  41 import nsk.share.log.Log;
  42 import nsk.share.test.ExecutionController;
  43 import nsk.share.test.LocalRandom;
  44 
  45 class Forest {
  46 
  47     // the actual size of TreeNode in bytes in the memory calculated as occupied memory / count of nodes
  48     static int nodeSize;
  49 
  50     static long treeSize;
  51 
  52     private static long allNodesCount;
  53 
  54     /* log from test */
  55     static Log log;
  56 
  57 
  58     static int treeHeight;
  59 
  60     static long actuallyMut = 0;
  61     private static Forest instance = new Forest();
  62     private Tree[] trees;
  63     private Lock[] locks;

  64 
  65     private int nodeGarbageSize;
  66 
  67     private GarbageProducer gp;
  68     /*
  69      * Create array of trees occupyng given percent of heap
  70      */
  71     static Forest createForest(long percent, int heightToSizeRatio, int nodeGarbageSize, GarbageProducer gp, Log _log) {
  72         log = _log;
  73 
  74         long size = Runtime.getRuntime().maxMemory() * percent / 100;
  75         treeHeight = Memory.balancedTreeHeightFromMemory(size, (int) new TreeNode(nodeGarbageSize).getTotalSize());
  76         int ntrees = 0;
  77         while (treeHeight * heightToSizeRatio > ntrees) {
  78             ntrees++;
  79             treeHeight = Memory.balancedTreeHeightFromMemory(size / ntrees, (int) new TreeNode(nodeGarbageSize).getTotalSize());
  80         }
  81 
  82         log.debug("The expected forest paramteres: tree height = " + treeHeight  + " number of trees = " + ntrees
  83                 + " size = " +  new TreeNode(nodeGarbageSize).getTotalSize());


 158                 tn2 = tn2.getRight();
 159             }
 160             path >>= 1;
 161         }
 162         TreeNode tmp;
 163         if ((path & 1) == 0) {
 164             tmp = tn1.getLeft();
 165             tn1.setLeft(tn2.getLeft());
 166             tn2.setLeft(tmp);
 167         } else {
 168             tmp = tn1.getRight();
 169             tn1.setRight(tn2.getRight());
 170             tn2.setLeft(tmp);
 171         }
 172     }
 173 
 174 
 175     // Interchanges two randomly selected subtrees (of same size and depth) several times
 176     void swapSubtrees(long count) {
 177         for (int i = 0; i < count; i++) {
 178             int index1 = LocalRandom.nextInt(trees.length);
 179             int index2 = LocalRandom.nextInt(trees.length);
 180             int depth = LocalRandom.nextInt(treeHeight);
 181             int path = LocalRandom.nextInt();
 182             locks[index1].lock();
 183             // Skip the round to avoid deadlocks
 184             if (locks[index2].tryLock()) {
 185                 swapSubtrees(trees[index1], trees[index2], depth, path);
 186                 actuallyMut += 2;
 187                 locks[index2].unlock();
 188             }
 189             locks[index1].unlock();
 190 
 191         }
 192 
 193     }
 194 
 195 
 196     static class AtomicCycleInteger extends AtomicInteger {
 197         private int max;
 198         public AtomicCycleInteger(int cycleLength) {
 199             super();
 200             this.max = cycleLength - 1;
 201         }


< prev index next >