jit.h revision 0462c4c87c39db6cfcd338f323844738109ac3c9
1/*
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_H_
18#define ART_RUNTIME_JIT_JIT_H_
19
20#include <unordered_map>
21
22#include "atomic.h"
23#include "base/macros.h"
24#include "base/mutex.h"
25#include "base/timing_logger.h"
26#include "gc_root.h"
27#include "jni.h"
28#include "object_callbacks.h"
29#include "thread_pool.h"
30
31namespace art {
32
33class CompilerCallbacks;
34struct RuntimeArgumentMap;
35
36namespace jit {
37
38class JitCodeCache;
39class JitInstrumentationCache;
40class JitOptions;
41
42class Jit {
43 public:
44  static constexpr bool kStressMode = kIsDebugBuild;
45  static constexpr size_t kDefaultCompileThreshold = kStressMode ? 1 : 1000;
46
47  virtual ~Jit();
48  static Jit* Create(JitOptions* options, std::string* error_msg);
49  bool CompileMethod(mirror::ArtMethod* method, Thread* self)
50      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
51  void CreateInstrumentationCache(size_t compile_threshold);
52  void CreateThreadPool();
53  CompilerCallbacks* GetCompilerCallbacks() {
54    return compiler_callbacks_;
55  }
56  const JitCodeCache* GetCodeCache() const {
57    return code_cache_.get();
58  }
59  JitCodeCache* GetCodeCache() {
60    return code_cache_.get();
61  }
62  void DeleteThreadPool();
63  // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
64  // loggers.
65  void DumpInfo(std::ostream& os);
66  // Add a timing logger to cumulative_timings_.
67  void AddTimingLogger(const TimingLogger& logger);
68
69 private:
70  Jit();
71  bool LoadCompiler(std::string* error_msg);
72
73  // JIT compiler
74  void* jit_library_handle_;
75  void* jit_compiler_handle_;
76  void* (*jit_load_)(CompilerCallbacks**);
77  void (*jit_unload_)(void*);
78  bool (*jit_compile_method_)(void*, mirror::ArtMethod*, Thread*);
79
80  // Performance monitoring.
81  bool dump_info_on_shutdown_;
82  CumulativeLogger cumulative_timings_;
83
84  std::unique_ptr<jit::JitInstrumentationCache> instrumentation_cache_;
85  std::unique_ptr<jit::JitCodeCache> code_cache_;
86  CompilerCallbacks* compiler_callbacks_;  // Owned by the jit compiler.
87
88  DISALLOW_COPY_AND_ASSIGN(Jit);
89};
90
91class JitOptions {
92 public:
93  static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
94  size_t GetCompileThreshold() const {
95    return compile_threshold_;
96  }
97  size_t GetCodeCacheCapacity() const {
98    return code_cache_capacity_;
99  }
100  bool DumpJitInfoOnShutdown() const {
101    return dump_info_on_shutdown_;
102  }
103  bool UseJIT() const {
104    return use_jit_;
105  }
106  void SetUseJIT(bool b) {
107    use_jit_ = b;
108  }
109
110 private:
111  bool use_jit_;
112  size_t code_cache_capacity_;
113  size_t compile_threshold_;
114  bool dump_info_on_shutdown_;
115
116  JitOptions() : use_jit_(false), code_cache_capacity_(0), compile_threshold_(0),
117      dump_info_on_shutdown_(false) { }
118
119  DISALLOW_COPY_AND_ASSIGN(JitOptions);
120};
121
122}  // namespace jit
123}  // namespace art
124
125#endif  // ART_RUNTIME_JIT_JIT_H_
126