blob: 024c73a2c1883b8d969364785cf7fee6f9d00516 [file] [log] [blame]
Calin Juravle31f2c152015-10-23 17:56:15 +01001/*
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
Calin Juravle64142952016-03-21 14:37:55 +000019#include "errno.h"
20#include <limits.h>
Calin Juravle4d77b6a2015-12-01 18:38:09 +000021#include <vector>
Calin Juravle31f2c152015-10-23 17:56:15 +010022#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"
Calin Juravle877fd962016-01-05 14:29:29 +000028#include "base/scoped_flock.h"
Calin Juravle66f55232015-12-08 15:09:10 +000029#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080030#include "base/systrace.h"
Calin Juravle877fd962016-01-05 14:29:29 +000031#include "base/unix_file/fd_file.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010032#include "jit/profiling_info.h"
Calin Juravle877fd962016-01-05 14:29:29 +000033#include "os.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010034#include "safe_map.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010035
36namespace art {
37
Calin Juravle64142952016-03-21 14:37:55 +000038const uint8_t ProfileCompilationInfo::kProfileMagic[] = { 'p', 'r', 'o', '\0' };
39const uint8_t ProfileCompilationInfo::kProfileVersion[] = { '0', '0', '1', '\0' };
40
41static constexpr uint16_t kMaxDexFileKeyLength = PATH_MAX;
42
Calin Juravle34900cc2016-02-05 16:19:19 +000043// 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.
Calin Juravle31708b72016-02-05 19:44:05 +000047std::string ProfileCompilationInfo::GetProfileDexFileKey(const std::string& dex_location) {
Calin Juravle34900cc2016-02-05 16:19:19 +000048 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 {
Calin Juravle31708b72016-02-05 19:44:05 +000053 DCHECK(last_sep_index < dex_location.size());
54 return dex_location.substr(last_sep_index + 1);
Calin Juravle34900cc2016-02-05 16:19:19 +000055 }
56}
57
Calin Juravle67265462016-03-18 16:23:40 +000058bool ProfileCompilationInfo::AddMethodsAndClasses(
59 const std::vector<ArtMethod*>& methods,
60 const std::set<DexCacheResolvedClasses>& resolved_classes) {
61 ScopedObjectAccess soa(Thread::Current());
62 for (ArtMethod* method : methods) {
63 const DexFile* dex_file = method->GetDexFile();
64 if (!AddMethodIndex(GetProfileDexFileKey(dex_file->GetLocation()),
65 dex_file->GetLocationChecksum(),
66 method->GetDexMethodIndex())) {
67 return false;
68 }
69 }
70 for (const DexCacheResolvedClasses& dex_cache : resolved_classes) {
71 if (!AddResolvedClasses(dex_cache)) {
72 return false;
73 }
74 }
75 return true;
76}
77
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000078bool ProfileCompilationInfo::MergeAndSave(const std::string& filename,
79 uint64_t* bytes_written,
80 bool force) {
Calin Juravle67265462016-03-18 16:23:40 +000081 ScopedTrace trace(__PRETTY_FUNCTION__);
82 ScopedFlock flock;
83 std::string error;
84 if (!flock.Init(filename.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC, /* block */ false, &error)) {
85 LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
86 return false;
87 }
88
89 int fd = flock.GetFile()->Fd();
90
91 // Load the file but keep a copy around to be able to infer if the content has changed.
92 ProfileCompilationInfo fileInfo;
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000093 ProfileLoadSatus status = fileInfo.LoadInternal(fd, &error);
94 if (status == kProfileLoadSuccess) {
95 // Merge the content of file into the current object.
96 if (MergeWith(fileInfo)) {
97 // If after the merge we have the same data as what is the file there's no point
98 // in actually doing the write. The file will be exactly the same as before.
99 if (Equals(fileInfo)) {
100 if (bytes_written != nullptr) {
101 *bytes_written = 0;
102 }
103 return true;
104 }
105 } else {
106 LOG(WARNING) << "Could not merge previous profile data from file " << filename;
107 if (!force) {
108 return false;
109 }
110 }
111 } else if (force &&
112 ((status == kProfileLoadVersionMismatch) || (status == kProfileLoadBadData))) {
113 // Log a warning but don't return false. We will clear the profile anyway.
114 LOG(WARNING) << "Clearing bad or obsolete profile data from file "
115 << filename << ": " << error;
116 } else {
117 LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error;
Calin Juravle67265462016-03-18 16:23:40 +0000118 return false;
119 }
120
Calin Juravle5d1bd0a2016-03-24 20:33:22 +0000121 // We need to clear the data because we don't support appending to the profiles yet.
Calin Juravle67265462016-03-18 16:23:40 +0000122 if (!flock.GetFile()->ClearContent()) {
123 PLOG(WARNING) << "Could not clear profile file: " << filename;
124 return false;
125 }
126
127 // This doesn't need locking because we are trying to lock the file for exclusive
128 // access and fail immediately if we can't.
129 bool result = Save(fd);
130 if (result) {
131 VLOG(profiler) << "Successfully saved profile info to " << filename
132 << " Size: " << GetFileSizeBytes(filename);
133 if (bytes_written != nullptr) {
134 *bytes_written = GetFileSizeBytes(filename);
135 }
136 } else {
137 VLOG(profiler) << "Failed to save profile info to " << filename;
138 }
139 return result;
140}
141
Calin Juravle64142952016-03-21 14:37:55 +0000142// Returns true if all the bytes were successfully written to the file descriptor.
143static bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) {
144 while (byte_count > 0) {
145 int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count));
146 if (bytes_written == -1) {
Calin Juravle877fd962016-01-05 14:29:29 +0000147 return false;
148 }
Calin Juravle64142952016-03-21 14:37:55 +0000149 byte_count -= bytes_written; // Reduce the number of remaining bytes.
150 buffer += bytes_written; // Move the buffer forward.
151 }
Calin Juravle877fd962016-01-05 14:29:29 +0000152 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +0100153}
154
Calin Juravle64142952016-03-21 14:37:55 +0000155// Add the string bytes to the buffer.
156static void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) {
157 buffer->insert(buffer->end(), value.begin(), value.end());
158}
159
160// Insert each byte, from low to high into the buffer.
161template <typename T>
162static void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) {
163 for (size_t i = 0; i < sizeof(T); i++) {
164 buffer->push_back((value >> (i * kBitsPerByte)) & 0xff);
165 }
166}
167
168static constexpr size_t kLineHeaderSize =
169 3 * sizeof(uint16_t) + // method_set.size + class_set.size + dex_location.size
170 sizeof(uint32_t); // checksum
Calin Juravle31f2c152015-10-23 17:56:15 +0100171
172/**
173 * Serialization format:
Calin Juravle64142952016-03-21 14:37:55 +0000174 * magic,version,number_of_lines
175 * dex_location1,number_of_methods1,number_of_classes1,dex_location_checksum1, \
176 * method_id11,method_id12...,class_id1,class_id2...
177 * dex_location2,number_of_methods2,number_of_classes2,dex_location_checksum2, \
178 * method_id21,method_id22...,,class_id1,class_id2...
179 * .....
Calin Juravle31f2c152015-10-23 17:56:15 +0100180 **/
Calin Juravle2e2db782016-02-23 12:00:03 +0000181bool ProfileCompilationInfo::Save(int fd) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800182 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000183 DCHECK_GE(fd, 0);
Calin Juravle64142952016-03-21 14:37:55 +0000184
185 // Cache at most 5KB before writing.
186 static constexpr size_t kMaxSizeToKeepBeforeWriting = 5 * KB;
187 // Use a vector wrapper to avoid keeping track of offsets when we add elements.
188 std::vector<uint8_t> buffer;
189 WriteBuffer(fd, kProfileMagic, sizeof(kProfileMagic));
190 WriteBuffer(fd, kProfileVersion, sizeof(kProfileVersion));
191 AddUintToBuffer(&buffer, static_cast<uint16_t>(info_.size()));
192
Calin Juravle998c2162015-12-21 15:39:33 +0200193 for (const auto& it : info_) {
Calin Juravle64142952016-03-21 14:37:55 +0000194 if (buffer.size() > kMaxSizeToKeepBeforeWriting) {
195 if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
196 return false;
197 }
198 buffer.clear();
199 }
Calin Juravle998c2162015-12-21 15:39:33 +0200200 const std::string& dex_location = it.first;
201 const DexFileData& dex_data = it.second;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800202 if (dex_data.method_set.empty() && dex_data.class_set.empty()) {
203 continue;
204 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100205
Calin Juravle64142952016-03-21 14:37:55 +0000206 if (dex_location.size() >= kMaxDexFileKeyLength) {
207 LOG(WARNING) << "DexFileKey exceeds allocated limit";
208 return false;
209 }
210
211 // Make sure that the buffer has enough capacity to avoid repeated resizings
212 // while we add data.
213 size_t required_capacity = buffer.size() +
214 kLineHeaderSize +
215 dex_location.size() +
216 sizeof(uint16_t) * (dex_data.class_set.size() + dex_data.method_set.size());
217
218 buffer.reserve(required_capacity);
219
220 DCHECK_LE(dex_location.size(), std::numeric_limits<uint16_t>::max());
221 DCHECK_LE(dex_data.method_set.size(), std::numeric_limits<uint16_t>::max());
222 DCHECK_LE(dex_data.class_set.size(), std::numeric_limits<uint16_t>::max());
223 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_location.size()));
224 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.method_set.size()));
225 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.class_set.size()));
226 AddUintToBuffer(&buffer, dex_data.checksum); // uint32_t
227
228 AddStringToBuffer(&buffer, dex_location);
229
Calin Juravle998c2162015-12-21 15:39:33 +0200230 for (auto method_it : dex_data.method_set) {
Calin Juravle64142952016-03-21 14:37:55 +0000231 AddUintToBuffer(&buffer, method_it);
Calin Juravle31f2c152015-10-23 17:56:15 +0100232 }
Calin Juravle64142952016-03-21 14:37:55 +0000233 for (auto class_id : dex_data.class_set) {
234 AddUintToBuffer(&buffer, class_id);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800235 }
Calin Juravle64142952016-03-21 14:37:55 +0000236 DCHECK_EQ(required_capacity, buffer.size())
237 << "Failed to add the expected number of bytes in the buffer";
Calin Juravle31f2c152015-10-23 17:56:15 +0100238 }
239
Calin Juravle64142952016-03-21 14:37:55 +0000240 return WriteBuffer(fd, buffer.data(), buffer.size());
Calin Juravle226501b2015-12-11 14:41:31 +0000241}
242
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800243ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData(
244 const std::string& dex_location,
245 uint32_t checksum) {
Calin Juravle998c2162015-12-21 15:39:33 +0200246 auto info_it = info_.find(dex_location);
247 if (info_it == info_.end()) {
248 info_it = info_.Put(dex_location, DexFileData(checksum));
249 }
250 if (info_it->second.checksum != checksum) {
251 LOG(WARNING) << "Checksum mismatch for dex " << dex_location;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800252 return nullptr;
253 }
254 return &info_it->second;
255}
256
257bool ProfileCompilationInfo::AddResolvedClasses(const DexCacheResolvedClasses& classes) {
258 const std::string dex_location = GetProfileDexFileKey(classes.GetDexLocation());
259 const uint32_t checksum = classes.GetLocationChecksum();
260 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
261 if (data == nullptr) {
Calin Juravle998c2162015-12-21 15:39:33 +0200262 return false;
263 }
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800264 data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end());
265 return true;
266}
267
268bool ProfileCompilationInfo::AddMethodIndex(const std::string& dex_location,
269 uint32_t checksum,
270 uint16_t method_idx) {
271 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
272 if (data == nullptr) {
273 return false;
274 }
275 data->method_set.insert(method_idx);
276 return true;
277}
278
279bool ProfileCompilationInfo::AddClassIndex(const std::string& dex_location,
280 uint32_t checksum,
281 uint16_t class_idx) {
282 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
283 if (data == nullptr) {
284 return false;
285 }
286 data->class_set.insert(class_idx);
Calin Juravle998c2162015-12-21 15:39:33 +0200287 return true;
288}
289
Calin Juravle64142952016-03-21 14:37:55 +0000290bool ProfileCompilationInfo::ProcessLine(SafeBuffer& line_buffer,
291 uint16_t method_set_size,
292 uint16_t class_set_size,
293 uint32_t checksum,
294 const std::string& dex_location) {
295 for (uint16_t i = 0; i < method_set_size; i++) {
296 uint16_t method_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800297 if (!AddMethodIndex(dex_location, checksum, method_idx)) {
Calin Juravle877fd962016-01-05 14:29:29 +0000298 return false;
299 }
Calin Juravle226501b2015-12-11 14:41:31 +0000300 }
Calin Juravle64142952016-03-21 14:37:55 +0000301
302 for (uint16_t i = 0; i < class_set_size; i++) {
303 uint16_t class_def_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
304 if (!AddClassIndex(dex_location, checksum, class_def_idx)) {
305 return false;
306 }
307 }
Calin Juravle226501b2015-12-11 14:41:31 +0000308 return true;
309}
310
Calin Juravle64142952016-03-21 14:37:55 +0000311// Tests for EOF by trying to read 1 byte from the descriptor.
312// Returns:
313// 0 if the descriptor is at the EOF,
314// -1 if there was an IO error
315// 1 if the descriptor has more content to read
316static int testEOF(int fd) {
317 uint8_t buffer[1];
318 return TEMP_FAILURE_RETRY(read(fd, buffer, 1));
319}
320
321// Reads an uint value previously written with AddUintToBuffer.
322template <typename T>
323T ProfileCompilationInfo::SafeBuffer::ReadUintAndAdvance() {
324 static_assert(std::is_unsigned<T>::value, "Type is not unsigned");
325 CHECK_LE(ptr_current_ + sizeof(T), ptr_end_);
326 T value = 0;
327 for (size_t i = 0; i < sizeof(T); i++) {
328 value += ptr_current_[i] << (i * kBitsPerByte);
Calin Juravle226501b2015-12-11 14:41:31 +0000329 }
Calin Juravle64142952016-03-21 14:37:55 +0000330 ptr_current_ += sizeof(T);
331 return value;
332}
333
334bool ProfileCompilationInfo::SafeBuffer::CompareAndAdvance(const uint8_t* data, size_t data_size) {
335 if (ptr_current_ + data_size > ptr_end_) {
336 return false;
337 }
338 if (memcmp(ptr_current_, data, data_size) == 0) {
339 ptr_current_ += data_size;
340 return true;
341 }
342 return false;
343}
344
345ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::SafeBuffer::FillFromFd(
346 int fd,
347 const std::string& source,
348 /*out*/std::string* error) {
349 size_t byte_count = ptr_end_ - ptr_current_;
350 uint8_t* buffer = ptr_current_;
351 while (byte_count > 0) {
352 int bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, byte_count));
353 if (bytes_read == 0) {
354 *error += "Profile EOF reached prematurely for " + source;
355 return kProfileLoadBadData;
356 } else if (bytes_read < 0) {
357 *error += "Profile IO error for " + source + strerror(errno);
358 return kProfileLoadIOError;
Calin Juravle226501b2015-12-11 14:41:31 +0000359 }
Calin Juravle64142952016-03-21 14:37:55 +0000360 byte_count -= bytes_read;
361 buffer += bytes_read;
Calin Juravle226501b2015-12-11 14:41:31 +0000362 }
Calin Juravle64142952016-03-21 14:37:55 +0000363 return kProfileLoadSuccess;
364}
365
366ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileHeader(
367 int fd,
368 /*out*/uint16_t* number_of_lines,
369 /*out*/std::string* error) {
370 // Read magic and version
371 const size_t kMagicVersionSize =
372 sizeof(kProfileMagic) +
373 sizeof(kProfileVersion) +
374 sizeof(uint16_t); // number of lines
375
376 SafeBuffer safe_buffer(kMagicVersionSize);
377
378 ProfileLoadSatus status = safe_buffer.FillFromFd(fd, "ReadProfileHeader", error);
379 if (status != kProfileLoadSuccess) {
380 return status;
381 }
382
383 if (!safe_buffer.CompareAndAdvance(kProfileMagic, sizeof(kProfileMagic))) {
384 *error = "Profile missing magic";
385 return kProfileLoadVersionMismatch;
386 }
387 if (!safe_buffer.CompareAndAdvance(kProfileVersion, sizeof(kProfileVersion))) {
388 *error = "Profile version mismatch";
389 return kProfileLoadVersionMismatch;
390 }
391 *number_of_lines = safe_buffer.ReadUintAndAdvance<uint16_t>();
392 return kProfileLoadSuccess;
393}
394
395ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLineHeader(
396 int fd,
397 /*out*/ProfileLineHeader* line_header,
398 /*out*/std::string* error) {
399 SafeBuffer header_buffer(kLineHeaderSize);
400 ProfileLoadSatus status = header_buffer.FillFromFd(fd, "ReadProfileHeader", error);
401 if (status != kProfileLoadSuccess) {
402 return status;
403 }
404
405 uint16_t dex_location_size = header_buffer.ReadUintAndAdvance<uint16_t>();
406 line_header->method_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
407 line_header->class_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
408 line_header->checksum = header_buffer.ReadUintAndAdvance<uint32_t>();
409
410 if (dex_location_size == 0 || dex_location_size > kMaxDexFileKeyLength) {
411 *error = "DexFileKey has an invalid size: " + std::to_string(dex_location_size);
412 return kProfileLoadBadData;
413 }
414
415 SafeBuffer location_buffer(dex_location_size);
416 status = location_buffer.FillFromFd(fd, "ReadProfileHeaderDexLocation", error);
417 if (status != kProfileLoadSuccess) {
418 return status;
419 }
420 line_header->dex_location.assign(
421 reinterpret_cast<char*>(location_buffer.Get()), dex_location_size);
422 return kProfileLoadSuccess;
423}
424
425ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLine(
426 int fd,
427 const ProfileLineHeader& line_header,
428 /*out*/std::string* error) {
429 // Make sure that we don't try to read everything in memory (in case the profile if full).
430 // Split readings in chunks of at most 10kb.
431 static constexpr uint16_t kMaxNumberOfEntriesToRead = 5120;
432 uint16_t methods_left_to_read = line_header.method_set_size;
433 uint16_t classes_left_to_read = line_header.class_set_size;
434
435 while ((methods_left_to_read > 0) || (classes_left_to_read > 0)) {
436 uint16_t methods_to_read = std::min(kMaxNumberOfEntriesToRead, methods_left_to_read);
437 uint16_t max_classes_to_read = kMaxNumberOfEntriesToRead - methods_to_read;
438 uint16_t classes_to_read = std::min(max_classes_to_read, classes_left_to_read);
439
440 size_t line_size = sizeof(uint16_t) * (methods_to_read + classes_to_read);
441 SafeBuffer line_buffer(line_size);
442
443 ProfileLoadSatus status = line_buffer.FillFromFd(fd, "ReadProfileLine", error);
444 if (status != kProfileLoadSuccess) {
445 return status;
446 }
447 if (!ProcessLine(line_buffer,
448 methods_to_read,
449 classes_to_read,
450 line_header.checksum,
451 line_header.dex_location)) {
452 *error = "Error when reading profile file line";
453 return kProfileLoadBadData;
454 }
455 methods_left_to_read -= methods_to_read;
456 classes_left_to_read -= classes_to_read;
457 }
458 return kProfileLoadSuccess;
Calin Juravle226501b2015-12-11 14:41:31 +0000459}
460
Calin Juravle2e2db782016-02-23 12:00:03 +0000461bool ProfileCompilationInfo::Load(int fd) {
Calin Juravle64142952016-03-21 14:37:55 +0000462 std::string error;
463 ProfileLoadSatus status = LoadInternal(fd, &error);
464
465 if (status == kProfileLoadSuccess) {
466 return true;
467 } else {
468 PLOG(WARNING) << "Error when reading profile " << error;
469 return false;
470 }
471}
472
473ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::LoadInternal(
474 int fd, std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800475 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000476 DCHECK_GE(fd, 0);
Calin Juravle226501b2015-12-11 14:41:31 +0000477
Calin Juravle64142952016-03-21 14:37:55 +0000478 struct stat stat_buffer;
479 if (fstat(fd, &stat_buffer) != 0) {
480 return kProfileLoadIOError;
Calin Juravle226501b2015-12-11 14:41:31 +0000481 }
Calin Juravle64142952016-03-21 14:37:55 +0000482 // We allow empty profile files.
483 // Profiles may be created by ActivityManager or installd before we manage to
484 // process them in the runtime or profman.
485 if (stat_buffer.st_size == 0) {
486 return kProfileLoadSuccess;
487 }
488 // Read profile header: magic + version + number_of_lines.
489 uint16_t number_of_lines;
490 ProfileLoadSatus status = ReadProfileHeader(fd, &number_of_lines, error);
491 if (status != kProfileLoadSuccess) {
492 return status;
493 }
494
495 while (number_of_lines > 0) {
496 ProfileLineHeader line_header;
497 // First, read the line header to get the amount of data we need to read.
498 status = ReadProfileLineHeader(fd, &line_header, error);
499 if (status != kProfileLoadSuccess) {
500 return status;
501 }
502
503 // Now read the actual profile line.
504 status = ReadProfileLine(fd, line_header, error);
505 if (status != kProfileLoadSuccess) {
506 return status;
507 }
508 number_of_lines--;
509 }
510
511 // Check that we read everything and that profiles don't contain junk data.
512 int result = testEOF(fd);
513 if (result == 0) {
514 return kProfileLoadSuccess;
515 } else if (result < 0) {
516 return kProfileLoadIOError;
517 } else {
518 *error = "Unexpected content in the profile file";
519 return kProfileLoadBadData;
520 }
Calin Juravle998c2162015-12-21 15:39:33 +0200521}
522
Calin Juravle67265462016-03-18 16:23:40 +0000523bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other) {
Calin Juravle5d1bd0a2016-03-24 20:33:22 +0000524 // First verify that all checksums match. This will avoid adding garbage to
525 // the current profile info.
526 // Note that the number of elements should be very small, so this should not
527 // be a performance issue.
528 for (const auto& other_it : other.info_) {
529 auto info_it = info_.find(other_it.first);
530 if ((info_it != info_.end()) && (info_it->second.checksum != other_it.second.checksum)) {
531 LOG(WARNING) << "Checksum mismatch for dex " << other_it.first;
532 return false;
533 }
534 }
535 // All checksums match. Import the data.
Calin Juravle998c2162015-12-21 15:39:33 +0200536 for (const auto& other_it : other.info_) {
537 const std::string& other_dex_location = other_it.first;
538 const DexFileData& other_dex_data = other_it.second;
Calin Juravle998c2162015-12-21 15:39:33 +0200539 auto info_it = info_.find(other_dex_location);
540 if (info_it == info_.end()) {
541 info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum));
542 }
Calin Juravle998c2162015-12-21 15:39:33 +0200543 info_it->second.method_set.insert(other_dex_data.method_set.begin(),
544 other_dex_data.method_set.end());
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800545 info_it->second.class_set.insert(other_dex_data.class_set.begin(),
546 other_dex_data.class_set.end());
Calin Juravle998c2162015-12-21 15:39:33 +0200547 }
548 return true;
Calin Juravle226501b2015-12-11 14:41:31 +0000549}
550
551bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
Calin Juravle34900cc2016-02-05 16:19:19 +0000552 auto info_it = info_.find(GetProfileDexFileKey(method_ref.dex_file->GetLocation()));
Calin Juravle226501b2015-12-11 14:41:31 +0000553 if (info_it != info_.end()) {
Calin Juravle998c2162015-12-21 15:39:33 +0200554 if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) {
555 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000556 }
Calin Juravle998c2162015-12-21 15:39:33 +0200557 const std::set<uint16_t>& methods = info_it->second.method_set;
558 return methods.find(method_ref.dex_method_index) != methods.end();
Calin Juravle226501b2015-12-11 14:41:31 +0000559 }
560 return false;
561}
562
Mathieu Chartiera8077802016-03-16 19:08:31 -0700563bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const {
564 auto info_it = info_.find(GetProfileDexFileKey(dex_file.GetLocation()));
565 if (info_it != info_.end()) {
566 if (dex_file.GetLocationChecksum() != info_it->second.checksum) {
567 return false;
568 }
569 const std::set<uint16_t>& classes = info_it->second.class_set;
570 return classes.find(class_def_idx) != classes.end();
571 }
572 return false;
573}
574
Calin Juravle998c2162015-12-21 15:39:33 +0200575uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
576 uint32_t total = 0;
577 for (const auto& it : info_) {
578 total += it.second.method_set.size();
579 }
580 return total;
581}
582
Calin Juravle67265462016-03-18 16:23:40 +0000583uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
584 uint32_t total = 0;
585 for (const auto& it : info_) {
586 total += it.second.class_set.size();
587 }
588 return total;
589}
590
Calin Juravle998c2162015-12-21 15:39:33 +0200591std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
592 bool print_full_dex_location) const {
Calin Juravle226501b2015-12-11 14:41:31 +0000593 std::ostringstream os;
594 if (info_.empty()) {
595 return "ProfileInfo: empty";
596 }
597
598 os << "ProfileInfo:";
599
Calin Juravle226501b2015-12-11 14:41:31 +0000600 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
Calin Juravle998c2162015-12-21 15:39:33 +0200601 for (const auto& it : info_) {
Calin Juravle226501b2015-12-11 14:41:31 +0000602 os << "\n";
Calin Juravle998c2162015-12-21 15:39:33 +0200603 const std::string& location = it.first;
604 const DexFileData& dex_data = it.second;
Calin Juravle226501b2015-12-11 14:41:31 +0000605 if (print_full_dex_location) {
606 os << location;
607 } else {
608 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
609 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
610 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
611 }
Calin Juravle876f3502016-03-24 16:16:34 +0000612 const DexFile* dex_file = nullptr;
613 if (dex_files != nullptr) {
614 for (size_t i = 0; i < dex_files->size(); i++) {
615 if (location == (*dex_files)[i]->GetLocation()) {
616 dex_file = (*dex_files)[i];
Calin Juravle998c2162015-12-21 15:39:33 +0200617 }
Calin Juravle226501b2015-12-11 14:41:31 +0000618 }
Calin Juravle876f3502016-03-24 16:16:34 +0000619 }
620 os << "\n\tmethods: ";
621 for (const auto method_it : dex_data.method_set) {
622 if (dex_file != nullptr) {
623 os << "\n\t\t" << PrettyMethod(method_it, *dex_file, true);
624 } else {
625 os << method_it << ",";
626 }
627 }
628 os << "\n\tclasses: ";
629 for (const auto class_it : dex_data.class_set) {
630 if (dex_file != nullptr) {
631 os << "\n\t\t" << PrettyType(class_it, *dex_file);
632 } else {
633 os << class_it << ",";
634 }
Calin Juravle226501b2015-12-11 14:41:31 +0000635 }
636 }
637 return os.str();
638}
639
Calin Juravle2e2db782016-02-23 12:00:03 +0000640bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) {
Calin Juravle877fd962016-01-05 14:29:29 +0000641 return info_.Equals(other.info_);
642}
643
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800644std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses() const {
645 std::set<DexCacheResolvedClasses> ret;
646 for (auto&& pair : info_) {
647 const std::string& profile_key = pair.first;
648 const DexFileData& data = pair.second;
649 DexCacheResolvedClasses classes(profile_key, data.checksum);
650 classes.AddClasses(data.class_set.begin(), data.class_set.end());
651 ret.insert(classes);
652 }
653 return ret;
654}
655
Calin Juravle67265462016-03-18 16:23:40 +0000656void ProfileCompilationInfo::ClearResolvedClasses() {
657 for (auto& pair : info_) {
658 pair.second.class_set.clear();
659 }
660}
661
Calin Juravle31f2c152015-10-23 17:56:15 +0100662} // namespace art