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 "offline_profiling_info.h"
18
19 #include "errno.h"
20 #include <limits.h>
21 #include <vector>
22 #include <sys/file.h>
23 #include <sys/stat.h>
24 #include <sys/uio.h>
25
26 #include "art_method-inl.h"
27 #include "base/mutex.h"
28 #include "base/scoped_flock.h"
29 #include "base/stl_util.h"
30 #include "base/systrace.h"
31 #include "base/unix_file/fd_file.h"
32 #include "jit/profiling_info.h"
33 #include "os.h"
34 #include "safe_map.h"
35
36 namespace art {
37
38 const uint8_t ProfileCompilationInfo::kProfileMagic[] = { 'p', 'r', 'o', '\0' };
39 const uint8_t ProfileCompilationInfo::kProfileVersion[] = { '0', '0', '1', '\0' };
40
41 static constexpr uint16_t kMaxDexFileKeyLength = PATH_MAX;
42
43 // Transform the actual dex location into relative paths.
44 // Note: this is OK because we don't store profiles of different apps into the same file.
45 // Apps with split apks don't cause trouble because each split has a different name and will not
46 // collide with other entries.
GetProfileDexFileKey(const std::string & dex_location)47 std::string ProfileCompilationInfo::GetProfileDexFileKey(const std::string& dex_location) {
48 DCHECK(!dex_location.empty());
49 size_t last_sep_index = dex_location.find_last_of('/');
50 if (last_sep_index == std::string::npos) {
51 return dex_location;
52 } else {
53 DCHECK(last_sep_index < dex_location.size());
54 return dex_location.substr(last_sep_index + 1);
55 }
56 }
57
AddMethodsAndClasses(const std::vector<MethodReference> & methods,const std::set<DexCacheResolvedClasses> & resolved_classes)58 bool ProfileCompilationInfo::AddMethodsAndClasses(
59 const std::vector<MethodReference>& methods,
60 const std::set<DexCacheResolvedClasses>& resolved_classes) {
61 for (const MethodReference& method : methods) {
62 if (!AddMethodIndex(GetProfileDexFileKey(method.dex_file->GetLocation()),
63 method.dex_file->GetLocationChecksum(),
64 method.dex_method_index)) {
65 return false;
66 }
67 }
68 for (const DexCacheResolvedClasses& dex_cache : resolved_classes) {
69 if (!AddResolvedClasses(dex_cache)) {
70 return false;
71 }
72 }
73 return true;
74 }
75
MergeAndSave(const std::string & filename,uint64_t * bytes_written,bool force)76 bool ProfileCompilationInfo::MergeAndSave(const std::string& filename,
77 uint64_t* bytes_written,
78 bool force) {
79 ScopedTrace trace(__PRETTY_FUNCTION__);
80 ScopedFlock flock;
81 std::string error;
82 if (!flock.Init(filename.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC, /* block */ false, &error)) {
83 LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
84 return false;
85 }
86
87 int fd = flock.GetFile()->Fd();
88
89 // Load the file but keep a copy around to be able to infer if the content has changed.
90 ProfileCompilationInfo fileInfo;
91 ProfileLoadSatus status = fileInfo.LoadInternal(fd, &error);
92 if (status == kProfileLoadSuccess) {
93 // Merge the content of file into the current object.
94 if (MergeWith(fileInfo)) {
95 // If after the merge we have the same data as what is the file there's no point
96 // in actually doing the write. The file will be exactly the same as before.
97 if (Equals(fileInfo)) {
98 if (bytes_written != nullptr) {
99 *bytes_written = 0;
100 }
101 return true;
102 }
103 } else {
104 LOG(WARNING) << "Could not merge previous profile data from file " << filename;
105 if (!force) {
106 return false;
107 }
108 }
109 } else if (force &&
110 ((status == kProfileLoadVersionMismatch) || (status == kProfileLoadBadData))) {
111 // Log a warning but don't return false. We will clear the profile anyway.
112 LOG(WARNING) << "Clearing bad or obsolete profile data from file "
113 << filename << ": " << error;
114 } else {
115 LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error;
116 return false;
117 }
118
119 // We need to clear the data because we don't support appending to the profiles yet.
120 if (!flock.GetFile()->ClearContent()) {
121 PLOG(WARNING) << "Could not clear profile file: " << filename;
122 return false;
123 }
124
125 // This doesn't need locking because we are trying to lock the file for exclusive
126 // access and fail immediately if we can't.
127 bool result = Save(fd);
128 if (result) {
129 VLOG(profiler) << "Successfully saved profile info to " << filename
130 << " Size: " << GetFileSizeBytes(filename);
131 if (bytes_written != nullptr) {
132 *bytes_written = GetFileSizeBytes(filename);
133 }
134 } else {
135 VLOG(profiler) << "Failed to save profile info to " << filename;
136 }
137 return result;
138 }
139
140 // Returns true if all the bytes were successfully written to the file descriptor.
WriteBuffer(int fd,const uint8_t * buffer,size_t byte_count)141 static bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) {
142 while (byte_count > 0) {
143 int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count));
144 if (bytes_written == -1) {
145 return false;
146 }
147 byte_count -= bytes_written; // Reduce the number of remaining bytes.
148 buffer += bytes_written; // Move the buffer forward.
149 }
150 return true;
151 }
152
153 // Add the string bytes to the buffer.
AddStringToBuffer(std::vector<uint8_t> * buffer,const std::string & value)154 static void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) {
155 buffer->insert(buffer->end(), value.begin(), value.end());
156 }
157
158 // Insert each byte, from low to high into the buffer.
159 template <typename T>
AddUintToBuffer(std::vector<uint8_t> * buffer,T value)160 static void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) {
161 for (size_t i = 0; i < sizeof(T); i++) {
162 buffer->push_back((value >> (i * kBitsPerByte)) & 0xff);
163 }
164 }
165
166 static constexpr size_t kLineHeaderSize =
167 3 * sizeof(uint16_t) + // method_set.size + class_set.size + dex_location.size
168 sizeof(uint32_t); // checksum
169
170 /**
171 * Serialization format:
172 * magic,version,number_of_lines
173 * dex_location1,number_of_methods1,number_of_classes1,dex_location_checksum1, \
174 * method_id11,method_id12...,class_id1,class_id2...
175 * dex_location2,number_of_methods2,number_of_classes2,dex_location_checksum2, \
176 * method_id21,method_id22...,,class_id1,class_id2...
177 * .....
178 **/
Save(int fd)179 bool ProfileCompilationInfo::Save(int fd) {
180 ScopedTrace trace(__PRETTY_FUNCTION__);
181 DCHECK_GE(fd, 0);
182
183 // Cache at most 5KB before writing.
184 static constexpr size_t kMaxSizeToKeepBeforeWriting = 5 * KB;
185 // Use a vector wrapper to avoid keeping track of offsets when we add elements.
186 std::vector<uint8_t> buffer;
187 WriteBuffer(fd, kProfileMagic, sizeof(kProfileMagic));
188 WriteBuffer(fd, kProfileVersion, sizeof(kProfileVersion));
189 AddUintToBuffer(&buffer, static_cast<uint16_t>(info_.size()));
190
191 for (const auto& it : info_) {
192 if (buffer.size() > kMaxSizeToKeepBeforeWriting) {
193 if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
194 return false;
195 }
196 buffer.clear();
197 }
198 const std::string& dex_location = it.first;
199 const DexFileData& dex_data = it.second;
200 if (dex_data.method_set.empty() && dex_data.class_set.empty()) {
201 continue;
202 }
203
204 if (dex_location.size() >= kMaxDexFileKeyLength) {
205 LOG(WARNING) << "DexFileKey exceeds allocated limit";
206 return false;
207 }
208
209 // Make sure that the buffer has enough capacity to avoid repeated resizings
210 // while we add data.
211 size_t required_capacity = buffer.size() +
212 kLineHeaderSize +
213 dex_location.size() +
214 sizeof(uint16_t) * (dex_data.class_set.size() + dex_data.method_set.size());
215
216 buffer.reserve(required_capacity);
217
218 DCHECK_LE(dex_location.size(), std::numeric_limits<uint16_t>::max());
219 DCHECK_LE(dex_data.method_set.size(), std::numeric_limits<uint16_t>::max());
220 DCHECK_LE(dex_data.class_set.size(), std::numeric_limits<uint16_t>::max());
221 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_location.size()));
222 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.method_set.size()));
223 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.class_set.size()));
224 AddUintToBuffer(&buffer, dex_data.checksum); // uint32_t
225
226 AddStringToBuffer(&buffer, dex_location);
227
228 for (auto method_it : dex_data.method_set) {
229 AddUintToBuffer(&buffer, method_it);
230 }
231 for (auto class_id : dex_data.class_set) {
232 AddUintToBuffer(&buffer, class_id);
233 }
234 DCHECK_EQ(required_capacity, buffer.size())
235 << "Failed to add the expected number of bytes in the buffer";
236 }
237
238 return WriteBuffer(fd, buffer.data(), buffer.size());
239 }
240
GetOrAddDexFileData(const std::string & dex_location,uint32_t checksum)241 ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData(
242 const std::string& dex_location,
243 uint32_t checksum) {
244 auto info_it = info_.find(dex_location);
245 if (info_it == info_.end()) {
246 info_it = info_.Put(dex_location, DexFileData(checksum));
247 }
248 if (info_it->second.checksum != checksum) {
249 LOG(WARNING) << "Checksum mismatch for dex " << dex_location;
250 return nullptr;
251 }
252 return &info_it->second;
253 }
254
AddResolvedClasses(const DexCacheResolvedClasses & classes)255 bool ProfileCompilationInfo::AddResolvedClasses(const DexCacheResolvedClasses& classes) {
256 const std::string dex_location = GetProfileDexFileKey(classes.GetDexLocation());
257 const uint32_t checksum = classes.GetLocationChecksum();
258 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
259 if (data == nullptr) {
260 return false;
261 }
262 data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end());
263 return true;
264 }
265
AddMethodIndex(const std::string & dex_location,uint32_t checksum,uint16_t method_idx)266 bool ProfileCompilationInfo::AddMethodIndex(const std::string& dex_location,
267 uint32_t checksum,
268 uint16_t method_idx) {
269 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
270 if (data == nullptr) {
271 return false;
272 }
273 data->method_set.insert(method_idx);
274 return true;
275 }
276
AddClassIndex(const std::string & dex_location,uint32_t checksum,uint16_t class_idx)277 bool ProfileCompilationInfo::AddClassIndex(const std::string& dex_location,
278 uint32_t checksum,
279 uint16_t class_idx) {
280 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
281 if (data == nullptr) {
282 return false;
283 }
284 data->class_set.insert(class_idx);
285 return true;
286 }
287
ProcessLine(SafeBuffer & line_buffer,uint16_t method_set_size,uint16_t class_set_size,uint32_t checksum,const std::string & dex_location)288 bool ProfileCompilationInfo::ProcessLine(SafeBuffer& line_buffer,
289 uint16_t method_set_size,
290 uint16_t class_set_size,
291 uint32_t checksum,
292 const std::string& dex_location) {
293 for (uint16_t i = 0; i < method_set_size; i++) {
294 uint16_t method_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
295 if (!AddMethodIndex(dex_location, checksum, method_idx)) {
296 return false;
297 }
298 }
299
300 for (uint16_t i = 0; i < class_set_size; i++) {
301 uint16_t class_def_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
302 if (!AddClassIndex(dex_location, checksum, class_def_idx)) {
303 return false;
304 }
305 }
306 return true;
307 }
308
309 // Tests for EOF by trying to read 1 byte from the descriptor.
310 // Returns:
311 // 0 if the descriptor is at the EOF,
312 // -1 if there was an IO error
313 // 1 if the descriptor has more content to read
testEOF(int fd)314 static int testEOF(int fd) {
315 uint8_t buffer[1];
316 return TEMP_FAILURE_RETRY(read(fd, buffer, 1));
317 }
318
319 // Reads an uint value previously written with AddUintToBuffer.
320 template <typename T>
ReadUintAndAdvance()321 T ProfileCompilationInfo::SafeBuffer::ReadUintAndAdvance() {
322 static_assert(std::is_unsigned<T>::value, "Type is not unsigned");
323 CHECK_LE(ptr_current_ + sizeof(T), ptr_end_);
324 T value = 0;
325 for (size_t i = 0; i < sizeof(T); i++) {
326 value += ptr_current_[i] << (i * kBitsPerByte);
327 }
328 ptr_current_ += sizeof(T);
329 return value;
330 }
331
CompareAndAdvance(const uint8_t * data,size_t data_size)332 bool ProfileCompilationInfo::SafeBuffer::CompareAndAdvance(const uint8_t* data, size_t data_size) {
333 if (ptr_current_ + data_size > ptr_end_) {
334 return false;
335 }
336 if (memcmp(ptr_current_, data, data_size) == 0) {
337 ptr_current_ += data_size;
338 return true;
339 }
340 return false;
341 }
342
FillFromFd(int fd,const std::string & source,std::string * error)343 ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::SafeBuffer::FillFromFd(
344 int fd,
345 const std::string& source,
346 /*out*/std::string* error) {
347 size_t byte_count = ptr_end_ - ptr_current_;
348 uint8_t* buffer = ptr_current_;
349 while (byte_count > 0) {
350 int bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, byte_count));
351 if (bytes_read == 0) {
352 *error += "Profile EOF reached prematurely for " + source;
353 return kProfileLoadBadData;
354 } else if (bytes_read < 0) {
355 *error += "Profile IO error for " + source + strerror(errno);
356 return kProfileLoadIOError;
357 }
358 byte_count -= bytes_read;
359 buffer += bytes_read;
360 }
361 return kProfileLoadSuccess;
362 }
363
ReadProfileHeader(int fd,uint16_t * number_of_lines,std::string * error)364 ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileHeader(
365 int fd,
366 /*out*/uint16_t* number_of_lines,
367 /*out*/std::string* error) {
368 // Read magic and version
369 const size_t kMagicVersionSize =
370 sizeof(kProfileMagic) +
371 sizeof(kProfileVersion) +
372 sizeof(uint16_t); // number of lines
373
374 SafeBuffer safe_buffer(kMagicVersionSize);
375
376 ProfileLoadSatus status = safe_buffer.FillFromFd(fd, "ReadProfileHeader", error);
377 if (status != kProfileLoadSuccess) {
378 return status;
379 }
380
381 if (!safe_buffer.CompareAndAdvance(kProfileMagic, sizeof(kProfileMagic))) {
382 *error = "Profile missing magic";
383 return kProfileLoadVersionMismatch;
384 }
385 if (!safe_buffer.CompareAndAdvance(kProfileVersion, sizeof(kProfileVersion))) {
386 *error = "Profile version mismatch";
387 return kProfileLoadVersionMismatch;
388 }
389 *number_of_lines = safe_buffer.ReadUintAndAdvance<uint16_t>();
390 return kProfileLoadSuccess;
391 }
392
ReadProfileLineHeader(int fd,ProfileLineHeader * line_header,std::string * error)393 ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLineHeader(
394 int fd,
395 /*out*/ProfileLineHeader* line_header,
396 /*out*/std::string* error) {
397 SafeBuffer header_buffer(kLineHeaderSize);
398 ProfileLoadSatus status = header_buffer.FillFromFd(fd, "ReadProfileHeader", error);
399 if (status != kProfileLoadSuccess) {
400 return status;
401 }
402
403 uint16_t dex_location_size = header_buffer.ReadUintAndAdvance<uint16_t>();
404 line_header->method_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
405 line_header->class_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
406 line_header->checksum = header_buffer.ReadUintAndAdvance<uint32_t>();
407
408 if (dex_location_size == 0 || dex_location_size > kMaxDexFileKeyLength) {
409 *error = "DexFileKey has an invalid size: " + std::to_string(dex_location_size);
410 return kProfileLoadBadData;
411 }
412
413 SafeBuffer location_buffer(dex_location_size);
414 status = location_buffer.FillFromFd(fd, "ReadProfileHeaderDexLocation", error);
415 if (status != kProfileLoadSuccess) {
416 return status;
417 }
418 line_header->dex_location.assign(
419 reinterpret_cast<char*>(location_buffer.Get()), dex_location_size);
420 return kProfileLoadSuccess;
421 }
422
ReadProfileLine(int fd,const ProfileLineHeader & line_header,std::string * error)423 ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLine(
424 int fd,
425 const ProfileLineHeader& line_header,
426 /*out*/std::string* error) {
427 // Make sure that we don't try to read everything in memory (in case the profile if full).
428 // Split readings in chunks of at most 10kb.
429 static constexpr uint16_t kMaxNumberOfEntriesToRead = 5120;
430 uint16_t methods_left_to_read = line_header.method_set_size;
431 uint16_t classes_left_to_read = line_header.class_set_size;
432
433 while ((methods_left_to_read > 0) || (classes_left_to_read > 0)) {
434 uint16_t methods_to_read = std::min(kMaxNumberOfEntriesToRead, methods_left_to_read);
435 uint16_t max_classes_to_read = kMaxNumberOfEntriesToRead - methods_to_read;
436 uint16_t classes_to_read = std::min(max_classes_to_read, classes_left_to_read);
437
438 size_t line_size = sizeof(uint16_t) * (methods_to_read + classes_to_read);
439 SafeBuffer line_buffer(line_size);
440
441 ProfileLoadSatus status = line_buffer.FillFromFd(fd, "ReadProfileLine", error);
442 if (status != kProfileLoadSuccess) {
443 return status;
444 }
445 if (!ProcessLine(line_buffer,
446 methods_to_read,
447 classes_to_read,
448 line_header.checksum,
449 line_header.dex_location)) {
450 *error = "Error when reading profile file line";
451 return kProfileLoadBadData;
452 }
453 methods_left_to_read -= methods_to_read;
454 classes_left_to_read -= classes_to_read;
455 }
456 return kProfileLoadSuccess;
457 }
458
Load(int fd)459 bool ProfileCompilationInfo::Load(int fd) {
460 std::string error;
461 ProfileLoadSatus status = LoadInternal(fd, &error);
462
463 if (status == kProfileLoadSuccess) {
464 return true;
465 } else {
466 PLOG(WARNING) << "Error when reading profile " << error;
467 return false;
468 }
469 }
470
LoadInternal(int fd,std::string * error)471 ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::LoadInternal(
472 int fd, std::string* error) {
473 ScopedTrace trace(__PRETTY_FUNCTION__);
474 DCHECK_GE(fd, 0);
475
476 struct stat stat_buffer;
477 if (fstat(fd, &stat_buffer) != 0) {
478 return kProfileLoadIOError;
479 }
480 // We allow empty profile files.
481 // Profiles may be created by ActivityManager or installd before we manage to
482 // process them in the runtime or profman.
483 if (stat_buffer.st_size == 0) {
484 return kProfileLoadSuccess;
485 }
486 // Read profile header: magic + version + number_of_lines.
487 uint16_t number_of_lines;
488 ProfileLoadSatus status = ReadProfileHeader(fd, &number_of_lines, error);
489 if (status != kProfileLoadSuccess) {
490 return status;
491 }
492
493 while (number_of_lines > 0) {
494 ProfileLineHeader line_header;
495 // First, read the line header to get the amount of data we need to read.
496 status = ReadProfileLineHeader(fd, &line_header, error);
497 if (status != kProfileLoadSuccess) {
498 return status;
499 }
500
501 // Now read the actual profile line.
502 status = ReadProfileLine(fd, line_header, error);
503 if (status != kProfileLoadSuccess) {
504 return status;
505 }
506 number_of_lines--;
507 }
508
509 // Check that we read everything and that profiles don't contain junk data.
510 int result = testEOF(fd);
511 if (result == 0) {
512 return kProfileLoadSuccess;
513 } else if (result < 0) {
514 return kProfileLoadIOError;
515 } else {
516 *error = "Unexpected content in the profile file";
517 return kProfileLoadBadData;
518 }
519 }
520
MergeWith(const ProfileCompilationInfo & other)521 bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other) {
522 // First verify that all checksums match. This will avoid adding garbage to
523 // the current profile info.
524 // Note that the number of elements should be very small, so this should not
525 // be a performance issue.
526 for (const auto& other_it : other.info_) {
527 auto info_it = info_.find(other_it.first);
528 if ((info_it != info_.end()) && (info_it->second.checksum != other_it.second.checksum)) {
529 LOG(WARNING) << "Checksum mismatch for dex " << other_it.first;
530 return false;
531 }
532 }
533 // All checksums match. Import the data.
534 for (const auto& other_it : other.info_) {
535 const std::string& other_dex_location = other_it.first;
536 const DexFileData& other_dex_data = other_it.second;
537 auto info_it = info_.find(other_dex_location);
538 if (info_it == info_.end()) {
539 info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum));
540 }
541 info_it->second.method_set.insert(other_dex_data.method_set.begin(),
542 other_dex_data.method_set.end());
543 info_it->second.class_set.insert(other_dex_data.class_set.begin(),
544 other_dex_data.class_set.end());
545 }
546 return true;
547 }
548
ContainsMethod(const MethodReference & method_ref) const549 bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
550 auto info_it = info_.find(GetProfileDexFileKey(method_ref.dex_file->GetLocation()));
551 if (info_it != info_.end()) {
552 if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) {
553 return false;
554 }
555 const std::set<uint16_t>& methods = info_it->second.method_set;
556 return methods.find(method_ref.dex_method_index) != methods.end();
557 }
558 return false;
559 }
560
ContainsClass(const DexFile & dex_file,uint16_t class_def_idx) const561 bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const {
562 auto info_it = info_.find(GetProfileDexFileKey(dex_file.GetLocation()));
563 if (info_it != info_.end()) {
564 if (dex_file.GetLocationChecksum() != info_it->second.checksum) {
565 return false;
566 }
567 const std::set<uint16_t>& classes = info_it->second.class_set;
568 return classes.find(class_def_idx) != classes.end();
569 }
570 return false;
571 }
572
GetNumberOfMethods() const573 uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
574 uint32_t total = 0;
575 for (const auto& it : info_) {
576 total += it.second.method_set.size();
577 }
578 return total;
579 }
580
GetNumberOfResolvedClasses() const581 uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
582 uint32_t total = 0;
583 for (const auto& it : info_) {
584 total += it.second.class_set.size();
585 }
586 return total;
587 }
588
DumpInfo(const std::vector<const DexFile * > * dex_files,bool print_full_dex_location) const589 std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
590 bool print_full_dex_location) const {
591 std::ostringstream os;
592 if (info_.empty()) {
593 return "ProfileInfo: empty";
594 }
595
596 os << "ProfileInfo:";
597
598 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
599 for (const auto& it : info_) {
600 os << "\n";
601 const std::string& location = it.first;
602 const DexFileData& dex_data = it.second;
603 if (print_full_dex_location) {
604 os << location;
605 } else {
606 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
607 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
608 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
609 }
610 const DexFile* dex_file = nullptr;
611 if (dex_files != nullptr) {
612 for (size_t i = 0; i < dex_files->size(); i++) {
613 if (location == (*dex_files)[i]->GetLocation()) {
614 dex_file = (*dex_files)[i];
615 }
616 }
617 }
618 os << "\n\tmethods: ";
619 for (const auto method_it : dex_data.method_set) {
620 if (dex_file != nullptr) {
621 os << "\n\t\t" << PrettyMethod(method_it, *dex_file, true);
622 } else {
623 os << method_it << ",";
624 }
625 }
626 os << "\n\tclasses: ";
627 for (const auto class_it : dex_data.class_set) {
628 if (dex_file != nullptr) {
629 os << "\n\t\t" << PrettyType(class_it, *dex_file);
630 } else {
631 os << class_it << ",";
632 }
633 }
634 }
635 return os.str();
636 }
637
Equals(const ProfileCompilationInfo & other)638 bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) {
639 return info_.Equals(other.info_);
640 }
641
GetResolvedClasses() const642 std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses() const {
643 std::set<DexCacheResolvedClasses> ret;
644 for (auto&& pair : info_) {
645 const std::string& profile_key = pair.first;
646 const DexFileData& data = pair.second;
647 // TODO: Is it OK to use the same location for both base and dex location here?
648 DexCacheResolvedClasses classes(profile_key, profile_key, data.checksum);
649 classes.AddClasses(data.class_set.begin(), data.class_set.end());
650 ret.insert(classes);
651 }
652 return ret;
653 }
654
ClearResolvedClasses()655 void ProfileCompilationInfo::ClearResolvedClasses() {
656 for (auto& pair : info_) {
657 pair.second.class_set.clear();
658 }
659 }
660
661 } // namespace art
662