blob: 511b53d5dbb5d24b1b1ed1bcf1432795bd8a94c6 [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>
20#include <set>
21#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
33// An arbitrary value to throttle save requests. Set to 500ms for now.
34static constexpr const uint64_t kMilisecondsToNano = 1000000;
35static constexpr const uint64_t kMinimumTimeBetweenSavesNs = 500 * kMilisecondsToNano;
36
Calin Juravle66f55232015-12-08 15:09:10 +000037void OfflineProfilingInfo::SetTrackedDexLocations(
38 const std::vector<std::string>& dex_base_locations) {
39 tracked_dex_base_locations_.clear();
40 tracked_dex_base_locations_.insert(dex_base_locations.begin(), dex_base_locations.end());
41 VLOG(profiler) << "Tracking dex locations: " << Join(dex_base_locations, ':');
42}
43
44const std::set<const std::string>& OfflineProfilingInfo::GetTrackedDexLocations() const {
45 return tracked_dex_base_locations_;
46}
47
Calin Juravle31f2c152015-10-23 17:56:15 +010048bool OfflineProfilingInfo::NeedsSaving(uint64_t last_update_time_ns) const {
Calin Juravle66f55232015-12-08 15:09:10 +000049 return !tracked_dex_base_locations_.empty() &&
50 (last_update_time_ns - last_update_time_ns_.LoadRelaxed() > kMinimumTimeBetweenSavesNs);
Calin Juravle31f2c152015-10-23 17:56:15 +010051}
52
53void OfflineProfilingInfo::SaveProfilingInfo(const std::string& filename,
54 uint64_t last_update_time_ns,
55 const std::set<ArtMethod*>& methods) {
56 if (!NeedsSaving(last_update_time_ns)) {
57 VLOG(profiler) << "No need to saved profile info to " << filename;
58 return;
59 }
60
61 if (methods.empty()) {
62 VLOG(profiler) << "No info to save to " << filename;
63 return;
64 }
65
66 DexFileToMethodsMap info;
67 {
68 ScopedObjectAccess soa(Thread::Current());
69 for (auto it = methods.begin(); it != methods.end(); it++) {
Calin Juravle66f55232015-12-08 15:09:10 +000070 DCHECK(ContainsElement(tracked_dex_base_locations_, (*it)->GetDexFile()->GetBaseLocation()));
Calin Juravle31f2c152015-10-23 17:56:15 +010071 AddMethodInfo(*it, &info);
72 }
73 }
74
75 // This doesn't need locking because we are trying to lock the file for exclusive
76 // access and fail immediately if we can't.
77 if (Serialize(filename, info)) {
78 last_update_time_ns_.StoreRelaxed(last_update_time_ns);
79 VLOG(profiler) << "Successfully saved profile info to "
80 << filename << " with time stamp: " << last_update_time_ns;
81 }
82}
83
Calin Juravle31f2c152015-10-23 17:56:15 +010084void OfflineProfilingInfo::AddMethodInfo(ArtMethod* method, DexFileToMethodsMap* info) {
85 DCHECK(method != nullptr);
86 const DexFile* dex_file = method->GetDexFile();
87
88 auto info_it = info->find(dex_file);
89 if (info_it == info->end()) {
90 info_it = info->Put(dex_file, std::set<uint32_t>());
91 }
92 info_it->second.insert(method->GetDexMethodIndex());
93}
94
Calin Juravle226501b2015-12-11 14:41:31 +000095enum OpenMode {
96 READ,
97 READ_WRITE
98};
99
100static int OpenFile(const std::string& filename, OpenMode open_mode) {
101 int fd = -1;
102 switch (open_mode) {
103 case READ:
104 fd = open(filename.c_str(), O_RDONLY);
105 break;
106 case READ_WRITE:
107 // TODO(calin) allow the shared uid of the app to access the file.
108 fd = open(filename.c_str(),
109 O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
110 S_IRUSR | S_IWUSR);
111 break;
112 }
113
Calin Juravle31f2c152015-10-23 17:56:15 +0100114 if (fd < 0) {
115 PLOG(WARNING) << "Failed to open profile file " << filename;
116 return -1;
117 }
118
119 // Lock the file for exclusive access but don't wait if we can't lock it.
120 int err = flock(fd, LOCK_EX | LOCK_NB);
121 if (err < 0) {
122 PLOG(WARNING) << "Failed to lock profile file " << filename;
123 return -1;
124 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100125 return fd;
126}
127
128static bool CloseDescriptorForFile(int fd, const std::string& filename) {
129 // Now unlock the file, allowing another process in.
130 int err = flock(fd, LOCK_UN);
131 if (err < 0) {
132 PLOG(WARNING) << "Failed to unlock profile file " << filename;
133 return false;
134 }
135
136 // Done, close the file.
137 err = ::close(fd);
138 if (err < 0) {
139 PLOG(WARNING) << "Failed to close descriptor for profile file" << filename;
140 return false;
141 }
142
143 return true;
144}
145
146static void WriteToFile(int fd, const std::ostringstream& os) {
147 std::string data(os.str());
148 const char *p = data.c_str();
149 size_t length = data.length();
150 do {
151 int n = ::write(fd, p, length);
152 p += n;
153 length -= n;
154 } while (length > 0);
155}
156
Calin Juravle226501b2015-12-11 14:41:31 +0000157static constexpr const char kFieldSeparator = ',';
158static constexpr const char kLineSeparator = '\n';
Calin Juravle31f2c152015-10-23 17:56:15 +0100159
160/**
161 * Serialization format:
Calin Juravle66f55232015-12-08 15:09:10 +0000162 * dex_location1,dex_location_checksum1,method_id11,method_id12...
163 * dex_location2,dex_location_checksum2,method_id21,method_id22...
Calin Juravle31f2c152015-10-23 17:56:15 +0100164 * e.g.
Calin Juravle66f55232015-12-08 15:09:10 +0000165 * /system/priv-app/app/app.apk,131232145,11,23,454,54
166 * /system/priv-app/app/app.apk:classes5.dex,218490184,39,13,49,1
Calin Juravle31f2c152015-10-23 17:56:15 +0100167 **/
168bool OfflineProfilingInfo::Serialize(const std::string& filename,
169 const DexFileToMethodsMap& info) const {
Calin Juravle226501b2015-12-11 14:41:31 +0000170 int fd = OpenFile(filename, READ_WRITE);
Calin Juravle31f2c152015-10-23 17:56:15 +0100171 if (fd == -1) {
172 return false;
173 }
174
175 // TODO(calin): Merge with a previous existing profile.
176 // TODO(calin): Profile this and see how much memory it takes. If too much,
177 // write to file directly.
178 std::ostringstream os;
179 for (auto it : info) {
180 const DexFile* dex_file = it.first;
181 const std::set<uint32_t>& method_dex_ids = it.second;
182
Calin Juravle66f55232015-12-08 15:09:10 +0000183 os << dex_file->GetLocation()
Calin Juravle31f2c152015-10-23 17:56:15 +0100184 << kFieldSeparator
185 << dex_file->GetLocationChecksum();
186 for (auto method_it : method_dex_ids) {
187 os << kFieldSeparator << method_it;
188 }
189 os << kLineSeparator;
190 }
191
192 WriteToFile(fd, os);
193
194 return CloseDescriptorForFile(fd, filename);
195}
Calin Juravle226501b2015-12-11 14:41:31 +0000196
197// TODO(calin): This a duplicate of Utils::Split fixing the case where the first character
198// is the separator. Merge the fix into Utils::Split once verified that it doesn't break its users.
199static void SplitString(const std::string& s, char separator, std::vector<std::string>* result) {
200 const char* p = s.data();
201 const char* end = p + s.size();
202 // Check if the first character is the separator.
203 if (p != end && *p ==separator) {
204 result->push_back("");
205 ++p;
206 }
207 // Process the rest of the characters.
208 while (p != end) {
209 if (*p == separator) {
210 ++p;
211 } else {
212 const char* start = p;
213 while (++p != end && *p != separator) {
214 // Skip to the next occurrence of the separator.
215 }
216 result->push_back(std::string(start, p - start));
217 }
218 }
219}
220
221bool ProfileCompilationInfo::ProcessLine(const std::string& line,
222 const std::vector<const DexFile*>& dex_files) {
223 std::vector<std::string> parts;
224 SplitString(line, kFieldSeparator, &parts);
225 if (parts.size() < 3) {
226 LOG(WARNING) << "Invalid line: " << line;
227 return false;
228 }
229
Calin Juravle66f55232015-12-08 15:09:10 +0000230 const std::string& dex_location = parts[0];
Calin Juravle226501b2015-12-11 14:41:31 +0000231 uint32_t checksum;
232 if (!ParseInt(parts[1].c_str(), &checksum)) {
233 return false;
234 }
235
236 const DexFile* current_dex_file = nullptr;
237 for (auto dex_file : dex_files) {
Calin Juravle66f55232015-12-08 15:09:10 +0000238 if (dex_file->GetLocation() == dex_location) {
Calin Juravle226501b2015-12-11 14:41:31 +0000239 if (checksum != dex_file->GetLocationChecksum()) {
240 LOG(WARNING) << "Checksum mismatch for "
241 << dex_file->GetLocation() << " when parsing " << filename_;
242 return false;
243 }
244 current_dex_file = dex_file;
245 break;
246 }
247 }
248 if (current_dex_file == nullptr) {
249 return true;
250 }
251
252 for (size_t i = 2; i < parts.size(); i++) {
253 uint32_t method_idx;
254 if (!ParseInt(parts[i].c_str(), &method_idx)) {
255 LOG(WARNING) << "Cannot parse method_idx " << parts[i];
256 return false;
257 }
258 uint16_t class_idx = current_dex_file->GetMethodId(method_idx).class_idx_;
259 auto info_it = info_.find(current_dex_file);
260 if (info_it == info_.end()) {
261 info_it = info_.Put(current_dex_file, ClassToMethodsMap());
262 }
263 ClassToMethodsMap& class_map = info_it->second;
264 auto class_it = class_map.find(class_idx);
265 if (class_it == class_map.end()) {
266 class_it = class_map.Put(class_idx, std::set<uint32_t>());
267 }
268 class_it->second.insert(method_idx);
269 }
270 return true;
271}
272
273// Parses the buffer (of length n) starting from start_from and identify new lines
274// based on kLineSeparator marker.
275// Returns the first position after kLineSeparator in the buffer (starting from start_from),
276// or -1 if the marker doesn't appear.
277// The processed characters are appended to the given line.
278static int GetLineFromBuffer(char* buffer, int n, int start_from, std::string& line) {
279 if (start_from >= n) {
280 return -1;
281 }
282 int new_line_pos = -1;
283 for (int i = start_from; i < n; i++) {
284 if (buffer[i] == kLineSeparator) {
285 new_line_pos = i;
286 break;
287 }
288 }
289 int append_limit = new_line_pos == -1 ? n : new_line_pos;
290 line.append(buffer + start_from, append_limit - start_from);
291 // Jump over kLineSeparator and return the position of the next character.
292 return new_line_pos == -1 ? new_line_pos : new_line_pos + 1;
293}
294
295bool ProfileCompilationInfo::Load(const std::vector<const DexFile*>& dex_files) {
296 if (dex_files.empty()) {
297 return true;
298 }
299 if (kIsDebugBuild) {
Calin Juravle66f55232015-12-08 15:09:10 +0000300 // In debug builds verify that the locations are unique.
301 std::set<std::string> locations;
Calin Juravle226501b2015-12-11 14:41:31 +0000302 for (auto dex_file : dex_files) {
Calin Juravle66f55232015-12-08 15:09:10 +0000303 const std::string& location = dex_file->GetLocation();
304 DCHECK(locations.find(location) == locations.end())
Calin Juravle226501b2015-12-11 14:41:31 +0000305 << "DexFiles appear to belong to different apks."
Calin Juravle66f55232015-12-08 15:09:10 +0000306 << " There are multiple dex files with the same location: "
307 << location;
308 locations.insert(location);
Calin Juravle226501b2015-12-11 14:41:31 +0000309 }
310 }
311 info_.clear();
312
313 int fd = OpenFile(filename_, READ);
314 if (fd == -1) {
315 return false;
316 }
317
318 std::string current_line;
319 const int kBufferSize = 1024;
320 char buffer[kBufferSize];
321 bool success = true;
322
323 while (success) {
324 int n = read(fd, buffer, kBufferSize);
325 if (n < 0) {
326 PLOG(WARNING) << "Error when reading profile file " << filename_;
327 success = false;
328 break;
329 } else if (n == 0) {
330 break;
331 }
332 // Detect the new lines from the buffer. If we manage to complete a line,
333 // process it. Otherwise append to the current line.
334 int current_start_pos = 0;
335 while (current_start_pos < n) {
336 current_start_pos = GetLineFromBuffer(buffer, n, current_start_pos, current_line);
337 if (current_start_pos == -1) {
338 break;
339 }
340 if (!ProcessLine(current_line, dex_files)) {
341 success = false;
342 break;
343 }
344 // Reset the current line (we just processed it).
345 current_line.clear();
346 }
347 }
348 if (!success) {
349 info_.clear();
350 }
351 return CloseDescriptorForFile(fd, filename_) && success;
352}
353
354bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const {
355 auto info_it = info_.find(method_ref.dex_file);
356 if (info_it != info_.end()) {
357 uint16_t class_idx = method_ref.dex_file->GetMethodId(method_ref.dex_method_index).class_idx_;
358 const ClassToMethodsMap& class_map = info_it->second;
359 auto class_it = class_map.find(class_idx);
360 if (class_it != class_map.end()) {
361 const std::set<uint32_t>& methods = class_it->second;
362 return methods.find(method_ref.dex_method_index) != methods.end();
363 }
364 return false;
365 }
366 return false;
367}
368
369std::string ProfileCompilationInfo::DumpInfo(bool print_full_dex_location) const {
370 std::ostringstream os;
371 if (info_.empty()) {
372 return "ProfileInfo: empty";
373 }
374
375 os << "ProfileInfo:";
376
377 // Use an additional map to achieve a predefined order based on the dex locations.
378 SafeMap<const std::string, const DexFile*> dex_locations_map;
379 for (auto info_it : info_) {
380 dex_locations_map.Put(info_it.first->GetLocation(), info_it.first);
381 }
382
383 const std::string kFirstDexFileKeySubstitute = ":classes.dex";
384 for (auto dex_file_it : dex_locations_map) {
385 os << "\n";
386 const std::string& location = dex_file_it.first;
387 const DexFile* dex_file = dex_file_it.second;
388 if (print_full_dex_location) {
389 os << location;
390 } else {
391 // Replace the (empty) multidex suffix of the first key with a substitute for easier reading.
392 std::string multidex_suffix = DexFile::GetMultiDexSuffix(location);
393 os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix);
394 }
395 for (auto class_it : info_.find(dex_file)->second) {
396 for (auto method_it : class_it.second) {
397 os << "\n " << PrettyMethod(method_it, *dex_file, true);
398 }
399 }
400 }
401 return os.str();
402}
403
Calin Juravle31f2c152015-10-23 17:56:15 +0100404} // namespace art