Add PID tunables for sample window

Bug: 191480755
Test: Build
Signed-off-by: Wei Wang <wvw@google.com>
Change-Id: Id559796ee4a423410148b8c2df0524909658af82
diff --git a/aidl/power-libperfmgr/PowerHintSession.cpp b/aidl/power-libperfmgr/PowerHintSession.cpp
index 23a4a10..4d84353 100644
--- a/aidl/power-libperfmgr/PowerHintSession.cpp
+++ b/aidl/power-libperfmgr/PowerHintSession.cpp
@@ -21,12 +21,10 @@
 #include <android-base/parsedouble.h>
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
-#include <log/log.h>
+#include <sys/syscall.h>
 #include <time.h>
 #include <utils/Trace.h>
 
-#include <sys/syscall.h>
-
 #include "PowerHintSession.h"
 #include "PowerSessionManager.h"
 
@@ -52,7 +50,9 @@
 constexpr char kPowerHalAdpfUclampBoostCap[] = "vendor.powerhal.adpf.uclamp.boost_cap";
 constexpr char kPowerHalAdpfUclampGranularity[] = "vendor.powerhal.adpf.uclamp.granularity";
 constexpr char kPowerHalAdpfStaleTimeFactor[] = "vendor.powerhal.adpf.stale_timeout_factor";
-constexpr char kPowerHalAdpfSamplingWindow[] = "vendor.powerhal.adpf.sampling_window";
+constexpr char kPowerHalAdpfPSamplingWindow[] = "vendor.powerhal.adpf.p.window";
+constexpr char kPowerHalAdpfISamplingWindow[] = "vendor.powerhal.adpf.i.window";
+constexpr char kPowerHalAdpfDSamplingWindow[] = "vendor.powerhal.adpf.d.window";
 
 namespace {
 /* there is no glibc or bionic wrapper */
@@ -105,14 +105,18 @@
                      : std::abs(static_cast<int64_t>(::android::base::GetIntProperty<int64_t>(
                                                              kPowerHalAdpfPidIClamp, 512) /
                                                      sPidI));
-static const int sUclampCap =
-        ::android::base::GetIntProperty<int>(kPowerHalAdpfUclampBoostCap, 512);
+static const int32_t sUclampCap =
+        ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampBoostCap, 512);
 static const uint32_t sUclampGranularity =
         ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampGranularity, 5);
 static const int64_t sStaleTimeFactor =
-        ::android::base::GetIntProperty<int64_t>(kPowerHalAdpfStaleTimeFactor, 20);
-static const size_t sSamplingWindow =
-        ::android::base::GetUintProperty<size_t>(kPowerHalAdpfSamplingWindow, 0);
+        ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfStaleTimeFactor, 20);
+static const int64_t sPSamplingWindow =
+        ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfPSamplingWindow, 1);
+static const int64_t sISamplingWindow =
+        ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfISamplingWindow, 0);
+static const int64_t sDSamplingWindow =
+        ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfDSamplingWindow, 1);
 
 }  // namespace
 
@@ -270,12 +274,17 @@
         mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
     }
     int64_t targetDurationNanos = (int64_t)mDescriptor->duration.count();
-    size_t length = actualDurations.size();
-    size_t start = sSamplingWindow == 0 || sSamplingWindow > length ? 0 : length - sSamplingWindow;
+    int64_t length = actualDurations.size();
+    int64_t p_start =
+            sPSamplingWindow == 0 || sPSamplingWindow > length ? 0 : length - sPSamplingWindow;
+    int64_t i_start =
+            sISamplingWindow == 0 || sISamplingWindow > length ? 0 : length - sISamplingWindow;
+    int64_t d_start =
+            sDSamplingWindow == 0 || sDSamplingWindow > length ? 0 : length - sDSamplingWindow;
     int64_t dt = ns_to_100us(targetDurationNanos);
     int64_t err_sum = 0;
     int64_t derivative_sum = 0;
-    for (size_t i = start; i < length; i++) {
+    for (int64_t i = std::min({p_start, i_start, d_start}); i < length; i++) {
         int64_t actualDurationNanos = actualDurations[i].durationNanos;
         if (std::abs(actualDurationNanos) > targetDurationNanos * 20) {
             ALOGW("The actual duration is way far from the target (%" PRId64 " >> %" PRId64 ")",
@@ -284,16 +293,22 @@
         // PID control algorithm
         int64_t error = ns_to_100us(actualDurationNanos - targetDurationNanos) +
                         static_cast<int64_t>(sPidOffset);
-        derivative_sum += error - mDescriptor->previous_error;
-        err_sum += error;
+        if (i >= d_start) {
+            derivative_sum += error - mDescriptor->previous_error;
+        }
+        if (i >= p_start) {
+            err_sum += error;
+        }
+        if (i >= i_start) {
+            mDescriptor->integral_error = mDescriptor->integral_error + error * dt;
+            mDescriptor->integral_error = std::min(sPidIClamp, mDescriptor->integral_error);
+            mDescriptor->integral_error = std::max(-sPidIClamp, mDescriptor->integral_error);
+        }
         mDescriptor->previous_error = error;
-        mDescriptor->integral_error = mDescriptor->integral_error + error * dt;
-        mDescriptor->integral_error = std::min(sPidIClamp, mDescriptor->integral_error);
-        mDescriptor->integral_error = std::max(-sPidIClamp, mDescriptor->integral_error);
     }
-    int64_t pOut = static_cast<int64_t>(sPidP * err_sum / (length - start));
+    int64_t pOut = static_cast<int64_t>(sPidP * err_sum / (length - p_start));
     int64_t iOut = static_cast<int64_t>(sPidI * mDescriptor->integral_error);
-    int64_t dOut = static_cast<int64_t>(sPidD * derivative_sum / dt / (length - start));
+    int64_t dOut = static_cast<int64_t>(sPidD * derivative_sum / dt / (length - d_start));
 
     int64_t output = pOut + iOut + dOut;
     if (ATRACE_ENABLED()) {