Howard Hao | 1f81981 | 2021-06-21 15:28:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
| 17 | package com.android.car.telemetry.systemmonitor; |
| 18 | |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
| 20 | import android.app.ActivityManager; |
| 21 | import android.app.ActivityManager.MemoryInfo; |
| 22 | import android.content.Context; |
| 23 | import android.os.Handler; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 24 | |
| 25 | import com.android.car.CarLog; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 26 | import com.android.internal.annotations.VisibleForTesting; |
Zhomart Mukhamejanov | 4d99b1e | 2021-10-15 21:36:52 -0700 | [diff] [blame] | 27 | import com.android.server.utils.Slogf; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 28 | |
| 29 | import java.io.BufferedReader; |
| 30 | import java.io.FileReader; |
| 31 | import java.io.IOException; |
| 32 | |
Howard Hao | 1f81981 | 2021-06-21 15:28:29 -0700 | [diff] [blame] | 33 | /** |
| 34 | * SystemMonitor monitors system states and report to listeners when there are |
| 35 | * important changes. |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 36 | * All methods in this class should be invoked from the telemetry thread. |
Howard Hao | 1f81981 | 2021-06-21 15:28:29 -0700 | [diff] [blame] | 37 | */ |
| 38 | public class SystemMonitor { |
| 39 | |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 40 | private static final int NUM_LOADAVG_VALS = 3; |
| 41 | private static final float HI_CPU_LOAD_PER_CORE_BASE_LEVEL = 1.0f; |
| 42 | private static final float MED_CPU_LOAD_PER_CORE_BASE_LEVEL = 0.5f; |
| 43 | private static final float HI_MEM_LOAD_BASE_LEVEL = 0.95f; |
| 44 | private static final float MED_MEM_LOAD_BASE_LEVEL = 0.80f; |
| 45 | private static final String LOADAVG_PATH = "/proc/loadavg"; |
| 46 | |
| 47 | private static final int POLL_INTERVAL_MILLIS = 60000; |
| 48 | |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 49 | private final Handler mTelemetryHandler; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 50 | |
| 51 | private final Context mContext; |
| 52 | private final ActivityManager mActivityManager; |
| 53 | private final String mLoadavgPath; |
Howard Hao | ada36ef | 2021-08-25 11:45:42 -0700 | [diff] [blame] | 54 | private final Runnable mSystemLoadRunnable = this::getSystemLoadRepeated; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 55 | |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 56 | @Nullable private SystemMonitorCallback mCallback; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 57 | private boolean mSystemMonitorRunning = false; |
| 58 | |
Howard Hao | 1f81981 | 2021-06-21 15:28:29 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Interface for receiving notifications about system monitor changes. |
| 61 | */ |
| 62 | public interface SystemMonitorCallback { |
| 63 | /** |
| 64 | * Listens to system monitor event. |
| 65 | * |
| 66 | * @param event the system monitor event. |
| 67 | */ |
| 68 | void onSystemMonitorEvent(SystemMonitorEvent event); |
| 69 | } |
| 70 | |
| 71 | /** |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 72 | * Creates a SystemMonitor instance set with default loadavg path. |
| 73 | * |
| 74 | * @param context the context this is running in. |
| 75 | * @param workerHandler a handler for running monitoring jobs. |
| 76 | * @return SystemMonitor instance. |
| 77 | */ |
| 78 | public static SystemMonitor create(Context context, Handler workerHandler) { |
| 79 | return new SystemMonitor(context, workerHandler, LOADAVG_PATH); |
| 80 | } |
| 81 | |
| 82 | @VisibleForTesting |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 83 | SystemMonitor(Context context, Handler telemetryHandler, String loadavgPath) { |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 84 | mContext = context; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 85 | mTelemetryHandler = telemetryHandler; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 86 | mActivityManager = (ActivityManager) |
| 87 | mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 88 | mLoadavgPath = loadavgPath; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sets the {@link SystemMonitorCallback} to notify of system state changes. |
Howard Hao | 1f81981 | 2021-06-21 15:28:29 -0700 | [diff] [blame] | 93 | * |
| 94 | * @param callback the callback to nofify state changes on. |
| 95 | */ |
| 96 | public void setSystemMonitorCallback(SystemMonitorCallback callback) { |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 97 | mCallback = callback; |
| 98 | if (!mSystemMonitorRunning) { |
| 99 | startSystemLoadMonitoring(); |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Unsets the {@link SystemMonitorCallback}. |
| 105 | */ |
| 106 | public void unsetSystemMonitorCallback() { |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 107 | mTelemetryHandler.removeCallbacks(mSystemLoadRunnable); |
| 108 | mSystemMonitorRunning = false; |
| 109 | mCallback = null; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets the loadavg data from /proc/loadavg, getting the first 3 averages, |
| 114 | * which are 1-min, 5-min and 15-min moving averages respectively. |
| 115 | * |
| 116 | * Requires Selinux permissions 'open', 'read, 'getattr' to proc_loadavg, |
| 117 | * which is set in Car/car_product/sepolicy/private/carservice_app.te. |
| 118 | * |
| 119 | * @return the {@link CpuLoadavg}. |
| 120 | */ |
| 121 | @VisibleForTesting |
| 122 | @Nullable |
| 123 | CpuLoadavg getCpuLoad() { |
| 124 | try (BufferedReader reader = new BufferedReader(new FileReader(mLoadavgPath))) { |
| 125 | String line = reader.readLine(); |
| 126 | String[] vals = line.split("\\s+", NUM_LOADAVG_VALS + 1); |
| 127 | if (vals.length < NUM_LOADAVG_VALS) { |
Zhomart Mukhamejanov | 4d99b1e | 2021-10-15 21:36:52 -0700 | [diff] [blame] | 128 | Slogf.w(CarLog.TAG_TELEMETRY, "Loadavg wrong format"); |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 129 | return null; |
| 130 | } |
| 131 | CpuLoadavg cpuLoadavg = new CpuLoadavg(); |
| 132 | cpuLoadavg.mOneMinuteVal = Float.parseFloat(vals[0]); |
| 133 | cpuLoadavg.mFiveMinutesVal = Float.parseFloat(vals[1]); |
| 134 | cpuLoadavg.mFifteenMinutesVal = Float.parseFloat(vals[2]); |
| 135 | return cpuLoadavg; |
| 136 | } catch (IOException | NumberFormatException ex) { |
Zhomart Mukhamejanov | 4d99b1e | 2021-10-15 21:36:52 -0700 | [diff] [blame] | 137 | Slogf.w(CarLog.TAG_TELEMETRY, "Failed to read loadavg file.", ex); |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 138 | return null; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Gets the {@link ActivityManager.MemoryInfo} for system memory pressure. |
| 144 | * |
| 145 | * Of the MemoryInfo fields, we will only be using availMem and totalMem, |
| 146 | * since lowMemory and threshold are likely deprecated. |
| 147 | * |
| 148 | * @return {@link MemoryInfo} for the system. |
| 149 | */ |
| 150 | private MemoryInfo getMemoryLoad() { |
| 151 | MemoryInfo mi = new ActivityManager.MemoryInfo(); |
| 152 | mActivityManager.getMemoryInfo(mi); |
| 153 | return mi; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the CPU usage level for a {@link SystemMonitorEvent}. |
| 158 | * |
| 159 | * @param event the {@link SystemMonitorEvent}. |
| 160 | * @param cpuLoadPerCore the CPU load average per CPU core. |
| 161 | */ |
| 162 | @VisibleForTesting |
| 163 | void setEventCpuUsageLevel(SystemMonitorEvent event, double cpuLoadPerCore) { |
| 164 | if (cpuLoadPerCore > HI_CPU_LOAD_PER_CORE_BASE_LEVEL) { |
| 165 | event.setCpuUsageLevel(SystemMonitorEvent.USAGE_LEVEL_HI); |
| 166 | } else if (cpuLoadPerCore > MED_CPU_LOAD_PER_CORE_BASE_LEVEL |
| 167 | && cpuLoadPerCore <= HI_CPU_LOAD_PER_CORE_BASE_LEVEL) { |
| 168 | event.setCpuUsageLevel(SystemMonitorEvent.USAGE_LEVEL_MED); |
| 169 | } else { |
| 170 | event.setCpuUsageLevel(SystemMonitorEvent.USAGE_LEVEL_LOW); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sets the memory usage level for a {@link SystemMonitorEvent}. |
| 176 | * |
| 177 | * @param event the {@link SystemMonitorEvent}. |
| 178 | * @param memLoadRatio ratio of used memory to total memory. |
| 179 | */ |
| 180 | @VisibleForTesting |
| 181 | void setEventMemUsageLevel(SystemMonitorEvent event, double memLoadRatio) { |
| 182 | if (memLoadRatio > HI_MEM_LOAD_BASE_LEVEL) { |
| 183 | event.setMemoryUsageLevel(SystemMonitorEvent.USAGE_LEVEL_HI); |
| 184 | } else if (memLoadRatio > MED_MEM_LOAD_BASE_LEVEL |
| 185 | && memLoadRatio <= HI_MEM_LOAD_BASE_LEVEL) { |
| 186 | event.setMemoryUsageLevel(SystemMonitorEvent.USAGE_LEVEL_MED); |
| 187 | } else { |
| 188 | event.setMemoryUsageLevel(SystemMonitorEvent.USAGE_LEVEL_LOW); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * The Runnable to repeatedly getting system load data with some interval. |
| 194 | */ |
| 195 | private void getSystemLoadRepeated() { |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 196 | try { |
| 197 | CpuLoadavg cpuLoadAvg = getCpuLoad(); |
| 198 | if (cpuLoadAvg == null) { |
| 199 | return; |
| 200 | } |
| 201 | int numProcessors = Runtime.getRuntime().availableProcessors(); |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 202 | |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 203 | MemoryInfo memInfo = getMemoryLoad(); |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 204 | |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 205 | SystemMonitorEvent event = new SystemMonitorEvent(); |
| 206 | setEventCpuUsageLevel(event, cpuLoadAvg.mOneMinuteVal / numProcessors); |
Rui Qiu | fd8cc59 | 2021-09-30 11:44:31 -0700 | [diff] [blame] | 207 | setEventMemUsageLevel(event, 1 - (double) memInfo.availMem / memInfo.totalMem); |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 208 | |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 209 | mCallback.onSystemMonitorEvent(event); |
| 210 | } finally { |
| 211 | if (mSystemMonitorRunning) { |
| 212 | mTelemetryHandler.postDelayed(mSystemLoadRunnable, POLL_INTERVAL_MILLIS); |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Starts system load monitoring. |
| 219 | */ |
| 220 | private void startSystemLoadMonitoring() { |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 221 | mTelemetryHandler.post(mSystemLoadRunnable); |
| 222 | mSystemMonitorRunning = true; |
Howard Hao | 2b11eb5 | 2021-08-06 16:43:24 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static final class CpuLoadavg { |
| 226 | float mOneMinuteVal; |
| 227 | float mFiveMinutesVal; |
| 228 | float mFifteenMinutesVal; |
Howard Hao | 1f81981 | 2021-06-21 15:28:29 -0700 | [diff] [blame] | 229 | } |
| 230 | } |