1 /*
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/dex_instruction.h"
21 #include "jit/jit.h"
22 #include "jit/jit_code_cache.h"
23 #include "scoped_thread_state_change-inl.h"
24 #include "thread.h"
25 
26 namespace art {
27 
ProfilingInfo(ArtMethod * method,const std::vector<uint32_t> & entries)28 ProfilingInfo::ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries)
29       : baseline_hotness_count_(0),
30         method_(method),
31         saved_entry_point_(nullptr),
32         number_of_inline_caches_(entries.size()),
33         current_inline_uses_(0) {
34   memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache));
35   for (size_t i = 0; i < number_of_inline_caches_; ++i) {
36     cache_[i].dex_pc_ = entries[i];
37   }
38 }
39 
Create(Thread * self,ArtMethod * method,bool retry_allocation)40 bool ProfilingInfo::Create(Thread* self, ArtMethod* method, bool retry_allocation) {
41   // Walk over the dex instructions of the method and keep track of
42   // instructions we are interested in profiling.
43   DCHECK(!method->IsNative());
44 
45   std::vector<uint32_t> entries;
46   for (const DexInstructionPcPair& inst : method->DexInstructions()) {
47     switch (inst->Opcode()) {
48       case Instruction::INVOKE_VIRTUAL:
49       case Instruction::INVOKE_VIRTUAL_RANGE:
50       case Instruction::INVOKE_VIRTUAL_QUICK:
51       case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
52       case Instruction::INVOKE_INTERFACE:
53       case Instruction::INVOKE_INTERFACE_RANGE:
54         entries.push_back(inst.DexPc());
55         break;
56 
57       default:
58         break;
59     }
60   }
61 
62   // We always create a `ProfilingInfo` object, even if there is no instruction we are
63   // interested in. The JIT code cache internally uses it.
64 
65   // Allocate the `ProfilingInfo` object int the JIT's data space.
66   jit::JitCodeCache* code_cache = Runtime::Current()->GetJit()->GetCodeCache();
67   return code_cache->AddProfilingInfo(self, method, entries, retry_allocation) != nullptr;
68 }
69 
GetInlineCache(uint32_t dex_pc)70 InlineCache* ProfilingInfo::GetInlineCache(uint32_t dex_pc) {
71   // TODO: binary search if array is too long.
72   for (size_t i = 0; i < number_of_inline_caches_; ++i) {
73     if (cache_[i].dex_pc_ == dex_pc) {
74       return &cache_[i];
75     }
76   }
77   LOG(FATAL) << "No inline cache found for "  << ArtMethod::PrettyMethod(method_) << "@" << dex_pc;
78   UNREACHABLE();
79 }
80 
AddInvokeInfo(uint32_t dex_pc,mirror::Class * cls)81 void ProfilingInfo::AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) {
82   InlineCache* cache = GetInlineCache(dex_pc);
83   for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
84     mirror::Class* existing = cache->classes_[i].Read<kWithoutReadBarrier>();
85     mirror::Class* marked = ReadBarrier::IsMarked(existing);
86     if (marked == cls) {
87       // Receiver type is already in the cache, nothing else to do.
88       return;
89     } else if (marked == nullptr) {
90       // Cache entry is empty, try to put `cls` in it.
91       // Note: it's ok to spin on 'existing' here: if 'existing' is not null, that means
92       // it is a stalled heap address, which will only be cleared during SweepSystemWeaks,
93       // *after* this thread hits a suspend point.
94       GcRoot<mirror::Class> expected_root(existing);
95       GcRoot<mirror::Class> desired_root(cls);
96       auto atomic_root = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&cache->classes_[i]);
97       if (!atomic_root->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root)) {
98         // Some other thread put a class in the cache, continue iteration starting at this
99         // entry in case the entry contains `cls`.
100         --i;
101       } else {
102         // We successfully set `cls`, just return.
103         return;
104       }
105     }
106   }
107   // Unsuccessfull - cache is full, making it megamorphic. We do not DCHECK it though,
108   // as the garbage collector might clear the entries concurrently.
109 }
110 
111 }  // namespace art
112