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