1 /*
   2  * Copyright (c) 2015, 2019, 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 #ifndef SHARE_GC_SHARED_WORKERDATAARRAY_INLINE_HPP
  26 #define SHARE_GC_SHARED_WORKERDATAARRAY_INLINE_HPP
  27 
  28 #include "gc/shared/workerDataArray.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "utilities/ostream.hpp"
  31 
  32 template <typename T>
  33 WorkerDataArray<T>::WorkerDataArray(uint length, const char* title, bool is_serial) :
  34  _data(NULL),
  35  _length(length),
  36  _title(title),
  37  _is_serial(is_serial) {
  38   assert(length > 0, "Must have some workers to store data for");
  39   assert(!is_serial || length == 1, "Serial phase must only have a single entry.");
  40   _data = NEW_C_HEAP_ARRAY(T, _length, mtGC);
  41   for (uint i = 0; i < MaxThreadWorkItems; i++) {
  42     _thread_work_items[i] = NULL;
  43   }
  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   return _data[worker_i];
  58 }
  59 
  60 template <typename T>
  61 WorkerDataArray<T>::~WorkerDataArray() {
  62   FREE_C_HEAP_ARRAY(T, _data);
  63 }
  64 
  65 template <typename T>
  66 void WorkerDataArray<T>::link_thread_work_items(WorkerDataArray<size_t>* thread_work_items, uint index) {
  67   assert(index < MaxThreadWorkItems, "Tried to access thread work item %u (max %u)", index, MaxThreadWorkItems);
  68   _thread_work_items[index] = thread_work_items;
  69 }
  70 
  71 template <typename T>
  72 void WorkerDataArray<T>::set_thread_work_item(uint worker_i, size_t value, uint index) {
  73   assert(index < MaxThreadWorkItems, "Tried to access thread work item %u (max %u)", index, MaxThreadWorkItems);
  74   assert(_thread_work_items[index] != NULL, "No sub count");
  75   _thread_work_items[index]->set(worker_i, value);
  76 }
  77 
  78 template <typename T>
  79 void WorkerDataArray<T>::add_thread_work_item(uint worker_i, size_t value, uint index) {
  80   assert(index < MaxThreadWorkItems, "Tried to access thread work item %u (max %u)", index, MaxThreadWorkItems);
  81   assert(_thread_work_items[index] != NULL, "No sub count");
  82   _thread_work_items[index]->add(worker_i, value);
  83 }
  84 
  85 template <typename T>
  86 void WorkerDataArray<T>::set_or_add_thread_work_item(uint worker_i, size_t value, uint index) {
  87   assert(index < MaxThreadWorkItems, "Tried to access thread work item %u (max %u)", index, MaxThreadWorkItems);
  88   assert(_thread_work_items[index] != NULL, "No sub count");
  89   if (_thread_work_items[index]->get(worker_i) == _thread_work_items[index]->uninitialized()) {
  90     _thread_work_items[index]->set(worker_i, value);
  91   } else {
  92     _thread_work_items[index]->add(worker_i, value);
  93   }
  94 }
  95 
  96 template <typename T>
  97 size_t WorkerDataArray<T>::get_thread_work_item(uint worker_i, uint index) {
  98   assert(index < MaxThreadWorkItems, "Tried to access thread work item %u (max %u)", index, MaxThreadWorkItems);
  99   assert(_thread_work_items[index] != NULL, "No sub count");
 100   return _thread_work_items[index]->get(worker_i);
 101 }
 102 
 103 template <typename T>
 104 void WorkerDataArray<T>::add(uint worker_i, T value) {
 105   assert(worker_i < _length, "Worker %d is greater than max: %d", worker_i, _length);
 106   assert(_data[worker_i] != uninitialized(), "No data to add to %s for worker %d", _title, worker_i);
 107   _data[worker_i] += value;
 108 }
 109 
 110 template <typename T>
 111 double WorkerDataArray<T>::average() const {
 112   uint contributing_threads = 0;
 113   for (uint i = 0; i < _length; ++i) {
 114     if (get(i) != uninitialized()) {
 115       contributing_threads++;
 116     }
 117   }
 118   if (contributing_threads == 0) {
 119     return 0.0;
 120   }
 121   return sum() / (double) contributing_threads;
 122 }
 123 
 124 template <typename T>
 125 T WorkerDataArray<T>::sum() const {
 126   T s = 0;
 127   for (uint i = 0; i < _length; ++i) {
 128     if (get(i) != uninitialized()) {
 129       s += get(i);
 130     }
 131   }
 132   return s;
 133 }
 134 
 135 template <typename T>
 136 void WorkerDataArray<T>::set_all(T value) {
 137   for (uint i = 0; i < _length; i++) {
 138     _data[i] = value;
 139   }
 140 }
 141 
 142 template <class T>
 143 void WorkerDataArray<T>::print_summary_on(outputStream* out, bool print_sum) const {
 144   if (_is_serial) {
 145     out->print("%s:", title());
 146   } else {
 147     out->print("%-25s", title());
 148   }
 149 
 150   uint start = 0;
 151   while (start < _length && get(start) == uninitialized()) {
 152     start++;
 153   }
 154   if (start < _length) {
 155     if (_is_serial) {
 156       WDAPrinter::summary(out, get(0));
 157     } else {
 158       T min = get(start);
 159       T max = min;
 160       T sum = 0;
 161       uint contributing_threads = 0;
 162       for (uint i = start; i < _length; ++i) {
 163         T value = get(i);
 164         if (value != uninitialized()) {
 165           max = MAX2(max, value);
 166           min = MIN2(min, value);
 167           sum += value;
 168           contributing_threads++;
 169         }
 170       }
 171       T diff = max - min;
 172       assert(contributing_threads != 0, "Must be since we found a used value for the start index");
 173       double avg = sum / (double) contributing_threads;
 174       WDAPrinter::summary(out, min, avg, max, diff, sum, print_sum);
 175       out->print_cr(", Workers: %d", contributing_threads);
 176     }
 177   } else {
 178     // No data for this phase.
 179     out->print_cr(" skipped");
 180   }
 181 }
 182 
 183 template <class T>
 184 void WorkerDataArray<T>::print_details_on(outputStream* out) const {
 185   WDAPrinter::details(this, out);
 186 }
 187 
 188 template <typename T>
 189 void WorkerDataArray<T>::reset() {
 190   set_all(uninitialized());
 191   for (uint i = 0; i < MaxThreadWorkItems; i++) {
 192     if (_thread_work_items[i] != NULL) {
 193       _thread_work_items[i]->reset();
 194     }
 195   }
 196 }
 197 
 198 #endif // SHARE_GC_SHARED_WORKERDATAARRAY_INLINE_HPP