• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/dex_file.h"
18 
19 #include "art_method-inl.h"
20 #include "dex/method_reference.h"
21 #include "jit/profile_saver.h"
22 #include "jni.h"
23 #include "mirror/class-inl.h"
24 #include "mirror/executable.h"
25 #include "nativehelper/ScopedUtfChars.h"
26 #include "oat_file_assistant.h"
27 #include "oat_file_manager.h"
28 #include "profile/profile_compilation_info.h"
29 #include "scoped_thread_state_change-inl.h"
30 #include "thread.h"
31 
32 namespace art {
33 namespace {
34 
Java_Main_ensureProfilingInfo(JNIEnv * env,jclass,jobject method)35 extern "C" JNIEXPORT void JNICALL Java_Main_ensureProfilingInfo(JNIEnv* env,
36                                                                 jclass,
37                                                                 jobject method) {
38   CHECK(method != nullptr);
39   ScopedObjectAccess soa(env);
40   ObjPtr<mirror::Executable> exec = soa.Decode<mirror::Executable>(method);
41   ArtMethod* art_method = exec->GetArtMethod();
42   if (ProfilingInfo::Create(soa.Self(), art_method) == nullptr) {
43     LOG(ERROR) << "Failed to create profiling info for method " << art_method->PrettyMethod();
44   }
45 }
46 
Java_Main_ensureProfileProcessing(JNIEnv *,jclass)47 extern "C" JNIEXPORT void JNICALL Java_Main_ensureProfileProcessing(JNIEnv*, jclass) {
48   ProfileSaver::ForceProcessProfiles();
49 }
50 
Java_Main_isForBootImage(JNIEnv * env,jclass,jstring filename)51 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isForBootImage(JNIEnv* env,
52                                                                jclass,
53                                                                jstring filename) {
54   ScopedUtfChars filename_chars(env, filename);
55   CHECK(filename_chars.c_str() != nullptr);
56 
57   ProfileCompilationInfo info(/*for_boot_image=*/ true);
58   bool result = info.Load(std::string(filename_chars.c_str()), /*clear_if_invalid=*/ false);
59   return result ? JNI_TRUE : JNI_FALSE;
60 }
61 
Java_Main_presentInProfile(JNIEnv * env,jclass c,jstring filename,jobject method)62 extern "C" JNIEXPORT jboolean JNICALL Java_Main_presentInProfile(JNIEnv* env,
63                                                                  jclass c,
64                                                                  jstring filename,
65                                                                  jobject method) {
66   bool for_boot_image = Java_Main_isForBootImage(env, c, filename) == JNI_TRUE;
67   ScopedUtfChars filename_chars(env, filename);
68   CHECK(filename_chars.c_str() != nullptr);
69   ScopedObjectAccess soa(env);
70   ObjPtr<mirror::Executable> exec = soa.Decode<mirror::Executable>(method);
71   ArtMethod* art_method = exec->GetArtMethod();
72   MethodReference ref(art_method->GetDexFile(), art_method->GetDexMethodIndex());
73 
74   ProfileCompilationInfo info(Runtime::Current()->GetArenaPool(), for_boot_image);
75   if (!info.Load(filename_chars.c_str(), /*clear_if_invalid=*/false)) {
76     LOG(ERROR) << "Failed to load profile from " << filename;
77     return JNI_FALSE;
78   }
79   const ProfileCompilationInfo::MethodHotness hotness = info.GetMethodHotness(ref);
80   // TODO: Why do we check `hotness.IsHot()` instead of `hotness.IsInProfile()`
81   // in a method named `presentInProfile()`?
82   return hotness.IsHot() ? JNI_TRUE : JNI_FALSE;
83 }
84 
85 }  // namespace
86 }  // namespace art
87