1 /*
   2  * Copyright (c) 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/g1Predictions.hpp"
  27 #include "unittest.hpp"
  28 
  29 #include "utilities/ostream.hpp"
  30 
  31 static const double epsilon = 1e-6;
  32 
  33 // Some basic formula tests with confidence = 0.0
  34 TEST_VM(G1Predictions, basic_predictions) {
  35   G1Predictions predictor(0.0);
  36   TruncatedSeq s;
  37 
  38   double p0 = predictor.get_new_prediction(&s);
  39   ASSERT_LT(p0, epsilon) << "Initial prediction of empty sequence must be 0.0";
  40 
  41   s.add(5.0);
  42   double p1 = predictor.get_new_prediction(&s);
  43   ASSERT_NEAR(p1, 5.0, epsilon);
  44 
  45   for (int i = 0; i < 40; i++) {
  46     s.add(5.0);
  47   }
  48   double p2 = predictor.get_new_prediction(&s);
  49   ASSERT_NEAR(p2, 5.0, epsilon);
  50 }
  51 
  52 // The following tests checks that the initial predictions are based on
  53 // the average of the sequence and not on the stddev (which is 0).
  54 TEST_VM(G1Predictions, average_not_stdev_predictions) {
  55   G1Predictions predictor(0.5);
  56   TruncatedSeq s;
  57 
  58   s.add(1.0);
  59   double p1 = predictor.get_new_prediction(&s);
  60   ASSERT_GT(p1, s.davg()) << "First prediction must be greater than average";
  61 
  62   s.add(1.0);
  63   double p2 = predictor.get_new_prediction(&s);
  64   ASSERT_GT(p1, p2) << "First prediction must be greater than second";
  65 
  66   s.add(1.0);
  67   double p3 = predictor.get_new_prediction(&s);
  68   ASSERT_GT(p2, p3) << "Second prediction must be greater than third";
  69 
  70   s.add(1.0);
  71   s.add(1.0); // Five elements are now in the sequence.
  72   double p4 = predictor.get_new_prediction(&s);
  73   ASSERT_LT(p4, p3) << "Fourth prediction must be smaller than third";
  74   ASSERT_NEAR(p4, 1.0, epsilon);
  75 }
  76 
  77 // The following tests checks that initially prediction based on
  78 // the average is used, that gets overridden by the stddev prediction at
  79 // the end.
  80 TEST_VM(G1Predictions, average_stdev_predictions) {
  81   G1Predictions predictor(0.5);
  82   TruncatedSeq s;
  83 
  84   s.add(0.5);
  85   double p1 = predictor.get_new_prediction(&s);
  86   ASSERT_GT(p1, s.davg()) << "First prediction must be greater than average";
  87 
  88   s.add(0.2);
  89   double p2 = predictor.get_new_prediction(&s);
  90   ASSERT_GT(p1, p2) << "First prediction must be greater than second";
  91 
  92   s.add(0.5);
  93   double p3 = predictor.get_new_prediction(&s);
  94   ASSERT_GT(p2, p3) << "Second prediction must be greater than third";
  95 
  96   s.add(0.2);
  97   s.add(2.0);
  98   double p4 = predictor.get_new_prediction(&s);
  99   ASSERT_GT(p4, p3) << "Fourth prediction must be greater than third";
 100 }
 101 
 102 // Some tests to verify bounding between [0 .. 1]
 103 TEST_VM(G1Predictions, unit_predictions) {
 104   G1Predictions predictor(0.5);
 105   TruncatedSeq s;
 106 
 107   double p0 = predictor.get_new_unit_prediction(&s);
 108   ASSERT_LT(p0, epsilon) << "Initial prediction of empty sequence must be 0.0";
 109 
 110   s.add(100.0);
 111   double p1 = predictor.get_new_unit_prediction(&s);
 112   ASSERT_NEAR(p1, 1.0, epsilon);
 113 
 114   // Feed the sequence additional positive values to test the high bound.
 115   for (int i = 0; i < 3; i++) {
 116     s.add(2.0);
 117   }
 118   ASSERT_NEAR(predictor.get_new_unit_prediction(&s), 1.0, epsilon);
 119 
 120   // Feed the sequence additional large negative value to test the low bound.
 121   for (int i = 0; i < 4; i++) {
 122     s.add(-200.0);
 123   }
 124   ASSERT_NEAR(predictor.get_new_unit_prediction(&s), 0.0, epsilon);
 125 }
 126 
 127 // Some tests to verify bounding between [0 .. +inf]
 128 TEST_VM(G1Predictions, lower_bound_zero_predictions) {
 129   G1Predictions predictor(0.5);
 130   TruncatedSeq s;
 131 
 132   double p0 = predictor.get_new_lower_zero_bound_prediction(&s);
 133   ASSERT_LT(p0, epsilon) << "Initial prediction of empty sequence must be 0.0";
 134 
 135   s.add(100.0);
 136   // Feed the sequence additional positive values to see that the high bound is not
 137   // bounded by e.g. 1.0
 138   for (int i = 0; i < 3; i++) {
 139     s.add(2.0);
 140   }
 141   ASSERT_GT(predictor.get_new_lower_zero_bound_prediction(&s), 1.0);
 142 
 143   // Feed the sequence additional large negative value to test the low bound.
 144   for (int i = 0; i < 4; i++) {
 145     s.add(-200.0);
 146   }
 147   ASSERT_NEAR(predictor.get_new_lower_zero_bound_prediction(&s), 0.0, epsilon);
 148 }