summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-09-15 22:43:04 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2023-09-15 22:43:04 +0000
commit77fa788a9508169342c96f38f329524850f876ea (patch)
tree71da8b56f2f2882dd1ae9e1eb07e8c8d7200222c
parenta67d3225b1f141e3ac2e0a3f1f9f831f1e89df04 (diff)
parent8d5ec2e59a3fc8141115f7ade65fbad2f66fd457 (diff)
downloadStatsD-android14-gsi.tar.gz
Merge cherrypicks of ['googleplex-android-review.googlesource.com/24184305'] into udc-release. am: 8d5ec2e59aandroid14-gsi
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/modules/StatsD/+/24776832 Change-Id: Idde4399883f66dcc11aa3c33d05c76eb19552192 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--statsd/src/StatsService.cpp23
-rw-r--r--statsd/src/StatsService.h9
2 files changed, 30 insertions, 2 deletions
diff --git a/statsd/src/StatsService.cpp b/statsd/src/StatsService.cpp
index f1d48a1a..c34964dd 100644
--- a/statsd/src/StatsService.cpp
+++ b/statsd/src/StatsService.cpp
@@ -230,12 +230,15 @@ StatsService::StatsService(const sp<UidMap>& uidMap, shared_ptr<LogEventQueue> q
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. */
@@ -244,6 +247,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.
@@ -1502,6 +1512,15 @@ void StatsService::initShellSubscriber() {
}
}
+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 4d05cc1c..da1b4ab5 100644
--- a/statsd/src/StatsService.h
+++ b/statsd/src/StatsService.h
@@ -409,6 +409,13 @@ private:
void onStatsdInitCompleted();
/**
+ * 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;
@@ -452,6 +459,8 @@ private:
shared_ptr<LogEventQueue> mEventQueue;
std::shared_ptr<LogEventFilter> mLogEventFilter;
+ std::unique_ptr<std::thread> mLogsReaderThread;
+
MultiConditionTrigger mBootCompleteTrigger;
static const inline string kBootCompleteTag = "BOOT_COMPLETE";
static const inline string kUidMapReceivedTag = "UID_MAP";