/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "gc/g1/workerDataArray.inline.hpp" #include "utilities/ostream.hpp" template <> size_t WorkerDataArray::uninitialized() { return (size_t)-1; } template <> double WorkerDataArray::uninitialized() { return -1.0; } template <> void WorkerDataArray::WDAPrinter::summary(outputStream* out, double min, double avg, double max, double diff, double sum, bool print_sum) { out->print(" Min: %4.1lf, Avg: %4.1lf, Max: %4.1lf, Diff: %4.1lf", min * MILLIUNITS, avg * MILLIUNITS, max * MILLIUNITS, diff* MILLIUNITS); if (print_sum) { out->print(", Sum: %4.1lf", sum * MILLIUNITS); } } template <> void WorkerDataArray::WDAPrinter::summary(outputStream* out, size_t min, double avg, size_t max, size_t diff, size_t sum, bool print_sum) { out->print(" Min: " SIZE_FORMAT ", Avg: %4.1lf, Max: " SIZE_FORMAT ", Diff: " SIZE_FORMAT, min, avg, max, diff); if (print_sum) { out->print(", Sum: " SIZE_FORMAT, sum); } } template <> void WorkerDataArray::WDAPrinter::details(const WorkerDataArray* phase, outputStream* out) { out->print("%-25s", ""); for (uint i = 0; i < phase->_length; ++i) { double value = phase->get(i); if (value != phase->uninitialized()) { out->print(" %4.1lf", phase->get(i) * 1000.0); } else { out->print(" -"); } } out->cr(); } template <> void WorkerDataArray::WDAPrinter::details(const WorkerDataArray* phase, outputStream* out) { out->print("%-25s", ""); for (uint i = 0; i < phase->_length; ++i) { size_t value = phase->get(i); if (value != phase->uninitialized()) { out->print(" " SIZE_FORMAT, phase->get(i)); } else { out->print(" -"); } } out->cr(); } #ifndef PRODUCT #include "memory/resourceArea.hpp" void WorkerDataArray_test_basic() { const uint length = 3; const char* title = "Test array"; WorkerDataArray array(length, title); assert(strncmp(array.title(), title, strlen(title)) == 0 , "Expected titles to match"); const size_t expected[length] = {5, 3, 7}; for (uint i = 0; i < length; i++) { array.set(i, expected[i]); } for (uint i = 0; i < length; i++) { assert(array.get(i) == expected[i], "Expected elements to match"); } assert(array.sum() == (5 + 3 + 7), "Expected sums to match"); assert(array.average() == 5.0, "Expected averages to match"); for (uint i = 0; i < length; i++) { array.add(i, 1); } for (uint i = 0; i < length; i++) { assert(array.get(i) == expected[i] + 1, "Expected add to increment values"); } } void WorkerDataArray_test_with_uninitialized() { const uint length = 3; const size_t uninitilized = WorkerDataArray::uninitialized(); WorkerDataArray array(length, "Test array"); const size_t expected[length] = {5, uninitilized, 7}; for (uint i = 0; i < length; i++) { array.set(i, expected[i]); } for (uint i = 0; i < length; i++) { assert(array.get(i) == expected[i], "Expected elements to match"); } assert(array.sum() == (5 + 7), "Expected sums to match"); assert(array.average() == 6.0, "Expected averages to match"); } void WorkerDataArray_test_uninitialized() { const uint length = 3; const size_t uninitilized = WorkerDataArray::uninitialized(); WorkerDataArray array(length, "Test array"); for (uint i = 0; i < length; i++) { array.set(i, uninitilized); } assert(array.sum() == 0, "Sum should be 0 when no slots have been used."); assert(array.average() == 0.0, "Average should be 0.0 when no slots have been used."); } void WorkerDataArray_test_print_summary() { const uint length = 4; const size_t uninitilized = WorkerDataArray::uninitialized(); WorkerDataArray array(length, "Test array"); const size_t expected[length] = {5, uninitilized, 7, uninitilized}; for (uint i = 0; i < length; i++) { array.set(i, expected[i]); } ResourceMark rm; stringStream out; array.print_summary_on(&out); const char* out_string = out.as_string(); const char* expected_string = "Test array Min: 5, Avg: 6,0, Max: 7, Diff: 2, Sum: 12, Workers: 2\n"; const size_t expected_len = strlen(expected_string); assert(expected_len == strlen(out_string), "Wrong string length, expected " SIZE_FORMAT " but got " SIZE_FORMAT, expected_len, strlen(out_string)); assert(strncmp(expected_string, out_string, expected_len) == 0, "Expected '%s' but got: '%s'", expected_string, out_string); } void WorkerDataArray_test_print_summary_skipped() { const uint length = 2; const size_t uninitilized = WorkerDataArray::uninitialized(); WorkerDataArray array(length, "Test array"); const size_t expected[length] = {uninitilized, uninitilized}; for (uint i = 0; i < length; i++) { array.set(i, expected[i]); } ResourceMark rm; stringStream out; array.print_summary_on(&out); const char* out_string = out.as_string(); const char* expected_string = "Test array skipped\n"; const size_t expected_len = strlen(expected_string); assert(expected_len == strlen(out_string), "Wrong string length, expected " SIZE_FORMAT " but got " SIZE_FORMAT, expected_len, strlen(out_string)); assert(strncmp(expected_string, out_string, expected_len) == 0, "Expected '%s' but got: '%s'", expected_string, out_string); } void WorkerDataArray_test_print_details() { const uint length = 4; const size_t uninitilized = WorkerDataArray::uninitialized(); WorkerDataArray array(length, "Test array"); const size_t expected[length] = {5, uninitilized, 7, uninitilized}; for (uint i = 0; i < length; i++) { array.set(i, expected[i]); } ResourceMark rm; stringStream out; array.print_details_on(&out); const char* out_string = out.as_string(); const char* expected_string = " 5 - 7 -\n"; const size_t expected_len = strlen(expected_string); assert(expected_len == strlen(out_string), "Wrong string length, expected " SIZE_FORMAT " but got " SIZE_FORMAT, expected_len, strlen(out_string)); assert(strncmp(expected_string, out_string, expected_len) == 0, "Expected '%s' but got: '%s'", expected_string, out_string); } void WorkerDataArray_test() { WorkerDataArray_test_basic(); WorkerDataArray_test_with_uninitialized(); WorkerDataArray_test_uninitialized(); WorkerDataArray_test_print_summary(); WorkerDataArray_test_print_summary_skipped(); WorkerDataArray_test_print_details(); } #endif