1 /* 2 * Copyright (C) 2024 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 package com.android.server.am; 17 18 import android.app.StackTrace; 19 import android.util.Slog; 20 21 /** 22 * Helper for writing debug log about proc/uid state changes. 23 */ 24 class OomAdjusterDebugLogger { 25 // Use the "am_" tag to make it similar to event logs. 26 private static final String STACK_TRACE_TAG = "am_stack"; 27 28 private final OomAdjuster mOomAdjuster; 29 private final ActivityManagerConstants mConstants; 30 31 private static final int MISC_SCHEDULE_IDLE_UIDS_MSG_1 = 1; 32 private static final int MISC_SCHEDULE_IDLE_UIDS_MSG_2 = 2; 33 private static final int MISC_SCHEDULE_IDLE_UIDS_MSG_3 = 3; 34 35 private static final int MISC_SET_LAST_BG_TIME = 10; 36 private static final int MISC_CLEAR_LAST_BG_TIME = 11; 37 OomAdjusterDebugLogger(OomAdjuster oomAdjuster, ActivityManagerConstants constants)38 OomAdjusterDebugLogger(OomAdjuster oomAdjuster, ActivityManagerConstants constants) { 39 mOomAdjuster = oomAdjuster; 40 mConstants = constants; 41 } 42 shouldLog(int uid)43 boolean shouldLog(int uid) { 44 return mConstants.shouldDebugUidForProcState(uid); 45 } 46 maybeLogStacktrace(String msg)47 private void maybeLogStacktrace(String msg) { 48 if (!mConstants.mEnableProcStateStacktrace) { 49 return; 50 } 51 Slog.i(STACK_TRACE_TAG, 52 msg + ": " + OomAdjuster.oomAdjReasonToString(mOomAdjuster.mLastReason), 53 new StackTrace("Called here")); 54 } 55 maybeSleep(int millis)56 private void maybeSleep(int millis) { 57 if (millis == 0) { 58 return; 59 } 60 try { 61 Thread.sleep(millis); 62 } catch (InterruptedException e) { 63 } 64 } 65 logUidStateChanged(int uid, int uidstate, int olduidstate, int capability, int oldcapability, int flags)66 void logUidStateChanged(int uid, int uidstate, int olduidstate, 67 int capability, int oldcapability, int flags) { 68 EventLogTags.writeAmUidStateChanged( 69 uid, mOomAdjuster.mAdjSeq, uidstate, olduidstate, capability, oldcapability, flags, 70 OomAdjuster.oomAdjReasonToString(mOomAdjuster.mLastReason)); 71 maybeLogStacktrace("uidStateChanged"); 72 maybeSleep(mConstants.mProcStateDebugSetUidStateDelay); 73 } 74 logProcStateChanged(int uid, int pid, int procstate, int oldprocstate, int oomadj, int oldoomadj)75 void logProcStateChanged(int uid, int pid, int procstate, int oldprocstate, 76 int oomadj, int oldoomadj) { 77 EventLogTags.writeAmProcStateChanged( 78 uid, pid, mOomAdjuster.mAdjSeq, procstate, oldprocstate, oomadj, oldoomadj, 79 OomAdjuster.oomAdjReasonToString(mOomAdjuster.mLastReason)); 80 maybeLogStacktrace("procStateChanged"); 81 maybeSleep(mConstants.mProcStateDebugSetProcStateDelay); 82 } 83 logScheduleUidIdle1(int uid, long delay)84 void logScheduleUidIdle1(int uid, long delay) { 85 EventLogTags.writeAmOomAdjMisc(MISC_SCHEDULE_IDLE_UIDS_MSG_1, 86 uid, 0, mOomAdjuster.mAdjSeq, (int) delay, 0, ""); 87 } 88 logScheduleUidIdle2(int uid, int pid, long delay)89 void logScheduleUidIdle2(int uid, int pid, long delay) { 90 EventLogTags.writeAmOomAdjMisc(MISC_SCHEDULE_IDLE_UIDS_MSG_2, 91 uid, pid, mOomAdjuster.mAdjSeq, (int) delay, 0, ""); 92 } 93 logScheduleUidIdle3(long delay)94 void logScheduleUidIdle3(long delay) { 95 EventLogTags.writeAmOomAdjMisc(MISC_SCHEDULE_IDLE_UIDS_MSG_3, 96 0, 0, mOomAdjuster.mAdjSeq, (int) delay, 0, ""); 97 } 98 logSetLastBackgroundTime(int uid, long time)99 void logSetLastBackgroundTime(int uid, long time) { 100 EventLogTags.writeAmOomAdjMisc(MISC_SET_LAST_BG_TIME, 101 uid, 0, mOomAdjuster.mAdjSeq, (int) time, 0, 102 OomAdjuster.oomAdjReasonToString(mOomAdjuster.mLastReason)); 103 } 104 logClearLastBackgroundTime(int uid)105 void logClearLastBackgroundTime(int uid) { 106 EventLogTags.writeAmOomAdjMisc(MISC_CLEAR_LAST_BG_TIME, 107 uid, 0, mOomAdjuster.mAdjSeq, 0, 0, 108 OomAdjuster.oomAdjReasonToString(mOomAdjuster.mLastReason)); 109 } 110 } 111