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 "gc/g1/g1CollectedHeap.inline.hpp"
  26 #include "gc/g1/g1IHOPControl.hpp"
  27 #include "gc/g1/g1Predictions.hpp"
  28 #include "unittest.hpp"
  29 
  30 static void test_update(G1IHOPControl* ctrl, double alloc_time,
  31                         size_t alloc_amount, size_t young_size,
  32                         double mark_time) {
  33   for (int i = 0; i < 100; i++) {
  34     ctrl->update_allocation_info(alloc_time, alloc_amount);
  35     ctrl->update_additional_buffer(young_size);
  36     ctrl->update_marking_length(mark_time);
  37   }
  38 }
  39 
  40 // @requires UseG1GC
  41 TEST_VM(G1StaticIHOPControl, simple) {
  42   // Test requires G1
  43   if (!UseG1GC) {
  44     return;
  45   }
  46 
  47   const size_t initial_ihop = 45;
  48 
  49   G1StaticIHOPControl ctrl(initial_ihop);
  50   ctrl.update_target_occupancy(100);
  51 
  52   size_t threshold = ctrl.get_conc_mark_start_threshold();
  53   EXPECT_EQ(initial_ihop, threshold);
  54 
  55   ctrl.update_allocation_info(100.0, 100);
  56   ctrl.update_additional_buffer(100);
  57   threshold = ctrl.get_conc_mark_start_threshold();
  58   EXPECT_EQ(initial_ihop, threshold);
  59 
  60   ctrl.update_marking_length(1000.0);
  61   threshold = ctrl.get_conc_mark_start_threshold();
  62   EXPECT_EQ(initial_ihop, threshold);
  63 
  64   // Whatever we pass, the IHOP value must stay the same.
  65   test_update(&ctrl, 2, 10, 10, 3);
  66   threshold = ctrl.get_conc_mark_start_threshold();
  67 
  68   EXPECT_EQ(initial_ihop, threshold);
  69 
  70   test_update(&ctrl, 12, 10, 10, 3);
  71   threshold = ctrl.get_conc_mark_start_threshold();
  72 
  73   EXPECT_EQ(initial_ihop, threshold);
  74 }
  75 
  76 // @requires UseG1GC
  77 TEST_VM(G1AdaptiveIHOPControl, simple) {
  78   // Test requires G1
  79   if (!UseG1GC) {
  80     return;
  81   }
  82 
  83   const size_t initial_threshold = 45;
  84   const size_t young_size = 10;
  85   const size_t target_size = 100;
  86 
  87   // The final IHOP value is always
  88   // target_size - (young_size + alloc_amount/alloc_time * marking_time)
  89 
  90   G1Predictions pred(0.95);
  91   G1AdaptiveIHOPControl ctrl(initial_threshold, &pred, 0, 0);
  92   ctrl.update_target_occupancy(target_size);
  93 
  94   // First "load".
  95   const size_t alloc_time1 = 2;
  96   const size_t alloc_amount1 = 10;
  97   const size_t marking_time1 = 2;
  98   const size_t settled_ihop1 = target_size
  99           - (young_size + alloc_amount1 / alloc_time1 * marking_time1);
 100 
 101   size_t threshold;
 102   threshold = ctrl.get_conc_mark_start_threshold();
 103 
 104   EXPECT_EQ(initial_threshold, threshold);
 105 
 106   for (size_t i = 0; i < G1AdaptiveIHOPNumInitialSamples - 1; i++) {
 107     ctrl.update_allocation_info(alloc_time1, alloc_amount1);
 108     ctrl.update_additional_buffer(young_size);
 109     ctrl.update_marking_length(marking_time1);
 110     // Not enough data yet.
 111     threshold = ctrl.get_conc_mark_start_threshold();
 112 
 113     ASSERT_EQ(initial_threshold, threshold) << "on step " << i;
 114   }
 115 
 116   test_update(&ctrl, alloc_time1, alloc_amount1, young_size, marking_time1);
 117 
 118   threshold = ctrl.get_conc_mark_start_threshold();
 119 
 120   EXPECT_EQ(settled_ihop1, threshold);
 121 
 122   // Second "load". A bit higher allocation rate.
 123   const size_t alloc_time2 = 2;
 124   const size_t alloc_amount2 = 30;
 125   const size_t marking_time2 = 2;
 126   const size_t settled_ihop2 = target_size
 127           - (young_size + alloc_amount2 / alloc_time2 * marking_time2);
 128 
 129   test_update(&ctrl, alloc_time2, alloc_amount2, young_size, marking_time2);
 130 
 131   threshold = ctrl.get_conc_mark_start_threshold();
 132 
 133   EXPECT_LT(threshold, settled_ihop1);
 134 
 135   // Third "load". Very high (impossible) allocation rate.
 136   const size_t alloc_time3 = 1;
 137   const size_t alloc_amount3 = 50;
 138   const size_t marking_time3 = 2;
 139   const size_t settled_ihop3 = 0;
 140 
 141   test_update(&ctrl, alloc_time3, alloc_amount3, young_size, marking_time3);
 142   threshold = ctrl.get_conc_mark_start_threshold();
 143 
 144   EXPECT_EQ(settled_ihop3, threshold);
 145 
 146   // And back to some arbitrary value.
 147   test_update(&ctrl, alloc_time2, alloc_amount2, young_size, marking_time2);
 148 
 149   threshold = ctrl.get_conc_mark_start_threshold();
 150 
 151   EXPECT_GT(threshold, settled_ihop3);
 152 }