Path: blob/master/test/hotspot/gtest/utilities/test_spinYield.cpp
41144 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "runtime/os.hpp"26#include "utilities/ostream.hpp"27#include "utilities/spinYield.hpp"28#include "unittest.hpp"2930// Some basic tests of SpinYield, using comparison of report output with31// expected results to verify state. This is all very hard-wired to the32// current implementation of SpinYield, esp. the report function.3334static void check_report(const SpinYield* spinner, const char* expected) {35char buffer[100];36stringStream s(buffer, sizeof(buffer));37spinner->report(&s);38ASSERT_STREQ(expected, buffer);39}4041TEST(SpinYield, no_waiting) {42SpinYield spinner;43check_report(&spinner, "no waiting");44}4546TEST(SpinYield, one_wait) {47SpinYield spinner(100);48spinner.wait();49check_report(&spinner, os::is_MP() ? "spins = 1" : "yields = 1");50}5152TEST(SpinYield, ten_waits) {53SpinYield spinner(100, 100);54for (unsigned i = 0; i < 10; ++i) {55spinner.wait();56}57check_report(&spinner, os::is_MP() ? "spins = 10" : "yields = 10");58}5960TEST(SpinYield, two_yields) {61SpinYield spinner(0, 10);62spinner.wait();63spinner.wait();64check_report(&spinner, "yields = 2");65}6667TEST_VM(SpinYield, one_sleep) {68SpinYield spinner(0, 0);69spinner.wait();7071char buffer[100];72stringStream s(buffer, sizeof(buffer));73spinner.report(&s);7475const char* expected = "sleep = ";76ASSERT_TRUE(strncmp(expected, buffer, strlen(expected)) == 0);77}7879TEST_VM(SpinYield, one_spin_one_sleep) {80SpinYield spinner(1, 0);81spinner.wait();82spinner.wait();8384char buffer[100];85stringStream s(buffer, sizeof(buffer));86spinner.report(&s);8788const char* expected_MP = "spins = 1, sleep = ";89const char* expected_UP = "sleep = ";90const char* expected = os::is_MP() ? expected_MP : expected_UP;91ASSERT_TRUE(strncmp(expected, buffer, strlen(expected)) == 0);92}939495