Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2869)

Unified Diff: base/android/library_loader/library_prefetcher.cc

Issue 1001343002: Prefetch the native library from native code by parsing /proc/pid/maps. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/android/library_loader/library_prefetcher.cc
diff --git a/base/android/library_loader/library_prefetcher.cc b/base/android/library_loader/library_prefetcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cc281472899c99fe38cf88214c4de6a8434cf17a
--- /dev/null
+++ b/base/android/library_loader/library_prefetcher.cc
@@ -0,0 +1,165 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/android/library_loader/library_prefetcher.h"
+
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <unistd.h>
+#include <utility>
+#include <vector>
+
+namespace base {
+namespace android {
+
+FileLineIterator::FileLineIterator(const base::FilePath& path) {
+ file_ = base::OpenFile(path, "r");
+}
+
+FileLineIterator::~FileLineIterator() {
+ if (file_) {
+ base::CloseFile(file_);
+ }
+}
+
+std::string FileLineIterator::NextLine() {
+ if (!file_) {
+ return std::string();
+ }
+ char* line = fgets(line_buffer_, kMaxLineLength - 1, file_);
+ return line ? std::string(line) : std::string();
+}
+
+ProcMapsIterator::ProcMapsIterator(FileLineIterator* line_iterator)
+ : line_iterator_(line_iterator) {
+}
+
+bool ProcMapsIterator::Next(uint64_t* start,
+ uint64_t* end,
+ std::string* flags,
+ uint64_t* offset,
+ std::string* device,
+ int64_t* inode,
+ std::string* filename) {
+ std::string line = line_iterator_->NextLine();
+ if (line.size() == 0) {
+ return false;
+ }
+ // Example:
+ // b6e62000-b6e64000 r-xp 00000000 b3:15 1204 /system/lib/libstdc++.so
+ // start-end flags offset device inode [filename]
+ std::string line_str(line);
+ std::vector<std::string> pieces;
+ base::SplitStringAlongWhitespace(line_str, &pieces);
+ size_t pieces_count = pieces.size();
+ // Filename is optional
+ DCHECK((pieces_count == 5) || (pieces_count == 6));
+
+ const char* start_end = pieces[0].c_str();
+ char* delimiter_ptr, *dummy;
+ static_assert(sizeof(long long) == sizeof(int64_t),
+ "long long is not int64_t"); // This way we can use strtoll().
+ *start = strtoull(start_end, &delimiter_ptr, 16);
+ *end = strtoull(delimiter_ptr + 1, &dummy, 16);
+ *flags = pieces[1];
+ *offset = strtoull(pieces[2].c_str(), &dummy, 16);
+ *device = pieces[3];
+ *inode = strtoll(pieces[4].c_str(), &dummy, 10);
+ if (pieces_count == 5) {
+ *filename = std::string();
+ } else {
+ *filename = pieces[5];
+ }
+ return true;
+}
+
+namespace {
+
+// Android defines the background priority to this value since at least 2009
+// (see Process.java).
+const int kBackgroundPriority = 10;
+// We may load directly from the APK.
+const int kSuffixesToMatchCount = 2;
+const char* kSuffixesToMatch[] = {"libchrome.so", "base.apk"};
+
+bool IsReadableAndPrivate(const char* flags) {
+ return flags && flags[0] == 'r' && flags[3] == 'p';
+}
+
+bool FilenameMatchesSuffix(const char* filename) {
+ if (!filename)
+ return false;
+ const size_t filename_length = strlen(filename);
+ for (int i = 0; i < kSuffixesToMatchCount; i++) {
+ const size_t suffix_length = strlen(kSuffixesToMatch[i]);
+ if (filename_length < suffix_length)
+ continue;
+ if (!strcmp(kSuffixesToMatch[i],
+ filename + filename_length - suffix_length))
+ return true;
+ }
+ return false;
+}
+
+// For each range, reads a byte per page to force it into the page cache.
+void Prefetch(const std::vector<std::pair<uint64_t, uint64_t>>& ranges) {
+ for (const auto& range : ranges) {
+ unsigned char* start_address = (unsigned char*)range.first;
petrcermak 2015/03/23 15:00:48 nit: There should be a space after the closing par
Benoit L 2015/03/23 16:41:58 Done.
+ unsigned char* end_address = (unsigned char*)range.second;
+ unsigned char dummy;
+ for (unsigned char* addr = start_address; addr < end_address - sizeof(int);
+ addr += 4096) {
petrcermak 2015/03/23 15:00:47 Shouldn't this be a constant (e.g. kPageSize)?
Benoit L 2015/03/23 16:41:58 Done.
+ // Volatile is required to prevent the compiler from eliminating this
+ // loop.
+ dummy ^= *((volatile unsigned char*) addr);
+ }
+ }
+}
+
+} // namespace
+
+std::vector<std::pair<uint64_t, uint64_t>> FindRanges(
+ FileLineIterator* file_line_iterator) {
+ std::vector<std::pair<uint64_t, uint64_t>> ranges;
+ ProcMapsIterator maps_iterator(file_line_iterator);
+ uint64_t start, end, offset;
+ int64_t inode;
+ std::string flags, device, filename;
+
+ while (maps_iterator.Next(&start, &end, &flags, &offset, &device, &inode,
+ &filename)) {
+ if (FilenameMatchesSuffix(filename.c_str()) &&
+ IsReadableAndPrivate(flags.c_str())) {
+ ranges.push_back(std::make_pair(start, end));
+ }
+ }
+ return ranges;
+}
+
+// Forks a process prefetching the native library. This is done in a forked
+// process for the following reasons:
+// - Isolating the main process from mistakes in the parsing. If the parsing
petrcermak 2015/03/23 15:00:47 This doesn't seem to be the case here. If I unders
Benoit L 2015/03/23 16:41:58 I was talking about the fact that we might get inc
+// returns an incorrect address, only the forked process will crash.
petrcermak 2015/03/23 15:00:47 nit: Incorrect indentation of this line.
Benoit L 2015/03/23 16:41:58 Done.
+// - Not inflating the memory used by the main process uselessly, which could
+// increase its likelihood to be killed.
+// - Have the pages clean and not referenced, to make them easy for the kernel
+// to evict, minimizing disruption caused by this prefetching.
+// The forked process has background priority, and since it is not declared to
petrcermak 2015/03/23 15:00:47 nit: The first comma should actually appear *after
Benoit L 2015/03/23 16:41:58 Done.
+// the android runtime, can be killed at any time, which is not an issue here.
+void ForkAndPrefetchNativeLibrary() {
+ // Looking for ranges is done before the fork, to avoid syscalls and/or
+ // memory allocations in the forked process, that can be problematic.
+ FileLineIterator file_line_iterator(base::FilePath("/proc/self/maps"));
+ std::vector<std::pair<uint64_t, uint64_t>> ranges =
+ FindRanges(&file_line_iterator);
+ pid_t pid = fork();
+ if (pid == 0) {
+ setpriority(PRIO_PROCESS, 0, kBackgroundPriority);
+ Prefetch(ranges);
+ exit(0);
+ }
+}
+
+} // namespace android
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698