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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/android/library_loader/library_prefetcher.h"
6
7 #include <stdlib.h>
8 #include <sys/resource.h>
9 #include <unistd.h>
10 #include <utility>
11 #include <vector>
12
13 namespace base {
14 namespace android {
15
16 FileLineIterator::FileLineIterator(const base::FilePath& path) {
17 file_ = base::OpenFile(path, "r");
18 }
19
20 FileLineIterator::~FileLineIterator() {
21 if (file_) {
22 base::CloseFile(file_);
23 }
24 }
25
26 std::string FileLineIterator::NextLine() {
27 if (!file_) {
28 return std::string();
29 }
30 char* line = fgets(line_buffer_, kMaxLineLength - 1, file_);
31 return line ? std::string(line) : std::string();
32 }
33
34 ProcMapsIterator::ProcMapsIterator(FileLineIterator* line_iterator)
35 : line_iterator_(line_iterator) {
36 }
37
38 bool ProcMapsIterator::Next(uint64_t* start,
39 uint64_t* end,
40 std::string* flags,
41 uint64_t* offset,
42 std::string* device,
43 int64_t* inode,
44 std::string* filename) {
45 std::string line = line_iterator_->NextLine();
46 if (line.size() == 0) {
47 return false;
48 }
49 // Example:
50 // b6e62000-b6e64000 r-xp 00000000 b3:15 1204 /system/lib/libstdc++.so
51 // start-end flags offset device inode [filename]
52 std::string line_str(line);
53 std::vector<std::string> pieces;
54 base::SplitStringAlongWhitespace(line_str, &pieces);
55 size_t pieces_count = pieces.size();
56 // Filename is optional
57 DCHECK((pieces_count == 5) || (pieces_count == 6));
58
59 const char* start_end = pieces[0].c_str();
60 char* delimiter_ptr, *dummy;
61 static_assert(sizeof(long long) == sizeof(int64_t),
62 "long long is not int64_t"); // This way we can use strtoll().
63 *start = strtoull(start_end, &delimiter_ptr, 16);
64 *end = strtoull(delimiter_ptr + 1, &dummy, 16);
65 *flags = pieces[1];
66 *offset = strtoull(pieces[2].c_str(), &dummy, 16);
67 *device = pieces[3];
68 *inode = strtoll(pieces[4].c_str(), &dummy, 10);
69 if (pieces_count == 5) {
70 *filename = std::string();
71 } else {
72 *filename = pieces[5];
73 }
74 return true;
75 }
76
77 namespace {
78
79 // Android defines the background priority to this value since at least 2009
80 // (see Process.java).
81 const int kBackgroundPriority = 10;
82 // We may load directly from the APK.
83 const int kSuffixesToMatchCount = 2;
84 const char* kSuffixesToMatch[] = {"libchrome.so", "base.apk"};
85
86 bool IsReadableAndPrivate(const char* flags) {
87 return flags && flags[0] == 'r' && flags[3] == 'p';
88 }
89
90 bool FilenameMatchesSuffix(const char* filename) {
91 if (!filename)
92 return false;
93 const size_t filename_length = strlen(filename);
94 for (int i = 0; i < kSuffixesToMatchCount; i++) {
95 const size_t suffix_length = strlen(kSuffixesToMatch[i]);
96 if (filename_length < suffix_length)
97 continue;
98 if (!strcmp(kSuffixesToMatch[i],
99 filename + filename_length - suffix_length))
100 return true;
101 }
102 return false;
103 }
104
105 // For each range, reads a byte per page to force it into the page cache.
106 void Prefetch(const std::vector<std::pair<uint64_t, uint64_t>>& ranges) {
107 for (const auto& range : ranges) {
108 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.
109 unsigned char* end_address = (unsigned char*)range.second;
110 unsigned char dummy;
111 for (unsigned char* addr = start_address; addr < end_address - sizeof(int);
112 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.
113 // Volatile is required to prevent the compiler from eliminating this
114 // loop.
115 dummy ^= *((volatile unsigned char*) addr);
116 }
117 }
118 }
119
120 } // namespace
121
122 std::vector<std::pair<uint64_t, uint64_t>> FindRanges(
123 FileLineIterator* file_line_iterator) {
124 std::vector<std::pair<uint64_t, uint64_t>> ranges;
125 ProcMapsIterator maps_iterator(file_line_iterator);
126 uint64_t start, end, offset;
127 int64_t inode;
128 std::string flags, device, filename;
129
130 while (maps_iterator.Next(&start, &end, &flags, &offset, &device, &inode,
131 &filename)) {
132 if (FilenameMatchesSuffix(filename.c_str()) &&
133 IsReadableAndPrivate(flags.c_str())) {
134 ranges.push_back(std::make_pair(start, end));
135 }
136 }
137 return ranges;
138 }
139
140 // Forks a process prefetching the native library. This is done in a forked
141 // process for the following reasons:
142 // - 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
143 // 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.
144 // - Not inflating the memory used by the main process uselessly, which could
145 // increase its likelihood to be killed.
146 // - Have the pages clean and not referenced, to make them easy for the kernel
147 // to evict, minimizing disruption caused by this prefetching.
148 // 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.
149 // the android runtime, can be killed at any time, which is not an issue here.
150 void ForkAndPrefetchNativeLibrary() {
151 // Looking for ranges is done before the fork, to avoid syscalls and/or
152 // memory allocations in the forked process, that can be problematic.
153 FileLineIterator file_line_iterator(base::FilePath("/proc/self/maps"));
154 std::vector<std::pair<uint64_t, uint64_t>> ranges =
155 FindRanges(&file_line_iterator);
156 pid_t pid = fork();
157 if (pid == 0) {
158 setpriority(PRIO_PROCESS, 0, kBackgroundPriority);
159 Prefetch(ranges);
160 exit(0);
161 }
162 }
163
164 } // namespace android
165 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698