blob: bf3d812f948728ba89b49eaa021618cd5850f8ef [file] [log] [blame]
Calin Juravleffc87072016-04-20 14:22:09 +01001/*
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
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070019#include "art_method-inl.h"
Calin Juravleffc87072016-04-20 14:22:09 +010020#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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Calin Juravleffc87072016-04-20 14:22:09 +010028#include "ScopedUtfChars.h"
29#include "thread.h"
30
31namespace art {
32namespace {
33
34class CreateProfilingInfoVisitor : public StackVisitor {
35 public:
36 explicit CreateProfilingInfoVisitor(Thread* thread, const char* method_name)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070037 REQUIRES_SHARED(Locks::mutator_lock_)
Calin Juravleffc87072016-04-20 14:22:09 +010038 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
39 method_name_(method_name) {}
40
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070041 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravleffc87072016-04-20 14:22:09 +010042 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
57extern "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
68extern "C" JNIEXPORT void JNICALL Java_Main_ensureProfileProcessing(JNIEnv*, jclass) {
69 ProfileSaver::ForceProcessProfiles();
70}
71
72extern "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());
Mathieu Chartier0795f232016-09-27 18:43:30 -070077 const DexFile* dex_file = soa.Decode<mirror::Class>(cls)->GetDexCache()->GetDexFile();
Calin Juravleffc87072016-04-20 14:22:09 +010078 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