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