src/hotspot/share/utilities/spinYield.cpp
author egahlin
Tue, 15 May 2018 20:24:34 +0200
changeset 50113 caf115bb98ad
parent 49718 713d9b03e990
child 53077 33b8f6f4cdf5
permissions -rw-r--r--
8199712: Flight Recorder Reviewed-by: coleenp, ihse, erikj, dsamersoff, mseledtsov, egahlin, mgronlun Contributed-by: erik.gahlin@oracle.com, markus.gronlund@oracle.com
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
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    30
SpinYield::SpinYield(uint spin_limit, uint yield_limit) :
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    31
  _sleep_time(),
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    32
  _spins(0),
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    33
  _yields(0),
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    34
  _spin_limit(os::is_MP() ? spin_limit : 0),
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    35
  _yield_limit(yield_limit)
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    36
{}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    37
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    38
void SpinYield::yield_or_sleep() {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    39
  if (_yields < _yield_limit) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    40
    ++_yields;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    41
    os::naked_yield();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    42
  } else {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    43
    Ticks sleep_start = Ticks::now();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    44
    os::naked_short_sleep(1);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents: 49718
diff changeset
    45
    _sleep_time += Ticks::now() - sleep_start;
49718
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
}
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    48
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    49
static const char* print_separator(outputStream* s, const char* separator) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    50
  s->print("%s", separator);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    51
  return ", ";
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
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    54
void SpinYield::report(outputStream* s) const {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    55
  const char* initial_separator = "";
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    56
  const char* separator = initial_separator;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    57
  if (_spins > 0) {             // Report spins, if any.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    58
    separator = print_separator(s, separator);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    59
    s->print("spins = %u", _spins);
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
  if (_yields > 0) {            // Report yields, if any.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    62
    separator = print_separator(s, separator);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    63
    s->print("yields = %u", _yields);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    64
  }
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    65
  if (_sleep_time.value() != 0) { // Report sleep duration, if slept.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    66
    separator = print_separator(s, separator);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents: 49718
diff changeset
    67
    s->print("sleep = " UINT64_FORMAT " usecs",
caf115bb98ad 8199712: Flight Recorder
egahlin
parents: 49718
diff changeset
    68
             _sleep_time.milliseconds());
49718
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    69
  }
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    70
  if (separator == initial_separator) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    71
    s->print("no waiting");
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    72
  }
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    73
}