Android applyOomAdjLocked

applyOomAdjLocked 概览

applyOomAdjLocked

applyOomAdjLocked 分析

private final boolean applyOomAdjLocked(ProcessRecord app, boolean doingAll, long now,
        long nowElapsed) {
    boolean success = true;

    if (app.curRawAdj != app.setRawAdj) {
        String seempStr = "app_uid=" + app.uid
            + ",app_pid=" + app.pid + ",oom_adj=" + app.curAdj
            + ",setAdj=" + app.setAdj + ",hasShownUi=" + (app.hasShownUi ? 1 : 0)
            + ",cached=" + (app.cached ? 1 : 0)
            + ",fA=" + (app.foregroundActivities ? 1 : 0)
            + ",fS=" + (app.foregroundServices ? 1 : 0)
            + ",systemNoUi=" + (app.systemNoUi ? 1 : 0)
            + ",curSchedGroup=" + app.curSchedGroup
            + ",curProcState=" + app.curProcState + ",setProcState=" + app.setProcState
            + ",killed=" + (app.killed ? 1 : 0) + ",killedByAm=" + (app.killedByAm ? 1 : 0)
            + ",debugging=" + (app.debugging ? 1 : 0);
        android.util.SeempLog.record_str(385, seempStr);
        app.setRawAdj = app.curRawAdj;
    }

    int changes = 0;

    if (app.curAdj != app.setAdj) {
        ProcessList.setOomAdj(app.pid, app.info.uid, app.curAdj); //通过socket通知lmkd
        if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG_OOM_ADJ,
                "Set " + app.pid + " " + app.processName + " adj " + app.curAdj + ": "
                + app.adjType);
        app.setAdj = app.curAdj;
        app.verifiedAdj = ProcessList.INVALID_ADJ;
    }

    if (app.setSchedGroup != app.curSchedGroup) {
        int oldSchedGroup = app.setSchedGroup;
        app.setSchedGroup = app.curSchedGroup;
        if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG_OOM_ADJ,
                "Setting sched group of " + app.processName
                + " to " + app.curSchedGroup);
        if (app.waitingToKill != null && app.curReceiver == null
                && app.setSchedGroup == ProcessList.SCHED_GROUP_BACKGROUND) {
            app.kill(app.waitingToKill, true);
            success = false;
        } else {
            int processGroup;
            switch (app.curSchedGroup) {
                case ProcessList.SCHED_GROUP_BACKGROUND:
                    processGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
                    break;
                case ProcessList.SCHED_GROUP_TOP_APP:
                case ProcessList.SCHED_GROUP_TOP_APP_BOUND:
                    processGroup = Process.THREAD_GROUP_TOP_APP;
                    break;
                default:
                    processGroup = Process.THREAD_GROUP_DEFAULT;
                    break;
            }
            long oldId = Binder.clearCallingIdentity();
            try {
                Process.setProcessGroup(app.pid, processGroup);
                if (app.curSchedGroup == ProcessList.SCHED_GROUP_TOP_APP) {
                    // do nothing if we already switched to RT
                    if (oldSchedGroup != ProcessList.SCHED_GROUP_TOP_APP) {
                        // Switch VR thread for app to SCHED_FIFO
                        if (mInVrMode && app.vrThreadTid != 0) {
                            try {
                                Process.setThreadScheduler(app.vrThreadTid,
                                    Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
                            } catch (IllegalArgumentException e) {
                                // thread died, ignore
                            }
                        }
                        if (mUseFifoUiScheduling) {
                            // Switch UI pipeline for app to SCHED_FIFO
                            app.savedPriority = Process.getThreadPriority(app.pid);
                            try {
                                Process.setThreadScheduler(app.pid,
                                    Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
                            } catch (IllegalArgumentException e) {
                                // thread died, ignore
                            }
                            if (app.renderThreadTid != 0) {
                                try {
                                    Process.setThreadScheduler(app.renderThreadTid,
                                        Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
                                } catch (IllegalArgumentException e) {
                                    // thread died, ignore
                                }
                                if (DEBUG_OOM_ADJ) {
                                    Slog.d("UI_FIFO", "Set RenderThread (TID " +
                                        app.renderThreadTid + ") to FIFO");
                                }
                            } else {
                                if (DEBUG_OOM_ADJ) {
                                    Slog.d("UI_FIFO", "Not setting RenderThread TID");
                                }
                            }
                        } else {
                            // Boost priority for top app UI and render threads
                            Process.setThreadPriority(app.pid, -10);
                            if (app.renderThreadTid != 0) {
                                try {
                                    Process.setThreadPriority(app.renderThreadTid, -10);
                                } catch (IllegalArgumentException e) {
                                    // thread died, ignore
                                }
                            }
                        }
                    }
                } else if (oldSchedGroup == ProcessList.SCHED_GROUP_TOP_APP &&
                           app.curSchedGroup != ProcessList.SCHED_GROUP_TOP_APP) {
                    // Reset VR thread to SCHED_OTHER
                    // Safe to do even if we're not in VR mode
                    if (app.vrThreadTid != 0) {
                        Process.setThreadScheduler(app.vrThreadTid, Process.SCHED_OTHER, 0);
                    }
                    if (mUseFifoUiScheduling) {
                        // Reset UI pipeline to SCHED_OTHER
                        Process.setThreadScheduler(app.pid, Process.SCHED_OTHER, 0);
                        Process.setThreadPriority(app.pid, app.savedPriority);
                        if (app.renderThreadTid != 0) {
                            Process.setThreadScheduler(app.renderThreadTid,
                                Process.SCHED_OTHER, 0);
                            Process.setThreadPriority(app.renderThreadTid, -4);
                        }
                    } else {
                        // Reset priority for top app UI and render threads
                        Process.setThreadPriority(app.pid, 0);
                        if (app.renderThreadTid != 0) {
                            Process.setThreadPriority(app.renderThreadTid, 0);
                        }
                    }
                }
            } catch (Exception e) {
                Slog.w(TAG, "Failed setting process group of " + app.pid
                        + " to " + app.curSchedGroup);
                e.printStackTrace();
            } finally {
                Binder.restoreCallingIdentity(oldId);
            }
        }
    }
    if (app.repForegroundActivities != app.foregroundActivities) {
        app.repForegroundActivities = app.foregroundActivities;
        changes |= ProcessChangeItem.CHANGE_ACTIVITIES;
    }
    if (app.repProcState != app.curProcState) {
        app.repProcState = app.curProcState;
        changes |= ProcessChangeItem.CHANGE_PROCESS_STATE;
        if (app.thread != null) {
            try {
                if (false) {
                    //RuntimeException h = new RuntimeException("here");
                    Slog.i(TAG, "Sending new process state " + app.repProcState
                            + " to " + app /*, h*/);
                }
                app.thread.setProcessState(app.repProcState);
            } catch (RemoteException e) {
            }
        }
    }
    if (app.setProcState == ActivityManager.PROCESS_STATE_NONEXISTENT
            || ProcessList.procStatesDifferForMem(app.curProcState, app.setProcState)) {
        if (false && mTestPssMode && app.setProcState >= 0 && app.lastStateTime <= (now-200)) {
            // Experimental code to more aggressively collect pss while
            // running test...  the problem is that this tends to collect
            // the data right when a process is transitioning between process
            // states, which well tend to give noisy data.
            long start = SystemClock.uptimeMillis();
            long pss = Debug.getPss(app.pid, mTmpLong, null);
            recordPssSampleLocked(app, app.curProcState, pss, mTmpLong[0], mTmpLong[1], now);
            mPendingPssProcesses.remove(app);
            Slog.i(TAG, "Recorded pss for " + app + " state " + app.setProcState
                    + " to " + app.curProcState + ": "
                    + (SystemClock.uptimeMillis()-start) + "ms");
        }
        app.lastStateTime = now;
        app.nextPssTime = ProcessList.computeNextPssTime(app.curProcState, true,
                mTestPssMode, isSleepingLocked(), now);
        if (DEBUG_PSS) Slog.d(TAG_PSS, "Process state change from "
                + ProcessList.makeProcStateString(app.setProcState) + " to "
                + ProcessList.makeProcStateString(app.curProcState) + " next pss in "
                + (app.nextPssTime-now) + ": " + app);
    } else {
        if (now > app.nextPssTime || (now > (app.lastPssTime+ProcessList.PSS_MAX_INTERVAL)
                && now > (app.lastStateTime+ProcessList.minTimeFromStateChange(
                mTestPssMode)))) {
            requestPssLocked(app, app.setProcState);
            app.nextPssTime = ProcessList.computeNextPssTime(app.curProcState, false,
                    mTestPssMode, isSleepingLocked(), now);
        } else if (false && DEBUG_PSS) Slog.d(TAG_PSS,
                "Not requesting PSS of " + app + ": next=" + (app.nextPssTime-now));
    }
    if (app.setProcState != app.curProcState) {
        if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG_OOM_ADJ,
                "Proc state change of " + app.processName
                        + " to " + app.curProcState);
        boolean setImportant = app.setProcState < ActivityManager.PROCESS_STATE_SERVICE;
        boolean curImportant = app.curProcState < ActivityManager.PROCESS_STATE_SERVICE;
        if (setImportant && !curImportant) {
            // This app is no longer something we consider important enough to allow to
            // use arbitrary amounts of battery power.  Note
            // its current wake lock time to later know to kill it if
            // it is not behaving well.
            BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
            synchronized (stats) {
                app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
                        app.pid, nowElapsed);
            }
            app.lastCpuTime = app.curCpuTime;

        }
        // Inform UsageStats of important process state change
        // Must be called before updating setProcState
        maybeUpdateUsageStatsLocked(app, nowElapsed);

        app.setProcState = app.curProcState;
        if (app.setProcState >= ActivityManager.PROCESS_STATE_HOME) {
            app.notCachedSinceIdle = false;
        }
        if (!doingAll) {
            setProcessTrackerStateLocked(app, mProcessStats.getMemFactorLocked(), now);
        } else {
            app.procStateChanged = true;
        }
    } else if (app.reportedInteraction && (nowElapsed-app.interactionEventTime)
            > USAGE_STATS_INTERACTION_INTERVAL) {
        // For apps that sit around for a long time in the interactive state, we need
        // to report this at least once a day so they don't go idle.
        maybeUpdateUsageStatsLocked(app, nowElapsed);
    }

    if (changes != 0) {
        if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG_PROCESS_OBSERVERS,
                "Changes in " + app + ": " + changes);
        int i = mPendingProcessChanges.size()-1;
        ProcessChangeItem item = null;
        while (i >= 0) {
            item = mPendingProcessChanges.get(i);
            if (item.pid == app.pid) {
                if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG_PROCESS_OBSERVERS,
                        "Re-using existing item: " + item);
                break;
            }
            i--;
        }
        if (i < 0) {
            // No existing item in pending changes; need a new one.
            final int NA = mAvailProcessChanges.size();
            if (NA > 0) {
                item = mAvailProcessChanges.remove(NA-1);
                if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG_PROCESS_OBSERVERS,
                        "Retrieving available item: " + item);
            } else {
                item = new ProcessChangeItem();
                if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG_PROCESS_OBSERVERS,
                        "Allocating new item: " + item);
            }
            item.changes = 0;
            item.pid = app.pid;
            item.uid = app.info.uid;
            if (mPendingProcessChanges.size() == 0) {
                if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG_PROCESS_OBSERVERS,
                        "*** Enqueueing dispatch processes changed!");
                mUiHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED_UI_MSG).sendToTarget();
            }
            mPendingProcessChanges.add(item);
        }
        item.changes |= changes;
        item.processState = app.repProcState;
        item.foregroundActivities = app.repForegroundActivities;
        if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG_PROCESS_OBSERVERS,
                "Item " + Integer.toHexString(System.identityHashCode(item))
                + " " + app.toShortString() + ": changes=" + item.changes
                + " procState=" + item.processState
                + " foreground=" + item.foregroundActivities
                + " type=" + app.adjType + " source=" + app.adjSource
                + " target=" + app.adjTarget);
    }

    return success;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 210,914评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 89,935评论 2 383
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,531评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,309评论 1 282
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,381评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,730评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,882评论 3 404
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,643评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,095评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,448评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,566评论 1 339
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,253评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,829评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,715评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,945评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,248评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,440评论 2 348

推荐阅读更多精彩内容