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 #include "precompiled.hpp"
  25 #include "utilities/macros.hpp"
  26 #include "gc/g1/g1Predictions.hpp"
  27 #include "unittest.hpp"
  28 
  29 #if INCLUDE_ALL_GCS
  30 
  31 TEST_VM(gc, testPredictionsBasic) {
  32   double const epsilon = 1e-6;
  33 
  34   // Some basic formula tests with confidence = 0.0
  35   G1Predictions predictor(0.0);
  36   TruncatedSeq s;
  37 
  38   double p0 = predictor.get_new_prediction(&s);
  39   ASSERT_LT(p0, epsilon)
  40           << "Initial prediction of empty sequence must be 0.0 ";
  41   s.add(5.0);
  42   double p1 = predictor.get_new_prediction(&s);
  43   ASSERT_LT(fabs(p1 - 5.0), epsilon) << "Prediction should be 5.0 but is "
  44           << p1;
  45   for (int i = 0; i < 40; i++) {
  46     s.add(5.0);
  47   }
  48   double p2 = predictor.get_new_prediction(&s);
  49 
  50   ASSERT_LT(fabs(p2 - 5.0), epsilon) << "Prediction should be 5.0 but is "
  51           << p1;
  52 }
  53 
  54 TEST_VM(gc, testPredictionsAverageNotStdev) {
  55   double const epsilon = 1e-6;
  56   // The following tests checks that the initial predictions are based on
  57   // the average of the sequence and not on the stddev (which is 0).
  58   G1Predictions predictor(0.5);
  59   TruncatedSeq s;
  60 
  61   s.add(1.0);
  62   double p1 = predictor.get_new_prediction(&s);
  63 
  64   ASSERT_GT(p1, s.davg()) << "First prediction must be larger than average, "
  65           << "but avg is " << s.davg() << " and prediction " << p1;
  66   s.add(1.0);
  67   double p2 = predictor.get_new_prediction(&s);
  68   ASSERT_LT(p2, p1) << "First prediction must be larger than second, but "
  69           << "they are " << p1 << " " << p2;
  70   s.add(1.0);
  71   double p3 = predictor.get_new_prediction(&s);
  72 
  73   ASSERT_LT(p3, p2) << "Second prediction must be larger than third, "
  74           << "but they are " << p2 << " " << p3;
  75   s.add(1.0);
  76   s.add(1.0); // Five elements are now in the sequence.
  77   double p4 = predictor.get_new_prediction(&s);
  78 
  79   ASSERT_LT(p4, p3) << "Fourth prediction must be smaller than third, "
  80           << "but they are " << p3 << " " << p4;
  81 
  82   ASSERT_LT(fabs(p4 - 1.0), epsilon) << "Prediction must be 1.0+epsilon, "
  83           << "but is " << p4;
  84 }
  85 
  86 TEST_VM(gc, testPredictionsAverageStdev) {
  87   double const epsilon = 1e-6;
  88   // The following tests checks that initially prediction based on
  89   // the average is used, that gets overridden by the stddev prediction at
  90   // the end.
  91   G1Predictions predictor(0.5);
  92   TruncatedSeq s;
  93 
  94   s.add(0.5);
  95   double p1 = predictor.get_new_prediction(&s);
  96   ASSERT_GT(p1, s.davg()) << "First prediction must be larger than average, but"
  97           << " avg is " << s.davg() << " and prediction " << p1;
  98   s.add(0.2);
  99   double p2 = predictor.get_new_prediction(&s);
 100   ASSERT_LT(p2, p1) << "First prediction must be larger than second, but "
 101           << "they are " << p1 << " " << p2;
 102   s.add(0.5);
 103   double p3 = predictor.get_new_prediction(&s);
 104 
 105   ASSERT_LT(p3, p2) << "Second prediction must be larger than third, but "
 106           << "they are " << p2 << " " << p3;
 107   s.add(0.2);
 108   s.add(2.0);
 109   double p4 = predictor.get_new_prediction(&s);
 110 
 111   ASSERT_GT(p4, p3) << "Fourth prediction must be bigger than third, but "
 112           << "they are " << p3 << " " << p4;
 113 }
 114 
 115 #endif