library_prefetcher.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef BASE_ANDROID_LIBRARY_LOADER_LIBRARY_PREFETCHER_H_
  5. #define BASE_ANDROID_LIBRARY_LOADER_LIBRARY_PREFETCHER_H_
  6. #include <jni.h>
  7. #include <stdint.h>
  8. #include "base/android/library_loader/anchor_functions_buildflags.h"
  9. #include "base/base_export.h"
  10. #include "base/gtest_prod_util.h"
  11. #if BUILDFLAG(SUPPORTS_CODE_ORDERING)
  12. namespace base {
  13. namespace android {
  14. // Forks and waits for a process prefetching the native library. This is done in
  15. // a forked process for the following reasons:
  16. // - Isolating the main process from mistakes in getting the address range, only
  17. // crashing the forked process in case of mistake.
  18. // - Not inflating the memory used by the main process uselessly, which could
  19. // increase its likelihood to be killed.
  20. // The forked process has background priority and, since it is not declared to
  21. // the Android runtime, can be killed at any time, which is not an issue here.
  22. class BASE_EXPORT NativeLibraryPrefetcher {
  23. public:
  24. NativeLibraryPrefetcher() = delete;
  25. NativeLibraryPrefetcher(const NativeLibraryPrefetcher&) = delete;
  26. NativeLibraryPrefetcher& operator=(const NativeLibraryPrefetcher&) = delete;
  27. // Finds the executable code range, forks a low priority process pre-fetching
  28. // it wait()s for the process to exit or die. If ordered_only is true, only
  29. // the ordered section is prefetched. See GetOrdrderedTextRange() in
  30. // library_prefetcher.cc.
  31. static void ForkAndPrefetchNativeLibrary(bool ordered_only);
  32. // Returns the percentage of the native library code currently resident in
  33. // memory, or -1 in case of error.
  34. static int PercentageOfResidentNativeLibraryCode();
  35. // Collects residency for the native library executable multiple times, then
  36. // dumps it to disk.
  37. static void PeriodicallyCollectResidency();
  38. // Calls madvise() on the native library executable, using orderfile
  39. // information to decide how to advise each part of the library.
  40. static void MadviseForOrderfile();
  41. // Calls madvise() on the native library executable so that residency
  42. // collection is accurate.
  43. static void MadviseForResidencyCollection();
  44. private:
  45. // Returns the percentage of [start, end] currently resident in
  46. // memory, or -1 in case of error.
  47. static int PercentageOfResidentCode(size_t start, size_t end);
  48. FRIEND_TEST_ALL_PREFIXES(NativeLibraryPrefetcherTest,
  49. TestPercentageOfResidentCode);
  50. };
  51. } // namespace android
  52. } // namespace base
  53. #endif // BUILDFLAG(SUPPORTS_CODE_ORDERING)
  54. #endif // BASE_ANDROID_LIBRARY_LOADER_LIBRARY_PREFETCHER_H_