1 /*
   2  * Copyright (c) 2001, 2018, 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/shared/gcTimer.hpp"
  26 #include "utilities/ticks.hpp"
  27 #include "unittest.hpp"
  28 
  29 class GCTimerTest {
  30   public:
  31     static void register_gc_start(GCTimer* const timer, jlong ticks) {
  32         timer->register_gc_start(ticks);
  33     }
  34     static void register_gc_end(GCTimer* const timer, jlong ticks) {
  35         timer->register_gc_end(ticks);
  36     }
  37 };
  38 
  39 TEST(GCTimer, start) {
  40   GCTimer gc_timer;
  41   GCTimerTest::register_gc_start(&gc_timer, 1);
  42 
  43   EXPECT_EQ(1, gc_timer.gc_start().value());
  44 }
  45 
  46 TEST(GCTimer, end) {
  47   GCTimer gc_timer;
  48 
  49   GCTimerTest::register_gc_start(&gc_timer, 1);
  50   GCTimerTest::register_gc_end(&gc_timer, 2);
  51 
  52   ASSERT_EQ(2, gc_timer.gc_end().value());
  53 }
  54 
  55 class TimePartitionPhasesIteratorTest {
  56  public:
  57 
  58   static void validate_gc_phase(GCPhase* phase, int level, const char* name, const jlong& start, const jlong& end) {
  59     EXPECT_EQ(level, phase->level());
  60     EXPECT_STREQ(name, phase->name());
  61     EXPECT_EQ(start, phase->start().value());
  62     EXPECT_EQ(end, phase->end().value());
  63   }
  64 
  65   static void validate_pauses(const TimePartitions& time_partitions, const Tickspan& expected_sum_of_pauses, const Tickspan& expected_longest_pause) {
  66     EXPECT_EQ(expected_sum_of_pauses, time_partitions.sum_of_pauses());
  67     EXPECT_EQ(expected_longest_pause, time_partitions.longest_pause());
  68   }
  69   static void validate_pauses(const TimePartitions& time_partitions, const Tickspan& expected_pause) {
  70     TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, expected_pause, expected_pause);
  71   }
  72   static void validate_pauses(const TimePartitions& time_partitions, jlong end, jlong start) {
  73     TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, Ticks(end) - Ticks(start));
  74   }
  75   static void validate_pauses(const TimePartitions& time_partitions, jlong all_end, jlong all_start, jlong longest_end, jlong longest_start) {
  76     TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, Ticks(all_end) - Ticks(all_start), Ticks(longest_end) - Ticks(longest_start));
  77   }
  78 
  79   static void report_gc_phase_start(TimePartitions* const partitions, const char* name, jlong ticks, GCPhase::PhaseType type=GCPhase::PausePhaseType) {
  80     partitions->report_gc_phase_start(name, Ticks(ticks), type);
  81   }
  82 
  83   static void report_gc_phase_end(TimePartitions* const partitions, jlong ticks, GCPhase::PhaseType type=GCPhase::PausePhaseType) {
  84     partitions->report_gc_phase_end(Ticks(ticks), type);
  85   }
  86 };
  87 
  88 TEST(TimePartitionPhasesIterator, one_pause) {
  89   TimePartitions time_partitions;
  90   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "PausePhase", 2);
  91   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 8);
  92 
  93   TimePartitionPhasesIterator iter(&time_partitions);
  94 
  95   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "PausePhase", 2, 8));
  96 
  97   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, 8, 2));
  98 
  99   EXPECT_FALSE(iter.has_next()) << "Too many elements";
 100 }
 101 
 102 TEST(TimePartitionPhasesIterator, two_pauses) {
 103   TimePartitions time_partitions;
 104   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "PausePhase1", 2);
 105   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 3);
 106   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "PausePhase2", 4);
 107   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 6);
 108 
 109   TimePartitionPhasesIterator iter(&time_partitions);
 110 
 111   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "PausePhase1", 2, 3));
 112   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "PausePhase2", 4, 6));
 113 
 114   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, 3, 0, 2, 0));
 115 
 116   EXPECT_FALSE(iter.has_next()) << "Too many elements";
 117 }
 118 
 119 TEST(TimePartitionPhasesIterator, one_sub_pause_phase) {
 120   TimePartitions time_partitions;
 121   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "PausePhase", 2);
 122   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase", 3);
 123   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 4);
 124   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 5);
 125 
 126   TimePartitionPhasesIterator iter(&time_partitions);
 127 
 128   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "PausePhase", 2, 5));
 129   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase", 3, 4));
 130 
 131   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, 3, 0));
 132 
 133   EXPECT_FALSE(iter.has_next()) << "Too many elements";
 134 }
 135 
 136 TEST(TimePartitionPhasesIterator, max_nested_pause_phases) {
 137   TimePartitions time_partitions;
 138   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "PausePhase", 2);
 139   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase1", 3);
 140   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase2", 4);
 141   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase3", 5);
 142   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 6);
 143   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 7);
 144   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 8);
 145   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 9);
 146 
 147   TimePartitionPhasesIterator iter(&time_partitions);
 148 
 149   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "PausePhase", 2, 9));
 150   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase1", 3, 8));
 151   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 2, "SubPhase2", 4, 7));
 152   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 3, "SubPhase3", 5, 6));
 153 
 154   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, 7, 0));
 155 
 156   EXPECT_FALSE(iter.has_next()) << "Too many elements";
 157 }
 158 
 159 TEST(TimePartitionPhasesIterator, many_sub_pause_phases) {
 160   TimePartitions time_partitions;
 161   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "PausePhase", 2);
 162 
 163   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase1", 3);
 164   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 4);
 165   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase2", 5);
 166   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 6);
 167   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase3", 7);
 168   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 8);
 169   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase4", 9);
 170   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 10);
 171 
 172   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 11);
 173 
 174   TimePartitionPhasesIterator iter(&time_partitions);
 175 
 176   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "PausePhase", 2, 11));
 177   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase1", 3, 4));
 178   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase2", 5, 6));
 179   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase3", 7, 8));
 180   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase4", 9, 10));
 181 
 182   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, 9, 0));
 183 
 184   EXPECT_FALSE(iter.has_next()) << "Too many elements";
 185 }
 186 
 187 TEST(TimePartitionPhasesIterator, many_sub_pause_phases2) {
 188   TimePartitions time_partitions;
 189   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "PausePhase", 2);
 190 
 191   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase1", 3);
 192   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase11", 4);
 193   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 5);
 194   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase12", 6);
 195   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 7);
 196   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 8);
 197 
 198   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase2", 9);
 199   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase21", 10);
 200   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 11);
 201   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase22", 12);
 202   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 13);
 203   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 14);
 204 
 205   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "SubPhase3", 15);
 206   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 16);
 207 
 208   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 17);
 209 
 210   TimePartitionPhasesIterator iter(&time_partitions);
 211 
 212   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "PausePhase", 2, 17));
 213   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase1", 3, 8));
 214   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 2, "SubPhase11", 4, 5));
 215   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 2, "SubPhase12", 6, 7));
 216   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase2", 9, 14));
 217   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 2, "SubPhase21", 10, 11));
 218   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 2, "SubPhase22", 12, 13));
 219   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 1, "SubPhase3", 15, 16));
 220 
 221   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, 15, 0));
 222 
 223   EXPECT_FALSE(iter.has_next()) << "Too many elements";
 224 }
 225 
 226 TEST(TimePartitionPhasesIterator, one_concurrent) {
 227   TimePartitions time_partitions;
 228   TimePartitionPhasesIteratorTest::report_gc_phase_start(&time_partitions, "ConcurrentPhase", 2, GCPhase::ConcurrentPhaseType);
 229   TimePartitionPhasesIteratorTest::report_gc_phase_end(&time_partitions, 8, GCPhase::ConcurrentPhaseType);
 230 
 231   TimePartitionPhasesIterator iter(&time_partitions);
 232 
 233   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_gc_phase(iter.next(), 0, "ConcurrentPhase", 2, 8));
 234   // ConcurrentPhaseType should not affect to both 'sum_of_pauses()' and 'longest_pause()'.
 235   EXPECT_NO_FATAL_FAILURE(TimePartitionPhasesIteratorTest::validate_pauses(time_partitions, Tickspan()));
 236 
 237   EXPECT_FALSE(iter.has_next()) << "Too many elements";
 238 }