test/hotspot/gtest/utilities/test_singleWriterSynchronizer.cpp
author kbarrett
Thu, 23 Aug 2018 18:14:53 -0400
changeset 51511 eb8d5aeabab3
child 51690 b83571bbc147
permissions -rw-r--r--
8209850: Allow NamedThreads to use GlobalCounter critical sections Summary: Add NamedThreads iterator and make GlobalCounter use it. Reviewed-by: eosterlund, rehn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51511
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     1
/*
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     4
 *
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     8
 *
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    14
 *
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    18
 *
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    21
 * questions.
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    22
 *
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    23
 */
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    24
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    25
#include "precompiled.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    26
#include "runtime/interfaceSupport.inline.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    27
#include "runtime/orderAccess.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    28
#include "runtime/os.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    29
#include "runtime/thread.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    30
#include "utilities/debug.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    31
#include "utilities/globalCounter.inline.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    32
#include "utilities/globalDefinitions.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    33
#include "utilities/ostream.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    34
#include "utilities/singleWriterSynchronizer.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    35
#include "threadHelper.inline.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    36
#include "unittest.hpp"
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    37
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    38
class SingleWriterSynchronizerTestReader : public JavaTestThread {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    39
  SingleWriterSynchronizer* _synchronizer;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    40
  volatile uintx* _synchronized_value;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    41
  volatile int* _continue_running;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    42
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    43
  static const uint reader_iterations = 10;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    44
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    45
public:
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    46
  SingleWriterSynchronizerTestReader(Semaphore* post,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    47
                                     SingleWriterSynchronizer* synchronizer,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    48
                                     volatile uintx* synchronized_value,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    49
                                     volatile int* continue_running) :
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    50
    JavaTestThread(post),
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    51
    _synchronizer(synchronizer),
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    52
    _synchronized_value(synchronized_value),
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    53
    _continue_running(continue_running)
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    54
  {}
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    55
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    56
  virtual void main_run() {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    57
    uintx iterations = 0;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    58
    while (OrderAccess::load_acquire(_continue_running) != 0) {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    59
      ++iterations;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    60
      SingleWriterSynchronizer::CriticalSection cs(_synchronizer);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    61
      uintx value = OrderAccess::load_acquire(_synchronized_value);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    62
      for (uint i = 0; i < reader_iterations; ++i) {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    63
        uintx new_value = OrderAccess::load_acquire(_synchronized_value);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    64
        // A reader can see either the value it first read after
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    65
        // entering the critical section, or that value + 1.  No other
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    66
        // values are possible.
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    67
        if (value != new_value) {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    68
          ASSERT_EQ((value + 1), new_value);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    69
        }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    70
      }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    71
    }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    72
    tty->print_cr("reader iterations: " UINTX_FORMAT, iterations);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    73
  }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    74
};
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    75
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    76
class SingleWriterSynchronizerTestWriter : public JavaTestThread {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    77
  SingleWriterSynchronizer* _synchronizer;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    78
  volatile uintx* _synchronized_value;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    79
  volatile int* _continue_running;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    80
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    81
public:
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    82
  SingleWriterSynchronizerTestWriter(Semaphore* post,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    83
                                     SingleWriterSynchronizer* synchronizer,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    84
                                     volatile uintx* synchronized_value,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    85
                                     volatile int* continue_running) :
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    86
    JavaTestThread(post),
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    87
    _synchronizer(synchronizer),
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    88
    _synchronized_value(synchronized_value),
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    89
    _continue_running(continue_running)
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    90
  {}
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    91
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    92
  virtual void main_run() {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    93
    while (OrderAccess::load_acquire(_continue_running) != 0) {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    94
      ++*_synchronized_value;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    95
      _synchronizer->synchronize();
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    96
    }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    97
    tty->print_cr("writer iterations: " UINTX_FORMAT, *_synchronized_value);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    98
  }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
    99
};
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   100
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   101
const uint nreaders = 5;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   102
const uint milliseconds_to_run = 3000;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   103
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   104
TEST_VM(TestSingleWriterSynchronizer, stress) {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   105
  Semaphore post;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   106
  SingleWriterSynchronizer synchronizer;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   107
  volatile uintx synchronized_value = 0;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   108
  volatile int continue_running = 1;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   109
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   110
  JavaTestThread* readers[nreaders] = {};
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   111
  for (uint i = 0; i < nreaders; ++i) {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   112
    readers[i] = new SingleWriterSynchronizerTestReader(&post,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   113
                                                        &synchronizer,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   114
                                                        &synchronized_value,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   115
                                                        &continue_running);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   116
    readers[i]->doit();
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   117
  }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   118
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   119
  JavaTestThread* writer =
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   120
    new SingleWriterSynchronizerTestWriter(&post,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   121
                                           &synchronizer,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   122
                                           &synchronized_value,
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   123
                                           &continue_running);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   124
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   125
  writer->doit();
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   126
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   127
  tty->print_cr("Stressing synchronizer for %u ms", milliseconds_to_run);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   128
  {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   129
    ThreadInVMfromNative invm(JavaThread::current());
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   130
    os::sleep(Thread::current(), milliseconds_to_run, true);
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   131
  }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   132
  continue_running = 0;
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   133
  for (uint i = 0; i < nreaders + 1; ++i) {
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   134
    post.wait();
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   135
  }
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents:
diff changeset
   136
}