1 /*
   2  * Copyright (c) 2015, 2017, 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 #include "precompiled.hpp"
  25 #include "gc/z/zHeap.hpp"
  26 #include "gc/z/zOopClosures.inline.hpp"
  27 #include "gc/z/zPage.hpp"
  28 #include "gc/z/zRelocate.hpp"
  29 #include "gc/z/zRelocationSet.inline.hpp"
  30 #include "gc/z/zRootsIterator.hpp"
  31 #include "gc/z/zTask.hpp"
  32 #include "gc/z/zWorkers.hpp"
  33 
  34 ZRelocate::ZRelocate(ZWorkers* workers) :
  35     _workers(workers) {}
  36 
  37 class ZRelocateRootsTask : public ZTask {
  38 private:
  39   ZRootsIterator _roots;
  40 
  41 public:
  42   ZRelocateRootsTask() :
  43       ZTask("ZRelocateRootsTask"),
  44       _roots() {}
  45 
  46   virtual void work() {
  47     // During relocation we need to visit the JVMTI
  48     // export weak roots to rehash the JVMTI tag map
  49     ZRelocateRootOopClosure cl;
  50     _roots.oops_do(&cl, true /* visit_jvmti_weak_export */);
  51   }
  52 };
  53 
  54 void ZRelocate::start() {
  55   ZRelocateRootsTask task;
  56   _workers->run_parallel(&task);
  57 }
  58 
  59 class ZRelocateObjectClosure : public ObjectClosure {
  60 private:
  61   ZPage* const _page;
  62 
  63 public:
  64   ZRelocateObjectClosure(ZPage* page) :
  65       _page(page) {}
  66 
  67   virtual void do_object(oop o) {
  68     _page->relocate_object(ZOop::to_address(o));
  69   }
  70 };
  71 
  72 bool ZRelocate::work(ZRelocationSetParallelIterator* iter) {
  73   bool success = true;
  74 
  75   // Relocate pages in the relocation set
  76   for (ZPage* page; iter->next(&page);) {
  77     // Relocate objects in page
  78     ZRelocateObjectClosure cl(page);
  79     page->object_iterate(&cl);
  80 
  81     if (ZVerifyForwarding) {
  82       page->verify_forwarding();
  83     }
  84 
  85     if (page->is_pinned()) {
  86       // Relocation failed, page is now pinned
  87       success = false;
  88     } else {
  89       // Relocation succeeded, release page
  90       ZHeap::heap()->release_page(page, true /* reclaimed */);
  91     }
  92   }
  93 
  94   return success;
  95 }
  96 
  97 class ZRelocateTask : public ZTask {
  98 private:
  99   ZRelocate* const               _relocate;
 100   ZRelocationSetParallelIterator _iter;
 101   bool                           _failed;
 102 
 103 public:
 104   ZRelocateTask(ZRelocate* relocate, ZRelocationSet* relocation_set) :
 105       ZTask("ZRelocateTask"),
 106       _relocate(relocate),
 107       _iter(relocation_set),
 108       _failed(false) {}
 109 
 110   virtual void work() {
 111     if (!_relocate->work(&_iter)) {
 112       _failed = true;
 113     }
 114   }
 115 
 116   bool failed() const {
 117     return _failed;
 118   }
 119 };
 120 
 121 bool ZRelocate::relocate(ZRelocationSet* relocation_set) {
 122   ZRelocateTask task(this, relocation_set);
 123   _workers->run_concurrent(&task);
 124   return !task.failed();
 125 }