1 /*
   2  * Copyright (c) 2014, 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 "classfile/stringTable.hpp"
  27 #include "gc/shared/stringdedup/stringDedup.hpp"
  28 #include "gc/shared/stringdedup/stringDedupQueue.hpp"
  29 #include "gc/shared/stringdedup/stringDedupQueue.inline.hpp"
  30 #include "gc/shared/stringdedup/stringDedupTable.hpp"
  31 #include "gc/shared/stringdedup/stringDedupThread.hpp"
  32 #include "gc/shared/suspendibleThreadSet.hpp"
  33 #include "logging/log.hpp"
  34 #include "oops/access.inline.hpp"
  35 #include "oops/oop.inline.hpp"
  36 
  37 StringDedupThread* StringDedupThread::_thread = NULL;
  38 
  39 StringDedupThread::StringDedupThread() :
  40   ConcurrentGCThread() {
  41   set_name("StrDedup");
  42   create_and_start();
  43 }
  44 
  45 StringDedupThread::~StringDedupThread() {
  46   ShouldNotReachHere();
  47 }
  48 
  49 StringDedupThread* StringDedupThread::thread() {
  50   assert(_thread != NULL, "String deduplication thread not created");
  51   return _thread;
  52 }
  53 
  54 class StringDedupSharedClosure: public OopClosure {
  55  private:
  56   StringDedupStat* _stat;
  57 
  58  public:
  59   StringDedupSharedClosure(StringDedupStat* stat) : _stat(stat) {}
  60 
  61   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
  62   virtual void do_oop(oop* p) {
  63     oop java_string = RawAccess<>::oop_load(p);
  64     StringDedupTable::deduplicate(java_string, _stat);
  65   }
  66 };
  67 
  68 // The CDS archive does not include the string deduplication table. Only the string
  69 // table is saved in the archive. The shared strings from CDS archive need to be
  70 // added to the string deduplication table before deduplication occurs. That is
  71 // done in the beginning of the StringDedupThread (see StringDedupThread::do_deduplication()).
  72 void StringDedupThread::deduplicate_shared_strings(StringDedupStat* stat) {
  73   StringDedupSharedClosure sharedStringDedup(stat);
  74   StringTable::shared_oops_do(&sharedStringDedup);
  75 }
  76 
  77 void StringDedupThread::stop_service() {
  78   StringDedupQueue::cancel_wait();
  79 }
  80 
  81 void StringDedupThread::print_start(const StringDedupStat* last_stat) {
  82   StringDedupStat::print_start(last_stat);
  83 }
  84 
  85 void StringDedupThread::print_end(const StringDedupStat* last_stat, const StringDedupStat* total_stat) {
  86   StringDedupStat::print_end(last_stat, total_stat);
  87   if (log_is_enabled(Debug, gc, stringdedup)) {
  88     last_stat->print_statistics(false);
  89     total_stat->print_statistics(true);
  90 
  91     StringDedupTable::print_statistics();
  92     StringDedupQueue::print_statistics();
  93   }
  94 }