Home | History | Annotate | Download | only in 595-profile-saving
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "dex_file.h"
     18 
     19 #include "art_method-inl.h"
     20 #include "jit/offline_profiling_info.h"
     21 #include "jit/profile_saver.h"
     22 #include "jni.h"
     23 #include "method_reference.h"
     24 #include "mirror/class-inl.h"
     25 #include "oat_file_assistant.h"
     26 #include "oat_file_manager.h"
     27 #include "scoped_thread_state_change.h"
     28 #include "ScopedUtfChars.h"
     29 #include "thread.h"
     30 
     31 namespace art {
     32 namespace {
     33 
     34 class CreateProfilingInfoVisitor : public StackVisitor {
     35  public:
     36   explicit CreateProfilingInfoVisitor(Thread* thread, const char* method_name)
     37       SHARED_REQUIRES(Locks::mutator_lock_)
     38       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
     39         method_name_(method_name) {}
     40 
     41   bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
     42     ArtMethod* m = GetMethod();
     43     std::string m_name(m->GetName());
     44 
     45     if (m_name.compare(method_name_) == 0) {
     46       ProfilingInfo::Create(Thread::Current(), m, /* retry_allocation */ true);
     47       method_index_ = m->GetDexMethodIndex();
     48       return false;
     49     }
     50     return true;
     51   }
     52 
     53   int method_index_ = -1;
     54   const char* const method_name_;
     55 };
     56 
     57 extern "C" JNIEXPORT jint JNICALL Java_Main_ensureProfilingInfo(JNIEnv* env,
     58                                                                 jclass,
     59                                                                 jstring method_name) {
     60   ScopedUtfChars chars(env, method_name);
     61   CHECK(chars.c_str() != nullptr);
     62   ScopedObjectAccess soa(Thread::Current());
     63   CreateProfilingInfoVisitor visitor(soa.Self(), chars.c_str());
     64   visitor.WalkStack();
     65   return visitor.method_index_;
     66 }
     67 
     68 extern "C" JNIEXPORT void JNICALL Java_Main_ensureProfileProcessing(JNIEnv*, jclass) {
     69   ProfileSaver::ForceProcessProfiles();
     70 }
     71 
     72 extern "C" JNIEXPORT jboolean JNICALL Java_Main_presentInProfile(
     73       JNIEnv* env, jclass cls, jstring filename, jint method_index) {
     74   ScopedUtfChars filename_chars(env, filename);
     75   CHECK(filename_chars.c_str() != nullptr);
     76   ScopedObjectAccess soa(Thread::Current());
     77   const DexFile* dex_file = soa.Decode<mirror::Class*>(cls)->GetDexCache()->GetDexFile();
     78   return ProfileSaver::HasSeenMethod(std::string(filename_chars.c_str()),
     79                                      dex_file,
     80                                      static_cast<uint16_t>(method_index));
     81 }
     82 
     83 }  // namespace
     84 }  // namespace art
     85