Skip to main content

ActivityManagerService: Basics

ActivityManagerService: Is a rockstar of a systemservice. 
It has multiple responsibilities:
PROCESS MANAGEMENT
ACTIVITY MANAGEMENT
PERMISSIONS
TASK MANAGEMENT
CONTENT PROVIDERS
GLOBAL MANAGEMENT
SERVICES
BACKUP AND RESTORE
BROADCASTS
INSTRUMENTATION
CONFIGURATION
LIFETIME MANAGEMENT

Source Code:
ActivityManager:
ActivityManagerNative
ActivityManagerService
Other files in /frameworks/base/services/core/java/com/android/server/am/

Creation:
Starts from SystemServer.
        LifeCycle class which extends SystemService is used to star and initialize          ActivityManagerService..We do a Lifecycle.startService to start ActivityManagerService.

AMS Tasks in systemserver (initialization):
mActivityManagerService = mSystemServiceManager.startService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mActivityManagerService.initPowerManagement();
mActivityManagerService.setSystemProcess();
mActivityManagerService.setUsageStatsManager(
mActivityManagerService.installSystemProviders();
mActivityManagerService.setWindowManager(wm);
mActivityManagerService.systemReady(new Runnable() 
          mActivityManagerService.startObservingNativeCrashes();


Watchdog: 
From frameworks/base/services/core/java/com/android/server/Watchdog.java
This is a watchdog implementation to monitor system critical threads in systemserver process.
If any of the threads is stuck for more than 30 secs it dumps the stack traces of all threads to traces.txt.
After 1 min if the thread is still stuck, it dumps the trace again and kills the system server process
In logcat logs you would see the following Message
    *** WATCHDOG KILLING SYSTEM PROCESS:
Watchdog class extends Thread
  
ServiceThreads
ActivityManager has the following special threads.Service threads extend handler threads. 
"android.bg" : - Shared singleton background thread for each process.
"android.fg" :-  Shared singleton foreground thread for the system.  This is a thread for regular  foreground service operations, which shouldn't be blocked by anything running in  the background. 
"android.ui" :-  Shared singleton thread for showing UI.  This is a foreground thread, and in  additional should not have operations that can take more than a few ms scheduled  on it to avoid UI jank.
"android.io" :- Shared singleton I/O thread for the system.  This is a thread for non-background  service operations that can potential block briefly on network IO operations
    "android.display" :- Shared singleton foreground thread for the system.  This is a thread for  operations that affect what's on the display, which needs to have a minimum of latency.  This thread should pretty much only be used by the WindowManager, DisplayManager, and InputManager to perform quick operations in real time.
"activitymanager" -- main handler of ActivityManager.

ActivityManager defines the following values:

    PROC_START_TIMEOUT (10 secs) : How long we wait for a launched process to attach to the activity manager before we decide it's never going to come up for real.
    GC_TIMEOUT ( 5 secs): How long to wait after going idle before forcing apps to GC.
    GC_MIN_INTERVAL ( 60 secs): The minimum amount of time between successive GC requests for a process.
    FULL_PSS_MIN_INTERVAL (10 mins):The minimum amount of time between successive PSS requests for a process.
    FULL_PSS_LOWERED_INTERVAL (2 mins):The minimum amount of time between successive PSS requests for a process when the request is due to the memory state being lowered.
    How long we allow a receiver to run before giving up on it.
        BROADCAST_FG_TIMEOUT = 10 secs
        BROADCAST_BG_TIMEOUT = 60 secs
    How long we wait until we timeout on key dispatching.
        KEY_DISPATCHING_TIMEOUT = 5 secs (during instrumentation 60secs)
    Amount of time we wait for observers to handle a user switch before giving up on them and unfreezing the screen.
        USER_SWITCH_TIMEOUT = 2 secs
    Maximum number of users we allow to be running at a time.
        MAX_RUNNING_USERS  4
    How long to wait in getAssistContextExtras for the activity and foreground services to respond with the result.
        PENDING_ASSIST_EXTRAS_TIMEOUT 0.5 secs
    Maximum number of persisted Uri grants a package is allowed
        MAX_PERSISTED_URI_GRANTS   128

Flags::
    mShowDialogs: flag to indicate if systems dialogs ANR crash etc. to be shown or not.

Major Players:
ActivityStack: State and management of a single stack of activities.
ActivityStackSupevisor: Supervises complete back stack

Record Holder Classes
ActivityRecords Contains Information per Activity
        dumpsys activity processes(ActivityRecord.Java)

ProcessRecord Contains Information per Process
dumpsys activity processes (ProcessRecord.java)

TaskRecord: (taskRecord.java)
A task record is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack (the "back stack"), in the order in which each activity is opened. When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. 

Activity Attributes to manage task behavior:

taskAffinity: 
launchMode: defines how a new instance of an activity is associated with the current task. You can define different launch modes in two ways:
Using the manifest file : When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts. (standard, single top, single task and single instance).
Using Intent flags : When you call startActivity(), you can include a flag in the Intent that declares how (or whether) the new activity should associate with the current task.(FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP)

allowTaskReparenting
• clearTaskOnLaunch
• alwaysRetainTaskState
• finishOnTaskLaunch

ProcessList.java:
Activity manager code dealing with processes.

The minimum time we allow between crashes, for us to consider this application to be bad and stop and its services and reject broadcasts.
 MIN_CRASH_INTERVAL = 60 secs

OOM adjustments for processes in various states:
This is a process only hosting activities that are not visible, so it can be killed without any disruption.
    CACHED_APP_MAX_ADJ = 15;
    CACHED_APP_MIN_ADJ = 9;

    The B list of SERVICE_ADJ -- these are the old and decrepit services that aren't as shiny and interesting as the ones in the A list.
    SERVICE_B_ADJ = 8;

    This is the process of the previous application that the user was in. This process is kept above other things, because it is very common to
    switch back to the previous app.  This is important both for recent task switch (toggling between the two top recent apps) as well as normal
    UI flow such as clicking on a URI in the e-mail app to view in the browser, and then pressing back to return to e-mail.
    PREVIOUS_APP_ADJ = 7;

    This is a process holding the home application -- we want to try avoiding killing it, even if it would normally be in the background,
    because the user interacts with it so much.
    HOME_APP_ADJ = 6;

    This is a process holding an application service -- killing it will not have much of an impact as far as the user is concerned.
    SERVICE_ADJ = 5;

    This is a process with a heavy-weight application.  It is in the background, but we want to try to avoid killing it.
    HEAVY_WEIGHT_APP_ADJ = 4;

    This is a process currently hosting a backup operation.  Killing it is not entirely fatal but is generally a bad idea.
    BACKUP_APP_ADJ = 3;

    This is a process only hosting components that are perceptible to the user, and we really want to avoid killing them, but they are not
    immediately visible. An example is background music playback.
    PERCEPTIBLE_APP_ADJ = 2;

    This is a process only hosting activities that are visible to the user, so we'd prefer they don't disappear.
    VISIBLE_APP_ADJ = 1;

    This is the process running the current foreground app.  We'd really rather not kill it!
    FOREGROUND_APP_ADJ = 0;

The minimum number of cached apps we want to be able to keep around, without empty apps being able to push them out of memory.
     MIN_CACHED_APPS = 2;

The maximum number of cached processes we will keep around before killing them.
    MAX_CACHED_APPS = 32;

    We allow empty processes to stick around for at most 30 minutes.
    MAX_EMPTY_TIME = 30 mins

    The maximum number of empty app processes we will let sit around.
    MAX_EMPTY_APPS = 16 (half of max cached apps)

    The number of empty apps at which we don't consider it necessary to do memory trimming.
    TRIM_EMPTY_APPS = 8

    The number of cached at which we don't consider it necessary to do memory trimming.
    TRIM_CACHED_APPS = 5

    Threshold of number of cached+empty where we consider memory critical.
    TRIM_CRITICAL_THRESHOLD = 3;

    Threshold of number of cached+empty where we consider memory critical.
    TRIM_LOW_THRESHOLD = 5;

Comments