| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "content/browser/memory/memory_monitor_android.h" | 5 #include "content/browser/memory/memory_monitor_android.h" |
| 6 | 6 |
| 7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "content/browser/memory/memory_coordinator.h" |
| 10 #include "jni/MemoryMonitorAndroid_jni.h" | 12 #include "jni/MemoryMonitorAndroid_jni.h" |
| 11 | 13 |
| 12 namespace content { | 14 namespace content { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 17 |
| 15 const size_t kMBShift = 20; | 18 const size_t kMBShift = 20; |
| 19 |
| 20 void RegisterComponentCallbacks() { |
| 21 Java_MemoryMonitorAndroid_registerComponentCallbacks( |
| 22 base::android::AttachCurrentThread(), |
| 23 base::android::GetApplicationContext()); |
| 24 } |
| 25 |
| 16 } | 26 } |
| 17 | 27 |
| 18 // An implementation of MemoryMonitorAndroid::Delegate using the Android APIs. | 28 // An implementation of MemoryMonitorAndroid::Delegate using the Android APIs. |
| 19 class MemoryMonitorAndroidDelegateImpl : public MemoryMonitorAndroid::Delegate { | 29 class MemoryMonitorAndroidDelegateImpl : public MemoryMonitorAndroid::Delegate { |
| 20 public: | 30 public: |
| 21 MemoryMonitorAndroidDelegateImpl() {} | 31 MemoryMonitorAndroidDelegateImpl() {} |
| 22 ~MemoryMonitorAndroidDelegateImpl() override {} | 32 ~MemoryMonitorAndroidDelegateImpl() override {} |
| 23 | 33 |
| 24 using MemoryInfo = MemoryMonitorAndroid::MemoryInfo; | 34 using MemoryInfo = MemoryMonitorAndroid::MemoryInfo; |
| 25 void GetMemoryInfo(MemoryInfo* out) override; | 35 void GetMemoryInfo(MemoryInfo* out) override; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 47 jlong out_ptr) { | 57 jlong out_ptr) { |
| 48 DCHECK(out_ptr); | 58 DCHECK(out_ptr); |
| 49 MemoryMonitorAndroid::MemoryInfo* info = | 59 MemoryMonitorAndroid::MemoryInfo* info = |
| 50 reinterpret_cast<MemoryMonitorAndroid::MemoryInfo*>(out_ptr); | 60 reinterpret_cast<MemoryMonitorAndroid::MemoryInfo*>(out_ptr); |
| 51 info->avail_mem = avail_mem; | 61 info->avail_mem = avail_mem; |
| 52 info->low_memory = low_memory; | 62 info->low_memory = low_memory; |
| 53 info->threshold = threshold; | 63 info->threshold = threshold; |
| 54 info->total_mem = total_mem; | 64 info->total_mem = total_mem; |
| 55 } | 65 } |
| 56 | 66 |
| 67 // The maximum level of onTrimMemory (TRIM_MEMORY_COMPLETE). |
| 68 const int kTrimMemoryLevelMax = 0x80; |
| 69 |
| 70 // Called by JNI. |
| 71 static void OnTrimMemory(JNIEnv* env, |
| 72 const base::android::JavaParamRef<jclass>& jcaller, |
| 73 jint level) { |
| 74 DCHECK(level >= 0 && level <= kTrimMemoryLevelMax); |
| 75 auto state = MemoryCoordinator::GetInstance()->GetCurrentMemoryState(); |
| 76 switch (state) { |
| 77 case base::MemoryState::NORMAL: |
| 78 UMA_HISTOGRAM_ENUMERATION("Memory.Coordinator.TrimMemoryLevel.Normal", |
| 79 level, kTrimMemoryLevelMax); |
| 80 break; |
| 81 case base::MemoryState::THROTTLED: |
| 82 UMA_HISTOGRAM_ENUMERATION("Memory.Coordinator.TrimMemoryLevel.Throttled", |
| 83 level, kTrimMemoryLevelMax); |
| 84 break; |
| 85 case base::MemoryState::SUSPENDED: |
| 86 UMA_HISTOGRAM_ENUMERATION("Memory.Coordinator.TrimMemoryLevel.Suspended", |
| 87 level, kTrimMemoryLevelMax); |
| 88 break; |
| 89 case base::MemoryState::UNKNOWN: |
| 90 NOTREACHED(); |
| 91 break; |
| 92 } |
| 93 } |
| 94 |
| 57 // static | 95 // static |
| 58 std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() { | 96 std::unique_ptr<MemoryMonitorAndroid> MemoryMonitorAndroid::Create() { |
| 59 auto delegate = base::WrapUnique(new MemoryMonitorAndroidDelegateImpl); | 97 auto delegate = base::WrapUnique(new MemoryMonitorAndroidDelegateImpl); |
| 60 return base::WrapUnique(new MemoryMonitorAndroid(std::move(delegate))); | 98 return base::WrapUnique(new MemoryMonitorAndroid(std::move(delegate))); |
| 61 } | 99 } |
| 62 | 100 |
| 63 // static | 101 // static |
| 64 bool MemoryMonitorAndroid::Register(JNIEnv* env) { | 102 bool MemoryMonitorAndroid::Register(JNIEnv* env) { |
| 65 return RegisterNativesImpl(env); | 103 return RegisterNativesImpl(env); |
| 66 } | 104 } |
| 67 | 105 |
| 68 MemoryMonitorAndroid::MemoryMonitorAndroid(std::unique_ptr<Delegate> delegate) | 106 MemoryMonitorAndroid::MemoryMonitorAndroid(std::unique_ptr<Delegate> delegate) |
| 69 : delegate_(std::move(delegate)) { | 107 : delegate_(std::move(delegate)) { |
| 70 DCHECK(delegate_.get()); | 108 DCHECK(delegate_.get()); |
| 109 RegisterComponentCallbacks(); |
| 71 } | 110 } |
| 72 | 111 |
| 73 MemoryMonitorAndroid::~MemoryMonitorAndroid() {} | 112 MemoryMonitorAndroid::~MemoryMonitorAndroid() {} |
| 74 | 113 |
| 75 int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() { | 114 int MemoryMonitorAndroid::GetFreeMemoryUntilCriticalMB() { |
| 76 MemoryInfo info; | 115 MemoryInfo info; |
| 77 GetMemoryInfo(&info); | 116 GetMemoryInfo(&info); |
| 78 return (info.avail_mem - info.threshold) >> kMBShift; | 117 return (info.avail_mem - info.threshold) >> kMBShift; |
| 79 } | 118 } |
| 80 | 119 |
| 81 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { | 120 void MemoryMonitorAndroid::GetMemoryInfo(MemoryInfo* out) { |
| 82 delegate_->GetMemoryInfo(out); | 121 delegate_->GetMemoryInfo(out); |
| 83 } | 122 } |
| 84 | 123 |
| 85 // Implementation of a factory function defined in memory_monitor.h. | 124 // Implementation of a factory function defined in memory_monitor.h. |
| 86 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { | 125 std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
| 87 return MemoryMonitorAndroid::Create(); | 126 return MemoryMonitorAndroid::Create(); |
| 88 } | 127 } |
| 89 | 128 |
| 90 } // namespace content | 129 } // namespace content |
| OLD | NEW |