blob: 9eb464b841ba4f89385fb1ce517eb5b897540bb8 [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 Class;
35 class Object;
36 class Throwable;
37} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070038class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070039class ArtMethod;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040union JValue;
41class Thread;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042
43namespace jit {
44
45// Keeps track of which methods are hot.
46class JitInstrumentationCache {
47 public:
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010048 JitInstrumentationCache(size_t hot_method_threshold, size_t warm_method_threshold);
Mathieu Chartiere401d142015-04-22 13:56:20 -070049 void AddSamples(Thread* self, ArtMethod* method, size_t samples)
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010050 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051 void CreateThreadPool();
52 void DeleteThreadPool();
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070053 // Wait until there is no more pending compilation tasks.
54 void WaitForCompilationToFinish(Thread* self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080055
56 private:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057 size_t hot_method_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010058 size_t warm_method_threshold_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059 std::unique_ptr<ThreadPool> thread_pool_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070060
61 DISALLOW_IMPLICIT_CONSTRUCTORS(JitInstrumentationCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062};
63
64class JitInstrumentationListener : public instrumentation::InstrumentationListener {
65 public:
66 explicit JitInstrumentationListener(JitInstrumentationCache* cache);
67
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010068 void MethodEntered(Thread* thread, mirror::Object* /*this_object*/,
69 ArtMethod* method, uint32_t /*dex_pc*/)
Mathieu Chartier90443472015-07-16 20:32:27 -070070 OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071 instrumentation_cache_->AddSamples(thread, method, 1);
72 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010073 void MethodExited(Thread* /*thread*/, mirror::Object* /*this_object*/,
74 ArtMethod* /*method*/, uint32_t /*dex_pc*/,
75 const JValue& /*return_value*/)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 OVERRIDE { }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010077 void MethodUnwind(Thread* /*thread*/, mirror::Object* /*this_object*/,
78 ArtMethod* /*method*/, uint32_t /*dex_pc*/) OVERRIDE { }
79 void FieldRead(Thread* /*thread*/, mirror::Object* /*this_object*/,
80 ArtMethod* /*method*/, uint32_t /*dex_pc*/,
81 ArtField* /*field*/) OVERRIDE { }
82 void FieldWritten(Thread* /*thread*/, mirror::Object* /*this_object*/,
83 ArtMethod* /*method*/, uint32_t /*dex_pc*/,
84 ArtField* /*field*/, const JValue& /*field_value*/)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085 OVERRIDE { }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010086 void ExceptionCaught(Thread* /*thread*/,
87 mirror::Throwable* /*exception_object*/) OVERRIDE { }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080088
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010089 void DexPcMoved(Thread* /*self*/, mirror::Object* /*this_object*/,
90 ArtMethod* /*method*/, uint32_t /*new_dex_pc*/) OVERRIDE { }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010092 void BackwardBranch(Thread* thread, ArtMethod* method, int32_t dex_pc_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -070093 OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080094 CHECK_LE(dex_pc_offset, 0);
95 instrumentation_cache_->AddSamples(thread, method, 1);
96 }
97
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010098 void InvokeVirtualOrInterface(Thread* thread,
99 mirror::Object* this_object,
100 ArtMethod* caller,
101 uint32_t dex_pc,
102 ArtMethod* callee)
103 OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
104
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800105 private:
106 JitInstrumentationCache* const instrumentation_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700107
108 DISALLOW_IMPLICIT_CONSTRUCTORS(JitInstrumentationListener);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800109};
110
111} // namespace jit
112} // namespace art
113
114#endif // ART_RUNTIME_JIT_JIT_INSTRUMENTATION_H_