blob: 8c8876015879f2b4abf52a0ba5667cc47d9b98e5 [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"
David Sehr9e734c72018-01-04 17:56:19 -080020#include "dex/dex_instruction.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010021#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)
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +000029 : baseline_hotness_count_(0),
30 method_(method),
Andreas Gampee40f65f2018-05-07 17:02:22 -070031 saved_entry_point_(nullptr),
32 number_of_inline_caches_(entries.size()),
Mathieu Chartier65975772016-08-05 10:46:36 -070033 current_inline_uses_(0),
Andreas Gampee40f65f2018-05-07 17:02:22 -070034 is_method_being_compiled_(false),
35 is_osr_method_being_compiled_(false) {
Mathieu Chartier65975772016-08-05 10:46:36 -070036 memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache));
37 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
38 cache_[i].dex_pc_ = entries[i];
39 }
Mathieu Chartier65975772016-08-05 10:46:36 -070040}
41
Nicolas Geoffray26705e22015-10-28 12:50:11 +000042bool ProfilingInfo::Create(Thread* self, ArtMethod* method, bool retry_allocation) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010043 // Walk over the dex instructions of the method and keep track of
44 // instructions we are interested in profiling.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010045 DCHECK(!method->IsNative());
Mathieu Chartier65975772016-08-05 10:46:36 -070046
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010047 std::vector<uint32_t> entries;
Mathieu Chartier69147f12017-11-06 20:02:24 -080048 for (const DexInstructionPcPair& inst : method->DexInstructions()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070049 switch (inst->Opcode()) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010050 case Instruction::INVOKE_VIRTUAL:
51 case Instruction::INVOKE_VIRTUAL_RANGE:
52 case Instruction::INVOKE_VIRTUAL_QUICK:
53 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
54 case Instruction::INVOKE_INTERFACE:
55 case Instruction::INVOKE_INTERFACE_RANGE:
Mathieu Chartier2b2bef22017-10-26 17:10:19 -070056 entries.push_back(inst.DexPc());
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010057 break;
58
59 default:
60 break;
61 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010062 }
63
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010064 // We always create a `ProfilingInfo` object, even if there is no instruction we are
65 // interested in. The JIT code cache internally uses it.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010066
67 // Allocate the `ProfilingInfo` object int the JIT's data space.
68 jit::JitCodeCache* code_cache = Runtime::Current()->GetJit()->GetCodeCache();
Nicolas Geoffray26705e22015-10-28 12:50:11 +000069 return code_cache->AddProfilingInfo(self, method, entries, retry_allocation) != nullptr;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010070}
71
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010072InlineCache* ProfilingInfo::GetInlineCache(uint32_t dex_pc) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010073 // TODO: binary search if array is too long.
74 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010075 if (cache_[i].dex_pc_ == dex_pc) {
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +000076 return &cache_[i];
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010077 }
78 }
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +000079 LOG(FATAL) << "No inline cache found for " << ArtMethod::PrettyMethod(method_) << "@" << dex_pc;
80 UNREACHABLE();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010081}
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010082
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010083void ProfilingInfo::AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) {
84 InlineCache* cache = GetInlineCache(dex_pc);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010085 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
Nicolas Geoffray13056a12017-05-11 11:48:28 +000086 mirror::Class* existing = cache->classes_[i].Read<kWithoutReadBarrier>();
87 mirror::Class* marked = ReadBarrier::IsMarked(existing);
88 if (marked == cls) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010089 // Receiver type is already in the cache, nothing else to do.
90 return;
Nicolas Geoffray13056a12017-05-11 11:48:28 +000091 } else if (marked == nullptr) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010092 // Cache entry is empty, try to put `cls` in it.
Nicolas Geoffray13056a12017-05-11 11:48:28 +000093 // Note: it's ok to spin on 'existing' here: if 'existing' is not null, that means
94 // it is a stalled heap address, which will only be cleared during SweepSystemWeaks,
95 // *after* this thread hits a suspend point.
96 GcRoot<mirror::Class> expected_root(existing);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010097 GcRoot<mirror::Class> desired_root(cls);
Orion Hodson4557b382018-01-03 11:47:54 +000098 auto atomic_root = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&cache->classes_[i]);
99 if (!atomic_root->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root)) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100100 // Some other thread put a class in the cache, continue iteration starting at this
101 // entry in case the entry contains `cls`.
102 --i;
103 } else {
104 // We successfully set `cls`, just return.
105 return;
106 }
107 }
108 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000109 // Unsuccessfull - cache is full, making it megamorphic. We do not DCHECK it though,
110 // as the garbage collector might clear the entries concurrently.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100111}
112
113} // namespace art