Only set SCHED_RESET_ON_FORK flag if the thread is using the default scheduling policy.

Keep nice and policy same as well to reduce the overhead in syscall.


Bug: 370988407
Change-Id: I4d9ef914905da6807339cf2c55650b8188787b43
Test: Build
Flag: com.android.server.power.hint.reset_on_fork_enabled
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 c969eff..2c0ce25 100644
--- a/services/core/java/com/android/server/power/hint/HintManagerService.java
+++ b/services/core/java/com/android/server/power/hint/HintManagerService.java
@@ -1059,8 +1059,22 @@
                     throw new SecurityException(errMsg);
                 }
                 if (resetOnForkEnabled()){
-                    for (int tid : tids) {
-                        Process.setThreadScheduler(tid, Process.SCHED_RESET_ON_FORK, 0);
+                    try {
+                        for (int tid : tids) {
+                            int policy = Process.getThreadScheduler(tid);
+                            // If the thread is not using the default scheduling policy (SCHED_OTHER),
+                            // we don't change it.
+                            if (policy != Process.SCHED_OTHER) {
+                                continue;
+                            }
+                            // set the SCHED_RESET_ON_FORK flag.
+                            int prio = Process.getThreadPriority(tid);
+                            Process.setThreadScheduler(tid, Process.SCHED_OTHER | Process.SCHED_RESET_ON_FORK, 0);
+                            Process.setThreadPriority(tid, prio);
+                        }
+                    } catch (Exception e) {
+                        Slog.e(TAG, "Failed to set SCHED_RESET_ON_FORK for tids "
+                                + Arrays.toString(tids), e);
                     }
                 }
 
@@ -1454,8 +1468,22 @@
                             throw new SecurityException(errMsg);
                         }
                         if (resetOnForkEnabled()){
-                            for (int tid : tids) {
-                                Process.setThreadScheduler(tid, Process.SCHED_RESET_ON_FORK, 0);
+                            try {
+                                for (int tid : tids) {
+                                    int policy = Process.getThreadScheduler(tid);
+                                    // If the thread is not using the default scheduling policy (SCHED_OTHER),
+                                    // we don't change it.
+                                    if (policy != Process.SCHED_OTHER) {
+                                        continue;
+                                    }
+                                    // set the SCHED_RESET_ON_FORK flag.
+                                    int prio = Process.getThreadPriority(tid);
+                                    Process.setThreadScheduler(tid, Process.SCHED_OTHER | Process.SCHED_RESET_ON_FORK, 0);
+                                    Process.setThreadPriority(tid, prio);
+                                }
+                            } catch (Exception e) {
+                                Slog.e(TAG, "Failed to set SCHED_RESET_ON_FORK for tids "
+                                        + Arrays.toString(tids), e);
                             }
                         }
                         if (powerhintThreadCleanup()) {