xref: /aosp_15_r20/external/cronet/base/android/sys_utils.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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/sys_utils.h"
6 
7 #include <memory>
8 
9 #include "base/android/build_info.h"
10 #include "base/base_jni/SysUtils_jni.h"
11 #include "base/process/process_metrics.h"
12 #include "base/system/sys_info.h"
13 #include "base/trace_event/base_tracing.h"
14 
15 namespace base {
16 namespace android {
17 
IsLowEndDeviceFromJni()18 bool SysUtils::IsLowEndDeviceFromJni() {
19   JNIEnv* env = AttachCurrentThread();
20   return Java_SysUtils_isLowEndDevice(env);
21 }
22 
IsCurrentlyLowMemory()23 bool SysUtils::IsCurrentlyLowMemory() {
24   JNIEnv* env = AttachCurrentThread();
25   return Java_SysUtils_isCurrentlyLowMemory(env);
26 }
27 
28 // static
AmountOfPhysicalMemoryKB()29 int SysUtils::AmountOfPhysicalMemoryKB() {
30   JNIEnv* env = AttachCurrentThread();
31   return Java_SysUtils_amountOfPhysicalMemoryKB(env);
32 }
33 
34 // Logs the number of minor / major page faults to tracing (and also the time to
35 // collect) the metrics. Does nothing if tracing is not enabled.
JNI_SysUtils_LogPageFaultCountToTracing(JNIEnv * env)36 static void JNI_SysUtils_LogPageFaultCountToTracing(JNIEnv* env) {
37   // This is racy, but we are OK losing data, and collecting it is potentially
38   // expensive (reading and parsing a file).
39   bool enabled;
40   TRACE_EVENT_CATEGORY_GROUP_ENABLED("startup", &enabled);
41   if (!enabled)
42     return;
43   TRACE_EVENT_BEGIN2("memory", "CollectPageFaultCount", "minor", 0, "major", 0);
44   std::unique_ptr<base::ProcessMetrics> process_metrics(
45       base::ProcessMetrics::CreateProcessMetrics(
46           base::GetCurrentProcessHandle()));
47   base::PageFaultCounts counts;
48   process_metrics->GetPageFaultCounts(&counts);
49   TRACE_EVENT_END2("memory", "CollectPageFaults", "minor", counts.minor,
50                    "major", counts.major);
51 }
52 
53 }  // namespace android
54 
55 }  // namespace base
56