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 #if INCLUDE_CDS
 52   auto nmethods = MethodProfiler::sampled_nmethods();
 53   GrowableArray<MethodTrainingData*> dyn_schedule;
 54   for (auto it = nmethods->begin(); it != nmethods->end(); ++it) {
 55     nmethod* nm = *it;
 56     if (RecordOnlyTopCompilations && nm->method_profiling_count() == 0) {
 57       break;
 58     }
 59     if (nm->method() != nullptr) {
 60       MethodTrainingData* mtd = nm->method()->training_data_or_null();
 61       if (mtd != nullptr) {
 62         dyn_schedule.append(mtd);
 63       }
 64     }
 65   }
 66   delete nmethods;
 67   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 68   _schedule_for_dumping = MetadataFactory::new_array<MethodTrainingData*>(loader_data, dyn_schedule.length(), CHECK);
 69   int i = 0;
 70   for (auto it = dyn_schedule.begin(); it != dyn_schedule.end(); ++it) {
 71     _schedule_for_dumping->at_put(i++, *it);
 72   }
 73 #endif
 74 }
 75 
 76 #if INCLUDE_CDS
 77 void RecompilationSchedule::iterate_roots(MetaspaceClosure* it) {
 78   if (!TrainingData::need_data()) {
 79     return;
 80   }
 81   it->push(&_schedule_for_dumping);
 82 }
 83 
 84 void RecompilationSchedule::cleanup() {
 85   _status = nullptr;
 86 }
 87 
 88 void RecompilationSchedule::serialize_training_data(SerializeClosure* soc) {
 89   if (soc->writing()) {
 90     soc->do_ptr(&_schedule_for_dumping);
 91   } else {
 92     soc->do_ptr(&_schedule);
 93   }
 94 }
 95 
 96 
 97 void RecompilationSchedule::print_archived_training_data_on(outputStream* st) {
 98   if (_schedule != nullptr && _schedule->length() > 0) {
 99     st->print_cr("Archived TrainingData Recompilation Schedule");
100     for (int i = 0; i < _schedule->length(); i++) {
101       st->print("%4d: ", i);
102       MethodTrainingData* mtd = _schedule->at(i);
103       if (mtd != nullptr) {
104         mtd->print_on(st);
105       } else {
106         st->print("nullptr");
107       }
108       st->cr();
109     }
110   }
111 }
112 #endif //INCLUDE_CDS