blob: d0545f8e24777dea02c47f075d2057c8d23fded4 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
2 * Copyright 2014 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#ifndef ART_RUNTIME_JIT_JIT_INSTRUMENTATION_H_
18#define ART_RUNTIME_JIT_JIT_INSTRUMENTATION_H_
19
20#include <unordered_map>
21
22#include "instrumentation.h"
23
24#include "atomic.h"
25#include "base/macros.h"
26#include "base/mutex.h"
27#include "gc_root.h"
28#include "jni.h"
29#include "object_callbacks.h"
30#include "thread_pool.h"
31
32namespace art {
33namespace mirror {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034 class Object;
35 class Throwable;
36} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070037class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070038class ArtMethod;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039union JValue;
40class Thread;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080041
42namespace jit {
Bill Buzbee1d011d92016-04-04 16:59:29 +000043static constexpr int16_t kJitCheckForOSR = -1;
44static constexpr int16_t kJitHotnessDisabled = -2;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045
Nicolas Geoffray629e9352015-11-04 17:22:16 +000046class JitInstrumentationCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080047
48class JitInstrumentationListener : public instrumentation::InstrumentationListener {
49 public:
50 explicit JitInstrumentationListener(JitInstrumentationCache* cache);
51
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010052 void MethodEntered(Thread* thread, mirror::Object* /*this_object*/,
53 ArtMethod* method, uint32_t /*dex_pc*/)
Nicolas Geoffray629e9352015-11-04 17:22:16 +000054 OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
55
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010056 void MethodExited(Thread* /*thread*/, mirror::Object* /*this_object*/,
57 ArtMethod* /*method*/, uint32_t /*dex_pc*/,
58 const JValue& /*return_value*/)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059 OVERRIDE { }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010060 void MethodUnwind(Thread* /*thread*/, mirror::Object* /*this_object*/,
61 ArtMethod* /*method*/, uint32_t /*dex_pc*/) OVERRIDE { }
62 void FieldRead(Thread* /*thread*/, mirror::Object* /*this_object*/,
63 ArtMethod* /*method*/, uint32_t /*dex_pc*/,
64 ArtField* /*field*/) OVERRIDE { }
65 void FieldWritten(Thread* /*thread*/, mirror::Object* /*this_object*/,
66 ArtMethod* /*method*/, uint32_t /*dex_pc*/,
67 ArtField* /*field*/, const JValue& /*field_value*/)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080068 OVERRIDE { }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010069 void ExceptionCaught(Thread* /*thread*/,
70 mirror::Throwable* /*exception_object*/) OVERRIDE { }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010072 void DexPcMoved(Thread* /*self*/, mirror::Object* /*this_object*/,
73 ArtMethod* /*method*/, uint32_t /*new_dex_pc*/) OVERRIDE { }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080074
Nicolas Geoffray81f0f952016-01-20 16:25:19 +000075 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t dex_pc_offset)
Nicolas Geoffray629e9352015-11-04 17:22:16 +000076 OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010078 void InvokeVirtualOrInterface(Thread* thread,
79 mirror::Object* this_object,
80 ArtMethod* caller,
81 uint32_t dex_pc,
82 ArtMethod* callee)
Mathieu Chartier3fdb3fe2016-01-14 10:24:28 -080083 OVERRIDE
84 REQUIRES(Roles::uninterruptible_)
85 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010086
Nicolas Geoffray629e9352015-11-04 17:22:16 +000087 static constexpr uint32_t kJitEvents =
88 instrumentation::Instrumentation::kMethodEntered |
Nicolas Geoffray629e9352015-11-04 17:22:16 +000089 instrumentation::Instrumentation::kInvokeVirtualOrInterface;
90
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091 private:
92 JitInstrumentationCache* const instrumentation_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070093
94 DISALLOW_IMPLICIT_CONSTRUCTORS(JitInstrumentationListener);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095};
96
Nicolas Geoffray629e9352015-11-04 17:22:16 +000097// Keeps track of which methods are hot.
98class JitInstrumentationCache {
99 public:
Bill Buzbee1d011d92016-04-04 16:59:29 +0000100 JitInstrumentationCache(uint16_t hot_method_threshold,
101 uint16_t warm_method_threshold,
Calin Juravleb2771b42016-04-07 17:09:25 +0100102 uint16_t osr_method_threshold,
103 uint16_t priority_thread_weight);
Bill Buzbee1d011d92016-04-04 16:59:29 +0000104 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples)
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000105 SHARED_REQUIRES(Locks::mutator_lock_);
106 void CreateThreadPool();
107 void DeleteThreadPool(Thread* self);
Siva Chandra05d24152016-01-05 17:43:17 -0800108
Bill Buzbee1d011d92016-04-04 16:59:29 +0000109 size_t OSRMethodThreshold() const {
110 return osr_method_threshold_;
111 }
112
Siva Chandra05d24152016-01-05 17:43:17 -0800113 size_t HotMethodThreshold() const {
114 return hot_method_threshold_;
115 }
116
Bill Buzbee1d011d92016-04-04 16:59:29 +0000117 size_t WarmMethodThreshold() const {
118 return warm_method_threshold_;
119 }
120
Calin Juravleb2771b42016-04-07 17:09:25 +0100121 size_t PriorityThreadWeight() const {
122 return priority_thread_weight_;
123 }
124
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000125 // Wait until there is no more pending compilation tasks.
126 void WaitForCompilationToFinish(Thread* self);
127
128 private:
Bill Buzbee1d011d92016-04-04 16:59:29 +0000129 uint16_t hot_method_threshold_;
130 uint16_t warm_method_threshold_;
131 uint16_t osr_method_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100132 uint16_t priority_thread_weight_;
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000133 JitInstrumentationListener listener_;
134 std::unique_ptr<ThreadPool> thread_pool_;
135
136 DISALLOW_IMPLICIT_CONSTRUCTORS(JitInstrumentationCache);
137};
138
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800139} // namespace jit
140} // namespace art
141
142#endif // ART_RUNTIME_JIT_JIT_INSTRUMENTATION_H_