|
| 1 | +/* |
| 2 | + * Copyright (C) 2015. Jared Rummler <jared.rummler@gmail.com> |
| 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 | + */ |
| 17 | + |
| 18 | +package com.jaredrummler.android.processes.models; |
| 19 | + |
| 20 | +import android.os.Parcel; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Locale; |
| 23 | + |
| 24 | +/** |
| 25 | + * <p>/proc/[pid]/status</p> |
| 26 | + * |
| 27 | + * <p>Provides much of the information in /proc/[pid]/stat and /proc/[pid]/statm in a format that's |
| 28 | + * easier for humans to parse.</p> |
| 29 | + * |
| 30 | + * <p>Here's an example:</p> |
| 31 | + * |
| 32 | + * <pre> |
| 33 | + * $ cat /proc/$$/status |
| 34 | + * Name: bash |
| 35 | + * State: S (sleeping) |
| 36 | + * Tgid: 3515 |
| 37 | + * Pid: 3515 |
| 38 | + * PPid: 3452 |
| 39 | + * TracerPid: 0 |
| 40 | + * Uid: 1000 1000 1000 1000 |
| 41 | + * Gid: 100 100 100 100 |
| 42 | + * FDSize: 256 |
| 43 | + * Groups: 16 33 100 |
| 44 | + * VmPeak: 9136 kB |
| 45 | + * VmSize: 7896 kB |
| 46 | + * VmLck: 0 kB |
| 47 | + * VmPin: 0 kB |
| 48 | + * VmHWM: 7572 kB |
| 49 | + * VmRSS: 6316 kB |
| 50 | + * VmData: 5224 kB |
| 51 | + * VmStk: 88 kB |
| 52 | + * VmExe: 572 kB |
| 53 | + * VmLib: 1708 kB |
| 54 | + * VmPMD: 4 kB |
| 55 | + * VmPTE: 20 kB |
| 56 | + * VmSwap: 0 kB |
| 57 | + * Threads: 1 |
| 58 | + * SigQ: 0/3067 |
| 59 | + * SigPnd: 0000000000000000 |
| 60 | + * ShdPnd: 0000000000000000 |
| 61 | + * SigBlk: 0000000000010000 |
| 62 | + * SigIgn: 0000000000384004 |
| 63 | + * SigCgt: 000000004b813efb |
| 64 | + * CapInh: 0000000000000000 |
| 65 | + * CapPrm: 0000000000000000 |
| 66 | + * CapEff: 0000000000000000 |
| 67 | + * CapBnd: ffffffffffffffff |
| 68 | + * Seccomp: 0 |
| 69 | + * Cpus_allowed: 00000001 |
| 70 | + * Cpus_allowed_list: 0 |
| 71 | + * Mems_allowed: 1 |
| 72 | + * Mems_allowed_list: 0 |
| 73 | + * voluntary_ctxt_switches: 150 |
| 74 | + * nonvoluntary_ctxt_switches: 545 |
| 75 | + * </pre> |
| 76 | + * |
| 77 | + * <p>The fields are as follows:</p> |
| 78 | + * |
| 79 | + * <ol> |
| 80 | + * <li>Name: Command run by this process.</li> |
| 81 | + * <li>State: Current state of the process. One of "R (running)", "S (sleeping)", "D (disk |
| 82 | + * sleep)", |
| 83 | + * "T (stopped)", "T (tracing stop)", "Z (zombie)", or "X (dead)".</li> |
| 84 | + * <li>Tgid: Thread group ID (i.e., Process ID).</li> |
| 85 | + * <li>Pid: Thread ID (see gettid(2)).</li> |
| 86 | + * <li>PPid: PID of parent process.</li> |
| 87 | + * <li>TracerPid: PID of process tracing this process (0 if not being traced).</li> |
| 88 | + * <li>Uid, Gid: Real, effective, saved set, and filesystem UIDs (GIDs).</li> |
| 89 | + * <li>FDSize: Number of file descriptor slots currently allocated.</li> |
| 90 | + * <li>Groups: Supplementary group list.</li> |
| 91 | + * <li>VmPeak: Peak virtual memory size.</li> |
| 92 | + * <li>VmSize: Virtual memory size.</li> |
| 93 | + * <li>VmLck: Locked memory size (see mlock(3)).</li> |
| 94 | + * <li>VmPin: Pinned memory size (since Linux 3.2). These are pages that can't be moved because |
| 95 | + * something needs to directly access physical memory.</li> |
| 96 | + * <li>VmHWM: Peak resident set size ("high water mark").</li> |
| 97 | + * <li>VmRSS: Resident set size.</li> |
| 98 | + * <li>VmData, VmStk, VmExe: Size of data, stack, and text segments.</li> |
| 99 | + * <li>VmLib: Shared library code size.</li> |
| 100 | + * <li>VmPTE: Page table entries size (since Linux 2.6.10).</li> |
| 101 | + * <li>VmPMD: Size of second-level page tables (since Linux 4.0).</li> |
| 102 | + * <li>VmSwap: Swapped-out virtual memory size by anonymous private pages; shmem swap usage is not |
| 103 | + * included (since Linux 2.6.34).</li> |
| 104 | + * <li>Threads: Number of threads in process containing this thread.</li> |
| 105 | + * <li>SigQ: This field contains two slash-separated numbers that relate to queued signals for the |
| 106 | + * real user ID of this process. The first of these is the number of currently queued signals for |
| 107 | + * this real user ID, and the second is the resource limit on the number of queued signals for this |
| 108 | + * process (see the description of RLIMIT_SIGPENDING in getrlimit(2)).</li> |
| 109 | + * <li>SigPnd, ShdPnd: Number of signals pending for thread and for process as a whole (see |
| 110 | + * pthreads(7) and signal(7)).</li> |
| 111 | + * <li>SigBlk, SigIgn, SigCgt: Masks indicating signals being blocked, ignored, and caught (see |
| 112 | + * signal(7)).</li> |
| 113 | + * <li>CapInh, CapPrm, CapEff: Masks of capabilities enabled in inheritable, permitted, and |
| 114 | + * effective sets (see capabilities(7)).</li> |
| 115 | + * <li>CapBnd: Capability Bounding set (since Linux 2.6.26, see capabilities(7)).</li> |
| 116 | + * <li>Seccomp: Seccomp mode of the process (since Linux 3.8, see seccomp(2)). 0 means |
| 117 | + * SECCOMP_MODE_DISABLED; 1 means SECCOMP_MODE_STRICT; 2 means SECCOMP_MODE_FILTER. This field is |
| 118 | + * provided only if the kernel was built with the CONFIG_SECCOMP kernel configuration option |
| 119 | + * enabled.</li> |
| 120 | + * <li>Cpus_allowed: Mask of CPUs on which this process may run (since Linux 2.6.24, see |
| 121 | + * cpuset(7)).</li> |
| 122 | + * <li>Cpus_allowed_list: Same as previous, but in "list format" (since Linux 2.6.26, see |
| 123 | + * cpuset(7)).</li> |
| 124 | + * <li>Mems_allowed: Mask of memory nodes allowed to this process (since Linux 2.6.24, see |
| 125 | + * cpuset(7)).</li> |
| 126 | + * <li>Mems_allowed_list: Same as previous, but in "list format" (since Linux 2.6.26, see |
| 127 | + * cpuset(7)). |
| 128 | + * voluntary_ctxt_switches, nonvoluntary_ctxt_switches: Number of voluntary and involuntary context |
| 129 | + * switches (since Linux 2.6.23).</li> |
| 130 | + * </ol> |
| 131 | + */ |
| 132 | +public final class Status extends ProcFile { |
| 133 | + |
| 134 | + /** |
| 135 | + * Read /proc/[pid]/status. |
| 136 | + * |
| 137 | + * @param pid |
| 138 | + * the process id. |
| 139 | + * @return the {@link Status} |
| 140 | + * @throws IOException |
| 141 | + * if the file does not exist or we don't have read permissions. |
| 142 | + */ |
| 143 | + public static Status get(int pid) throws IOException { |
| 144 | + return new Status(String.format(Locale.ENGLISH, "/proc/%d/status", pid)); |
| 145 | + } |
| 146 | + |
| 147 | + private Status(String path) throws IOException { |
| 148 | + super(path); |
| 149 | + } |
| 150 | + |
| 151 | + private Status(Parcel in) { |
| 152 | + super(in); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get the value of one of the fields. |
| 157 | + * |
| 158 | + * @param fieldName |
| 159 | + * the field name. E.g "PPid", "Uid", "Groups". |
| 160 | + * @return The value of the field or {@code null}. |
| 161 | + */ |
| 162 | + public String getValue(String fieldName) { |
| 163 | + String[] lines = content.split("\n"); |
| 164 | + for (String line : lines) { |
| 165 | + if (line.startsWith(fieldName + ":")) { |
| 166 | + return line.split(fieldName + ":")[1].trim(); |
| 167 | + } |
| 168 | + } |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @return The process' UID or -1 if parsing the UID failed. |
| 174 | + */ |
| 175 | + public int getUid() { |
| 176 | + try { |
| 177 | + return Integer.parseInt(getValue("Uid").split("\\s+")[0]); |
| 178 | + } catch (Exception e) { |
| 179 | + return -1; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @return The process' GID or -1 if parsing the GID failed. |
| 185 | + */ |
| 186 | + public int getGid() { |
| 187 | + try { |
| 188 | + return Integer.parseInt(getValue("Gid").split("\\s+")[0]); |
| 189 | + } catch (Exception e) { |
| 190 | + return -1; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public static final Creator<Status> CREATOR = new Creator<Status>() { |
| 195 | + |
| 196 | + @Override public Status createFromParcel(Parcel source) { |
| 197 | + return new Status(source); |
| 198 | + } |
| 199 | + |
| 200 | + @Override public Status[] newArray(int size) { |
| 201 | + return new Status[size]; |
| 202 | + } |
| 203 | + }; |
| 204 | + |
| 205 | +} |
0 commit comments