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

Commit b2594157 authored by Xiang Wang's avatar Xiang Wang
Browse files

Update createHintSession API doc and add error slog

Bug: 290843763
Test: atest PerformanceHintManagerTest
Change-Id: Ie0797e410e1c1c0b4c6d53a2e64daa20613d4455
parent d91397a6
Loading
Loading
Loading
Loading
+16 −10
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import java.io.Closeable;
import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.Reference;
import java.lang.ref.Reference;
import java.util.Objects;




/** The PerformanceHintManager allows apps to send performance hint to system. */
/** The PerformanceHintManager allows apps to send performance hint to system. */
@@ -54,15 +55,20 @@ public final class PerformanceHintManager {
     * duration.
     * duration.
     *
     *
     * @param tids The list of threads to be associated with this session. They must be part of
     * @param tids The list of threads to be associated with this session. They must be part of
     *     this process' thread group.
     *     this process' thread group
     * @param initialTargetWorkDurationNanos The desired duration in nanoseconds for the new
     * @param initialTargetWorkDurationNanos The desired duration in nanoseconds for the new
     *     session.
     *     session
     * @return the new session if it is supported on this device, null if hint session is not
     * @return the new session if it is supported on this device, null if hint session is not
     *     supported on this device.
     *     supported on this device or the tid doesn't belong to the application
     * @throws IllegalArgumentException if the thread id list is empty, or
     *                                  initialTargetWorkDurationNanos is non-positive
     */
     */
    @Nullable
    @Nullable
    public Session createHintSession(@NonNull int[] tids, long initialTargetWorkDurationNanos) {
    public Session createHintSession(@NonNull int[] tids, long initialTargetWorkDurationNanos) {
        Preconditions.checkNotNull(tids, "tids cannot be null");
        Objects.requireNonNull(tids, "tids cannot be null");
        if (tids.length == 0) {
            throw new IllegalArgumentException("thread id list can't be empty.");
        }
        Preconditions.checkArgumentPositive(initialTargetWorkDurationNanos,
        Preconditions.checkArgumentPositive(initialTargetWorkDurationNanos,
                "the hint target duration should be positive.");
                "the hint target duration should be positive.");
        long nativeSessionPtr = nativeCreateSession(mNativeManagerPtr, tids,
        long nativeSessionPtr = nativeCreateSession(mNativeManagerPtr, tids,
@@ -74,7 +80,7 @@ public final class PerformanceHintManager {
    /**
    /**
     * Get preferred update rate information for this device.
     * Get preferred update rate information for this device.
     *
     *
     * @return the preferred update rate supported by device software.
     * @return the preferred update rate supported by device software
     */
     */
    public long getPreferredUpdateRateNanos() {
    public long getPreferredUpdateRateNanos() {
        return nativeGetPreferredUpdateRateNanos(mNativeManagerPtr);
        return nativeGetPreferredUpdateRateNanos(mNativeManagerPtr);
@@ -208,7 +214,7 @@ public final class PerformanceHintManager {
        /**
        /**
         * Sends performance hints to inform the hint session of changes in the workload.
         * Sends performance hints to inform the hint session of changes in the workload.
         *
         *
         * @param hint The hint to send to the session.
         * @param hint The hint to send to the session
         *
         *
         * @hide
         * @hide
         */
         */
@@ -229,11 +235,11 @@ public final class PerformanceHintManager {
         * Note that this is not an oneway method.
         * Note that this is not an oneway method.
         *
         *
         * @param tids The list of threads to be associated with this session. They must be
         * @param tids The list of threads to be associated with this session. They must be
         *     part of this app's thread group.
         *     part of this app's thread group
         *
         *
         * @throws IllegalStateException if the hint session is not in the foreground.
         * @throws IllegalStateException if the hint session is not in the foreground
         * @throws IllegalArgumentException if the thread id list is empty.
         * @throws IllegalArgumentException if the thread id list is empty
         * @throws SecurityException if any thread id doesn't belong to the application.
         * @throws SecurityException if any thread id doesn't belong to the application
         */
         */
        public void setThreads(@NonNull int[] tids) {
        public void setThreads(@NonNull int[] tids) {
            if (mNativeSessionPtr == 0) {
            if (mNativeSessionPtr == 0) {
+18 −0
Original line number Original line Diff line number Diff line
@@ -68,6 +68,24 @@ public class PerformanceHintManagerTest {
        }
        }
    }
    }


    @Test
    public void testCreateHintSession_noTids() {
        assertThrows(NullPointerException.class, () -> {
            mPerformanceHintManager.createHintSession(
                    null, DEFAULT_TARGET_NS);
        });
        assertThrows(IllegalArgumentException.class, () -> {
            mPerformanceHintManager.createHintSession(
                    new int[]{}, DEFAULT_TARGET_NS);
        });
    }

    @Test
    public void testCreateHintSession_invalidTids() {
        assertNull(mPerformanceHintManager.createHintSession(
                new int[]{-1}, DEFAULT_TARGET_NS));
    }

    @Test
    @Test
    public void testGetPreferredUpdateRateNanos() {
    public void testGetPreferredUpdateRateNanos() {
        if (createSession() != null) {
        if (createSession() != null) {
+21 −9
Original line number Original line Diff line number Diff line
@@ -304,7 +304,8 @@ public final class HintManagerService extends SystemService {
        return mService;
        return mService;
    }
    }


    private boolean checkTidValid(int uid, int tgid, int [] tids) {
    // returns the first invalid tid or null if not found
    private Integer checkTidValid(int uid, int tgid, int [] tids) {
        // Make sure all tids belongs to the same UID (including isolated UID),
        // Make sure all tids belongs to the same UID (including isolated UID),
        // tids can belong to different application processes.
        // tids can belong to different application processes.
        List<Integer> isolatedPids = null;
        List<Integer> isolatedPids = null;
@@ -326,19 +327,24 @@ public final class HintManagerService extends SystemService {
            if (isolatedPids == null) {
            if (isolatedPids == null) {
                // To avoid deadlock, do not call into AMS if the call is from system.
                // To avoid deadlock, do not call into AMS if the call is from system.
                if (uid == Process.SYSTEM_UID) {
                if (uid == Process.SYSTEM_UID) {
                    return false;
                    return threadId;
                }
                }
                isolatedPids = mAmInternal.getIsolatedProcesses(uid);
                isolatedPids = mAmInternal.getIsolatedProcesses(uid);
                if (isolatedPids == null) {
                if (isolatedPids == null) {
                    return false;
                    return threadId;
                }
                }
            }
            }
            if (isolatedPids.contains(pidOfThreadId)) {
            if (isolatedPids.contains(pidOfThreadId)) {
                continue;
                continue;
            }
            }
            return false;
            return threadId;
        }
        }
        return true;
        return null;
    }

    private String formatTidCheckErrMsg(int callingUid, int[] tids, Integer invalidTid) {
        return "Tid" + invalidTid + " from list " + Arrays.toString(tids)
                + " doesn't belong to the calling application" + callingUid;
    }
    }


    @VisibleForTesting
    @VisibleForTesting
@@ -356,8 +362,11 @@ public final class HintManagerService extends SystemService {
            final int callingTgid = Process.getThreadGroupLeader(Binder.getCallingPid());
            final int callingTgid = Process.getThreadGroupLeader(Binder.getCallingPid());
            final long identity = Binder.clearCallingIdentity();
            final long identity = Binder.clearCallingIdentity();
            try {
            try {
                if (!checkTidValid(callingUid, callingTgid, tids)) {
                final Integer invalidTid = checkTidValid(callingUid, callingTgid, tids);
                    throw new SecurityException("Some tid doesn't belong to the application");
                if (invalidTid != null) {
                    final String errMsg = formatTidCheckErrMsg(callingUid, tids, invalidTid);
                    Slogf.w(TAG, errMsg);
                    throw new SecurityException(errMsg);
                }
                }


                long halSessionPtr = mNativeWrapper.halCreateHintSession(callingTgid, callingUid,
                long halSessionPtr = mNativeWrapper.halCreateHintSession(callingTgid, callingUid,
@@ -561,8 +570,11 @@ public final class HintManagerService extends SystemService {
                final int callingTgid = Process.getThreadGroupLeader(Binder.getCallingPid());
                final int callingTgid = Process.getThreadGroupLeader(Binder.getCallingPid());
                final long identity = Binder.clearCallingIdentity();
                final long identity = Binder.clearCallingIdentity();
                try {
                try {
                    if (!checkTidValid(callingUid, callingTgid, tids)) {
                    final Integer invalidTid = checkTidValid(callingUid, callingTgid, tids);
                        throw new SecurityException("Some tid doesn't belong to the application.");
                    if (invalidTid != null) {
                        final String errMsg = formatTidCheckErrMsg(callingUid, tids, invalidTid);
                        Slogf.w(TAG, errMsg);
                        throw new SecurityException(errMsg);
                    }
                    }
                } finally {
                } finally {
                    Binder.restoreCallingIdentity(identity);
                    Binder.restoreCallingIdentity(identity);