1 /*
   2  * Copyright (c) 2017, 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 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1CollectedHeap.hpp"
  27 #include "gc/g1/g1FullCollector.hpp"
  28 #include "gc/g1/g1FullGCMarker.hpp"
  29 #include "gc/g1/g1FullGCOopClosures.inline.hpp"
  30 #include "gc/g1/g1FullGCReferenceProcessorExecutor.hpp"
  31 #include "gc/shared/gcTraceTime.inline.hpp"
  32 #include "gc/shared/referenceProcessor.hpp"
  33 #include "memory/iterator.inline.hpp"
  34 
  35 G1FullGCReferenceProcessingExecutor::G1FullGCReferenceProcessingExecutor(G1FullCollector* collector) :
  36     _collector(collector),
  37     _reference_processor(collector->reference_processor()),
  38     _old_mt_degree(_reference_processor->num_queues()) {
  39   if (_reference_processor->processing_is_mt()) {
  40     _reference_processor->set_active_mt_degree(_collector->workers());
  41   }
  42 }
  43 
  44 G1FullGCReferenceProcessingExecutor::~G1FullGCReferenceProcessingExecutor() {
  45   if (_reference_processor->processing_is_mt()) {
  46     _reference_processor->set_active_mt_degree(_old_mt_degree);
  47   }
  48 }
  49 
  50 G1FullGCReferenceProcessingExecutor::G1RefProcTaskProxy::G1RefProcTaskProxy(ProcessTask& proc_task,
  51                                                                       G1FullCollector* collector) :
  52      AbstractGangTask("G1 reference processing task"),
  53      _proc_task(proc_task),
  54      _collector(collector),
  55      _terminator(_collector->workers(), _collector->oop_queue_set()) { }
  56 
  57 void G1FullGCReferenceProcessingExecutor::G1RefProcTaskProxy::work(uint worker_id) {
  58   G1FullGCMarker* marker = _collector->marker(worker_id);
  59   G1IsAliveClosure is_alive(_collector->mark_bitmap());
  60   G1FullKeepAliveClosure keep_alive(marker);
  61   _proc_task.work(worker_id,
  62                   is_alive,
  63                   keep_alive,
  64                   *marker->stack_closure());
  65 }
  66 
  67 void G1FullGCReferenceProcessingExecutor::run_task(AbstractGangTask* task) {
  68   G1CollectedHeap::heap()->workers()->run_task(task, _collector->workers());
  69 }
  70 
  71 void G1FullGCReferenceProcessingExecutor::run_task(AbstractGangTask* task, uint workers) {
  72   G1CollectedHeap::heap()->workers()->run_task(task, workers);
  73 }
  74 
  75 void G1FullGCReferenceProcessingExecutor::execute(ProcessTask& proc_task, uint ergo_workers) {
  76   G1RefProcTaskProxy proc_task_proxy(proc_task, _collector);
  77   run_task(&proc_task_proxy, ergo_workers);
  78 }
  79 
  80 void G1FullGCReferenceProcessingExecutor::execute(STWGCTimer* timer, G1FullGCTracer* tracer) {
  81   GCTraceTime(Debug, gc, phases) debug("Phase 1: Reference Processing", timer);
  82   // Process reference objects found during marking.
  83   G1FullGCMarker* marker = _collector->marker(0);
  84   G1IsAliveClosure is_alive(_collector->mark_bitmap());
  85   G1FullKeepAliveClosure keep_alive(marker);
  86   ReferenceProcessorPhaseTimes pt(timer, _reference_processor->max_num_queues());
  87   AbstractRefProcTaskExecutor* executor = _reference_processor->processing_is_mt() ? this : NULL;
  88 
  89   // Process discovered references, use this executor if multi-threaded
  90   // processing is enabled.
  91   const ReferenceProcessorStats& stats =
  92       _reference_processor->process_discovered_references(&is_alive,
  93                                                           &keep_alive,
  94                                                           marker->stack_closure(),
  95                                                           executor,
  96                                                           &pt);
  97 
  98   tracer->report_gc_reference_stats(stats);
  99   pt.print_all_references();
 100 
 101   assert(marker->oop_stack()->is_empty(), "Should be no oops on the stack");
 102 }