1 /* 2 * Copyright (C) 2020 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.server; 18 19 import android.os.StrictMode; 20 import android.util.Slog; 21 22 import libcore.io.IoUtils; 23 24 import java.io.File; 25 import java.io.IOException; 26 import java.io.StringWriter; 27 28 /** 29 * Utility method for memory pressure (PSI). 30 */ 31 public final class MemoryPressureUtil { 32 private static final String FILE = "/proc/pressure/memory"; 33 private static final String TAG = "MemoryPressure"; 34 35 /** 36 * @return a stanza about memory PSI to add to a report. 37 */ currentPsiState()38 public static String currentPsiState() { 39 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); 40 StringWriter contents = new StringWriter(); 41 try { 42 if (new File(FILE).exists()) { 43 contents.append("----- Output from /proc/pressure/memory -----\n"); 44 contents.append(IoUtils.readFileAsString(FILE)); 45 contents.append("----- End output from /proc/pressure/memory -----\n\n"); 46 } 47 } catch (IOException e) { 48 Slog.e(TAG, "Could not read " + FILE, e); 49 } finally { 50 StrictMode.setThreadPolicy(savedPolicy); 51 } 52 return contents.toString(); 53 } 54 MemoryPressureUtil()55 private MemoryPressureUtil(){} 56 } 57