blob: c34dc2f5a9b17fc962539112dbda8536b43b199f [file] [log] [blame]
Simon MacMullen30f04a72020-02-05 10:57:40 +00001/*
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
17package com.android.server;
18
19import android.os.StrictMode;
20import android.util.Slog;
21
22import libcore.io.IoUtils;
23
24import java.io.File;
25import java.io.IOException;
26import java.io.StringWriter;
27
28/**
29 * Utility method for memory pressure (PSI).
30 */
31public 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 */
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
55 private MemoryPressureUtil(){}
56}