use mallopt(M_DECAY_TIME, 1) for camera HAL

saves 5%+ of all cycles in camera HAL

Test: simpleperf of camera HAL
Bug: 160883639

Change-Id: I7d57c21fd7423453dba923b2b88f06b2dab4ad44
diff --git a/common/hal/hidl_service/hidl_camera_device_session.cc b/common/hal/hidl_service/hidl_camera_device_session.cc
index 0b3aede..0179805 100644
--- a/common/hal/hidl_service/hidl_camera_device_session.cc
+++ b/common/hal/hidl_service/hidl_camera_device_session.cc
@@ -15,10 +15,13 @@
  */
 
 #define LOG_TAG "GCH_HidlCameraDeviceSession"
+#define ATRACE_TAG ATRACE_TAG_CAMERA
 //#define LOG_NDEBUG 0
 #include <log/log.h>
 
 #include <cutils/properties.h>
+#include <cutils/trace.h>
+#include <malloc.h>
 
 #include "hidl_camera_device_session.h"
 #include "hidl_profiler.h"
@@ -68,6 +71,8 @@
 
 HidlCameraDeviceSession::~HidlCameraDeviceSession() {
   close();
+  // camera's closing, so flush any unused malloc pages
+  mallopt(M_PURGE, 0);
 }
 
 void HidlCameraDeviceSession::ProcessCaptureResult(
@@ -661,7 +666,6 @@
     auto profiler_item = hidl_profiler::OnCameraClose();
     device_session_ = nullptr;
   }
-
   return Void();
 }
 
diff --git a/common/hal/hidl_service/hidl_camera_provider.cc b/common/hal/hidl_service/hidl_camera_provider.cc
index a7ce5e2..7193a26 100644
--- a/common/hal/hidl_service/hidl_camera_provider.cc
+++ b/common/hal/hidl_service/hidl_camera_provider.cc
@@ -157,6 +157,8 @@
   };
 
   google_camera_provider_->SetCallback(&camera_provider_callback_);
+  // purge pending malloc pages after initialization
+  mallopt(M_PURGE, 0);
   return OK;
 }
 
diff --git a/common/hal/hidl_service/service.cc b/common/hal/hidl_service/service.cc
index 48217b7..2b203d7 100644
--- a/common/hal/hidl_service/service.cc
+++ b/common/hal/hidl_service/service.cc
@@ -23,6 +23,7 @@
 #include <android/hardware/camera/provider/2.6/ICameraProvider.h>
 #include <binder/ProcessState.h>
 #include <hidl/LegacySupport.h>
+#include <malloc.h>
 
 using android::hardware::defaultLazyPassthroughServiceImplementation;
 using android::hardware::defaultPassthroughServiceImplementation;
@@ -38,6 +39,7 @@
   ALOGI("Google camera provider service is starting.");
   // The camera HAL may communicate to other vendor components via
   // /dev/vndbinder
+  mallopt(M_DECAY_TIME, 1);
   android::ProcessState::initWithDriver("/dev/vndbinder");
   int res;
   if (kLazyService) {