test/hotspot/gtest/utilities/test_spinYield.cpp
author manc
Mon, 14 Oct 2019 18:48:10 -0700
changeset 58652 9b67dd88a931
parent 51277 dfe1cff5c2f6
permissions -rw-r--r--
8232232: G1RemSetSummary::_rs_threads_vtimes is not initialized to zero Summary: Fix error in "Concurrent refinement threads times" in GC log and cleanup. Reviewed-by: tschatzl, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49718
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     1
/*
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     4
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     8
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    14
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    18
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    21
 * questions.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    22
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    23
 */
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    24
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    25
#include "precompiled.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    26
#include "runtime/os.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    27
#include "utilities/ostream.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    28
#include "utilities/spinYield.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    29
#include "unittest.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    30
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    31
// Some basic tests of SpinYield, using comparison of report output with
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    32
// expected results to verify state.  This is all very hard-wired to the
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    33
// current implementation of SpinYield, esp. the report function.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    34
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    35
static void check_report(const SpinYield* spinner, const char* expected) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    36
  char buffer[100];
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    37
  stringStream s(buffer, sizeof(buffer));
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    38
  spinner->report(&s);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    39
  ASSERT_STREQ(expected, buffer);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    40
}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    41
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    42
TEST(SpinYield, no_waiting) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    43
  SpinYield spinner;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    44
  check_report(&spinner, "no waiting");
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    45
}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    46
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    47
TEST(SpinYield, one_wait) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    48
  SpinYield spinner(100);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    49
  spinner.wait();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    50
  check_report(&spinner, os::is_MP() ? "spins = 1" : "yields = 1");
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    51
}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    52
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    53
TEST(SpinYield, ten_waits) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    54
  SpinYield spinner(100, 100);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    55
  for (unsigned i = 0; i < 10; ++i) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    56
    spinner.wait();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    57
  }
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    58
  check_report(&spinner, os::is_MP() ? "spins = 10" : "yields = 10");
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    59
}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    60
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    61
TEST(SpinYield, two_yields) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    62
  SpinYield spinner(0, 10);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    63
  spinner.wait();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    64
  spinner.wait();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    65
  check_report(&spinner, "yields = 2");
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    66
}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    67
51277
dfe1cff5c2f6 8174691: [TESTBUG] A number of native hotspot unit tests fail when executed in stand-alone mode
iignatyev
parents: 49718
diff changeset
    68
TEST_VM(SpinYield, one_sleep) {
49718
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    69
  SpinYield spinner(0, 0);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    70
  spinner.wait();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    71
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    72
  char buffer[100];
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    73
  stringStream s(buffer, sizeof(buffer));
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    74
  spinner.report(&s);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    75
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    76
  const char* expected = "sleep = ";
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    77
  ASSERT_TRUE(strncmp(expected, buffer, strlen(expected)) == 0);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    78
}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    79
51277
dfe1cff5c2f6 8174691: [TESTBUG] A number of native hotspot unit tests fail when executed in stand-alone mode
iignatyev
parents: 49718
diff changeset
    80
TEST_VM(SpinYield, one_spin_one_sleep) {
49718
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    81
  SpinYield spinner(1, 0);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    82
  spinner.wait();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    83
  spinner.wait();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    84
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    85
  char buffer[100];
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    86
  stringStream s(buffer, sizeof(buffer));
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    87
  spinner.report(&s);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    88
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    89
  const char* expected_MP = "spins = 1, sleep = ";
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    90
  const char* expected_UP = "sleep = ";
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    91
  const char* expected = os::is_MP() ? expected_MP : expected_UP;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    92
  ASSERT_TRUE(strncmp(expected, buffer, strlen(expected)) == 0);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    93
}