blob: 7d80d2c2082d5e4a47f775aad807e0bca146492a [file] [log] [blame]
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001/*
2 * Copyright (C) 2015 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 "profiling_info.h"
18
19#include "art_method-inl.h"
20#include "dex_instruction.h"
21#include "jit/jit.h"
22#include "jit/jit_code_cache.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070023#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010024#include "thread.h"
25
26namespace art {
27
Mathieu Chartier65975772016-08-05 10:46:36 -070028ProfilingInfo::ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries)
29 : number_of_inline_caches_(entries.size()),
30 method_(method),
31 is_method_being_compiled_(false),
32 is_osr_method_being_compiled_(false),
33 current_inline_uses_(0),
34 saved_entry_point_(nullptr) {
35 memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache));
36 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
37 cache_[i].dex_pc_ = entries[i];
38 }
Mathieu Chartier65975772016-08-05 10:46:36 -070039}
40
Nicolas Geoffray26705e22015-10-28 12:50:11 +000041bool ProfilingInfo::Create(Thread* self, ArtMethod* method, bool retry_allocation) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010042 // Walk over the dex instructions of the method and keep track of
43 // instructions we are interested in profiling.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010044 DCHECK(!method->IsNative());
Mathieu Chartier65975772016-08-05 10:46:36 -070045
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010046 const DexFile::CodeItem& code_item = *method->GetCodeItem();
47 const uint16_t* code_ptr = code_item.insns_;
48 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010049
50 uint32_t dex_pc = 0;
51 std::vector<uint32_t> entries;
52 while (code_ptr < code_end) {
53 const Instruction& instruction = *Instruction::At(code_ptr);
54 switch (instruction.Opcode()) {
55 case Instruction::INVOKE_VIRTUAL:
56 case Instruction::INVOKE_VIRTUAL_RANGE:
57 case Instruction::INVOKE_VIRTUAL_QUICK:
58 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
59 case Instruction::INVOKE_INTERFACE:
60 case Instruction::INVOKE_INTERFACE_RANGE:
61 entries.push_back(dex_pc);
62 break;
63
64 default:
65 break;
66 }
67 dex_pc += instruction.SizeInCodeUnits();
68 code_ptr += instruction.SizeInCodeUnits();
69 }
70
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010071 // We always create a `ProfilingInfo` object, even if there is no instruction we are
72 // interested in. The JIT code cache internally uses it.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010073
74 // Allocate the `ProfilingInfo` object int the JIT's data space.
75 jit::JitCodeCache* code_cache = Runtime::Current()->GetJit()->GetCodeCache();
Nicolas Geoffray26705e22015-10-28 12:50:11 +000076 return code_cache->AddProfilingInfo(self, method, entries, retry_allocation) != nullptr;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010077}
78
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010079InlineCache* ProfilingInfo::GetInlineCache(uint32_t dex_pc) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010080 // TODO: binary search if array is too long.
81 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010082 if (cache_[i].dex_pc_ == dex_pc) {
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +000083 return &cache_[i];
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010084 }
85 }
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +000086 LOG(FATAL) << "No inline cache found for " << ArtMethod::PrettyMethod(method_) << "@" << dex_pc;
87 UNREACHABLE();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010088}
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010089
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010090void ProfilingInfo::AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) {
91 InlineCache* cache = GetInlineCache(dex_pc);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010092 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010093 mirror::Class* existing = cache->classes_[i].Read();
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010094 if (existing == cls) {
95 // Receiver type is already in the cache, nothing else to do.
96 return;
97 } else if (existing == nullptr) {
98 // Cache entry is empty, try to put `cls` in it.
99 GcRoot<mirror::Class> expected_root(nullptr);
100 GcRoot<mirror::Class> desired_root(cls);
101 if (!reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&cache->classes_[i])->
102 CompareExchangeStrongSequentiallyConsistent(expected_root, desired_root)) {
103 // Some other thread put a class in the cache, continue iteration starting at this
104 // entry in case the entry contains `cls`.
105 --i;
106 } else {
107 // We successfully set `cls`, just return.
108 return;
109 }
110 }
111 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000112 // Unsuccessfull - cache is full, making it megamorphic. We do not DCHECK it though,
113 // as the garbage collector might clear the entries concurrently.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100114}
115
116} // namespace art