test/jdk/java/util/concurrent/ConcurrentHashMap/LoopHelpers.java
author dl
Tue, 16 Jan 2018 18:28:39 -0800
changeset 48541 946e34c2dec9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01 Reviewed-by: martin
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * questions.
2
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
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
2
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
 * Misc utilities in JSR166 performance tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class LoopHelpers {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // Some mindless computation to do between synchronizations...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * generates 32 bit pseudo-random numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * Adapted from http://www.snippets.org
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static int compute1(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int lo = 16807 * (x & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        int hi = 16807 * (x >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        lo += (hi & 0x7FFF) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if ((lo & 0x80000000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            lo &= 0x7fffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            ++lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        lo += hi >>> 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (lo == 0 || (lo & 0x80000000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            lo &= 0x7fffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            ++lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *  Computes a linear congruential random number a random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *  of times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static int compute2(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int loops = (x >>> 4) & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        while (loops-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            x = (x * 2147483647) % 16807;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static class BarrierTimer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public volatile long startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public volatile long endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            long t = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (startTime == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                startTime = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                endTime = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            startTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            endTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return endTime - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static String rightJustify(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // There's probably a better way to do this...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String field = "         ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        String num = Long.toString(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (num.length() >= field.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return num;
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    98
        StringBuilder b = new StringBuilder(field);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        b.replace(b.length()-num.length(), b.length(), num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return b.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}