4141N/A/*
4141N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4141N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4141N/A *
4141N/A * This code is free software; you can redistribute it and/or modify it
4141N/A * under the terms of the GNU General Public License version 2 only, as
4141N/A * published by the Free Software Foundation.
4141N/A *
4141N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4141N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4141N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4141N/A * version 2 for more details (a copy is included in the LICENSE file that
4141N/A * accompanied this code).
4141N/A *
4141N/A * You should have received a copy of the GNU General Public License version
4141N/A * 2 along with this work; if not, write to the Free Software Foundation,
4141N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4141N/A *
4141N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4141N/A * or visit www.oracle.com if you need additional information or have any
4141N/A * questions.
4141N/A *
4141N/A */
4141N/A
4141N/A#include "precompiled.hpp"
4141N/A#include "gc_implementation/shared/gcTimer.hpp"
4141N/A#include "utilities/growableArray.hpp"
4141N/A
4141N/Avoid GCTimer::register_gc_start(jlong time) {
4141N/A _time_partitions.clear();
4141N/A _gc_start = time;
4141N/A}
4141N/A
4141N/Avoid GCTimer::register_gc_end(jlong time) {
4141N/A assert(!_time_partitions.has_active_phases(),
4141N/A "We should have ended all started phases, before ending the GC");
4141N/A
4141N/A _gc_end = time;
4141N/A}
4141N/A
4141N/Avoid GCTimer::register_gc_pause_start(const char* name, jlong time) {
4141N/A _time_partitions.report_gc_phase_start(name, time);
4141N/A}
4141N/A
4141N/Avoid GCTimer::register_gc_pause_end(jlong time) {
4141N/A _time_partitions.report_gc_phase_end(time);
4141N/A}
4141N/A
4141N/Avoid GCTimer::register_gc_phase_start(const char* name, jlong time) {
4141N/A _time_partitions.report_gc_phase_start(name, time);
4141N/A}
4141N/A
4141N/Avoid GCTimer::register_gc_phase_end(jlong time) {
4141N/A _time_partitions.report_gc_phase_end(time);
4141N/A}
4141N/A
4141N/A
4141N/Avoid STWGCTimer::register_gc_start(jlong time) {
4141N/A GCTimer::register_gc_start(time);
4141N/A register_gc_pause_start("GC Pause", time);
4141N/A}
4141N/A
4141N/Avoid STWGCTimer::register_gc_end(jlong time) {
4141N/A register_gc_pause_end(time);
4141N/A GCTimer::register_gc_end(time);
4141N/A}
4141N/A
4141N/Avoid ConcurrentGCTimer::register_gc_pause_start(const char* name, jlong time) {
4141N/A GCTimer::register_gc_pause_start(name, time);
4141N/A}
4141N/A
4141N/Avoid ConcurrentGCTimer::register_gc_pause_end(jlong time) {
4141N/A GCTimer::register_gc_pause_end(time);
4141N/A}
4141N/A
4141N/Avoid PhasesStack::clear() {
4141N/A _next_phase_level = 0;
4141N/A}
4141N/A
4141N/Avoid PhasesStack::push(int phase_index) {
4141N/A assert(_next_phase_level < PHASE_LEVELS, "Overflow");
4141N/A
4141N/A _phase_indices[_next_phase_level] = phase_index;
4141N/A
4141N/A _next_phase_level++;
4141N/A}
4141N/A
4141N/Aint PhasesStack::pop() {
4141N/A assert(_next_phase_level > 0, "Underflow");
4141N/A
4141N/A _next_phase_level--;
4141N/A
4141N/A return _phase_indices[_next_phase_level];
4141N/A}
4141N/A
4141N/Aint PhasesStack::count() const {
4141N/A return _next_phase_level;
4141N/A}
4141N/A
4141N/A
4141N/ATimePartitions::TimePartitions() {
4141N/A _phases = new (ResourceObj::C_HEAP, mtGC) GrowableArray<PausePhase>(INITIAL_CAPACITY, true, mtGC);
4141N/A clear();
4141N/A}
4141N/A
4141N/ATimePartitions::~TimePartitions() {
4141N/A delete _phases;
4141N/A _phases = NULL;
4141N/A}
4141N/A
4141N/Avoid TimePartitions::clear() {
4141N/A _phases->clear();
4141N/A _active_phases.clear();
4141N/A _sum_of_pauses = 0;
4141N/A _longest_pause = 0;
4141N/A}
4141N/A
4141N/Avoid TimePartitions::report_gc_phase_start(const char* name, jlong time) {
4141N/A assert(_phases->length() <= 1000, "Too many recored phases?");
4141N/A
4141N/A int level = _active_phases.count();
4141N/A
4141N/A PausePhase phase;
4141N/A phase.set_level(level);
4141N/A phase.set_name(name);
4141N/A phase.set_start(time);
4141N/A
4141N/A int index = _phases->append(phase);
4141N/A
4141N/A _active_phases.push(index);
4141N/A}
4141N/A
4141N/Avoid TimePartitions::update_statistics(GCPhase* phase) {
4141N/A // FIXME: This should only be done for pause phases
4141N/A if (phase->level() == 0) {
4141N/A jlong pause = phase->end() - phase->start();
4141N/A _sum_of_pauses += pause;
4141N/A _longest_pause = MAX2(pause, _longest_pause);
4141N/A }
4141N/A}
4141N/A
4141N/Avoid TimePartitions::report_gc_phase_end(jlong time) {
4141N/A int phase_index = _active_phases.pop();
4141N/A GCPhase* phase = _phases->adr_at(phase_index);
4141N/A phase->set_end(time);
4141N/A update_statistics(phase);
4141N/A}
4141N/A
4141N/Aint TimePartitions::num_phases() const {
4141N/A return _phases->length();
4141N/A}
4141N/A
4141N/AGCPhase* TimePartitions::phase_at(int index) const {
4141N/A assert(index >= 0, "Out of bounds");
4141N/A assert(index < _phases->length(), "Out of bounds");
4141N/A
4141N/A return _phases->adr_at(index);
4141N/A}
4141N/A
4141N/Ajlong TimePartitions::sum_of_pauses() {
4141N/A return _sum_of_pauses;
4141N/A}
4141N/A
4141N/Ajlong TimePartitions::longest_pause() {
4141N/A return _longest_pause;
4141N/A}
4141N/A
4141N/Abool TimePartitions::has_active_phases() {
4141N/A return _active_phases.count() > 0;
4141N/A}
4141N/A
4141N/Abool TimePartitionPhasesIterator::has_next() {
4141N/A return _next < _time_partitions->num_phases();
4141N/A}
4141N/A
4141N/AGCPhase* TimePartitionPhasesIterator::next() {
4141N/A assert(has_next(), "Must have phases left");
4141N/A return _time_partitions->phase_at(_next++);
4141N/A}
4141N/A
4141N/A
4141N/A/////////////// Unit tests ///////////////
4141N/A
4141N/A#ifndef PRODUCT
4141N/A
4141N/Aclass TimePartitionPhasesIteratorTest {
4141N/A public:
4141N/A static void all() {
4141N/A one_pause();
4141N/A two_pauses();
4141N/A one_sub_pause_phase();
4141N/A many_sub_pause_phases();
4141N/A many_sub_pause_phases2();
4141N/A max_nested_pause_phases();
4141N/A }
4141N/A
4141N/A static void validate_pause_phase(GCPhase* phase, int level, const char* name, jlong start, jlong end) {
4141N/A assert(phase->level() == level, "Incorrect level");
4141N/A assert(strcmp(phase->name(), name) == 0, "Incorrect name");
4141N/A assert(phase->start() == start, "Incorrect start");
4141N/A assert(phase->end() == end, "Incorrect end");
4141N/A }
4141N/A
4141N/A static void one_pause() {
4141N/A TimePartitions time_partitions;
4141N/A time_partitions.report_gc_phase_start("PausePhase", 2);
4141N/A time_partitions.report_gc_phase_end(8);
4141N/A
4141N/A TimePartitionPhasesIterator iter(&time_partitions);
4141N/A
4141N/A validate_pause_phase(iter.next(), 0, "PausePhase", 2, 8);
4141N/A assert(time_partitions.sum_of_pauses() == 8-2, "Incorrect");
4141N/A assert(time_partitions.longest_pause() == 8-2, "Incorrect");
4141N/A
4141N/A assert(!iter.has_next(), "Too many elements");
4141N/A }
4141N/A
4141N/A static void two_pauses() {
4141N/A TimePartitions time_partitions;
4141N/A time_partitions.report_gc_phase_start("PausePhase1", 2);
4141N/A time_partitions.report_gc_phase_end(3);
4141N/A time_partitions.report_gc_phase_start("PausePhase2", 4);
4141N/A time_partitions.report_gc_phase_end(6);
4141N/A
4141N/A TimePartitionPhasesIterator iter(&time_partitions);
4141N/A
4141N/A validate_pause_phase(iter.next(), 0, "PausePhase1", 2, 3);
4141N/A validate_pause_phase(iter.next(), 0, "PausePhase2", 4, 6);
4141N/A
4141N/A assert(time_partitions.sum_of_pauses() == 3, "Incorrect");
4141N/A assert(time_partitions.longest_pause() == 2, "Incorrect");
4141N/A
4141N/A assert(!iter.has_next(), "Too many elements");
4141N/A }
4141N/A
4141N/A static void one_sub_pause_phase() {
4141N/A TimePartitions time_partitions;
4141N/A time_partitions.report_gc_phase_start("PausePhase", 2);
4141N/A time_partitions.report_gc_phase_start("SubPhase", 3);
4141N/A time_partitions.report_gc_phase_end(4);
4141N/A time_partitions.report_gc_phase_end(5);
4141N/A
4141N/A TimePartitionPhasesIterator iter(&time_partitions);
4141N/A
4141N/A validate_pause_phase(iter.next(), 0, "PausePhase", 2, 5);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase", 3, 4);
4141N/A
4141N/A assert(time_partitions.sum_of_pauses() == 3, "Incorrect");
4141N/A assert(time_partitions.longest_pause() == 3, "Incorrect");
4141N/A
4141N/A assert(!iter.has_next(), "Too many elements");
4141N/A }
4141N/A
4141N/A static void max_nested_pause_phases() {
4141N/A TimePartitions time_partitions;
4141N/A time_partitions.report_gc_phase_start("PausePhase", 2);
4141N/A time_partitions.report_gc_phase_start("SubPhase1", 3);
4141N/A time_partitions.report_gc_phase_start("SubPhase2", 4);
4141N/A time_partitions.report_gc_phase_start("SubPhase3", 5);
4141N/A time_partitions.report_gc_phase_end(6);
4141N/A time_partitions.report_gc_phase_end(7);
4141N/A time_partitions.report_gc_phase_end(8);
4141N/A time_partitions.report_gc_phase_end(9);
4141N/A
4141N/A TimePartitionPhasesIterator iter(&time_partitions);
4141N/A
4141N/A validate_pause_phase(iter.next(), 0, "PausePhase", 2, 9);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase1", 3, 8);
4141N/A validate_pause_phase(iter.next(), 2, "SubPhase2", 4, 7);
4141N/A validate_pause_phase(iter.next(), 3, "SubPhase3", 5, 6);
4141N/A
4141N/A assert(time_partitions.sum_of_pauses() == 7, "Incorrect");
4141N/A assert(time_partitions.longest_pause() == 7, "Incorrect");
4141N/A
4141N/A assert(!iter.has_next(), "Too many elements");
4141N/A }
4141N/A
4141N/A static void many_sub_pause_phases() {
4141N/A TimePartitions time_partitions;
4141N/A time_partitions.report_gc_phase_start("PausePhase", 2);
4141N/A
4141N/A time_partitions.report_gc_phase_start("SubPhase1", 3);
4141N/A time_partitions.report_gc_phase_end(4);
4141N/A time_partitions.report_gc_phase_start("SubPhase2", 5);
4141N/A time_partitions.report_gc_phase_end(6);
4141N/A time_partitions.report_gc_phase_start("SubPhase3", 7);
4141N/A time_partitions.report_gc_phase_end(8);
4141N/A time_partitions.report_gc_phase_start("SubPhase4", 9);
4141N/A time_partitions.report_gc_phase_end(10);
4141N/A
4141N/A time_partitions.report_gc_phase_end(11);
4141N/A
4141N/A TimePartitionPhasesIterator iter(&time_partitions);
4141N/A
4141N/A validate_pause_phase(iter.next(), 0, "PausePhase", 2, 11);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase1", 3, 4);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase2", 5, 6);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase3", 7, 8);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase4", 9, 10);
4141N/A
4141N/A assert(time_partitions.sum_of_pauses() == 9, "Incorrect");
4141N/A assert(time_partitions.longest_pause() == 9, "Incorrect");
4141N/A
4141N/A assert(!iter.has_next(), "Too many elements");
4141N/A }
4141N/A
4141N/A static void many_sub_pause_phases2() {
4141N/A TimePartitions time_partitions;
4141N/A time_partitions.report_gc_phase_start("PausePhase", 2);
4141N/A
4141N/A time_partitions.report_gc_phase_start("SubPhase1", 3);
4141N/A time_partitions.report_gc_phase_start("SubPhase11", 4);
4141N/A time_partitions.report_gc_phase_end(5);
4141N/A time_partitions.report_gc_phase_start("SubPhase12", 6);
4141N/A time_partitions.report_gc_phase_end(7);
4141N/A time_partitions.report_gc_phase_end(8);
4141N/A time_partitions.report_gc_phase_start("SubPhase2", 9);
4141N/A time_partitions.report_gc_phase_start("SubPhase21", 10);
4141N/A time_partitions.report_gc_phase_end(11);
4141N/A time_partitions.report_gc_phase_start("SubPhase22", 12);
4141N/A time_partitions.report_gc_phase_end(13);
4141N/A time_partitions.report_gc_phase_end(14);
4141N/A time_partitions.report_gc_phase_start("SubPhase3", 15);
4141N/A time_partitions.report_gc_phase_end(16);
4141N/A
4141N/A time_partitions.report_gc_phase_end(17);
4141N/A
4141N/A TimePartitionPhasesIterator iter(&time_partitions);
4141N/A
4141N/A validate_pause_phase(iter.next(), 0, "PausePhase", 2, 17);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase1", 3, 8);
4141N/A validate_pause_phase(iter.next(), 2, "SubPhase11", 4, 5);
4141N/A validate_pause_phase(iter.next(), 2, "SubPhase12", 6, 7);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase2", 9, 14);
4141N/A validate_pause_phase(iter.next(), 2, "SubPhase21", 10, 11);
4141N/A validate_pause_phase(iter.next(), 2, "SubPhase22", 12, 13);
4141N/A validate_pause_phase(iter.next(), 1, "SubPhase3", 15, 16);
4141N/A
4141N/A assert(time_partitions.sum_of_pauses() == 15, "Incorrect");
4141N/A assert(time_partitions.longest_pause() == 15, "Incorrect");
4141N/A
4141N/A assert(!iter.has_next(), "Too many elements");
4141N/A }
4141N/A};
4141N/A
4141N/Aclass GCTimerTest {
4141N/Apublic:
4141N/A static void all() {
4141N/A gc_start();
4141N/A gc_end();
4141N/A }
4141N/A
4141N/A static void gc_start() {
4141N/A GCTimer gc_timer;
4141N/A gc_timer.register_gc_start(1);
4141N/A
4141N/A assert(gc_timer.gc_start() == 1, "Incorrect");
4141N/A }
4141N/A
4141N/A static void gc_end() {
4141N/A GCTimer gc_timer;
4141N/A gc_timer.register_gc_start(1);
4141N/A gc_timer.register_gc_end(2);
4141N/A
4141N/A assert(gc_timer.gc_end() == 2, "Incorrect");
4141N/A }
4141N/A};
4141N/A
4141N/Avoid GCTimerAllTest::all() {
4141N/A GCTimerTest::all();
4141N/A TimePartitionPhasesIteratorTest::all();
4141N/A}
4141N/A
4141N/A#endif