1 /*
   2  * Copyright (c) 2015, 2016, 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/workerDataArray.inline.hpp"
  27 #include "utilities/ostream.hpp"
  28 
  29 template <>
  30 size_t WorkerDataArray<size_t>::uninitialized() {
  31   return (size_t)-1;
  32 }
  33 
  34 template <>
  35 double WorkerDataArray<double>::uninitialized() {
  36   return -1.0;
  37 }
  38 
  39 template <>
  40 void WorkerDataArray<double>::WDAPrinter::summary(outputStream* out, double min, double avg, double max, double diff, double sum, bool print_sum) {
  41   out->print(" Min: %4.1lf, Avg: %4.1lf, Max: %4.1lf, Diff: %4.1lf", min * MILLIUNITS, avg * MILLIUNITS, max * MILLIUNITS, diff* MILLIUNITS);
  42   if (print_sum) {
  43     out->print(", Sum: %4.1lf", sum * MILLIUNITS);
  44   }
  45 }
  46 
  47 template <>
  48 void WorkerDataArray<size_t>::WDAPrinter::summary(outputStream* out, size_t min, double avg, size_t max, size_t diff, size_t sum, bool print_sum) {
  49   out->print(" Min: " SIZE_FORMAT ", Avg: %4.1lf, Max: " SIZE_FORMAT ", Diff: " SIZE_FORMAT, min, avg, max, diff);
  50   if (print_sum) {
  51     out->print(", Sum: " SIZE_FORMAT, sum);
  52   }
  53 }
  54 
  55 template <>
  56 void WorkerDataArray<double>::WDAPrinter::details(const WorkerDataArray<double>* phase, outputStream* out) {
  57   out->print("%-25s", "");
  58   for (uint i = 0; i < phase->_length; ++i) {
  59     double value = phase->get(i);
  60     if (value != phase->uninitialized()) {
  61       out->print(" %4.1lf", phase->get(i) * 1000.0);
  62     } else {
  63       out->print(" -");
  64     }
  65   }
  66   out->cr();
  67 }
  68 
  69 template <>
  70 void WorkerDataArray<size_t>::WDAPrinter::details(const WorkerDataArray<size_t>* phase, outputStream* out) {
  71   out->print("%-25s", "");
  72   for (uint i = 0; i < phase->_length; ++i) {
  73     size_t value = phase->get(i);
  74     if (value != phase->uninitialized()) {
  75       out->print("  " SIZE_FORMAT, phase->get(i));
  76     } else {
  77       out->print(" -");
  78     }
  79   }
  80   out->cr();
  81 }
  82 
  83 #ifndef PRODUCT
  84 
  85 #include "memory/resourceArea.hpp"
  86 
  87 void WorkerDataArray_test_verify_string(const char* expected_string, const char* actual_string) {
  88   const size_t expected_len = strlen(expected_string);
  89 
  90   assert(expected_len == strlen(actual_string),
  91       "Wrong string length, expected " SIZE_FORMAT " but got " SIZE_FORMAT "(Expected '%s' but got: '%s')",
  92       expected_len, strlen(actual_string), expected_string, actual_string);
  93 
  94   // Can't use strncmp here because floating point values use different decimal points for different locales.
  95   // Allow strings to differ in "." vs. "," only. This should still catch most errors.
  96   for (size_t i = 0; i < expected_len; i++) {
  97     char e = expected_string[i];
  98     char a = actual_string[i];
  99     if (e != a) {
 100       if ((e == '.' || e == ',') && (a == '.' || a == ',')) {
 101         // Most likely just a difference in locale
 102       } else {
 103         assert(false, "Expected '%s' but got: '%s'", expected_string, actual_string);
 104       }
 105     }
 106   }
 107 }
 108 
 109 void WorkerDataArray_test_verify_array(WorkerDataArray<size_t>& array, size_t expected_sum, double expected_avg, const char* expected_summary, const char* exected_details) {
 110   const double epsilon = 0.0001;
 111   assert(array.sum() == expected_sum, "Wrong sum, expected: " SIZE_FORMAT " but got: " SIZE_FORMAT, expected_sum, array.sum());
 112   assert(fabs(array.average() - expected_avg) < epsilon, "Wrong average, expected: %f but got: %f", expected_avg, array.average());
 113 
 114   ResourceMark rm;
 115   stringStream out;
 116   array.print_summary_on(&out);
 117   WorkerDataArray_test_verify_string(expected_summary, out.as_string());
 118   out.reset();
 119   array.print_details_on(&out);
 120   WorkerDataArray_test_verify_string(exected_details, out.as_string());
 121 }
 122 
 123 void WorkerDataArray_test_verify_array(WorkerDataArray<double>& array, double expected_sum, double expected_avg, const char* expected_summary, const char* exected_details) {
 124   const double epsilon = 0.0001;
 125   assert(fabs(array.sum() - expected_sum) < epsilon, "Wrong sum, expected: %f but got: %f", expected_sum, array.sum());
 126   assert(fabs(array.average() - expected_avg) < epsilon, "Wrong average, expected: %f but got: %f", expected_avg, array.average());
 127 
 128   ResourceMark rm;
 129   stringStream out;
 130   array.print_summary_on(&out);
 131   WorkerDataArray_test_verify_string(expected_summary, out.as_string());
 132   out.reset();
 133   array.print_details_on(&out);
 134   WorkerDataArray_test_verify_string(exected_details, out.as_string());
 135 }
 136 
 137 void WorkerDataArray_test_basic() {
 138   WorkerDataArray<size_t> array(3, "Test array");
 139   array.set(0, 5);
 140   array.set(1, 3);
 141   array.set(2, 7);
 142 
 143   WorkerDataArray_test_verify_array(array, 15, 5.0,
 144       "Test array                Min: 3, Avg:  5.0, Max: 7, Diff: 4, Sum: 15, Workers: 3\n",
 145       "                           5  3  7\n" );
 146 }
 147 
 148 void WorkerDataArray_test_add() {
 149   WorkerDataArray<size_t> array(3, "Test array");
 150   array.set(0, 5);
 151   array.set(1, 3);
 152   array.set(2, 7);
 153 
 154   for (uint i = 0; i < 3; i++) {
 155     array.add(i, 1);
 156   }
 157 
 158   WorkerDataArray_test_verify_array(array, 18, 6.0,
 159       "Test array                Min: 4, Avg:  6.0, Max: 8, Diff: 4, Sum: 18, Workers: 3\n",
 160       "                           6  4  8\n" );
 161 }
 162 
 163 void WorkerDataArray_test_with_uninitialized() {
 164   WorkerDataArray<size_t> array(3, "Test array");
 165   array.set(0, 5);
 166   array.set(1, WorkerDataArray<size_t>::uninitialized());
 167   array.set(2, 7);
 168 
 169   WorkerDataArray_test_verify_array(array, 12, 6,
 170       "Test array                Min: 5, Avg:  6.0, Max: 7, Diff: 2, Sum: 12, Workers: 2\n",
 171       "                           5 -  7\n" );
 172 }
 173 
 174 void WorkerDataArray_test_uninitialized() {
 175   WorkerDataArray<size_t> array(3, "Test array");
 176   array.set(0, WorkerDataArray<size_t>::uninitialized());
 177   array.set(1, WorkerDataArray<size_t>::uninitialized());
 178   array.set(2, WorkerDataArray<size_t>::uninitialized());
 179 
 180   WorkerDataArray_test_verify_array(array, 0, 0.0,
 181       "Test array                skipped\n",
 182       "                          - - -\n" );
 183 }
 184 
 185 void WorkerDataArray_test_double_with_uninitialized() {
 186   WorkerDataArray<double> array(3, "Test array");
 187   array.set(0, 5.1 / MILLIUNITS);
 188   array.set(1, WorkerDataArray<double>::uninitialized());
 189   array.set(2, 7.2 / MILLIUNITS);
 190 
 191   WorkerDataArray_test_verify_array(array, 12.3 / MILLIUNITS, 6.15 / MILLIUNITS,
 192       "Test array                Min:  5.1, Avg:  6.1, Max:  7.2, Diff:  2.1, Sum: 12.3, Workers: 2\n",
 193       "                           5.1 -  7.2\n" );
 194 }
 195 
 196 void WorkerDataArray_test() {
 197   WorkerDataArray_test_basic();
 198   WorkerDataArray_test_add();
 199   WorkerDataArray_test_with_uninitialized();
 200   WorkerDataArray_test_uninitialized();
 201   WorkerDataArray_test_double_with_uninitialized();
 202 }
 203 
 204 #endif