OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base.library_loader; | 5 package org.chromium.base.library_loader; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
10 import android.os.SystemClock; | 10 import android.os.SystemClock; |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 * | 243 * |
244 * This is done this way, as testing shows that fadvise(FADV_WILLNEED) is | 244 * This is done this way, as testing shows that fadvise(FADV_WILLNEED) is |
245 * detrimental to the startup time. | 245 * detrimental to the startup time. |
246 * | 246 * |
247 * @param context the application context. | 247 * @param context the application context. |
248 */ | 248 */ |
249 public void asyncPrefetchLibrariesToMemory(final Context context) { | 249 public void asyncPrefetchLibrariesToMemory(final Context context) { |
250 new AsyncTask<Void, Void, Void>() { | 250 new AsyncTask<Void, Void, Void>() { |
251 @Override | 251 @Override |
252 protected Void doInBackground(Void... params) { | 252 protected Void doInBackground(Void... params) { |
253 // Note: AsyncTasks are executed in a low priority background | 253 TraceEvent.begin("LibraryLoader.asyncPrefetchLibrariesToMemory")
; |
254 // thread, which is the desired behavior here since we don't | 254 // First, try to fork a new process to do the prefetch. If it |
255 // want to interfere with the rest of the initialization. | 255 // fails, fall back to doing it by mapping the library. |
256 for (String library : NativeLibraries.LIBRARIES) { | 256 boolean ok = nativeForkAndPrefetchNativeLibrary(); |
257 if (Linker.isChromiumLinkerLibrary(library)) { | 257 if (!ok) { |
258 continue; | 258 Log.w(TAG, "Forking a process to prefetch the native library
failed."); |
| 259 // AsyncTasks are executed in a low priority background |
| 260 // thread, which is the desired behavior to avoid |
| 261 // interfering with the rest of the initialization. |
| 262 for (String library : NativeLibraries.LIBRARIES) { |
| 263 if (Linker.isChromiumLinkerLibrary(library)) { |
| 264 continue; |
| 265 } |
| 266 prefetchLibraryToMemory(context, library); |
259 } | 267 } |
260 prefetchLibraryToMemory(context, library); | |
261 } | 268 } |
| 269 TraceEvent.end("LibraryLoader.asyncPrefetchLibrariesToMemory"); |
262 return null; | 270 return null; |
263 } | 271 } |
264 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | 272 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
265 } | 273 } |
266 | 274 |
267 // Invoke System.loadLibrary(...), triggering JNI_OnLoad in native code | 275 // Invoke System.loadLibrary(...), triggering JNI_OnLoad in native code |
268 private void loadAlreadyLocked( | 276 private void loadAlreadyLocked( |
269 Context context, boolean shouldDeleteFallbackLibraries) | 277 Context context, boolean shouldDeleteFallbackLibraries) |
270 throws ProcessInitException { | 278 throws ProcessInitException { |
271 try { | 279 try { |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 // Method called to register (for later recording) statistics about the Chro
mium linker | 574 // Method called to register (for later recording) statistics about the Chro
mium linker |
567 // operation for a renderer process. Indicates whether the linker attempted
relro sharing, | 575 // operation for a renderer process. Indicates whether the linker attempted
relro sharing, |
568 // and if it did, whether the library failed to load at a fixed address. | 576 // and if it did, whether the library failed to load at a fixed address. |
569 private native void nativeRegisterChromiumAndroidLinkerRendererHistogram( | 577 private native void nativeRegisterChromiumAndroidLinkerRendererHistogram( |
570 boolean requestedSharedRelro, | 578 boolean requestedSharedRelro, |
571 boolean loadAtFixedAddressFailed); | 579 boolean loadAtFixedAddressFailed); |
572 | 580 |
573 // Get the version of the native library. This is needed so that we can chec
k we | 581 // Get the version of the native library. This is needed so that we can chec
k we |
574 // have the right version before initializing the (rest of the) JNI. | 582 // have the right version before initializing the (rest of the) JNI. |
575 private native String nativeGetVersionNumber(); | 583 private native String nativeGetVersionNumber(); |
| 584 |
| 585 // Finds the ranges corresponding to the native library pages, forks a new |
| 586 // process to prefetch these pages and waits for it. The new process then |
| 587 // terminates. This is blocking. |
| 588 private static native boolean nativeForkAndPrefetchNativeLibrary(); |
576 } | 589 } |
OLD | NEW |