Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "dexanalyze_experiments.h" |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 18 | |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 19 | #include <algorithm> |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <iostream> |
| 23 | #include <map> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "android-base/stringprintf.h" |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 27 | #include "dex/class_accessor-inl.h" |
Mathieu Chartier | 05dc23e | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 28 | #include "dex/class_iterator.h" |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 29 | #include "dex/code_item_accessors-inl.h" |
| 30 | #include "dex/dex_instruction-inl.h" |
| 31 | #include "dex/standard_dex_file.h" |
Mathieu Chartier | f275979 | 2018-05-18 13:16:54 -0700 | [diff] [blame] | 32 | #include "dex/utf-inl.h" |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 36 | static inline bool IsRange(Instruction::Code code) { |
| 37 | return code == Instruction::INVOKE_VIRTUAL_RANGE || |
| 38 | code == Instruction::INVOKE_DIRECT_RANGE || |
| 39 | code == Instruction::INVOKE_SUPER_RANGE || |
| 40 | code == Instruction::INVOKE_STATIC_RANGE || |
| 41 | code == Instruction::INVOKE_INTERFACE_RANGE; |
| 42 | } |
| 43 | |
| 44 | static inline uint16_t NumberOfArgs(const Instruction& inst) { |
| 45 | return IsRange(inst.Opcode()) ? inst.VRegA_3rc() : inst.VRegA_35c(); |
| 46 | } |
| 47 | |
| 48 | static inline uint16_t DexMethodIndex(const Instruction& inst) { |
| 49 | return IsRange(inst.Opcode()) ? inst.VRegB_3rc() : inst.VRegB_35c(); |
| 50 | } |
| 51 | |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 52 | std::string Percent(uint64_t value, uint64_t max) { |
| 53 | if (max == 0) { |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 54 | return "0"; |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 55 | } |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 56 | return android::base::StringPrintf( |
| 57 | "%" PRId64 "(%.2f%%)", |
| 58 | value, |
| 59 | static_cast<double>(value * 100) / static_cast<double>(max)); |
| 60 | } |
| 61 | |
| 62 | std::string PercentDivide(uint64_t value, uint64_t max) { |
| 63 | if (max == 0) { |
| 64 | return "0"; |
| 65 | } |
| 66 | return android::base::StringPrintf( |
| 67 | "%" PRId64 "/%" PRId64 "(%.2f%%)", |
| 68 | value, |
| 69 | max, |
| 70 | static_cast<double>(value * 100) / static_cast<double>(max)); |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static size_t PrefixLen(const std::string& a, const std::string& b) { |
| 74 | size_t len = 0; |
| 75 | for (; len < a.length() && len < b.length() && a[len] == b[len]; ++len) {} |
| 76 | return len; |
| 77 | } |
| 78 | |
Mathieu Chartier | 48de723 | 2018-06-01 17:30:29 -0700 | [diff] [blame] | 79 | void Experiment::ProcessDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
| 80 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 81 | ProcessDexFile(*dex_file); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void AnalyzeDebugInfo::ProcessDexFiles( |
| 86 | const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
Mathieu Chartier | 31380c7 | 2018-05-31 18:12:27 -0700 | [diff] [blame] | 87 | std::set<const uint8_t*> seen; |
| 88 | std::vector<size_t> counts(256, 0u); |
| 89 | std::vector<size_t> opcode_counts(256, 0u); |
| 90 | std::set<std::vector<uint8_t>> unique_non_header; |
Mathieu Chartier | 48de723 | 2018-06-01 17:30:29 -0700 | [diff] [blame] | 91 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 92 | for (ClassAccessor accessor : dex_file->GetClasses()) { |
| 93 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
| 94 | CodeItemDebugInfoAccessor code_item(*dex_file, method.GetCodeItem(), method.GetIndex()); |
| 95 | const uint8_t* debug_info = dex_file->GetDebugInfoStream(code_item.DebugInfoOffset()); |
| 96 | if (debug_info != nullptr && seen.insert(debug_info).second) { |
| 97 | const uint8_t* stream = debug_info; |
| 98 | DecodeUnsignedLeb128(&stream); // line_start |
| 99 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 100 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 101 | DecodeUnsignedLeb128P1(&stream); // Parameter name. |
| 102 | } |
| 103 | bool done = false; |
| 104 | const uint8_t* after_header_start = stream; |
| 105 | while (!done) { |
| 106 | const uint8_t* const op_start = stream; |
| 107 | uint8_t opcode = *stream++; |
| 108 | ++opcode_counts[opcode]; |
| 109 | ++total_opcode_bytes_; |
| 110 | switch (opcode) { |
| 111 | case DexFile::DBG_END_SEQUENCE: |
| 112 | ++total_end_seq_bytes_; |
| 113 | done = true; |
| 114 | break; |
| 115 | case DexFile::DBG_ADVANCE_PC: |
| 116 | DecodeUnsignedLeb128(&stream); // addr_diff |
| 117 | total_advance_pc_bytes_ += stream - op_start; |
| 118 | break; |
| 119 | case DexFile::DBG_ADVANCE_LINE: |
| 120 | DecodeSignedLeb128(&stream); // line_diff |
| 121 | total_advance_line_bytes_ += stream - op_start; |
| 122 | break; |
| 123 | case DexFile::DBG_START_LOCAL: |
| 124 | DecodeUnsignedLeb128(&stream); // register_num |
| 125 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 126 | DecodeUnsignedLeb128P1(&stream); // type_idx |
| 127 | total_start_local_bytes_ += stream - op_start; |
| 128 | break; |
| 129 | case DexFile::DBG_START_LOCAL_EXTENDED: |
| 130 | DecodeUnsignedLeb128(&stream); // register_num |
| 131 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 132 | DecodeUnsignedLeb128P1(&stream); // type_idx |
| 133 | DecodeUnsignedLeb128P1(&stream); // sig_idx |
| 134 | total_start_local_extended_bytes_ += stream - op_start; |
| 135 | break; |
| 136 | case DexFile::DBG_END_LOCAL: |
| 137 | DecodeUnsignedLeb128(&stream); // register_num |
| 138 | total_end_local_bytes_ += stream - op_start; |
| 139 | break; |
| 140 | case DexFile::DBG_RESTART_LOCAL: |
| 141 | DecodeUnsignedLeb128(&stream); // register_num |
| 142 | total_restart_local_bytes_ += stream - op_start; |
| 143 | break; |
| 144 | case DexFile::DBG_SET_PROLOGUE_END: |
| 145 | case DexFile::DBG_SET_EPILOGUE_BEGIN: |
| 146 | total_epilogue_bytes_ += stream - op_start; |
| 147 | break; |
| 148 | case DexFile::DBG_SET_FILE: { |
| 149 | DecodeUnsignedLeb128P1(&stream); // name_idx |
| 150 | total_set_file_bytes_ += stream - op_start; |
| 151 | break; |
| 152 | } |
| 153 | default: { |
| 154 | total_other_bytes_ += stream - op_start; |
| 155 | break; |
| 156 | } |
Mathieu Chartier | 31380c7 | 2018-05-31 18:12:27 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
Mathieu Chartier | 48de723 | 2018-06-01 17:30:29 -0700 | [diff] [blame] | 159 | const size_t bytes = stream - debug_info; |
| 160 | total_bytes_ += bytes; |
| 161 | total_non_header_bytes_ += stream - after_header_start; |
| 162 | if (unique_non_header.insert(std::vector<uint8_t>(after_header_start, stream)).second) { |
| 163 | total_unique_non_header_bytes_ += stream - after_header_start; |
| 164 | } |
| 165 | for (size_t i = 0; i < bytes; ++i) { |
| 166 | ++counts[debug_info[i]]; |
| 167 | } |
Mathieu Chartier | 31380c7 | 2018-05-31 18:12:27 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | auto calc_entropy = [](std::vector<size_t> data) { |
| 173 | size_t total = std::accumulate(data.begin(), data.end(), 0u); |
| 174 | double avg_entropy = 0.0; |
| 175 | for (size_t c : data) { |
| 176 | if (c > 0) { |
| 177 | double ratio = static_cast<double>(c) / static_cast<double>(total); |
| 178 | avg_entropy -= ratio * log(ratio) / log(256.0); |
| 179 | } |
| 180 | } |
| 181 | return avg_entropy * total; |
| 182 | }; |
| 183 | total_entropy_ += calc_entropy(counts); |
| 184 | total_opcode_entropy_ += calc_entropy(opcode_counts); |
| 185 | } |
| 186 | |
| 187 | void AnalyzeDebugInfo::Dump(std::ostream& os, uint64_t total_size) const { |
| 188 | os << "Debug info bytes " << Percent(total_bytes_, total_size) << "\n"; |
| 189 | |
| 190 | os << " DBG_END_SEQUENCE: " << Percent(total_end_seq_bytes_, total_size) << "\n"; |
| 191 | os << " DBG_ADVANCE_PC: " << Percent(total_advance_pc_bytes_, total_size) << "\n"; |
| 192 | os << " DBG_ADVANCE_LINE: " << Percent(total_advance_line_bytes_, total_size) << "\n"; |
| 193 | os << " DBG_START_LOCAL: " << Percent(total_start_local_bytes_, total_size) << "\n"; |
| 194 | os << " DBG_START_LOCAL_EXTENDED: " |
| 195 | << Percent(total_start_local_extended_bytes_, total_size) << "\n"; |
| 196 | os << " DBG_END_LOCAL: " << Percent(total_end_local_bytes_, total_size) << "\n"; |
| 197 | os << " DBG_RESTART_LOCAL: " << Percent(total_restart_local_bytes_, total_size) << "\n"; |
| 198 | os << " DBG_SET_PROLOGUE bytes " << Percent(total_epilogue_bytes_, total_size) << "\n"; |
| 199 | os << " DBG_SET_FILE bytes " << Percent(total_set_file_bytes_, total_size) << "\n"; |
| 200 | os << " special: " |
| 201 | << Percent(total_other_bytes_, total_size) << "\n"; |
| 202 | os << "Debug info entropy " << Percent(total_entropy_, total_size) << "\n"; |
| 203 | os << "Debug info opcode bytes " << Percent(total_opcode_bytes_, total_size) << "\n"; |
| 204 | os << "Debug info opcode entropy " << Percent(total_opcode_entropy_, total_size) << "\n"; |
| 205 | os << "Debug info non header bytes " << Percent(total_non_header_bytes_, total_size) << "\n"; |
| 206 | os << "Debug info deduped non header bytes " |
| 207 | << Percent(total_unique_non_header_bytes_, total_size) << "\n"; |
| 208 | } |
| 209 | |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 210 | void AnalyzeStrings::ProcessDexFile(const DexFile& dex_file) { |
| 211 | std::vector<std::string> strings; |
| 212 | for (size_t i = 0; i < dex_file.NumStringIds(); ++i) { |
| 213 | uint32_t length = 0; |
Mathieu Chartier | f275979 | 2018-05-18 13:16:54 -0700 | [diff] [blame] | 214 | const char* data = dex_file.StringDataAndUtf16LengthByIdx(dex::StringIndex(i), &length); |
| 215 | // Analyze if the string has any UTF16 chars. |
| 216 | bool have_wide_char = false; |
| 217 | const char* ptr = data; |
| 218 | for (size_t j = 0; j < length; ++j) { |
| 219 | have_wide_char = have_wide_char || GetUtf16FromUtf8(&ptr) >= 0x100; |
| 220 | } |
| 221 | if (have_wide_char) { |
| 222 | wide_string_bytes_ += 2 * length; |
| 223 | } else { |
| 224 | ascii_string_bytes_ += length; |
| 225 | } |
| 226 | string_data_bytes_ += ptr - data; |
| 227 | |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 228 | strings.push_back(data); |
| 229 | } |
| 230 | // Note that the strings are probably already sorted. |
| 231 | std::sort(strings.begin(), strings.end()); |
| 232 | |
| 233 | // Tunable parameters. |
| 234 | static const size_t kMinPrefixLen = 3; |
| 235 | static const size_t kPrefixConstantCost = 5; |
| 236 | static const size_t kPrefixIndexCost = 2; |
| 237 | |
| 238 | // Calculate total shared prefix. |
| 239 | std::vector<size_t> shared_len; |
| 240 | std::set<std::string> prefixes; |
| 241 | for (size_t i = 0; i < strings.size(); ++i) { |
| 242 | size_t best_len = 0; |
| 243 | if (i > 0) { |
| 244 | best_len = std::max(best_len, PrefixLen(strings[i], strings[i - 1])); |
| 245 | } |
| 246 | if (i < strings.size() - 1) { |
| 247 | best_len = std::max(best_len, PrefixLen(strings[i], strings[i + 1])); |
| 248 | } |
| 249 | std::string prefix; |
| 250 | if (best_len >= kMinPrefixLen) { |
| 251 | prefix = strings[i].substr(0, best_len); |
| 252 | prefixes.insert(prefix); |
| 253 | total_prefix_savings_ += prefix.length(); |
| 254 | } |
| 255 | total_prefix_index_cost_ += kPrefixIndexCost; |
| 256 | } |
| 257 | total_num_prefixes_ += prefixes.size(); |
| 258 | for (const std::string& s : prefixes) { |
| 259 | // 4 bytes for an offset, one for length. |
| 260 | total_prefix_dict_ += s.length(); |
| 261 | total_prefix_table_ += kPrefixConstantCost; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void AnalyzeStrings::Dump(std::ostream& os, uint64_t total_size) const { |
Mathieu Chartier | f275979 | 2018-05-18 13:16:54 -0700 | [diff] [blame] | 266 | os << "Total string data bytes " << Percent(string_data_bytes_, total_size) << "\n"; |
| 267 | os << "UTF-16 string data bytes " << Percent(wide_string_bytes_, total_size) << "\n"; |
| 268 | os << "ASCII string data bytes " << Percent(ascii_string_bytes_, total_size) << "\n"; |
| 269 | |
| 270 | // Prefix based strings. |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 271 | os << "Total shared prefix bytes " << Percent(total_prefix_savings_, total_size) << "\n"; |
| 272 | os << "Prefix dictionary cost " << Percent(total_prefix_dict_, total_size) << "\n"; |
| 273 | os << "Prefix table cost " << Percent(total_prefix_table_, total_size) << "\n"; |
| 274 | os << "Prefix index cost " << Percent(total_prefix_index_cost_, total_size) << "\n"; |
| 275 | int64_t net_savings = total_prefix_savings_; |
| 276 | net_savings -= total_prefix_dict_; |
| 277 | net_savings -= total_prefix_table_; |
| 278 | net_savings -= total_prefix_index_cost_; |
| 279 | os << "Prefix net savings " << Percent(net_savings, total_size) << "\n"; |
| 280 | os << "Prefix dictionary elements " << total_num_prefixes_ << "\n"; |
| 281 | } |
| 282 | |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 283 | void CountDexIndices::ProcessDexFile(const DexFile& dex_file) { |
| 284 | num_string_ids_ += dex_file.NumStringIds(); |
| 285 | num_method_ids_ += dex_file.NumMethodIds(); |
| 286 | num_field_ids_ += dex_file.NumFieldIds(); |
| 287 | num_type_ids_ += dex_file.NumTypeIds(); |
| 288 | num_class_defs_ += dex_file.NumClassDefs(); |
Mathieu Chartier | 7c3a8c1 | 2018-05-23 18:09:45 -0700 | [diff] [blame] | 289 | std::set<size_t> unique_code_items; |
Mathieu Chartier | 05dc23e | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 290 | for (ClassAccessor accessor : dex_file.GetClasses()) { |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 291 | std::set<size_t> unique_method_ids; |
| 292 | std::set<size_t> unique_string_ids; |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 293 | // Types accessed and count. |
| 294 | std::map<size_t, size_t> types_accessed; |
| 295 | |
| 296 | // Map from dex field index -> class field index. |
| 297 | std::map<uint32_t, uint32_t> field_index_map_; |
| 298 | size_t current_idx = 0u; |
| 299 | for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) { |
| 300 | field_index_map_[field.GetIndex()] = current_idx++; |
| 301 | } |
| 302 | |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 303 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 304 | CodeItemDataAccessor code_item(dex_file, method.GetCodeItem()); |
| 305 | dex_code_bytes_ += code_item.InsnsSizeInBytes(); |
Mathieu Chartier | 7c3a8c1 | 2018-05-23 18:09:45 -0700 | [diff] [blame] | 306 | unique_code_items.insert(method.GetCodeItemOffset()); |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 307 | for (const DexInstructionPcPair& inst : code_item) { |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 308 | switch (inst->Opcode()) { |
| 309 | case Instruction::CONST_STRING: { |
| 310 | const dex::StringIndex string_index(inst->VRegB_21c()); |
| 311 | unique_string_ids.insert(string_index.index_); |
| 312 | ++num_string_ids_from_code_; |
| 313 | break; |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 314 | } |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 315 | case Instruction::IGET: |
| 316 | case Instruction::IGET_WIDE: |
| 317 | case Instruction::IGET_OBJECT: |
| 318 | case Instruction::IGET_BOOLEAN: |
| 319 | case Instruction::IGET_BYTE: |
| 320 | case Instruction::IGET_CHAR: |
| 321 | case Instruction::IGET_SHORT: |
| 322 | case Instruction::IPUT: |
| 323 | case Instruction::IPUT_WIDE: |
| 324 | case Instruction::IPUT_OBJECT: |
| 325 | case Instruction::IPUT_BOOLEAN: |
| 326 | case Instruction::IPUT_BYTE: |
| 327 | case Instruction::IPUT_CHAR: |
| 328 | case Instruction::IPUT_SHORT: { |
| 329 | const uint32_t receiver = inst->VRegB_22c(); |
| 330 | const uint32_t dex_field_idx = inst->VRegC_22c(); |
| 331 | const uint32_t first_arg_reg = code_item.RegistersSize() - code_item.InsSize(); |
| 332 | ++field_receiver_[(receiver - first_arg_reg) & 0xF]; |
| 333 | ++types_accessed[dex_file.GetFieldId(dex_field_idx).class_idx_.index_]; |
| 334 | if (first_arg_reg == receiver) { |
| 335 | auto it = field_index_map_.find(dex_field_idx); |
| 336 | if (it != field_index_map_.end() && it->second < kMaxFieldIndex) { |
| 337 | ++field_index_[it->second]; |
| 338 | } else { |
| 339 | ++field_index_other_; |
| 340 | } |
| 341 | } |
| 342 | ++field_output_[inst->VRegA_22c()]; |
| 343 | break; |
| 344 | } |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 345 | case Instruction::CONST_STRING_JUMBO: { |
| 346 | const dex::StringIndex string_index(inst->VRegB_31c()); |
| 347 | unique_string_ids.insert(string_index.index_); |
| 348 | ++num_string_ids_from_code_; |
| 349 | break; |
| 350 | } |
| 351 | // Invoke cases. |
| 352 | case Instruction::INVOKE_VIRTUAL: |
| 353 | case Instruction::INVOKE_VIRTUAL_RANGE: { |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 354 | uint32_t method_idx = DexMethodIndex(inst.Inst()); |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 355 | ++types_accessed[dex_file.GetMethodId(method_idx).class_idx_.index_]; |
Mathieu Chartier | c8c8d5f | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 356 | if (dex_file.GetMethodId(method_idx).class_idx_ == accessor.GetClassIdx()) { |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 357 | ++same_class_virtual_; |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 358 | } |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 359 | ++total_virtual_; |
| 360 | unique_method_ids.insert(method_idx); |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | case Instruction::INVOKE_DIRECT: |
| 364 | case Instruction::INVOKE_DIRECT_RANGE: { |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 365 | uint32_t method_idx = DexMethodIndex(inst.Inst()); |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 366 | ++types_accessed[dex_file.GetMethodId(method_idx).class_idx_.index_]; |
Mathieu Chartier | c8c8d5f | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 367 | if (dex_file.GetMethodId(method_idx).class_idx_ == accessor.GetClassIdx()) { |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 368 | ++same_class_direct_; |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 369 | } |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 370 | ++total_direct_; |
| 371 | unique_method_ids.insert(method_idx); |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | case Instruction::INVOKE_STATIC: |
| 375 | case Instruction::INVOKE_STATIC_RANGE: { |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 376 | uint32_t method_idx = DexMethodIndex(inst.Inst()); |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 377 | ++types_accessed[dex_file.GetMethodId(method_idx).class_idx_.index_]; |
Mathieu Chartier | c8c8d5f | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 378 | if (dex_file.GetMethodId(method_idx).class_idx_ == accessor.GetClassIdx()) { |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 379 | ++same_class_static_; |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 380 | } |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 381 | ++total_static_; |
| 382 | unique_method_ids.insert(method_idx); |
| 383 | break; |
| 384 | } |
| 385 | case Instruction::INVOKE_INTERFACE: |
| 386 | case Instruction::INVOKE_INTERFACE_RANGE: { |
| 387 | uint32_t method_idx = DexMethodIndex(inst.Inst()); |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 388 | ++types_accessed[dex_file.GetMethodId(method_idx).class_idx_.index_]; |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 389 | if (dex_file.GetMethodId(method_idx).class_idx_ == accessor.GetClassIdx()) { |
| 390 | ++same_class_interface_; |
| 391 | } |
| 392 | ++total_interface_; |
| 393 | unique_method_ids.insert(method_idx); |
| 394 | break; |
| 395 | } |
| 396 | case Instruction::INVOKE_SUPER: |
| 397 | case Instruction::INVOKE_SUPER_RANGE: { |
| 398 | uint32_t method_idx = DexMethodIndex(inst.Inst()); |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 399 | ++types_accessed[dex_file.GetMethodId(method_idx).class_idx_.index_]; |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 400 | if (dex_file.GetMethodId(method_idx).class_idx_ == accessor.GetClassIdx()) { |
| 401 | ++same_class_super_; |
| 402 | } |
| 403 | ++total_super_; |
| 404 | unique_method_ids.insert(method_idx); |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 405 | break; |
| 406 | } |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 407 | case Instruction::NEW_ARRAY: { |
| 408 | ++types_accessed[inst->VRegC_22c()]; |
| 409 | break; |
| 410 | } |
| 411 | case Instruction::FILLED_NEW_ARRAY: { |
| 412 | ++types_accessed[inst->VRegB_35c()]; |
| 413 | break; |
| 414 | } |
| 415 | case Instruction::FILLED_NEW_ARRAY_RANGE: { |
| 416 | ++types_accessed[inst->VRegB_3rc()]; |
| 417 | break; |
| 418 | } |
| 419 | case Instruction::CONST_CLASS: |
| 420 | case Instruction::CHECK_CAST: |
| 421 | case Instruction::NEW_INSTANCE: { |
| 422 | ++types_accessed[inst->VRegB_21c()]; |
| 423 | break; |
| 424 | } |
| 425 | case Instruction::INSTANCE_OF: { |
| 426 | ++types_accessed[inst->VRegB_21c()]; |
| 427 | break; |
| 428 | } |
Mathieu Chartier | c2b4db6 | 2018-05-18 13:58:12 -0700 | [diff] [blame] | 429 | default: |
| 430 | break; |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 433 | } |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 434 | // Count uses of top 16n. |
| 435 | std::vector<size_t> uses; |
| 436 | for (auto&& p : types_accessed) { |
| 437 | uses.push_back(p.second); |
| 438 | } |
| 439 | std::sort(uses.rbegin(), uses.rend()); |
| 440 | for (size_t i = 0; i < uses.size(); ++i) { |
| 441 | if (i < 16) { |
| 442 | uses_top_types_ += uses[i]; |
| 443 | } |
| 444 | uses_all_types_ += uses[i]; |
| 445 | } |
| 446 | total_unique_types_ += types_accessed.size(); |
| 447 | total_unique_method_ids_ += unique_method_ids.size(); |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 448 | total_unique_string_ids_ += unique_string_ids.size(); |
| 449 | } |
Mathieu Chartier | 7c3a8c1 | 2018-05-23 18:09:45 -0700 | [diff] [blame] | 450 | total_unique_code_items_ += unique_code_items.size(); |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Mathieu Chartier | 35ddc6f | 2018-05-09 11:34:07 -0700 | [diff] [blame] | 453 | void CountDexIndices::Dump(std::ostream& os, uint64_t total_size) const { |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 454 | const uint64_t fields_total = std::accumulate(field_receiver_, field_receiver_ + 16u, 0u); |
| 455 | for (size_t i = 0; i < 16; ++i) { |
| 456 | os << "receiver_reg=" << i << ": " << Percent(field_receiver_[i], fields_total) << "\n"; |
| 457 | } |
| 458 | for (size_t i = 0; i < 16; ++i) { |
| 459 | os << "output_reg=" << i << ": " << Percent(field_output_[i], fields_total) << "\n"; |
| 460 | } |
| 461 | const uint64_t fields_idx_total = std::accumulate(field_index_, |
| 462 | field_index_ + kMaxFieldIndex, |
| 463 | 0u) + field_index_other_; |
| 464 | for (size_t i = 0; i < kMaxFieldIndex; ++i) { |
| 465 | os << "field_idx=" << i << ": " << Percent(field_index_[i], fields_idx_total) << "\n"; |
| 466 | } |
| 467 | os << "field_idx=other: " << Percent(field_index_other_, fields_idx_total) << "\n"; |
| 468 | os << "field_idx_savings=" << Percent((fields_idx_total - field_index_other_) * 2, total_size) |
| 469 | << "\n"; |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 470 | os << "Num string ids: " << num_string_ids_ << "\n"; |
| 471 | os << "Num method ids: " << num_method_ids_ << "\n"; |
| 472 | os << "Num field ids: " << num_field_ids_ << "\n"; |
| 473 | os << "Num type ids: " << num_type_ids_ << "\n"; |
| 474 | os << "Num class defs: " << num_class_defs_ << "\n"; |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 475 | os << "Direct same class: " << PercentDivide(same_class_direct_, total_direct_) << "\n"; |
| 476 | os << "Virtual same class: " << PercentDivide(same_class_virtual_, total_virtual_) << "\n"; |
| 477 | os << "Static same class: " << PercentDivide(same_class_static_, total_static_) << "\n"; |
| 478 | os << "Interface same class: " << PercentDivide(same_class_interface_, total_interface_) << "\n"; |
| 479 | os << "Super same class: " << PercentDivide(same_class_super_, total_super_) << "\n"; |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 480 | os << "Num strings accessed from code: " << num_string_ids_from_code_ << "\n"; |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 481 | os << "Avg unique methods accessed per class: " |
| 482 | << double(total_unique_method_ids_) / double(num_class_defs_) << "\n"; |
| 483 | os << "Avg unique strings accessed per class: " |
| 484 | << double(total_unique_string_ids_) / double(num_class_defs_) << "\n"; |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 485 | const size_t same_class_total = |
| 486 | same_class_direct_ + |
| 487 | same_class_virtual_ + |
| 488 | same_class_static_ + |
| 489 | same_class_interface_ + |
| 490 | same_class_super_; |
| 491 | const size_t other_class_total = |
| 492 | total_direct_ + |
| 493 | total_virtual_ + |
| 494 | total_static_ + |
| 495 | total_interface_ + |
| 496 | total_super_; |
| 497 | os << "Same class invokes: " << PercentDivide(same_class_total, other_class_total) << "\n"; |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 498 | os << "Invokes from code: " << (same_class_total + other_class_total) << "\n"; |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 499 | os << "Type uses on top types: " << PercentDivide(uses_top_types_, uses_all_types_) << "\n"; |
| 500 | os << "Type uses 1b savings: " << PercentDivide(uses_top_types_, total_size) << "\n"; |
| 501 | os << "Total unique types accessed per class " << total_unique_types_ << "\n"; |
Mathieu Chartier | 7c3a8c1 | 2018-05-23 18:09:45 -0700 | [diff] [blame] | 502 | os << "Total Dex code bytes: " << Percent(dex_code_bytes_, total_size) << "\n"; |
| 503 | os << "Total unique code items: " << total_unique_code_items_ << "\n"; |
| 504 | os << "Total Dex size: " << total_size << "\n"; |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 507 | void CodeMetrics::ProcessDexFile(const DexFile& dex_file) { |
| 508 | for (ClassAccessor accessor : dex_file.GetClasses()) { |
| 509 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
| 510 | bool space_for_out_arg = false; |
| 511 | for (const DexInstructionPcPair& inst : method.GetInstructions()) { |
| 512 | switch (inst->Opcode()) { |
| 513 | case Instruction::INVOKE_VIRTUAL: |
| 514 | case Instruction::INVOKE_DIRECT: |
| 515 | case Instruction::INVOKE_SUPER: |
| 516 | case Instruction::INVOKE_INTERFACE: |
| 517 | case Instruction::INVOKE_STATIC: { |
| 518 | const uint32_t args = NumberOfArgs(inst.Inst()); |
| 519 | CHECK_LT(args, kMaxArgCount); |
| 520 | ++arg_counts_[args]; |
| 521 | space_for_out_arg = args < kMaxArgCount - 1; |
| 522 | break; |
| 523 | } |
| 524 | case Instruction::MOVE_RESULT: |
| 525 | case Instruction::MOVE_RESULT_OBJECT: { |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 526 | if (space_for_out_arg && inst->VRegA_11x() < 16) { |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 527 | move_result_savings_ += inst->SizeInCodeUnits() * 2; |
| 528 | } |
| 529 | break; |
| 530 | } |
| 531 | default: |
| 532 | space_for_out_arg = false; |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | } |
Mathieu Chartier | ef2fa26 | 2018-04-12 15:14:07 -0700 | [diff] [blame] | 539 | |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 540 | void CodeMetrics::Dump(std::ostream& os, uint64_t total_size) const { |
| 541 | const uint64_t total = std::accumulate(arg_counts_, arg_counts_ + kMaxArgCount, 0u); |
| 542 | for (size_t i = 0; i < kMaxArgCount; ++i) { |
| 543 | os << "args=" << i << ": " << Percent(arg_counts_[i], total) << "\n"; |
| 544 | } |
| 545 | os << "Move result savings: " << Percent(move_result_savings_, total_size) << "\n"; |
| 546 | os << "One byte invoke savings: " << Percent(total, total_size) << "\n"; |
Mathieu Chartier | 5d16154 | 2018-05-31 11:02:39 -0700 | [diff] [blame^] | 547 | const uint64_t low_arg_total = std::accumulate(arg_counts_, arg_counts_ + 2, 0u); |
Mathieu Chartier | b7ae0b1 | 2018-05-29 09:25:57 -0700 | [diff] [blame] | 548 | os << "Low arg savings: " << Percent(low_arg_total * 2, total_size) << "\n"; |
| 549 | } |
| 550 | |
| 551 | } // namespace art |