src/hotspot/share/jfr/recorder/storage/jfrStorageControl.cpp
changeset 50113 caf115bb98ad
child 50234 6ba3e32a9882
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 
       
    25 #include "precompiled.hpp"
       
    26 #include "jfr/recorder/storage/jfrStorageControl.hpp"
       
    27 #include "runtime/atomic.hpp"
       
    28 #include "runtime/orderAccess.inline.hpp"
       
    29 
       
    30 // returns the updated value
       
    31 static jlong atomic_add(size_t value, size_t volatile* const dest) {
       
    32   size_t compare_value;
       
    33   size_t exchange_value;
       
    34   do {
       
    35     compare_value = OrderAccess::load_acquire(dest);
       
    36     exchange_value = compare_value + value;
       
    37   } while (Atomic::cmpxchg(exchange_value, dest, compare_value) != compare_value);
       
    38   return exchange_value;
       
    39 }
       
    40 
       
    41 static jlong atomic_dec(size_t volatile* const dest) {
       
    42   size_t compare_value;
       
    43   size_t exchange_value;
       
    44   do {
       
    45     compare_value = OrderAccess::load_acquire(dest);
       
    46     assert(compare_value >= 1, "invariant");
       
    47     exchange_value = compare_value - 1;
       
    48   } while (Atomic::cmpxchg(exchange_value, dest, compare_value) != compare_value);
       
    49   return exchange_value;
       
    50 }
       
    51 
       
    52 const size_t max_lease_factor = 2;
       
    53 JfrStorageControl::JfrStorageControl(size_t global_count_total, size_t in_memory_discard_threshold) :
       
    54   _global_count_total(global_count_total),
       
    55   _full_count(0),
       
    56   _global_lease_count(0),
       
    57   _dead_count(0),
       
    58   _to_disk_threshold(0),
       
    59   _in_memory_discard_threshold(in_memory_discard_threshold),
       
    60   _global_lease_threshold(global_count_total / max_lease_factor),
       
    61   _scavenge_threshold(0),
       
    62   _to_disk(false) {}
       
    63 
       
    64 bool JfrStorageControl::to_disk() const {
       
    65   return _to_disk;
       
    66 }
       
    67 
       
    68 void JfrStorageControl::set_to_disk(bool enable) {
       
    69   _to_disk = enable;
       
    70 }
       
    71 
       
    72 // concurrent with lax requirement
       
    73 
       
    74 size_t JfrStorageControl::full_count() const {
       
    75   return _full_count;
       
    76 }
       
    77 
       
    78 size_t JfrStorageControl::increment_full() {
       
    79   return atomic_add(1, &_full_count);
       
    80 }
       
    81 
       
    82 size_t JfrStorageControl::decrement_full() {
       
    83   return atomic_dec(&_full_count);
       
    84 }
       
    85 
       
    86 void JfrStorageControl::reset_full() {
       
    87   Atomic::store((size_t)0, &_full_count);
       
    88 }
       
    89 
       
    90 bool JfrStorageControl::should_post_buffer_full_message() const {
       
    91   return to_disk() && (full_count() > _to_disk_threshold);
       
    92 }
       
    93 
       
    94 bool JfrStorageControl::should_discard() const {
       
    95   return !to_disk() && full_count() >= _in_memory_discard_threshold;
       
    96 }
       
    97 
       
    98 // concurrent with accuracy requirement
       
    99 
       
   100 size_t JfrStorageControl::global_lease_count() const {
       
   101   return OrderAccess::load_acquire(&_global_lease_count);
       
   102 }
       
   103 
       
   104 size_t JfrStorageControl::increment_leased() {
       
   105   return atomic_add(1, &_global_lease_count);
       
   106 }
       
   107 
       
   108 size_t JfrStorageControl::decrement_leased() {
       
   109   return atomic_dec(&_global_lease_count);
       
   110 }
       
   111 
       
   112 bool JfrStorageControl::is_global_lease_allowed() const {
       
   113   return global_lease_count() <= _global_lease_threshold;
       
   114 }
       
   115 
       
   116 // concurrent with lax requirement
       
   117 
       
   118 size_t JfrStorageControl::dead_count() const {
       
   119   return _dead_count;
       
   120 }
       
   121 
       
   122 size_t JfrStorageControl::increment_dead() {
       
   123   return atomic_add(1, &_dead_count);
       
   124 }
       
   125 
       
   126 size_t JfrStorageControl::decrement_dead() {
       
   127   return atomic_dec(&_dead_count);
       
   128 }
       
   129 
       
   130 bool JfrStorageControl::should_scavenge() const {
       
   131   return dead_count() >= _scavenge_threshold;
       
   132 }
       
   133 
       
   134 void JfrStorageControl::set_scavenge_threshold(size_t number_of_dead_buffers) {
       
   135   _scavenge_threshold = number_of_dead_buffers;
       
   136 }
       
   137