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