xref: /aosp_15_r20/art/runtime/jit/jit_options.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright 2024 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "jit_options.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "runtime_options.h"
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
22*795d594fSAndroid Build Coastguard Worker namespace jit {
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker // Maximum permitted threshold value.
25*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kJitMaxThreshold = std::numeric_limits<uint16_t>::max();
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kJitDefaultOptimizeThreshold = 0xffff;
28*795d594fSAndroid Build Coastguard Worker // Different optimization threshold constants. These default to the equivalent optimization
29*795d594fSAndroid Build Coastguard Worker // thresholds divided by 2, but can be overridden at the command-line.
30*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kJitStressDefaultOptimizeThreshold = kJitDefaultOptimizeThreshold / 2;
31*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kJitSlowStressDefaultOptimizeThreshold =
32*795d594fSAndroid Build Coastguard Worker     kJitStressDefaultOptimizeThreshold / 2;
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kJitDefaultWarmupThreshold = 0xffff;
35*795d594fSAndroid Build Coastguard Worker // Different warm-up threshold constants. These default to the equivalent warmup thresholds divided
36*795d594fSAndroid Build Coastguard Worker // by 2, but can be overridden at the command-line.
37*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kJitStressDefaultWarmupThreshold = kJitDefaultWarmupThreshold / 2;
38*795d594fSAndroid Build Coastguard Worker static constexpr uint32_t kJitSlowStressDefaultWarmupThreshold =
39*795d594fSAndroid Build Coastguard Worker     kJitStressDefaultWarmupThreshold / 2;
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
42*795d594fSAndroid Build Coastguard Worker static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker DEFINE_RUNTIME_DEBUG_FLAG(JitOptions, kSlowMode);
45*795d594fSAndroid Build Coastguard Worker 
CreateFromRuntimeArguments(const RuntimeArgumentMap & options)46*795d594fSAndroid Build Coastguard Worker JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
47*795d594fSAndroid Build Coastguard Worker   auto* jit_options = new JitOptions;
48*795d594fSAndroid Build Coastguard Worker   jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
49*795d594fSAndroid Build Coastguard Worker   jit_options->use_profiled_jit_compilation_ =
50*795d594fSAndroid Build Coastguard Worker       options.GetOrDefault(RuntimeArgumentMap::UseProfiledJitCompilation);
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   jit_options->code_cache_initial_capacity_ =
53*795d594fSAndroid Build Coastguard Worker       options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
54*795d594fSAndroid Build Coastguard Worker   jit_options->code_cache_max_capacity_ =
55*795d594fSAndroid Build Coastguard Worker       options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
56*795d594fSAndroid Build Coastguard Worker   jit_options->dump_info_on_shutdown_ =
57*795d594fSAndroid Build Coastguard Worker       options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
58*795d594fSAndroid Build Coastguard Worker   jit_options->profile_saver_options_ =
59*795d594fSAndroid Build Coastguard Worker       options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
60*795d594fSAndroid Build Coastguard Worker   jit_options->thread_pool_pthread_priority_ =
61*795d594fSAndroid Build Coastguard Worker       options.GetOrDefault(RuntimeArgumentMap::JITPoolThreadPthreadPriority);
62*795d594fSAndroid Build Coastguard Worker   jit_options->zygote_thread_pool_pthread_priority_ =
63*795d594fSAndroid Build Coastguard Worker       options.GetOrDefault(RuntimeArgumentMap::JITZygotePoolThreadPthreadPriority);
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   // Set default optimize threshold to aid with checking defaults.
66*795d594fSAndroid Build Coastguard Worker   jit_options->optimize_threshold_ = kIsDebugBuild
67*795d594fSAndroid Build Coastguard Worker       ? (kSlowMode ? kJitSlowStressDefaultOptimizeThreshold : kJitStressDefaultOptimizeThreshold)
68*795d594fSAndroid Build Coastguard Worker       : kJitDefaultOptimizeThreshold;
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker   // Set default warm-up threshold to aid with checking defaults.
71*795d594fSAndroid Build Coastguard Worker   jit_options->warmup_threshold_ = kIsDebugBuild
72*795d594fSAndroid Build Coastguard Worker       ? (kSlowMode ? kJitSlowStressDefaultWarmupThreshold : kJitStressDefaultWarmupThreshold)
73*795d594fSAndroid Build Coastguard Worker       : kJitDefaultWarmupThreshold;
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker   if (options.Exists(RuntimeArgumentMap::JITOptimizeThreshold)) {
76*795d594fSAndroid Build Coastguard Worker     jit_options->optimize_threshold_ = *options.Get(RuntimeArgumentMap::JITOptimizeThreshold);
77*795d594fSAndroid Build Coastguard Worker   }
78*795d594fSAndroid Build Coastguard Worker   DCHECK_LE(jit_options->optimize_threshold_, kJitMaxThreshold);
79*795d594fSAndroid Build Coastguard Worker 
80*795d594fSAndroid Build Coastguard Worker   if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
81*795d594fSAndroid Build Coastguard Worker     jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
82*795d594fSAndroid Build Coastguard Worker   }
83*795d594fSAndroid Build Coastguard Worker   DCHECK_LE(jit_options->warmup_threshold_, kJitMaxThreshold);
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
86*795d594fSAndroid Build Coastguard Worker     jit_options->priority_thread_weight_ =
87*795d594fSAndroid Build Coastguard Worker         *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
88*795d594fSAndroid Build Coastguard Worker     if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
89*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
90*795d594fSAndroid Build Coastguard Worker     } else if (jit_options->priority_thread_weight_ == 0) {
91*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Priority thread weight cannot be 0.";
92*795d594fSAndroid Build Coastguard Worker     }
93*795d594fSAndroid Build Coastguard Worker   } else {
94*795d594fSAndroid Build Coastguard Worker     jit_options->priority_thread_weight_ = std::max(
95*795d594fSAndroid Build Coastguard Worker         jit_options->warmup_threshold_ / kDefaultPriorityThreadWeightRatio,
96*795d594fSAndroid Build Coastguard Worker         static_cast<size_t>(1));
97*795d594fSAndroid Build Coastguard Worker   }
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker   if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
100*795d594fSAndroid Build Coastguard Worker     jit_options->invoke_transition_weight_ =
101*795d594fSAndroid Build Coastguard Worker         *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
102*795d594fSAndroid Build Coastguard Worker     if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
103*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
104*795d594fSAndroid Build Coastguard Worker     } else if (jit_options->invoke_transition_weight_  == 0) {
105*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Invoke transition weight cannot be 0.";
106*795d594fSAndroid Build Coastguard Worker     }
107*795d594fSAndroid Build Coastguard Worker   } else {
108*795d594fSAndroid Build Coastguard Worker     jit_options->invoke_transition_weight_ = std::max(
109*795d594fSAndroid Build Coastguard Worker         jit_options->warmup_threshold_ / kDefaultInvokeTransitionWeightRatio,
110*795d594fSAndroid Build Coastguard Worker         static_cast<size_t>(1));
111*795d594fSAndroid Build Coastguard Worker   }
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker   return jit_options;
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker }  // namespace jit
117*795d594fSAndroid Build Coastguard Worker }  // namespace art
118