blob: dcb346cd3ad627a124557f7a184c38d4a779612e [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"
23#include "scoped_thread_state_change.h"
24#include "thread.h"
25
26namespace art {
27
Nicolas Geoffray26705e22015-10-28 12:50:11 +000028bool ProfilingInfo::Create(Thread* self, ArtMethod* method, bool retry_allocation) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010029 // Walk over the dex instructions of the method and keep track of
30 // instructions we are interested in profiling.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010031 DCHECK(!method->IsNative());
32 const DexFile::CodeItem& code_item = *method->GetCodeItem();
33 const uint16_t* code_ptr = code_item.insns_;
34 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010035
36 uint32_t dex_pc = 0;
37 std::vector<uint32_t> entries;
38 while (code_ptr < code_end) {
39 const Instruction& instruction = *Instruction::At(code_ptr);
40 switch (instruction.Opcode()) {
41 case Instruction::INVOKE_VIRTUAL:
42 case Instruction::INVOKE_VIRTUAL_RANGE:
43 case Instruction::INVOKE_VIRTUAL_QUICK:
44 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
45 case Instruction::INVOKE_INTERFACE:
46 case Instruction::INVOKE_INTERFACE_RANGE:
47 entries.push_back(dex_pc);
48 break;
49
50 default:
51 break;
52 }
53 dex_pc += instruction.SizeInCodeUnits();
54 code_ptr += instruction.SizeInCodeUnits();
55 }
56
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010057 // We always create a `ProfilingInfo` object, even if there is no instruction we are
58 // interested in. The JIT code cache internally uses it.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010059
60 // Allocate the `ProfilingInfo` object int the JIT's data space.
61 jit::JitCodeCache* code_cache = Runtime::Current()->GetJit()->GetCodeCache();
Nicolas Geoffray26705e22015-10-28 12:50:11 +000062 return code_cache->AddProfilingInfo(self, method, entries, retry_allocation) != nullptr;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010063}
64
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010065InlineCache* ProfilingInfo::GetInlineCache(uint32_t dex_pc) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010066 InlineCache* cache = nullptr;
67 // TODO: binary search if array is too long.
68 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010069 if (cache_[i].dex_pc_ == dex_pc) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010070 cache = &cache_[i];
71 break;
72 }
73 }
74 DCHECK(cache != nullptr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010075 return cache;
76}
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010077
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010078void ProfilingInfo::AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) {
79 InlineCache* cache = GetInlineCache(dex_pc);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010080 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010081 mirror::Class* existing = cache->classes_[i].Read();
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010082 if (existing == cls) {
83 // Receiver type is already in the cache, nothing else to do.
84 return;
85 } else if (existing == nullptr) {
86 // Cache entry is empty, try to put `cls` in it.
87 GcRoot<mirror::Class> expected_root(nullptr);
88 GcRoot<mirror::Class> desired_root(cls);
89 if (!reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&cache->classes_[i])->
90 CompareExchangeStrongSequentiallyConsistent(expected_root, desired_root)) {
91 // Some other thread put a class in the cache, continue iteration starting at this
92 // entry in case the entry contains `cls`.
93 --i;
94 } else {
95 // We successfully set `cls`, just return.
96 return;
97 }
98 }
99 }
100 // Unsuccessfull - cache is full, making it megamorphic.
101 DCHECK(cache->IsMegamorphic());
102}
103
104} // namespace art