OLD | NEW |
---|---|
(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_); | |
pasko
2015/03/24 17:10:36
why so low level and complex with FILE* and fgets
Benoit L
2015/03/25 10:36:18
I would prefer to avoid an unbounded memory alloca
| |
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) { | |
rmcilroy
2015/03/24 15:18:26
nit - this might be neater wrapping up the argumen
Benoit L
2015/03/25 10:36:19
Done.
| |
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)); | |
rmcilroy
2015/03/24 15:18:26
Either do this as a CHECK or add an if to only to
Benoit L
2015/03/25 10:36:18
Done.
| |
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(). | |
pasko
2015/03/24 17:10:36
StringToInt64?
Benoit L
2015/03/25 10:36:19
Done.
| |
63 *start = strtoull(start_end, &delimiter_ptr, 16); | |
64 *end = strtoull(delimiter_ptr + 1, &dummy, 16); | |
rmcilroy
2015/03/24 15:18:26
nit - I would prefer you just used base::SplitStri
Benoit L
2015/03/25 10:36:18
Done.
| |
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 // Valid for all the Android architectures. | |
83 const size_t kPageSize = 4096; | |
84 // We may load directly from the APK. | |
85 const int kSuffixesToMatchCount = 2; | |
86 const char* kSuffixesToMatch[] = {"libchrome.so", "base.apk"}; | |
pasko
2015/03/24 17:10:36
why base.apk? is the chrome apk always renamed to
Benoit L
2015/03/25 10:36:19
According to the android source code (see PackageI
| |
87 | |
88 bool IsReadableAndPrivate(const char* flags) { | |
89 return flags && flags[0] == 'r' && flags[3] == 'p'; | |
rmcilroy
2015/03/24 15:18:26
nit - add a check here that flags is long enough t
Benoit L
2015/03/25 10:36:18
Done.
| |
90 } | |
91 | |
92 bool FilenameMatchesSuffix(const char* filename) { | |
93 if (!filename) | |
94 return false; | |
95 const size_t filename_length = strlen(filename); | |
96 for (int i = 0; i < kSuffixesToMatchCount; i++) { | |
97 const size_t suffix_length = strlen(kSuffixesToMatch[i]); | |
98 if (filename_length < suffix_length) | |
99 continue; | |
100 if (!strcmp(kSuffixesToMatch[i], | |
101 filename + filename_length - suffix_length)) | |
102 return true; | |
103 } | |
104 return false; | |
105 } | |
106 | |
107 // For each range, reads a byte per page to force it into the page cache. | |
108 void Prefetch(const std::vector<std::pair<uint64_t, uint64_t>>& ranges) { | |
109 for (const auto& range : ranges) { | |
110 unsigned char* start_address = (unsigned char*) range.first; | |
111 unsigned char* end_address = (unsigned char*) range.second; | |
112 unsigned char dummy; | |
113 for (unsigned char* addr = start_address; addr < end_address - sizeof(int); | |
pasko
2015/03/24 17:10:36
s/ - sizeof(int)// ?
you can CHECK(!(start_addres
Benoit L
2015/03/25 10:36:18
Done.
| |
114 addr += kPageSize) { | |
115 // Volatile is required to prevent the compiler from eliminating this | |
116 // loop. | |
117 dummy ^= *((volatile unsigned char*) addr); | |
118 } | |
119 } | |
120 } | |
121 | |
122 } // namespace | |
123 | |
124 std::vector<std::pair<uint64_t, uint64_t>> FindRanges( | |
125 FileLineIterator* file_line_iterator) { | |
126 std::vector<std::pair<uint64_t, uint64_t>> ranges; | |
127 ProcMapsIterator maps_iterator(file_line_iterator); | |
128 uint64_t start, end, offset; | |
129 int64_t inode; | |
130 std::string flags, device, filename; | |
131 | |
132 while (maps_iterator.Next(&start, &end, &flags, &offset, &device, &inode, | |
133 &filename)) { | |
134 if (FilenameMatchesSuffix(filename.c_str()) && | |
135 IsReadableAndPrivate(flags.c_str())) { | |
136 ranges.push_back(std::make_pair(start, end)); | |
pasko
2015/03/24 17:10:36
We could potentially have both libchrome.so and .a
Benoit L
2015/03/25 10:36:18
Well, we have several ranges in pretty much all ca
pasko
2015/03/25 12:40:53
Got it. As per offline discussion, on later system
rmcilroy
2015/03/25 15:12:18
It's due to packed relocations, but yes we will ne
| |
137 } | |
138 } | |
139 return ranges; | |
140 } | |
141 | |
142 // Forks a process prefetching the native library. This is done in a forked | |
143 // process for the following reasons: | |
144 // - Isolating the main process from mistakes in the parsing. If the parsing | |
145 // returns an incorrect address, only the forked process will crash. | |
rmcilroy
2015/03/24 15:18:26
The parsing is done on the main process though.
Benoit L
2015/03/25 10:36:18
Acknowledged.
| |
146 // - Not inflating the memory used by the main process uselessly, which could | |
147 // increase its likelihood to be killed. | |
148 // - Have the pages clean and not referenced, to make them easy for the kernel | |
149 // to evict, minimizing disruption caused by this prefetching. | |
rmcilroy
2015/03/24 15:18:26
Does forking actually helps this - the pages shoul
Benoit L
2015/03/25 10:36:18
You are right, removed this comment.
Done.
| |
150 // The forked process has background priority and, since it is not declared to | |
151 // the android runtime, can be killed at any time, which is not an issue here. | |
152 void ForkAndPrefetchNativeLibrary() { | |
153 // Looking for ranges is done before the fork, to avoid syscalls and/or | |
154 // memory allocations in the forked process, that can be problematic. | |
155 FileLineIterator file_line_iterator(base::FilePath("/proc/self/maps")); | |
156 std::vector<std::pair<uint64_t, uint64_t>> ranges = | |
157 FindRanges(&file_line_iterator); | |
158 pid_t pid = fork(); | |
159 if (pid == 0) { | |
160 setpriority(PRIO_PROCESS, 0, kBackgroundPriority); | |
161 Prefetch(ranges); | |
162 exit(0); | |
163 } | |
164 } | |
165 | |
166 } // namespace android | |
167 } // namespace base | |
OLD | NEW |