/* * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" #include "gc_implementation/g1/g1GCPhaseTimes.hpp" #include "gc_implementation/g1/g1Log.hpp" #include "gc_implementation/g1/g1StringDedup.hpp" #include "memory/allocation.hpp" #include "runtime/atomic.inline.hpp" #include "runtime/os.hpp" // Helper class for avoiding interleaved logging class LineBuffer: public StackObj { private: static const int BUFFER_LEN = 1024; static const int INDENT_CHARS = 3; char _buffer[BUFFER_LEN]; int _indent_level; int _cur; void vappend(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0) { int res = vsnprintf(&_buffer[_cur], BUFFER_LEN - _cur, format, ap); if (res != -1) { _cur += res; } else { DEBUG_ONLY(warning("buffer too small in LineBuffer");) _buffer[BUFFER_LEN -1] = 0; _cur = BUFFER_LEN; // vsnprintf above should not add to _buffer if we are called again } } public: explicit LineBuffer(int indent_level): _indent_level(indent_level), _cur(0) { for (; (_cur < BUFFER_LEN && _cur < (_indent_level * INDENT_CHARS)); _cur++) { _buffer[_cur] = ' '; } } #ifndef PRODUCT ~LineBuffer() { assert(_cur == _indent_level * INDENT_CHARS, "pending data in buffer - append_and_print_cr() not called?"); } #endif void append(const char* format, ...) ATTRIBUTE_PRINTF(2, 3) { va_list ap; va_start(ap, format); vappend(format, ap); va_end(ap); } void append_and_print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3) { va_list ap; va_start(ap, format); vappend(format, ap); va_end(ap); gclog_or_tty->print_cr("%s", _buffer); _cur = _indent_level * INDENT_CHARS; } }; template WorkerDataArray::WorkerDataArray(uint length, const char* title, bool print_sum, int log_level, uint indent_level) : _title(title), _length(0), _print_sum(print_sum), _log_level(log_level), _indent_level(indent_level), _has_new_data(true), _sub_count(NULL), _enabled(true) { assert(length > 0, "Must have some workers to store data for"); _length = length; _data = NEW_C_HEAP_ARRAY(T, _length, mtGC); } template WorkerDataArray::~WorkerDataArray() { FREE_C_HEAP_ARRAY(T, _data); } template <> void WorkerDataArray::log_value(LineBuffer& buf, double value) { buf.append("%.1lf", value * 1000); } template <> void WorkerDataArray::log_value(LineBuffer& buf, size_t value) { buf.append(SIZE_FORMAT, value); } template void WorkerDataArray::print() { if (_log_level > G1Log::level() || !_enabled) { return; } if (_length == 1) { // No need for min, max, average and sum for only one worker LineBuffer buf(_indent_level); buf.append("[%s: ", _title); log_value(buf, _data[0]); buf.append_and_print_cr("]"); return; } T min = _data[0]; T max = _data[0]; T sum = 0; LineBuffer buf(_indent_level); buf.append("[%s:", _title); for (uint i = 0; i < _length; ++i) { T val = _data[i]; min = MIN2(val, min); max = MAX2(val, max); sum += val; if (G1Log::finest()) { buf.append(" "); log_value(buf, val); } } if (G1Log::finest()) { buf.append_and_print_cr("%s", ""); } double avg = (double)sum / (double)_length; buf.append(" Min: "); log_value(buf, min); buf.append(", Avg: "); buf.append("%.1lf", avg); // Always print average as a double buf.append(", Max: "); log_value(buf, max); buf.append(", Diff: "); log_value(buf, max - min); if (_print_sum) { // for things like the start and end times the sum is not // that relevant buf.append(", Sum: "); log_value(buf, sum); } buf.append_and_print_cr("]"); if (_sub_count != NULL) { _sub_count->print(); } } #ifndef PRODUCT template <> size_t WorkerDataArray::uninitialized() { return (size_t)-1; } template <> double WorkerDataArray::uninitialized() { return -1.0; } template void WorkerDataArray::reset() { for (uint i = 0; i < _length; i++) { _data[i] = WorkerDataArray::uninitialized(); } if (_sub_count != NULL) { _sub_count->reset(); } } template void WorkerDataArray::verify() { for (uint i = 0; i < _length; i++) { assert(_data[i] != WorkerDataArray::uninitialized(), err_msg("Invalid data for worker %u in '%s'", i, _title)); } if (_sub_count != NULL) { _sub_count->verify(); } } #endif template class WorkerDataArray; template class WorkerDataArray; G1GCPhaseTimes::G1GCPhaseTimes(uint max_gc_threads) : _max_gc_threads(max_gc_threads) { assert(max_gc_threads > 0, "Must have some GC threads"); _gc_phases[GCWorkerStart] = new WorkerDataArray(max_gc_threads, "GC Worker Start (ms)", false, G1Log::LevelFiner, 2); _gc_phases[ExtRootScan] = new WorkerDataArray(max_gc_threads, "Ext Root Scanning (ms)", true, G1Log::LevelFiner, 2); _gc_phases[SATBFiltering] = new WorkerDataArray(max_gc_threads, "SATB Filtering (ms)", true, G1Log::LevelFiner, 2); _gc_phases[UpdateRS] = new WorkerDataArray(max_gc_threads, "Update RS (ms)", true, G1Log::LevelFiner, 2); _gc_phases[ScanRS] = new WorkerDataArray(max_gc_threads, "Scan RS (ms)", true, G1Log::LevelFiner, 2); _gc_phases[CodeRoots] = new WorkerDataArray(max_gc_threads, "Code Root Scanning (ms)", true, G1Log::LevelFiner, 2); _gc_phases[ObjCopy] = new WorkerDataArray(max_gc_threads, "Object Copy (ms)", true, G1Log::LevelFiner, 2); _gc_phases[Termination] = new WorkerDataArray(max_gc_threads, "Termination (ms)", true, G1Log::LevelFiner, 2); _gc_phases[GCWorkerTotal] = new WorkerDataArray(max_gc_threads, "GC Worker Total (ms)", true, G1Log::LevelFiner, 2); _gc_phases[GCWorkerEnd] = new WorkerDataArray(max_gc_threads, "GC Worker End (ms)", false, G1Log::LevelFiner, 2); _gc_phases[Other] = new WorkerDataArray(max_gc_threads, "GC Worker Other (ms)", true, G1Log::LevelFiner, 2); _update_rs_processed_buffers = new WorkerDataArray(max_gc_threads, "Processed Buffers", true, G1Log::LevelFiner, 3); _gc_phases[UpdateRS]->link_sub_count_array(_update_rs_processed_buffers); _termination_attempts = new WorkerDataArray(max_gc_threads, "Termination Attempts", true, G1Log::LevelFinest, 3); _gc_phases[Termination]->link_sub_count_array(_termination_attempts); _gc_phases[StringDedupQueueFixup] = new WorkerDataArray(max_gc_threads, "Queue Fixup (ms)", true, G1Log::LevelFiner, 2); _gc_phases[StringDedupTableFixup] = new WorkerDataArray(max_gc_threads, "Table Fixup (ms)", true, G1Log::LevelFiner, 2); _gc_phases[RedirtyCards] = new WorkerDataArray(max_gc_threads, "Parallel Redirty", true, G1Log::LevelFinest, 3); _redirtied_cards = new WorkerDataArray(max_gc_threads, "Redirtied Cards", true, G1Log::LevelFinest, 3); _gc_phases[RedirtyCards]->link_sub_count_array(_redirtied_cards); } void G1GCPhaseTimes::note_gc_start(uint active_gc_threads, bool mark_in_progress) { assert(active_gc_threads > 0, "The number of threads must be > 0"); assert(active_gc_threads <= _max_gc_threads, "The number of active threads must be <= the max number of threads"); _active_gc_threads = active_gc_threads; for (int i = 0; i < Sentinel; i++) { _gc_phases[i]->reset(); } _gc_phases[SATBFiltering]->set_enabled(mark_in_progress); _gc_phases[StringDedupQueueFixup]->set_enabled(G1StringDedup::is_enabled()); _gc_phases[StringDedupTableFixup]->set_enabled(G1StringDedup::is_enabled()); } void G1GCPhaseTimes::note_gc_end() { for (uint i = 0; i < _active_gc_threads; i++) { double worker_time = _gc_phases[GCWorkerEnd]->get(i) - _gc_phases[GCWorkerStart]->get(i); record_time(GCWorkerTotal, i , worker_time); double worker_known_time = _gc_phases[ExtRootScan]->get(i) + _gc_phases[SATBFiltering]->get(i) + _gc_phases[UpdateRS]->get(i) + _gc_phases[ScanRS]->get(i) + _gc_phases[CodeRoots]->get(i) + _gc_phases[ObjCopy]->get(i) + _gc_phases[Termination]->get(i); record_time(Other, i, worker_time - worker_known_time); } for (int i = 0; i < Sentinel; i++) { _gc_phases[i]->verify(); } } void G1GCPhaseTimes::print_stats(int level, const char* str, double value) { LineBuffer(level).append_and_print_cr("[%s: %.1lf ms]", str, value); } void G1GCPhaseTimes::print_stats(int level, const char* str, size_t value) { LineBuffer(level).append_and_print_cr("[%s: "SIZE_FORMAT"]", str, value); } void G1GCPhaseTimes::print_stats(int level, const char* str, double value, uint workers) { LineBuffer(level).append_and_print_cr("[%s: %.1lf ms, GC Workers: %u]", str, value, workers); } double G1GCPhaseTimes::accounted_time_ms() { // Subtract the root region scanning wait time. It's initialized to // zero at the start of the pause. double misc_time_ms = _root_region_scan_wait_time_ms; misc_time_ms += _cur_collection_par_time_ms; // Now subtract the time taken to fix up roots in generated code misc_time_ms += _cur_collection_code_root_fixup_time_ms; // Strong code root purge time misc_time_ms += _cur_strong_code_root_purge_time_ms; if (G1StringDedup::is_enabled()) { // String dedup fixup time misc_time_ms += _cur_string_dedup_fixup_time_ms; } // Subtract the time taken to clean the card table from the // current value of "other time" misc_time_ms += _cur_clear_ct_time_ms; return misc_time_ms; } void G1GCPhaseTimes::print(double pause_time_sec) { if (_root_region_scan_wait_time_ms > 0.0) { print_stats(1, "Root Region Scan Waiting", _root_region_scan_wait_time_ms); } print_stats(1, "Parallel Time", _cur_collection_par_time_ms, _active_gc_threads); for (int i = 0; i <= GCMainPhasesLast; i++) { _gc_phases[i]->print(); } print_stats(1, "Code Root Fixup", _cur_collection_code_root_fixup_time_ms); print_stats(1, "Code Root Purge", _cur_strong_code_root_purge_time_ms); if (G1StringDedup::is_enabled()) { print_stats(1, "String Dedup Fixup", _cur_string_dedup_fixup_time_ms, _active_gc_threads); for (int i = StringDedupPhasesFirst; i <= StringDedupPhasesLast; i++) { _gc_phases[i]->print(); } } print_stats(1, "Clear CT", _cur_clear_ct_time_ms); double misc_time_ms = pause_time_sec * MILLIUNITS - accounted_time_ms(); print_stats(1, "Other", misc_time_ms); if (_cur_verify_before_time_ms > 0.0) { print_stats(2, "Verify Before", _cur_verify_before_time_ms); } if (G1CollectedHeap::heap()->evacuation_failed()) { double evac_fail_handling = _cur_evac_fail_recalc_used + _cur_evac_fail_remove_self_forwards + _cur_evac_fail_restore_remsets; print_stats(2, "Evacuation Failure", evac_fail_handling); if (G1Log::finest()) { print_stats(3, "Recalculate Used", _cur_evac_fail_recalc_used); print_stats(3, "Remove Self Forwards", _cur_evac_fail_remove_self_forwards); print_stats(3, "Restore RemSet", _cur_evac_fail_restore_remsets); } } print_stats(2, "Choose CSet", (_recorded_young_cset_choice_time_ms + _recorded_non_young_cset_choice_time_ms)); print_stats(2, "Ref Proc", _cur_ref_proc_time_ms); print_stats(2, "Ref Enq", _cur_ref_enq_time_ms); print_stats(2, "Redirty Cards", _recorded_redirty_logged_cards_time_ms); _gc_phases[RedirtyCards]->print(); if (G1EagerReclaimHumongousObjects) { print_stats(2, "Humongous Register", _cur_fast_reclaim_humongous_register_time_ms); if (G1Log::finest()) { print_stats(3, "Humongous Total", _cur_fast_reclaim_humongous_total); print_stats(3, "Humongous Candidate", _cur_fast_reclaim_humongous_candidates); } print_stats(2, "Humongous Reclaim", _cur_fast_reclaim_humongous_time_ms); if (G1Log::finest()) { print_stats(3, "Humongous Reclaimed", _cur_fast_reclaim_humongous_reclaimed); } } print_stats(2, "Free CSet", (_recorded_young_free_cset_time_ms + _recorded_non_young_free_cset_time_ms)); if (G1Log::finest()) { print_stats(3, "Young Free CSet", _recorded_young_free_cset_time_ms); print_stats(3, "Non-Young Free CSet", _recorded_non_young_free_cset_time_ms); } if (_cur_verify_after_time_ms > 0.0) { print_stats(2, "Verify After", _cur_verify_after_time_ms); } } G1GCPhaseTimesTracker::G1GCPhaseTimesTracker(G1GCPhaseTimes* phase_times, G1GCPhaseTimes::GCPhases phase, uint worker_id) : _phase_times(phase_times), _phase(phase), _worker_id(worker_id) { _start_time = os::elapsedTime(); } G1GCPhaseTimesTracker::~G1GCPhaseTimesTracker() { _phase_times->record_time(_phase, _worker_id, os::elapsedTime() - _start_time); }