1 /*
  2  * Copyright (c) 2024, 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 "cds/methodProfiler.hpp"
 27 #include "code/nmethod.hpp"
 28 #include "memory/metadataFactory.hpp"
 29 #include "oops/recompilationSchedule.hpp"
 30 #include "oops/trainingData.hpp"
 31 
 32 Array<MethodTrainingData*>* RecompilationSchedule::_schedule = nullptr;
 33 Array<MethodTrainingData*>* RecompilationSchedule::_schedule_for_dumping = nullptr;
 34 volatile bool* RecompilationSchedule::_status = nullptr;
 35 
 36 void RecompilationSchedule::initialize() {
 37   if (TrainingData::have_data()) {
 38     if (_schedule != nullptr && _schedule->length() > 0) {
 39       const int size = _schedule->length();
 40       _status = NEW_C_HEAP_ARRAY(bool, size, mtCompiler);
 41       for (int i = 0; i < size; i++) {
 42         _status[i] = false;
 43       }
 44     }
 45   }
 46 }
 47 
 48 void RecompilationSchedule::prepare(TRAPS) {
 49   if (!TrainingData::need_data()) {
 50     return;
 51   }
 52 #if INCLUDE_CDS
 53   auto nmethods = MethodProfiler::sampled_nmethods();
 54   GrowableArray<MethodTrainingData*> dyn_schedule;
 55   for (auto it = nmethods->begin(); it != nmethods->end(); ++it) {
 56     nmethod* nm = *it;
 57     if (RecordOnlyTopCompilations && nm->method_profiling_count() == 0) {
 58       break;
 59     }
 60     if (nm->method() != nullptr) {
 61       MethodTrainingData* mtd = nm->method()->training_data_or_null();
 62       if (mtd != nullptr) {
 63         dyn_schedule.append(mtd);
 64       }
 65     }
 66   }
 67   delete nmethods;
 68   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 69   _schedule_for_dumping = MetadataFactory::new_array<MethodTrainingData*>(loader_data, dyn_schedule.length(), CHECK);
 70   int i = 0;
 71   for (auto it = dyn_schedule.begin(); it != dyn_schedule.end(); ++it) {
 72     _schedule_for_dumping->at_put(i++, *it);
 73   }
 74 #endif
 75 }
 76 
 77 #if INCLUDE_CDS
 78 void RecompilationSchedule::iterate_roots(MetaspaceClosure* it) {
 79   if (!TrainingData::need_data()) {
 80     return;
 81   }
 82   it->push(&_schedule_for_dumping);
 83 }
 84 
 85 void RecompilationSchedule::cleanup() {
 86   _status = nullptr;
 87 }
 88 
 89 void RecompilationSchedule::serialize_training_data(SerializeClosure* soc) {
 90   if (soc->writing()) {
 91     soc->do_ptr(&_schedule_for_dumping);
 92   } else {
 93     soc->do_ptr(&_schedule);
 94   }
 95 }
 96 
 97 
 98 void RecompilationSchedule::print_archived_training_data_on(outputStream* st) {
 99   if (_schedule != nullptr && _schedule->length() > 0) {
100     st->print_cr("Archived TrainingData Recompilation Schedule");
101     for (int i = 0; i < _schedule->length(); i++) {
102       st->print("%4d: ", i);
103       MethodTrainingData* mtd = _schedule->at(i);
104       if (mtd != nullptr) {
105         mtd->print_on(st);
106       } else {
107         st->print("nullptr");
108       }
109       st->cr();
110     }
111   }
112 }
113 #endif //INCLUDE_CDS