diff options
author | George Lee <geolee@google.com> | 2022-10-11 18:09:59 -0700 |
---|---|---|
committer | George Lee <geolee@google.com> | 2022-10-13 09:00:10 -0700 |
commit | 7a231f06777384b6330bda9b393fbe0bce438364 (patch) | |
tree | baa0edb01474d7aa1559c58d3ccb1739e0c71a52 | |
parent | e6e2e2e1ca2ab0dd013772f2554a57e14969c64c (diff) | |
download | pixel-7a231f06777384b6330bda9b393fbe0bce438364.tar.gz |
bcl: Add log check for real brownout.
Adding year to the data capture for timestamp comparison during
lastmeal.txt generation. Add check within Battery Mitigation to confirm
lastmeal.txt contains information related to brownout and only generates
if it does contain information about the brownout.
Bug: 246817058
Test: Confirm proper lastmeal.txt generation
Signed-off-by: George Lee <geolee@google.com>
Change-Id: Ie5efbfbdb86688231728056fa3254d698d63046b
5 files changed, 49 insertions, 6 deletions
diff --git a/battery_mitigation/BatteryMitigation.cpp b/battery_mitigation/BatteryMitigation.cpp index 252b1eec..98419fc5 100644 --- a/battery_mitigation/BatteryMitigation.cpp +++ b/battery_mitigation/BatteryMitigation.cpp @@ -16,6 +16,10 @@ #include <battery_mitigation/BatteryMitigation.h> +#include <sstream> + +#define MAX_BROWNOUT_DATA_AGE_SECONDS 300 + namespace android { namespace hardware { namespace google { @@ -26,6 +30,37 @@ BatteryMitigation::BatteryMitigation(const struct MitigationConfig::Config &cfg) mThermalMgr->updateConfig(cfg); } +bool BatteryMitigation::isMitigationLogTimeValid(std::chrono::system_clock::time_point startTime, + const char *const logFilePath, + const char *const timestampFormat, + const std::regex pattern) { + std::string logFile; + if (!android::base::ReadFileToString(logFilePath, &logFile)) { + return false; + } + std::istringstream content(logFile); + std::string line; + int counter = 0; + std::smatch pattern_match; + while (std::getline(content, line)) { + if (std::regex_match(line, pattern_match, pattern)) { + std::tm triggeredTimestamp = {}; + std::istringstream ss(pattern_match.str()); + ss >> std::get_time(&triggeredTimestamp, timestampFormat); + auto logFileTime = std::chrono::system_clock::from_time_t(mktime(&triggeredTimestamp)); + auto delta = std::chrono::duration_cast<std::chrono::seconds>(startTime - logFileTime); + if ((delta.count() < MAX_BROWNOUT_DATA_AGE_SECONDS) && (delta.count() > 0)) { + return true; + } + } + counter += 1; + if (counter > 5) { + break; + } + } + return false; +} + } // namespace pixel } // namespace google } // namespace hardware diff --git a/battery_mitigation/MitigationThermalManager.cpp b/battery_mitigation/MitigationThermalManager.cpp index 833e294b..c3049173 100644 --- a/battery_mitigation/MitigationThermalManager.cpp +++ b/battery_mitigation/MitigationThermalManager.cpp @@ -15,14 +15,13 @@ */ #define LOG_TAG "mitigation-logger" -#include <battery_mitigation/MitigationThermalManager.h> - #include <android-base/chrono_utils.h> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/parseint.h> #include <android-base/properties.h> #include <android-base/strings.h> +#include <battery_mitigation/MitigationThermalManager.h> #include <errno.h> #include <sys/time.h> @@ -84,6 +83,7 @@ void MitigationThermalManager::updateConfig(const struct MitigationConfig::Confi kSystemPath = cfg.SystemPath; kSystemName = cfg.SystemName; kFilteredZones = cfg.FilteredZones; + kTimestampFormat = cfg.TimestampFormat; } bool MitigationThermalManager::connectThermalHal() { @@ -133,8 +133,9 @@ void MitigationThermalManager::thermalCb(const Temperature &temperature) { std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); struct tm now_tm; localtime_r(&time_sec, &now_tm); - oss << std::put_time(&now_tm, "%m-%d %H:%M:%S.") << std::setw(3) << std::setfill('0') - << ms.count() << std::endl << std::flush; + oss << std::put_time(&now_tm, kTimestampFormat.c_str()) << "." << std::setw(3) + << std::setfill('0') << ms.count() << std::endl + << std::flush; android::base::WriteStringToFd(oss.str(), fd); fsync(fd); oss.str(""); diff --git a/battery_mitigation/include/battery_mitigation/BatteryMitigation.h b/battery_mitigation/include/battery_mitigation/BatteryMitigation.h index 86dda064..d3839699 100644 --- a/battery_mitigation/include/battery_mitigation/BatteryMitigation.h +++ b/battery_mitigation/include/battery_mitigation/BatteryMitigation.h @@ -28,6 +28,9 @@ using ::android::sp; class BatteryMitigation : public RefBase { public: BatteryMitigation(const struct MitigationConfig::Config &cfg); + bool isMitigationLogTimeValid(std::chrono::system_clock::time_point startTime, + const char *const logFilePath, const char *const timestampFormat, + const std::regex pattern); private: MitigationThermalManager *mThermalMgr; diff --git a/battery_mitigation/include/battery_mitigation/MitigationConfig.h b/battery_mitigation/include/battery_mitigation/MitigationConfig.h index 36a4f5e6..825e30c6 100644 --- a/battery_mitigation/include/battery_mitigation/MitigationConfig.h +++ b/battery_mitigation/include/battery_mitigation/MitigationConfig.h @@ -29,6 +29,7 @@ class MitigationConfig { const std::vector<std::string> FilteredZones; const std::vector<std::string> SystemName; const char *const LogFilePath; + const char *const TimestampFormat; }; MitigationConfig(const struct Config &cfg); @@ -38,6 +39,7 @@ class MitigationConfig { const std::vector<std::string> kFilteredZones; const std::vector<std::string> kSystemName; const char *const kLogFilePath; + const char *const kTimestampFormat; }; } // namespace pixel diff --git a/battery_mitigation/include/battery_mitigation/MitigationThermalManager.h b/battery_mitigation/include/battery_mitigation/MitigationThermalManager.h index 5c4ddbe7..688c5d89 100644 --- a/battery_mitigation/include/battery_mitigation/MitigationThermalManager.h +++ b/battery_mitigation/include/battery_mitigation/MitigationThermalManager.h @@ -31,10 +31,11 @@ #include <unistd.h> #include <utils/Mutex.h> -#include "MitigationConfig.h" - #include <fstream> #include <iostream> +#include <regex> + +#include "MitigationConfig.h" namespace android { namespace hardware { @@ -116,6 +117,7 @@ class MitigationThermalManager { std::vector<std::string> kFilteredZones; std::vector<std::string> kSystemName; std::string kLogFilePath; + std::string kTimestampFormat; }; } // namespace pixel |