Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 | */ |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 16 | package com.android.server.stats.pull; |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 17 | |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 18 | import static android.os.Process.PROC_OUT_STRING; |
| 19 | |
Rafal Slawik | 21d3f51 | 2019-09-24 20:00:18 +0100 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 21 | import android.os.Process; |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 22 | import android.util.SparseArray; |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 23 | |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 24 | public final class ProcfsMemoryUtil { |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 25 | private static final int[] CMDLINE_OUT = new int[] { PROC_OUT_STRING }; |
| 26 | private static final String[] STATUS_KEYS = new String[] { |
| 27 | "Uid:", |
| 28 | "VmHWM:", |
| 29 | "VmRSS:", |
| 30 | "RssAnon:", |
| 31 | "VmSwap:" |
| 32 | }; |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 33 | |
| 34 | private ProcfsMemoryUtil() {} |
| 35 | |
| 36 | /** |
Rafal Slawik | 21d3f51 | 2019-09-24 20:00:18 +0100 | [diff] [blame] | 37 | * Reads memory stats of a process from procfs. Returns values of the VmHWM, VmRss, AnonRSS, |
| 38 | * VmSwap fields in /proc/pid/status in kilobytes or null if not available. |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 39 | */ |
Rafal Slawik | 21d3f51 | 2019-09-24 20:00:18 +0100 | [diff] [blame] | 40 | @Nullable |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 41 | public static MemorySnapshot readMemorySnapshotFromProcfs(int pid) { |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 42 | long[] output = new long[STATUS_KEYS.length]; |
| 43 | output[0] = -1; |
| 44 | Process.readProcLines("/proc/" + pid + "/status", STATUS_KEYS, output); |
| 45 | if (output[0] == -1 || (output[3] == 0 && output[4] == 0)) { |
| 46 | // Could not open file or anon rss / swap are 0 indicating the process is in a zombie |
| 47 | // state. |
Rafal Slawik | 21d3f51 | 2019-09-24 20:00:18 +0100 | [diff] [blame] | 48 | return null; |
| 49 | } |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 50 | final MemorySnapshot snapshot = new MemorySnapshot(); |
| 51 | snapshot.uid = (int) output[0]; |
| 52 | snapshot.rssHighWaterMarkInKilobytes = (int) output[1]; |
| 53 | snapshot.rssInKilobytes = (int) output[2]; |
| 54 | snapshot.anonRssInKilobytes = (int) output[3]; |
| 55 | snapshot.swapInKilobytes = (int) output[4]; |
| 56 | return snapshot; |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Rafal Slawik | 203c3db | 2019-09-25 19:53:01 +0100 | [diff] [blame] | 59 | /** |
| 60 | * Reads cmdline of a process from procfs. |
| 61 | * |
| 62 | * Returns content of /proc/pid/cmdline (e.g. /system/bin/statsd) or an empty string |
| 63 | * if the file is not available. |
| 64 | */ |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 65 | public static String readCmdlineFromProcfs(int pid) { |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 66 | String[] cmdline = new String[1]; |
| 67 | if (!Process.readProcFile("/proc/" + pid + "/cmdline", CMDLINE_OUT, cmdline, null, null)) { |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 68 | return ""; |
| 69 | } |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 70 | return cmdline[0]; |
| 71 | } |
| 72 | |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 73 | /** |
| 74 | * Scans all /proc/pid/cmdline entries and returns a mapping between pid and cmdline. |
| 75 | */ |
| 76 | public static SparseArray<String> getProcessCmdlines() { |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 77 | int[] pids = new int[1024]; |
| 78 | pids = Process.getPids("/proc", pids); |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 79 | |
| 80 | SparseArray<String> cmdlines = new SparseArray<>(pids.length); |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 81 | for (int pid : pids) { |
| 82 | if (pid < 0) { |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 83 | break; |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 84 | } |
| 85 | String cmdline = readCmdlineFromProcfs(pid); |
| 86 | if (cmdline.isEmpty()) { |
| 87 | continue; |
| 88 | } |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 89 | cmdlines.append(pid, cmdline); |
Ioannis Ilkos | 9035a22 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 90 | } |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 91 | return cmdlines; |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 92 | } |
Rafal Slawik | 7efcefb | 2019-09-20 11:16:20 +0100 | [diff] [blame] | 93 | |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 94 | public static final class MemorySnapshot { |
Rafal Slawik | 21d3f51 | 2019-09-24 20:00:18 +0100 | [diff] [blame] | 95 | public int uid; |
| 96 | public int rssHighWaterMarkInKilobytes; |
Rafal Slawik | 7efcefb | 2019-09-20 11:16:20 +0100 | [diff] [blame] | 97 | public int rssInKilobytes; |
| 98 | public int anonRssInKilobytes; |
| 99 | public int swapInKilobytes; |
Rafal Slawik | 7efcefb | 2019-09-20 11:16:20 +0100 | [diff] [blame] | 100 | } |
Rafal Slawik | 196532a | 2019-09-13 11:50:27 +0100 | [diff] [blame] | 101 | } |