Add atoms to support app kill stats logging

Bug: 184859606
Bug: 154511130
Test: statsd_testdrive
Test: atest CtsStatsdAtomHostTestCases:AppExitHostTest
Change-Id: I4771dd6cd5629bfc31c2b602e7a059c69f705c99
diff --git a/stats/atoms.proto b/stats/atoms.proto
index 3951d51..1940dbf 100644
--- a/stats/atoms.proto
+++ b/stats/atoms.proto
@@ -569,6 +569,7 @@
         UserLevelHibernationStateChanged user_level_hibernation_state_changed = 370 [(module) = "framework"];
         AppSearchInitializeStatsReported app_search_initialize_stats_reported = 371 [(module) = "framework"];
         AppSearchQueryStatsReported app_search_query_stats_reported = 372 [(module) = "framework"];
+        AppProcessDied app_process_died = 373 [(module) = "framework"];
 
         // StatsdStats tracks platform atoms with ids upto 500.
         // Update StatsdStats::kMaxPushedAtomId when atom ids here approach that value.
@@ -15625,3 +15626,31 @@
     // Total number of apps in global hibernation
     optional int32 hibernated_app_count = 1;
 }
+
+/*
+ * Report the event of app process kills.
+ * Logged from:
+ *   frameworks/base/services/core/java/com/android/server/am/AppExitInfoTracker.java
+ */
+message AppProcessDied {
+    // Uid of the package of the dying process
+    optional int32 uid = 1 [(is_uid) = true];
+
+    // Name of the dying process, it'll be omitted if the process name and package name are the same
+    optional string process_name = 2;
+
+    // The reason code of why the process dies
+    optional android.app.AppExitReasonCode reason = 3;
+
+    // The supplemental reason code of why the process dies
+    optional android.app.AppExitSubReasonCode sub_reason = 4;
+
+    // The importance of the process when it dies
+    optional android.app.Importance importance = 5;
+
+    // The last known PSS (in kB) of the process before it dies, could be 0.
+    optional int32 pss = 6;
+
+    // The last known RSS (in kB) of the process before it dies, could be 0.
+    optional int32 rss = 7;
+}
diff --git a/stats/enums/app/enums.proto b/stats/enums/app/enums.proto
index cb549f2..3ac5c0b 100644
--- a/stats/enums/app/enums.proto
+++ b/stats/enums/app/enums.proto
@@ -224,3 +224,229 @@
     APP_OP_BLUETOOTH_ADVERTISE = 114;
     APP_OP_RECORD_INCOMING_PHONE_AUDIO = 115;
 }
