1 /*
  2  * Copyright (c) 2019, 2020, 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 "gc/g1/g1NUMAStats.hpp"
 27 #include "logging/logStream.hpp"
 28 
 29 double G1NUMAStats::Stat::rate() const {
 30   return _requested == 0 ? 0 : (double)_hit / _requested * 100;
 31 }
 32 
 33 G1NUMAStats::NodeDataArray::NodeDataArray(uint num_nodes) {
 34   // Not using > 1, for -XX:+ForceNUMA support.
 35   guarantee(num_nodes > 0, "Number of nodes (%u) should be set", num_nodes);
 36 
 37   // The row represents the number of nodes.
 38   _num_column = num_nodes;
 39   // +1 for G1MemoryNodeManager::AnyNodeIndex.
 40   _num_row = num_nodes + 1;
 41 
 42   _data = NEW_C_HEAP_ARRAY(size_t*, _num_row, mtGC);
 43   for (uint row = 0; row < _num_row; row++) {
 44     _data[row] = NEW_C_HEAP_ARRAY(size_t, _num_column, mtGC);
 45   }
 46 
 47   clear();
 48 }
 49 
 50 G1NUMAStats::NodeDataArray::~NodeDataArray() {
 51   for (uint row = 0; row < _num_row; row++) {
 52     FREE_C_HEAP_ARRAY(size_t, _data[row]);
 53   }
 54   FREE_C_HEAP_ARRAY(size_t*, _data);
 55 }
 56 
 57 void G1NUMAStats::NodeDataArray::create_hit_rate(Stat* result) const {
 58   size_t requested = 0;
 59   size_t hit = 0;
 60 
 61   for (size_t row = 0; row < _num_row; row++) {
 62     for (size_t column = 0; column < _num_column; column++) {
 63       requested += _data[row][column];
 64       if (row == column) {
 65         hit += _data[row][column];
 66       }
 67     }
 68   }
 69 
 70   assert(result != NULL, "Invariant");
 71   result->_hit = hit;
 72   result->_requested = requested;
 73 }
 74 
 75 void G1NUMAStats::NodeDataArray::create_hit_rate(Stat* result, uint req_index) const {
 76   size_t requested = 0;
 77   size_t hit = _data[req_index][req_index];
 78 
 79   for (size_t column = 0; column < _num_column; column++) {
 80     requested += _data[req_index][column];
 81   }
 82 
 83   assert(result != NULL, "Invariant");
 84   result->_hit = hit;
 85   result->_requested = requested;
 86 }
 87 
 88 size_t G1NUMAStats::NodeDataArray::sum(uint req_index) const {
 89   size_t sum = 0;
 90   for (size_t column = 0; column < _num_column; column++) {
 91     sum += _data[req_index][column];
 92   }
 93 
 94   return sum;
 95 }
 96 
 97 void G1NUMAStats::NodeDataArray::increase(uint req_index, uint alloc_index) {
 98   assert(req_index < _num_row,
 99          "Requested index %u should be less than the row size %u",
100          req_index, _num_row);
101   assert(alloc_index < _num_column,
102          "Allocated index %u should be less than the column size %u",
103          alloc_index, _num_column);
104   _data[req_index][alloc_index] += 1;
105 }
106 
107 void G1NUMAStats::NodeDataArray::clear() {
108   for (uint row = 0; row < _num_row; row++) {
109     memset((void*)_data[row], 0, sizeof(size_t) * _num_column);
110   }
111 }
112 
113 size_t G1NUMAStats::NodeDataArray::get(uint req_index, uint alloc_index) {
114   return _data[req_index][alloc_index];
115 }
116 
117 void G1NUMAStats::NodeDataArray::copy(uint req_index, size_t* stat) {
118   assert(stat != NULL, "Invariant");
119 
120   for (uint column = 0; column < _num_column; column++) {
121     _data[req_index][column] += stat[column];
122   }
123 }
124 
125 G1NUMAStats::G1NUMAStats(const int* node_ids, uint num_node_ids) :
126   _node_ids(node_ids), _num_node_ids(num_node_ids), _node_data() {
127 
128   // Not using > 1, for -XX:+ForceNUMA support.
129   assert(_num_node_ids > 0, "Should have at least one node id: %u", _num_node_ids);
130 
131   for (int i = 0; i < NodeDataItemsSentinel; i++) {
132     _node_data[i] = new NodeDataArray(_num_node_ids);
133   }
134 }
135 
136 G1NUMAStats::~G1NUMAStats() {
137   for (int i = 0; i < NodeDataItemsSentinel; i++) {
138     delete _node_data[i];
139   }
140 }
141 
142 void G1NUMAStats::clear(G1NUMAStats::NodeDataItems phase) {
143   _node_data[phase]->clear();
144 }
145 
146 void G1NUMAStats::update(G1NUMAStats::NodeDataItems phase,
147                          uint requested_node_index,
148                          uint allocated_node_index) {
149   _node_data[phase]->increase(requested_node_index, allocated_node_index);
150 }
151 
152 void G1NUMAStats::copy(G1NUMAStats::NodeDataItems phase,
153                        uint requested_node_index,
154                        size_t* allocated_stat) {
155   _node_data[phase]->copy(requested_node_index, allocated_stat);
156 }
157 
158 static const char* phase_to_explanatory_string(G1NUMAStats::NodeDataItems phase) {
159   switch(phase) {
160     case G1NUMAStats::NewRegionAlloc:
161       return "Placement match ratio";
162     case G1NUMAStats::LocalObjProcessAtCopyToSurv:
163       return "Worker task locality match ratio";
164     default:
165       return "";
166   }
167 }
168 
169 #define RATE_TOTAL_FORMAT "%0.0f%% " SIZE_FORMAT "/" SIZE_FORMAT
170 
171 void G1NUMAStats::print_info(G1NUMAStats::NodeDataItems phase) {
172   LogTarget(Info, gc, heap, numa) lt;
173 
174   if (lt.is_enabled()) {
175     LogStream ls(lt);
176     Stat result;
177     size_t array_width = _num_node_ids;
178 
179     _node_data[phase]->create_hit_rate(&result);
180 
181     ls.print("%s: " RATE_TOTAL_FORMAT " (",
182              phase_to_explanatory_string(phase), result.rate(), result._hit, result._requested);
183 
184     for (uint i = 0; i < array_width; i++) {
185       if (i != 0) {
186         ls.print(", ");
187       }
188       _node_data[phase]->create_hit_rate(&result, i);
189       ls.print("%d: " RATE_TOTAL_FORMAT,
190                _node_ids[i], result.rate(), result._hit, result._requested);
191     }
192     ls.print_cr(")");
193   }
194 }
195 
196 void G1NUMAStats::print_mutator_alloc_stat_debug() {
197   LogTarget(Debug, gc, heap, numa) lt;
198 
199   if (lt.is_enabled()) {
200     LogStream ls(lt);
201     uint array_width = _num_node_ids;
202 
203     ls.print("Allocated NUMA ids    ");
204     for (uint i = 0; i < array_width; i++) {
205       ls.print("%8d", _node_ids[i]);
206     }
207     ls.print_cr("   Total");
208 
209     ls.print("Requested NUMA id ");
210     for (uint req = 0; req < array_width; req++) {
211       ls.print("%3d ", _node_ids[req]);
212       for (uint alloc = 0; alloc < array_width; alloc++) {
213         ls.print(SIZE_FORMAT_W(8), _node_data[NewRegionAlloc]->get(req, alloc));
214       }
215       ls.print(SIZE_FORMAT_W(8), _node_data[NewRegionAlloc]->sum(req));
216       ls.print_cr("");
217       // Add padding to align with the string 'Requested NUMA id'.
218       ls.print("                  ");
219     }
220     ls.print("Any ");
221     for (uint alloc = 0; alloc < array_width; alloc++) {
222       ls.print(SIZE_FORMAT_W(8), _node_data[NewRegionAlloc]->get(array_width, alloc));
223     }
224     ls.print(SIZE_FORMAT_W(8), _node_data[NewRegionAlloc]->sum(array_width));
225     ls.print_cr("");
226   }
227 }
228 
229 void G1NUMAStats::print_statistics() {
230   print_info(NewRegionAlloc);
231   print_mutator_alloc_stat_debug();
232 
233   print_info(LocalObjProcessAtCopyToSurv);
234 }