1 /*
2  * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
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.containers;
24 
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28 import nsk.share.gc.gp.GarbageProducer;
29 import nsk.share.gc.gp.MemoryStrategy;
30 import nsk.share.test.LocalRandom;
31 
32 /*
33  * The MapContainer select a certain amout of random elements and remove
34  * them. After this it put the same number of elements with random keys.
35  */
36 class MapContainer extends TypicalContainer {
37 
38     Map map;
39 
MapContainer(Map map, long maximumSize, GarbageProducer garbageProducer, MemoryStrategy memoryStrategy, Speed speed)40     public MapContainer(Map map, long maximumSize, GarbageProducer garbageProducer,
41             MemoryStrategy memoryStrategy, Speed speed) {
42         super(maximumSize, garbageProducer, memoryStrategy, speed);
43         this.map = map;
44     }
45 
46     @Override
initialize()47     public void initialize() {
48         for (int i = 0; i < count; i++) {
49             if (!stresser.continueExecution()) {
50                 return;
51             }
52             map.put(i, garbageProducer.create(size));
53         }
54     }
55 
56     @Override
update()57     public void update() {
58         Set<Integer> updated = new HashSet();
59         for (int i = 0; i < count * speed.getValue() / 100; i++) {
60             updated.add(LocalRandom.nextInt((int) count));
61         }
62         for (Integer i : updated) {
63             if (!stresser.continueExecution()) {
64                 return;
65             }
66             Object obj = map.remove(i);
67             if (obj != null) {
68                 garbageProducer.validate(obj);
69             }
70         }
71         for (Integer i : updated) {
72             if (!stresser.continueExecution()) {
73                 return;
74             }
75             map.put(i, garbageProducer.create(size));
76         }
77     }
78 }
79