view src/main/java/org/openjdk/gcbench/roots/Synchronizers.java @ 88:583fef4276f5

Update license and copyright headers.
author shade
date Wed, 22 Nov 2017 15:58:02 +0100
parents 7b87f91c5db9
children
line wrap: on
line source

/*
 * Copyright (c) 2017, Red Hat Inc. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package org.openjdk.gcbench.roots;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.infra.Control;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1, jvmArgsAppend = {"-Xss32m"})
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Threads(Threads.MAX)
@State(Scope.Thread)
public class Synchronizers {

    List<Object> list;

    @Param({"40000"})
    private int size;

    @Setup
    public void setup() {
        list = new ArrayList<>();
        for (int c = 0; c < size; c++) {
            list.add(new Object());
        }
    }

    @Benchmark
    public void test(Control cnt, Blackhole bh) throws InterruptedException {
        recursiveLock(cnt, bh, list, 0);
    }

    private void recursiveLock(Control cnt, Blackhole bh, List<Object> list, int i) {
        if (i < list.size()) {
            Object o0 = list.get(i + 0);
            Object o1 = list.get(i + 1);
            Object o2 = list.get(i + 2);
            Object o3 = list.get(i + 3);
            Object o4 = list.get(i + 4);
            Object o5 = list.get(i + 5);
            Object o6 = list.get(i + 6);
            Object o7 = list.get(i + 7);
            Object o8 = list.get(i + 8);
            Object o9 = list.get(i + 9);
            synchronized (o0) {
                synchronized (o1) {
                    synchronized (o2) {
                        synchronized (o3) {
                            synchronized (o4) {
                                synchronized (o5) {
                                    synchronized (o6) {
                                        synchronized (o7) {
                                            synchronized (o8) {
                                                synchronized (o9) {
                                                    recursiveLock(cnt, bh, list, i + 10);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            for (Object o : list) {
                bh.consume(o.hashCode());
            }
            if (cnt.startMeasurement) {
                // Do not leave until we are finished
                while (!cnt.stopMeasurement) {
                    bh.consume(new Object());
                }
            }
        }
    }

}