1 /*
2 * Copyright (C) 2015 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 "profile_compilation_info.h"
18
19 #include <fcntl.h>
20 #include <sys/file.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <zlib.h>
25
26 #include <algorithm>
27 #include <cerrno>
28 #include <climits>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <iostream>
32 #include <numeric>
33 #include <random>
34 #include <string>
35 #include <vector>
36
37 #include "android-base/file.h"
38 #include "android-base/properties.h"
39 #include "android-base/scopeguard.h"
40 #include "android-base/unique_fd.h"
41 #include "base/arena_allocator.h"
42 #include "base/bit_utils.h"
43 #include "base/dumpable.h"
44 #include "base/file_utils.h"
45 #include "base/globals.h"
46 #include "base/logging.h" // For VLOG.
47 #include "base/malloc_arena_pool.h"
48 #include "base/os.h"
49 #include "base/safe_map.h"
50 #include "base/scoped_flock.h"
51 #include "base/stl_util.h"
52 #include "base/systrace.h"
53 #include "base/time_utils.h"
54 #include "base/unix_file/fd_file.h"
55 #include "base/utils.h"
56 #include "base/zip_archive.h"
57 #include "dex/descriptors_names.h"
58 #include "dex/dex_file_loader.h"
59
60 #ifdef ART_TARGET_ANDROID
61 #include "android-modules-utils/sdk_level.h"
62 #endif
63
64 namespace art {
65
66 const uint8_t ProfileCompilationInfo::kProfileMagic[] = { 'p', 'r', 'o', '\0' };
67 // Last profile version: New extensible profile format.
68 const uint8_t ProfileCompilationInfo::kProfileVersion[] = { '0', '1', '5', '\0' };
69 const uint8_t ProfileCompilationInfo::kProfileVersionForBootImage[] = { '0', '1', '6', '\0' };
70
71 static_assert(sizeof(ProfileCompilationInfo::kProfileVersion) == 4,
72 "Invalid profile version size");
73 static_assert(sizeof(ProfileCompilationInfo::kProfileVersionForBootImage) == 4,
74 "Invalid profile version size");
75
76 // The name of the profile entry in the dex metadata file.
77 // DO NOT CHANGE THIS! (it's similar to classes.dex in the apk files).
78 const char ProfileCompilationInfo::kDexMetadataProfileEntry[] = "primary.prof";
79
80 // A synthetic annotations that can be used to denote that no annotation should
81 // be associated with the profile samples. We use the empty string for the package name
82 // because that's an invalid package name and should never occur in practice.
83 const ProfileCompilationInfo::ProfileSampleAnnotation
84 ProfileCompilationInfo::ProfileSampleAnnotation::kNone =
85 ProfileCompilationInfo::ProfileSampleAnnotation("");
86
87 static constexpr char kSampleMetadataSeparator = ':';
88
89 // Note: This used to be PATH_MAX (usually 4096) but that seems excessive
90 // and we do not want to rely on that external constant anyway.
91 static constexpr uint16_t kMaxDexFileKeyLength = 1024;
92
93 // Extra descriptors are serialized with a `uint16_t` prefix. This defines the length limit.
94 static constexpr size_t kMaxExtraDescriptorLength = std::numeric_limits<uint16_t>::max();
95
96 // According to dex file specification, there can be more than 2^16 valid method indexes
97 // but bytecode uses only 16 bits, so higher method indexes are not very useful (though
98 // such methods could be reached through virtual or interface dispatch). Consequently,
99 // dex files with more than 2^16 method indexes are not really used and the profile file
100 // format does not support higher method indexes.
101 static constexpr uint32_t kMaxSupportedMethodIndex = 0xffffu;
102
103 // Debug flag to ignore checksums when testing if a method or a class is present in the profile.
104 // Used to facilitate testing profile guided compilation across a large number of apps
105 // using the same test profile.
106 static constexpr bool kDebugIgnoreChecksum = false;
107
108 static constexpr uint8_t kIsMissingTypesEncoding = 6;
109 static constexpr uint8_t kIsMegamorphicEncoding = 7;
110
111 static_assert(sizeof(ProfileCompilationInfo::kIndividualInlineCacheSize) == sizeof(uint8_t),
112 "InlineCache::kIndividualInlineCacheSize does not have the expect type size");
113 static_assert(ProfileCompilationInfo::kIndividualInlineCacheSize < kIsMegamorphicEncoding,
114 "InlineCache::kIndividualInlineCacheSize is larger than expected");
115 static_assert(ProfileCompilationInfo::kIndividualInlineCacheSize < kIsMissingTypesEncoding,
116 "InlineCache::kIndividualInlineCacheSize is larger than expected");
117
118 static constexpr uint32_t kSizeWarningThresholdBytes = 500000U;
119 static constexpr uint32_t kSizeErrorThresholdBytes = 1500000U;
120
121 static constexpr uint32_t kSizeWarningThresholdBootBytes = 25000000U;
122 static constexpr uint32_t kSizeErrorThresholdBootBytes = 100000000U;
123
ChecksumMatch(uint32_t dex_file_checksum,uint32_t checksum)124 static bool ChecksumMatch(uint32_t dex_file_checksum, uint32_t checksum) {
125 return kDebugIgnoreChecksum || dex_file_checksum == checksum;
126 }
127
128 namespace {
129
130 // Deflate the input buffer `in_buffer`. It returns a buffer of
131 // compressed data for the input buffer of `*compressed_data_size` size.
DeflateBuffer(ArrayRef<const uint8_t> in_buffer,uint32_t * compressed_data_size)132 std::unique_ptr<uint8_t[]> DeflateBuffer(ArrayRef<const uint8_t> in_buffer,
133 /*out*/ uint32_t* compressed_data_size) {
134 z_stream strm;
135 strm.zalloc = Z_NULL;
136 strm.zfree = Z_NULL;
137 strm.opaque = Z_NULL;
138 int init_ret = deflateInit(&strm, 1);
139 if (init_ret != Z_OK) {
140 return nullptr;
141 }
142
143 uint32_t out_size = dchecked_integral_cast<uint32_t>(deflateBound(&strm, in_buffer.size()));
144
145 std::unique_ptr<uint8_t[]> compressed_buffer(new uint8_t[out_size]);
146 strm.avail_in = in_buffer.size();
147 strm.next_in = const_cast<uint8_t*>(in_buffer.data());
148 strm.avail_out = out_size;
149 strm.next_out = &compressed_buffer[0];
150 int ret = deflate(&strm, Z_FINISH);
151 if (ret == Z_STREAM_ERROR) {
152 return nullptr;
153 }
154 *compressed_data_size = out_size - strm.avail_out;
155
156 int end_ret = deflateEnd(&strm);
157 if (end_ret != Z_OK) {
158 return nullptr;
159 }
160
161 return compressed_buffer;
162 }
163
164 // Inflate the data from `in_buffer` into `out_buffer`. The `out_buffer.size()`
165 // is the expected output size of the buffer. It returns Z_STREAM_END on success.
166 // On error, it returns Z_STREAM_ERROR if the compressed data is inconsistent
167 // and Z_DATA_ERROR if the stream ended prematurely or the stream has extra data.
InflateBuffer(ArrayRef<const uint8_t> in_buffer,ArrayRef<uint8_t> out_buffer)168 int InflateBuffer(ArrayRef<const uint8_t> in_buffer, /*out*/ ArrayRef<uint8_t> out_buffer) {
169 /* allocate inflate state */
170 z_stream strm;
171 strm.zalloc = Z_NULL;
172 strm.zfree = Z_NULL;
173 strm.opaque = Z_NULL;
174 strm.avail_in = in_buffer.size();
175 strm.next_in = const_cast<uint8_t*>(in_buffer.data());
176 strm.avail_out = out_buffer.size();
177 strm.next_out = out_buffer.data();
178
179 int init_ret = inflateInit(&strm);
180 if (init_ret != Z_OK) {
181 return init_ret;
182 }
183
184 int ret = inflate(&strm, Z_NO_FLUSH);
185 if (strm.avail_in != 0 || strm.avail_out != 0) {
186 return Z_DATA_ERROR;
187 }
188
189 int end_ret = inflateEnd(&strm);
190 if (end_ret != Z_OK) {
191 return end_ret;
192 }
193
194 return ret;
195 }
196
197 } // anonymous namespace
198
199 enum class ProfileCompilationInfo::ProfileLoadStatus : uint32_t {
200 kSuccess,
201 kIOError,
202 kBadMagic,
203 kVersionMismatch,
204 kBadData,
205 kMergeError, // Merging failed. There are too many extra descriptors
206 // or classes without TypeId referenced by a dex file.
207 };
208
209 enum class ProfileCompilationInfo::FileSectionType : uint32_t {
210 // The values of section enumerators and data format for individual sections
211 // must not be changed without changing the profile file version. New sections
212 // can be added at the end and they shall be ignored by old versions of ART.
213
214 // The list of the dex files included in the profile.
215 // There must be exactly one dex file section and it must be first.
216 kDexFiles = 0,
217
218 // Extra descriptors for referencing classes that do not have a `dex::TypeId`
219 // in the referencing dex file, such as classes from a different dex file
220 // (even outside of the dex files in the profile) or array classes that were
221 // used from other dex files or created through reflection.
222 kExtraDescriptors = 1,
223
224 // Classes included in the profile.
225 kClasses = 2,
226
227 // Methods included in the profile, their hotness flags and inline caches.
228 kMethods = 3,
229
230 // The aggregation counts of the profile, classes and methods. This section is
231 // an optional reserved section not implemented on client yet.
232 kAggregationCounts = 4,
233
234 // The number of known sections.
235 kNumberOfSections = 5
236 };
237
238 class ProfileCompilationInfo::FileSectionInfo {
239 public:
240 // Constructor for reading from a `ProfileSource`. Data shall be filled from the source.
FileSectionInfo()241 FileSectionInfo() {}
242
243 // Constructor for writing to a file.
FileSectionInfo(FileSectionType type,uint32_t file_offset,uint32_t file_size,uint32_t inflated_size)244 FileSectionInfo(FileSectionType type,
245 uint32_t file_offset,
246 uint32_t file_size,
247 uint32_t inflated_size)
248 : type_(type),
249 file_offset_(file_offset),
250 file_size_(file_size),
251 inflated_size_(inflated_size) {}
252
SetFileOffset(uint32_t file_offset)253 void SetFileOffset(uint32_t file_offset) {
254 DCHECK_EQ(file_offset_, 0u);
255 DCHECK_NE(file_offset, 0u);
256 file_offset_ = file_offset;
257 }
258
GetType() const259 FileSectionType GetType() const {
260 return type_;
261 }
262
GetFileOffset() const263 uint32_t GetFileOffset() const {
264 return file_offset_;
265 }
266
GetFileSize() const267 uint32_t GetFileSize() const {
268 return file_size_;
269 }
270
GetInflatedSize() const271 uint32_t GetInflatedSize() const {
272 return inflated_size_;
273 }
274
GetMemSize() const275 uint32_t GetMemSize() const {
276 return inflated_size_ != 0u ? inflated_size_ : file_size_;
277 }
278
279 private:
280 FileSectionType type_;
281 uint32_t file_offset_;
282 uint32_t file_size_;
283 uint32_t inflated_size_; // If 0, do not inflate and use data from file directly.
284 };
285
286 // The file header.
287 class ProfileCompilationInfo::FileHeader {
288 public:
289 // Constructor for reading from a `ProfileSource`. Data shall be filled from the source.
FileHeader()290 FileHeader() {
291 DCHECK(!IsValid());
292 }
293
294 // Constructor for writing to a file.
FileHeader(const uint8_t * version,uint32_t file_section_count)295 FileHeader(const uint8_t* version, uint32_t file_section_count)
296 : file_section_count_(file_section_count) {
297 static_assert(sizeof(magic_) == sizeof(kProfileMagic));
298 static_assert(sizeof(version_) == sizeof(kProfileVersion));
299 static_assert(sizeof(version_) == sizeof(kProfileVersionForBootImage));
300 memcpy(magic_, kProfileMagic, sizeof(kProfileMagic));
301 memcpy(version_, version, sizeof(version_));
302 DCHECK_LE(file_section_count, kMaxFileSectionCount);
303 DCHECK(IsValid());
304 }
305
IsValid() const306 bool IsValid() const {
307 return memcmp(magic_, kProfileMagic, sizeof(kProfileMagic)) == 0 &&
308 (memcmp(version_, kProfileVersion, kProfileVersionSize) == 0 ||
309 memcmp(version_, kProfileVersionForBootImage, kProfileVersionSize) == 0) &&
310 file_section_count_ != 0u && // The dex files section is mandatory.
311 file_section_count_ <= kMaxFileSectionCount;
312 }
313
GetVersion() const314 const uint8_t* GetVersion() const {
315 DCHECK(IsValid());
316 return version_;
317 }
318
319 ProfileLoadStatus InvalidHeaderMessage(/*out*/ std::string* error_msg) const;
320
GetFileSectionCount() const321 uint32_t GetFileSectionCount() const {
322 DCHECK(IsValid());
323 return file_section_count_;
324 }
325
326 private:
327 // The upper bound for file section count is used to ensure that there
328 // shall be no arithmetic overflow when calculating size of the header
329 // with section information.
330 static const uint32_t kMaxFileSectionCount;
331
332 uint8_t magic_[4] = {0, 0, 0, 0};
333 uint8_t version_[4] = {0, 0, 0, 0};
334 uint32_t file_section_count_ = 0u;
335 };
336
337 const uint32_t ProfileCompilationInfo::FileHeader::kMaxFileSectionCount =
338 (std::numeric_limits<uint32_t>::max() - sizeof(FileHeader)) / sizeof(FileSectionInfo);
339
340 ProfileCompilationInfo::ProfileLoadStatus
InvalidHeaderMessage(std::string * error_msg) const341 ProfileCompilationInfo::FileHeader::InvalidHeaderMessage(/*out*/ std::string* error_msg) const {
342 if (memcmp(magic_, kProfileMagic, sizeof(kProfileMagic)) != 0) {
343 *error_msg = "Profile missing magic.";
344 return ProfileLoadStatus::kBadMagic;
345 }
346 if (memcmp(version_, kProfileVersion, sizeof(kProfileVersion)) != 0 &&
347 memcmp(version_, kProfileVersion, sizeof(kProfileVersionForBootImage)) != 0) {
348 *error_msg = "Profile version mismatch.";
349 return ProfileLoadStatus::kVersionMismatch;
350 }
351 if (file_section_count_ == 0u) {
352 *error_msg = "Missing mandatory dex files section.";
353 return ProfileLoadStatus::kBadData;
354 }
355 DCHECK_GT(file_section_count_, kMaxFileSectionCount);
356 *error_msg ="Too many sections.";
357 return ProfileLoadStatus::kBadData;
358 }
359
360 /**
361 * Encapsulate the source of profile data for loading.
362 * The source can be either a plain file or a zip file.
363 * For zip files, the profile entry will be extracted to
364 * the memory map.
365 */
366 class ProfileCompilationInfo::ProfileSource {
367 public:
368 /**
369 * Create a profile source for the given fd. The ownership of the fd
370 * remains to the caller; as this class will not attempt to close it at any
371 * point.
372 */
Create(int32_t fd)373 static ProfileSource* Create(int32_t fd) {
374 DCHECK_GT(fd, -1);
375 return new ProfileSource(fd, MemMap::Invalid());
376 }
377
378 /**
379 * Create a profile source backed by a memory map. The map can be null in
380 * which case it will the treated as an empty source.
381 */
Create(MemMap && mem_map)382 static ProfileSource* Create(MemMap&& mem_map) {
383 return new ProfileSource(/*fd*/ -1, std::move(mem_map));
384 }
385
386 // Seek to the given offset in the source.
387 bool Seek(off_t offset);
388
389 /**
390 * Read bytes from this source.
391 * Reading will advance the current source position so subsequent
392 * invocations will read from the las position.
393 */
394 ProfileLoadStatus Read(void* buffer,
395 size_t byte_count,
396 const std::string& debug_stage,
397 std::string* error);
398
399 /** Return true if the source has 0 data. */
400 bool HasEmptyContent() const;
401
402 private:
ProfileSource(int32_t fd,MemMap && mem_map)403 ProfileSource(int32_t fd, MemMap&& mem_map)
404 : fd_(fd), mem_map_(std::move(mem_map)), mem_map_cur_(0) {}
405
IsMemMap() const406 bool IsMemMap() const {
407 return fd_ == -1;
408 }
409
410 int32_t fd_; // The fd is not owned by this class.
411 MemMap mem_map_;
412 size_t mem_map_cur_; // Current position in the map to read from.
413 };
414
415 // A helper structure to make sure we don't read past our buffers in the loops.
416 // Also used for writing but the buffer should be pre-sized correctly for that, so we
417 // DCHECK() we do not write beyond the end, rather than returning `false` on failure.
418 class ProfileCompilationInfo::SafeBuffer {
419 public:
SafeBuffer()420 SafeBuffer()
421 : storage_(nullptr),
422 ptr_current_(nullptr),
423 ptr_end_(nullptr) {}
424
SafeBuffer(size_t size)425 explicit SafeBuffer(size_t size)
426 : storage_(new uint8_t[size]),
427 ptr_current_(storage_.get()),
428 ptr_end_(ptr_current_ + size) {}
429
430 // Reads an uint value and advances the current pointer.
431 template <typename T>
ReadUintAndAdvance(T * value)432 bool ReadUintAndAdvance(/*out*/ T* value) {
433 static_assert(std::is_unsigned<T>::value, "Type is not unsigned");
434 if (sizeof(T) > GetAvailableBytes()) {
435 return false;
436 }
437 *value = 0;
438 for (size_t i = 0; i < sizeof(T); i++) {
439 *value += ptr_current_[i] << (i * kBitsPerByte);
440 }
441 ptr_current_ += sizeof(T);
442 return true;
443 }
444
445 // Reads a length-prefixed string as `std::string_view` and advances the current pointer.
446 // The length is `uint16_t`.
ReadStringAndAdvance(std::string_view * value)447 bool ReadStringAndAdvance(/*out*/ std::string_view* value) {
448 uint16_t length;
449 if (!ReadUintAndAdvance(&length)) {
450 return false;
451 }
452 if (length > GetAvailableBytes()) {
453 return false;
454 }
455 const void* null_char = memchr(GetCurrentPtr(), 0, length);
456 if (null_char != nullptr) {
457 // Embedded nulls are invalid.
458 return false;
459 }
460 *value = std::string_view(reinterpret_cast<const char*>(GetCurrentPtr()), length);
461 Advance(length);
462 return true;
463 }
464
465 // Compares the given data with the content at the current pointer.
466 // If the contents are equal it advances the current pointer by data_size.
CompareAndAdvance(const uint8_t * data,size_t data_size)467 bool CompareAndAdvance(const uint8_t* data, size_t data_size) {
468 if (data_size > GetAvailableBytes()) {
469 return false;
470 }
471 if (memcmp(ptr_current_, data, data_size) == 0) {
472 ptr_current_ += data_size;
473 return true;
474 }
475 return false;
476 }
477
WriteAndAdvance(const void * data,size_t data_size)478 void WriteAndAdvance(const void* data, size_t data_size) {
479 DCHECK_LE(data_size, GetAvailableBytes());
480 memcpy(ptr_current_, data, data_size);
481 ptr_current_ += data_size;
482 }
483
484 template <typename T>
WriteUintAndAdvance(T value)485 void WriteUintAndAdvance(T value) {
486 static_assert(std::is_integral_v<T>);
487 WriteAndAdvance(&value, sizeof(value));
488 }
489
490 // Deflate a filled buffer. Replaces the internal buffer with a new one, also filled.
Deflate()491 bool Deflate() {
492 DCHECK_EQ(GetAvailableBytes(), 0u);
493 DCHECK_NE(Size(), 0u);
494 ArrayRef<const uint8_t> in_buffer(Get(), Size());
495 uint32_t output_size = 0;
496 std::unique_ptr<uint8_t[]> compressed_buffer = DeflateBuffer(in_buffer, &output_size);
497 if (compressed_buffer == nullptr) {
498 return false;
499 }
500 storage_ = std::move(compressed_buffer);
501 ptr_current_ = storage_.get() + output_size;
502 ptr_end_ = ptr_current_;
503 return true;
504 }
505
506 // Inflate an unread buffer. Replaces the internal buffer with a new one, also unread.
Inflate(size_t uncompressed_data_size)507 bool Inflate(size_t uncompressed_data_size) {
508 DCHECK(ptr_current_ == storage_.get());
509 DCHECK_NE(Size(), 0u);
510 ArrayRef<const uint8_t> in_buffer(Get(), Size());
511 SafeBuffer uncompressed_buffer(uncompressed_data_size);
512 ArrayRef<uint8_t> out_buffer(uncompressed_buffer.Get(), uncompressed_data_size);
513 int ret = InflateBuffer(in_buffer, out_buffer);
514 if (ret != Z_STREAM_END) {
515 return false;
516 }
517 Swap(uncompressed_buffer);
518 DCHECK(ptr_current_ == storage_.get());
519 return true;
520 }
521
522 // Advances current pointer by data_size.
Advance(size_t data_size)523 void Advance(size_t data_size) {
524 DCHECK_LE(data_size, GetAvailableBytes());
525 ptr_current_ += data_size;
526 }
527
528 // Returns the count of unread bytes.
GetAvailableBytes() const529 size_t GetAvailableBytes() const {
530 DCHECK_LE(static_cast<void*>(ptr_current_), static_cast<void*>(ptr_end_));
531 return (ptr_end_ - ptr_current_) * sizeof(*ptr_current_);
532 }
533
534 // Returns the current pointer.
GetCurrentPtr()535 uint8_t* GetCurrentPtr() {
536 return ptr_current_;
537 }
538
539 // Get the underlying raw buffer.
Get()540 uint8_t* Get() {
541 return storage_.get();
542 }
543
544 // Get the size of the raw buffer.
Size() const545 size_t Size() const {
546 return ptr_end_ - storage_.get();
547 }
548
Swap(SafeBuffer & other)549 void Swap(SafeBuffer& other) {
550 std::swap(storage_, other.storage_);
551 std::swap(ptr_current_, other.ptr_current_);
552 std::swap(ptr_end_, other.ptr_end_);
553 }
554
555 private:
556 std::unique_ptr<uint8_t[]> storage_;
557 uint8_t* ptr_current_;
558 uint8_t* ptr_end_;
559 };
560
ProfileCompilationInfo(ArenaPool * custom_arena_pool,bool for_boot_image)561 ProfileCompilationInfo::ProfileCompilationInfo(ArenaPool* custom_arena_pool, bool for_boot_image)
562 : default_arena_pool_(),
563 allocator_(custom_arena_pool),
564 info_(allocator_.Adapter(kArenaAllocProfile)),
565 profile_key_map_(std::less<const std::string_view>(), allocator_.Adapter(kArenaAllocProfile)),
566 extra_descriptors_(),
567 extra_descriptors_indexes_(ExtraDescriptorHash(&extra_descriptors_),
568 ExtraDescriptorEquals(&extra_descriptors_)) {
569 memcpy(version_,
570 for_boot_image ? kProfileVersionForBootImage : kProfileVersion,
571 kProfileVersionSize);
572 }
573
ProfileCompilationInfo(ArenaPool * custom_arena_pool)574 ProfileCompilationInfo::ProfileCompilationInfo(ArenaPool* custom_arena_pool)
575 : ProfileCompilationInfo(custom_arena_pool, /*for_boot_image=*/ false) { }
576
ProfileCompilationInfo()577 ProfileCompilationInfo::ProfileCompilationInfo()
578 : ProfileCompilationInfo(/*for_boot_image=*/ false) { }
579
ProfileCompilationInfo(bool for_boot_image)580 ProfileCompilationInfo::ProfileCompilationInfo(bool for_boot_image)
581 : ProfileCompilationInfo(&default_arena_pool_, for_boot_image) { }
582
~ProfileCompilationInfo()583 ProfileCompilationInfo::~ProfileCompilationInfo() {
584 VLOG(profiler) << Dumpable<MemStats>(allocator_.GetMemStats());
585 }
586
AddClass(const dex::TypeIndex & type_idx)587 void ProfileCompilationInfo::DexPcData::AddClass(const dex::TypeIndex& type_idx) {
588 if (is_megamorphic || is_missing_types) {
589 return;
590 }
591
592 // Perform an explicit lookup for the type instead of directly emplacing the
593 // element. We do this because emplace() allocates the node before doing the
594 // lookup and if it then finds an identical element, it shall deallocate the
595 // node. For Arena allocations, that's essentially a leak.
596 auto lb = classes.lower_bound(type_idx);
597 if (lb != classes.end() && *lb == type_idx) {
598 // The type index exists.
599 return;
600 }
601
602 // Check if the adding the type will cause the cache to become megamorphic.
603 if (classes.size() + 1 >= ProfileCompilationInfo::kIndividualInlineCacheSize) {
604 is_megamorphic = true;
605 classes.clear();
606 return;
607 }
608
609 // The type does not exist and the inline cache will not be megamorphic.
610 classes.emplace_hint(lb, type_idx);
611 }
612
613 // Transform the actual dex location into a key used to index the dex file in the profile.
614 // See ProfileCompilationInfo#GetProfileDexFileBaseKey as well.
GetProfileDexFileAugmentedKey(const std::string & dex_location,const ProfileSampleAnnotation & annotation)615 std::string ProfileCompilationInfo::GetProfileDexFileAugmentedKey(
616 const std::string& dex_location,
617 const ProfileSampleAnnotation& annotation) {
618 std::string base_key = GetProfileDexFileBaseKey(dex_location);
619 return annotation == ProfileSampleAnnotation::kNone
620 ? base_key
621 : base_key + kSampleMetadataSeparator + annotation.GetOriginPackageName();;
622 }
623
624 // Transform the actual dex location into a base profile key (represented as relative paths).
625 // Note: this is OK because we don't store profiles of different apps into the same file.
626 // Apps with split apks don't cause trouble because each split has a different name and will not
627 // collide with other entries.
GetProfileDexFileBaseKeyView(std::string_view dex_location)628 std::string_view ProfileCompilationInfo::GetProfileDexFileBaseKeyView(
629 std::string_view dex_location) {
630 DCHECK(!dex_location.empty());
631 size_t last_sep_index = dex_location.find_last_of('/');
632 if (last_sep_index == std::string::npos) {
633 return dex_location;
634 } else {
635 DCHECK(last_sep_index < dex_location.size());
636 return dex_location.substr(last_sep_index + 1);
637 }
638 }
639
GetProfileDexFileBaseKey(const std::string & dex_location)640 std::string ProfileCompilationInfo::GetProfileDexFileBaseKey(const std::string& dex_location) {
641 // Note: Conversions between std::string and std::string_view.
642 return std::string(GetProfileDexFileBaseKeyView(dex_location));
643 }
644
GetBaseKeyViewFromAugmentedKey(std::string_view profile_key)645 std::string_view ProfileCompilationInfo::GetBaseKeyViewFromAugmentedKey(
646 std::string_view profile_key) {
647 size_t pos = profile_key.rfind(kSampleMetadataSeparator);
648 return (pos == std::string::npos) ? profile_key : profile_key.substr(0, pos);
649 }
650
GetBaseKeyFromAugmentedKey(const std::string & profile_key)651 std::string ProfileCompilationInfo::GetBaseKeyFromAugmentedKey(
652 const std::string& profile_key) {
653 // Note: Conversions between std::string and std::string_view.
654 return std::string(GetBaseKeyViewFromAugmentedKey(profile_key));
655 }
656
MigrateAnnotationInfo(const std::string & base_key,const std::string & augmented_key)657 std::string ProfileCompilationInfo::MigrateAnnotationInfo(
658 const std::string& base_key,
659 const std::string& augmented_key) {
660 size_t pos = augmented_key.rfind(kSampleMetadataSeparator);
661 return (pos == std::string::npos)
662 ? base_key
663 : base_key + augmented_key.substr(pos);
664 }
665
GetAnnotationFromKey(const std::string & augmented_key)666 ProfileCompilationInfo::ProfileSampleAnnotation ProfileCompilationInfo::GetAnnotationFromKey(
667 const std::string& augmented_key) {
668 size_t pos = augmented_key.rfind(kSampleMetadataSeparator);
669 return (pos == std::string::npos)
670 ? ProfileSampleAnnotation::kNone
671 : ProfileSampleAnnotation(augmented_key.substr(pos + 1));
672 }
673
AddMethods(const std::vector<ProfileMethodInfo> & methods,MethodHotness::Flag flags,const ProfileSampleAnnotation & annotation)674 bool ProfileCompilationInfo::AddMethods(const std::vector<ProfileMethodInfo>& methods,
675 MethodHotness::Flag flags,
676 const ProfileSampleAnnotation& annotation) {
677 for (const ProfileMethodInfo& method : methods) {
678 if (!AddMethod(method, flags, annotation)) {
679 return false;
680 }
681 }
682 return true;
683 }
684
FindOrCreateTypeIndex(const DexFile & dex_file,TypeReference class_ref)685 dex::TypeIndex ProfileCompilationInfo::FindOrCreateTypeIndex(const DexFile& dex_file,
686 TypeReference class_ref) {
687 DCHECK(class_ref.dex_file != nullptr);
688 DCHECK_LT(class_ref.TypeIndex().index_, class_ref.dex_file->NumTypeIds());
689 if (class_ref.dex_file == &dex_file) {
690 // We can use the type index from the `class_ref` as it's a valid index in the `dex_file`.
691 return class_ref.TypeIndex();
692 }
693 // Try to find a `TypeId` in the method's dex file.
694 const char* descriptor = class_ref.dex_file->StringByTypeIdx(class_ref.TypeIndex());
695 return FindOrCreateTypeIndex(dex_file, descriptor);
696 }
697
FindOrCreateTypeIndex(const DexFile & dex_file,const char * descriptor)698 dex::TypeIndex ProfileCompilationInfo::FindOrCreateTypeIndex(const DexFile& dex_file,
699 const char* descriptor) {
700 const dex::TypeId* type_id = dex_file.FindTypeId(descriptor);
701 if (type_id != nullptr) {
702 return dex_file.GetIndexForTypeId(*type_id);
703 }
704 // Try to find an existing extra descriptor.
705 uint32_t num_type_ids = dex_file.NumTypeIds();
706 uint32_t max_artificial_ids = DexFile::kDexNoIndex16 - num_type_ids;
707 std::string_view descriptor_view(descriptor);
708 // Check descriptor length for "extra descriptor". We are using `uint16_t` as prefix.
709 if (UNLIKELY(descriptor_view.size() > kMaxExtraDescriptorLength)) {
710 return dex::TypeIndex(); // Invalid.
711 }
712 auto it = extra_descriptors_indexes_.find(descriptor_view);
713 if (it != extra_descriptors_indexes_.end()) {
714 return (*it < max_artificial_ids) ? dex::TypeIndex(num_type_ids + *it) : dex::TypeIndex();
715 }
716 // Check if inserting the extra descriptor yields a valid artificial type index.
717 if (UNLIKELY(extra_descriptors_.size() >= max_artificial_ids)) {
718 return dex::TypeIndex(); // Invalid.
719 }
720 // Add the descriptor to extra descriptors and return the artificial type index.
721 ExtraDescriptorIndex new_extra_descriptor_index = AddExtraDescriptor(descriptor_view);
722 DCHECK_LT(new_extra_descriptor_index, max_artificial_ids);
723 return dex::TypeIndex(num_type_ids + new_extra_descriptor_index);
724 }
725
AddClass(const DexFile & dex_file,const char * descriptor,const ProfileSampleAnnotation & annotation)726 bool ProfileCompilationInfo::AddClass(const DexFile& dex_file,
727 const char* descriptor,
728 const ProfileSampleAnnotation& annotation) {
729 DexFileData* const data = GetOrAddDexFileData(&dex_file, annotation);
730 if (data == nullptr) { // checksum mismatch
731 return false;
732 }
733 dex::TypeIndex type_index = FindOrCreateTypeIndex(dex_file, descriptor);
734 if (!type_index.IsValid()) {
735 return false;
736 }
737 data->class_set.insert(type_index);
738 return true;
739 }
740
MergeWith(const std::string & filename)741 bool ProfileCompilationInfo::MergeWith(const std::string& filename) {
742 std::string error;
743 #ifdef _WIN32
744 int flags = O_RDONLY;
745 #else
746 int flags = O_RDONLY | O_NOFOLLOW | O_CLOEXEC;
747 #endif
748 ScopedFlock profile_file =
749 LockedFile::Open(filename.c_str(), flags, /*block=*/false, &error);
750
751 if (profile_file.get() == nullptr) {
752 LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
753 return false;
754 }
755
756 int fd = profile_file->Fd();
757
758 ProfileLoadStatus status = LoadInternal(fd, &error);
759 if (status == ProfileLoadStatus::kSuccess) {
760 return true;
761 }
762
763 LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error;
764 return false;
765 }
766
Load(const std::string & filename,bool clear_if_invalid)767 bool ProfileCompilationInfo::Load(const std::string& filename, bool clear_if_invalid) {
768 ScopedTrace trace(__PRETTY_FUNCTION__);
769 std::string error;
770
771 if (!IsEmpty()) {
772 return false;
773 }
774
775 #ifdef _WIN32
776 int flags = O_RDWR;
777 #else
778 int flags = O_RDWR | O_NOFOLLOW | O_CLOEXEC;
779 #endif
780 // There's no need to fsync profile data right away. We get many chances
781 // to write it again in case something goes wrong. We can rely on a simple
782 // close(), no sync, and let to the kernel decide when to write to disk.
783 ScopedFlock profile_file =
784 LockedFile::Open(filename.c_str(), flags, /*block=*/false, &error);
785
786 if (profile_file.get() == nullptr) {
787 if (clear_if_invalid && errno == ENOENT) {
788 return true;
789 }
790 LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
791 return false;
792 }
793
794 int fd = profile_file->Fd();
795
796 ProfileLoadStatus status = LoadInternal(fd, &error);
797 if (status == ProfileLoadStatus::kSuccess) {
798 return true;
799 }
800
801 if (clear_if_invalid &&
802 ((status == ProfileLoadStatus::kBadMagic) ||
803 (status == ProfileLoadStatus::kVersionMismatch) ||
804 (status == ProfileLoadStatus::kBadData))) {
805 LOG(WARNING) << "Clearing bad or obsolete profile data from file "
806 << filename << ": " << error;
807 // When ART Service is enabled, this is the only place where we mutate a profile in place.
808 // TODO(jiakaiz): Get rid of this.
809 if (profile_file->ClearContent()) {
810 return true;
811 } else {
812 PLOG(WARNING) << "Could not clear profile file: " << filename;
813 return false;
814 }
815 }
816
817 LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error;
818 return false;
819 }
820
Save(const std::string & filename,uint64_t * bytes_written)821 bool ProfileCompilationInfo::Save(const std::string& filename, uint64_t* bytes_written) {
822 ScopedTrace trace(__PRETTY_FUNCTION__);
823
824 #ifndef ART_TARGET_ANDROID
825 return SaveFallback(filename, bytes_written);
826 #else
827 // Prior to U, SELinux policy doesn't allow apps to create profile files.
828 // Additionally, when installd is being used for dexopt, it acquires a flock when working on a
829 // profile. It's unclear to us whether the flock means that the file at the fd shouldn't change or
830 // that the file at the path shouldn't change, especially when the installd code is modified by
831 // partners. Therefore, we fall back to using a flock as well just to be safe.
832 if (!android::modules::sdklevel::IsAtLeastU() ||
833 !android::base::GetBoolProperty("dalvik.vm.useartservice", /*default_value=*/false)) {
834 return SaveFallback(filename, bytes_written);
835 }
836
837 std::string tmp_filename = filename + ".XXXXXX.tmp";
838 // mkstemps creates the file with permissions 0600, which is the desired permissions, so there's
839 // no need to chmod.
840 android::base::unique_fd fd(mkostemps(tmp_filename.data(), /*suffixlen=*/4, O_CLOEXEC));
841 if (fd.get() < 0) {
842 PLOG(WARNING) << "Failed to create temp profile file for " << filename;
843 return false;
844 }
845
846 // In case anything goes wrong.
847 auto remove_tmp_file = android::base::make_scope_guard([&]() {
848 if (unlink(tmp_filename.c_str()) != 0) {
849 PLOG(WARNING) << "Failed to remove temp profile file " << tmp_filename;
850 }
851 });
852
853 bool result = Save(fd.get());
854 if (!result) {
855 VLOG(profiler) << "Failed to save profile info to temp profile file " << tmp_filename;
856 return false;
857 }
858
859 fd.reset();
860
861 // Move the temp profile file to the final location.
862 if (rename(tmp_filename.c_str(), filename.c_str()) != 0) {
863 PLOG(WARNING) << "Failed to commit profile file " << filename;
864 return false;
865 }
866
867 remove_tmp_file.Disable();
868
869 int64_t size = OS::GetFileSizeBytes(filename.c_str());
870 if (size != -1) {
871 VLOG(profiler) << "Successfully saved profile info to " << filename << " Size: " << size;
872 if (bytes_written != nullptr) {
873 *bytes_written = static_cast<uint64_t>(size);
874 }
875 } else {
876 VLOG(profiler) << "Saved profile info to " << filename
877 << " but failed to get size: " << strerror(errno);
878 }
879
880 return true;
881 #endif
882 }
883
SaveFallback(const std::string & filename,uint64_t * bytes_written)884 bool ProfileCompilationInfo::SaveFallback(const std::string& filename, uint64_t* bytes_written) {
885 std::string error;
886 #ifdef _WIN32
887 int flags = O_WRONLY | O_CREAT;
888 #else
889 int flags = O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT;
890 #endif
891 // There's no need to fsync profile data right away. We get many chances
892 // to write it again in case something goes wrong. We can rely on a simple
893 // close(), no sync, and let to the kernel decide when to write to disk.
894 ScopedFlock profile_file =
895 LockedFile::Open(filename.c_str(), flags, /*block=*/false, &error);
896 if (profile_file.get() == nullptr) {
897 LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
898 return false;
899 }
900
901 int fd = profile_file->Fd();
902
903 // We need to clear the data because we don't support appending to the profiles yet.
904 if (!profile_file->ClearContent()) {
905 PLOG(WARNING) << "Could not clear profile file: " << filename;
906 return false;
907 }
908
909 // This doesn't need locking because we are trying to lock the file for exclusive
910 // access and fail immediately if we can't.
911 bool result = Save(fd);
912 if (result) {
913 int64_t size = OS::GetFileSizeBytes(filename.c_str());
914 if (size != -1) {
915 VLOG(profiler)
916 << "Successfully saved profile info to " << filename << " Size: "
917 << size;
918 if (bytes_written != nullptr) {
919 *bytes_written = static_cast<uint64_t>(size);
920 }
921 } else {
922 VLOG(profiler) << "Saved profile info to " << filename
923 << " but failed to get size: " << strerror(errno);
924 }
925 } else {
926 VLOG(profiler) << "Failed to save profile info to " << filename;
927 }
928 return result;
929 }
930
931 // Returns true if all the bytes were successfully written to the file descriptor.
WriteBuffer(int fd,const void * buffer,size_t byte_count)932 static bool WriteBuffer(int fd, const void* buffer, size_t byte_count) {
933 while (byte_count > 0) {
934 int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count));
935 if (bytes_written == -1) {
936 return false;
937 }
938 byte_count -= bytes_written; // Reduce the number of remaining bytes.
939 reinterpret_cast<const uint8_t*&>(buffer) += bytes_written; // Move the buffer forward.
940 }
941 return true;
942 }
943
944 /**
945 * Serialization format:
946 *
947 * The file starts with a header and section information:
948 * FileHeader
949 * FileSectionInfo[]
950 * The first FileSectionInfo must be for the DexFiles section.
951 *
952 * The rest of the file is allowed to contain different sections in any order,
953 * at arbitrary offsets, with any gaps betweeen them and each section can be
954 * either plaintext or separately zipped. However, we're writing sections
955 * without any gaps with the following order and compression:
956 * DexFiles - mandatory, plaintext
957 * ExtraDescriptors - optional, zipped
958 * Classes - optional, zipped
959 * Methods - optional, zipped
960 * AggregationCounts - optional, zipped, server-side
961 *
962 * DexFiles:
963 * number_of_dex_files
964 * (checksum,num_type_ids,num_method_ids,profile_key)[number_of_dex_files]
965 * where `profile_key` is a length-prefixed string, the length is `uint16_t`.
966 *
967 * ExtraDescriptors:
968 * number_of_extra_descriptors
969 * (extra_descriptor)[number_of_extra_descriptors]
970 * where `extra_descriptor` is a length-prefixed string, the length is `uint16_t`.
971 *
972 * Classes contains records for any number of dex files, each consisting of:
973 * profile_index // Index of the dex file in DexFiles section.
974 * number_of_classes
975 * type_index_diff[number_of_classes]
976 * where instead of storing plain sorted type indexes, we store their differences
977 * as smaller numbers are likely to compress better.
978 *
979 * Methods contains records for any number of dex files, each consisting of:
980 * profile_index // Index of the dex file in DexFiles section.
981 * following_data_size // For easy skipping of remaining data when dex file is filtered out.
982 * method_flags
983 * bitmap_data
984 * method_encoding[] // Until the size indicated by `following_data_size`.
985 * where `method_flags` is a union of flags recorded for methods in the referenced dex file,
986 * `bitmap_data` contains `num_method_ids` bits for each bit set in `method_flags` other
987 * than "hot" (the size of `bitmap_data` is rounded up to whole bytes) and `method_encoding[]`
988 * contains data for hot methods. The `method_encoding` is:
989 * method_index_diff
990 * number_of_inline_caches
991 * inline_cache_encoding[number_of_inline_caches]
992 * where differences in method indexes are used for better compression,
993 * and the `inline_cache_encoding` is
994 * dex_pc
995 * (M|dex_map_size)
996 * type_index_diff[dex_map_size]
997 * where `M` stands for special encodings indicating missing types (kIsMissingTypesEncoding)
998 * or memamorphic call (kIsMegamorphicEncoding) which both imply `dex_map_size == 0`.
999 **/
Save(int fd)1000 bool ProfileCompilationInfo::Save(int fd) {
1001 uint64_t start = NanoTime();
1002 ScopedTrace trace(__PRETTY_FUNCTION__);
1003 DCHECK_GE(fd, 0);
1004
1005 // Collect uncompressed section sizes.
1006 // Use `uint64_t` and assume this cannot overflow as we would have run out of memory.
1007 uint64_t extra_descriptors_section_size = 0u;
1008 if (!extra_descriptors_.empty()) {
1009 extra_descriptors_section_size += sizeof(uint16_t); // Number of descriptors.
1010 for (const std::string& descriptor : extra_descriptors_) {
1011 // Length-prefixed string, the length is `uint16_t`.
1012 extra_descriptors_section_size += sizeof(uint16_t) + descriptor.size();
1013 }
1014 }
1015 uint64_t dex_files_section_size = sizeof(ProfileIndexType); // Number of dex files.
1016 uint64_t classes_section_size = 0u;
1017 uint64_t methods_section_size = 0u;
1018 DCHECK_LE(info_.size(), MaxProfileIndex());
1019 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
1020 if (dex_data->profile_key.size() > kMaxDexFileKeyLength) {
1021 LOG(WARNING) << "DexFileKey exceeds allocated limit";
1022 return false;
1023 }
1024 dex_files_section_size +=
1025 3 * sizeof(uint32_t) + // Checksum, num_type_ids, num_method_ids.
1026 // Length-prefixed string, the length is `uint16_t`.
1027 sizeof(uint16_t) + dex_data->profile_key.size();
1028 classes_section_size += dex_data->ClassesDataSize();
1029 methods_section_size += dex_data->MethodsDataSize();
1030 }
1031
1032 const uint32_t file_section_count =
1033 /* dex files */ 1u +
1034 /* extra descriptors */ (extra_descriptors_section_size != 0u ? 1u : 0u) +
1035 /* classes */ (classes_section_size != 0u ? 1u : 0u) +
1036 /* methods */ (methods_section_size != 0u ? 1u : 0u);
1037 uint64_t header_and_infos_size =
1038 sizeof(FileHeader) + file_section_count * sizeof(FileSectionInfo);
1039
1040 // Check size limit. Allow large profiles for non target builds for the case
1041 // where we are merging many profiles to generate a boot image profile.
1042 uint64_t total_uncompressed_size =
1043 header_and_infos_size +
1044 dex_files_section_size +
1045 extra_descriptors_section_size +
1046 classes_section_size +
1047 methods_section_size;
1048 VLOG(profiler) << "Required capacity: " << total_uncompressed_size << " bytes.";
1049 if (total_uncompressed_size > GetSizeErrorThresholdBytes()) {
1050 LOG(WARNING) << "Profile data size exceeds "
1051 << GetSizeErrorThresholdBytes()
1052 << " bytes. Profile will not be written to disk."
1053 << " It requires " << total_uncompressed_size << " bytes.";
1054 return false;
1055 }
1056
1057 // Start with an invalid file header and section infos.
1058 DCHECK_EQ(lseek(fd, 0, SEEK_CUR), 0);
1059 constexpr uint32_t kMaxNumberOfSections = enum_cast<uint32_t>(FileSectionType::kNumberOfSections);
1060 constexpr uint64_t kMaxHeaderAndInfosSize =
1061 sizeof(FileHeader) + kMaxNumberOfSections * sizeof(FileSectionInfo);
1062 DCHECK_LE(header_and_infos_size, kMaxHeaderAndInfosSize);
1063 std::array<uint8_t, kMaxHeaderAndInfosSize> placeholder;
1064 memset(placeholder.data(), 0, header_and_infos_size);
1065 if (!WriteBuffer(fd, placeholder.data(), header_and_infos_size)) {
1066 return false;
1067 }
1068
1069 std::array<FileSectionInfo, kMaxNumberOfSections> section_infos;
1070 size_t section_index = 0u;
1071 uint32_t file_offset = header_and_infos_size;
1072 auto add_section_info = [&](FileSectionType type, uint32_t file_size, uint32_t inflated_size) {
1073 DCHECK_LT(section_index, section_infos.size());
1074 section_infos[section_index] = FileSectionInfo(type, file_offset, file_size, inflated_size);
1075 file_offset += file_size;
1076 section_index += 1u;
1077 };
1078
1079 // Write the dex files section.
1080 {
1081 SafeBuffer buffer(dex_files_section_size);
1082 buffer.WriteUintAndAdvance(dchecked_integral_cast<ProfileIndexType>(info_.size()));
1083 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
1084 buffer.WriteUintAndAdvance(dex_data->checksum);
1085 buffer.WriteUintAndAdvance(dex_data->num_type_ids);
1086 buffer.WriteUintAndAdvance(dex_data->num_method_ids);
1087 buffer.WriteUintAndAdvance(dchecked_integral_cast<uint16_t>(dex_data->profile_key.size()));
1088 buffer.WriteAndAdvance(dex_data->profile_key.c_str(), dex_data->profile_key.size());
1089 }
1090 DCHECK_EQ(buffer.GetAvailableBytes(), 0u);
1091 // Write the dex files section uncompressed.
1092 if (!WriteBuffer(fd, buffer.Get(), dex_files_section_size)) {
1093 return false;
1094 }
1095 add_section_info(FileSectionType::kDexFiles, dex_files_section_size, /*inflated_size=*/ 0u);
1096 }
1097
1098 // Write the extra descriptors section.
1099 if (extra_descriptors_section_size != 0u) {
1100 SafeBuffer buffer(extra_descriptors_section_size);
1101 buffer.WriteUintAndAdvance(dchecked_integral_cast<uint16_t>(extra_descriptors_.size()));
1102 for (const std::string& descriptor : extra_descriptors_) {
1103 buffer.WriteUintAndAdvance(dchecked_integral_cast<uint16_t>(descriptor.size()));
1104 buffer.WriteAndAdvance(descriptor.c_str(), descriptor.size());
1105 }
1106 if (!buffer.Deflate()) {
1107 return false;
1108 }
1109 if (!WriteBuffer(fd, buffer.Get(), buffer.Size())) {
1110 return false;
1111 }
1112 add_section_info(
1113 FileSectionType::kExtraDescriptors, buffer.Size(), extra_descriptors_section_size);
1114 }
1115
1116 // Write the classes section.
1117 if (classes_section_size != 0u) {
1118 SafeBuffer buffer(classes_section_size);
1119 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
1120 dex_data->WriteClasses(buffer);
1121 }
1122 if (!buffer.Deflate()) {
1123 return false;
1124 }
1125 if (!WriteBuffer(fd, buffer.Get(), buffer.Size())) {
1126 return false;
1127 }
1128 add_section_info(FileSectionType::kClasses, buffer.Size(), classes_section_size);
1129 }
1130
1131 // Write the methods section.
1132 if (methods_section_size != 0u) {
1133 SafeBuffer buffer(methods_section_size);
1134 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
1135 dex_data->WriteMethods(buffer);
1136 }
1137 if (!buffer.Deflate()) {
1138 return false;
1139 }
1140 if (!WriteBuffer(fd, buffer.Get(), buffer.Size())) {
1141 return false;
1142 }
1143 add_section_info(FileSectionType::kMethods, buffer.Size(), methods_section_size);
1144 }
1145
1146 if (file_offset > GetSizeWarningThresholdBytes()) {
1147 LOG(WARNING) << "Profile data size exceeds "
1148 << GetSizeWarningThresholdBytes()
1149 << " It has " << file_offset << " bytes";
1150 }
1151
1152 // Write section infos.
1153 if (lseek64(fd, sizeof(FileHeader), SEEK_SET) != sizeof(FileHeader)) {
1154 return false;
1155 }
1156 SafeBuffer section_infos_buffer(section_index * 4u * sizeof(uint32_t));
1157 for (size_t i = 0; i != section_index; ++i) {
1158 const FileSectionInfo& info = section_infos[i];
1159 section_infos_buffer.WriteUintAndAdvance(enum_cast<uint32_t>(info.GetType()));
1160 section_infos_buffer.WriteUintAndAdvance(info.GetFileOffset());
1161 section_infos_buffer.WriteUintAndAdvance(info.GetFileSize());
1162 section_infos_buffer.WriteUintAndAdvance(info.GetInflatedSize());
1163 }
1164 DCHECK_EQ(section_infos_buffer.GetAvailableBytes(), 0u);
1165 if (!WriteBuffer(fd, section_infos_buffer.Get(), section_infos_buffer.Size())) {
1166 return false;
1167 }
1168
1169 // Write header.
1170 FileHeader header(version_, section_index);
1171 if (lseek(fd, 0, SEEK_SET) != 0) {
1172 return false;
1173 }
1174 if (!WriteBuffer(fd, &header, sizeof(FileHeader))) {
1175 return false;
1176 }
1177
1178 uint64_t total_time = NanoTime() - start;
1179 VLOG(profiler) << "Compressed from "
1180 << std::to_string(total_uncompressed_size)
1181 << " to "
1182 << std::to_string(file_offset);
1183 VLOG(profiler) << "Time to save profile: " << std::to_string(total_time);
1184 return true;
1185 }
1186
GetOrAddDexFileData(const std::string & profile_key,uint32_t checksum,uint32_t num_type_ids,uint32_t num_method_ids)1187 ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData(
1188 const std::string& profile_key,
1189 uint32_t checksum,
1190 uint32_t num_type_ids,
1191 uint32_t num_method_ids) {
1192 DCHECK_EQ(profile_key_map_.size(), info_.size());
1193 auto profile_index_it = profile_key_map_.lower_bound(profile_key);
1194 if (profile_index_it == profile_key_map_.end() || profile_index_it->first != profile_key) {
1195 // We did not find the key. Create a new DexFileData if we did not reach the limit.
1196 DCHECK_LE(profile_key_map_.size(), MaxProfileIndex());
1197 if (profile_key_map_.size() == MaxProfileIndex()) {
1198 // Allow only a limited number dex files to be profiled. This allows us to save bytes
1199 // when encoding. For regular profiles this 2^8, and for boot profiles is 2^16
1200 // (well above what we expect for normal applications).
1201 LOG(ERROR) << "Exceeded the maximum number of dex file. Something went wrong";
1202 return nullptr;
1203 }
1204 ProfileIndexType new_profile_index = dchecked_integral_cast<ProfileIndexType>(info_.size());
1205 std::unique_ptr<DexFileData> dex_file_data(new (&allocator_) DexFileData(
1206 &allocator_,
1207 profile_key,
1208 checksum,
1209 new_profile_index,
1210 num_type_ids,
1211 num_method_ids,
1212 IsForBootImage()));
1213 // Record the new data in `profile_key_map_` and `info_`.
1214 std::string_view new_key(dex_file_data->profile_key);
1215 profile_index_it = profile_key_map_.PutBefore(profile_index_it, new_key, new_profile_index);
1216 info_.push_back(std::move(dex_file_data));
1217 DCHECK_EQ(profile_key_map_.size(), info_.size());
1218 }
1219
1220 ProfileIndexType profile_index = profile_index_it->second;
1221 DexFileData* result = info_[profile_index].get();
1222
1223 // Check that the checksum matches.
1224 // This may different if for example the dex file was updated and we had a record of the old one.
1225 if (result->checksum != checksum) {
1226 LOG(WARNING) << "Checksum mismatch for dex " << profile_key;
1227 return nullptr;
1228 }
1229
1230 // DCHECK that profile info map key is consistent with the one stored in the dex file data.
1231 // This should always be the case since since the cache map is managed by ProfileCompilationInfo.
1232 DCHECK_EQ(profile_key, result->profile_key);
1233 DCHECK_EQ(profile_index, result->profile_index);
1234
1235 if (num_type_ids != result->num_type_ids || num_method_ids != result->num_method_ids) {
1236 // This should not happen... added to help investigating b/65812889.
1237 LOG(ERROR) << "num_type_ids or num_method_ids mismatch for dex " << profile_key
1238 << ", types: expected=" << num_type_ids << " v. actual=" << result->num_type_ids
1239 << ", methods: expected=" << num_method_ids << " actual=" << result->num_method_ids;
1240 return nullptr;
1241 }
1242
1243 return result;
1244 }
1245
FindDexData(const std::string & profile_key,uint32_t checksum,bool verify_checksum) const1246 const ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::FindDexData(
1247 const std::string& profile_key,
1248 uint32_t checksum,
1249 bool verify_checksum) const {
1250 const auto profile_index_it = profile_key_map_.find(profile_key);
1251 if (profile_index_it == profile_key_map_.end()) {
1252 return nullptr;
1253 }
1254
1255 ProfileIndexType profile_index = profile_index_it->second;
1256 const DexFileData* result = info_[profile_index].get();
1257 if (verify_checksum && !ChecksumMatch(result->checksum, checksum)) {
1258 return nullptr;
1259 }
1260 DCHECK_EQ(profile_key, result->profile_key);
1261 DCHECK_EQ(profile_index, result->profile_index);
1262 return result;
1263 }
1264
FindDexDataUsingAnnotations(const DexFile * dex_file,const ProfileSampleAnnotation & annotation) const1265 const ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::FindDexDataUsingAnnotations(
1266 const DexFile* dex_file,
1267 const ProfileSampleAnnotation& annotation) const {
1268 if (annotation == ProfileSampleAnnotation::kNone) {
1269 std::string_view profile_key = GetProfileDexFileBaseKeyView(dex_file->GetLocation());
1270 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
1271 if (profile_key == GetBaseKeyViewFromAugmentedKey(dex_data->profile_key)) {
1272 if (!ChecksumMatch(dex_data->checksum, dex_file->GetLocationChecksum())) {
1273 return nullptr;
1274 }
1275 return dex_data.get();
1276 }
1277 }
1278 } else {
1279 std::string profile_key = GetProfileDexFileAugmentedKey(dex_file->GetLocation(), annotation);
1280 return FindDexData(profile_key, dex_file->GetLocationChecksum());
1281 }
1282
1283 return nullptr;
1284 }
1285
FindAllDexData(const DexFile * dex_file,std::vector<const ProfileCompilationInfo::DexFileData * > * result) const1286 void ProfileCompilationInfo::FindAllDexData(
1287 const DexFile* dex_file,
1288 /*out*/ std::vector<const ProfileCompilationInfo::DexFileData*>* result) const {
1289 std::string_view profile_key = GetProfileDexFileBaseKeyView(dex_file->GetLocation());
1290 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
1291 if (profile_key == GetBaseKeyViewFromAugmentedKey(dex_data->profile_key)) {
1292 if (ChecksumMatch(dex_data->checksum, dex_file->GetLocationChecksum())) {
1293 result->push_back(dex_data.get());
1294 }
1295 }
1296 }
1297 }
1298
AddExtraDescriptor(std::string_view extra_descriptor)1299 ProfileCompilationInfo::ExtraDescriptorIndex ProfileCompilationInfo::AddExtraDescriptor(
1300 std::string_view extra_descriptor) {
1301 DCHECK_LE(extra_descriptor.size(), kMaxExtraDescriptorLength);
1302 DCHECK(extra_descriptors_indexes_.find(extra_descriptor) == extra_descriptors_indexes_.end());
1303 ExtraDescriptorIndex new_extra_descriptor_index = extra_descriptors_.size();
1304 DCHECK_LE(new_extra_descriptor_index, kMaxExtraDescriptors);
1305 if (UNLIKELY(new_extra_descriptor_index == kMaxExtraDescriptors)) {
1306 return kMaxExtraDescriptors; // Cannot add another extra descriptor.
1307 }
1308 // Add the extra descriptor and record the new index.
1309 extra_descriptors_.emplace_back(extra_descriptor);
1310 extra_descriptors_indexes_.insert(new_extra_descriptor_index);
1311 return new_extra_descriptor_index;
1312 }
1313
AddMethod(const ProfileMethodInfo & pmi,MethodHotness::Flag flags,const ProfileSampleAnnotation & annotation)1314 bool ProfileCompilationInfo::AddMethod(const ProfileMethodInfo& pmi,
1315 MethodHotness::Flag flags,
1316 const ProfileSampleAnnotation& annotation) {
1317 DexFileData* const data = GetOrAddDexFileData(pmi.ref.dex_file, annotation);
1318 if (data == nullptr) { // checksum mismatch
1319 return false;
1320 }
1321 if (!data->AddMethod(flags, pmi.ref.index)) {
1322 return false;
1323 }
1324 if ((flags & MethodHotness::kFlagHot) == 0) {
1325 // The method is not hot, do not add inline caches.
1326 return true;
1327 }
1328
1329 // Add inline caches.
1330 InlineCacheMap* inline_cache = data->FindOrAddHotMethod(pmi.ref.index);
1331 DCHECK(inline_cache != nullptr);
1332
1333 for (const ProfileMethodInfo::ProfileInlineCache& cache : pmi.inline_caches) {
1334 if (cache.is_missing_types) {
1335 FindOrAddDexPc(inline_cache, cache.dex_pc)->SetIsMissingTypes();
1336 continue;
1337 }
1338 if (cache.is_megamorphic) {
1339 FindOrAddDexPc(inline_cache, cache.dex_pc)->SetIsMegamorphic();
1340 continue;
1341 }
1342 for (const TypeReference& class_ref : cache.classes) {
1343 DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, cache.dex_pc);
1344 if (dex_pc_data->is_missing_types || dex_pc_data->is_megamorphic) {
1345 // Don't bother adding classes if we are missing types or already megamorphic.
1346 break;
1347 }
1348 dex::TypeIndex type_index = FindOrCreateTypeIndex(*pmi.ref.dex_file, class_ref);
1349 if (type_index.IsValid()) {
1350 dex_pc_data->AddClass(type_index);
1351 } else {
1352 // Could not create artificial type index.
1353 dex_pc_data->SetIsMissingTypes();
1354 }
1355 }
1356 }
1357 return true;
1358 }
1359
1360 // TODO(calin): Fix this API. ProfileCompilationInfo::Load should be static and
1361 // return a unique pointer to a ProfileCompilationInfo upon success.
Load(int fd,bool merge_classes,const ProfileLoadFilterFn & filter_fn)1362 bool ProfileCompilationInfo::Load(
1363 int fd, bool merge_classes, const ProfileLoadFilterFn& filter_fn) {
1364 std::string error;
1365
1366 ProfileLoadStatus status = LoadInternal(fd, &error, merge_classes, filter_fn);
1367
1368 if (status == ProfileLoadStatus::kSuccess) {
1369 return true;
1370 } else {
1371 LOG(WARNING) << "Error when reading profile: " << error;
1372 return false;
1373 }
1374 }
1375
VerifyProfileData(const std::vector<const DexFile * > & dex_files)1376 bool ProfileCompilationInfo::VerifyProfileData(const std::vector<const DexFile*>& dex_files) {
1377 std::unordered_map<std::string_view, const DexFile*> key_to_dex_file;
1378 for (const DexFile* dex_file : dex_files) {
1379 key_to_dex_file.emplace(GetProfileDexFileBaseKeyView(dex_file->GetLocation()), dex_file);
1380 }
1381 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
1382 // We need to remove any annotation from the key during verification.
1383 const auto it = key_to_dex_file.find(GetBaseKeyViewFromAugmentedKey(dex_data->profile_key));
1384 if (it == key_to_dex_file.end()) {
1385 // It is okay if profile contains data for additional dex files.
1386 continue;
1387 }
1388 const DexFile* dex_file = it->second;
1389 const std::string& dex_location = dex_file->GetLocation();
1390 if (!ChecksumMatch(dex_data->checksum, dex_file->GetLocationChecksum())) {
1391 LOG(ERROR) << "Dex checksum mismatch while verifying profile "
1392 << "dex location " << dex_location << " (checksum="
1393 << dex_file->GetLocationChecksum() << ", profile checksum="
1394 << dex_data->checksum;
1395 return false;
1396 }
1397
1398 if (dex_data->num_method_ids != dex_file->NumMethodIds() ||
1399 dex_data->num_type_ids != dex_file->NumTypeIds()) {
1400 LOG(ERROR) << "Number of type or method ids in dex file and profile don't match."
1401 << "dex location " << dex_location
1402 << " dex_file.NumTypeIds=" << dex_file->NumTypeIds()
1403 << " .v dex_data.num_type_ids=" << dex_data->num_type_ids
1404 << ", dex_file.NumMethodIds=" << dex_file->NumMethodIds()
1405 << " v. dex_data.num_method_ids=" << dex_data->num_method_ids;
1406 return false;
1407 }
1408
1409 // Class and method data should be valid. Verify only in debug builds.
1410 if (kIsDebugBuild) {
1411 // Verify method_encoding.
1412 for (const auto& method_it : dex_data->method_map) {
1413 CHECK_LT(method_it.first, dex_data->num_method_ids);
1414
1415 // Verify class indices of inline caches.
1416 const InlineCacheMap &inline_cache_map = method_it.second;
1417 for (const auto& inline_cache_it : inline_cache_map) {
1418 const DexPcData& dex_pc_data = inline_cache_it.second;
1419 if (dex_pc_data.is_missing_types || dex_pc_data.is_megamorphic) {
1420 // No class indices to verify.
1421 CHECK(dex_pc_data.classes.empty());
1422 continue;
1423 }
1424
1425 for (const dex::TypeIndex& type_index : dex_pc_data.classes) {
1426 if (type_index.index_ >= dex_data->num_type_ids) {
1427 CHECK_LT(type_index.index_ - dex_data->num_type_ids, extra_descriptors_.size());
1428 }
1429 }
1430 }
1431 }
1432 // Verify class_ids.
1433 for (const dex::TypeIndex& type_index : dex_data->class_set) {
1434 if (type_index.index_ >= dex_data->num_type_ids) {
1435 CHECK_LT(type_index.index_ - dex_data->num_type_ids, extra_descriptors_.size());
1436 }
1437 }
1438 }
1439 }
1440 return true;
1441 }
1442
OpenSource(int32_t fd,std::unique_ptr<ProfileSource> * source,std::string * error)1443 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::OpenSource(
1444 int32_t fd,
1445 /*out*/ std::unique_ptr<ProfileSource>* source,
1446 /*out*/ std::string* error) {
1447 if (IsProfileFile(fd)) {
1448 source->reset(ProfileSource::Create(fd));
1449 return ProfileLoadStatus::kSuccess;
1450 } else {
1451 std::unique_ptr<ZipArchive> zip_archive(
1452 ZipArchive::OpenFromFd(DupCloexec(fd), "profile", error));
1453 if (zip_archive.get() == nullptr) {
1454 *error = "Could not open the profile zip archive";
1455 return ProfileLoadStatus::kBadData;
1456 }
1457 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(kDexMetadataProfileEntry, error));
1458 if (zip_entry == nullptr) {
1459 // Allow archives without the profile entry. In this case, create an empty profile.
1460 // This gives more flexible when ure-using archives that may miss the entry.
1461 // (e.g. dex metadata files)
1462 LOG(WARNING) << "Could not find entry " << kDexMetadataProfileEntry
1463 << " in the zip archive. Creating an empty profile.";
1464 source->reset(ProfileSource::Create(MemMap::Invalid()));
1465 return ProfileLoadStatus::kSuccess;
1466 }
1467 if (zip_entry->GetUncompressedLength() == 0) {
1468 *error = "Empty profile entry in the zip archive.";
1469 return ProfileLoadStatus::kBadData;
1470 }
1471
1472 // TODO(calin) pass along file names to assist with debugging.
1473 MemMap map = zip_entry->MapDirectlyOrExtract(
1474 kDexMetadataProfileEntry, "profile file", error, alignof(ProfileSource));
1475
1476 if (map.IsValid()) {
1477 source->reset(ProfileSource::Create(std::move(map)));
1478 return ProfileLoadStatus::kSuccess;
1479 } else {
1480 return ProfileLoadStatus::kBadData;
1481 }
1482 }
1483 }
1484
Seek(off_t offset)1485 bool ProfileCompilationInfo::ProfileSource::Seek(off_t offset) {
1486 DCHECK_GE(offset, 0);
1487 if (IsMemMap()) {
1488 if (offset > static_cast<int64_t>(mem_map_.Size())) {
1489 return false;
1490 }
1491 mem_map_cur_ = offset;
1492 return true;
1493 } else {
1494 if (lseek64(fd_, offset, SEEK_SET) != offset) {
1495 return false;
1496 }
1497 return true;
1498 }
1499 }
1500
Read(void * buffer,size_t byte_count,const std::string & debug_stage,std::string * error)1501 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ProfileSource::Read(
1502 void* buffer,
1503 size_t byte_count,
1504 const std::string& debug_stage,
1505 std::string* error) {
1506 if (IsMemMap()) {
1507 DCHECK_LE(mem_map_cur_, mem_map_.Size());
1508 if (byte_count > mem_map_.Size() - mem_map_cur_) {
1509 return ProfileLoadStatus::kBadData;
1510 }
1511 memcpy(buffer, mem_map_.Begin() + mem_map_cur_, byte_count);
1512 mem_map_cur_ += byte_count;
1513 } else {
1514 while (byte_count > 0) {
1515 int bytes_read = TEMP_FAILURE_RETRY(read(fd_, buffer, byte_count));;
1516 if (bytes_read == 0) {
1517 *error += "Profile EOF reached prematurely for " + debug_stage;
1518 return ProfileLoadStatus::kBadData;
1519 } else if (bytes_read < 0) {
1520 *error += "Profile IO error for " + debug_stage + strerror(errno);
1521 return ProfileLoadStatus::kIOError;
1522 }
1523 byte_count -= bytes_read;
1524 reinterpret_cast<uint8_t*&>(buffer) += bytes_read;
1525 }
1526 }
1527 return ProfileLoadStatus::kSuccess;
1528 }
1529
1530
HasEmptyContent() const1531 bool ProfileCompilationInfo::ProfileSource::HasEmptyContent() const {
1532 if (IsMemMap()) {
1533 return !mem_map_.IsValid() || mem_map_.Size() == 0;
1534 } else {
1535 struct stat stat_buffer;
1536 if (fstat(fd_, &stat_buffer) != 0) {
1537 return false;
1538 }
1539 return stat_buffer.st_size == 0;
1540 }
1541 }
1542
ReadSectionData(ProfileSource & source,const FileSectionInfo & section_info,SafeBuffer * buffer,std::string * error)1543 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadSectionData(
1544 ProfileSource& source,
1545 const FileSectionInfo& section_info,
1546 /*out*/ SafeBuffer* buffer,
1547 /*out*/ std::string* error) {
1548 DCHECK_EQ(buffer->Size(), 0u);
1549 if (!source.Seek(section_info.GetFileOffset())) {
1550 *error = "Failed to seek to section data.";
1551 return ProfileLoadStatus::kIOError;
1552 }
1553 SafeBuffer temp_buffer(section_info.GetFileSize());
1554 ProfileLoadStatus status = source.Read(
1555 temp_buffer.GetCurrentPtr(), temp_buffer.GetAvailableBytes(), "ReadSectionData", error);
1556 if (status != ProfileLoadStatus::kSuccess) {
1557 return status;
1558 }
1559 if (section_info.GetInflatedSize() != 0u &&
1560 !temp_buffer.Inflate(section_info.GetInflatedSize())) {
1561 *error += "Error uncompressing section data.";
1562 return ProfileLoadStatus::kBadData;
1563 }
1564 buffer->Swap(temp_buffer);
1565 return ProfileLoadStatus::kSuccess;
1566 }
1567
ReadDexFilesSection(ProfileSource & source,const FileSectionInfo & section_info,const ProfileLoadFilterFn & filter_fn,dchecked_vector<ProfileIndexType> * dex_profile_index_remap,std::string * error)1568 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadDexFilesSection(
1569 ProfileSource& source,
1570 const FileSectionInfo& section_info,
1571 const ProfileLoadFilterFn& filter_fn,
1572 /*out*/ dchecked_vector<ProfileIndexType>* dex_profile_index_remap,
1573 /*out*/ std::string* error) {
1574 DCHECK(section_info.GetType() == FileSectionType::kDexFiles);
1575 SafeBuffer buffer;
1576 ProfileLoadStatus status = ReadSectionData(source, section_info, &buffer, error);
1577 if (status != ProfileLoadStatus::kSuccess) {
1578 return status;
1579 }
1580
1581 ProfileIndexType num_dex_files;
1582 if (!buffer.ReadUintAndAdvance(&num_dex_files)) {
1583 *error = "Error reading number of dex files.";
1584 return ProfileLoadStatus::kBadData;
1585 }
1586 if (num_dex_files >= MaxProfileIndex()) {
1587 *error = "Too many dex files.";
1588 return ProfileLoadStatus::kBadData;
1589 }
1590
1591 DCHECK(dex_profile_index_remap->empty());
1592 for (ProfileIndexType i = 0u; i != num_dex_files; ++i) {
1593 uint32_t checksum, num_type_ids, num_method_ids;
1594 if (!buffer.ReadUintAndAdvance(&checksum) ||
1595 !buffer.ReadUintAndAdvance(&num_type_ids) ||
1596 !buffer.ReadUintAndAdvance(&num_method_ids)) {
1597 *error = "Error reading dex file data.";
1598 return ProfileLoadStatus::kBadData;
1599 }
1600 std::string_view profile_key_view;
1601 if (!buffer.ReadStringAndAdvance(&profile_key_view)) {
1602 *error += "Missing terminating null character for profile key.";
1603 return ProfileLoadStatus::kBadData;
1604 }
1605 if (profile_key_view.size() == 0u || profile_key_view.size() > kMaxDexFileKeyLength) {
1606 *error = "ProfileKey has an invalid size: " + std::to_string(profile_key_view.size());
1607 return ProfileLoadStatus::kBadData;
1608 }
1609 std::string profile_key(profile_key_view);
1610 if (!filter_fn(profile_key, checksum)) {
1611 // Do not load data for this key. Store invalid index to `dex_profile_index_remap`.
1612 VLOG(compiler) << "Profile: Filtered out " << profile_key << " 0x" << std::hex << checksum;
1613 dex_profile_index_remap->push_back(MaxProfileIndex());
1614 continue;
1615 }
1616 DexFileData* data = GetOrAddDexFileData(profile_key, checksum, num_type_ids, num_method_ids);
1617 if (data == nullptr) {
1618 if (UNLIKELY(profile_key_map_.size() == MaxProfileIndex()) &&
1619 profile_key_map_.find(profile_key) == profile_key_map_.end()) {
1620 *error = "Too many dex files.";
1621 } else {
1622 *error = "Checksum, NumTypeIds, or NumMethodIds mismatch for " + profile_key;
1623 }
1624 return ProfileLoadStatus::kBadData;
1625 }
1626 dex_profile_index_remap->push_back(data->profile_index);
1627 }
1628 if (buffer.GetAvailableBytes() != 0u) {
1629 *error = "Unexpected data at end of dex files section.";
1630 return ProfileLoadStatus::kBadData;
1631 }
1632 return ProfileLoadStatus::kSuccess;
1633 }
1634
ReadExtraDescriptorsSection(ProfileSource & source,const FileSectionInfo & section_info,dchecked_vector<ExtraDescriptorIndex> * extra_descriptors_remap,std::string * error)1635 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadExtraDescriptorsSection(
1636 ProfileSource& source,
1637 const FileSectionInfo& section_info,
1638 /*out*/ dchecked_vector<ExtraDescriptorIndex>* extra_descriptors_remap,
1639 /*out*/ std::string* error) {
1640 DCHECK(section_info.GetType() == FileSectionType::kExtraDescriptors);
1641 SafeBuffer buffer;
1642 ProfileLoadStatus status = ReadSectionData(source, section_info, &buffer, error);
1643 if (status != ProfileLoadStatus::kSuccess) {
1644 return status;
1645 }
1646
1647 uint16_t num_extra_descriptors;
1648 if (!buffer.ReadUintAndAdvance(&num_extra_descriptors)) {
1649 *error = "Error reading number of extra descriptors.";
1650 return ProfileLoadStatus::kBadData;
1651 }
1652
1653 // Note: We allow multiple extra descriptors sections in a single profile file
1654 // but that can lead to `kMergeError` if there are too many extra descriptors.
1655 // Other sections can reference only extra descriptors from preceding sections.
1656 extra_descriptors_remap->reserve(
1657 std::min<size_t>(extra_descriptors_remap->size() + num_extra_descriptors,
1658 std::numeric_limits<uint16_t>::max()));
1659 for (uint16_t i = 0; i != num_extra_descriptors; ++i) {
1660 std::string_view extra_descriptor;
1661 if (!buffer.ReadStringAndAdvance(&extra_descriptor)) {
1662 *error += "Missing terminating null character for extra descriptor.";
1663 return ProfileLoadStatus::kBadData;
1664 }
1665 if (!IsValidDescriptor(std::string(extra_descriptor).c_str())) {
1666 *error += "Invalid extra descriptor.";
1667 return ProfileLoadStatus::kBadData;
1668 }
1669 // Try to match an existing extra descriptor.
1670 auto it = extra_descriptors_indexes_.find(extra_descriptor);
1671 if (it != extra_descriptors_indexes_.end()) {
1672 extra_descriptors_remap->push_back(*it);
1673 continue;
1674 }
1675 // Try to insert a new extra descriptor.
1676 ExtraDescriptorIndex extra_descriptor_index = AddExtraDescriptor(extra_descriptor);
1677 if (extra_descriptor_index == kMaxExtraDescriptors) {
1678 *error = "Too many extra descriptors.";
1679 return ProfileLoadStatus::kMergeError;
1680 }
1681 extra_descriptors_remap->push_back(extra_descriptor_index);
1682 }
1683 return ProfileLoadStatus::kSuccess;
1684 }
1685
ReadClassesSection(ProfileSource & source,const FileSectionInfo & section_info,const dchecked_vector<ProfileIndexType> & dex_profile_index_remap,const dchecked_vector<ExtraDescriptorIndex> & extra_descriptors_remap,std::string * error)1686 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadClassesSection(
1687 ProfileSource& source,
1688 const FileSectionInfo& section_info,
1689 const dchecked_vector<ProfileIndexType>& dex_profile_index_remap,
1690 const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
1691 /*out*/ std::string* error) {
1692 DCHECK(section_info.GetType() == FileSectionType::kClasses);
1693 SafeBuffer buffer;
1694 ProfileLoadStatus status = ReadSectionData(source, section_info, &buffer, error);
1695 if (status != ProfileLoadStatus::kSuccess) {
1696 return status;
1697 }
1698
1699 while (buffer.GetAvailableBytes() != 0u) {
1700 ProfileIndexType profile_index;
1701 if (!buffer.ReadUintAndAdvance(&profile_index)) {
1702 *error = "Error profile index in classes section.";
1703 return ProfileLoadStatus::kBadData;
1704 }
1705 if (profile_index >= dex_profile_index_remap.size()) {
1706 *error = "Invalid profile index in classes section.";
1707 return ProfileLoadStatus::kBadData;
1708 }
1709 profile_index = dex_profile_index_remap[profile_index];
1710 if (profile_index == MaxProfileIndex()) {
1711 status = DexFileData::SkipClasses(buffer, error);
1712 } else {
1713 status = info_[profile_index]->ReadClasses(buffer, extra_descriptors_remap, error);
1714 }
1715 if (status != ProfileLoadStatus::kSuccess) {
1716 return status;
1717 }
1718 }
1719 return ProfileLoadStatus::kSuccess;
1720 }
1721
ReadMethodsSection(ProfileSource & source,const FileSectionInfo & section_info,const dchecked_vector<ProfileIndexType> & dex_profile_index_remap,const dchecked_vector<ExtraDescriptorIndex> & extra_descriptors_remap,std::string * error)1722 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::ReadMethodsSection(
1723 ProfileSource& source,
1724 const FileSectionInfo& section_info,
1725 const dchecked_vector<ProfileIndexType>& dex_profile_index_remap,
1726 const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
1727 /*out*/ std::string* error) {
1728 DCHECK(section_info.GetType() == FileSectionType::kMethods);
1729 SafeBuffer buffer;
1730 ProfileLoadStatus status = ReadSectionData(source, section_info, &buffer, error);
1731 if (status != ProfileLoadStatus::kSuccess) {
1732 return status;
1733 }
1734
1735 while (buffer.GetAvailableBytes() != 0u) {
1736 ProfileIndexType profile_index;
1737 if (!buffer.ReadUintAndAdvance(&profile_index)) {
1738 *error = "Error profile index in methods section.";
1739 return ProfileLoadStatus::kBadData;
1740 }
1741 if (profile_index >= dex_profile_index_remap.size()) {
1742 *error = "Invalid profile index in methods section.";
1743 return ProfileLoadStatus::kBadData;
1744 }
1745 profile_index = dex_profile_index_remap[profile_index];
1746 if (profile_index == MaxProfileIndex()) {
1747 status = DexFileData::SkipMethods(buffer, error);
1748 } else {
1749 status = info_[profile_index]->ReadMethods(buffer, extra_descriptors_remap, error);
1750 }
1751 if (status != ProfileLoadStatus::kSuccess) {
1752 return status;
1753 }
1754 }
1755 return ProfileLoadStatus::kSuccess;
1756 }
1757
1758 // TODO(calin): fail fast if the dex checksums don't match.
LoadInternal(int32_t fd,std::string * error,bool merge_classes,const ProfileLoadFilterFn & filter_fn)1759 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::LoadInternal(
1760 int32_t fd,
1761 std::string* error,
1762 bool merge_classes,
1763 const ProfileLoadFilterFn& filter_fn) {
1764 ScopedTrace trace(__PRETTY_FUNCTION__);
1765 DCHECK_GE(fd, 0);
1766
1767 std::unique_ptr<ProfileSource> source;
1768 ProfileLoadStatus status = OpenSource(fd, &source, error);
1769 if (status != ProfileLoadStatus::kSuccess) {
1770 return status;
1771 }
1772
1773 // We allow empty profile files.
1774 // Profiles may be created by ActivityManager or installd before we manage to
1775 // process them in the runtime or profman.
1776 if (source->HasEmptyContent()) {
1777 return ProfileLoadStatus::kSuccess;
1778 }
1779
1780 // Read file header.
1781 FileHeader header;
1782 status = source->Read(&header, sizeof(FileHeader), "ReadProfileHeader", error);
1783 if (status != ProfileLoadStatus::kSuccess) {
1784 return status;
1785 }
1786 if (!header.IsValid()) {
1787 return header.InvalidHeaderMessage(error);
1788 }
1789 if (memcmp(header.GetVersion(), version_, kProfileVersionSize) != 0) {
1790 *error = IsForBootImage() ? "Expected boot profile, got app profile."
1791 : "Expected app profile, got boot profile.";
1792 return ProfileLoadStatus::kVersionMismatch;
1793 }
1794
1795 // Check if there are too many section infos.
1796 uint32_t section_count = header.GetFileSectionCount();
1797 uint32_t uncompressed_data_size = sizeof(FileHeader) + section_count * sizeof(FileSectionInfo);
1798 if (uncompressed_data_size > GetSizeErrorThresholdBytes()) {
1799 LOG(WARNING) << "Profile data size exceeds " << GetSizeErrorThresholdBytes()
1800 << " bytes. It has " << uncompressed_data_size << " bytes.";
1801 return ProfileLoadStatus::kBadData;
1802 }
1803
1804 // Read section infos.
1805 dchecked_vector<FileSectionInfo> section_infos(section_count);
1806 status = source->Read(
1807 section_infos.data(), section_count * sizeof(FileSectionInfo), "ReadSectionInfos", error);
1808 if (status != ProfileLoadStatus::kSuccess) {
1809 return status;
1810 }
1811
1812 // Finish uncompressed data size calculation.
1813 for (const FileSectionInfo& section_info : section_infos) {
1814 uint32_t mem_size = section_info.GetMemSize();
1815 if (UNLIKELY(mem_size > std::numeric_limits<uint32_t>::max() - uncompressed_data_size)) {
1816 *error = "Total memory size overflow.";
1817 return ProfileLoadStatus::kBadData;
1818 }
1819 uncompressed_data_size += mem_size;
1820 }
1821
1822 // Allow large profiles for non target builds for the case where we are merging many profiles
1823 // to generate a boot image profile.
1824 if (uncompressed_data_size > GetSizeErrorThresholdBytes()) {
1825 LOG(WARNING) << "Profile data size exceeds "
1826 << GetSizeErrorThresholdBytes()
1827 << " bytes. It has " << uncompressed_data_size << " bytes.";
1828 return ProfileLoadStatus::kBadData;
1829 }
1830 if (uncompressed_data_size > GetSizeWarningThresholdBytes()) {
1831 LOG(WARNING) << "Profile data size exceeds "
1832 << GetSizeWarningThresholdBytes()
1833 << " bytes. It has " << uncompressed_data_size << " bytes.";
1834 }
1835
1836 // Process the mandatory dex files section.
1837 DCHECK_NE(section_count, 0u); // Checked by `header.IsValid()` above.
1838 const FileSectionInfo& dex_files_section_info = section_infos[0];
1839 if (dex_files_section_info.GetType() != FileSectionType::kDexFiles) {
1840 *error = "First section is not dex files section.";
1841 return ProfileLoadStatus::kBadData;
1842 }
1843 dchecked_vector<ProfileIndexType> dex_profile_index_remap;
1844 status = ReadDexFilesSection(
1845 *source, dex_files_section_info, filter_fn, &dex_profile_index_remap, error);
1846 if (status != ProfileLoadStatus::kSuccess) {
1847 DCHECK(!error->empty());
1848 return status;
1849 }
1850
1851 // Process all other sections.
1852 dchecked_vector<ExtraDescriptorIndex> extra_descriptors_remap;
1853 for (uint32_t i = 1u; i != section_count; ++i) {
1854 const FileSectionInfo& section_info = section_infos[i];
1855 DCHECK(status == ProfileLoadStatus::kSuccess);
1856 switch (section_info.GetType()) {
1857 case FileSectionType::kDexFiles:
1858 *error = "Unsupported additional dex files section.";
1859 status = ProfileLoadStatus::kBadData;
1860 break;
1861 case FileSectionType::kExtraDescriptors:
1862 status = ReadExtraDescriptorsSection(
1863 *source, section_info, &extra_descriptors_remap, error);
1864 break;
1865 case FileSectionType::kClasses:
1866 // Skip if all dex files were filtered out.
1867 if (!info_.empty() && merge_classes) {
1868 status = ReadClassesSection(
1869 *source, section_info, dex_profile_index_remap, extra_descriptors_remap, error);
1870 }
1871 break;
1872 case FileSectionType::kMethods:
1873 // Skip if all dex files were filtered out.
1874 if (!info_.empty()) {
1875 status = ReadMethodsSection(
1876 *source, section_info, dex_profile_index_remap, extra_descriptors_remap, error);
1877 }
1878 break;
1879 case FileSectionType::kAggregationCounts:
1880 // This section is only used on server side.
1881 break;
1882 default:
1883 // Unknown section. Skip it. New versions of ART are allowed
1884 // to add sections that shall be ignored by old versions.
1885 break;
1886 }
1887 if (status != ProfileLoadStatus::kSuccess) {
1888 DCHECK(!error->empty());
1889 return status;
1890 }
1891 }
1892
1893 return ProfileLoadStatus::kSuccess;
1894 }
1895
MergeWith(const ProfileCompilationInfo & other,bool merge_classes)1896 bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other,
1897 bool merge_classes) {
1898 if (!SameVersion(other)) {
1899 LOG(WARNING) << "Cannot merge different profile versions";
1900 return false;
1901 }
1902
1903 // First verify that all checksums match. This will avoid adding garbage to
1904 // the current profile info.
1905 // Note that the number of elements should be very small, so this should not
1906 // be a performance issue.
1907 for (const std::unique_ptr<DexFileData>& other_dex_data : other.info_) {
1908 // verify_checksum is false because we want to differentiate between a missing dex data and
1909 // a mismatched checksum.
1910 const DexFileData* dex_data = FindDexData(other_dex_data->profile_key,
1911 /* checksum= */ 0u,
1912 /* verify_checksum= */ false);
1913 if ((dex_data != nullptr) && (dex_data->checksum != other_dex_data->checksum)) {
1914 LOG(WARNING) << "Checksum mismatch for dex " << other_dex_data->profile_key;
1915 return false;
1916 }
1917 }
1918 // All checksums match. Import the data.
1919
1920 // The other profile might have a different indexing of dex files.
1921 // That is because each dex files gets a 'dex_profile_index' on a first come first served basis.
1922 // That means that the order in with the methods are added to the profile matters for the
1923 // actual indices.
1924 // The reason we cannot rely on the actual multidex index is that a single profile may store
1925 // data from multiple splits. This means that a profile may contain a classes2.dex from split-A
1926 // and one from split-B.
1927
1928 // First, build a mapping from other_dex_profile_index to this_dex_profile_index.
1929 dchecked_vector<ProfileIndexType> dex_profile_index_remap;
1930 dex_profile_index_remap.reserve(other.info_.size());
1931 for (const std::unique_ptr<DexFileData>& other_dex_data : other.info_) {
1932 const DexFileData* dex_data = GetOrAddDexFileData(other_dex_data->profile_key,
1933 other_dex_data->checksum,
1934 other_dex_data->num_type_ids,
1935 other_dex_data->num_method_ids);
1936 if (dex_data == nullptr) {
1937 // Could happen if we exceed the number of allowed dex files or there is
1938 // a mismatch in `num_type_ids` or `num_method_ids`.
1939 return false;
1940 }
1941 DCHECK_EQ(other_dex_data->profile_index, dex_profile_index_remap.size());
1942 dex_profile_index_remap.push_back(dex_data->profile_index);
1943 }
1944
1945 // Then merge extra descriptors.
1946 dchecked_vector<ExtraDescriptorIndex> extra_descriptors_remap;
1947 extra_descriptors_remap.reserve(other.extra_descriptors_.size());
1948 for (const std::string& other_extra_descriptor : other.extra_descriptors_) {
1949 auto it = extra_descriptors_indexes_.find(std::string_view(other_extra_descriptor));
1950 if (it != extra_descriptors_indexes_.end()) {
1951 extra_descriptors_remap.push_back(*it);
1952 } else {
1953 ExtraDescriptorIndex extra_descriptor_index = AddExtraDescriptor(other_extra_descriptor);
1954 if (extra_descriptor_index == kMaxExtraDescriptors) {
1955 // Too many extra descriptors.
1956 return false;
1957 }
1958 extra_descriptors_remap.push_back(extra_descriptor_index);
1959 }
1960 }
1961
1962 // Merge the actual profile data.
1963 for (const std::unique_ptr<DexFileData>& other_dex_data : other.info_) {
1964 DexFileData* dex_data = info_[dex_profile_index_remap[other_dex_data->profile_index]].get();
1965 DCHECK_EQ(dex_data, FindDexData(other_dex_data->profile_key, other_dex_data->checksum));
1966
1967 // Merge the classes.
1968 uint32_t num_type_ids = dex_data->num_type_ids;
1969 DCHECK_EQ(num_type_ids, other_dex_data->num_type_ids);
1970 if (merge_classes) {
1971 // Classes are ordered by the `TypeIndex`, so we have the classes with a `TypeId`
1972 // in the dex file first, followed by classes using extra descriptors.
1973 auto it = other_dex_data->class_set.lower_bound(dex::TypeIndex(num_type_ids));
1974 dex_data->class_set.insert(other_dex_data->class_set.begin(), it);
1975 for (auto end = other_dex_data->class_set.end(); it != end; ++it) {
1976 ExtraDescriptorIndex new_extra_descriptor_index =
1977 extra_descriptors_remap[it->index_ - num_type_ids];
1978 if (new_extra_descriptor_index >= DexFile::kDexNoIndex16 - num_type_ids) {
1979 // Cannot represent the type with new extra descriptor index.
1980 return false;
1981 }
1982 dex_data->class_set.insert(dex::TypeIndex(num_type_ids + new_extra_descriptor_index));
1983 }
1984 }
1985
1986 // Merge the methods and the inline caches.
1987 for (const auto& other_method_it : other_dex_data->method_map) {
1988 uint16_t other_method_index = other_method_it.first;
1989 InlineCacheMap* inline_cache = dex_data->FindOrAddHotMethod(other_method_index);
1990 if (inline_cache == nullptr) {
1991 return false;
1992 }
1993 const auto& other_inline_cache = other_method_it.second;
1994 for (const auto& other_ic_it : other_inline_cache) {
1995 uint16_t other_dex_pc = other_ic_it.first;
1996 const ArenaSet<dex::TypeIndex>& other_class_set = other_ic_it.second.classes;
1997 DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, other_dex_pc);
1998 if (other_ic_it.second.is_missing_types) {
1999 dex_pc_data->SetIsMissingTypes();
2000 } else if (other_ic_it.second.is_megamorphic) {
2001 dex_pc_data->SetIsMegamorphic();
2002 } else {
2003 for (dex::TypeIndex type_index : other_class_set) {
2004 if (type_index.index_ >= num_type_ids) {
2005 ExtraDescriptorIndex new_extra_descriptor_index =
2006 extra_descriptors_remap[type_index.index_ - num_type_ids];
2007 if (new_extra_descriptor_index >= DexFile::kDexNoIndex16 - num_type_ids) {
2008 // Cannot represent the type with new extra descriptor index.
2009 return false;
2010 }
2011 type_index = dex::TypeIndex(num_type_ids + new_extra_descriptor_index);
2012 }
2013 dex_pc_data->AddClass(type_index);
2014 }
2015 }
2016 }
2017 }
2018
2019 // Merge the method bitmaps.
2020 dex_data->MergeBitmap(*other_dex_data);
2021 }
2022
2023 return true;
2024 }
2025
GetMethodHotness(const MethodReference & method_ref,const ProfileSampleAnnotation & annotation) const2026 ProfileCompilationInfo::MethodHotness ProfileCompilationInfo::GetMethodHotness(
2027 const MethodReference& method_ref,
2028 const ProfileSampleAnnotation& annotation) const {
2029 const DexFileData* dex_data = FindDexDataUsingAnnotations(method_ref.dex_file, annotation);
2030 return dex_data != nullptr
2031 ? dex_data->GetHotnessInfo(method_ref.index)
2032 : MethodHotness();
2033 }
2034
ContainsClass(const DexFile & dex_file,dex::TypeIndex type_idx,const ProfileSampleAnnotation & annotation) const2035 bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file,
2036 dex::TypeIndex type_idx,
2037 const ProfileSampleAnnotation& annotation) const {
2038 const DexFileData* dex_data = FindDexDataUsingAnnotations(&dex_file, annotation);
2039 return (dex_data != nullptr) && dex_data->ContainsClass(type_idx);
2040 }
2041
GetNumberOfMethods() const2042 uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
2043 uint32_t total = 0;
2044 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
2045 total += dex_data->method_map.size();
2046 }
2047 return total;
2048 }
2049
GetNumberOfResolvedClasses() const2050 uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
2051 uint32_t total = 0;
2052 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
2053 total += dex_data->class_set.size();
2054 }
2055 return total;
2056 }
2057
DumpInfo(const std::vector<const DexFile * > & dex_files,bool print_full_dex_location) const2058 std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>& dex_files,
2059 bool print_full_dex_location) const {
2060 std::ostringstream os;
2061
2062 os << "ProfileInfo [";
2063
2064 for (size_t k = 0; k < kProfileVersionSize - 1; k++) {
2065 // Iterate to 'kProfileVersionSize - 1' because the version_ ends with '\0'
2066 // which we don't want to print.
2067 os << static_cast<char>(version_[k]);
2068 }
2069 os << "]\n";
2070
2071 if (info_.empty()) {
2072 os << "-empty-";
2073 return os.str();
2074 }
2075
2076 if (!extra_descriptors_.empty()) {
2077 os << "\nextra descriptors:";
2078 for (const std::string& str : extra_descriptors_) {
2079 os << "\n\t" << str;
2080 }
2081 os << "\n";
2082 }
2083
2084 const std::string kFirstDexFileKeySubstitute = "!classes.dex";
2085
2086 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
2087 os << "\n";
2088 if (print_full_dex_location) {
2089 os << dex_data->profile_key;
2090 } else {
2091 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
2092 std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(
2093 GetBaseKeyFromAugmentedKey(dex_data->profile_key));
2094 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
2095 }
2096 os << " [index=" << static_cast<uint32_t>(dex_data->profile_index) << "]";
2097 os << " [checksum=" << std::hex << dex_data->checksum << "]" << std::dec;
2098 os << " [num_type_ids=" << dex_data->num_type_ids << "]";
2099 os << " [num_method_ids=" << dex_data->num_method_ids << "]";
2100 const DexFile* dex_file = nullptr;
2101 for (const DexFile* current : dex_files) {
2102 if (GetBaseKeyViewFromAugmentedKey(dex_data->profile_key) ==
2103 GetProfileDexFileBaseKeyView(current->GetLocation()) &&
2104 ChecksumMatch(dex_data->checksum, current->GetLocationChecksum())) {
2105 dex_file = current;
2106 break;
2107 }
2108 }
2109 os << "\n\thot methods: ";
2110 for (const auto& method_it : dex_data->method_map) {
2111 if (dex_file != nullptr) {
2112 os << "\n\t\t" << dex_file->PrettyMethod(method_it.first, true);
2113 } else {
2114 os << method_it.first;
2115 }
2116
2117 os << "[";
2118 for (const auto& inline_cache_it : method_it.second) {
2119 os << "{" << std::hex << inline_cache_it.first << std::dec << ":";
2120 if (inline_cache_it.second.is_missing_types) {
2121 os << "MT";
2122 } else if (inline_cache_it.second.is_megamorphic) {
2123 os << "MM";
2124 } else {
2125 const char* separator = "";
2126 for (dex::TypeIndex type_index : inline_cache_it.second.classes) {
2127 os << separator << type_index.index_;
2128 separator = ",";
2129 }
2130 }
2131 os << "}";
2132 }
2133 os << "], ";
2134 }
2135 bool startup = true;
2136 while (true) {
2137 os << "\n\t" << (startup ? "startup methods: " : "post startup methods: ");
2138 for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) {
2139 MethodHotness hotness_info(dex_data->GetHotnessInfo(method_idx));
2140 if (startup ? hotness_info.IsStartup() : hotness_info.IsPostStartup()) {
2141 if (dex_file != nullptr) {
2142 os << "\n\t\t" << dex_file->PrettyMethod(method_idx, true);
2143 } else {
2144 os << method_idx << ", ";
2145 }
2146 }
2147 }
2148 if (startup == false) {
2149 break;
2150 }
2151 startup = false;
2152 }
2153 os << "\n\tclasses: ";
2154 for (dex::TypeIndex type_index : dex_data->class_set) {
2155 if (dex_file != nullptr) {
2156 os << "\n\t\t" << PrettyDescriptor(GetTypeDescriptor(dex_file, type_index));
2157 } else {
2158 os << type_index.index_ << ",";
2159 }
2160 }
2161 }
2162 return os.str();
2163 }
2164
GetClassesAndMethods(const DexFile & dex_file,std::set<dex::TypeIndex> * class_set,std::set<uint16_t> * hot_method_set,std::set<uint16_t> * startup_method_set,std::set<uint16_t> * post_startup_method_method_set,const ProfileSampleAnnotation & annotation) const2165 bool ProfileCompilationInfo::GetClassesAndMethods(
2166 const DexFile& dex_file,
2167 /*out*/std::set<dex::TypeIndex>* class_set,
2168 /*out*/std::set<uint16_t>* hot_method_set,
2169 /*out*/std::set<uint16_t>* startup_method_set,
2170 /*out*/std::set<uint16_t>* post_startup_method_method_set,
2171 const ProfileSampleAnnotation& annotation) const {
2172 std::set<std::string> ret;
2173 const DexFileData* dex_data = FindDexDataUsingAnnotations(&dex_file, annotation);
2174 if (dex_data == nullptr) {
2175 return false;
2176 }
2177 for (const auto& it : dex_data->method_map) {
2178 hot_method_set->insert(it.first);
2179 }
2180 for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) {
2181 MethodHotness hotness = dex_data->GetHotnessInfo(method_idx);
2182 if (hotness.IsStartup()) {
2183 startup_method_set->insert(method_idx);
2184 }
2185 if (hotness.IsPostStartup()) {
2186 post_startup_method_method_set->insert(method_idx);
2187 }
2188 }
2189 for (const dex::TypeIndex& type_index : dex_data->class_set) {
2190 class_set->insert(type_index);
2191 }
2192 return true;
2193 }
2194
GetClasses(const DexFile & dex_file,const ProfileSampleAnnotation & annotation) const2195 const ArenaSet<dex::TypeIndex>* ProfileCompilationInfo::GetClasses(
2196 const DexFile& dex_file,
2197 const ProfileSampleAnnotation& annotation) const {
2198 const DexFileData* dex_data = FindDexDataUsingAnnotations(&dex_file, annotation);
2199 if (dex_data == nullptr) {
2200 return nullptr;
2201 }
2202 return &dex_data->class_set;
2203 }
2204
SameVersion(const ProfileCompilationInfo & other) const2205 bool ProfileCompilationInfo::SameVersion(const ProfileCompilationInfo& other) const {
2206 return memcmp(version_, other.version_, kProfileVersionSize) == 0;
2207 }
2208
Equals(const ProfileCompilationInfo & other)2209 bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) {
2210 // No need to compare profile_key_map_. That's only a cache for fast search.
2211 // All the information is already in the info_ vector.
2212 if (!SameVersion(other)) {
2213 return false;
2214 }
2215 if (info_.size() != other.info_.size()) {
2216 return false;
2217 }
2218 for (size_t i = 0; i < info_.size(); i++) {
2219 const DexFileData& dex_data = *info_[i];
2220 const DexFileData& other_dex_data = *other.info_[i];
2221 if (!(dex_data == other_dex_data)) {
2222 return false;
2223 }
2224 }
2225
2226 return true;
2227 }
2228
2229 // Naive implementation to generate a random profile file suitable for testing.
GenerateTestProfile(int fd,uint16_t number_of_dex_files,uint16_t method_percentage,uint16_t class_percentage,uint32_t random_seed)2230 bool ProfileCompilationInfo::GenerateTestProfile(int fd,
2231 uint16_t number_of_dex_files,
2232 uint16_t method_percentage,
2233 uint16_t class_percentage,
2234 uint32_t random_seed) {
2235 const std::string base_dex_location = "base.apk";
2236 ProfileCompilationInfo info;
2237 // The limits are defined by the dex specification.
2238 const uint16_t max_methods = std::numeric_limits<uint16_t>::max();
2239 const uint16_t max_classes = std::numeric_limits<uint16_t>::max();
2240 uint16_t number_of_methods = max_methods * method_percentage / 100;
2241 uint16_t number_of_classes = max_classes * class_percentage / 100;
2242
2243 std::srand(random_seed);
2244
2245 // Make sure we generate more samples with a low index value.
2246 // This makes it more likely to hit valid method/class indices in small apps.
2247 const uint16_t kFavorFirstN = 10000;
2248 const uint16_t kFavorSplit = 2;
2249
2250 for (uint16_t i = 0; i < number_of_dex_files; i++) {
2251 std::string dex_location = DexFileLoader::GetMultiDexLocation(i, base_dex_location.c_str());
2252 std::string profile_key = info.GetProfileDexFileBaseKey(dex_location);
2253
2254 DexFileData* const data =
2255 info.GetOrAddDexFileData(profile_key, /*checksum=*/ 0, max_classes, max_methods);
2256 for (uint16_t m = 0; m < number_of_methods; m++) {
2257 uint16_t method_idx = rand() % max_methods;
2258 if (m < (number_of_methods / kFavorSplit)) {
2259 method_idx %= kFavorFirstN;
2260 }
2261 // Alternate between startup and post startup.
2262 uint32_t flags = MethodHotness::kFlagHot;
2263 flags |= ((m & 1) != 0) ? MethodHotness::kFlagPostStartup : MethodHotness::kFlagStartup;
2264 data->AddMethod(static_cast<MethodHotness::Flag>(flags), method_idx);
2265 }
2266
2267 for (uint16_t c = 0; c < number_of_classes; c++) {
2268 uint16_t type_idx = rand() % max_classes;
2269 if (c < (number_of_classes / kFavorSplit)) {
2270 type_idx %= kFavorFirstN;
2271 }
2272 data->class_set.insert(dex::TypeIndex(type_idx));
2273 }
2274 }
2275 return info.Save(fd);
2276 }
2277
2278 // Naive implementation to generate a random profile file suitable for testing.
2279 // Description of random selection:
2280 // * Select a random starting point S.
2281 // * For every index i, add (S+i) % (N - total number of methods/classes) to profile with the
2282 // probably of 1/(N - i - number of methods/classes needed to add in profile).
GenerateTestProfile(int fd,std::vector<std::unique_ptr<const DexFile>> & dex_files,uint16_t method_percentage,uint16_t class_percentage,uint32_t random_seed)2283 bool ProfileCompilationInfo::GenerateTestProfile(
2284 int fd,
2285 std::vector<std::unique_ptr<const DexFile>>& dex_files,
2286 uint16_t method_percentage,
2287 uint16_t class_percentage,
2288 uint32_t random_seed) {
2289 ProfileCompilationInfo info;
2290 std::default_random_engine rng(random_seed);
2291 auto create_shuffled_range = [&rng](uint32_t take, uint32_t out_of) {
2292 CHECK_LE(take, out_of);
2293 std::vector<uint32_t> vec(out_of);
2294 std::iota(vec.begin(), vec.end(), 0u);
2295 std::shuffle(vec.begin(), vec.end(), rng);
2296 vec.erase(vec.begin() + take, vec.end());
2297 std::sort(vec.begin(), vec.end());
2298 return vec;
2299 };
2300 for (std::unique_ptr<const DexFile>& dex_file : dex_files) {
2301 const std::string& dex_location = dex_file->GetLocation();
2302 std::string profile_key = info.GetProfileDexFileBaseKey(dex_location);
2303 uint32_t checksum = dex_file->GetLocationChecksum();
2304
2305 uint32_t number_of_classes = dex_file->NumClassDefs();
2306 uint32_t classes_required_in_profile = (number_of_classes * class_percentage) / 100;
2307
2308 DexFileData* const data = info.GetOrAddDexFileData(
2309 profile_key, checksum, dex_file->NumTypeIds(), dex_file->NumMethodIds());
2310 for (uint32_t class_index : create_shuffled_range(classes_required_in_profile,
2311 number_of_classes)) {
2312 data->class_set.insert(dex_file->GetClassDef(class_index).class_idx_);
2313 }
2314
2315 uint32_t number_of_methods = dex_file->NumMethodIds();
2316 uint32_t methods_required_in_profile = (number_of_methods * method_percentage) / 100;
2317 for (uint32_t method_index : create_shuffled_range(methods_required_in_profile,
2318 number_of_methods)) {
2319 // Alternate between startup and post startup.
2320 uint32_t flags = MethodHotness::kFlagHot;
2321 flags |= ((method_index & 1) != 0)
2322 ? MethodHotness::kFlagPostStartup
2323 : MethodHotness::kFlagStartup;
2324 data->AddMethod(static_cast<MethodHotness::Flag>(flags), method_index);
2325 }
2326 }
2327 return info.Save(fd);
2328 }
2329
IsEmpty() const2330 bool ProfileCompilationInfo::IsEmpty() const {
2331 DCHECK_EQ(info_.size(), profile_key_map_.size());
2332 // Note that this doesn't look at the bitmap region, so we will return true
2333 // when the profile contains only non-hot methods. This is generally ok
2334 // as for speed-profile to be useful we do need hot methods and resolved classes.
2335 return GetNumberOfMethods() == 0 && GetNumberOfResolvedClasses() == 0;
2336 }
2337
2338 ProfileCompilationInfo::InlineCacheMap*
FindOrAddHotMethod(uint16_t method_index)2339 ProfileCompilationInfo::DexFileData::FindOrAddHotMethod(uint16_t method_index) {
2340 if (method_index >= num_method_ids) {
2341 LOG(ERROR) << "Invalid method index " << method_index << ". num_method_ids=" << num_method_ids;
2342 return nullptr;
2343 }
2344 return &(method_map.FindOrAdd(
2345 method_index,
2346 InlineCacheMap(std::less<uint16_t>(), allocator_->Adapter(kArenaAllocProfile)))->second);
2347 }
2348
2349 // Mark a method as executed at least once.
AddMethod(MethodHotness::Flag flags,size_t index)2350 bool ProfileCompilationInfo::DexFileData::AddMethod(MethodHotness::Flag flags, size_t index) {
2351 if (index >= num_method_ids || index > kMaxSupportedMethodIndex) {
2352 LOG(ERROR) << "Invalid method index " << index << ". num_method_ids=" << num_method_ids
2353 << ", max: " << kMaxSupportedMethodIndex;
2354 return false;
2355 }
2356
2357 SetMethodHotness(index, flags);
2358
2359 if ((flags & MethodHotness::kFlagHot) != 0) {
2360 ProfileCompilationInfo::InlineCacheMap* result = FindOrAddHotMethod(index);
2361 DCHECK(result != nullptr);
2362 }
2363 return true;
2364 }
2365
SetMethodHotness(size_t index,MethodHotness::Flag flags)2366 void ProfileCompilationInfo::DexFileData::SetMethodHotness(size_t index,
2367 MethodHotness::Flag flags) {
2368 DCHECK_LT(index, num_method_ids);
2369 ForMethodBitmapHotnessFlags([&](MethodHotness::Flag flag) {
2370 if ((flags & flag) != 0) {
2371 method_bitmap.StoreBit(MethodFlagBitmapIndex(
2372 static_cast<MethodHotness::Flag>(flag), index), /*value=*/ true);
2373 }
2374 return true;
2375 });
2376 }
2377
GetHotnessInfo(uint32_t dex_method_index) const2378 ProfileCompilationInfo::MethodHotness ProfileCompilationInfo::DexFileData::GetHotnessInfo(
2379 uint32_t dex_method_index) const {
2380 MethodHotness ret;
2381 ForMethodBitmapHotnessFlags([&](MethodHotness::Flag flag) {
2382 if (method_bitmap.LoadBit(MethodFlagBitmapIndex(
2383 static_cast<MethodHotness::Flag>(flag), dex_method_index))) {
2384 ret.AddFlag(static_cast<MethodHotness::Flag>(flag));
2385 }
2386 return true;
2387 });
2388 auto it = method_map.find(dex_method_index);
2389 if (it != method_map.end()) {
2390 ret.SetInlineCacheMap(&it->second);
2391 ret.AddFlag(MethodHotness::kFlagHot);
2392 }
2393 return ret;
2394 }
2395
2396 // To simplify the implementation we use the MethodHotness flag values as indexes into the internal
2397 // bitmap representation. As such, they should never change unless the profile version is updated
2398 // and the implementation changed accordingly.
2399 static_assert(ProfileCompilationInfo::MethodHotness::kFlagFirst == 1 << 0);
2400 static_assert(ProfileCompilationInfo::MethodHotness::kFlagHot == 1 << 0);
2401 static_assert(ProfileCompilationInfo::MethodHotness::kFlagStartup == 1 << 1);
2402 static_assert(ProfileCompilationInfo::MethodHotness::kFlagPostStartup == 1 << 2);
2403 static_assert(ProfileCompilationInfo::MethodHotness::kFlagLastRegular == 1 << 2);
2404 static_assert(ProfileCompilationInfo::MethodHotness::kFlag32bit == 1 << 3);
2405 static_assert(ProfileCompilationInfo::MethodHotness::kFlag64bit == 1 << 4);
2406 static_assert(ProfileCompilationInfo::MethodHotness::kFlagSensitiveThread == 1 << 5);
2407 static_assert(ProfileCompilationInfo::MethodHotness::kFlagAmStartup == 1 << 6);
2408 static_assert(ProfileCompilationInfo::MethodHotness::kFlagAmPostStartup == 1 << 7);
2409 static_assert(ProfileCompilationInfo::MethodHotness::kFlagBoot == 1 << 8);
2410 static_assert(ProfileCompilationInfo::MethodHotness::kFlagPostBoot == 1 << 9);
2411 static_assert(ProfileCompilationInfo::MethodHotness::kFlagStartupBin == 1 << 10);
2412 static_assert(ProfileCompilationInfo::MethodHotness::kFlagStartupMaxBin == 1 << 15);
2413 static_assert(ProfileCompilationInfo::MethodHotness::kFlagLastBoot == 1 << 15);
2414
GetUsedBitmapFlags() const2415 uint16_t ProfileCompilationInfo::DexFileData::GetUsedBitmapFlags() const {
2416 uint32_t used_flags = 0u;
2417 ForMethodBitmapHotnessFlags([&](MethodHotness::Flag flag) {
2418 size_t index = FlagBitmapIndex(static_cast<MethodHotness::Flag>(flag));
2419 if (method_bitmap.HasSomeBitSet(index * num_method_ids, num_method_ids)) {
2420 used_flags |= flag;
2421 }
2422 return true;
2423 });
2424 return dchecked_integral_cast<uint16_t>(used_flags);
2425 }
2426
2427 ProfileCompilationInfo::DexPcData*
FindOrAddDexPc(InlineCacheMap * inline_cache,uint32_t dex_pc)2428 ProfileCompilationInfo::FindOrAddDexPc(InlineCacheMap* inline_cache, uint32_t dex_pc) {
2429 return &(inline_cache->FindOrAdd(dex_pc, DexPcData(inline_cache->get_allocator()))->second);
2430 }
2431
GetClassDescriptors(const std::vector<const DexFile * > & dex_files,const ProfileSampleAnnotation & annotation)2432 HashSet<std::string> ProfileCompilationInfo::GetClassDescriptors(
2433 const std::vector<const DexFile*>& dex_files,
2434 const ProfileSampleAnnotation& annotation) {
2435 HashSet<std::string> ret;
2436 for (const DexFile* dex_file : dex_files) {
2437 const DexFileData* data = FindDexDataUsingAnnotations(dex_file, annotation);
2438 if (data != nullptr) {
2439 for (dex::TypeIndex type_idx : data->class_set) {
2440 ret.insert(GetTypeDescriptor(dex_file, type_idx));
2441 }
2442 } else {
2443 VLOG(compiler) << "Failed to find profile data for " << dex_file->GetLocation();
2444 }
2445 }
2446 return ret;
2447 }
2448
IsProfileFile(int fd)2449 bool ProfileCompilationInfo::IsProfileFile(int fd) {
2450 // First check if it's an empty file as we allow empty profile files.
2451 // Profiles may be created by ActivityManager or installd before we manage to
2452 // process them in the runtime or profman.
2453 struct stat stat_buffer;
2454 if (fstat(fd, &stat_buffer) != 0) {
2455 return false;
2456 }
2457
2458 if (stat_buffer.st_size == 0) {
2459 return true;
2460 }
2461
2462 // The files is not empty. Check if it contains the profile magic.
2463 size_t byte_count = sizeof(kProfileMagic);
2464 uint8_t buffer[sizeof(kProfileMagic)];
2465 if (!android::base::ReadFullyAtOffset(fd, buffer, byte_count, /*offset=*/ 0)) {
2466 return false;
2467 }
2468
2469 // Reset the offset to prepare the file for reading.
2470 off_t rc = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET));
2471 if (rc == static_cast<off_t>(-1)) {
2472 PLOG(ERROR) << "Failed to reset the offset";
2473 return false;
2474 }
2475
2476 return memcmp(buffer, kProfileMagic, byte_count) == 0;
2477 }
2478
UpdateProfileKeys(const std::vector<std::unique_ptr<const DexFile>> & dex_files,bool * matched)2479 bool ProfileCompilationInfo::UpdateProfileKeys(
2480 const std::vector<std::unique_ptr<const DexFile>>& dex_files, /*out*/ bool* matched) {
2481 *matched = false;
2482 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
2483 for (const std::unique_ptr<DexFileData>& dex_data : info_) {
2484 if (dex_data->checksum == dex_file->GetLocationChecksum() &&
2485 dex_data->num_type_ids == dex_file->NumTypeIds() &&
2486 dex_data->num_method_ids == dex_file->NumMethodIds()) {
2487 std::string new_profile_key = GetProfileDexFileBaseKey(dex_file->GetLocation());
2488 std::string dex_data_base_key = GetBaseKeyFromAugmentedKey(dex_data->profile_key);
2489 if (dex_data_base_key != new_profile_key) {
2490 if (profile_key_map_.find(new_profile_key) != profile_key_map_.end()) {
2491 // We can't update the key if the new key belongs to a different dex file.
2492 LOG(ERROR) << "Cannot update profile key to " << new_profile_key
2493 << " because the new key belongs to another dex file.";
2494 return false;
2495 }
2496 profile_key_map_.erase(dex_data->profile_key);
2497 // Retain the annotation (if any) during the renaming by re-attaching the info
2498 // form the old key.
2499 dex_data->profile_key = MigrateAnnotationInfo(new_profile_key, dex_data->profile_key);
2500 profile_key_map_.Put(dex_data->profile_key, dex_data->profile_index);
2501 }
2502 *matched = true;
2503 }
2504 }
2505 }
2506 return true;
2507 }
2508
ProfileFilterFnAcceptAll(const std::string & dex_location ATTRIBUTE_UNUSED,uint32_t checksum ATTRIBUTE_UNUSED)2509 bool ProfileCompilationInfo::ProfileFilterFnAcceptAll(
2510 const std::string& dex_location ATTRIBUTE_UNUSED,
2511 uint32_t checksum ATTRIBUTE_UNUSED) {
2512 return true;
2513 }
2514
ClearData()2515 void ProfileCompilationInfo::ClearData() {
2516 profile_key_map_.clear();
2517 info_.clear();
2518 extra_descriptors_indexes_.clear();
2519 extra_descriptors_.clear();
2520 }
2521
ClearDataAndAdjustVersion(bool for_boot_image)2522 void ProfileCompilationInfo::ClearDataAndAdjustVersion(bool for_boot_image) {
2523 ClearData();
2524 memcpy(version_,
2525 for_boot_image ? kProfileVersionForBootImage : kProfileVersion,
2526 kProfileVersionSize);
2527 }
2528
IsForBootImage() const2529 bool ProfileCompilationInfo::IsForBootImage() const {
2530 return memcmp(version_, kProfileVersionForBootImage, sizeof(kProfileVersionForBootImage)) == 0;
2531 }
2532
GetVersion() const2533 const uint8_t* ProfileCompilationInfo::GetVersion() const {
2534 return version_;
2535 }
2536
ContainsClass(dex::TypeIndex type_index) const2537 bool ProfileCompilationInfo::DexFileData::ContainsClass(dex::TypeIndex type_index) const {
2538 return class_set.find(type_index) != class_set.end();
2539 }
2540
ClassesDataSize() const2541 uint32_t ProfileCompilationInfo::DexFileData::ClassesDataSize() const {
2542 return class_set.empty()
2543 ? 0u
2544 : sizeof(ProfileIndexType) + // Which dex file.
2545 sizeof(uint16_t) + // Number of classes.
2546 sizeof(uint16_t) * class_set.size(); // Type index diffs.
2547 }
2548
WriteClasses(SafeBuffer & buffer) const2549 void ProfileCompilationInfo::DexFileData::WriteClasses(SafeBuffer& buffer) const {
2550 if (class_set.empty()) {
2551 return;
2552 }
2553 buffer.WriteUintAndAdvance(profile_index);
2554 buffer.WriteUintAndAdvance(dchecked_integral_cast<uint16_t>(class_set.size()));
2555 WriteClassSet(buffer, class_set);
2556 }
2557
ReadClasses(SafeBuffer & buffer,const dchecked_vector<ExtraDescriptorIndex> & extra_descriptors_remap,std::string * error)2558 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::DexFileData::ReadClasses(
2559 SafeBuffer& buffer,
2560 const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
2561 std::string* error) {
2562 uint16_t classes_size;
2563 if (!buffer.ReadUintAndAdvance(&classes_size)) {
2564 *error = "Error reading classes size.";
2565 return ProfileLoadStatus::kBadData;
2566 }
2567 uint16_t num_valid_type_indexes = dchecked_integral_cast<uint16_t>(
2568 std::min<size_t>(num_type_ids + extra_descriptors_remap.size(), DexFile::kDexNoIndex16));
2569 uint16_t type_index = 0u;
2570 for (size_t i = 0; i != classes_size; ++i) {
2571 uint16_t type_index_diff;
2572 if (!buffer.ReadUintAndAdvance(&type_index_diff)) {
2573 *error = "Error reading class type index diff.";
2574 return ProfileLoadStatus::kBadData;
2575 }
2576 if (type_index_diff == 0u && i != 0u) {
2577 *error = "Duplicate type index.";
2578 return ProfileLoadStatus::kBadData;
2579 }
2580 if (type_index_diff >= num_valid_type_indexes - type_index) {
2581 *error = "Invalid type index.";
2582 return ProfileLoadStatus::kBadData;
2583 }
2584 type_index += type_index_diff;
2585 if (type_index >= num_type_ids) {
2586 uint32_t new_extra_descriptor_index = extra_descriptors_remap[type_index - num_type_ids];
2587 if (new_extra_descriptor_index >= DexFile::kDexNoIndex16 - num_type_ids) {
2588 *error = "Remapped type index out of range.";
2589 return ProfileLoadStatus::kMergeError;
2590 }
2591 class_set.insert(dex::TypeIndex(num_type_ids + new_extra_descriptor_index));
2592 } else {
2593 class_set.insert(dex::TypeIndex(type_index));
2594 }
2595 }
2596 return ProfileLoadStatus::kSuccess;
2597 }
2598
SkipClasses(SafeBuffer & buffer,std::string * error)2599 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::DexFileData::SkipClasses(
2600 SafeBuffer& buffer,
2601 std::string* error) {
2602 uint16_t classes_size;
2603 if (!buffer.ReadUintAndAdvance(&classes_size)) {
2604 *error = "Error reading classes size to skip.";
2605 return ProfileLoadStatus::kBadData;
2606 }
2607 size_t following_data_size = static_cast<size_t>(classes_size) * sizeof(uint16_t);
2608 if (following_data_size > buffer.GetAvailableBytes()) {
2609 *error = "Classes data size to skip exceeds remaining data.";
2610 return ProfileLoadStatus::kBadData;
2611 }
2612 buffer.Advance(following_data_size);
2613 return ProfileLoadStatus::kSuccess;
2614 }
2615
MethodsDataSize(uint16_t * method_flags,size_t * saved_bitmap_bit_size) const2616 uint32_t ProfileCompilationInfo::DexFileData::MethodsDataSize(
2617 /*out*/ uint16_t* method_flags,
2618 /*out*/ size_t* saved_bitmap_bit_size) const {
2619 uint16_t local_method_flags = GetUsedBitmapFlags();
2620 size_t local_saved_bitmap_bit_size = POPCOUNT(local_method_flags) * num_method_ids;
2621 if (!method_map.empty()) {
2622 local_method_flags |= enum_cast<uint16_t>(MethodHotness::kFlagHot);
2623 }
2624 size_t size = 0u;
2625 if (local_method_flags != 0u) {
2626 size_t num_hot_methods = method_map.size();
2627 size_t num_dex_pc_entries = 0u;
2628 size_t num_class_entries = 0u;
2629 for (const auto& method_entry : method_map) {
2630 const InlineCacheMap& inline_cache_map = method_entry.second;
2631 num_dex_pc_entries += inline_cache_map.size();
2632 for (const auto& inline_cache_entry : inline_cache_map) {
2633 const DexPcData& dex_pc_data = inline_cache_entry.second;
2634 num_class_entries += dex_pc_data.classes.size();
2635 }
2636 }
2637
2638 constexpr size_t kPerHotMethodSize =
2639 sizeof(uint16_t) + // Method index diff.
2640 sizeof(uint16_t); // Inline cache size.
2641 constexpr size_t kPerDexPcEntrySize =
2642 sizeof(uint16_t) + // Dex PC.
2643 sizeof(uint8_t); // Number of inline cache classes.
2644 constexpr size_t kPerClassEntrySize =
2645 sizeof(uint16_t); // Type index diff.
2646
2647 size_t saved_bitmap_byte_size = BitsToBytesRoundUp(local_saved_bitmap_bit_size);
2648 size = sizeof(ProfileIndexType) + // Which dex file.
2649 sizeof(uint32_t) + // Total size of following data.
2650 sizeof(uint16_t) + // Method flags.
2651 saved_bitmap_byte_size + // Bitmap data.
2652 num_hot_methods * kPerHotMethodSize + // Data for hot methods.
2653 num_dex_pc_entries * kPerDexPcEntrySize + // Data for dex pc entries.
2654 num_class_entries * kPerClassEntrySize; // Data for inline cache class entries.
2655 }
2656 if (method_flags != nullptr) {
2657 *method_flags = local_method_flags;
2658 }
2659 if (saved_bitmap_bit_size != nullptr) {
2660 *saved_bitmap_bit_size = local_saved_bitmap_bit_size;
2661 }
2662 return size;
2663 }
2664
WriteMethods(SafeBuffer & buffer) const2665 void ProfileCompilationInfo::DexFileData::WriteMethods(SafeBuffer& buffer) const {
2666 uint16_t method_flags;
2667 size_t saved_bitmap_bit_size;
2668 uint32_t methods_data_size = MethodsDataSize(&method_flags, &saved_bitmap_bit_size);
2669 if (methods_data_size == 0u) {
2670 return; // No data to write.
2671 }
2672 DCHECK_GE(buffer.GetAvailableBytes(), methods_data_size);
2673 uint32_t expected_available_bytes_at_end = buffer.GetAvailableBytes() - methods_data_size;
2674
2675 // Write the profile index.
2676 buffer.WriteUintAndAdvance(profile_index);
2677 // Write the total size of the following methods data (without the profile index
2678 // and the total size itself) for easy skipping when the dex file is filtered out.
2679 uint32_t following_data_size = methods_data_size - sizeof(ProfileIndexType) - sizeof(uint32_t);
2680 buffer.WriteUintAndAdvance(following_data_size);
2681 // Write the used method flags.
2682 buffer.WriteUintAndAdvance(method_flags);
2683
2684 // Write the bitmap data.
2685 size_t saved_bitmap_byte_size = BitsToBytesRoundUp(saved_bitmap_bit_size);
2686 DCHECK_LE(saved_bitmap_byte_size, buffer.GetAvailableBytes());
2687 BitMemoryRegion saved_bitmap(buffer.GetCurrentPtr(), /*bit_start=*/ 0, saved_bitmap_bit_size);
2688 size_t saved_bitmap_index = 0u;
2689 ForMethodBitmapHotnessFlags([&](MethodHotness::Flag flag) {
2690 if ((method_flags & flag) != 0u) {
2691 size_t index = FlagBitmapIndex(static_cast<MethodHotness::Flag>(flag));
2692 BitMemoryRegion src = method_bitmap.Subregion(index * num_method_ids, num_method_ids);
2693 saved_bitmap.Subregion(saved_bitmap_index * num_method_ids, num_method_ids).CopyBits(src);
2694 ++saved_bitmap_index;
2695 }
2696 return true;
2697 });
2698 DCHECK_EQ(saved_bitmap_index * num_method_ids, saved_bitmap_bit_size);
2699 // Clear the padding bits.
2700 size_t padding_bit_size = saved_bitmap_byte_size * kBitsPerByte - saved_bitmap_bit_size;
2701 BitMemoryRegion padding_region(buffer.GetCurrentPtr(), saved_bitmap_bit_size, padding_bit_size);
2702 padding_region.StoreBits(/*bit_offset=*/ 0u, /*value=*/ 0u, /*bit_length=*/ padding_bit_size);
2703 buffer.Advance(saved_bitmap_byte_size);
2704
2705 uint16_t last_method_index = 0;
2706 for (const auto& method_entry : method_map) {
2707 uint16_t method_index = method_entry.first;
2708 const InlineCacheMap& inline_cache_map = method_entry.second;
2709
2710 // Store the difference between the method indices for better compression.
2711 // The SafeMap is ordered by method_id, so the difference will always be non negative.
2712 DCHECK_GE(method_index, last_method_index);
2713 uint16_t diff_with_last_method_index = method_index - last_method_index;
2714 last_method_index = method_index;
2715 buffer.WriteUintAndAdvance(diff_with_last_method_index);
2716
2717 // Add inline cache map size.
2718 buffer.WriteUintAndAdvance(dchecked_integral_cast<uint16_t>(inline_cache_map.size()));
2719
2720 // Add inline cache entries.
2721 for (const auto& inline_cache_entry : inline_cache_map) {
2722 uint16_t dex_pc = inline_cache_entry.first;
2723 const DexPcData& dex_pc_data = inline_cache_entry.second;
2724 const ArenaSet<dex::TypeIndex>& classes = dex_pc_data.classes;
2725
2726 // Add the dex pc.
2727 buffer.WriteUintAndAdvance(dex_pc);
2728
2729 // Add the megamorphic/missing_types encoding if needed and continue.
2730 // In either cases we don't add any classes to the profiles and so there's
2731 // no point to continue.
2732 // TODO: in case we miss types there is still value to add the rest of the
2733 // classes. (This requires changing profile version or using a new section type.)
2734 if (dex_pc_data.is_missing_types) {
2735 // At this point the megamorphic flag should not be set.
2736 DCHECK(!dex_pc_data.is_megamorphic);
2737 DCHECK_EQ(classes.size(), 0u);
2738 buffer.WriteUintAndAdvance(kIsMissingTypesEncoding);
2739 continue;
2740 } else if (dex_pc_data.is_megamorphic) {
2741 DCHECK_EQ(classes.size(), 0u);
2742 buffer.WriteUintAndAdvance(kIsMegamorphicEncoding);
2743 continue;
2744 }
2745
2746 DCHECK_LT(classes.size(), ProfileCompilationInfo::kIndividualInlineCacheSize);
2747 DCHECK_NE(classes.size(), 0u) << "InlineCache contains a dex_pc with 0 classes";
2748
2749 // Add the number of classes for the dex PC.
2750 buffer.WriteUintAndAdvance(dchecked_integral_cast<uint8_t>(classes.size()));
2751 // Store the class set.
2752 WriteClassSet(buffer, classes);
2753 }
2754 }
2755
2756 // Check if we've written the right number of bytes.
2757 DCHECK_EQ(buffer.GetAvailableBytes(), expected_available_bytes_at_end);
2758 }
2759
ReadMethods(SafeBuffer & buffer,const dchecked_vector<ExtraDescriptorIndex> & extra_descriptors_remap,std::string * error)2760 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::DexFileData::ReadMethods(
2761 SafeBuffer& buffer,
2762 const dchecked_vector<ExtraDescriptorIndex>& extra_descriptors_remap,
2763 std::string* error) {
2764 uint32_t following_data_size;
2765 if (!buffer.ReadUintAndAdvance(&following_data_size)) {
2766 *error = "Error reading methods data size.";
2767 return ProfileLoadStatus::kBadData;
2768 }
2769 if (following_data_size > buffer.GetAvailableBytes()) {
2770 *error = "Methods data size exceeds available data size.";
2771 return ProfileLoadStatus::kBadData;
2772 }
2773 uint32_t expected_available_bytes_at_end = buffer.GetAvailableBytes() - following_data_size;
2774
2775 // Read method flags.
2776 uint16_t method_flags;
2777 if (!buffer.ReadUintAndAdvance(&method_flags)) {
2778 *error = "Error reading method flags.";
2779 return ProfileLoadStatus::kBadData;
2780 }
2781 if (!is_for_boot_image && method_flags >= (MethodHotness::kFlagLastRegular << 1)) {
2782 // The profile we're loading contains data for boot image.
2783 *error = "Method flags contain boot image profile flags for non-boot image profile.";
2784 return ProfileLoadStatus::kBadData;
2785 }
2786
2787 // Read method bitmap.
2788 size_t saved_bitmap_bit_size = POPCOUNT(method_flags & ~MethodHotness::kFlagHot) * num_method_ids;
2789 size_t saved_bitmap_byte_size = BitsToBytesRoundUp(saved_bitmap_bit_size);
2790 if (sizeof(uint16_t) + saved_bitmap_byte_size > following_data_size) {
2791 *error = "Insufficient available data for method bitmap.";
2792 return ProfileLoadStatus::kBadData;
2793 }
2794 BitMemoryRegion saved_bitmap(buffer.GetCurrentPtr(), /*bit_start=*/ 0, saved_bitmap_bit_size);
2795 size_t saved_bitmap_index = 0u;
2796 ForMethodBitmapHotnessFlags([&](MethodHotness::Flag flag) {
2797 if ((method_flags & flag) != 0u) {
2798 size_t index = FlagBitmapIndex(static_cast<MethodHotness::Flag>(flag));
2799 BitMemoryRegion src =
2800 saved_bitmap.Subregion(saved_bitmap_index * num_method_ids, num_method_ids);
2801 method_bitmap.Subregion(index * num_method_ids, num_method_ids).OrBits(src);
2802 ++saved_bitmap_index;
2803 }
2804 return true;
2805 });
2806 buffer.Advance(saved_bitmap_byte_size);
2807
2808 // Load hot methods.
2809 if ((method_flags & MethodHotness::kFlagHot) != 0u) {
2810 uint32_t num_valid_method_indexes =
2811 std::min<uint32_t>(kMaxSupportedMethodIndex + 1u, num_method_ids);
2812 uint16_t num_valid_type_indexes = dchecked_integral_cast<uint16_t>(
2813 std::min<size_t>(num_type_ids + extra_descriptors_remap.size(), DexFile::kDexNoIndex16));
2814 uint16_t method_index = 0;
2815 bool first_diff = true;
2816 while (buffer.GetAvailableBytes() > expected_available_bytes_at_end) {
2817 uint16_t diff_with_last_method_index;
2818 if (!buffer.ReadUintAndAdvance(&diff_with_last_method_index)) {
2819 *error = "Error reading method index diff.";
2820 return ProfileLoadStatus::kBadData;
2821 }
2822 if (diff_with_last_method_index == 0u && !first_diff) {
2823 *error = "Duplicate method index.";
2824 return ProfileLoadStatus::kBadData;
2825 }
2826 first_diff = false;
2827 if (diff_with_last_method_index >= num_valid_method_indexes - method_index) {
2828 *error = "Invalid method index.";
2829 return ProfileLoadStatus::kBadData;
2830 }
2831 method_index += diff_with_last_method_index;
2832 InlineCacheMap* inline_cache = FindOrAddHotMethod(method_index);
2833 DCHECK(inline_cache != nullptr);
2834
2835 // Load inline cache map size.
2836 uint16_t inline_cache_size;
2837 if (!buffer.ReadUintAndAdvance(&inline_cache_size)) {
2838 *error = "Error reading inline cache size.";
2839 return ProfileLoadStatus::kBadData;
2840 }
2841 for (uint16_t ic_index = 0; ic_index != inline_cache_size; ++ic_index) {
2842 // Load dex pc.
2843 uint16_t dex_pc;
2844 if (!buffer.ReadUintAndAdvance(&dex_pc)) {
2845 *error = "Error reading inline cache dex pc.";
2846 return ProfileLoadStatus::kBadData;
2847 }
2848 DexPcData* dex_pc_data = FindOrAddDexPc(inline_cache, dex_pc);
2849 DCHECK(dex_pc_data != nullptr);
2850
2851 // Load inline cache classes.
2852 uint8_t inline_cache_classes_size;
2853 if (!buffer.ReadUintAndAdvance(&inline_cache_classes_size)) {
2854 *error = "Error reading inline cache classes size.";
2855 return ProfileLoadStatus::kBadData;
2856 }
2857 if (inline_cache_classes_size == kIsMissingTypesEncoding) {
2858 dex_pc_data->SetIsMissingTypes();
2859 } else if (inline_cache_classes_size == kIsMegamorphicEncoding) {
2860 dex_pc_data->SetIsMegamorphic();
2861 } else if (inline_cache_classes_size >= kIndividualInlineCacheSize) {
2862 *error = "Inline cache size too large.";
2863 return ProfileLoadStatus::kBadData;
2864 } else {
2865 uint16_t type_index = 0u;
2866 for (size_t i = 0; i != inline_cache_classes_size; ++i) {
2867 uint16_t type_index_diff;
2868 if (!buffer.ReadUintAndAdvance(&type_index_diff)) {
2869 *error = "Error reading inline cache type index diff.";
2870 return ProfileLoadStatus::kBadData;
2871 }
2872 if (type_index_diff == 0u && i != 0u) {
2873 *error = "Duplicate inline cache type index.";
2874 return ProfileLoadStatus::kBadData;
2875 }
2876 if (type_index_diff >= num_valid_type_indexes - type_index) {
2877 *error = "Invalid inline cache type index.";
2878 return ProfileLoadStatus::kBadData;
2879 }
2880 type_index += type_index_diff;
2881 if (type_index >= num_type_ids) {
2882 ExtraDescriptorIndex new_extra_descriptor_index =
2883 extra_descriptors_remap[type_index - num_type_ids];
2884 if (new_extra_descriptor_index >= DexFile::kDexNoIndex16 - num_type_ids) {
2885 *error = "Remapped inline cache type index out of range.";
2886 return ProfileLoadStatus::kMergeError;
2887 }
2888 dex_pc_data->AddClass(dex::TypeIndex(num_type_ids + new_extra_descriptor_index));
2889 } else {
2890 dex_pc_data->AddClass(dex::TypeIndex(type_index));
2891 }
2892 }
2893 }
2894 }
2895 }
2896 }
2897
2898 if (buffer.GetAvailableBytes() != expected_available_bytes_at_end) {
2899 *error = "Methods data did not end at expected position.";
2900 return ProfileLoadStatus::kBadData;
2901 }
2902
2903 return ProfileLoadStatus::kSuccess;
2904 }
2905
SkipMethods(SafeBuffer & buffer,std::string * error)2906 ProfileCompilationInfo::ProfileLoadStatus ProfileCompilationInfo::DexFileData::SkipMethods(
2907 SafeBuffer& buffer,
2908 std::string* error) {
2909 uint32_t following_data_size;
2910 if (!buffer.ReadUintAndAdvance(&following_data_size)) {
2911 *error = "Error reading methods data size to skip.";
2912 return ProfileLoadStatus::kBadData;
2913 }
2914 if (following_data_size > buffer.GetAvailableBytes()) {
2915 *error = "Methods data size to skip exceeds remaining data.";
2916 return ProfileLoadStatus::kBadData;
2917 }
2918 buffer.Advance(following_data_size);
2919 return ProfileLoadStatus::kSuccess;
2920 }
2921
WriteClassSet(SafeBuffer & buffer,const ArenaSet<dex::TypeIndex> & class_set)2922 void ProfileCompilationInfo::DexFileData::WriteClassSet(
2923 SafeBuffer& buffer,
2924 const ArenaSet<dex::TypeIndex>& class_set) {
2925 // Store the difference between the type indexes for better compression.
2926 uint16_t last_type_index = 0u;
2927 for (const dex::TypeIndex& type_index : class_set) {
2928 DCHECK_GE(type_index.index_, last_type_index);
2929 uint16_t diff_with_last_type_index = type_index.index_ - last_type_index;
2930 last_type_index = type_index.index_;
2931 buffer.WriteUintAndAdvance(diff_with_last_type_index);
2932 }
2933 }
2934
GetSizeWarningThresholdBytes() const2935 size_t ProfileCompilationInfo::GetSizeWarningThresholdBytes() const {
2936 return IsForBootImage() ? kSizeWarningThresholdBootBytes : kSizeWarningThresholdBytes;
2937 }
2938
GetSizeErrorThresholdBytes() const2939 size_t ProfileCompilationInfo::GetSizeErrorThresholdBytes() const {
2940 return IsForBootImage() ? kSizeErrorThresholdBootBytes : kSizeErrorThresholdBytes;
2941 }
2942
operator <<(std::ostream & stream,ProfileCompilationInfo::DexReferenceDumper dumper)2943 std::ostream& operator<<(std::ostream& stream,
2944 ProfileCompilationInfo::DexReferenceDumper dumper) {
2945 stream << "[profile_key=" << dumper.GetProfileKey()
2946 << ",dex_checksum=" << std::hex << dumper.GetDexChecksum() << std::dec
2947 << ",num_type_ids=" << dumper.GetNumTypeIds()
2948 << ",num_method_ids=" << dumper.GetNumMethodIds()
2949 << "]";
2950 return stream;
2951 }
2952
FlattenProfileData()2953 FlattenProfileData::FlattenProfileData() :
2954 max_aggregation_for_methods_(0),
2955 max_aggregation_for_classes_(0) {
2956 }
2957
ItemMetadata()2958 FlattenProfileData::ItemMetadata::ItemMetadata() :
2959 flags_(0) {
2960 }
2961
ItemMetadata(const ItemMetadata & other)2962 FlattenProfileData::ItemMetadata::ItemMetadata(const ItemMetadata& other) :
2963 flags_(other.flags_),
2964 annotations_(other.annotations_) {
2965 }
2966
ExtractProfileData(const std::vector<std::unique_ptr<const DexFile>> & dex_files) const2967 std::unique_ptr<FlattenProfileData> ProfileCompilationInfo::ExtractProfileData(
2968 const std::vector<std::unique_ptr<const DexFile>>& dex_files) const {
2969
2970 std::unique_ptr<FlattenProfileData> result(new FlattenProfileData());
2971
2972 auto create_metadata_fn = []() { return FlattenProfileData::ItemMetadata(); };
2973
2974 // Iterate through all the dex files, find the methods/classes associated with each of them,
2975 // and add them to the flatten result.
2976 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
2977 // Find all the dex data for the given dex file.
2978 // We may have multiple dex data if the methods or classes were added using
2979 // different annotations.
2980 std::vector<const DexFileData*> all_dex_data;
2981 FindAllDexData(dex_file.get(), &all_dex_data);
2982 for (const DexFileData* dex_data : all_dex_data) {
2983 // Extract the annotation from the key as we want to store it in the flatten result.
2984 ProfileSampleAnnotation annotation = GetAnnotationFromKey(dex_data->profile_key);
2985
2986 // Check which methods from the current dex files are in the profile.
2987 for (uint32_t method_idx = 0; method_idx < dex_data->num_method_ids; ++method_idx) {
2988 MethodHotness hotness = dex_data->GetHotnessInfo(method_idx);
2989 if (!hotness.IsInProfile()) {
2990 // Not in the profile, continue.
2991 continue;
2992 }
2993 // The method is in the profile, create metadata item for it and added to the result.
2994 MethodReference ref(dex_file.get(), method_idx);
2995 FlattenProfileData::ItemMetadata& metadata =
2996 result->method_metadata_.GetOrCreate(ref, create_metadata_fn);
2997 metadata.flags_ |= hotness.flags_;
2998 metadata.annotations_.push_back(annotation);
2999 // Update the max aggregation counter for methods.
3000 // This is essentially a cache, to avoid traversing all the methods just to find out
3001 // this value.
3002 result->max_aggregation_for_methods_ = std::max(
3003 result->max_aggregation_for_methods_,
3004 static_cast<uint32_t>(metadata.annotations_.size()));
3005 }
3006
3007 // Check which classes from the current dex files are in the profile.
3008 for (const dex::TypeIndex& type_index : dex_data->class_set) {
3009 if (type_index.index_ >= dex_file->NumTypeIds()) {
3010 // Not a valid `dex::TypeIndex` for `TypeReference`.
3011 // TODO: Rewrite the API to use descriptors or the `ProfileCompilationInfo` directly
3012 // instead of the `FlattenProfileData` helper class.
3013 continue;
3014 }
3015 TypeReference ref(dex_file.get(), type_index);
3016 FlattenProfileData::ItemMetadata& metadata =
3017 result->class_metadata_.GetOrCreate(ref, create_metadata_fn);
3018 metadata.annotations_.push_back(annotation);
3019 // Update the max aggregation counter for classes.
3020 result->max_aggregation_for_classes_ = std::max(
3021 result->max_aggregation_for_classes_,
3022 static_cast<uint32_t>(metadata.annotations_.size()));
3023 }
3024 }
3025 }
3026
3027 return result;
3028 }
3029
MergeData(const FlattenProfileData & other)3030 void FlattenProfileData::MergeData(const FlattenProfileData& other) {
3031 auto create_metadata_fn = []() { return FlattenProfileData::ItemMetadata(); };
3032 for (const auto& it : other.method_metadata_) {
3033 const MethodReference& otherRef = it.first;
3034 const FlattenProfileData::ItemMetadata otherData = it.second;
3035 const std::list<ProfileCompilationInfo::ProfileSampleAnnotation>& other_annotations =
3036 otherData.GetAnnotations();
3037
3038 FlattenProfileData::ItemMetadata& metadata =
3039 method_metadata_.GetOrCreate(otherRef, create_metadata_fn);
3040 metadata.flags_ |= otherData.GetFlags();
3041 metadata.annotations_.insert(
3042 metadata.annotations_.end(), other_annotations.begin(), other_annotations.end());
3043
3044 max_aggregation_for_methods_ = std::max(
3045 max_aggregation_for_methods_,
3046 static_cast<uint32_t>(metadata.annotations_.size()));
3047 }
3048 for (const auto& it : other.class_metadata_) {
3049 const TypeReference& otherRef = it.first;
3050 const FlattenProfileData::ItemMetadata otherData = it.second;
3051 const std::list<ProfileCompilationInfo::ProfileSampleAnnotation>& other_annotations =
3052 otherData.GetAnnotations();
3053
3054 FlattenProfileData::ItemMetadata& metadata =
3055 class_metadata_.GetOrCreate(otherRef, create_metadata_fn);
3056 metadata.flags_ |= otherData.GetFlags();
3057 metadata.annotations_.insert(
3058 metadata.annotations_.end(), other_annotations.begin(), other_annotations.end());
3059
3060 max_aggregation_for_classes_ = std::max(
3061 max_aggregation_for_classes_,
3062 static_cast<uint32_t>(metadata.annotations_.size()));
3063 }
3064 }
3065
3066 } // namespace art
3067