summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Xiang Wang <xwxw@google.com> 2024-12-11 21:13:37 -0800
committer Android (Google) Code Review <android-gerrit@google.com> 2024-12-11 21:13:37 -0800
commit1b16b1da99306e6781a5fc24c80a336484858d70 (patch)
tree6d58e9eb6960a02e2bbdde2830b008cc34a0cb88
parent619d71249d9adabc9185979cda5ab82ab0dadba4 (diff)
parent143873de72c8d5fa282af5cd6c960a4e9c6fd4e8 (diff)
Merge "Add server side headroom param checking" into main
-rw-r--r--services/core/java/com/android/server/power/hint/HintManagerService.java67
-rw-r--r--services/tests/performancehinttests/src/com/android/server/power/hint/HintManagerServiceTest.java47
2 files changed, 110 insertions, 4 deletions
diff --git a/services/core/java/com/android/server/power/hint/HintManagerService.java b/services/core/java/com/android/server/power/hint/HintManagerService.java
index 137ea0617f21..51e0195f13fc 100644
--- a/services/core/java/com/android/server/power/hint/HintManagerService.java
+++ b/services/core/java/com/android/server/power/hint/HintManagerService.java
@@ -80,6 +80,7 @@ import com.android.server.utils.Slogf;
import java.io.FileDescriptor;
import java.io.PrintWriter;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -1480,6 +1481,7 @@ public final class HintManagerService extends SystemService {
if (!mSupportInfo.headroom.isCpuSupported) {
throw new UnsupportedOperationException();
}
+ checkCpuHeadroomParams(params);
final CpuHeadroomParams halParams = new CpuHeadroomParams();
halParams.tids = new int[]{Binder.getCallingPid()};
halParams.calculationType = params.calculationType;
@@ -1487,10 +1489,6 @@ public final class HintManagerService extends SystemService {
if (params.usesDeviceHeadroom) {
halParams.tids = new int[]{};
} else if (params.tids != null && params.tids.length > 0) {
- if (params.tids.length > 5) {
- throw new IllegalArgumentException(
- "More than 5 TIDs is requested: " + params.tids.length);
- }
if (SystemProperties.getBoolean(PROPERTY_CHECK_HEADROOM_TID, true)) {
final int tgid = Process.getThreadGroupLeader(Binder.getCallingPid());
for (int tid : params.tids) {
@@ -1530,11 +1528,45 @@ public final class HintManagerService extends SystemService {
}
}
+ private void checkCpuHeadroomParams(CpuHeadroomParamsInternal params) {
+ boolean calculationTypeMatched = false;
+ try {
+ for (final Field field :
+ CpuHeadroomParams.CalculationType.class.getDeclaredFields()) {
+ if (field.getType() == byte.class) {
+ byte value = field.getByte(null);
+ if (value == params.calculationType) {
+ calculationTypeMatched = true;
+ break;
+ }
+ }
+ }
+ } catch (IllegalAccessException e) {
+ Slog.wtf(TAG, "Checking the calculation type was unexpectedly not allowed");
+ }
+ if (!calculationTypeMatched) {
+ throw new IllegalArgumentException(
+ "Unknown CPU headroom calculation type " + (int) params.calculationType);
+ }
+ if (params.calculationWindowMillis < 50 || params.calculationWindowMillis > 10000) {
+ throw new IllegalArgumentException(
+ "Invalid CPU headroom calculation window, expected [50, 10000] but got "
+ + params.calculationWindowMillis);
+ }
+ if (!params.usesDeviceHeadroom) {
+ if (params.tids != null && params.tids.length > 5) {
+ throw new IllegalArgumentException(
+ "More than 5 TIDs requested: " + params.tids.length);
+ }
+ }
+ }
+
@Override
public GpuHeadroomResult getGpuHeadroom(@NonNull GpuHeadroomParamsInternal params) {
if (!mSupportInfo.headroom.isGpuSupported) {
throw new UnsupportedOperationException();
}
+ checkGpuHeadroomParams(params);
final GpuHeadroomParams halParams = new GpuHeadroomParams();
halParams.calculationType = params.calculationType;
halParams.calculationWindowMillis = params.calculationWindowMillis;
@@ -1565,6 +1597,33 @@ public final class HintManagerService extends SystemService {
}
}
+ private void checkGpuHeadroomParams(GpuHeadroomParamsInternal params) {
+ boolean calculationTypeMatched = false;
+ try {
+ for (final Field field :
+ GpuHeadroomParams.CalculationType.class.getDeclaredFields()) {
+ if (field.getType() == byte.class) {
+ byte value = field.getByte(null);
+ if (value == params.calculationType) {
+ calculationTypeMatched = true;
+ break;
+ }
+ }
+ }
+ } catch (IllegalAccessException e) {
+ Slog.wtf(TAG, "Checking the calculation type was unexpectedly not allowed");
+ }
+ if (!calculationTypeMatched) {
+ throw new IllegalArgumentException(
+ "Unknown GPU headroom calculation type " + (int) params.calculationType);
+ }
+ if (params.calculationWindowMillis < 50 || params.calculationWindowMillis > 10000) {
+ throw new IllegalArgumentException(
+ "Invalid GPU headroom calculation window, expected [50, 10000] but got "
+ + params.calculationWindowMillis);
+ }
+ }
+
@Override
public long getCpuHeadroomMinIntervalMillis() {
if (!mSupportInfo.headroom.isCpuSupported) {
diff --git a/services/tests/performancehinttests/src/com/android/server/power/hint/HintManagerServiceTest.java b/services/tests/performancehinttests/src/com/android/server/power/hint/HintManagerServiceTest.java
index b166514ce0b9..428e438443cd 100644
--- a/services/tests/performancehinttests/src/com/android/server/power/hint/HintManagerServiceTest.java
+++ b/services/tests/performancehinttests/src/com/android/server/power/hint/HintManagerServiceTest.java
@@ -1274,6 +1274,53 @@ public class HintManagerServiceTest {
}
@Test
+ public void testCpuHeadroomInvalidParams() {
+ HintManagerService service = createService();
+ final CpuHeadroomParamsInternal param1 = new CpuHeadroomParamsInternal();
+ param1.calculationType = 100;
+ assertThrows(IllegalArgumentException.class, () -> {
+ service.getBinderServiceInstance().getCpuHeadroom(param1);
+ });
+
+ final CpuHeadroomParamsInternal param2 = new CpuHeadroomParamsInternal();
+ param2.calculationWindowMillis = 49;
+ assertThrows(IllegalArgumentException.class, () -> {
+ service.getBinderServiceInstance().getCpuHeadroom(param2);
+ });
+ param2.calculationWindowMillis = 10001;
+ assertThrows(IllegalArgumentException.class, () -> {
+ service.getBinderServiceInstance().getCpuHeadroom(param2);
+ });
+
+ final CpuHeadroomParamsInternal param3 = new CpuHeadroomParamsInternal();
+ param3.tids = new int[]{1, 2, 3, 4, 5, 6};
+ assertThrows(IllegalArgumentException.class, () -> {
+ service.getBinderServiceInstance().getCpuHeadroom(param3);
+ });
+ }
+
+ @Test
+ public void testGpuHeadroomInvalidParams() {
+ HintManagerService service = createService();
+ final GpuHeadroomParamsInternal param1 = new GpuHeadroomParamsInternal();
+ param1.calculationType = 100;
+ assertThrows(IllegalArgumentException.class, () -> {
+ service.getBinderServiceInstance().getGpuHeadroom(param1);
+ });
+
+ final GpuHeadroomParamsInternal param2 = new GpuHeadroomParamsInternal();
+ param2.calculationWindowMillis = 49;
+ assertThrows(IllegalArgumentException.class, () -> {
+ service.getBinderServiceInstance().getGpuHeadroom(param2);
+ });
+ param2.calculationWindowMillis = 10001;
+ assertThrows(IllegalArgumentException.class, () -> {
+ service.getBinderServiceInstance().getGpuHeadroom(param2);
+ });
+ }
+
+
+ @Test
public void testCpuHeadroomCache() throws Exception {
CpuHeadroomParamsInternal params1 = new CpuHeadroomParamsInternal();
CpuHeadroomParams halParams1 = new CpuHeadroomParams();