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