Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b16b1da authored by Xiang Wang's avatar Xiang Wang Committed by Android (Google) Code Review
Browse files

Merge "Add server side headroom param checking" into main

parents 619d7124 143873de
Loading
Loading
Loading
Loading
+63 −4
Original line number Diff line number Diff line
@@ -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) {
+47 −0
Original line number Diff line number Diff line
@@ -1273,6 +1273,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();