summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsamou <samou@google.com>2024-01-10 11:43:44 +0000
committersamou <samou@google.com>2024-01-10 11:43:44 +0000
commit9fff0bac1f4a4344915d7666b95e7febbbd3fa00 (patch)
tree8d9cce7f4bb0e8c06d37a4df05e9ab2b82ea4c47
parent040b51f835c50feec538fed40d136a20a702ac04 (diff)
downloadpixel-9fff0bac1f4a4344915d7666b95e7febbbd3fa00.tar.gz
bm: fix thread teardown function
- running teardown function only when thread is running - add thread start/stop log Bug: 316079843 Change-Id: Id3519e5eaff4818e6b2bf06426fb390064958373 Signed-off-by: samou <samou@google.com>
-rw-r--r--battery_mitigation/BatteryMitigationService.cpp47
-rw-r--r--battery_mitigation/include/battery_mitigation/BatteryMitigationService.h7
2 files changed, 41 insertions, 13 deletions
diff --git a/battery_mitigation/BatteryMitigationService.cpp b/battery_mitigation/BatteryMitigationService.cpp
index 121c85ac..ce5f6402 100644
--- a/battery_mitigation/BatteryMitigationService.cpp
+++ b/battery_mitigation/BatteryMitigationService.cpp
@@ -51,10 +51,10 @@ BatteryMitigationService::BatteryMitigationService(
}
BatteryMitigationService::~BatteryMitigationService() {
- stopEventThread(threadStop, wakeupEventFd, brownoutEventThread);
- tearDownBrownoutEventThread();
- stopEventThread(triggerThreadStop, triggeredStateWakeupEventFd, eventThread);
- tearDownTriggerEventThread();
+ stopEventThread(threadStop, wakeupEventFd, brownoutEventThread,
+ &BatteryMitigationService::tearDownBrownoutEventThread);
+ stopEventThread(triggerThreadStop, triggeredStateWakeupEventFd, eventThread,
+ &BatteryMitigationService::tearDownTriggerEventThread);
}
bool readSysfsToInt(const std::string &path, int *val) {
@@ -99,18 +99,32 @@ bool WriteStringToFile(const std::string& content, const char *path) {
}
bool BatteryMitigationService::isBrownoutStatsBinarySupported() {
- int isEnabled;
if (access(cfg.TriggeredIdxPath, F_OK) == 0 &&
access(cfg.BrownoutStatsPath, F_OK) == 0 &&
access(cfg.BrownoutStatsEnablePath, F_OK) == 0) {
- if (!WriteStringToFile("1", cfg.BrownoutStatsEnablePath)) {
+ return true;
+ }
+ return false;
+}
+
+bool BatteryMitigationService::enableBrownoutStatsBinary(bool enable) {
+ int isEnabled;
+ if (isBrownoutStatsBinarySupported()) {
+ if (!WriteStringToFile(enable ? "1" : "0", cfg.BrownoutStatsEnablePath)) {
return false;
}
if (!readSysfsToInt(cfg.BrownoutStatsEnablePath, &isEnabled)) {
return false;
}
if (isEnabled) {
+ LOG(INFO) << "br_stats is on";
+ } else {
+ LOG(INFO) << "br_stats is off";
+ }
+ if (isEnabled == enable) {
return true;
+ } else {
+ return false;
}
}
return false;
@@ -193,16 +207,18 @@ int BatteryMitigationService::readNumericStats(struct BrownoutStatsExtend *brown
return i;
}
-
void BatteryMitigationService::startBrownoutEventThread() {
- if (isBrownoutStatsBinarySupported()) {
+ if (isBrownoutStatsBinarySupported() && enableBrownoutStatsBinary(true)) {
+ threadStop.store(false);
brownoutEventThread = std::thread(&BatteryMitigationService::BrownoutEventThread, this);
+ triggerThreadStop.store(false);
eventThread = std::thread(&BatteryMitigationService::TriggerEventThread, this);
}
}
void BatteryMitigationService::stopEventThread(std::atomic_bool &thread_stop, int wakeup_event_fd,
- std::thread &event_thread) {
+ std::thread &event_thread,
+ void (BatteryMitigationService::*tearDown)()) {
if (!thread_stop.load()) {
thread_stop.store(true);
uint64_t flag = 1;
@@ -212,6 +228,7 @@ void BatteryMitigationService::stopEventThread(std::atomic_bool &thread_stop, in
if (event_thread.joinable()) {
event_thread.join();
}
+ (this->*tearDown)();
}
}
@@ -303,6 +320,10 @@ void BatteryMitigationService::TriggerEventThread() {
read(triggeredStateFd[idx], buf, BUF_SIZE);
}
+ if (!triggerThreadStop.load()) {
+ LOG(INFO) << "Start TriggerEventThread";
+ }
+
while (!triggerThreadStop.load()) {
requestedFd = epoll_wait(triggeredStateEpollFd, events, EPOLL_MAXEVENTS, -1);
if (requestedFd <= 0) {
@@ -333,6 +354,7 @@ void BatteryMitigationService::TriggerEventThread() {
/* b/299700579 handle wakeupEvent here if we need to do something after this loop */
}
}
+ LOG(INFO) << "Stop TriggerEventThread";
}
int BatteryMitigationService::initFd() {
@@ -408,6 +430,10 @@ void BatteryMitigationService::BrownoutEventThread() {
/* allow epoll_wait sleep in the first loop */
read(triggeredIdxFd, buf, BUF_SIZE);
+ if (!threadStop.load()) {
+ LOG(INFO) << "Start BrownoutEventThread";
+ }
+
while (!threadStop.load()) {
requestedFd = epoll_wait(triggeredIdxEpollFd, events, EPOLL_MAXEVENTS, -1);
if (requestedFd <= 0) {
@@ -471,6 +497,7 @@ void BatteryMitigationService::BrownoutEventThread() {
brownoutEventCounter = 0;
}
}
+ LOG(INFO) << "Stop BrownoutEventThread";
}
void readLPFPowerBitResolutions(const char *odpmDir, double *bitResolutions) {
@@ -549,7 +576,7 @@ void BatteryMitigationService::tearDownBrownoutEventThread() {
freeLpfChannelNames(mainLpfChannelNames);
freeLpfChannelNames(subLpfChannelNames);
/* disable br_stats */
- if (!WriteStringToFile("0", cfg.BrownoutStatsEnablePath)) {
+ if (!enableBrownoutStatsBinary(false)) {
LOG(ERROR) << "failed to disable br_stats";
}
}
diff --git a/battery_mitigation/include/battery_mitigation/BatteryMitigationService.h b/battery_mitigation/include/battery_mitigation/BatteryMitigationService.h
index 9c697140..b21fbd0a 100644
--- a/battery_mitigation/include/battery_mitigation/BatteryMitigationService.h
+++ b/battery_mitigation/include/battery_mitigation/BatteryMitigationService.h
@@ -122,8 +122,9 @@ class BatteryMitigationService : public RefBase {
void startBrownoutEventThread();
void stopEventThread(std::atomic_bool &thread_stop, int wakeup_event_fd,
- std::thread &event_thread);
+ std::thread &event_thread, void (BatteryMitigationService::*tearDown)());
bool isBrownoutStatsBinarySupported();
+ bool enableBrownoutStatsBinary(bool);
bool isPlatformSupported();
bool isTimeValid(const char*, std::chrono::system_clock::time_point);
bool genParsedMeal(const char*);
@@ -136,13 +137,13 @@ class BatteryMitigationService : public RefBase {
int triggeredStateEpollFd;
int triggeredStateWakeupEventFd;
std::thread eventThread;
- std::atomic_bool triggerThreadStop{false};
+ std::atomic_bool triggerThreadStop{true};
int brownoutStatsFd;
int triggeredIdxFd;
int triggeredIdxEpollFd;
int wakeupEventFd;
std::thread brownoutEventThread;
- std::atomic_bool threadStop{false};
+ std::atomic_bool threadStop{true};
int mainPmicID;
int subPmicID;