hotspot/src/share/vm/gc/g1/workerDataArray.inline.hpp
changeset 33623 8b6afaf25abd
child 35061 be6025ebffea
equal deleted inserted replaced
33622:45baf494b37d 33623:8b6afaf25abd
       
     1 /*
       
     2  * Copyright (c) 2015, 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 "gc/g1/workerDataArray.hpp"
       
    26 #include "memory/allocation.inline.hpp"
       
    27 
       
    28 template <typename T>
       
    29 WorkerDataArray<T>::WorkerDataArray(uint length,
       
    30                                     const char* title,
       
    31                                     bool print_sum,
       
    32                                     int log_level,
       
    33                                     uint indent_level) :
       
    34  _title(title),
       
    35  _length(0),
       
    36  _print_sum(print_sum),
       
    37  _log_level(log_level),
       
    38  _indent_level(indent_level),
       
    39  _thread_work_items(NULL),
       
    40  _enabled(true) {
       
    41   assert(length > 0, "Must have some workers to store data for");
       
    42   _length = length;
       
    43   _data = NEW_C_HEAP_ARRAY(T, _length, mtGC);
       
    44   reset();
       
    45 }
       
    46 
       
    47 template <typename T>
       
    48 void WorkerDataArray<T>::set(uint worker_i, T value) {
       
    49   assert(worker_i < _length, "Worker %d is greater than max: %d", worker_i, _length);
       
    50   assert(_data[worker_i] == uninitialized(), "Overwriting data for worker %d in %s", worker_i, _title);
       
    51   _data[worker_i] = value;
       
    52 }
       
    53 
       
    54 template <typename T>
       
    55 T WorkerDataArray<T>::get(uint worker_i) const {
       
    56   assert(worker_i < _length, "Worker %d is greater than max: %d", worker_i, _length);
       
    57   assert(_data[worker_i] != uninitialized(), "No data added for worker %d", worker_i);
       
    58   return _data[worker_i];
       
    59 }
       
    60 
       
    61 template <typename T>
       
    62 WorkerDataArray<T>::~WorkerDataArray() {
       
    63   FREE_C_HEAP_ARRAY(T, _data);
       
    64 }
       
    65 
       
    66 template <typename T>
       
    67 void WorkerDataArray<T>::link_thread_work_items(WorkerDataArray<size_t>* thread_work_items) {
       
    68   _thread_work_items = thread_work_items;
       
    69 }
       
    70 
       
    71 template <typename T>
       
    72 void WorkerDataArray<T>::set_thread_work_item(uint worker_i, size_t value) {
       
    73   assert(_thread_work_items != NULL, "No sub count");
       
    74   _thread_work_items->set(worker_i, value);
       
    75 }
       
    76 
       
    77 template <typename T>
       
    78 void WorkerDataArray<T>::add(uint worker_i, T value) {
       
    79   assert(worker_i < _length, "Worker %d is greater than max: %d", worker_i, _length);
       
    80   assert(_data[worker_i] != uninitialized(), "No data to add to for worker %d", worker_i);
       
    81   _data[worker_i] += value;
       
    82 }
       
    83 
       
    84 template <typename T>
       
    85 double WorkerDataArray<T>::average(uint active_threads) const {
       
    86   return sum(active_threads) / (double) active_threads;
       
    87 }
       
    88 
       
    89 template <typename T>
       
    90 T WorkerDataArray<T>::sum(uint active_threads) const {
       
    91   T s = get(0);
       
    92   for (uint i = 1; i < active_threads; ++i) {
       
    93     s += get(i);
       
    94   }
       
    95   return s;
       
    96 }
       
    97 
       
    98 template <typename T>
       
    99 T WorkerDataArray<T>::minimum(uint active_threads) const {
       
   100   T min = get(0);
       
   101   for (uint i = 1; i < active_threads; ++i) {
       
   102     min = MIN2(min, get(i));
       
   103   }
       
   104   return min;
       
   105 }
       
   106 
       
   107 template <typename T>
       
   108 T WorkerDataArray<T>::maximum(uint active_threads) const {
       
   109   T max = get(0);
       
   110   for (uint i = 1; i < active_threads; ++i) {
       
   111     max = MAX2(max, get(i));
       
   112   }
       
   113   return max;
       
   114 }
       
   115 
       
   116 template <typename T>
       
   117 T WorkerDataArray<T>::diff(uint active_threads) const {
       
   118   return maximum(active_threads) - minimum(active_threads);
       
   119 }
       
   120 
       
   121 template <typename T>
       
   122 void WorkerDataArray<T>::clear() {
       
   123   set_all(0);
       
   124 }
       
   125 
       
   126 template <typename T>
       
   127 void WorkerDataArray<T>::set_all(T value) {
       
   128   for (uint i = 0; i < _length; i++) {
       
   129     _data[i] = value;
       
   130   }
       
   131 }
       
   132 
       
   133 #ifndef PRODUCT
       
   134 template <typename T>
       
   135 void WorkerDataArray<T>::reset() {
       
   136   set_all(uninitialized());
       
   137   if (_thread_work_items != NULL) {
       
   138     _thread_work_items->reset();
       
   139   }
       
   140 }
       
   141 
       
   142 template <typename T>
       
   143 void WorkerDataArray<T>::verify(uint active_threads) const {
       
   144   if (!_enabled) {
       
   145     return;
       
   146   }
       
   147 
       
   148   assert(active_threads <= _length, "Wrong number of active threads");
       
   149   for (uint i = 0; i < active_threads; i++) {
       
   150     assert(_data[i] != uninitialized(),
       
   151            "Invalid data for worker %u in '%s'", i, _title);
       
   152   }
       
   153   if (_thread_work_items != NULL) {
       
   154     _thread_work_items->verify(active_threads);
       
   155   }
       
   156 }
       
   157 
       
   158 template <>
       
   159 inline size_t WorkerDataArray<size_t>::uninitialized() const {
       
   160   return (size_t)-1;
       
   161 }
       
   162 
       
   163 template <>
       
   164 inline double WorkerDataArray<double>::uninitialized() const {
       
   165   return -1.0;
       
   166 }
       
   167 #endif