blob: 4ecb9265aa3d6405bb4b73bf94ccbb82e5001433 [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
78bool ProfileCompilationInfo::MergeAndSave(const std::string& filename, uint64_t* bytes_written) {
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 if (!fileInfo.Load(fd)) {
92 LOG(WARNING) << "Could not load previous profile data from file " << filename;
93 return false;
94 }
95
96 // Merge the content of file into the current object.
97 if (!MergeWith(fileInfo)) {
98 LOG(WARNING) << "Could not merge previous profile data from file " << filename;
99 }
100
101 // If after the merge we have the same data as what is the file there's no point
102 // in actually doing the write. The file will be exactly the same as before.
103 if (Equals(fileInfo)) {
104 if (bytes_written != nullptr) {
105 *bytes_written = 0;
106 }
107 return true;
108 }
109
110 // We need to clear the data because we don't support append to the profiles yet.
111 if (!flock.GetFile()->ClearContent()) {
112 PLOG(WARNING) << "Could not clear profile file: " << filename;
113 return false;
114 }
115
116 // This doesn't need locking because we are trying to lock the file for exclusive
117 // access and fail immediately if we can't.
118 bool result = Save(fd);
119 if (result) {
120 VLOG(profiler) << "Successfully saved profile info to " << filename
121 << " Size: " << GetFileSizeBytes(filename);
122 if (bytes_written != nullptr) {
123 *bytes_written = GetFileSizeBytes(filename);
124 }
125 } else {
126 VLOG(profiler) << "Failed to save profile info to " << filename;
127 }
128 return result;
129}
130
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800131bool ProfileCompilationInfo::SaveProfilingInfo(
132 const std::string& filename,
133 const std::vector<ArtMethod*>& methods,
Calin Juravleb8e69992016-03-09 15:37:48 +0000134 const std::set<DexCacheResolvedClasses>& resolved_classes,
135 uint64_t* bytes_written) {
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800136 if (methods.empty() && resolved_classes.empty()) {
Calin Juravle31f2c152015-10-23 17:56:15 +0100137 VLOG(profiler) << "No info to save to " << filename;
Calin Juravleb8e69992016-03-09 15:37:48 +0000138 if (bytes_written != nullptr) {
139 *bytes_written = 0;
140 }
Calin Juravle998c2162015-12-21 15:39:33 +0200141 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +0100142 }
143
Calin Juravle998c2162015-12-21 15:39:33 +0200144 ProfileCompilationInfo info;
Calin Juravle67265462016-03-18 16:23:40 +0000145 if (!info.AddMethodsAndClasses(methods, resolved_classes)) {
146 LOG(WARNING) << "Checksum mismatch when processing methods and resolved classes for "
147 << filename;
Calin Juravleb8e69992016-03-09 15:37:48 +0000148 if (bytes_written != nullptr) {
Calin Juravle67265462016-03-18 16:23:40 +0000149 *bytes_written = 0;
Calin Juravleb8e69992016-03-09 15:37:48 +0000150 }
Calin Juravle67265462016-03-18 16:23:40 +0000151 return false;
Calin Juravle31f2c152015-10-23 17:56:15 +0100152 }
Calin Juravle67265462016-03-18 16:23:40 +0000153 return info.MergeAndSave(filename, bytes_written);
Calin Juravle31f2c152015-10-23 17:56:15 +0100154}
155
Calin Juravle64142952016-03-21 14:37:55 +0000156// Returns true if all the bytes were successfully written to the file descriptor.
157static bool WriteBuffer(int fd, const uint8_t* buffer, size_t byte_count) {
158 while (byte_count > 0) {
159 int bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer, byte_count));
160 if (bytes_written == -1) {
Calin Juravle877fd962016-01-05 14:29:29 +0000161 return false;
162 }
Calin Juravle64142952016-03-21 14:37:55 +0000163 byte_count -= bytes_written; // Reduce the number of remaining bytes.
164 buffer += bytes_written; // Move the buffer forward.
165 }
Calin Juravle877fd962016-01-05 14:29:29 +0000166 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +0100167}
168
Calin Juravle64142952016-03-21 14:37:55 +0000169// Add the string bytes to the buffer.
170static void AddStringToBuffer(std::vector<uint8_t>* buffer, const std::string& value) {
171 buffer->insert(buffer->end(), value.begin(), value.end());
172}
173
174// Insert each byte, from low to high into the buffer.
175template <typename T>
176static void AddUintToBuffer(std::vector<uint8_t>* buffer, T value) {
177 for (size_t i = 0; i < sizeof(T); i++) {
178 buffer->push_back((value >> (i * kBitsPerByte)) & 0xff);
179 }
180}
181
182static constexpr size_t kLineHeaderSize =
183 3 * sizeof(uint16_t) + // method_set.size + class_set.size + dex_location.size
184 sizeof(uint32_t); // checksum
Calin Juravle31f2c152015-10-23 17:56:15 +0100185
186/**
187 * Serialization format:
Calin Juravle64142952016-03-21 14:37:55 +0000188 * magic,version,number_of_lines
189 * dex_location1,number_of_methods1,number_of_classes1,dex_location_checksum1, \
190 * method_id11,method_id12...,class_id1,class_id2...
191 * dex_location2,number_of_methods2,number_of_classes2,dex_location_checksum2, \
192 * method_id21,method_id22...,,class_id1,class_id2...
193 * .....
Calin Juravle31f2c152015-10-23 17:56:15 +0100194 **/
Calin Juravle2e2db782016-02-23 12:00:03 +0000195bool ProfileCompilationInfo::Save(int fd) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800196 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000197 DCHECK_GE(fd, 0);
Calin Juravle64142952016-03-21 14:37:55 +0000198
199 // Cache at most 5KB before writing.
200 static constexpr size_t kMaxSizeToKeepBeforeWriting = 5 * KB;
201 // Use a vector wrapper to avoid keeping track of offsets when we add elements.
202 std::vector<uint8_t> buffer;
203 WriteBuffer(fd, kProfileMagic, sizeof(kProfileMagic));
204 WriteBuffer(fd, kProfileVersion, sizeof(kProfileVersion));
205 AddUintToBuffer(&buffer, static_cast<uint16_t>(info_.size()));
206
Calin Juravle998c2162015-12-21 15:39:33 +0200207 for (const auto& it : info_) {
Calin Juravle64142952016-03-21 14:37:55 +0000208 if (buffer.size() > kMaxSizeToKeepBeforeWriting) {
209 if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
210 return false;
211 }
212 buffer.clear();
213 }
Calin Juravle998c2162015-12-21 15:39:33 +0200214 const std::string& dex_location = it.first;
215 const DexFileData& dex_data = it.second;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800216 if (dex_data.method_set.empty() && dex_data.class_set.empty()) {
217 continue;
218 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100219
Calin Juravle64142952016-03-21 14:37:55 +0000220 if (dex_location.size() >= kMaxDexFileKeyLength) {
221 LOG(WARNING) << "DexFileKey exceeds allocated limit";
222 return false;
223 }
224
225 // Make sure that the buffer has enough capacity to avoid repeated resizings
226 // while we add data.
227 size_t required_capacity = buffer.size() +
228 kLineHeaderSize +
229 dex_location.size() +
230 sizeof(uint16_t) * (dex_data.class_set.size() + dex_data.method_set.size());
231
232 buffer.reserve(required_capacity);
233
234 DCHECK_LE(dex_location.size(), std::numeric_limits<uint16_t>::max());
235 DCHECK_LE(dex_data.method_set.size(), std::numeric_limits<uint16_t>::max());
236 DCHECK_LE(dex_data.class_set.size(), std::numeric_limits<uint16_t>::max());
237 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_location.size()));
238 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.method_set.size()));
239 AddUintToBuffer(&buffer, static_cast<uint16_t>(dex_data.class_set.size()));
240 AddUintToBuffer(&buffer, dex_data.checksum); // uint32_t
241
242 AddStringToBuffer(&buffer, dex_location);
243
Calin Juravle998c2162015-12-21 15:39:33 +0200244 for (auto method_it : dex_data.method_set) {
Calin Juravle64142952016-03-21 14:37:55 +0000245 AddUintToBuffer(&buffer, method_it);
Calin Juravle31f2c152015-10-23 17:56:15 +0100246 }
Calin Juravle64142952016-03-21 14:37:55 +0000247 for (auto class_id : dex_data.class_set) {
248 AddUintToBuffer(&buffer, class_id);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800249 }
Calin Juravle64142952016-03-21 14:37:55 +0000250 DCHECK_EQ(required_capacity, buffer.size())
251 << "Failed to add the expected number of bytes in the buffer";
Calin Juravle31f2c152015-10-23 17:56:15 +0100252 }
253
Calin Juravle64142952016-03-21 14:37:55 +0000254 return WriteBuffer(fd, buffer.data(), buffer.size());
Calin Juravle226501b2015-12-11 14:41:31 +0000255}
256
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800257ProfileCompilationInfo::DexFileData* ProfileCompilationInfo::GetOrAddDexFileData(
258 const std::string& dex_location,
259 uint32_t checksum) {
Calin Juravle998c2162015-12-21 15:39:33 +0200260 auto info_it = info_.find(dex_location);
261 if (info_it == info_.end()) {
262 info_it = info_.Put(dex_location, DexFileData(checksum));
263 }
264 if (info_it->second.checksum != checksum) {
265 LOG(WARNING) << "Checksum mismatch for dex " << dex_location;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800266 return nullptr;
267 }
268 return &info_it->second;
269}
270
271bool ProfileCompilationInfo::AddResolvedClasses(const DexCacheResolvedClasses& classes) {
272 const std::string dex_location = GetProfileDexFileKey(classes.GetDexLocation());
273 const uint32_t checksum = classes.GetLocationChecksum();
274 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
275 if (data == nullptr) {
Calin Juravle998c2162015-12-21 15:39:33 +0200276 return false;
277 }
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800278 data->class_set.insert(classes.GetClasses().begin(), classes.GetClasses().end());
279 return true;
280}
281
282bool ProfileCompilationInfo::AddMethodIndex(const std::string& dex_location,
283 uint32_t checksum,
284 uint16_t method_idx) {
285 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
286 if (data == nullptr) {
287 return false;
288 }
289 data->method_set.insert(method_idx);
290 return true;
291}
292
293bool ProfileCompilationInfo::AddClassIndex(const std::string& dex_location,
294 uint32_t checksum,
295 uint16_t class_idx) {
296 DexFileData* const data = GetOrAddDexFileData(dex_location, checksum);
297 if (data == nullptr) {
298 return false;
299 }
300 data->class_set.insert(class_idx);
Calin Juravle998c2162015-12-21 15:39:33 +0200301 return true;
302}
303
Calin Juravle64142952016-03-21 14:37:55 +0000304bool ProfileCompilationInfo::ProcessLine(SafeBuffer& line_buffer,
305 uint16_t method_set_size,
306 uint16_t class_set_size,
307 uint32_t checksum,
308 const std::string& dex_location) {
309 for (uint16_t i = 0; i < method_set_size; i++) {
310 uint16_t method_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800311 if (!AddMethodIndex(dex_location, checksum, method_idx)) {
Calin Juravle877fd962016-01-05 14:29:29 +0000312 return false;
313 }
Calin Juravle226501b2015-12-11 14:41:31 +0000314 }
Calin Juravle64142952016-03-21 14:37:55 +0000315
316 for (uint16_t i = 0; i < class_set_size; i++) {
317 uint16_t class_def_idx = line_buffer.ReadUintAndAdvance<uint16_t>();
318 if (!AddClassIndex(dex_location, checksum, class_def_idx)) {
319 return false;
320 }
321 }
Calin Juravle226501b2015-12-11 14:41:31 +0000322 return true;
323}
324
Calin Juravle64142952016-03-21 14:37:55 +0000325// Tests for EOF by trying to read 1 byte from the descriptor.
326// Returns:
327// 0 if the descriptor is at the EOF,
328// -1 if there was an IO error
329// 1 if the descriptor has more content to read
330static int testEOF(int fd) {
331 uint8_t buffer[1];
332 return TEMP_FAILURE_RETRY(read(fd, buffer, 1));
333}
334
335// Reads an uint value previously written with AddUintToBuffer.
336template <typename T>
337T ProfileCompilationInfo::SafeBuffer::ReadUintAndAdvance() {
338 static_assert(std::is_unsigned<T>::value, "Type is not unsigned");
339 CHECK_LE(ptr_current_ + sizeof(T), ptr_end_);
340 T value = 0;
341 for (size_t i = 0; i < sizeof(T); i++) {
342 value += ptr_current_[i] << (i * kBitsPerByte);
Calin Juravle226501b2015-12-11 14:41:31 +0000343 }
Calin Juravle64142952016-03-21 14:37:55 +0000344 ptr_current_ += sizeof(T);
345 return value;
346}
347
348bool ProfileCompilationInfo::SafeBuffer::CompareAndAdvance(const uint8_t* data, size_t data_size) {
349 if (ptr_current_ + data_size > ptr_end_) {
350 return false;
351 }
352 if (memcmp(ptr_current_, data, data_size) == 0) {
353 ptr_current_ += data_size;
354 return true;
355 }
356 return false;
357}
358
359ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::SafeBuffer::FillFromFd(
360 int fd,
361 const std::string& source,
362 /*out*/std::string* error) {
363 size_t byte_count = ptr_end_ - ptr_current_;
364 uint8_t* buffer = ptr_current_;
365 while (byte_count > 0) {
366 int bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, byte_count));
367 if (bytes_read == 0) {
368 *error += "Profile EOF reached prematurely for " + source;
369 return kProfileLoadBadData;
370 } else if (bytes_read < 0) {
371 *error += "Profile IO error for " + source + strerror(errno);
372 return kProfileLoadIOError;
Calin Juravle226501b2015-12-11 14:41:31 +0000373 }
Calin Juravle64142952016-03-21 14:37:55 +0000374 byte_count -= bytes_read;
375 buffer += bytes_read;
Calin Juravle226501b2015-12-11 14:41:31 +0000376 }
Calin Juravle64142952016-03-21 14:37:55 +0000377 return kProfileLoadSuccess;
378}
379
380ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileHeader(
381 int fd,
382 /*out*/uint16_t* number_of_lines,
383 /*out*/std::string* error) {
384 // Read magic and version
385 const size_t kMagicVersionSize =
386 sizeof(kProfileMagic) +
387 sizeof(kProfileVersion) +
388 sizeof(uint16_t); // number of lines
389
390 SafeBuffer safe_buffer(kMagicVersionSize);
391
392 ProfileLoadSatus status = safe_buffer.FillFromFd(fd, "ReadProfileHeader", error);
393 if (status != kProfileLoadSuccess) {
394 return status;
395 }
396
397 if (!safe_buffer.CompareAndAdvance(kProfileMagic, sizeof(kProfileMagic))) {
398 *error = "Profile missing magic";
399 return kProfileLoadVersionMismatch;
400 }
401 if (!safe_buffer.CompareAndAdvance(kProfileVersion, sizeof(kProfileVersion))) {
402 *error = "Profile version mismatch";
403 return kProfileLoadVersionMismatch;
404 }
405 *number_of_lines = safe_buffer.ReadUintAndAdvance<uint16_t>();
406 return kProfileLoadSuccess;
407}
408
409ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLineHeader(
410 int fd,
411 /*out*/ProfileLineHeader* line_header,
412 /*out*/std::string* error) {
413 SafeBuffer header_buffer(kLineHeaderSize);
414 ProfileLoadSatus status = header_buffer.FillFromFd(fd, "ReadProfileHeader", error);
415 if (status != kProfileLoadSuccess) {
416 return status;
417 }
418
419 uint16_t dex_location_size = header_buffer.ReadUintAndAdvance<uint16_t>();
420 line_header->method_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
421 line_header->class_set_size = header_buffer.ReadUintAndAdvance<uint16_t>();
422 line_header->checksum = header_buffer.ReadUintAndAdvance<uint32_t>();
423
424 if (dex_location_size == 0 || dex_location_size > kMaxDexFileKeyLength) {
425 *error = "DexFileKey has an invalid size: " + std::to_string(dex_location_size);
426 return kProfileLoadBadData;
427 }
428
429 SafeBuffer location_buffer(dex_location_size);
430 status = location_buffer.FillFromFd(fd, "ReadProfileHeaderDexLocation", error);
431 if (status != kProfileLoadSuccess) {
432 return status;
433 }
434 line_header->dex_location.assign(
435 reinterpret_cast<char*>(location_buffer.Get()), dex_location_size);
436 return kProfileLoadSuccess;
437}
438
439ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::ReadProfileLine(
440 int fd,
441 const ProfileLineHeader& line_header,
442 /*out*/std::string* error) {
443 // Make sure that we don't try to read everything in memory (in case the profile if full).
444 // Split readings in chunks of at most 10kb.
445 static constexpr uint16_t kMaxNumberOfEntriesToRead = 5120;
446 uint16_t methods_left_to_read = line_header.method_set_size;
447 uint16_t classes_left_to_read = line_header.class_set_size;
448
449 while ((methods_left_to_read > 0) || (classes_left_to_read > 0)) {
450 uint16_t methods_to_read = std::min(kMaxNumberOfEntriesToRead, methods_left_to_read);
451 uint16_t max_classes_to_read = kMaxNumberOfEntriesToRead - methods_to_read;
452 uint16_t classes_to_read = std::min(max_classes_to_read, classes_left_to_read);
453
454 size_t line_size = sizeof(uint16_t) * (methods_to_read + classes_to_read);
455 SafeBuffer line_buffer(line_size);
456
457 ProfileLoadSatus status = line_buffer.FillFromFd(fd, "ReadProfileLine", error);
458 if (status != kProfileLoadSuccess) {
459 return status;
460 }
461 if (!ProcessLine(line_buffer,
462 methods_to_read,
463 classes_to_read,
464 line_header.checksum,
465 line_header.dex_location)) {
466 *error = "Error when reading profile file line";
467 return kProfileLoadBadData;
468 }
469 methods_left_to_read -= methods_to_read;
470 classes_left_to_read -= classes_to_read;
471 }
472 return kProfileLoadSuccess;
Calin Juravle226501b2015-12-11 14:41:31 +0000473}
474
Calin Juravle2e2db782016-02-23 12:00:03 +0000475bool ProfileCompilationInfo::Load(int fd) {
Calin Juravle64142952016-03-21 14:37:55 +0000476 std::string error;
477 ProfileLoadSatus status = LoadInternal(fd, &error);
478
479 if (status == kProfileLoadSuccess) {
480 return true;
481 } else {
482 PLOG(WARNING) << "Error when reading profile " << error;
483 return false;
484 }
485}
486
487ProfileCompilationInfo::ProfileLoadSatus ProfileCompilationInfo::LoadInternal(
488 int fd, std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800489 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle2e2db782016-02-23 12:00:03 +0000490 DCHECK_GE(fd, 0);
Calin Juravle226501b2015-12-11 14:41:31 +0000491
Calin Juravle64142952016-03-21 14:37:55 +0000492 struct stat stat_buffer;
493 if (fstat(fd, &stat_buffer) != 0) {
494 return kProfileLoadIOError;
Calin Juravle226501b2015-12-11 14:41:31 +0000495 }
Calin Juravle64142952016-03-21 14:37:55 +0000496 // We allow empty profile files.
497 // Profiles may be created by ActivityManager or installd before we manage to
498 // process them in the runtime or profman.
499 if (stat_buffer.st_size == 0) {
500 return kProfileLoadSuccess;
501 }
502 // Read profile header: magic + version + number_of_lines.
503 uint16_t number_of_lines;
504 ProfileLoadSatus status = ReadProfileHeader(fd, &number_of_lines, error);
505 if (status != kProfileLoadSuccess) {
506 return status;
507 }
508
509 while (number_of_lines > 0) {
510 ProfileLineHeader line_header;
511 // First, read the line header to get the amount of data we need to read.
512 status = ReadProfileLineHeader(fd, &line_header, error);
513 if (status != kProfileLoadSuccess) {
514 return status;
515 }
516
517 // Now read the actual profile line.
518 status = ReadProfileLine(fd, line_header, error);
519 if (status != kProfileLoadSuccess) {
520 return status;
521 }
522 number_of_lines--;
523 }
524
525 // Check that we read everything and that profiles don't contain junk data.
526 int result = testEOF(fd);
527 if (result == 0) {
528 return kProfileLoadSuccess;
529 } else if (result < 0) {
530 return kProfileLoadIOError;
531 } else {
532 *error = "Unexpected content in the profile file";
533 return kProfileLoadBadData;
534 }
Calin Juravle998c2162015-12-21 15:39:33 +0200535}
536
Calin Juravle67265462016-03-18 16:23:40 +0000537bool ProfileCompilationInfo::MergeWith(const ProfileCompilationInfo& other) {
Calin Juravle998c2162015-12-21 15:39:33 +0200538 for (const auto& other_it : other.info_) {
539 const std::string& other_dex_location = other_it.first;
540 const DexFileData& other_dex_data = other_it.second;
541
542 auto info_it = info_.find(other_dex_location);
543 if (info_it == info_.end()) {
544 info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum));
545 }
546 if (info_it->second.checksum != other_dex_data.checksum) {
547 LOG(WARNING) << "Checksum mismatch for dex " << other_dex_location;
548 return false;
549 }
550 info_it->second.method_set.insert(other_dex_data.method_set.begin(),
551 other_dex_data.method_set.end());
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800552 info_it->second.class_set.insert(other_dex_data.class_set.begin(),
553 other_dex_data.class_set.end());
Calin Juravle998c2162015-12-21 15:39:33 +0200554 }
555 return true;
Calin Juravle226501b2015-12-11 14:41:31 +0000556}
557
558bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
Calin Juravle34900cc2016-02-05 16:19:19 +0000559 auto info_it = info_.find(GetProfileDexFileKey(method_ref.dex_file->GetLocation()));
Calin Juravle226501b2015-12-11 14:41:31 +0000560 if (info_it != info_.end()) {
Calin Juravle998c2162015-12-21 15:39:33 +0200561 if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) {
562 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000563 }
Calin Juravle998c2162015-12-21 15:39:33 +0200564 const std::set<uint16_t>& methods = info_it->second.method_set;
565 return methods.find(method_ref.dex_method_index) != methods.end();
Calin Juravle226501b2015-12-11 14:41:31 +0000566 }
567 return false;
568}
569
Mathieu Chartiera8077802016-03-16 19:08:31 -0700570bool ProfileCompilationInfo::ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const {
571 auto info_it = info_.find(GetProfileDexFileKey(dex_file.GetLocation()));
572 if (info_it != info_.end()) {
573 if (dex_file.GetLocationChecksum() != info_it->second.checksum) {
574 return false;
575 }
576 const std::set<uint16_t>& classes = info_it->second.class_set;
577 return classes.find(class_def_idx) != classes.end();
578 }
579 return false;
580}
581
Calin Juravle998c2162015-12-21 15:39:33 +0200582uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
583 uint32_t total = 0;
584 for (const auto& it : info_) {
585 total += it.second.method_set.size();
586 }
587 return total;
588}
589
Calin Juravle67265462016-03-18 16:23:40 +0000590uint32_t ProfileCompilationInfo::GetNumberOfResolvedClasses() const {
591 uint32_t total = 0;
592 for (const auto& it : info_) {
593 total += it.second.class_set.size();
594 }
595 return total;
596}
597
Calin Juravle998c2162015-12-21 15:39:33 +0200598std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
599 bool print_full_dex_location) const {
Calin Juravle226501b2015-12-11 14:41:31 +0000600 std::ostringstream os;
601 if (info_.empty()) {
602 return "ProfileInfo: empty";
603 }
604
605 os << "ProfileInfo:";
606
Calin Juravle226501b2015-12-11 14:41:31 +0000607 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
Calin Juravle998c2162015-12-21 15:39:33 +0200608 for (const auto& it : info_) {
Calin Juravle226501b2015-12-11 14:41:31 +0000609 os << "\n";
Calin Juravle998c2162015-12-21 15:39:33 +0200610 const std::string& location = it.first;
611 const DexFileData& dex_data = it.second;
Calin Juravle226501b2015-12-11 14:41:31 +0000612 if (print_full_dex_location) {
613 os << location;
614 } else {
615 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
616 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
617 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
618 }
Calin Juravle998c2162015-12-21 15:39:33 +0200619 for (const auto method_it : dex_data.method_set) {
620 if (dex_files != nullptr) {
621 const DexFile* dex_file = 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];
625 }
626 }
627 if (dex_file != nullptr) {
628 os << "\n " << PrettyMethod(method_it, *dex_file, true);
629 }
Calin Juravle226501b2015-12-11 14:41:31 +0000630 }
Calin Juravle64142952016-03-21 14:37:55 +0000631 os << ", " << method_it;
Calin Juravle226501b2015-12-11 14:41:31 +0000632 }
633 }
634 return os.str();
635}
636
Calin Juravle2e2db782016-02-23 12:00:03 +0000637bool ProfileCompilationInfo::Equals(const ProfileCompilationInfo& other) {
Calin Juravle877fd962016-01-05 14:29:29 +0000638 return info_.Equals(other.info_);
639}
640
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800641std::set<DexCacheResolvedClasses> ProfileCompilationInfo::GetResolvedClasses() const {
642 std::set<DexCacheResolvedClasses> ret;
643 for (auto&& pair : info_) {
644 const std::string& profile_key = pair.first;
645 const DexFileData& data = pair.second;
646 DexCacheResolvedClasses classes(profile_key, data.checksum);
647 classes.AddClasses(data.class_set.begin(), data.class_set.end());
648 ret.insert(classes);
649 }
650 return ret;
651}
652
Calin Juravle67265462016-03-18 16:23:40 +0000653void ProfileCompilationInfo::ClearResolvedClasses() {
654 for (auto& pair : info_) {
655 pair.second.class_set.clear();
656 }
657}
658
Calin Juravle31f2c152015-10-23 17:56:15 +0100659} // namespace art