jdk/test/java/util/concurrent/locks/ReentrantReadWriteLock/MapLoops.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4347 ab0a9f495844
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @bug 4486658
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @compile -source 1.5 MapLoops.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/timeout=4700 MapLoops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @summary Exercise multithreaded maps, by default ConcurrentHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Multithreaded hash table test.  Each thread does a random walk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * though elements of "key" array. On each iteration, it checks if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * table includes key.  If absent, with probability pinsert it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * inserts it, and if present, with probability premove it removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * it.  (pinsert and premove are expressed as percentages to simplify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * parsing from command line.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class MapLoops {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final int NKEYS = 100000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static int pinsert     = 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static int premove     = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static int maxThreads  = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static int nops        = 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static int removesPerMaxRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static int insertsPerMaxRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final ExecutorService pool = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        Class mapClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                mapClass = Class.forName(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } catch(ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                throw new RuntimeException("Class " + args[0] + " not found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            mapClass = RWMap.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (args.length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            maxThreads = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (args.length > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            nops = Integer.parseInt(args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (args.length > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            pinsert = Integer.parseInt(args[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (args.length > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            premove = Integer.parseInt(args[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // normalize probabilities wrt random number generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        System.out.println("Using " + mapClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Random rng = new Random(315312);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Integer[] key = new Integer[NKEYS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        for (int i = 0; i < key.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            key[i] = new Integer(rng.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // warmup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.out.println("Warmup...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        for (int k = 0; k < 2; ++k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            CyclicBarrier barrier = new CyclicBarrier(1, timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            new Runner(map, key, barrier).run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            map.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.print("Threads: " + i + "\t:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            for (int k = 0; k < i; ++k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                pool.execute(new Runner(map, key, barrier));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            long time = timer.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            long tpo = time / (i * (long)nops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            double secs = (double)(time) / 1000000000.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("\t " + secs + "s run time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            map.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static class Runner implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        final Map<Integer,Integer> map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        final Integer[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        final CyclicBarrier barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Runner(Map<Integer,Integer> map, Integer[] key,  CyclicBarrier barrier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            this.map = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            this.barrier = barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            position = key.length / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int step() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // random-walk around key positions,  bunching accesses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            int r = rng.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            position += (r & 7) - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            while (position >= key.length) position -= key.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            while (position < 0) position += key.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            Integer k = key[position];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            Integer x = map.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (x.intValue() != k.intValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    throw new Error("bad mapping: " + x + " to " + k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (r < removesPerMaxRandom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    // get awy from this position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    position = r % key.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    map.remove(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    total += LoopHelpers.compute2(LoopHelpers.compute1(x.intValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (r < insertsPerMaxRandom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    map.put(k, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                int ops = nops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                while (ops > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    ops -= step();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}