xref: /aosp_15_r20/external/cronet/base/android/meminfo_dump_provider.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 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 #include "base/android/meminfo_dump_provider.h"
6 #include <jni.h>
7 #include "base/android/jni_android.h"
8 #include "base/logging.h"
9 #include "base/time/time.h"
10 #include "base/trace_event/base_tracing.h"
11 
12 #if BUILDFLAG(ENABLE_BASE_TRACING)
13 #include "base/base_jni/MemoryInfoBridge_jni.h"
14 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
15 
16 namespace base::android {
17 
MeminfoDumpProvider()18 MeminfoDumpProvider::MeminfoDumpProvider() {
19 #if BUILDFLAG(ENABLE_BASE_TRACING)
20   base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
21       this, kDumpProviderName, nullptr);
22 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
23 }
24 
25 // static
Initialize()26 MeminfoDumpProvider& MeminfoDumpProvider::Initialize() {
27   static base::NoDestructor<MeminfoDumpProvider> instance;
28   return *instance.get();
29 }
30 
OnMemoryDump(const base::trace_event::MemoryDumpArgs & args,base::trace_event::ProcessMemoryDump * pmd)31 bool MeminfoDumpProvider::OnMemoryDump(
32     const base::trace_event::MemoryDumpArgs& args,
33     base::trace_event::ProcessMemoryDump* pmd) {
34 #if BUILDFLAG(ENABLE_BASE_TRACING)
35   // This is best-effort, and will be wrong if there are other callers of
36   // ActivityManager#getProcessMemoryInfo(), either in this process or from
37   // another process which is allowed to do so (typically, adb).
38   //
39   // However, since the framework doesn't document throttling in any non-vague
40   // terms and the results are not timestamped, this is the best we can do. The
41   // delay and the rest of the assumptions here come from
42   // https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android13-dev/services/core/java/com/android/server/am/ActivityManagerService.java#4093.
43   //
44   // We could always report the value on pre-Q devices, but that would skew
45   // reported data. Also, some OEMs may have cherry-picked the Q change, meaning
46   // that it's safer and more accurate to not report likely-stale data on all
47   // Android releases.
48   base::TimeTicks now = base::TimeTicks::Now();
49   bool stale_data = (now - last_collection_time_) < base::Minutes(5);
50 
51   // Background data dumps (as in the BACKGROUND level of detail, not the
52   // application being in background) should not include stale data, since it
53   // would confuse data in UMA. In particular, the background/foreground session
54   // filter would no longer be accurate.
55   if (stale_data && args.level_of_detail !=
56                         base::trace_event::MemoryDumpLevelOfDetail::kDetailed) {
57     return true;
58   }
59 
60   base::trace_event::MemoryAllocatorDump* dump =
61       pmd->CreateAllocatorDump(kDumpName);
62   // Data is either expected to be fresh, or this is a manually requested dump,
63   // and we should still report data, but note that it is stale.
64   dump->AddScalar(kIsStaleName, "bool", stale_data);
65 
66   last_collection_time_ = now;
67   JNIEnv* env = AttachCurrentThread();
68   ScopedJavaLocalRef<jobject> memory_info =
69       Java_MemoryInfoBridge_getActivityManagerMemoryInfoForSelf(env);
70   // Tell the manager that collection failed. Since this is likely not a
71   // transient failure, don't return an empty dump, and let the manager exclude
72   // this provider from the next dump.
73   if (memory_info.is_null()) {
74     LOG(WARNING) << "Got a null value";
75     return false;
76   }
77 
78   ScopedJavaLocalRef<jclass> clazz{env, env->GetObjectClass(memory_info.obj())};
79 
80   jfieldID other_private_dirty_id =
81       env->GetFieldID(clazz.obj(), "otherPrivateDirty", "I");
82   jfieldID other_pss_id = env->GetFieldID(clazz.obj(), "otherPss", "I");
83 
84   int other_private_dirty_kb =
85       env->GetIntField(memory_info.obj(), other_private_dirty_id);
86   int other_pss_kb = env->GetIntField(memory_info.obj(), other_pss_id);
87 
88   // What "other" covers is not documented in Debug#MemoryInfo, nor in
89   // ActivityManager#getProcessMemoryInfo. However, it calls
90   // Debug#getMemoryInfo(), which ends up summing all the heaps in the range
91   // [HEAP_DALVIK_OTHER, HEAP_OTHER_MEMTRACK]. See the definitions in
92   // https://android.googlesource.com/platform/frameworks/base/+/0b7c1774ba42daef7c80bf2f00fe1c0327e756ae/core/jni/android_os_Debug.cpp#60,
93   // and the code in android_os_Debug_getDirtyPagesPid() in the same file.
94   dump->AddScalar(kPrivateDirtyMetricName, "bytes",
95                   static_cast<uint64_t>(other_private_dirty_kb) * 1024);
96   dump->AddScalar(kPssMetricName, "bytes",
97                   static_cast<uint64_t>(other_pss_kb) * 1024);
98 
99   return true;
100 #else   // BUILDFLAG(ENABLE_BASE_TRACING)
101   return false;
102 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
103 }
104 
105 }  // namespace base::android
106