public AndroidAppProcess(int pid) throws IOException, NotAndroidAppProcessException { super(pid); final boolean foreground; int uid; if (SYS_SUPPORTS_SCHEDGROUPS) { Cgroup cgroup = cgroup(); ControlGroup cpuacct = cgroup.getGroup("cpuacct"); ControlGroup cpu = cgroup.getGroup("cpu"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (cpu == null || cpuacct == null || !cpuacct.group.contains("pid_")) { throw new NotAndroidAppProcessException(pid); } foreground = !cpu.group.contains("bg_non_interactive"); try { uid = Integer.parseInt(cpuacct.group.split("/")[1].replace("uid_", "")); } catch (Exception e) { uid = status().getUid(); } ProcessManager.log( "name=%s, pid=%d, uid=%d, foreground=%b, cpuacct=%s, cpu=%s", name, pid, uid, foreground, cpuacct.toString(), cpu.toString()); } else { if (cpu == null || cpuacct == null || !cpu.group.contains("apps")) { throw new NotAndroidAppProcessException(pid); } foreground = !cpu.group.contains("bg_non_interactive"); try { uid = Integer.parseInt(cpuacct.group.substring(cpuacct.group.lastIndexOf("/") + 1)); } catch (Exception e) { uid = status().getUid(); } ProcessManager.log( "name=%s, pid=%d, uid=%d foreground=%b, cpuacct=%s, cpu=%s", name, pid, uid, foreground, cpuacct.toString(), cpu.toString()); } } else { // this is a really ugly way to check if the process is an application. // we could possibly check the UID name (starts with "app_" or "u<USER_ID>_a") if (name.startsWith("/") || !new File("/data/data", getPackageName()).exists()) { throw new NotAndroidAppProcessException(pid); } Stat stat = stat(); Status status = status(); // https://github.com/android/platform_system_core/blob/jb-mr1-release/libcutils/sched_policy.c#L245-256 foreground = stat.policy() == 0; // SCHED_NORMAL uid = status.getUid(); ProcessManager.log("name=%s, pid=%d, uid=%d foreground=%b", name, pid, uid, foreground); } this.foreground = foreground; this.uid = uid; }
public AndroidAppProcess(int pid) throws IOException, NotAndroidAppProcessException { super(pid); cgroup = super.cgroup(); ControlGroup cpuacct = cgroup.getGroup("cpuacct"); ControlGroup cpu = cgroup.getGroup("cpu"); if (cpu == null || cpuacct == null || !cpuacct.group.contains("pid_")) { throw new NotAndroidAppProcessException(pid); } foreground = !cpu.group.contains("bg_non_interactive"); try { uid = Integer.parseInt(cpuacct.group.split("/")[1].replace("uid_", "")); } catch (Exception e) { uid = status().getUid(); } }