diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-09-07 21:56:07 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-09-07 21:56:07 +0000 |
commit | 44e4f8a6fcee183fc1c254387c44528b57afa209 (patch) | |
tree | ee4a370faf6f44bca828c4069bdde9c9bc949b37 | |
parent | 49de4eafacc0750afaab2581846a354e420b8715 (diff) | |
parent | e4cd2d8f75d1b7b83a759d752f38099a9aa9997e (diff) | |
download | StatsD-44e4f8a6fcee183fc1c254387c44528b57afa209.tar.gz |
Merge cherrypicks of ['googleplex-android-review.googlesource.com/24183698'] into security-aosp-tm-release.android-security-13.0.0_r17android-security-13.0.0_r16android-security-13.0.0_r15android-security-13.0.0_r14android-security-13.0.0_r13android-security-13.0.0_r12android-security-13.0.0_r11android13-security-release
Change-Id: I2b9b01a2ad5d784478d6f6e86a4eb926099c831b
-rw-r--r-- | statsd/src/StatsService.cpp | 23 | ||||
-rw-r--r-- | statsd/src/StatsService.h | 9 |
2 files changed, 30 insertions, 2 deletions
diff --git a/statsd/src/StatsService.cpp b/statsd/src/StatsService.cpp index ef21a041..c0c10764 100644 --- a/statsd/src/StatsService.cpp +++ b/statsd/src/StatsService.cpp @@ -170,12 +170,15 @@ StatsService::StatsService(const sp<Looper>& handlerLooper, shared_ptr<LogEventQ 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. */ @@ -184,6 +187,13 @@ void StatsService::readLogs() { 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. @@ -1349,6 +1359,15 @@ void StatsService::statsCompanionServiceDiedImpl() { 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 790397f0..39c9a9fd 100644 --- a/statsd/src/StatsService.h +++ b/statsd/src/StatsService.h @@ -347,6 +347,13 @@ private: void statsCompanionServiceDiedImpl(); /** + * This method is used to stop log reader thread. + */ + void stopReadingLogs(); + + std::atomic<bool> mIsStopRequested = false; + + /** * Tracks the uid <--> package name mapping. */ sp<UidMap> mUidMap; @@ -389,6 +396,8 @@ private: mutable mutex mShellSubscriberMutex; std::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"; |