blob: a3078bc1babf4ddca202363089b8e3eed4bd5cdf [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(
Calin Juravle99629622016-04-19 16:33:46 +010059 const std::vector<MethodReference>& methods,
Calin Juravle67265462016-03-18 16:23:40 +000060 const std::set<DexCacheResolvedClasses>& resolved_classes) {
Calin Juravle99629622016-04-19 16:33:46 +010061 for (const MethodReference& method : methods) {
62 if (!AddMethodIndex(GetProfileDexFileKey(method.dex_file->GetLocation()),
63 method.dex_file->GetLocationChecksum(),
64 method.dex_method_index)) {
Calin Juravle67265462016-03-18 16:23:40 +000065 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
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000076bool ProfileCompilationInfo::MergeAndSave(const std::string& filename,
77 uint64_t* bytes_written,
78 bool force) {
Calin Juravle67265462016-03-18 16:23:40 +000079 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;
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000091 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;
Calin Juravle67265462016-03-18 16:23:40 +0000116 return false;
117 }
118
Calin Juravle5d1bd0a2016-03-24 20:33:22 +0000119 // We need to clear the data because we don't support appending to the profiles yet.
Calin Juravle67265462016-03-18 16:23:40 +0000120 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
Calin Juravle64142952016-03-21 14:37:55 +0000140// Returns true if all the bytes were successfully written to the file descriptor.
141static 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) {
Calin Juravle877fd962016-01-05 14:29:29 +0000145 return false;
146 }
Calin Juravle64142952016-03-21 14:37:55 +0000147 byte_count -= bytes_written; // Reduce the number of remaining bytes.
148 buffer += bytes_written; // Move the buffer forward.
149 }
Calin Juravle877fd962016-01-05 14:29:29 +0000150 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +0100151}
152
Calin Juravle64142952016-03-21 14:37:55 +0000153// Add the string bytes to the buffer.
154static 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.
159template <typename T>
160static 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
166static constexpr size_t kLineHeaderSize =
167 3 * sizeof(uint16_t) + // method_set.size + class_set.size + dex_location.size
168 sizeof(uint32_t); // checksum
Calin Juravle31f2c152015-10-23 17:56:15 +0100169
170/**
171 * Serialization format:
Calin Juravle64142952016-03-21 14:37:55 +0000172 * 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 * .....
Calin Juravle31f2c152015-10-23 17:56:15 +0100178 **/
Calin Juravle2e2db782016-02-23 12:00:03 +0000179bool ProfileCompilationInfo::Save(int fd) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800180 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000181 DCHECK_GE(fd, 0);
Calin Juravle64142952016-03-21 14:37:55 +0000182
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
Calin Juravle998c2162015-12-21 15:39:33 +0200191 for (const auto& it : info_) {
Calin Juravle64142952016-03-21 14:37:55 +0000192 if (buffer.size() > kMaxSizeToKeepBeforeWriting) {
193 if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
194 return false;
195 }
196 buffer.clear();
197 }
Calin Juravle998c2162015-12-21 15:39:33 +0200198 const std::string& dex_location = it.first;
199 const DexFileData& dex_data = it.second;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800200 if (dex_data.method_set.empty() && dex_data.class_set.empty()) {
201 continue;
202 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100203
Calin Juravle64142952016-03-21 14:37:55 +0000204 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
Calin Juravle998c2162015-12-21 15:39:33 +0200228 for (auto method_it : dex_data.method_set) {
Calin Juravle64142952016-03-21 14:37:55 +0000229 AddUintToBuffer(&buffer, method_it);
Calin Juravle31f2c152015-10-23 17:56:15 +0100230 }
Calin Juravle64142952016-03-21 14:37:55 +0000231 for (auto class_id : dex_data.class_set) {
232 AddUintToBuffer(&buffer, class_id);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800233 }
Calin Juravle64142952016-03-21 14:37:55 +0000234 DCHECK_EQ(required_capacity, buffer.size())
235 << "Failed to add the expected number of bytes in the buffer";
Calin Juravle31f2c152015-10-23 17:56:15 +0100236 }
237
Calin Juravle64142952016-03-21 14:37:55 +0000238 return WriteBuffer(fd, buffer.data(), buffer.size());
Calin Juravle226501b2015-12-11 14:41:31 +0000239}
240
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800241ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData(
242 const std::string& dex_location,
243 uint32_t checksum) {
Calin Juravle998c2162015-12-21 15:39:33 +0200244 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;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800250 return nullptr;
251 }
252 return &info_it->second;
253}
254
255bool 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) {
Calin Juravle998c2162015-12-21 15:39:33 +0200260 return false;
261 }
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800262 data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end());
263 return true;
264}
265
266bool 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
277bool 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);
Calin Juravle998c2162015-12-21 15:39:33 +0200285 return true;
286}
287
Calin Juravle64142952016-03-21 14:37:55 +0000288bool 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>();
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800295 if (!AddMethodIndex(dex_location, checksum, method_idx)) {
Calin Juravle877fd962016-01-05 14:29:29 +0000296 return false;
297 }
Calin Juravle226501b2015-12-11 14:41:31 +0000298 }
Calin Juravle64142952016-03-21 14:37:55 +0000299
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 }
Calin Juravle226501b2015-12-11 14:41:31 +0000306 return true;
307}
308
Calin Juravle64142952016-03-21 14:37:55 +0000309// 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
314static 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.
320template <typename T>
321T 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);
Calin Juravle226501b2015-12-11 14:41:31 +0000327 }
Calin Juravle64142952016-03-21 14:37:55 +0000328 ptr_current_ += sizeof(T);
329 return value;
330}
331
332bool 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
343ProfileCompilationInfo::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;
Calin Juravle226501b2015-12-11 14:41:31 +0000357 }
Calin Juravle64142952016-03-21 14:37:55 +0000358 byte_count -= bytes_read;
359 buffer += bytes_read;
Calin Juravle226501b2015-12-11 14:41:31 +0000360 }
Calin Juravle64142952016-03-21 14:37:55 +0000361 return kProfileLoadSuccess;
362}
363
364ProfileCompilationInfo::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
393ProfileCompilationInfo::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) {
Goran Jakovljevic4eb6fbf2016-04-25 19:14:17 +0200409 *error = "DexFileKey has an invalid size: " +
410 std::to_string(static_cast<uint32_t>(dex_location_size));
Calin Juravle64142952016-03-21 14:37:55 +0000411 return kProfileLoadBadData;
412 }
413
414 SafeBuffer location_buffer(dex_location_size);
415 status = location_buffer.FillFromFd(fd, "ReadProfileHeaderDexLocation", error);
416 if (status != kProfileLoadSuccess) {
417 return status;
418 }
419 line_header->dex_location.assign(
420 reinterpret_cast<char*>(location_buffer.Get()), dex_location_size);
421 return kProfileLoadSuccess;
422}
423
424ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLine(
425 int fd,
426 const ProfileLineHeader& line_header,
427 /*out*/std::string* error) {
428 // Make sure that we don't try to read everything in memory (in case the profile if full).
429 // Split readings in chunks of at most 10kb.
430 static constexpr uint16_t kMaxNumberOfEntriesToRead = 5120;
431 uint16_t methods_left_to_read = line_header.method_set_size;
432 uint16_t classes_left_to_read = line_header.class_set_size;
433
434 while ((methods_left_to_read > 0) || (classes_left_to_read > 0)) {
435 uint16_t methods_to_read = std::min(kMaxNumberOfEntriesToRead, methods_left_to_read);
436 uint16_t max_classes_to_read = kMaxNumberOfEntriesToRead - methods_to_read;
437 uint16_t classes_to_read = std::min(max_classes_to_read, classes_left_to_read);
438
439 size_t line_size = sizeof(uint16_t) * (methods_to_read + classes_to_read);
440 SafeBuffer line_buffer(line_size);
441
442 ProfileLoadSatus status = line_buffer.FillFromFd(fd, "ReadProfileLine", error);
443 if (status != kProfileLoadSuccess) {
444 return status;
445 }
446 if (!ProcessLine(line_buffer,
447 methods_to_read,
448 classes_to_read,
449 line_header.checksum,
450 line_header.dex_location)) {
451 *error = "Error when reading profile file line";
452 return kProfileLoadBadData;
453 }
454 methods_left_to_read -= methods_to_read;
455 classes_left_to_read -= classes_to_read;
456 }
457 return kProfileLoadSuccess;
Calin Juravle226501b2015-12-11 14:41:31 +0000458}
459
Calin Juravle2e2db782016-02-23 12:00:03 +0000460bool ProfileCompilationInfo::Load(int fd) {
Calin Juravle64142952016-03-21 14:37:55 +0000461 std::string error;
462 ProfileLoadSatus status = LoadInternal(fd, &error);
463
464 if (status == kProfileLoadSuccess) {
465 return true;
466 } else {
467 PLOG(WARNING) << "Error when reading profile " << error;
468 return false;
469 }
470}
471
472ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::LoadInternal(
473 int fd, std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800474 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000475 DCHECK_GE(fd, 0);
Calin Juravle226501b2015-12-11 14:41:31 +0000476
Calin Juravle64142952016-03-21 14:37:55 +0000477 struct stat stat_buffer;
478 if (fstat(fd, &stat_buffer) != 0) {
479 return kProfileLoadIOError;
Calin Juravle226501b2015-12-11 14:41:31 +0000480 }
Calin Juravle64142952016-03-21 14:37:55 +0000481 // We allow empty profile files.
482 // Profiles may be created by ActivityManager or installd before we manage to
483 // process them in the runtime or profman.
484 if (stat_buffer.st_size == 0) {
485 return kProfileLoadSuccess;
486 }
487 // Read profile header: magic + version + number_of_lines.
488 uint16_t number_of_lines;
489 ProfileLoadSatus status = ReadProfileHeader(fd, &number_of_lines, error);
490 if (status != kProfileLoadSuccess) {
491 return status;
492 }
493
494 while (number_of_lines > 0) {
495 ProfileLineHeader line_header;
496 // First, read the line header to get the amount of data we need to read.
497 status = ReadProfileLineHeader(fd, &line_header, error);
498 if (status != kProfileLoadSuccess) {
499 return status;
500 }
501
502 // Now read the actual profile line.
503 status = ReadProfileLine(fd, line_header, error);
504 if (status != kProfileLoadSuccess) {
505 return status;
506 }
507 number_of_lines--;
508 }
509
510 // Check that we read everything and that profiles don't contain junk data.
511 int result = testEOF(fd);
512 if (result == 0) {
513 return kProfileLoadSuccess;
514 } else if (result < 0) {
515 return kProfileLoadIOError;
516 } else {
517 *error = "Unexpected content in the profile file";
518 return kProfileLoadBadData;
519 }
Calin Juravle998c2162015-12-21 15:39:33 +0200520}
521
Calin Juravle67265462016-03-18 16:23:40 +0000522bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other) {
Calin Juravle5d1bd0a2016-03-24 20:33:22 +0000523 // First verify that all checksums match. This will avoid adding garbage to
524 // the current profile info.
525 // Note that the number of elements should be very small, so this should not
526 // be a performance issue.
527 for (const auto& other_it : other.info_) {
528 auto info_it = info_.find(other_it.first);
529 if ((info_it != info_.end()) && (info_it->second.checksum != other_it.second.checksum)) {
530 LOG(WARNING) << "Checksum mismatch for dex " << other_it.first;
531 return false;
532 }
533 }
534 // All checksums match. Import the data.
Calin Juravle998c2162015-12-21 15:39:33 +0200535 for (const auto& other_it : other.info_) {
536 const std::string& other_dex_location = other_it.first;
537 const DexFileData& other_dex_data = other_it.second;
Calin Juravle998c2162015-12-21 15:39:33 +0200538 auto info_it = info_.find(other_dex_location);
539 if (info_it == info_.end()) {
540 info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum));
541 }
Calin Juravle998c2162015-12-21 15:39:33 +0200542 info_it->second.method_set.insert(other_dex_data.method_set.begin(),
543 other_dex_data.method_set.end());
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800544 info_it->second.class_set.insert(other_dex_data.class_set.begin(),
545 other_dex_data.class_set.end());
Calin Juravle998c2162015-12-21 15:39:33 +0200546 }
547 return true;
Calin Juravle226501b2015-12-11 14:41:31 +0000548}
549
550bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
Calin Juravle34900cc2016-02-05 16:19:19 +0000551 auto info_it = info_.find(GetProfileDexFileKey(method_ref.dex_file->GetLocation()));
Calin Juravle226501b2015-12-11 14:41:31 +0000552 if (info_it != info_.end()) {
Calin Juravle998c2162015-12-21 15:39:33 +0200553 if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) {
554 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000555 }
Calin Juravle998c2162015-12-21 15:39:33 +0200556 const std::set<uint16_t>& methods = info_it->second.method_set;
557 return methods.find(method_ref.dex_method_index) != methods.end();
Calin Juravle226501b2015-12-11 14:41:31 +0000558 }
559 return false;
560}
561
Mathieu Chartiera8077802016-03-16 19:08:31 -0700562bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const {
563 auto info_it = info_.find(GetProfileDexFileKey(dex_file.GetLocation()));
564 if (info_it != info_.end()) {
565 if (dex_file.GetLocationChecksum() != info_it->second.checksum) {
566 return false;
567 }
568 const std::set<uint16_t>& classes = info_it->second.class_set;
569 return classes.find(class_def_idx) != classes.end();
570 }
571 return false;
572}
573
Calin Juravle998c2162015-12-21 15:39:33 +0200574uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
575 uint32_t total = 0;
576 for (const auto& it : info_) {
577 total += it.second.method_set.size();
578 }
579 return total;
580}
581
Calin Juravle67265462016-03-18 16:23:40 +0000582uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
583 uint32_t total = 0;
584 for (const auto& it : info_) {
585 total += it.second.class_set.size();
586 }
587 return total;
588}
589
Calin Juravle998c2162015-12-21 15:39:33 +0200590std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
591 bool print_full_dex_location) const {
Calin Juravle226501b2015-12-11 14:41:31 +0000592 std::ostringstream os;
593 if (info_.empty()) {
594 return "ProfileInfo: empty";
595 }
596
597 os << "ProfileInfo:";
598
Calin Juravle226501b2015-12-11 14:41:31 +0000599 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
Calin Juravle998c2162015-12-21 15:39:33 +0200600 for (const auto& it : info_) {
Calin Juravle226501b2015-12-11 14:41:31 +0000601 os << "\n";
Calin Juravle998c2162015-12-21 15:39:33 +0200602 const std::string& location = it.first;
603 const DexFileData& dex_data = it.second;
Calin Juravle226501b2015-12-11 14:41:31 +0000604 if (print_full_dex_location) {
605 os << location;
606 } else {
607 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
608 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
609 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
610 }
Calin Juravle876f3502016-03-24 16:16:34 +0000611 const DexFile* dex_file = nullptr;
612 if (dex_files != nullptr) {
613 for (size_t i = 0; i < dex_files->size(); i++) {
614 if (location == (*dex_files)[i]->GetLocation()) {
615 dex_file = (*dex_files)[i];
Calin Juravle998c2162015-12-21 15:39:33 +0200616 }
Calin Juravle226501b2015-12-11 14:41:31 +0000617 }
Calin Juravle876f3502016-03-24 16:16:34 +0000618 }
619 os << "\n\tmethods: ";
620 for (const auto method_it : dex_data.method_set) {
621 if (dex_file != nullptr) {
622 os << "\n\t\t" << PrettyMethod(method_it, *dex_file, true);
623 } else {
624 os << method_it << ",";
625 }
626 }
627 os << "\n\tclasses: ";
628 for (const auto class_it : dex_data.class_set) {
629 if (dex_file != nullptr) {
630 os << "\n\t\t" << PrettyType(class_it, *dex_file);
631 } else {
632 os << class_it << ",";
633 }
Calin Juravle226501b2015-12-11 14:41:31 +0000634 }
635 }
636 return os.str();
637}
638
Calin Juravle2e2db782016-02-23 12:00:03 +0000639bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) {
Calin Juravle877fd962016-01-05 14:29:29 +0000640 return info_.Equals(other.info_);
641}
642
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800643std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses() const {
644 std::set<DexCacheResolvedClasses> ret;
645 for (auto&& pair : info_) {
646 const std::string& profile_key = pair.first;
647 const DexFileData& data = pair.second;
648 DexCacheResolvedClasses classes(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
Calin Juravle67265462016-03-18 16:23:40 +0000655void ProfileCompilationInfo::ClearResolvedClasses() {
656 for (auto& pair : info_) {
657 pair.second.class_set.clear();
658 }
659}
660
Calin Juravle31f2c152015-10-23 17:56:15 +0100661} // namespace art