blob: a132701796c68a50199120f97537afa66e6f2e31 [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 Juravle66f55232015-12-08 15:09:10 +000027#include "base/stl_util.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010028#include "jit/profiling_info.h"
29#include "safe_map.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010030
31namespace art {
32
Calin Juravle998c2162015-12-21 15:39:33 +020033bool ProfileCompilationInfo::SaveProfilingInfo(const std::string& filename,
34 const std::vector<ArtMethod*>& methods) {
Calin Juravle31f2c152015-10-23 17:56:15 +010035 if (methods.empty()) {
36 VLOG(profiler) << "No info to save to " << filename;
Calin Juravle998c2162015-12-21 15:39:33 +020037 return true;
Calin Juravle31f2c152015-10-23 17:56:15 +010038 }
39
Calin Juravle998c2162015-12-21 15:39:33 +020040 ProfileCompilationInfo info;
41 if (!info.Load(filename)) {
42 LOG(WARNING) << "Could not load previous profile data from file " << filename;
43 return false;
44 }
Calin Juravle31f2c152015-10-23 17:56:15 +010045 {
46 ScopedObjectAccess soa(Thread::Current());
47 for (auto it = methods.begin(); it != methods.end(); it++) {
Calin Juravle998c2162015-12-21 15:39:33 +020048 const DexFile* dex_file = (*it)->GetDexFile();
49 if (!info.AddData(dex_file->GetLocation(),
50 dex_file->GetLocationChecksum(),
51 (*it)->GetDexMethodIndex())) {
52 return false;
53 }
Calin Juravle31f2c152015-10-23 17:56:15 +010054 }
55 }
56
57 // This doesn't need locking because we are trying to lock the file for exclusive
58 // access and fail immediately if we can't.
Calin Juravle998c2162015-12-21 15:39:33 +020059 bool result = info.Save(filename);
60 if (result) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +000061 VLOG(profiler) << "Successfully saved profile info to " << filename
62 << " Size: " << GetFileSizeBytes(filename);
Calin Juravle998c2162015-12-21 15:39:33 +020063 } else {
64 VLOG(profiler) << "Failed to save profile info to " << filename;
Calin Juravle31f2c152015-10-23 17:56:15 +010065 }
Calin Juravle998c2162015-12-21 15:39:33 +020066 return result;
Calin Juravle31f2c152015-10-23 17:56:15 +010067}
68
Calin Juravle226501b2015-12-11 14:41:31 +000069enum OpenMode {
70 READ,
71 READ_WRITE
72};
73
74static int OpenFile(const std::string& filename, OpenMode open_mode) {
75 int fd = -1;
76 switch (open_mode) {
77 case READ:
78 fd = open(filename.c_str(), O_RDONLY);
79 break;
80 case READ_WRITE:
81 // TODO(calin) allow the shared uid of the app to access the file.
Calin Juravle5e2b9712015-12-18 14:10:00 +020082 fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC);
Calin Juravle226501b2015-12-11 14:41:31 +000083 break;
84 }
85
Calin Juravle31f2c152015-10-23 17:56:15 +010086 if (fd < 0) {
87 PLOG(WARNING) << "Failed to open profile file " << filename;
88 return -1;
89 }
90
91 // Lock the file for exclusive access but don't wait if we can't lock it.
92 int err = flock(fd, LOCK_EX | LOCK_NB);
93 if (err < 0) {
94 PLOG(WARNING) << "Failed to lock profile file " << filename;
95 return -1;
96 }
Calin Juravle31f2c152015-10-23 17:56:15 +010097 return fd;
98}
99
100static bool CloseDescriptorForFile(int fd, const std::string& filename) {
101 // Now unlock the file, allowing another process in.
102 int err = flock(fd, LOCK_UN);
103 if (err < 0) {
104 PLOG(WARNING) << "Failed to unlock profile file " << filename;
105 return false;
106 }
107
108 // Done, close the file.
109 err = ::close(fd);
110 if (err < 0) {
111 PLOG(WARNING) << "Failed to close descriptor for profile file" << filename;
112 return false;
113 }
114
115 return true;
116}
117
118static void WriteToFile(int fd, const std::ostringstream& os) {
119 std::string data(os.str());
120 const char *p = data.c_str();
121 size_t length = data.length();
122 do {
123 int n = ::write(fd, p, length);
124 p += n;
125 length -= n;
126 } while (length > 0);
127}
128
Calin Juravle226501b2015-12-11 14:41:31 +0000129static constexpr const char kFieldSeparator = ',';
130static constexpr const char kLineSeparator = '\n';
Calin Juravle31f2c152015-10-23 17:56:15 +0100131
132/**
133 * Serialization format:
Calin Juravle66f55232015-12-08 15:09:10 +0000134 * dex_location1,dex_location_checksum1,method_id11,method_id12...
135 * dex_location2,dex_location_checksum2,method_id21,method_id22...
Calin Juravle31f2c152015-10-23 17:56:15 +0100136 * e.g.
Calin Juravle66f55232015-12-08 15:09:10 +0000137 * /system/priv-app/app/app.apk,131232145,11,23,454,54
138 * /system/priv-app/app/app.apk:classes5.dex,218490184,39,13,49,1
Calin Juravle31f2c152015-10-23 17:56:15 +0100139 **/
Calin Juravle998c2162015-12-21 15:39:33 +0200140bool ProfileCompilationInfo::Save(const std::string& filename) {
Calin Juravle226501b2015-12-11 14:41:31 +0000141 int fd = OpenFile(filename, READ_WRITE);
Calin Juravle31f2c152015-10-23 17:56:15 +0100142 if (fd == -1) {
143 return false;
144 }
145
146 // TODO(calin): Merge with a previous existing profile.
147 // TODO(calin): Profile this and see how much memory it takes. If too much,
148 // write to file directly.
149 std::ostringstream os;
Calin Juravle998c2162015-12-21 15:39:33 +0200150 for (const auto& it : info_) {
151 const std::string& dex_location = it.first;
152 const DexFileData& dex_data = it.second;
Calin Juravle31f2c152015-10-23 17:56:15 +0100153
Calin Juravle998c2162015-12-21 15:39:33 +0200154 os << dex_location << kFieldSeparator << dex_data.checksum;
155 for (auto method_it : dex_data.method_set) {
Calin Juravle31f2c152015-10-23 17:56:15 +0100156 os << kFieldSeparator << method_it;
157 }
158 os << kLineSeparator;
159 }
160
161 WriteToFile(fd, os);
162
163 return CloseDescriptorForFile(fd, filename);
164}
Calin Juravle226501b2015-12-11 14:41:31 +0000165
166// TODO(calin): This a duplicate of Utils::Split fixing the case where the first character
167// is the separator. Merge the fix into Utils::Split once verified that it doesn't break its users.
168static void SplitString(const std::string& s, char separator, std::vector<std::string>* result) {
169 const char* p = s.data();
170 const char* end = p + s.size();
171 // Check if the first character is the separator.
172 if (p != end && *p ==separator) {
173 result->push_back("");
174 ++p;
175 }
176 // Process the rest of the characters.
177 while (p != end) {
178 if (*p == separator) {
179 ++p;
180 } else {
181 const char* start = p;
182 while (++p != end && *p != separator) {
183 // Skip to the next occurrence of the separator.
184 }
185 result->push_back(std::string(start, p - start));
186 }
187 }
188}
189
Calin Juravle998c2162015-12-21 15:39:33 +0200190bool ProfileCompilationInfo::AddData(const std::string& dex_location,
191 uint32_t checksum,
192 uint16_t method_idx) {
193 auto info_it = info_.find(dex_location);
194 if (info_it == info_.end()) {
195 info_it = info_.Put(dex_location, DexFileData(checksum));
196 }
197 if (info_it->second.checksum != checksum) {
198 LOG(WARNING) << "Checksum mismatch for dex " << dex_location;
199 return false;
200 }
201 info_it->second.method_set.insert(method_idx);
202 return true;
203}
204
205bool ProfileCompilationInfo::ProcessLine(const std::string& line) {
Calin Juravle226501b2015-12-11 14:41:31 +0000206 std::vector<std::string> parts;
207 SplitString(line, kFieldSeparator, &parts);
208 if (parts.size() < 3) {
209 LOG(WARNING) << "Invalid line: " << line;
210 return false;
211 }
212
Calin Juravle66f55232015-12-08 15:09:10 +0000213 const std::string& dex_location = parts[0];
Calin Juravle226501b2015-12-11 14:41:31 +0000214 uint32_t checksum;
215 if (!ParseInt(parts[1].c_str(), &checksum)) {
216 return false;
217 }
218
Calin Juravle226501b2015-12-11 14:41:31 +0000219 for (size_t i = 2; i < parts.size(); i++) {
220 uint32_t method_idx;
221 if (!ParseInt(parts[i].c_str(), &method_idx)) {
222 LOG(WARNING) << "Cannot parse method_idx " << parts[i];
223 return false;
224 }
Calin Juravle998c2162015-12-21 15:39:33 +0200225 AddData(dex_location, checksum, method_idx);
Calin Juravle226501b2015-12-11 14:41:31 +0000226 }
227 return true;
228}
229
230// Parses the buffer (of length n) starting from start_from and identify new lines
231// based on kLineSeparator marker.
232// Returns the first position after kLineSeparator in the buffer (starting from start_from),
233// or -1 if the marker doesn't appear.
234// The processed characters are appended to the given line.
235static int GetLineFromBuffer(char* buffer, int n, int start_from, std::string& line) {
236 if (start_from >= n) {
237 return -1;
238 }
239 int new_line_pos = -1;
240 for (int i = start_from; i < n; i++) {
241 if (buffer[i] == kLineSeparator) {
242 new_line_pos = i;
243 break;
244 }
245 }
246 int append_limit = new_line_pos == -1 ? n : new_line_pos;
247 line.append(buffer + start_from, append_limit - start_from);
248 // Jump over kLineSeparator and return the position of the next character.
249 return new_line_pos == -1 ? new_line_pos : new_line_pos + 1;
250}
251
Calin Juravle998c2162015-12-21 15:39:33 +0200252bool ProfileCompilationInfo::Load(const std::string& filename) {
253 int fd = OpenFile(filename, READ);
Calin Juravle226501b2015-12-11 14:41:31 +0000254 if (fd == -1) {
255 return false;
256 }
257
258 std::string current_line;
259 const int kBufferSize = 1024;
260 char buffer[kBufferSize];
261 bool success = true;
262
263 while (success) {
264 int n = read(fd, buffer, kBufferSize);
265 if (n < 0) {
Calin Juravle998c2162015-12-21 15:39:33 +0200266 PLOG(WARNING) << "Error when reading profile file " << filename;
Calin Juravle226501b2015-12-11 14:41:31 +0000267 success = false;
268 break;
269 } else if (n == 0) {
270 break;
271 }
272 // Detect the new lines from the buffer. If we manage to complete a line,
273 // process it. Otherwise append to the current line.
274 int current_start_pos = 0;
275 while (current_start_pos < n) {
276 current_start_pos = GetLineFromBuffer(buffer, n, current_start_pos, current_line);
277 if (current_start_pos == -1) {
278 break;
279 }
Calin Juravle998c2162015-12-21 15:39:33 +0200280 if (!ProcessLine(current_line)) {
Calin Juravle226501b2015-12-11 14:41:31 +0000281 success = false;
282 break;
283 }
284 // Reset the current line (we just processed it).
285 current_line.clear();
286 }
287 }
288 if (!success) {
289 info_.clear();
290 }
Calin Juravle998c2162015-12-21 15:39:33 +0200291 return CloseDescriptorForFile(fd, filename) && success;
292}
293
294bool ProfileCompilationInfo::Load(const ProfileCompilationInfo& other) {
295 for (const auto& other_it : other.info_) {
296 const std::string& other_dex_location = other_it.first;
297 const DexFileData& other_dex_data = other_it.second;
298
299 auto info_it = info_.find(other_dex_location);
300 if (info_it == info_.end()) {
301 info_it = info_.Put(other_dex_location, DexFileData(other_dex_data.checksum));
302 }
303 if (info_it->second.checksum != other_dex_data.checksum) {
304 LOG(WARNING) << "Checksum mismatch for dex " << other_dex_location;
305 return false;
306 }
307 info_it->second.method_set.insert(other_dex_data.method_set.begin(),
308 other_dex_data.method_set.end());
309 }
310 return true;
Calin Juravle226501b2015-12-11 14:41:31 +0000311}
312
313bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
Calin Juravle998c2162015-12-21 15:39:33 +0200314 auto info_it = info_.find(method_ref.dex_file->GetLocation());
Calin Juravle226501b2015-12-11 14:41:31 +0000315 if (info_it != info_.end()) {
Calin Juravle998c2162015-12-21 15:39:33 +0200316 if (method_ref.dex_file->GetLocationChecksum() != info_it->second.checksum) {
317 return false;
Calin Juravle226501b2015-12-11 14:41:31 +0000318 }
Calin Juravle998c2162015-12-21 15:39:33 +0200319 const std::set<uint16_t>& methods = info_it->second.method_set;
320 return methods.find(method_ref.dex_method_index) != methods.end();
Calin Juravle226501b2015-12-11 14:41:31 +0000321 }
322 return false;
323}
324
Calin Juravle998c2162015-12-21 15:39:33 +0200325uint32_t ProfileCompilationInfo::GetNumberOfMethods() const {
326 uint32_t total = 0;
327 for (const auto& it : info_) {
328 total += it.second.method_set.size();
329 }
330 return total;
331}
332
333std::string ProfileCompilationInfo::DumpInfo(const std::vector<const DexFile*>* dex_files,
334 bool print_full_dex_location) const {
Calin Juravle226501b2015-12-11 14:41:31 +0000335 std::ostringstream os;
336 if (info_.empty()) {
337 return "ProfileInfo: empty";
338 }
339
340 os << "ProfileInfo:";
341
Calin Juravle226501b2015-12-11 14:41:31 +0000342 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
Calin Juravle998c2162015-12-21 15:39:33 +0200343 for (const auto& it : info_) {
Calin Juravle226501b2015-12-11 14:41:31 +0000344 os << "\n";
Calin Juravle998c2162015-12-21 15:39:33 +0200345 const std::string& location = it.first;
346 const DexFileData& dex_data = it.second;
Calin Juravle226501b2015-12-11 14:41:31 +0000347 if (print_full_dex_location) {
348 os << location;
349 } else {
350 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
351 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
352 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
353 }
Calin Juravle998c2162015-12-21 15:39:33 +0200354 for (const auto method_it : dex_data.method_set) {
355 if (dex_files != nullptr) {
356 const DexFile* dex_file = nullptr;
357 for (size_t i = 0; i < dex_files->size(); i++) {
358 if (location == (*dex_files)[i]->GetLocation()) {
359 dex_file = (*dex_files)[i];
360 }
361 }
362 if (dex_file != nullptr) {
363 os << "\n " << PrettyMethod(method_it, *dex_file, true);
364 }
Calin Juravle226501b2015-12-11 14:41:31 +0000365 }
Calin Juravle998c2162015-12-21 15:39:33 +0200366 os << "\n " << method_it;
Calin Juravle226501b2015-12-11 14:41:31 +0000367 }
368 }
369 return os.str();
370}
371
Calin Juravle31f2c152015-10-23 17:56:15 +0100372} // namespace art