1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "profile_boot_info.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <unistd.h>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include <vector>
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
24*795d594fSAndroid Build Coastguard Worker #include "profile_helpers.h"
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker namespace art {
28*795d594fSAndroid Build Coastguard Worker
Add(const DexFile * dex_file,uint32_t method_index)29*795d594fSAndroid Build Coastguard Worker void ProfileBootInfo::Add(const DexFile* dex_file, uint32_t method_index) {
30*795d594fSAndroid Build Coastguard Worker auto it = std::find(dex_files_.begin(), dex_files_.end(), dex_file);
31*795d594fSAndroid Build Coastguard Worker uint32_t index = 0;
32*795d594fSAndroid Build Coastguard Worker if (it == dex_files_.end()) {
33*795d594fSAndroid Build Coastguard Worker index = dex_files_.size();
34*795d594fSAndroid Build Coastguard Worker dex_files_.push_back(dex_file);
35*795d594fSAndroid Build Coastguard Worker } else {
36*795d594fSAndroid Build Coastguard Worker index = std::distance(dex_files_.begin(), it);
37*795d594fSAndroid Build Coastguard Worker }
38*795d594fSAndroid Build Coastguard Worker methods_.push_back(std::make_pair(index, method_index));
39*795d594fSAndroid Build Coastguard Worker }
40*795d594fSAndroid Build Coastguard Worker
Save(int fd) const41*795d594fSAndroid Build Coastguard Worker bool ProfileBootInfo::Save(int fd) const {
42*795d594fSAndroid Build Coastguard Worker std::vector<uint8_t> buffer;
43*795d594fSAndroid Build Coastguard Worker // Store dex file locations.
44*795d594fSAndroid Build Coastguard Worker for (const DexFile* dex_file : dex_files_) {
45*795d594fSAndroid Build Coastguard Worker AddUintToBuffer(&buffer, static_cast<uint8_t>(dex_file->GetLocation().size()));
46*795d594fSAndroid Build Coastguard Worker AddStringToBuffer(&buffer, dex_file->GetLocation());
47*795d594fSAndroid Build Coastguard Worker }
48*795d594fSAndroid Build Coastguard Worker // Store marker between dex file locations and methods.
49*795d594fSAndroid Build Coastguard Worker AddUintToBuffer(&buffer, static_cast<uint8_t>(0));
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker // Store pairs of <dex file index, method id>, in compilation order.
52*795d594fSAndroid Build Coastguard Worker for (const std::pair<uint32_t, uint32_t>& pair : methods_) {
53*795d594fSAndroid Build Coastguard Worker AddUintToBuffer(&buffer, pair.first);
54*795d594fSAndroid Build Coastguard Worker AddUintToBuffer(&buffer, pair.second);
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker if (!WriteBuffer(fd, buffer.data(), buffer.size())) {
57*795d594fSAndroid Build Coastguard Worker return false;
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker return true;
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker
Load(int fd,const std::vector<const DexFile * > & dex_files)62*795d594fSAndroid Build Coastguard Worker bool ProfileBootInfo::Load(int fd, const std::vector<const DexFile*>& dex_files) {
63*795d594fSAndroid Build Coastguard Worker // Read dex file locations.
64*795d594fSAndroid Build Coastguard Worker do {
65*795d594fSAndroid Build Coastguard Worker uint8_t string_length;
66*795d594fSAndroid Build Coastguard Worker int bytes_read = TEMP_FAILURE_RETRY(read(fd, &string_length, sizeof(uint8_t)));
67*795d594fSAndroid Build Coastguard Worker if (bytes_read < 0) {
68*795d594fSAndroid Build Coastguard Worker PLOG(ERROR) << "Unexpected error reading profile";
69*795d594fSAndroid Build Coastguard Worker return false;
70*795d594fSAndroid Build Coastguard Worker } else if (bytes_read == 0) {
71*795d594fSAndroid Build Coastguard Worker if (dex_files.empty()) {
72*795d594fSAndroid Build Coastguard Worker // If no dex files have been passed, that's expected.
73*795d594fSAndroid Build Coastguard Worker return true;
74*795d594fSAndroid Build Coastguard Worker } else {
75*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Unexpected end of file for length";
76*795d594fSAndroid Build Coastguard Worker return false;
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker if (string_length == 0) {
80*795d594fSAndroid Build Coastguard Worker break;
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker std::unique_ptr<char[]> data(new char[string_length]);
83*795d594fSAndroid Build Coastguard Worker bytes_read = TEMP_FAILURE_RETRY(read(fd, data.get(), string_length));
84*795d594fSAndroid Build Coastguard Worker if (bytes_read < 0) {
85*795d594fSAndroid Build Coastguard Worker PLOG(WARNING) << "Unexpected error reading profile";
86*795d594fSAndroid Build Coastguard Worker return false;
87*795d594fSAndroid Build Coastguard Worker } else if (bytes_read == 0) {
88*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Unexpected end of file for name";
89*795d594fSAndroid Build Coastguard Worker return false;
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker // Map the location to an instance of dex file in `dex_files`.
92*795d594fSAndroid Build Coastguard Worker auto it = std::find_if(dex_files.begin(),
93*795d594fSAndroid Build Coastguard Worker dex_files.end(),
94*795d594fSAndroid Build Coastguard Worker [string_length, &data](const DexFile* file) {
95*795d594fSAndroid Build Coastguard Worker std::string dex_location = file->GetLocation();
96*795d594fSAndroid Build Coastguard Worker return dex_location.size() == string_length &&
97*795d594fSAndroid Build Coastguard Worker (strncmp(data.get(), dex_location.data(), string_length) == 0);
98*795d594fSAndroid Build Coastguard Worker });
99*795d594fSAndroid Build Coastguard Worker if (it != dex_files.end()) {
100*795d594fSAndroid Build Coastguard Worker dex_files_.push_back(*it);
101*795d594fSAndroid Build Coastguard Worker } else {
102*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Couldn't find " << std::string(data.get(), string_length);
103*795d594fSAndroid Build Coastguard Worker return false;
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker } while (true);
106*795d594fSAndroid Build Coastguard Worker
107*795d594fSAndroid Build Coastguard Worker // Read methods.
108*795d594fSAndroid Build Coastguard Worker do {
109*795d594fSAndroid Build Coastguard Worker uint32_t dex_file_index;
110*795d594fSAndroid Build Coastguard Worker uint32_t method_id;
111*795d594fSAndroid Build Coastguard Worker int bytes_read = TEMP_FAILURE_RETRY(read(fd, &dex_file_index, sizeof(dex_file_index)));
112*795d594fSAndroid Build Coastguard Worker if (bytes_read <= 0) {
113*795d594fSAndroid Build Coastguard Worker break;
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker bytes_read = TEMP_FAILURE_RETRY(read(fd, &method_id, sizeof(method_id)));
116*795d594fSAndroid Build Coastguard Worker if (bytes_read <= 0) {
117*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << "Didn't get a method id";
118*795d594fSAndroid Build Coastguard Worker return false;
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker methods_.push_back(std::make_pair(dex_file_index, method_id));
121*795d594fSAndroid Build Coastguard Worker } while (true);
122*795d594fSAndroid Build Coastguard Worker return true;
123*795d594fSAndroid Build Coastguard Worker }
124*795d594fSAndroid Build Coastguard Worker
125*795d594fSAndroid Build Coastguard Worker } // namespace art
126