1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.os.logging; 18 19 import android.app.Application; 20 import android.os.Process; 21 import android.util.Log; 22 import android.view.WindowManager.LayoutParams; 23 24 import com.android.internal.os.ProcfsMemoryUtil; 25 import com.android.internal.util.FrameworkStatsLog; 26 import java.util.Collection; 27 import libcore.util.NativeAllocationRegistry; 28 29 /** 30 * Used to wrap different logging calls in one, so that client side code base is clean and more 31 * readable. 32 */ 33 public class MetricsLoggerWrapper { 34 logAppOverlayEnter(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow)35 public static void logAppOverlayEnter(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow) { 36 if (changed) { 37 if (type != LayoutParams.TYPE_APPLICATION_OVERLAY) { 38 FrameworkStatsLog.write(FrameworkStatsLog.OVERLAY_STATE_CHANGED, uid, packageName, 39 true, FrameworkStatsLog.OVERLAY_STATE_CHANGED__STATE__ENTERED); 40 } else if (!usingAlertWindow){ 41 FrameworkStatsLog.write(FrameworkStatsLog.OVERLAY_STATE_CHANGED, uid, packageName, 42 false, FrameworkStatsLog.OVERLAY_STATE_CHANGED__STATE__ENTERED); 43 } 44 } 45 } 46 logAppOverlayExit(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow)47 public static void logAppOverlayExit(int uid, String packageName, boolean changed, int type, boolean usingAlertWindow) { 48 if (changed) { 49 if (type != LayoutParams.TYPE_APPLICATION_OVERLAY) { 50 FrameworkStatsLog.write(FrameworkStatsLog.OVERLAY_STATE_CHANGED, uid, packageName, 51 true, FrameworkStatsLog.OVERLAY_STATE_CHANGED__STATE__EXITED); 52 } else if (!usingAlertWindow){ 53 FrameworkStatsLog.write(FrameworkStatsLog.OVERLAY_STATE_CHANGED, uid, packageName, 54 false, FrameworkStatsLog.OVERLAY_STATE_CHANGED__STATE__EXITED); 55 } 56 } 57 } 58 logPostGcMemorySnapshot()59 public static void logPostGcMemorySnapshot() { 60 if (!com.android.libcore.readonly.Flags.nativeMetrics()) { 61 return; 62 } 63 int pid = Process.myPid(); 64 String processName = Process.myProcessName(); 65 Collection<NativeAllocationRegistry.Metrics> metrics = 66 NativeAllocationRegistry.getMetrics(); 67 int nMetrics = metrics.size(); 68 69 String[] classNames = new String[nMetrics]; 70 long[] mallocedCount = new long[nMetrics]; 71 long[] mallocedBytes = new long[nMetrics]; 72 long[] nonmallocedCount = new long[nMetrics]; 73 long[] nonmallocedBytes = new long[nMetrics]; 74 75 int i = 0; 76 for (NativeAllocationRegistry.Metrics m : metrics) { 77 classNames[i] = m.getClassName(); 78 mallocedCount[i] = m.getMallocedCount(); 79 mallocedBytes[i] = m.getMallocedBytes(); 80 nonmallocedCount[i] = m.getNonmallocedCount(); 81 nonmallocedBytes[i] = m.getNonmallocedBytes(); 82 i++; 83 } 84 85 ProcfsMemoryUtil.MemorySnapshot m = ProcfsMemoryUtil.readMemorySnapshotFromProcfs(); 86 int oom_score_adj = ProcfsMemoryUtil.readOomScoreAdjFromProcfs(); 87 FrameworkStatsLog.write(FrameworkStatsLog.POSTGC_MEMORY_SNAPSHOT, 88 m.uid, processName, pid, 89 oom_score_adj, 90 m.rssInKilobytes, 91 m.anonRssInKilobytes, 92 m.swapInKilobytes, 93 m.anonRssInKilobytes + m.swapInKilobytes, 94 classNames, 95 mallocedCount, 96 mallocedBytes, 97 nonmallocedCount, 98 nonmallocedBytes); 99 } 100 } 101