blob: 7a6d1a891f2d6b9c2b6c20176fd76ed95fbb3144 [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 Juravlec4588572016-06-08 14:24:13 +010043// Debug flag to ignore checksums when testing if a method or a class is present in the profile.
44// Use to make facilitate testing profile guided compilation across a large number of apps
45// using the same test profile.
46static constexpr bool kDebugIgnoreChecksum = false;
47
Calin Juravle34900cc2016-02-05 16:19:19 +000048// Transform the actual dex location into relative paths.
49// Note: this is OK because we don't store profiles of different apps into the same file.
50// Apps with split apks don't cause trouble because each split has a different name and will not
51// collide with other entries.
Calin Juravle31708b72016-02-05 19:44:05 +000052std::string ProfileCompilationInfo::GetProfileDexFileKey(const std::string& dex_location) {
Calin Juravle34900cc2016-02-05 16:19:19 +000053 DCHECK(!dex_location.empty());
54 size_t last_sep_index = dex_location.find_last_of('/');
55 if (last_sep_index == std::string::npos) {
56 return dex_location;
57 } else {
Calin Juravle31708b72016-02-05 19:44:05 +000058 DCHECK(last_sep_index < dex_location.size());
59 return dex_location.substr(last_sep_index + 1);
Calin Juravle34900cc2016-02-05 16:19:19 +000060 }
61}
62
Calin Juravle67265462016-03-18 16:23:40 +000063bool ProfileCompilationInfo::AddMethodsAndClasses(
Calin Juravle99629622016-04-19 16:33:46 +010064 const std::vector<MethodReference>& methods,
Calin Juravle67265462016-03-18 16:23:40 +000065 const std::set<DexCacheResolvedClasses>& resolved_classes) {
Calin Juravle99629622016-04-19 16:33:46 +010066 for (const MethodReference& method : methods) {
67 if (!AddMethodIndex(GetProfileDexFileKey(method.dex_file->GetLocation()),
68 method.dex_file->GetLocationChecksum(),
69 method.dex_method_index)) {
Calin Juravle67265462016-03-18 16:23:40 +000070 return false;
71 }
72 }
73 for (const DexCacheResolvedClasses& dex_cache : resolved_classes) {
74 if (!AddResolvedClasses(dex_cache)) {
75 return false;
76 }
77 }
78 return true;
79}
80
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000081bool ProfileCompilationInfo::MergeAndSave(const std::string& filename,
82 uint64_t* bytes_written,
83 bool force) {
Calin Juravle67265462016-03-18 16:23:40 +000084 ScopedTrace trace(__PRETTY_FUNCTION__);
85 ScopedFlock flock;
86 std::string error;
87 if (!flock.Init(filename.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC, /* block */ false, &error)) {
88 LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
89 return false;
90 }
91
92 int fd = flock.GetFile()->Fd();
93
94 // Load the file but keep a copy around to be able to infer if the content has changed.
95 ProfileCompilationInfo fileInfo;
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000096 ProfileLoadSatus status = fileInfo.LoadInternal(fd, &error);
97 if (status == kProfileLoadSuccess) {
98 // Merge the content of file into the current object.
99 if (MergeWith(fileInfo)) {
100 // If after the merge we have the same data as what is the file there's no point
101 // in actually doing the write. The file will be exactly the same as before.
102 if (Equals(fileInfo)) {
103 if (bytes_written != nullptr) {
104 *bytes_written = 0;
105 }
106 return true;
107 }
108 } else {
109 LOG(WARNING) << "Could not merge previous profile data from file " << filename;
110 if (!force) {
111 return false;
112 }
113 }
114 } else if (force &&
115 ((status == kProfileLoadVersionMismatch) || (status == kProfileLoadBadData))) {
116 // Log a warning but don't return false. We will clear the profile anyway.
117 LOG(WARNING) << "Clearing bad or obsolete profile data from file "
118 << filename << ": " << error;
119 } else {
120 LOG(WARNING) << "Could not load profile data from file " << filename << ": " << error;
Calin Juravle67265462016-03-18 16:23:40 +0000121 return false;
122 }
123
Calin Juravle5d1bd0a2016-03-24 20:33:22 +0000124 // We need to clear the data because we don't support appending to the profiles yet.
Calin Juravle67265462016-03-18 16:23:40 +0000125 if (!flock.GetFile()->ClearContent()) {
126 PLOG(WARNING) << "Could not clear profile file: " << filename;
127 return false;
128 }
129
130 // This doesn't need locking because we are trying to lock the file for exclusive
131 // access and fail immediately if we can't.
132 bool result = Save(fd);
133 if (result) {
134 VLOG(profiler) << "Successfully saved profile info to " << filename
135 << " Size: " << GetFileSizeBytes(filename);
136 if (bytes_written != nullptr) {
137 *bytes_written = GetFileSizeBytes(filename);
138 }
139 } else {
140 VLOG(profiler) << "Failed to save profile info to " << filename;
141 }
142 return result;
143}
144
Calin Juravle64142952016-03-21 14:37:55 +0000145// Returns true if all the bytes were successfully written to the file descriptor.
146static bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) {
147 while (byte_count > 0) {
148 int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count));
149 if (bytes_written == -1) {
Calin Juravle877fd962016-01-05 14:29:29 +0000150 return false;
151 }
Calin Juravle64142952016-03-21 14:37:55 +0000152 byte_count -= bytes_written; // Reduce the number of remaining bytes.
153 buffer += bytes_written; // Move the buffer forward.
154 }
Calin Juravle877fd962016-01-05 14:29:29 +0000155 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +0100156}
157
Calin Juravle64142952016-03-21 14:37:55 +0000158// Add the string bytes to the buffer.
159static void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) {
160 buffer->insert(buffer->end(), value.begin(), value.end());
161}
162
163// Insert each byte, from low to high into the buffer.
164template <typename T>
165static void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) {
166 for (size_t i = 0; i < sizeof(T); i++) {
167 buffer->push_back((value >> (i * kBitsPerByte)) & 0xff);
168 }
169}
170
171static constexpr size_t kLineHeaderSize =
172 3 * sizeof(uint16_t) + // method_set.size + class_set.size + dex_location.size
173 sizeof(uint32_t); // checksum
Calin Juravle31f2c152015-10-23 17:56:15 +0100174
175/**
176 * Serialization format:
Calin Juravle64142952016-03-21 14:37:55 +0000177 * magic,version,number_of_lines
178 * dex_location1,number_of_methods1,number_of_classes1,dex_location_checksum1, \
179 * method_id11,method_id12...,class_id1,class_id2...
180 * dex_location2,number_of_methods2,number_of_classes2,dex_location_checksum2, \
181 * method_id21,method_id22...,,class_id1,class_id2...
182 * .....
Calin Juravle31f2c152015-10-23 17:56:15 +0100183 **/
Calin Juravle2e2db782016-02-23 12:00:03 +0000184bool ProfileCompilationInfo::Save(int fd) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800185 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000186 DCHECK_GE(fd, 0);
Calin Juravle64142952016-03-21 14:37:55 +0000187
188 // Cache at most 5KB before writing.
189 static constexpr size_t kMaxSizeToKeepBeforeWriting = 5 * KB;
190 // Use a vector wrapper to avoid keeping track of offsets when we add elements.
191 std::vector<uint8_t> buffer;
192 WriteBuffer(fd, kProfileMagic, sizeof(kProfileMagic));
193 WriteBuffer(fd, kProfileVersion, sizeof(kProfileVersion));
194 AddUintToBuffer(&buffer, static_cast<uint16_t>(info_.size()));
195
Calin Juravle998c2162015-12-21 15:39:33 +0200196 for (const auto& it : info_) {
Calin Juravle64142952016-03-21 14:37:55 +0000197 if (buffer.size() > kMaxSizeToKeepBeforeWriting) {
198 if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
199 return false;
200 }
201 buffer.clear();
202 }
Calin Juravle998c2162015-12-21 15:39:33 +0200203 const std::string& dex_location = it.first;
204 const DexFileData& dex_data = it.second;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800205 if (dex_data.method_set.empty() && dex_data.class_set.empty()) {
206 continue;
207 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100208
Calin Juravle64142952016-03-21 14:37:55 +0000209 if (dex_location.size() >= kMaxDexFileKeyLength) {
210 LOG(WARNING) << "DexFileKey exceeds allocated limit";
211 return false;
212 }
213
214 // Make sure that the buffer has enough capacity to avoid repeated resizings
215 // while we add data.
216 size_t required_capacity = buffer.size() +
217 kLineHeaderSize +
218 dex_location.size() +
219 sizeof(uint16_t) * (dex_data.class_set.size() + dex_data.method_set.size());
220
221 buffer.reserve(required_capacity);
222
223 DCHECK_LE(dex_location.size(), std::numeric_limits<uint16_t>::max());
224 DCHECK_LE(dex_data.method_set.size(), std::numeric_limits<uint16_t>::max());
225 DCHECK_LE(dex_data.class_set.size(), std::numeric_limits<uint16_t>::max());
226 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_location.size()));
227 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.method_set.size()));
228 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.class_set.size()));
229 AddUintToBuffer(&buffer, dex_data.checksum); // uint32_t
230
231 AddStringToBuffer(&buffer, dex_location);
232
Calin Juravle998c2162015-12-21 15:39:33 +0200233 for (auto method_it : dex_data.method_set) {
Calin Juravle64142952016-03-21 14:37:55 +0000234 AddUintToBuffer(&buffer, method_it);
Calin Juravle31f2c152015-10-23 17:56:15 +0100235 }
Calin Juravle64142952016-03-21 14:37:55 +0000236 for (auto class_id : dex_data.class_set) {
237 AddUintToBuffer(&buffer, class_id);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800238 }
Calin Juravle64142952016-03-21 14:37:55 +0000239 DCHECK_EQ(required_capacity, buffer.size())
240 << "Failed to add the expected number of bytes in the buffer";
Calin Juravle31f2c152015-10-23 17:56:15 +0100241 }
242
Calin Juravle64142952016-03-21 14:37:55 +0000243 return WriteBuffer(fd, buffer.data(), buffer.size());
Calin Juravle226501b2015-12-11 14:41:31 +0000244}
245
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800246ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData(
247 const std::string& dex_location,
248 uint32_t checksum) {
Calin Juravle998c2162015-12-21 15:39:33 +0200249 auto info_it = info_.find(dex_location);
250 if (info_it == info_.end()) {
251 info_it = info_.Put(dex_location, DexFileData(checksum));
252 }
253 if (info_it->second.checksum != checksum) {
254 LOG(WARNING) << "Checksum mismatch for dex " << dex_location;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800255 return nullptr;
256 }
257 return &info_it->second;
258}
259
260bool ProfileCompilationInfo::AddResolvedClasses(const DexCacheResolvedClasses& classes) {
261 const std::string dex_location = GetProfileDexFileKey(classes.GetDexLocation());
262 const uint32_t checksum = classes.GetLocationChecksum();
263 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
264 if (data == nullptr) {
Calin Juravle998c2162015-12-21 15:39:33 +0200265 return false;
266 }
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800267 data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end());
268 return true;
269}
270
271bool ProfileCompilationInfo::AddMethodIndex(const std::string& dex_location,
272 uint32_t checksum,
273 uint16_t method_idx) {
274 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
275 if (data == nullptr) {
276 return false;
277 }
278 data->method_set.insert(method_idx);
279 return true;
280}
281
282bool ProfileCompilationInfo::AddClassIndex(const std::string& dex_location,
283 uint32_t checksum,
284 uint16_t class_idx) {
285 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
286 if (data == nullptr) {
287 return false;
288 }
289 data->class_set.insert(class_idx);
Calin Juravle998c2162015-12-21 15:39:33 +0200290 return true;
291}
292
Calin Juravle64142952016-03-21 14:37:55 +0000293bool ProfileCompilationInfo::ProcessLine(SafeBuffer& line_buffer,
294 uint16_t method_set_size,
295 uint16_t class_set_size,
296 uint32_t checksum,
297 const std::string& dex_location) {
298 for (uint16_t i = 0; i < method_set_size; i++) {
299 uint16_t method_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800300 if (!AddMethodIndex(dex_location, checksum, method_idx)) {
Calin Juravle877fd962016-01-05 14:29:29 +0000301 return false;
302 }
Calin Juravle226501b2015-12-11 14:41:31 +0000303 }
Calin Juravle64142952016-03-21 14:37:55 +0000304
305 for (uint16_t i = 0; i < class_set_size; i++) {
306 uint16_t class_def_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
307 if (!AddClassIndex(dex_location, checksum, class_def_idx)) {
308 return false;
309 }
310 }
Calin Juravle226501b2015-12-11 14:41:31 +0000311 return true;
312}
313
Calin Juravle64142952016-03-21 14:37:55 +0000314// Tests for EOF by trying to read 1 byte from the descriptor.
315// Returns:
316// 0 if the descriptor is at the EOF,
317// -1 if there was an IO error
318// 1 if the descriptor has more content to read
319static int testEOF(int fd) {
320 uint8_t buffer[1];
321 return TEMP_FAILURE_RETRY(read(fd, buffer, 1));
322}
323
324// Reads an uint value previously written with AddUintToBuffer.
325template <typename T>
326T ProfileCompilationInfo::SafeBuffer::ReadUintAndAdvance() {
327 static_assert(std::is_unsigned<T>::value, "Type is not unsigned");
328 CHECK_LE(ptr_current_ + sizeof(T), ptr_end_);
329 T value = 0;
330 for (size_t i = 0; i < sizeof(T); i++) {
331 value += ptr_current_[i] << (i * kBitsPerByte);
Calin Juravle226501b2015-12-11 14:41:31 +0000332 }
Calin Juravle64142952016-03-21 14:37:55 +0000333 ptr_current_ += sizeof(T);
334 return value;
335}
336
337bool ProfileCompilationInfo::SafeBuffer::CompareAndAdvance(const uint8_t* data, size_t data_size) {
338 if (ptr_current_ + data_size > ptr_end_) {
339 return false;
340 }
341 if (memcmp(ptr_current_, data, data_size) == 0) {
342 ptr_current_ += data_size;
343 return true;
344 }
345 return false;
346}
347
348ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::SafeBuffer::FillFromFd(
349 int fd,
350 const std::string& source,
351 /*out*/std::string* error) {
352 size_t byte_count = ptr_end_ - ptr_current_;
353 uint8_t* buffer = ptr_current_;
354 while (byte_count > 0) {
355 int bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, byte_count));
356 if (bytes_read == 0) {
357 *error += "Profile EOF reached prematurely for " + source;
358 return kProfileLoadBadData;
359 } else if (bytes_read < 0) {
360 *error += "Profile IO error for " + source + strerror(errno);
361 return kProfileLoadIOError;
Calin Juravle226501b2015-12-11 14:41:31 +0000362 }
Calin Juravle64142952016-03-21 14:37:55 +0000363 byte_count -= bytes_read;
364 buffer += bytes_read;
Calin Juravle226501b2015-12-11 14:41:31 +0000365 }
Calin Juravle64142952016-03-21 14:37:55 +0000366 return kProfileLoadSuccess;
367}
368
369ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileHeader(
370 int fd,
371 /*out*/uint16_t* number_of_lines,
372 /*out*/std::string* error) {
373 // Read magic and version
374 const size_t kMagicVersionSize =
375 sizeof(kProfileMagic) +
376 sizeof(kProfileVersion) +
377 sizeof(uint16_t); // number of lines
378
379 SafeBuffer safe_buffer(kMagicVersionSize);
380
381 ProfileLoadSatus status = safe_buffer.FillFromFd(fd, "ReadProfileHeader", error);
382 if (status != kProfileLoadSuccess) {
383 return status;
384 }
385
386 if (!safe_buffer.CompareAndAdvance(kProfileMagic, sizeof(kProfileMagic))) {
387 *error = "Profile missing magic";
388 return kProfileLoadVersionMismatch;
389 }
390 if (!safe_buffer.CompareAndAdvance(kProfileVersion, sizeof(kProfileVersion))) {
391 *error = "Profile version mismatch";
392 return kProfileLoadVersionMismatch;
393 }
394 *number_of_lines = safe_buffer.ReadUintAndAdvance<uint16_t>();
395 return kProfileLoadSuccess;
396}
397
398ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLineHeader(
399 int fd,
400 /*out*/ProfileLineHeader* line_header,
401 /*out*/std::string* error) {
402 SafeBuffer header_buffer(kLineHeaderSize);
403 ProfileLoadSatus status = header_buffer.FillFromFd(fd, "ReadProfileHeader", error);
404 if (status != kProfileLoadSuccess) {
405 return status;
406 }
407
408 uint16_t dex_location_size = header_buffer.ReadUintAndAdvance<uint16_t>();
409 line_header->method_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
410 line_header->class_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
411 line_header->checksum = header_buffer.ReadUintAndAdvance<uint32_t>();
412
413 if (dex_location_size == 0 || dex_location_size > kMaxDexFileKeyLength) {
Goran Jakovljevic4eb6fbf2016-04-25 19:14:17 +0200414 *error = "DexFileKey has an invalid size: " +
415 std::to_string(static_cast<uint32_t>(dex_location_size));
Calin Juravle64142952016-03-21 14:37:55 +0000416 return kProfileLoadBadData;
417 }
418
419 SafeBuffer location_buffer(dex_location_size);
420 status = location_buffer.FillFromFd(fd, "ReadProfileHeaderDexLocation", error);
421 if (status != kProfileLoadSuccess) {
422 return status;
423 }
424 line_header->dex_location.assign(
425 reinterpret_cast<char*>(location_buffer.Get()), dex_location_size);
426 return kProfileLoadSuccess;
427}
428
429ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLine(
430 int fd,
431 const ProfileLineHeader& line_header,
432 /*out*/std::string* error) {
433 // Make sure that we don't try to read everything in memory (in case the profile if full).
434 // Split readings in chunks of at most 10kb.
435 static constexpr uint16_t kMaxNumberOfEntriesToRead = 5120;
436 uint16_t methods_left_to_read = line_header.method_set_size;
437 uint16_t classes_left_to_read = line_header.class_set_size;
438
439 while ((methods_left_to_read > 0) || (classes_left_to_read > 0)) {
440 uint16_t methods_to_read = std::min(kMaxNumberOfEntriesToRead, methods_left_to_read);
441 uint16_t max_classes_to_read = kMaxNumberOfEntriesToRead - methods_to_read;
442 uint16_t classes_to_read = std::min(max_classes_to_read, classes_left_to_read);
443
444 size_t line_size = sizeof(uint16_t) * (methods_to_read + classes_to_read);
445 SafeBuffer line_buffer(line_size);
446
447 ProfileLoadSatus status = line_buffer.FillFromFd(fd, "ReadProfileLine", error);
448 if (status != kProfileLoadSuccess) {
449 return status;
450 }
451 if (!ProcessLine(line_buffer,
452 methods_to_read,
453 classes_to_read,
454 line_header.checksum,
455 line_header.dex_location)) {
456 *error = "Error when reading profile file line";
457 return kProfileLoadBadData;
458 }
459 methods_left_to_read -= methods_to_read;
460 classes_left_to_read -= classes_to_read;
461 }
462 return kProfileLoadSuccess;
Calin Juravle226501b2015-12-11 14:41:31 +0000463}
464
Calin Juravle2e2db782016-02-23 12:00:03 +0000465bool ProfileCompilationInfo::Load(int fd) {
Calin Juravle64142952016-03-21 14:37:55 +0000466 std::string error;
467 ProfileLoadSatus status = LoadInternal(fd, &error);
468
469 if (status == kProfileLoadSuccess) {
470 return true;
471 } else {
472 PLOG(WARNING) << "Error when reading profile " << error;
473 return false;
474 }
475}
476
477ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::LoadInternal(
478 int fd, std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800479 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000480 DCHECK_GE(fd, 0);
Calin Juravle226501b2015-12-11 14:41:31 +0000481
Calin Juravle64142952016-03-21 14:37:55 +0000482 struct stat stat_buffer;
483 if (fstat(fd, &stat_buffer) != 0) {
484 return kProfileLoadIOError;
Calin Juravle226501b2015-12-11 14:41:31 +0000485 }
Calin Juravle64142952016-03-21 14:37:55 +0000486 // We allow empty profile files.
487 // Profiles may be created by ActivityManager or installd before we manage to
488 // process them in the runtime or profman.
489 if (stat_buffer.st_size == 0) {
490 return kProfileLoadSuccess;
491 }
492 // Read profile header: magic + version + number_of_lines.
493 uint16_t number_of_lines;
494 ProfileLoadSatus status = ReadProfileHeader(fd, &number_of_lines, error);
495 if (status != kProfileLoadSuccess) {
496 return status;
497 }
498
499 while (number_of_lines > 0) {
500 ProfileLineHeader line_header;
501 // First, read the line header to get the amount of data we need to read.
502 status = ReadProfileLineHeader(fd, &line_header, error);
503 if (status != kProfileLoadSuccess) {
504 return status;
505 }
506
507 // Now read the actual profile line.
508 status = ReadProfileLine(fd, line_header, error);
509 if (status != kProfileLoadSuccess) {
510 return status;
511 }
512 number_of_lines--;
513 }
514
515 // Check that we read everything and that profiles don't contain junk data.
516 int result = testEOF(fd);
517 if (result == 0) {
518 return kProfileLoadSuccess;
519 } else if (result < 0) {
520 return kProfileLoadIOError;
521 } else {
522 *error = "Unexpected content in the profile file";
523 return kProfileLoadBadData;
524 }
Calin Juravle998c2162015-12-21 15:39:33 +0200525}
526
Calin Juravle67265462016-03-18 16:23:40 +0000527bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other) {
Calin Juravle5d1bd0a2016-03-24 20:33:22 +0000528 // First verify that all checksums match. This will avoid adding garbage to
529 // the current profile info.
530 // Note that the number of elements should be very small, so this should not
531 // be a performance issue.
532 for (const auto& other_it : other.info_) {
533 auto info_it = info_.find(other_it.first);
534 if ((info_it != info_.end()) && (info_it->second.checksum != other_it.second.checksum)) {
535 LOG(WARNING) << "Checksum mismatch for dex " << other_it.first;
536 return false;
537 }
538 }
539 // All checksums match. Import the data.
Calin Juravle998c2162015-12-21 15:39:33 +0200540 for (const auto& other_it : other.info_) {
541 const std::string& other_dex_location = other_it.first;
542 const DexFileData& other_dex_data = other_it.second;
Calin Juravle998c2162015-12-21 15:39:33 +0200543 auto info_it = info_.find(other_dex_location);
544 if (info_it == info_.end()) {
545 info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum));
546 }
Calin Juravle998c2162015-12-21 15:39:33 +0200547 info_it->second.method_set.insert(other_dex_data.method_set.begin(),
548 other_dex_data.method_set.end());
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800549 info_it->second.class_set.insert(other_dex_data.class_set.begin(),
550 other_dex_data.class_set.end());
Calin Juravle998c2162015-12-21 15:39:33 +0200551 }
552 return true;
Calin Juravle226501b2015-12-11 14:41:31 +0000553}
554
Calin Juravlec4588572016-06-08 14:24:13 +0100555static bool ChecksumMatch(const DexFile& dex_file, uint32_t checksum) {
556 return kDebugIgnoreChecksum || dex_file.GetLocationChecksum() == checksum;
557}
558
Calin Juravle226501b2015-12-11 14:41:31 +0000559bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
Calin Juravle34900cc2016-02-05 16:19:19 +0000560 auto info_it = info_.find(GetProfileDexFileKey(method_ref.dex_file->GetLocation()));
Calin Juravle226501b2015-12-11 14:41:31 +0000561 if (info_it != info_.end()) {
Calin Juravlec4588572016-06-08 14:24:13 +0100562 if (!ChecksumMatch(*method_ref.dex_file, info_it->second.checksum)) {
Calin Juravle998c2162015-12-21 15:39:33 +0200563 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000564 }
Calin Juravle998c2162015-12-21 15:39:33 +0200565 const std::set<uint16_t>& methods = info_it->second.method_set;
566 return methods.find(method_ref.dex_method_index) != methods.end();
Calin Juravle226501b2015-12-11 14:41:31 +0000567 }
568 return false;
569}
570
Mathieu Chartiera8077802016-03-16 19:08:31 -0700571bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const {
572 auto info_it = info_.find(GetProfileDexFileKey(dex_file.GetLocation()));
573 if (info_it != info_.end()) {
Calin Juravlec4588572016-06-08 14:24:13 +0100574 if (!ChecksumMatch(dex_file, info_it->second.checksum)) {
Mathieu Chartiera8077802016-03-16 19:08:31 -0700575 return false;
576 }
577 const std::set<uint16_t>& classes = info_it->second.class_set;
578 return classes.find(class_def_idx) != classes.end();
579 }
580 return false;
581}
582
Calin Juravle998c2162015-12-21 15:39:33 +0200583uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
584 uint32_t total = 0;
585 for (const auto& it : info_) {
586 total += it.second.method_set.size();
587 }
588 return total;
589}
590
Calin Juravle67265462016-03-18 16:23:40 +0000591uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
592 uint32_t total = 0;
593 for (const auto& it : info_) {
594 total += it.second.class_set.size();
595 }
596 return total;
597}
598
Calin Juravle998c2162015-12-21 15:39:33 +0200599std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
600 bool print_full_dex_location) const {
Calin Juravle226501b2015-12-11 14:41:31 +0000601 std::ostringstream os;
602 if (info_.empty()) {
603 return "ProfileInfo: empty";
604 }
605
606 os << "ProfileInfo:";
607
Calin Juravle226501b2015-12-11 14:41:31 +0000608 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
Calin Juravle998c2162015-12-21 15:39:33 +0200609 for (const auto& it : info_) {
Calin Juravle226501b2015-12-11 14:41:31 +0000610 os << "\n";
Calin Juravle998c2162015-12-21 15:39:33 +0200611 const std::string& location = it.first;
612 const DexFileData& dex_data = it.second;
Calin Juravle226501b2015-12-11 14:41:31 +0000613 if (print_full_dex_location) {
614 os << location;
615 } else {
616 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
617 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
618 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
619 }
Calin Juravle876f3502016-03-24 16:16:34 +0000620 const DexFile* dex_file = nullptr;
621 if (dex_files != nullptr) {
622 for (size_t i = 0; i < dex_files->size(); i++) {
623 if (location == (*dex_files)[i]->GetLocation()) {
624 dex_file = (*dex_files)[i];
Calin Juravle998c2162015-12-21 15:39:33 +0200625 }
Calin Juravle226501b2015-12-11 14:41:31 +0000626 }
Calin Juravle876f3502016-03-24 16:16:34 +0000627 }
628 os << "\n\tmethods: ";
629 for (const auto method_it : dex_data.method_set) {
630 if (dex_file != nullptr) {
631 os << "\n\t\t" << PrettyMethod(method_it, *dex_file, true);
632 } else {
633 os << method_it << ",";
634 }
635 }
636 os << "\n\tclasses: ";
637 for (const auto class_it : dex_data.class_set) {
638 if (dex_file != nullptr) {
639 os << "\n\t\t" << PrettyType(class_it, *dex_file);
640 } else {
641 os << class_it << ",";
642 }
Calin Juravle226501b2015-12-11 14:41:31 +0000643 }
644 }
645 return os.str();
646}
647
Calin Juravle2e2db782016-02-23 12:00:03 +0000648bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) {
Calin Juravle877fd962016-01-05 14:29:29 +0000649 return info_.Equals(other.info_);
650}
651
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800652std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses() const {
653 std::set<DexCacheResolvedClasses> ret;
654 for (auto&& pair : info_) {
655 const std::string& profile_key = pair.first;
656 const DexFileData& data = pair.second;
Mathieu Chartierb384e5e2016-04-29 12:03:56 -0700657 // TODO: Is it OK to use the same location for both base and dex location here?
658 DexCacheResolvedClasses classes(profile_key, profile_key, data.checksum);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800659 classes.AddClasses(data.class_set.begin(), data.class_set.end());
660 ret.insert(classes);
661 }
662 return ret;
663}
664
Calin Juravle67265462016-03-18 16:23:40 +0000665void ProfileCompilationInfo::ClearResolvedClasses() {
666 for (auto& pair : info_) {
667 pair.second.class_set.clear();
668 }
669}
670
Calin Juravle31f2c152015-10-23 17:56:15 +0100671} // namespace art