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