1 /*
   2  * Copyright (c) 2014, 2015, 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 "classfile/stringTable.hpp"
  27 #include "gc/g1/g1Log.hpp"
  28 #include "gc/g1/g1StringDedup.hpp"
  29 #include "gc/g1/g1StringDedupQueue.hpp"
  30 #include "gc/g1/g1StringDedupTable.hpp"
  31 #include "gc/g1/g1StringDedupThread.hpp"
  32 #include "gc/g1/suspendibleThreadSet.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/atomic.inline.hpp"
  35 
  36 G1StringDedupThread* G1StringDedupThread::_thread = NULL;
  37 
  38 G1StringDedupThread::G1StringDedupThread() :
  39   ConcurrentGCThread() {
  40   set_name("G1 StrDedup");
  41   create_and_start();
  42 }
  43 
  44 G1StringDedupThread::~G1StringDedupThread() {
  45   ShouldNotReachHere();
  46 }
  47 
  48 void G1StringDedupThread::create() {
  49   assert(G1StringDedup::is_enabled(), "String deduplication not enabled");
  50   assert(_thread == NULL, "One string deduplication thread allowed");
  51   _thread = new G1StringDedupThread();
  52 }
  53 
  54 G1StringDedupThread* G1StringDedupThread::thread() {
  55   assert(G1StringDedup::is_enabled(), "String deduplication not enabled");
  56   assert(_thread != NULL, "String deduplication thread not created");
  57   return _thread;
  58 }
  59 
  60 class G1StringDedupSharedClosure: public OopClosure {
  61  private:
  62   G1StringDedupStat& _stat;
  63 
  64  public:
  65   G1StringDedupSharedClosure(G1StringDedupStat& stat) : _stat(stat) {}
  66 
  67   virtual void do_oop(oop* p) { ShouldNotReachHere(); }
  68   virtual void do_oop(narrowOop* p) {
  69     oop java_string = oopDesc::load_decode_heap_oop(p);
  70     G1StringDedupTable::deduplicate(java_string, _stat);
  71   }
  72 };
  73 
  74 void G1StringDedupThread::deduplicate_shared_strings(G1StringDedupStat& stat) {
  75   G1StringDedupSharedClosure sharedStringDedup(stat);
  76   StringTable::shared_oops_do(&sharedStringDedup);
  77 }
  78 
  79 void G1StringDedupThread::run() {
  80   G1StringDedupStat total_stat;
  81 
  82   initialize_in_thread();
  83   wait_for_universe_init();
  84   deduplicate_shared_strings(total_stat);
  85 
  86   // Main loop
  87   for (;;) {
  88     G1StringDedupStat stat;
  89 
  90     stat.mark_idle();
  91 
  92     // Wait for the queue to become non-empty
  93     G1StringDedupQueue::wait();
  94     if (_should_terminate) {
  95       break;
  96     }
  97 
  98     {
  99       // Include thread in safepoints
 100       SuspendibleThreadSetJoiner sts_join;
 101 
 102       stat.mark_exec();
 103 
 104       // Process the queue
 105       for (;;) {
 106         oop java_string = G1StringDedupQueue::pop();
 107         if (java_string == NULL) {
 108           break;
 109         }
 110 
 111         G1StringDedupTable::deduplicate(java_string, stat);
 112 
 113         // Safepoint this thread if needed
 114         if (sts_join.should_yield()) {
 115           stat.mark_block();
 116           sts_join.yield();
 117           stat.mark_unblock();
 118         }
 119       }
 120 
 121       G1StringDedupTable::trim_entry_cache();
 122 
 123       stat.mark_done();
 124 
 125       // Print statistics
 126       total_stat.add(stat);
 127       print(gclog_or_tty, stat, total_stat);
 128     }
 129   }
 130 
 131   terminate();
 132 }
 133 
 134 void G1StringDedupThread::stop() {
 135   {
 136     MonitorLockerEx ml(Terminator_lock);
 137     _thread->_should_terminate = true;
 138   }
 139 
 140   G1StringDedupQueue::cancel_wait();
 141 
 142   {
 143     MonitorLockerEx ml(Terminator_lock);
 144     while (!_thread->_has_terminated) {
 145       ml.wait();
 146     }
 147   }
 148 }
 149 
 150 void G1StringDedupThread::print(outputStream* st, const G1StringDedupStat& last_stat, const G1StringDedupStat& total_stat) {
 151   if (G1Log::fine() || PrintStringDeduplicationStatistics) {
 152     G1StringDedupStat::print_summary(st, last_stat, total_stat);
 153     if (PrintStringDeduplicationStatistics) {
 154       G1StringDedupStat::print_statistics(st, last_stat, false);
 155       G1StringDedupStat::print_statistics(st, total_stat, true);
 156       G1StringDedupTable::print_statistics(st);
 157       G1StringDedupQueue::print_statistics(st);
 158     }
 159   }
 160 }