blob: a7e6f8ba8cecc4aeb9f5d86a63be548ce4774e01 [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
19#include "jit/offline_profiling_info.h"
20#include "jit/profile_saver.h"
21#include "jni.h"
22#include "method_reference.h"
23#include "mirror/class-inl.h"
24#include "oat_file_assistant.h"
25#include "oat_file_manager.h"
26#include "scoped_thread_state_change.h"
27#include "ScopedUtfChars.h"
28#include "thread.h"
29
30namespace art {
31namespace {
32
33class CreateProfilingInfoVisitor : public StackVisitor {
34 public:
35 explicit CreateProfilingInfoVisitor(Thread* thread, const char* method_name)
36 SHARED_REQUIRES(Locks::mutator_lock_)
37 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
38 method_name_(method_name) {}
39
40 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
41 ArtMethod* m = GetMethod();
42 std::string m_name(m->GetName());
43
44 if (m_name.compare(method_name_) == 0) {
45 ProfilingInfo::Create(Thread::Current(), m, /* retry_allocation */ true);
46 method_index_ = m->GetDexMethodIndex();
47 return false;
48 }
49 return true;
50 }
51
52 int method_index_ = -1;
53 const char* const method_name_;
54};
55
56extern "C" JNIEXPORT jint JNICALL Java_Main_ensureProfilingInfo(JNIEnv* env,
57 jclass,
58 jstring method_name) {
59 ScopedUtfChars chars(env, method_name);
60 CHECK(chars.c_str() != nullptr);
61 ScopedObjectAccess soa(Thread::Current());
62 CreateProfilingInfoVisitor visitor(soa.Self(), chars.c_str());
63 visitor.WalkStack();
64 return visitor.method_index_;
65}
66
67extern "C" JNIEXPORT void JNICALL Java_Main_ensureProfileProcessing(JNIEnv*, jclass) {
68 ProfileSaver::ForceProcessProfiles();
69}
70
71extern "C" JNIEXPORT jboolean JNICALL Java_Main_presentInProfile(
72 JNIEnv* env, jclass cls, jstring filename, jint method_index) {
73 ScopedUtfChars filename_chars(env, filename);
74 CHECK(filename_chars.c_str() != nullptr);
75 ScopedObjectAccess soa(Thread::Current());
76 const DexFile* dex_file = soa.Decode<mirror::Class*>(cls)->GetDexCache()->GetDexFile();
77 return ProfileSaver::HasSeenMethod(std::string(filename_chars.c_str()),
78 dex_file,
79 static_cast<uint16_t>(method_index));
80}
81
82} // namespace
83} // namespace art