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