+
+/**
+ * The reason code that why app process is killed.
+ */
+enum AppExitReasonCode {
+    /**
+     * Application process died due to unknown reason.
+     */
+    REASON_UNKNOWN = 0;
+
+    /**
+     * Application process exit normally by itself, for example,
+     * via {@link android.os.Process#exit}; {@link #status} will specify the exit code.
+     *
+     * <p>Applications should normally not do this, as the system has a better knowledge
+     * in terms of process management.</p>
+     */
+    REASON_EXIT_SELF = 1;
+
+    /**
+     * Application process died due to the result of an OS signal; for example,
+     * {@link android.os.Process#SIGNAL_KILL}; {@link #status} will specify the signum.
+     */
+    REASON_SIGNALED = 2;
+
+    /**
+     * Application process was killed by the system low memory killer, meaning the system was
+     * under memory pressure at the time of kill.
+     */
+    REASON_LOW_MEMORY = 3;
+
+    /**
+     * Application process died because of an unhandled exception in Java code.
+     */
+    REASON_CRASH = 4;
+
+    /**
+     * Application process died because it's crashed due to a native code crash.
+     */
+    REASON_CRASH_NATIVE = 5;
+
+    /**
+     * Application process was killed due to being unresponsive (ANR).
+     */
+    REASON_ANR = 6;
+
+    /**
+     * Application process was killed because it took too long to attach to the system
+     * during the start.
+     */
+    REASON_INITIALIZATION_FAILURE = 7;
+
+    /**
+     * Application process was killed because of initialization failure,
+     * for example, it took too long to attach to the system during the start,
+     * or there was an error during initialization.
+     */
+    REASON_PERMISSION_CHANGE = 8;
+
+    /**
+     * Application process was killed by the activity manager due to excessive resource usage.
+     */
+    REASON_EXCESSIVE_RESOURCE_USAGE = 9;
+
+    /**
+     * Application process was killed because of the user request, for example,
+     * user clicked the "Force stop" button of the application in the Settings,
+     * or swiped away the application from Recents.
+     */
+    REASON_USER_REQUESTED = 10;
+
+    /**
+     * Application process was killed, because the user they are running as on devices
+     * with mutlple users, was stopped.
+     */
+    REASON_USER_STOPPED = 11;
+
+    /**
+     * Application process was killed because its dependency was going away, for example,
+     * a stable content provider connection's client will be killed if the provider is killed.
+     */
+    REASON_DEPENDENCY_DIED = 12;
+
+    /**
+     * Application process was killed by the system for various other reasons,
+     * for example, the application package got disabled by the user;
+     * {@link #description} will specify the cause given by the system.
+     */
+    REASON_OTHER = 13;
+}
+
+/**
+ * The supplemental reason code that why app process is killed
+ */
+enum AppExitSubReasonCode {
+    /**
+     * Application process kills subReason is unknown.
+     */
+    SUBREASON_UNKNOWN = 0;
+
+    /**
+     * Application process was killed because user quit it on the "wait for debugger" dialog.
+     */
+    SUBREASON_WAIT_FOR_DEBUGGER = 1;
+
+    /**
+     * Application process was killed by the activity manager because there were too many cached
+     * processes.
+     */
+    SUBREASON_TOO_MANY_CACHED = 2;
+
+    /**
+     * Application process was killed by the activity manager because there were too many empty
+     * processes.
+     */
+    SUBREASON_TOO_MANY_EMPTY = 3;
+
+    /**
+     * Application process was killed by the activity manager because there were too many cached
+     * processes and this process had been in empty state for a long time.
+     */
+    SUBREASON_TRIM_EMPTY = 4;
+
+    /**
+     * Application process was killed by the activity manager because system was on
+     * memory pressure and this process took large amount of cached memory.
+     */
+    SUBREASON_LARGE_CACHED = 5;
+
+    /**
+     * Application process was killed by the activity manager because the system was on
+     * low memory pressure for a significant amount of time since last idle.
+     */
+    SUBREASON_MEMORY_PRESSURE = 6;
+
+    /**
+     * Application process was killed by the activity manager due to excessive CPU usage.
+     */
+    SUBREASON_EXCESSIVE_CPU = 7;
+
+    /**
+     * System update has done (so the system update process should be killed).
+     */
+    SUBREASON_SYSTEM_UPDATE_DONE = 8;
+
+    /**
+     * Kill all foreground services, for now it only occurs when enabling the quiet
+     * mode for the managed profile.
+     */
+    SUBREASON_KILL_ALL_FG = 9;
+
+    /**
+     * All background processes except certain ones were killed, for now it only occurs
+     * when the density of the default display is changed.
+     */
+    SUBREASON_KILL_ALL_BG_EXCEPT = 10;
+
+    /**
+     * The process associated with the UID was explicitly killed, for example,
+     * it could be because of permission changes.
+     */
+    SUBREASON_KILL_UID = 11;
+
+    /**
+     * The process was explicitly killed with its PID, typically because of
+     * the low memory for surfaces.
+     */
+    SUBREASON_KILL_PID = 12;
+
+    /**
+     * The start of the process was invalid.
+     */
+    SUBREASON_INVALID_START = 13;
+
+    /**
+     * The process was killed because it's in an invalid state, typically
+     * it's triggered from SHELL.
+     */
+    SUBREASON_INVALID_STATE = 14;
+
+    /**
+     * The process was killed when it's imperceptible to user, because it was
+     * in a bad state.
+     */
+    SUBREASON_IMPERCEPTIBLE = 15;
+
+    /**
+     * The process was killed because it's being moved out from LRU list.
+     */
+    SUBREASON_REMOVE_LRU = 16;
+
+    /**
+     * The process was killed because it's isolated and was in a cached state.
+     */
+    SUBREASON_ISOLATED_NOT_NEEDED = 17;
+
+    /**
+     * The process was killed because it's in forced-app-standby state, and it's cached and
+     * its uid state is idle; this would be set only when the reason is {@link #REASON_OTHER}.
+     */
+    SUBREASON_CACHED_IDLE_FORCED_APP_STANDBY = 18;
+}
+
+/**
+ * The relative importance level that the system places on a process.
+ * Keep sync with the definitions in
+ * {@link android.app.ActivityManager.RunningAppProcessInfo}
+ */
+enum Importance {
+    option allow_alias = true;
+
+    IMPORTANCE_FOREGROUND = 100;
+    IMPORTANCE_FOREGROUND_SERVICE = 125;
+    IMPORTANCE_TOP_SLEEPING_PRE_28 = 150;
+    IMPORTANCE_VISIBLE = 200;
+    IMPORTANCE_PERCEPTIBLE_PRE_26 = 130;
+    IMPORTANCE_PERCEPTIBLE = 230;
+    IMPORTANCE_CANT_SAVE_STATE_PRE_26 = 170;
+    IMPORTANCE_SERVICE = 300;
+    IMPORTANCE_TOP_SLEEPING = 325;
+    IMPORTANCE_CANT_SAVE_STATE = 350;
+    IMPORTANCE_CACHED = 400;
+    IMPORTANCE_BACKGROUND = 400;
+    IMPORTANCE_EMPTY = 500;
+    IMPORTANCE_GONE = 1000;
+}