xref: /aosp_15_r20/art/compiler/optimizing/optimizing_compiler_stats.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 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 #ifndef ART_COMPILER_OPTIMIZING_OPTIMIZING_COMPILER_STATS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_COMPILER_OPTIMIZING_OPTIMIZING_COMPILER_STATS_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <atomic>
21*795d594fSAndroid Build Coastguard Worker #include <iomanip>
22*795d594fSAndroid Build Coastguard Worker #include <string>
23*795d594fSAndroid Build Coastguard Worker #include <type_traits>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include "base/atomic.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker enum class MethodCompilationStat {
34*795d594fSAndroid Build Coastguard Worker   kAttemptBytecodeCompilation = 0,
35*795d594fSAndroid Build Coastguard Worker   kAttemptIntrinsicCompilation,
36*795d594fSAndroid Build Coastguard Worker   kCompiledNativeStub,
37*795d594fSAndroid Build Coastguard Worker   kCompiledIntrinsic,
38*795d594fSAndroid Build Coastguard Worker   kCompiledBytecode,
39*795d594fSAndroid Build Coastguard Worker   kCHAInline,
40*795d594fSAndroid Build Coastguard Worker   kInlinedInvoke,
41*795d594fSAndroid Build Coastguard Worker   kInlinedLastInvoke,
42*795d594fSAndroid Build Coastguard Worker   kReplacedInvokeWithSimplePattern,
43*795d594fSAndroid Build Coastguard Worker   kInstructionSimplifications,
44*795d594fSAndroid Build Coastguard Worker   kInstructionSimplificationsArch,
45*795d594fSAndroid Build Coastguard Worker   kUnresolvedMethod,
46*795d594fSAndroid Build Coastguard Worker   kUnresolvedField,
47*795d594fSAndroid Build Coastguard Worker   kUnresolvedFieldNotAFastAccess,
48*795d594fSAndroid Build Coastguard Worker   kRemovedCheckedCast,
49*795d594fSAndroid Build Coastguard Worker   kRemovedDeadInstruction,
50*795d594fSAndroid Build Coastguard Worker   kRemovedDeadPhi,
51*795d594fSAndroid Build Coastguard Worker   kRemovedTry,
52*795d594fSAndroid Build Coastguard Worker   kRemovedNullCheck,
53*795d594fSAndroid Build Coastguard Worker   kRemovedVolatileLoad,
54*795d594fSAndroid Build Coastguard Worker   kRemovedVolatileStore,
55*795d594fSAndroid Build Coastguard Worker   kRemovedMonitorOp,
56*795d594fSAndroid Build Coastguard Worker   kNotCompiledSkipped,
57*795d594fSAndroid Build Coastguard Worker   kNotCompiledInvalidBytecode,
58*795d594fSAndroid Build Coastguard Worker   kNotCompiledThrowCatchLoop,
59*795d594fSAndroid Build Coastguard Worker   kNotCompiledAmbiguousArrayOp,
60*795d594fSAndroid Build Coastguard Worker   kNotCompiledHugeMethod,
61*795d594fSAndroid Build Coastguard Worker   kNotCompiledLargeMethodNoBranches,
62*795d594fSAndroid Build Coastguard Worker   kNotCompiledMalformedOpcode,
63*795d594fSAndroid Build Coastguard Worker   kNotCompiledNoCodegen,
64*795d594fSAndroid Build Coastguard Worker   kNotCompiledPathological,
65*795d594fSAndroid Build Coastguard Worker   kNotCompiledSpaceFilter,
66*795d594fSAndroid Build Coastguard Worker   kNotCompiledUnhandledInstruction,
67*795d594fSAndroid Build Coastguard Worker   kNotCompiledUnsupportedIsa,
68*795d594fSAndroid Build Coastguard Worker   kNotCompiledInliningIrreducibleLoop,
69*795d594fSAndroid Build Coastguard Worker   kNotCompiledIrreducibleLoopAndStringInit,
70*795d594fSAndroid Build Coastguard Worker   kNotCompiledPhiEquivalentInOsr,
71*795d594fSAndroid Build Coastguard Worker   kNotCompiledFrameTooBig,
72*795d594fSAndroid Build Coastguard Worker   kInlinedMonomorphicCall,
73*795d594fSAndroid Build Coastguard Worker   kInlinedPolymorphicCall,
74*795d594fSAndroid Build Coastguard Worker   kMonomorphicCall,
75*795d594fSAndroid Build Coastguard Worker   kPolymorphicCall,
76*795d594fSAndroid Build Coastguard Worker   kMegamorphicCall,
77*795d594fSAndroid Build Coastguard Worker   kBooleanSimplified,
78*795d594fSAndroid Build Coastguard Worker   kIntrinsicRecognized,
79*795d594fSAndroid Build Coastguard Worker   kLoopInvariantMoved,
80*795d594fSAndroid Build Coastguard Worker   kLoopVectorized,
81*795d594fSAndroid Build Coastguard Worker   kLoopVectorizedIdiom,
82*795d594fSAndroid Build Coastguard Worker   kSelectGenerated,
83*795d594fSAndroid Build Coastguard Worker   kRemovedInstanceOf,
84*795d594fSAndroid Build Coastguard Worker   kPropagatedIfValue,
85*795d594fSAndroid Build Coastguard Worker   kInlinedInvokeVirtualOrInterface,
86*795d594fSAndroid Build Coastguard Worker   kInlinedLastInvokeVirtualOrInterface,
87*795d594fSAndroid Build Coastguard Worker   kImplicitNullCheckGenerated,
88*795d594fSAndroid Build Coastguard Worker   kExplicitNullCheckGenerated,
89*795d594fSAndroid Build Coastguard Worker   kSimplifyIf,
90*795d594fSAndroid Build Coastguard Worker   kSimplifyIfAddedPhi,
91*795d594fSAndroid Build Coastguard Worker   kSimplifyThrowingInvoke,
92*795d594fSAndroid Build Coastguard Worker   kInstructionSunk,
93*795d594fSAndroid Build Coastguard Worker   kNotInlinedUnresolvedEntrypoint,
94*795d594fSAndroid Build Coastguard Worker   kNotInlinedBss,
95*795d594fSAndroid Build Coastguard Worker   kNotInlinedDexCacheInaccessibleToCaller,
96*795d594fSAndroid Build Coastguard Worker   kNotInlinedDexCacheClinitCheck,
97*795d594fSAndroid Build Coastguard Worker   kNotInlinedStackMaps,
98*795d594fSAndroid Build Coastguard Worker   kNotInlinedEnvironmentBudget,
99*795d594fSAndroid Build Coastguard Worker   kNotInlinedInstructionBudget,
100*795d594fSAndroid Build Coastguard Worker   kNotInlinedLoopWithoutExit,
101*795d594fSAndroid Build Coastguard Worker   kNotInlinedIrreducibleLoopCallee,
102*795d594fSAndroid Build Coastguard Worker   kNotInlinedIrreducibleLoopCaller,
103*795d594fSAndroid Build Coastguard Worker   kNotInlinedAlwaysThrows,
104*795d594fSAndroid Build Coastguard Worker   kNotInlinedInfiniteLoop,
105*795d594fSAndroid Build Coastguard Worker   kNotInlinedTryCatchCallee,
106*795d594fSAndroid Build Coastguard Worker   kNotInlinedTryCatchDisabled,
107*795d594fSAndroid Build Coastguard Worker   kNotInlinedRegisterAllocator,
108*795d594fSAndroid Build Coastguard Worker   kNotInlinedCannotBuild,
109*795d594fSAndroid Build Coastguard Worker   kNotInlinedNeverInlineAnnotation,
110*795d594fSAndroid Build Coastguard Worker   kNotInlinedNotCompilable,
111*795d594fSAndroid Build Coastguard Worker   kNotInlinedNotVerified,
112*795d594fSAndroid Build Coastguard Worker   kNotInlinedCodeItem,
113*795d594fSAndroid Build Coastguard Worker   kNotInlinedEndsWithThrow,
114*795d594fSAndroid Build Coastguard Worker   kNotInlinedWont,
115*795d594fSAndroid Build Coastguard Worker   kNotInlinedRecursiveBudget,
116*795d594fSAndroid Build Coastguard Worker   kNotInlinedPolymorphicRecursiveBudget,
117*795d594fSAndroid Build Coastguard Worker   kNotInlinedProxy,
118*795d594fSAndroid Build Coastguard Worker   kNotInlinedUnresolved,
119*795d594fSAndroid Build Coastguard Worker   kNotInlinedPolymorphic,
120*795d594fSAndroid Build Coastguard Worker   kNotInlinedCustom,
121*795d594fSAndroid Build Coastguard Worker   kNotVarAnalyzedPathological,
122*795d594fSAndroid Build Coastguard Worker   kTryInline,
123*795d594fSAndroid Build Coastguard Worker   kConstructorFenceGeneratedNew,
124*795d594fSAndroid Build Coastguard Worker   kConstructorFenceGeneratedFinal,
125*795d594fSAndroid Build Coastguard Worker   kConstructorFenceRemovedLSE,
126*795d594fSAndroid Build Coastguard Worker   kConstructorFenceRemovedPFRA,
127*795d594fSAndroid Build Coastguard Worker   kConstructorFenceRemovedCFRE,
128*795d594fSAndroid Build Coastguard Worker   kPossibleWriteBarrier,
129*795d594fSAndroid Build Coastguard Worker   kRemovedWriteBarrier,
130*795d594fSAndroid Build Coastguard Worker   kBitstringTypeCheck,
131*795d594fSAndroid Build Coastguard Worker   kJitOutOfMemoryForCommit,
132*795d594fSAndroid Build Coastguard Worker   kFullLSEAllocationRemoved,
133*795d594fSAndroid Build Coastguard Worker   kFullLSEPossible,
134*795d594fSAndroid Build Coastguard Worker   kNonPartialLoadRemoved,
135*795d594fSAndroid Build Coastguard Worker   kPartialLSEPossible,
136*795d594fSAndroid Build Coastguard Worker   kPartialStoreRemoved,
137*795d594fSAndroid Build Coastguard Worker   kPartialAllocationMoved,
138*795d594fSAndroid Build Coastguard Worker   kDevirtualized,
139*795d594fSAndroid Build Coastguard Worker   kLastStat
140*795d594fSAndroid Build Coastguard Worker };
141*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, MethodCompilationStat rhs);
142*795d594fSAndroid Build Coastguard Worker 
143*795d594fSAndroid Build Coastguard Worker class OptimizingCompilerStats {
144*795d594fSAndroid Build Coastguard Worker  public:
OptimizingCompilerStats()145*795d594fSAndroid Build Coastguard Worker   OptimizingCompilerStats() {
146*795d594fSAndroid Build Coastguard Worker     // The std::atomic<> default constructor leaves values uninitialized, so initialize them now.
147*795d594fSAndroid Build Coastguard Worker     Reset();
148*795d594fSAndroid Build Coastguard Worker   }
149*795d594fSAndroid Build Coastguard Worker 
150*795d594fSAndroid Build Coastguard Worker   void RecordStat(MethodCompilationStat stat, uint32_t count = 1) {
151*795d594fSAndroid Build Coastguard Worker     size_t stat_index = static_cast<size_t>(stat);
152*795d594fSAndroid Build Coastguard Worker     DCHECK_LT(stat_index, arraysize(compile_stats_));
153*795d594fSAndroid Build Coastguard Worker     compile_stats_[stat_index] += count;
154*795d594fSAndroid Build Coastguard Worker   }
155*795d594fSAndroid Build Coastguard Worker 
GetStat(MethodCompilationStat stat)156*795d594fSAndroid Build Coastguard Worker   uint32_t GetStat(MethodCompilationStat stat) const {
157*795d594fSAndroid Build Coastguard Worker     size_t stat_index = static_cast<size_t>(stat);
158*795d594fSAndroid Build Coastguard Worker     DCHECK_LT(stat_index, arraysize(compile_stats_));
159*795d594fSAndroid Build Coastguard Worker     return compile_stats_[stat_index];
160*795d594fSAndroid Build Coastguard Worker   }
161*795d594fSAndroid Build Coastguard Worker 
Log()162*795d594fSAndroid Build Coastguard Worker   void Log() const {
163*795d594fSAndroid Build Coastguard Worker     uint32_t compiled_intrinsics = GetStat(MethodCompilationStat::kCompiledIntrinsic);
164*795d594fSAndroid Build Coastguard Worker     uint32_t compiled_native_stubs = GetStat(MethodCompilationStat::kCompiledNativeStub);
165*795d594fSAndroid Build Coastguard Worker     uint32_t bytecode_attempts =
166*795d594fSAndroid Build Coastguard Worker         GetStat(MethodCompilationStat::kAttemptBytecodeCompilation);
167*795d594fSAndroid Build Coastguard Worker     if (compiled_intrinsics == 0u && compiled_native_stubs == 0u && bytecode_attempts == 0u) {
168*795d594fSAndroid Build Coastguard Worker       LOG(INFO) << "Did not compile any method.";
169*795d594fSAndroid Build Coastguard Worker     } else {
170*795d594fSAndroid Build Coastguard Worker       uint32_t compiled_bytecode_methods =
171*795d594fSAndroid Build Coastguard Worker           GetStat(MethodCompilationStat::kCompiledBytecode);
172*795d594fSAndroid Build Coastguard Worker       // Successful intrinsic compilation preempts other compilation attempts but failed intrinsic
173*795d594fSAndroid Build Coastguard Worker       // compilation shall still count towards bytecode or native stub compilation attempts.
174*795d594fSAndroid Build Coastguard Worker       uint32_t num_compilation_attempts =
175*795d594fSAndroid Build Coastguard Worker           compiled_intrinsics + compiled_native_stubs + bytecode_attempts;
176*795d594fSAndroid Build Coastguard Worker       uint32_t num_successful_compilations =
177*795d594fSAndroid Build Coastguard Worker           compiled_intrinsics + compiled_native_stubs + compiled_bytecode_methods;
178*795d594fSAndroid Build Coastguard Worker       float compiled_percent = num_successful_compilations * 100.0f / num_compilation_attempts;
179*795d594fSAndroid Build Coastguard Worker       LOG(INFO) << "Attempted compilation of "
180*795d594fSAndroid Build Coastguard Worker           << num_compilation_attempts << " methods: " << std::fixed << std::setprecision(2)
181*795d594fSAndroid Build Coastguard Worker           << compiled_percent << "% (" << num_successful_compilations << ") compiled.";
182*795d594fSAndroid Build Coastguard Worker 
183*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0; i < arraysize(compile_stats_); ++i) {
184*795d594fSAndroid Build Coastguard Worker         if (compile_stats_[i] != 0) {
185*795d594fSAndroid Build Coastguard Worker           LOG(INFO) << "OptStat#" << static_cast<MethodCompilationStat>(i) << ": "
186*795d594fSAndroid Build Coastguard Worker               << compile_stats_[i];
187*795d594fSAndroid Build Coastguard Worker         }
188*795d594fSAndroid Build Coastguard Worker       }
189*795d594fSAndroid Build Coastguard Worker     }
190*795d594fSAndroid Build Coastguard Worker   }
191*795d594fSAndroid Build Coastguard Worker 
AddTo(OptimizingCompilerStats * other_stats)192*795d594fSAndroid Build Coastguard Worker   void AddTo(OptimizingCompilerStats* other_stats) {
193*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0; i != arraysize(compile_stats_); ++i) {
194*795d594fSAndroid Build Coastguard Worker       uint32_t count = compile_stats_[i];
195*795d594fSAndroid Build Coastguard Worker       if (count != 0) {
196*795d594fSAndroid Build Coastguard Worker         other_stats->RecordStat(static_cast<MethodCompilationStat>(i), count);
197*795d594fSAndroid Build Coastguard Worker       }
198*795d594fSAndroid Build Coastguard Worker     }
199*795d594fSAndroid Build Coastguard Worker   }
200*795d594fSAndroid Build Coastguard Worker 
Reset()201*795d594fSAndroid Build Coastguard Worker   void Reset() {
202*795d594fSAndroid Build Coastguard Worker     for (std::atomic<uint32_t>& stat : compile_stats_) {
203*795d594fSAndroid Build Coastguard Worker       stat = 0u;
204*795d594fSAndroid Build Coastguard Worker     }
205*795d594fSAndroid Build Coastguard Worker   }
206*795d594fSAndroid Build Coastguard Worker 
207*795d594fSAndroid Build Coastguard Worker  private:
208*795d594fSAndroid Build Coastguard Worker   std::atomic<uint32_t> compile_stats_[static_cast<size_t>(MethodCompilationStat::kLastStat)];
209*795d594fSAndroid Build Coastguard Worker 
210*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(OptimizingCompilerStats);
211*795d594fSAndroid Build Coastguard Worker };
212*795d594fSAndroid Build Coastguard Worker 
213*795d594fSAndroid Build Coastguard Worker inline void MaybeRecordStat(OptimizingCompilerStats* compiler_stats,
214*795d594fSAndroid Build Coastguard Worker                             MethodCompilationStat stat,
215*795d594fSAndroid Build Coastguard Worker                             uint32_t count = 1) {
216*795d594fSAndroid Build Coastguard Worker   if (compiler_stats != nullptr) {
217*795d594fSAndroid Build Coastguard Worker     compiler_stats->RecordStat(stat, count);
218*795d594fSAndroid Build Coastguard Worker   }
219*795d594fSAndroid Build Coastguard Worker }
220*795d594fSAndroid Build Coastguard Worker 
221*795d594fSAndroid Build Coastguard Worker }  // namespace art
222*795d594fSAndroid Build Coastguard Worker 
223*795d594fSAndroid Build Coastguard Worker #endif  // ART_COMPILER_OPTIMIZING_OPTIMIZING_COMPILER_STATS_H_
224