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