Snap for 11219529 from 1af36ac6f10b847787da90b7f9489d9d3fbf8798 to mainline-tzdata4-release

Change-Id: I70ec9f8bca44298eee6185000d5516bddf407852
diff --git a/statsd/src/StatsService.cpp b/statsd/src/StatsService.cpp
index 5622949..302b81b 100644
--- a/statsd/src/StatsService.cpp
+++ b/statsd/src/StatsService.cpp
@@ -168,12 +168,15 @@
     init_system_properties();
 
     if (mEventQueue != nullptr) {
-        std::thread pushedEventThread([this] { readLogs(); });
-        pushedEventThread.detach();
+        mLogsReaderThread = std::make_unique<std::thread>([this] { readLogs(); });
     }
 }
 
 StatsService::~StatsService() {
+    if (mEventQueue != nullptr) {
+        stopReadingLogs();
+        mLogsReaderThread->join();
+    }
 }
 
 /* Runs on a dedicated thread to process pushed events. */
@@ -182,6 +185,13 @@
     while (1) {
         // Block until an event is available.
         auto event = mEventQueue->waitPop();
+
+        // Below flag will be set when statsd is exiting and log event will be pushed to break
+        // out of waitPop.
+        if (mIsStopRequested) {
+            break;
+        }
+
         // Pass it to StatsLogProcess to all configs/metrics
         // At this point, the LogEventQueue is not blocked, so that the socketListener
         // can read events from the socket and write to buffer to avoid data drop.
@@ -1346,6 +1356,15 @@
     mPullerManager->SetStatsCompanionService(nullptr);
 }
 
+void StatsService::stopReadingLogs() {
+    mIsStopRequested = true;
+    // Push this event so that readLogs will process and break out of the loop
+    // after the stop is requested.
+    int64_t timeStamp;
+    std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
+    mEventQueue->push(std::move(logEvent), &timeStamp);
+}
+
 }  // namespace statsd
 }  // namespace os
 }  // namespace android
diff --git a/statsd/src/StatsService.h b/statsd/src/StatsService.h
index 95ee8cc..f8fe03c 100644
--- a/statsd/src/StatsService.h
+++ b/statsd/src/StatsService.h
@@ -348,6 +348,13 @@
     void statsCompanionServiceDiedImpl();
 
     /**
+     *  This method is used to stop log reader thread.
+     */
+    void stopReadingLogs();
+
+    std::atomic<bool> mIsStopRequested = false;
+
+    /**
      * Tracks the uid <--> package name mapping.
      */
     const sp<UidMap> mUidMap;
@@ -390,6 +397,8 @@
     mutable mutex mShellSubscriberMutex;
     shared_ptr<LogEventQueue> mEventQueue;
 
+    std::unique_ptr<std::thread> mLogsReaderThread;
+
     MultiConditionTrigger mBootCompleteTrigger;
     static const inline string kBootCompleteTag = "BOOT_COMPLETE";
     static const inline string kUidMapReceivedTag = "UID_MAP";
diff --git a/tests/src/android/cts/statsd/metric/MetricActivationTests.java b/tests/src/android/cts/statsd/metric/MetricActivationTests.java
index 8e796cf..a95cb1b 100644
--- a/tests/src/android/cts/statsd/metric/MetricActivationTests.java
+++ b/tests/src/android/cts/statsd/metric/MetricActivationTests.java
@@ -494,7 +494,7 @@
         // Metric 2 Activation 1: 0 seconds
         // Metric 2 Activation 2: 0 seconds
         rebootDeviceAndWaitUntilReady();
-        Thread.sleep(3_000L);
+        Thread.sleep(10_000L);
 
         // Metric 1 event ignored.
         // Metric 2 event ignored.
diff --git a/tests/src/android/cts/statsd/validation/ProcStatsValidationTests.java b/tests/src/android/cts/statsd/validation/ProcStatsValidationTests.java
index 5b42aa9..b5baa36 100644
--- a/tests/src/android/cts/statsd/validation/ProcStatsValidationTests.java
+++ b/tests/src/android/cts/statsd/validation/ProcStatsValidationTests.java
@@ -30,6 +30,7 @@
 import com.android.os.StatsLog.DimensionsValue;
 import com.android.os.StatsLog.ValueBucketInfo;
 import com.android.os.StatsLog.ValueMetricData;
+import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.log.LogUtil;
 
 import java.util.List;
@@ -44,6 +45,10 @@
     private static final int EXTRA_WAIT_TIME_MS = 1_000; // as buffer when proc state changing.
 
     public void testProcessStatePssValue() throws Exception {
+        if(isPssProfilingDisabled())  {
+            LogUtil.CLog.i("testProcessStatePssValue is ignored when PSS profiling is disabled");
+            return;
+        }
         final String fileName = "PROCSTATSQ_PROCS_STATE_PSS_VALUE.pbtxt";
         StatsdConfig config = createValidationUtil().getConfig(fileName);
         LogUtil.CLog.d("Updating the following config:\n" + config.toString());
@@ -303,4 +308,13 @@
         assertThat(rssAvgStatsd).isEqualTo(rssAvgProcstats);
         */
     }
+
+    private boolean isPssProfilingDisabled() throws Exception {
+        ITestDevice device = getDevice();
+        final String disablePssProfilingKey = "disable_app_profiler_pss_profiling";
+        final String stringToCompare = " " + disablePssProfilingKey + "=true";
+
+        final String dumpsys = device.executeShellCommand("dumpsys activity settings");
+        return (dumpsys.contains(stringToCompare));
+    }
 }