test/jdk/sun/nio/ch/TestMaxCachedBufferSize.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 38557 jdk/test/sun/nio/ch/TestMaxCachedBufferSize.java@5c485e1ea6fa
child 50719 106dc156ce6b
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     1
/*
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     4
 *
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     8
 *
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    13
 * accompanied this code).
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    14
 *
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    18
 *
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    21
 * questions.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    22
 */
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    23
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    24
import java.io.IOException;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    25
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    26
import java.lang.management.BufferPoolMXBean;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    27
import java.lang.management.ManagementFactory;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    28
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    29
import java.nio.ByteBuffer;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    30
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    31
import java.nio.channels.FileChannel;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    32
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    33
import java.nio.file.Path;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    34
import java.nio.file.Paths;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    35
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    36
import static java.nio.file.StandardOpenOption.CREATE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    37
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    38
import static java.nio.file.StandardOpenOption.WRITE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    39
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    40
import java.util.List;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    41
import java.util.Random;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    42
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    43
/*
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    44
 * @test
35398
e65d11da9bc9 8148627: RestrictTestMaxCachedBufferSize.java to 64-bit platforms
darcy
parents: 35386
diff changeset
    45
 * @requires sun.arch.data.model == "64"
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 35398
diff changeset
    46
 * @modules java.management
35386
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    47
 * @build TestMaxCachedBufferSize
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    48
 * @run main/othervm TestMaxCachedBufferSize
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    49
 * @run main/othervm -Djdk.nio.maxCachedBufferSize=0 TestMaxCachedBufferSize
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    50
 * @run main/othervm -Djdk.nio.maxCachedBufferSize=2000 TestMaxCachedBufferSize
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    51
 * @run main/othervm -Djdk.nio.maxCachedBufferSize=100000 TestMaxCachedBufferSize
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    52
 * @run main/othervm -Djdk.nio.maxCachedBufferSize=10000000 TestMaxCachedBufferSize
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    53
 * @summary Test the implementation of the jdk.nio.maxCachedBufferSize property.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    54
 */
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    55
public class TestMaxCachedBufferSize {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    56
    private static final int DEFAULT_ITERS = 10 * 1000;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    57
    private static final int DEFAULT_THREAD_NUM = 4;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    58
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    59
    private static final int SMALL_BUFFER_MIN_SIZE =  4 * 1024;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    60
    private static final int SMALL_BUFFER_MAX_SIZE = 64 * 1024;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    61
    private static final int SMALL_BUFFER_DIFF_SIZE =
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    62
                                 SMALL_BUFFER_MAX_SIZE - SMALL_BUFFER_MIN_SIZE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    63
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    64
    private static final int LARGE_BUFFER_MIN_SIZE =      512 * 1024;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    65
    private static final int LARGE_BUFFER_MAX_SIZE = 4 * 1024 * 1024;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    66
    private static final int LARGE_BUFFER_DIFF_SIZE =
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    67
                                 LARGE_BUFFER_MAX_SIZE - LARGE_BUFFER_MIN_SIZE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    68
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    69
    private static final int LARGE_BUFFER_FREQUENCY = 100;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    70
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    71
    private static final String FILE_NAME_PREFIX = "nio-out-file-";
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    72
    private static final int VERBOSE_PERIOD = 5 * 1000;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    73
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    74
    private static int iters = DEFAULT_ITERS;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    75
    private static int threadNum = DEFAULT_THREAD_NUM;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    76
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    77
    private static BufferPoolMXBean getDirectPool() {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    78
        final List<BufferPoolMXBean> pools =
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    79
                  ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    80
        for (BufferPoolMXBean pool : pools) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    81
            if (pool.getName().equals("direct")) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    82
                return pool;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    83
            }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    84
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    85
        throw new Error("could not find direct pool");
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    86
    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    87
    private static final BufferPoolMXBean directPool = getDirectPool();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    88
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    89
    // Each worker will do write operations on a file channel using
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    90
    // buffers of various sizes. The buffer size is randomly chosen to
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    91
    // be within a small or a large range. This way we can control
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    92
    // which buffers can be cached (all, only the small ones, or none)
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    93
    // by setting the jdk.nio.maxCachedBufferSize property.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    94
    private static class Worker implements Runnable {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    95
        private final int id;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    96
        private final Random random = new Random();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    97
        private long smallBufferCount = 0;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    98
        private long largeBufferCount = 0;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
    99
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   100
        private int getWriteSize() {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   101
            int minSize = 0;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   102
            int diff = 0;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   103
            if (random.nextInt() % LARGE_BUFFER_FREQUENCY != 0) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   104
                // small buffer
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   105
                minSize = SMALL_BUFFER_MIN_SIZE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   106
                diff = SMALL_BUFFER_DIFF_SIZE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   107
                smallBufferCount += 1;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   108
            } else {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   109
                // large buffer
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   110
                minSize = LARGE_BUFFER_MIN_SIZE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   111
                diff = LARGE_BUFFER_DIFF_SIZE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   112
                largeBufferCount += 1;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   113
            }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   114
            return minSize + random.nextInt(diff);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   115
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   116
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   117
        private void loop() {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   118
            final String fileName = String.format("%s%d", FILE_NAME_PREFIX, id);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   119
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   120
            try {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   121
                for (int i = 0; i < iters; i += 1) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   122
                    final int writeSize = getWriteSize();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   123
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   124
                    // This will allocate a HeapByteBuffer. It should not
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   125
                    // be a direct buffer, otherwise the write() method on
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   126
                    // the channel below will not create a temporary
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   127
                    // direct buffer for the write.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   128
                    final ByteBuffer buffer = ByteBuffer.allocate(writeSize);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   129
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   130
                    // Put some random data on it.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   131
                    while (buffer.hasRemaining()) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   132
                        buffer.put((byte) random.nextInt());
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   133
                    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   134
                    buffer.rewind();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   135
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   136
                    final Path file = Paths.get(fileName);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   137
                    try (FileChannel outChannel = FileChannel.open(file, CREATE, TRUNCATE_EXISTING, WRITE)) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   138
                        // The write() method will create a temporary
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   139
                        // direct buffer for the write and attempt to cache
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   140
                        // it. It's important that buffer is not a
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   141
                        // direct buffer, otherwise the temporary buffer
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   142
                        // will not be created.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   143
                        long res = outChannel.write(buffer);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   144
                    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   145
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   146
                    if ((i + 1) % VERBOSE_PERIOD == 0) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   147
                        System.out.printf(
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   148
                          " Worker %3d | %8d Iters | Small %8d Large %8d | Direct %4d / %7dK\n",
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   149
                          id, i + 1, smallBufferCount, largeBufferCount,
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   150
                          directPool.getCount(), directPool.getTotalCapacity() / 1024);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   151
                    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   152
                }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   153
            } catch (IOException e) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   154
                throw new Error("I/O error", e);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   155
            }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   156
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   157
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   158
        @Override
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   159
        public void run() {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   160
            loop();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   161
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   162
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   163
        public Worker(int id) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   164
            this.id = id;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   165
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   166
    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   167
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   168
    public static void checkDirectBuffers(long expectedCount, long expectedMax) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   169
        final long directCount = directPool.getCount();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   170
        final long directTotalCapacity = directPool.getTotalCapacity();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   171
        System.out.printf("Direct %d / %dK\n",
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   172
                          directCount, directTotalCapacity / 1024);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   173
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   174
        // Note that directCount could be < expectedCount. This can
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   175
        // happen if a GC occurs after one of the worker threads exits
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   176
        // since its thread-local DirectByteBuffer could be cleaned up
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   177
        // before we reach here.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   178
        if (directCount > expectedCount) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   179
            throw new Error(String.format(
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   180
                "inconsistent direct buffer total count, expected = %d, found = %d",
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   181
                expectedCount, directCount));
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   182
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   183
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   184
        if (directTotalCapacity > expectedMax) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   185
            throw new Error(String.format(
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   186
                "inconsistent direct buffer total capacity, expectex max = %d, found = %d",
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   187
                expectedMax, directTotalCapacity));
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   188
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   189
    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   190
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   191
    public static void main(String[] args) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   192
        final String maxBufferSizeStr = System.getProperty("jdk.nio.maxCachedBufferSize");
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   193
        final long maxBufferSize =
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   194
            (maxBufferSizeStr != null) ? Long.valueOf(maxBufferSizeStr) : Long.MAX_VALUE;
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   195
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   196
        // We assume that the max cannot be equal to a size of a
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   197
        // buffer that can be allocated (makes sanity checking at the
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   198
        // end easier).
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   199
        if ((SMALL_BUFFER_MIN_SIZE <= maxBufferSize &&
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   200
                                     maxBufferSize <= SMALL_BUFFER_MAX_SIZE) ||
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   201
            (LARGE_BUFFER_MIN_SIZE <= maxBufferSize &&
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   202
                                     maxBufferSize <= LARGE_BUFFER_MAX_SIZE)) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   203
            throw new Error(String.format("max buffer size = %d not allowed",
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   204
                                          maxBufferSize));
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   205
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   206
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   207
        System.out.printf("Threads %d | Iterations %d | MaxBufferSize %d\n",
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   208
                          threadNum, iters, maxBufferSize);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   209
        System.out.println();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   210
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   211
        final Thread[] threads = new Thread[threadNum];
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   212
        for (int i = 0; i < threadNum; i += 1) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   213
            threads[i] = new Thread(new Worker(i));
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   214
            threads[i].start();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   215
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   216
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   217
        try {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   218
            for (int i = 0; i < threadNum; i += 1) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   219
                threads[i].join();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   220
            }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   221
        } catch (InterruptedException e) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   222
            throw new Error("join() interrupted!", e);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   223
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   224
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   225
        // There is an assumption here that, at this point, only the
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   226
        // cached DirectByteBuffers should be active. Given we
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   227
        // haven't used any other DirectByteBuffers in this test, this
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   228
        // should hold.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   229
        //
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   230
        // Also note that we can only do the sanity checking at the
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   231
        // end and not during the run given that, at any time, there
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   232
        // could be buffers currently in use by some of the workers
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   233
        // that will not be cached.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   234
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   235
        System.out.println();
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   236
        if (maxBufferSize < SMALL_BUFFER_MAX_SIZE) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   237
            // The max buffer size is smaller than all buffers that
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   238
            // were allocated. No buffers should have been cached.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   239
            checkDirectBuffers(0, 0);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   240
        } else if (maxBufferSize < LARGE_BUFFER_MIN_SIZE) {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   241
            // The max buffer size is larger than all small buffers
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   242
            // but smaller than all large buffers that were
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   243
            // allocated. Only small buffers could have been cached.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   244
            checkDirectBuffers(threadNum,
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   245
                               (long) threadNum * (long) SMALL_BUFFER_MAX_SIZE);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   246
        } else {
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   247
            // The max buffer size is larger than all buffers that
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   248
            // were allocated. All buffers could have been cached.
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   249
            checkDirectBuffers(threadNum,
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   250
                               (long) threadNum * (long) LARGE_BUFFER_MAX_SIZE);
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   251
        }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   252
    }
a54f3c20e83d 8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
diff changeset
   253
}