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