|
| 1 | +package com.drivehub.kamera.dev; |
| 2 | + |
| 3 | +import java.util.Locale; |
| 4 | + |
| 5 | +/** Pure calculations and formatting used by the head-unit performance overlay. */ |
| 6 | +final class PerformanceMetrics { |
| 7 | + |
| 8 | + private static final double BYTES_PER_MIB = 1024d * 1024d; |
| 9 | + private static final double BYTES_PER_GIB = 1024d * 1024d * 1024d; |
| 10 | + |
| 11 | + private PerformanceMetrics() { |
| 12 | + } |
| 13 | + |
| 14 | + static final class CpuTimes { |
| 15 | + final long total; |
| 16 | + final long idle; |
| 17 | + |
| 18 | + CpuTimes(long total, long idle) { |
| 19 | + this.total = total; |
| 20 | + this.idle = idle; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + static final class Snapshot { |
| 25 | + final double systemCpuPercent; |
| 26 | + final double appCpuPercent; |
| 27 | + final long usedRamBytes; |
| 28 | + final long totalRamBytes; |
| 29 | + final long appPssBytes; |
| 30 | + final double loadAverageOneMinute; |
| 31 | + final double cpuTemperatureCelsius; |
| 32 | + final int thermalStatus; |
| 33 | + |
| 34 | + Snapshot( |
| 35 | + double systemCpuPercent, |
| 36 | + double appCpuPercent, |
| 37 | + long usedRamBytes, |
| 38 | + long totalRamBytes, |
| 39 | + long appPssBytes, |
| 40 | + double loadAverageOneMinute, |
| 41 | + double cpuTemperatureCelsius, |
| 42 | + int thermalStatus |
| 43 | + ) { |
| 44 | + this.systemCpuPercent = systemCpuPercent; |
| 45 | + this.appCpuPercent = appCpuPercent; |
| 46 | + this.usedRamBytes = usedRamBytes; |
| 47 | + this.totalRamBytes = totalRamBytes; |
| 48 | + this.appPssBytes = appPssBytes; |
| 49 | + this.loadAverageOneMinute = loadAverageOneMinute; |
| 50 | + this.cpuTemperatureCelsius = cpuTemperatureCelsius; |
| 51 | + this.thermalStatus = thermalStatus; |
| 52 | + } |
| 53 | + |
| 54 | + String format(String thermalText) { |
| 55 | + return formatCpuLine(systemCpuPercent, appCpuPercent) |
| 56 | + + "\n" + formatRamLine(usedRamBytes, totalRamBytes, appPssBytes) |
| 57 | + + "\n" + formatLoadLine(loadAverageOneMinute, thermalText); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static CpuTimes parseProcStatCpuLine(String line) { |
| 62 | + if (line == null) return null; |
| 63 | + String trimmed = line.trim(); |
| 64 | + if (!trimmed.startsWith("cpu ") && !trimmed.startsWith("cpu\t")) return null; |
| 65 | + String[] fields = trimmed.split("\\s+"); |
| 66 | + if (fields.length < 5 || !"cpu".equals(fields[0])) return null; |
| 67 | + try { |
| 68 | + // Linux reports guest/guest_nice inside user/nice already, so only sum through steal. |
| 69 | + long total = 0L; |
| 70 | + int lastIncludedField = Math.min(fields.length - 1, 8); |
| 71 | + for (int i = 1; i <= lastIncludedField; i++) { |
| 72 | + long value = Long.parseLong(fields[i]); |
| 73 | + if (value < 0L) return null; |
| 74 | + total = Math.addExact(total, value); |
| 75 | + } |
| 76 | + long idle = Long.parseLong(fields[4]); |
| 77 | + if (idle < 0L) return null; |
| 78 | + if (fields.length > 5) { |
| 79 | + long ioWait = Long.parseLong(fields[5]); |
| 80 | + if (ioWait < 0L) return null; |
| 81 | + idle = Math.addExact(idle, ioWait); |
| 82 | + } |
| 83 | + return total > 0L && idle <= total ? new CpuTimes(total, idle) : null; |
| 84 | + } catch (ArithmeticException | NumberFormatException ignored) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static double calculateSystemCpuPercent(CpuTimes previous, CpuTimes current) { |
| 90 | + if (previous == null || current == null) return Double.NaN; |
| 91 | + long totalDelta = current.total - previous.total; |
| 92 | + long idleDelta = current.idle - previous.idle; |
| 93 | + if (totalDelta <= 0L || idleDelta < 0L) return Double.NaN; |
| 94 | + return clampPercent(((totalDelta - idleDelta) * 100d) / totalDelta); |
| 95 | + } |
| 96 | + |
| 97 | + static double calculateAppCpuPercent( |
| 98 | + long previousCpuMs, |
| 99 | + long currentCpuMs, |
| 100 | + long previousElapsedMs, |
| 101 | + long currentElapsedMs, |
| 102 | + int processorCount |
| 103 | + ) { |
| 104 | + long cpuDelta = currentCpuMs - previousCpuMs; |
| 105 | + long elapsedDelta = currentElapsedMs - previousElapsedMs; |
| 106 | + if (previousCpuMs < 0L || previousElapsedMs < 0L || cpuDelta < 0L |
| 107 | + || elapsedDelta <= 0L || processorCount <= 0) { |
| 108 | + return Double.NaN; |
| 109 | + } |
| 110 | + double deviceCapacityMs = elapsedDelta * (double) processorCount; |
| 111 | + return clampPercent((cpuDelta * 100d) / deviceCapacityMs); |
| 112 | + } |
| 113 | + |
| 114 | + static double calculateRamPercent(long usedBytes, long totalBytes) { |
| 115 | + if (usedBytes < 0L || totalBytes <= 0L || usedBytes > totalBytes) return Double.NaN; |
| 116 | + return clampPercent((usedBytes * 100d) / totalBytes); |
| 117 | + } |
| 118 | + |
| 119 | + static String formatCpuLine(double systemPercent, double appPercent) { |
| 120 | + return "CPU " + formatPercent(systemPercent) + " · App " + formatPercent(appPercent); |
| 121 | + } |
| 122 | + |
| 123 | + static String formatRamLine(long usedBytes, long totalBytes, long appPssBytes) { |
| 124 | + String systemRam = "–"; |
| 125 | + double ramPercent = calculateRamPercent(usedBytes, totalBytes); |
| 126 | + if (!Double.isNaN(ramPercent)) { |
| 127 | + systemRam = String.format(Locale.US, "%.1f/%.1f GB %s", |
| 128 | + usedBytes / BYTES_PER_GIB, |
| 129 | + totalBytes / BYTES_PER_GIB, |
| 130 | + formatPercent(ramPercent)); |
| 131 | + } |
| 132 | + String appRam = appPssBytes >= 0L |
| 133 | + ? String.format(Locale.US, "%.0f MB", appPssBytes / BYTES_PER_MIB) |
| 134 | + : "–"; |
| 135 | + return "RAM " + systemRam + " · App " + appRam; |
| 136 | + } |
| 137 | + |
| 138 | + static String formatLoadLine(double loadAverageOneMinute, String thermalText) { |
| 139 | + String load = isFiniteInRange(loadAverageOneMinute, 0d, 10_000d) |
| 140 | + ? String.format(Locale.US, "%.2f", loadAverageOneMinute) |
| 141 | + : "–"; |
| 142 | + String safeThermalText = thermalText == null || thermalText.trim().isEmpty() |
| 143 | + ? "Temp –" |
| 144 | + : thermalText; |
| 145 | + return "Load 1m " + load + " · " + safeThermalText; |
| 146 | + } |
| 147 | + |
| 148 | + private static String formatPercent(double value) { |
| 149 | + return isFiniteInRange(value, 0d, 100d) |
| 150 | + ? String.format(Locale.US, "%.0f %%", value) |
| 151 | + : "–"; |
| 152 | + } |
| 153 | + |
| 154 | + private static double clampPercent(double value) { |
| 155 | + if (!Double.isFinite(value)) return Double.NaN; |
| 156 | + return Math.max(0d, Math.min(100d, value)); |
| 157 | + } |
| 158 | + |
| 159 | + static boolean isFiniteInRange(double value, double minimum, double maximum) { |
| 160 | + return Double.isFinite(value) && value >= minimum && value <= maximum; |
| 161 | + } |
| 162 | +} |
0 commit comments