blob: b4b872ff502a5e7aebbad989228c58213d88a65d [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
19#include <fstream>
Calin Juravle4d77b6a2015-12-01 18:38:09 +000020#include <vector>
Calin Juravle31f2c152015-10-23 17:56:15 +010021#include <sys/file.h>
22#include <sys/stat.h>
23#include <sys/uio.h>
24
25#include "art_method-inl.h"
26#include "base/mutex.h"
Calin Juravle877fd962016-01-05 14:29:29 +000027#include "base/scoped_flock.h"
Calin Juravle66f55232015-12-08 15:09:10 +000028#include "base/stl_util.h"
Calin Juravle877fd962016-01-05 14:29:29 +000029#include "base/unix_file/fd_file.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010030#include "jit/profiling_info.h"
Calin Juravle877fd962016-01-05 14:29:29 +000031#include "os.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010032#include "safe_map.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010033
34namespace art {
35
Calin Juravle998c2162015-12-21 15:39:33 +020036bool ProfileCompilationInfo::SaveProfilingInfo(const std::string& filename,
37 const std::vector<ArtMethod*>& methods) {
Calin Juravle31f2c152015-10-23 17:56:15 +010038 if (methods.empty()) {
39 VLOG(profiler) << "No info to save to " << filename;
Calin Juravle998c2162015-12-21 15:39:33 +020040 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +010041 }
42
Calin Juravle877fd962016-01-05 14:29:29 +000043 ScopedFlock flock;
44 std::string error;
45 if (!flock.Init(filename.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC, /* block */ false, &error)) {
46 LOG(WARNING) << "Couldn't lock the profile file " << filename << ": " << error;
47 return false;
48 }
49
50 int fd = flock.GetFile()->Fd();
51
Calin Juravle998c2162015-12-21 15:39:33 +020052 ProfileCompilationInfo info;
Calin Juravle877fd962016-01-05 14:29:29 +000053 if (!info.Load(fd)) {
Calin Juravle998c2162015-12-21 15:39:33 +020054 LOG(WARNING) << "Could not load previous profile data from file " << filename;
55 return false;
56 }
Calin Juravle31f2c152015-10-23 17:56:15 +010057 {
58 ScopedObjectAccess soa(Thread::Current());
59 for (auto it = methods.begin(); it != methods.end(); it++) {
Calin Juravle998c2162015-12-21 15:39:33 +020060 const DexFile* dex_file = (*it)->GetDexFile();
61 if (!info.AddData(dex_file->GetLocation(),
62 dex_file->GetLocationChecksum(),
63 (*it)->GetDexMethodIndex())) {
64 return false;
65 }
Calin Juravle31f2c152015-10-23 17:56:15 +010066 }
67 }
68
Calin Juravle877fd962016-01-05 14:29:29 +000069 if (!flock.GetFile()->ClearContent()) {
70 PLOG(WARNING) << "Could not clear profile file: " << filename;
71 return false;
72 }
73
Calin Juravle31f2c152015-10-23 17:56:15 +010074 // This doesn't need locking because we are trying to lock the file for exclusive
75 // access and fail immediately if we can't.
Calin Juravle877fd962016-01-05 14:29:29 +000076 bool result = info.Save(fd);
Calin Juravle998c2162015-12-21 15:39:33 +020077 if (result) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +000078 VLOG(profiler) << "Successfully saved profile info to " << filename
79 << " Size: " << GetFileSizeBytes(filename);
Calin Juravle998c2162015-12-21 15:39:33 +020080 } else {
81 VLOG(profiler) << "Failed to save profile info to " << filename;
Calin Juravle31f2c152015-10-23 17:56:15 +010082 }
Calin Juravle998c2162015-12-21 15:39:33 +020083 return result;
Calin Juravle31f2c152015-10-23 17:56:15 +010084}
85
Calin Juravle877fd962016-01-05 14:29:29 +000086static bool WriteToFile(int fd, const std::ostringstream& os) {
Calin Juravle31f2c152015-10-23 17:56:15 +010087 std::string data(os.str());
88 const char *p = data.c_str();
89 size_t length = data.length();
90 do {
Calin Juravle877fd962016-01-05 14:29:29 +000091 int n = TEMP_FAILURE_RETRY(write(fd, p, length));
92 if (n < 0) {
93 PLOG(WARNING) << "Failed to write to descriptor: " << fd;
94 return false;
95 }
Calin Juravle31f2c152015-10-23 17:56:15 +010096 p += n;
97 length -= n;
98 } while (length > 0);
Calin Juravle877fd962016-01-05 14:29:29 +000099 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +0100100}
101
Calin Juravle226501b2015-12-11 14:41:31 +0000102static constexpr const char kFieldSeparator = ',';
103static constexpr const char kLineSeparator = '\n';
Calin Juravle31f2c152015-10-23 17:56:15 +0100104
105/**
106 * Serialization format:
Calin Juravle66f55232015-12-08 15:09:10 +0000107 * dex_location1,dex_location_checksum1,method_id11,method_id12...
108 * dex_location2,dex_location_checksum2,method_id21,method_id22...
Calin Juravle31f2c152015-10-23 17:56:15 +0100109 * e.g.
Calin Juravle66f55232015-12-08 15:09:10 +0000110 * /system/priv-app/app/app.apk,131232145,11,23,454,54
111 * /system/priv-app/app/app.apk:classes5.dex,218490184,39,13,49,1
Calin Juravle31f2c152015-10-23 17:56:15 +0100112 **/
Calin Juravle877fd962016-01-05 14:29:29 +0000113bool ProfileCompilationInfo::Save(uint32_t fd) {
114 DCHECK_GE(fd, 0u);
Calin Juravle31f2c152015-10-23 17:56:15 +0100115 // TODO(calin): Profile this and see how much memory it takes. If too much,
116 // write to file directly.
117 std::ostringstream os;
Calin Juravle998c2162015-12-21 15:39:33 +0200118 for (const auto& it : info_) {
119 const std::string& dex_location = it.first;
120 const DexFileData& dex_data = it.second;
Calin Juravle31f2c152015-10-23 17:56:15 +0100121
Calin Juravle998c2162015-12-21 15:39:33 +0200122 os << dex_location << kFieldSeparator << dex_data.checksum;
123 for (auto method_it : dex_data.method_set) {
Calin Juravle31f2c152015-10-23 17:56:15 +0100124 os << kFieldSeparator << method_it;
125 }
126 os << kLineSeparator;
127 }
128
Calin Juravle877fd962016-01-05 14:29:29 +0000129 return WriteToFile(fd, os);
Calin Juravle31f2c152015-10-23 17:56:15 +0100130}
Calin Juravle226501b2015-12-11 14:41:31 +0000131
132// TODO(calin): This a duplicate of Utils::Split fixing the case where the first character
133// is the separator. Merge the fix into Utils::Split once verified that it doesn't break its users.
134static void SplitString(const std::string& s, char separator, std::vector<std::string>* result) {
135 const char* p = s.data();
136 const char* end = p + s.size();
137 // Check if the first character is the separator.
138 if (p != end && *p ==separator) {
139 result->push_back("");
140 ++p;
141 }
142 // Process the rest of the characters.
143 while (p != end) {
144 if (*p == separator) {
145 ++p;
146 } else {
147 const char* start = p;
148 while (++p != end && *p != separator) {
149 // Skip to the next occurrence of the separator.
150 }
151 result->push_back(std::string(start, p - start));
152 }
153 }
154}
155
Calin Juravle998c2162015-12-21 15:39:33 +0200156bool ProfileCompilationInfo::AddData(const std::string& dex_location,
157 uint32_t checksum,
158 uint16_t method_idx) {
159 auto info_it = info_.find(dex_location);
160 if (info_it == info_.end()) {
161 info_it = info_.Put(dex_location, DexFileData(checksum));
162 }
163 if (info_it->second.checksum != checksum) {
164 LOG(WARNING) << "Checksum mismatch for dex " << dex_location;
165 return false;
166 }
167 info_it->second.method_set.insert(method_idx);
168 return true;
169}
170
171bool ProfileCompilationInfo::ProcessLine(const std::string& line) {
Calin Juravle226501b2015-12-11 14:41:31 +0000172 std::vector<std::string> parts;
173 SplitString(line, kFieldSeparator, &parts);
174 if (parts.size() < 3) {
175 LOG(WARNING) << "Invalid line: " << line;
176 return false;
177 }
178
Calin Juravle66f55232015-12-08 15:09:10 +0000179 const std::string& dex_location = parts[0];
Calin Juravle226501b2015-12-11 14:41:31 +0000180 uint32_t checksum;
181 if (!ParseInt(parts[1].c_str(), &checksum)) {
182 return false;
183 }
184
Calin Juravle226501b2015-12-11 14:41:31 +0000185 for (size_t i = 2; i < parts.size(); i++) {
186 uint32_t method_idx;
187 if (!ParseInt(parts[i].c_str(), &method_idx)) {
188 LOG(WARNING) << "Cannot parse method_idx " << parts[i];
189 return false;
190 }
Calin Juravle877fd962016-01-05 14:29:29 +0000191 if (!AddData(dex_location, checksum, method_idx)) {
192 return false;
193 }
Calin Juravle226501b2015-12-11 14:41:31 +0000194 }
195 return true;
196}
197
198// Parses the buffer (of length n) starting from start_from and identify new lines
199// based on kLineSeparator marker.
200// Returns the first position after kLineSeparator in the buffer (starting from start_from),
201// or -1 if the marker doesn't appear.
202// The processed characters are appended to the given line.
203static int GetLineFromBuffer(char* buffer, int n, int start_from, std::string& line) {
204 if (start_from >= n) {
205 return -1;
206 }
207 int new_line_pos = -1;
208 for (int i = start_from; i < n; i++) {
209 if (buffer[i] == kLineSeparator) {
210 new_line_pos = i;
211 break;
212 }
213 }
214 int append_limit = new_line_pos == -1 ? n : new_line_pos;
215 line.append(buffer + start_from, append_limit - start_from);
216 // Jump over kLineSeparator and return the position of the next character.
217 return new_line_pos == -1 ? new_line_pos : new_line_pos + 1;
218}
219
Calin Juravle877fd962016-01-05 14:29:29 +0000220bool ProfileCompilationInfo::Load(uint32_t fd) {
221 DCHECK_GE(fd, 0u);
Calin Juravle226501b2015-12-11 14:41:31 +0000222
223 std::string current_line;
224 const int kBufferSize = 1024;
225 char buffer[kBufferSize];
Calin Juravle226501b2015-12-11 14:41:31 +0000226
Calin Juravle877fd962016-01-05 14:29:29 +0000227 while (true) {
228 int n = TEMP_FAILURE_RETRY(read(fd, buffer, kBufferSize));
Calin Juravle226501b2015-12-11 14:41:31 +0000229 if (n < 0) {
Calin Juravle877fd962016-01-05 14:29:29 +0000230 PLOG(WARNING) << "Error when reading profile file";
231 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000232 } else if (n == 0) {
233 break;
234 }
235 // Detect the new lines from the buffer. If we manage to complete a line,
236 // process it. Otherwise append to the current line.
237 int current_start_pos = 0;
238 while (current_start_pos < n) {
239 current_start_pos = GetLineFromBuffer(buffer, n, current_start_pos, current_line);
240 if (current_start_pos == -1) {
241 break;
242 }
Calin Juravle998c2162015-12-21 15:39:33 +0200243 if (!ProcessLine(current_line)) {
Calin Juravle877fd962016-01-05 14:29:29 +0000244 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000245 }
246 // Reset the current line (we just processed it).
247 current_line.clear();
248 }
249 }
Calin Juravle877fd962016-01-05 14:29:29 +0000250 return true;
Calin Juravle998c2162015-12-21 15:39:33 +0200251}
252
253bool ProfileCompilationInfo::Load(const ProfileCompilationInfo& other) {
254 for (const auto& other_it : other.info_) {
255 const std::string& other_dex_location = other_it.first;
256 const DexFileData& other_dex_data = other_it.second;
257
258 auto info_it = info_.find(other_dex_location);
259 if (info_it == info_.end()) {
260 info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum));
261 }
262 if (info_it->second.checksum != other_dex_data.checksum) {
263 LOG(WARNING) << "Checksum mismatch for dex " << other_dex_location;
264 return false;
265 }
266 info_it->second.method_set.insert(other_dex_data.method_set.begin(),
267 other_dex_data.method_set.end());
268 }
269 return true;
Calin Juravle226501b2015-12-11 14:41:31 +0000270}
271
272bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
Calin Juravle998c2162015-12-21 15:39:33 +0200273 auto info_it = info_.find(method_ref.dex_file->GetLocation());
Calin Juravle226501b2015-12-11 14:41:31 +0000274 if (info_it != info_.end()) {
Calin Juravle998c2162015-12-21 15:39:33 +0200275 if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) {
276 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000277 }
Calin Juravle998c2162015-12-21 15:39:33 +0200278 const std::set<uint16_t>& methods = info_it->second.method_set;
279 return methods.find(method_ref.dex_method_index) != methods.end();
Calin Juravle226501b2015-12-11 14:41:31 +0000280 }
281 return false;
282}
283
Calin Juravle998c2162015-12-21 15:39:33 +0200284uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
285 uint32_t total = 0;
286 for (const auto& it : info_) {
287 total += it.second.method_set.size();
288 }
289 return total;
290}
291
292std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
293 bool print_full_dex_location) const {
Calin Juravle226501b2015-12-11 14:41:31 +0000294 std::ostringstream os;
295 if (info_.empty()) {
296 return "ProfileInfo: empty";
297 }
298
299 os << "ProfileInfo:";
300
Calin Juravle226501b2015-12-11 14:41:31 +0000301 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
Calin Juravle998c2162015-12-21 15:39:33 +0200302 for (const auto& it : info_) {
Calin Juravle226501b2015-12-11 14:41:31 +0000303 os << "\n";
Calin Juravle998c2162015-12-21 15:39:33 +0200304 const std::string& location = it.first;
305 const DexFileData& dex_data = it.second;
Calin Juravle226501b2015-12-11 14:41:31 +0000306 if (print_full_dex_location) {
307 os << location;
308 } else {
309 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
310 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
311 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
312 }
Calin Juravle998c2162015-12-21 15:39:33 +0200313 for (const auto method_it : dex_data.method_set) {
314 if (dex_files != nullptr) {
315 const DexFile* dex_file = nullptr;
316 for (size_t i = 0; i < dex_files->size(); i++) {
317 if (location == (*dex_files)[i]->GetLocation()) {
318 dex_file = (*dex_files)[i];
319 }
320 }
321 if (dex_file != nullptr) {
322 os << "\n " << PrettyMethod(method_it, *dex_file, true);
323 }
Calin Juravle226501b2015-12-11 14:41:31 +0000324 }
Calin Juravle998c2162015-12-21 15:39:33 +0200325 os << "\n " << method_it;
Calin Juravle226501b2015-12-11 14:41:31 +0000326 }
327 }
328 return os.str();
329}
330
Calin Juravle877fd962016-01-05 14:29:29 +0000331bool ProfileCompilationInfo::Equals(ProfileCompilationInfo& other) {
332 return info_.Equals(other.info_);
333}
334
Calin Juravle31f2c152015-10-23 17:56:15 +0100335} // namespace art