Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

Launcher: show_stats' memory figure on kernels without MemAvailable

Riku Pirttinokka8725bd02c984
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
index eda36c898..354e2bc81 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -2380,26 +2380,15 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
return Math.max(0, Math.min(100, busy));
}
- /** RAM usage as {used, total} in MB, used = MemTotal - MemAvailable, e.g. "945 / 1536 MB". Used over
- * free so the line reads as a load against a known whole; null when /proc/meminfo cannot be read. */
+ /** RAM usage as {used, total} in MB (MemInfo.usedOfTotalMb); null when /proc/meminfo cannot be read. */
private long[] memUsedOfTotalMb() {
- long available = -1, total = -1;
+ java.util.List<String> lines = new java.util.ArrayList<>();
try (java.io.BufferedReader r = new java.io.BufferedReader(new java.io.FileReader("/proc/meminfo"))) {
- String line;
- String[] t;
- while ((line = r.readLine()) != null) {
- if (line.startsWith("MemAvailable:")) {
- t = line.split("\\s+");
- if (t.length >= 2) available = Long.parseLong(t[1]) / 1024;
- } else if (line.startsWith("MemTotal:")) {
- t = line.split("\\s+");
- if (t.length >= 2) total = Long.parseLong(t[1]) / 1024;
- }
- }
- } catch (IOException | NumberFormatException ignored) {
+ for (String line; (line = r.readLine()) != null;) lines.add(line);
+ } catch (IOException ignored) {
+ return null;
}
- if (available < 0 || total <= 0) return null;
- return new long[]{Math.max(0, total - available), total};
+ return MemInfo.usedOfTotalMb(lines);
}
private static String readFirstLine(String path) {
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/MemInfo.java b/launcher/app/src/main/java/com/gathin/tvhome/MemInfo.java
new file mode 100644
index 000000000..24389e55f
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MemInfo.java
@@ -0,0 +1,39 @@
+package com.gathin.tvhome;
+
+/** show_stats' memory figure from the lines of /proc/meminfo. */
+final class MemInfo {
+ private MemInfo() {}
+
+ /**
+ * RAM usage as {used, total} in MB, used = MemTotal - MemAvailable, e.g. "945 / 1536 MB". Used over free so
+ * the line reads as a load against a known whole. MemAvailable only exists since Linux 3.14; on older kernels
+ * (TPM171E Philips, Android TV 8, kernel 3.10) available is MemFree + Buffers + Cached, the estimate free(1)
+ * used before it. Null when MemTotal or every available figure is missing.
+ */
+ static long[] usedOfTotalMb(Iterable<String> lines) {
+ long total = -1, available = -1, free = -1, buffers = -1, cached = -1;
+ for (String line : lines) {
+ if (line.startsWith("MemTotal:")) total = kb(line);
+ else if (line.startsWith("MemAvailable:")) available = kb(line);
+ else if (line.startsWith("MemFree:")) free = kb(line);
+ else if (line.startsWith("Buffers:")) buffers = kb(line);
+ else if (line.startsWith("Cached:")) cached = kb(line);
+ }
+ if (available < 0 && free >= 0) available = free + Math.max(0, buffers) + Math.max(0, cached);
+ if (available < 0 || total <= 0) return null;
+ long totalMb = total / 1024;
+ long availableMb = Math.min(total, available) / 1024;
+ return new long[]{Math.max(0, totalMb - availableMb), totalMb};
+ }
+
+ /** The kB figure of a "Name: 1234 kB" line, or -1. */
+ private static long kb(String line) {
+ String[] t = line.trim().split("\\s+");
+ if (t.length < 2) return -1;
+ try {
+ return Long.parseLong(t[1]);
+ } catch (NumberFormatException e) {
+ return -1;
+ }
+ }
+}
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/MemInfoTest.java b/launcher/app/src/test/java/com/gathin/tvhome/MemInfoTest.java
new file mode 100644
index 000000000..aefd876bf
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/MemInfoTest.java
@@ -0,0 +1,40 @@
+package com.gathin.tvhome;
+import org.junit.Test;
+import java.util.Arrays;
+import java.util.Collections;
+import static org.junit.Assert.*;
+public class MemInfoTest {
+ @Test public void usesMemAvailableWhenTheKernelHasIt() {
+ long[] mem = MemInfo.usedOfTotalMb(Arrays.asList(
+ "MemTotal: 1572864 kB",
+ "MemFree: 102400 kB",
+ "MemAvailable: 604160 kB",
+ "Buffers: 20480 kB",
+ "Cached: 409600 kB"));
+ assertArrayEquals(new long[]{946, 1536}, mem);
+ }
+ @Test public void fallsBackToFreeBuffersAndCachedBefore314() {
+ // A 3.10 kernel (Philips TPM171E, Android TV 8): no MemAvailable line.
+ long[] mem = MemInfo.usedOfTotalMb(Arrays.asList(
+ "MemTotal: 1909568 kB",
+ "MemFree: 58408 kB",
+ "Buffers: 48164 kB",
+ "Cached: 682468 kB",
+ "SwapCached: 0 kB"));
+ assertArrayEquals(new long[]{1864 - 770, 1864}, mem);
+ }
+ @Test public void freeAloneIsEnoughForTheFallback() {
+ assertArrayEquals(new long[]{1024, 2048},
+ MemInfo.usedOfTotalMb(Arrays.asList("MemTotal: 2097152 kB", "MemFree: 1048576 kB")));
+ }
+ @Test public void neverReportsMoreAvailableThanTotal() {
+ assertArrayEquals(new long[]{0, 1024},
+ MemInfo.usedOfTotalMb(Arrays.asList("MemTotal: 1048576 kB", "MemAvailable: 2097152 kB")));
+ }
+ @Test public void nullWithoutTotalOrAnyAvailableFigure() {
+ assertNull(MemInfo.usedOfTotalMb(Collections.emptyList()));
+ assertNull(MemInfo.usedOfTotalMb(Arrays.asList("MemTotal: 1048576 kB", "Buffers: 1024 kB")));
+ assertNull(MemInfo.usedOfTotalMb(Arrays.asList("MemFree: 1048576 kB")));
+ assertNull(MemInfo.usedOfTotalMb(Arrays.asList("MemTotal: x kB", "MemFree: 1024 kB")));
+ }
+}