diff options
author | Lakshman Annadorai <lakshmana@google.com> | 2021-04-30 08:29:10 -0700 |
---|---|---|
committer | Lakshman Annadorai <lakshmana@google.com> | 2021-05-12 12:02:34 -0700 |
commit | ec4d08a77ebcea133f647e34af4236801ad3baad (patch) | |
tree | ed0f960f5ae13809d2ccbb19c6fc346cdf0723ac /cpp/watchdog | |
parent | b21cc31474add7b10dd252397cb08ff597f36ea3 (diff) | |
download | Car-ec4d08a77ebcea133f647e34af4236801ad3baad.tar.gz |
Add dumpsys flag to reset resource overuse stats.
Command:
adb dumpsys android.automotive.watchdog.ICarWatchdog/default
--reset_resource_overuse_stats packageName,packageName
Test: atest libwatchdog_test CarWatchdogServiceUnitTest
Bug: 186045281
Change-Id: Ie674ae0cb6b828434c37311c2f8175de209f429e
Diffstat (limited to 'cpp/watchdog')
24 files changed, 312 insertions, 157 deletions
diff --git a/cpp/watchdog/aidl/android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.aidl b/cpp/watchdog/aidl/android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.aidl index ef68dbcec7..c0cfd23444 100644 --- a/cpp/watchdog/aidl/android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.aidl +++ b/cpp/watchdog/aidl/android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.aidl @@ -62,4 +62,11 @@ interface ICarWatchdogServiceForSystem { * recent collection. */ oneway void latestIoOveruseStats(in List<PackageIoOveruseStats> packageIoOveruseStats); + + /** + * Resets resource overuse stats on the watchdog server side. + * + * @param packageNames Package names for which to reset the stats. + */ + oneway void resetResourceOveruseStats(in @utf8InCpp List<String> packageNames); } diff --git a/cpp/watchdog/server/src/IoOveruseMonitor.cpp b/cpp/watchdog/server/src/IoOveruseMonitor.cpp index c724d57f83..ddc606bc73 100644 --- a/cpp/watchdog/server/src/IoOveruseMonitor.cpp +++ b/cpp/watchdog/server/src/IoOveruseMonitor.cpp @@ -22,6 +22,7 @@ #include "PackageInfoResolver.h" #include <WatchdogProperties.sysprop.h> +#include <android-base/file.h> #include <android/automotive/watchdog/internal/PackageIdentifier.h> #include <android/automotive/watchdog/internal/UidType.h> #include <binder/IPCThreadState.h> @@ -45,6 +46,7 @@ using ::android::automotive::watchdog::internal::ResourceOveruseConfiguration; using ::android::automotive::watchdog::internal::UidType; using ::android::base::Error; using ::android::base::Result; +using ::android::base::WriteStringToFd; using ::android::binder::Status; // Minimum written bytes to sync the stats with the Watchdog service. @@ -53,6 +55,10 @@ constexpr int64_t kMinSyncWrittenBytes = 100 * 1024; constexpr double kDefaultIoOveruseWarnPercentage = 80; // Maximum numer of system-wide stats (from periodic monitoring) to cache. constexpr size_t kMaxPeriodicMonitorBufferSize = 1000; +constexpr const char* kHelpText = + "\n%s dump options:\n" + "%s <package name>, <package name>,...: Reset resource overuse stats for the given package " + "names. Value for this flag is a comma-separated value containing package names.\n"; namespace { @@ -101,7 +107,7 @@ std::tuple<int64_t, int64_t> calculateStartAndDuration(const time_t& currentTime } IoOveruseMonitor::IoOveruseMonitor( - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper) : + const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper) : mMinSyncWrittenBytes(kMinSyncWrittenBytes), mWatchdogServiceHelper(watchdogServiceHelper), mSystemWideWrittenBytes({}), @@ -310,7 +316,8 @@ Result<void> IoOveruseMonitor::onPeriodicCollection( if (const auto status = mWatchdogServiceHelper->latestIoOveruseStats(mLatestIoOveruseStats); !status.isOk()) { // Don't clear the cache as it can be pushed again on the next collection. - ALOGW("Failed to push the latest I/O overuse stats to watchdog service"); + ALOGW("Failed to push the latest I/O overuse stats to watchdog service: %s", + status.toString8().c_str()); } else { mLatestIoOveruseStats.clear(); if (DEBUG) { @@ -394,6 +401,11 @@ Result<void> IoOveruseMonitor::onDump([[maybe_unused]] int fd) { return {}; } +bool IoOveruseMonitor::dumpHelpText(int fd) { + return WriteStringToFd(StringPrintf(kHelpText, name().c_str(), kResetResourceOveruseStatsFlag), + fd); +} + void IoOveruseMonitor::notifyNativePackagesLocked( const std::unordered_map<uid_t, IoOveruseStats>& statsByUid) { for (const auto& [uid, ioOveruseStats] : statsByUid) { @@ -521,6 +533,23 @@ Result<void> IoOveruseMonitor::getIoOveruseStats(IoOveruseStats* ioOveruseStats) return {}; } +Result<void> IoOveruseMonitor::resetIoOveruseStats(const std::vector<std::string>& packageNames) { + if (const auto status = mWatchdogServiceHelper->resetResourceOveruseStats(packageNames); + !status.isOk()) { + return Error() << "Failed to reset stats in watchdog service: " << status.toString8(); + } + std::unordered_set<std::string> uniquePackageNames; + std::copy(packageNames.begin(), packageNames.end(), + std::inserter(uniquePackageNames, uniquePackageNames.end())); + for (auto& [key, usage] : mUserPackageDailyIoUsageById) { + if (uniquePackageNames.find(usage.packageInfo.packageIdentifier.name) != + uniquePackageNames.end()) { + usage.resetStats(); + } + } + return {}; +} + void IoOveruseMonitor::handleBinderDeath(const wp<IBinder>& who) { std::unique_lock writeLock(mRwMutex); IBinder* binder = who.unsafe_get(); @@ -582,6 +611,14 @@ const std::string IoOveruseMonitor::UserPackageIoUsage::id() const { return uniquePackageIdStr(packageInfo.packageIdentifier); } +void IoOveruseMonitor::UserPackageIoUsage::resetStats() { + writtenBytes = {}; + forgivenWriteBytes = {}; + totalOveruses = 0; + isPackageWarned = false; + lastSyncedWrittenBytes = 0; +} + } // namespace watchdog } // namespace automotive } // namespace android diff --git a/cpp/watchdog/server/src/IoOveruseMonitor.h b/cpp/watchdog/server/src/IoOveruseMonitor.h index 70403532a9..e749eec1c0 100644 --- a/cpp/watchdog/server/src/IoOveruseMonitor.h +++ b/cpp/watchdog/server/src/IoOveruseMonitor.h @@ -49,6 +49,8 @@ namespace watchdog { // Number of periodically monitored stats to cache in memory. constexpr int32_t kDefaultPeriodicMonitorBufferSize = 360; +// Dumpsys flags. +constexpr const char* kResetResourceOveruseStatsFlag = "--reset_resource_overuse_stats"; // Forward declaration for testing use only. namespace internal { @@ -69,6 +71,9 @@ public: // Returns whether or not the monitor is initialized. virtual bool isInitialized() = 0; + // Dumps the help text. + virtual bool dumpHelpText(int fd) = 0; + // Below API is from internal/ICarWatchdog.aidl. Please refer to the AIDL for description. virtual android::base::Result<void> updateResourceOveruseConfigurations( const std::vector< @@ -90,12 +95,14 @@ public: const sp<IResourceOveruseListener>& listener) = 0; virtual android::base::Result<void> getIoOveruseStats(IoOveruseStats* ioOveruseStats) = 0; + + virtual android::base::Result<void> resetIoOveruseStats( + const std::vector<std::string>& packageNames) = 0; }; class IoOveruseMonitor final : public IIoOveruseMonitor { public: - explicit IoOveruseMonitor( - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper); + explicit IoOveruseMonitor(const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper); ~IoOveruseMonitor() { terminate(); } @@ -140,6 +147,8 @@ public: android::base::Result<void> onDump(int fd); + bool dumpHelpText(int fd); + android::base::Result<void> onCustomCollectionDump(int /*fd*/) { // No special processing for custom collection. Thus no custom collection dump. return {}; @@ -167,6 +176,8 @@ public: android::base::Result<void> getIoOveruseStats(IoOveruseStats* ioOveruseStats); + android::base::Result<void> resetIoOveruseStats(const std::vector<std::string>& packageName); + protected: android::base::Result<void> init(); @@ -191,6 +202,7 @@ private: UserPackageIoUsage& operator+=(const UserPackageIoUsage& r); const std::string id() const; + void resetStats(); }; class BinderDeathRecipient final : public android::IBinder::DeathRecipient { @@ -221,7 +233,7 @@ private: sp<IPackageInfoResolver> mPackageInfoResolver; // Minimum written bytes to sync the stats with the Watchdog service. double mMinSyncWrittenBytes; - android::sp<IWatchdogServiceHelperInterface> mWatchdogServiceHelper; + android::sp<IWatchdogServiceHelper> mWatchdogServiceHelper; // Makes sure only one collection is running at any given time. mutable std::shared_mutex mRwMutex; @@ -237,7 +249,7 @@ private: size_t mPeriodicMonitorBufferSize GUARDED_BY(mRwMutex); time_t mLastSystemWideIoMonitorTime GUARDED_BY(mRwMutex); - // Cache of per user package I/O usage. + // Cache of per user package I/O usage. Key is a unique ID with the format `packageName:userId`. std::unordered_map<std::string, UserPackageIoUsage> mUserPackageDailyIoUsageById GUARDED_BY(mRwMutex); double mIoOveruseWarnPercentage GUARDED_BY(mRwMutex); diff --git a/cpp/watchdog/server/src/PackageInfoResolver.cpp b/cpp/watchdog/server/src/PackageInfoResolver.cpp index 1a4346a380..2b30600e7a 100644 --- a/cpp/watchdog/server/src/PackageInfoResolver.cpp +++ b/cpp/watchdog/server/src/PackageInfoResolver.cpp @@ -109,7 +109,7 @@ void PackageInfoResolver::terminate() { } Result<void> PackageInfoResolver::initWatchdogServiceHelper( - const sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper) { + const sp<IWatchdogServiceHelper>& watchdogServiceHelper) { std::unique_lock writeLock(mRWMutex); if (watchdogServiceHelper == nullptr) { return Error() << "Must provide a non-null watchdog service helper instance"; diff --git a/cpp/watchdog/server/src/PackageInfoResolver.h b/cpp/watchdog/server/src/PackageInfoResolver.h index e13f47bb74..c1bba4d6fe 100644 --- a/cpp/watchdog/server/src/PackageInfoResolver.h +++ b/cpp/watchdog/server/src/PackageInfoResolver.h @@ -59,7 +59,7 @@ public: protected: virtual android::base::Result<void> initWatchdogServiceHelper( - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper) = 0; + const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper) = 0; virtual void setPackageConfigurations( const std::unordered_set<std::string>& vendorPackagePrefixes, const std::unordered_map< @@ -112,7 +112,7 @@ protected: static void terminate(); android::base::Result<void> initWatchdogServiceHelper( - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper); + const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper); virtual void setPackageConfigurations( const std::unordered_set<std::string>& vendorPackagePrefixes, @@ -141,7 +141,7 @@ private: * thread. In order to avoid a race condition between |initWatchdogServiceHelper| and * |getPackage*ForUids| calls, mWatchdogServiceHelper is guarded by a read-write lock. */ - android::sp<IWatchdogServiceHelperInterface> mWatchdogServiceHelper GUARDED_BY(mRWMutex); + android::sp<IWatchdogServiceHelper> mWatchdogServiceHelper GUARDED_BY(mRWMutex); std::unordered_map<uid_t, android::automotive::watchdog::internal::PackageInfo> mUidToPackageInfoMapping GUARDED_BY(mRWMutex); std::vector<std::string> mVendorPackagePrefixes GUARDED_BY(mRWMutex); diff --git a/cpp/watchdog/server/src/ServiceManager.cpp b/cpp/watchdog/server/src/ServiceManager.cpp index 6bcba4011a..a50167b830 100644 --- a/cpp/watchdog/server/src/ServiceManager.cpp +++ b/cpp/watchdog/server/src/ServiceManager.cpp @@ -34,7 +34,7 @@ using ::android::base::Result; sp<WatchdogProcessService> ServiceManager::sWatchdogProcessService = nullptr; sp<WatchdogPerfService> ServiceManager::sWatchdogPerfService = nullptr; sp<WatchdogBinderMediator> ServiceManager::sWatchdogBinderMediator = nullptr; -sp<IWatchdogServiceHelperInterface> ServiceManager::sWatchdogServiceHelper = nullptr; +sp<IWatchdogServiceHelper> ServiceManager::sWatchdogServiceHelper = nullptr; Result<void> ServiceManager::startServices(const sp<Looper>& looper) { if (sWatchdogBinderMediator != nullptr || sWatchdogServiceHelper != nullptr || diff --git a/cpp/watchdog/server/src/ServiceManager.h b/cpp/watchdog/server/src/ServiceManager.h index a536121dff..a0cc4d40c4 100644 --- a/cpp/watchdog/server/src/ServiceManager.h +++ b/cpp/watchdog/server/src/ServiceManager.h @@ -44,7 +44,7 @@ private: static android::sp<WatchdogProcessService> sWatchdogProcessService; static android::sp<WatchdogPerfService> sWatchdogPerfService; static android::sp<WatchdogBinderMediator> sWatchdogBinderMediator; - static android::sp<IWatchdogServiceHelperInterface> sWatchdogServiceHelper; + static android::sp<IWatchdogServiceHelper> sWatchdogServiceHelper; }; } // namespace watchdog diff --git a/cpp/watchdog/server/src/WatchdogBinderMediator.cpp b/cpp/watchdog/server/src/WatchdogBinderMediator.cpp index 0a4eb2518e..f3fba35d31 100644 --- a/cpp/watchdog/server/src/WatchdogBinderMediator.cpp +++ b/cpp/watchdog/server/src/WatchdogBinderMediator.cpp @@ -37,6 +37,7 @@ using ::android::base::Error; using ::android::base::Join; using ::android::base::ParseUint; using ::android::base::Result; +using ::android::base::Split; using ::android::base::StringAppendF; using ::android::base::StringPrintf; using ::android::base::WriteStringToFd; @@ -79,7 +80,7 @@ Result<void> addToServiceManager(const char* serviceName, sp<IBinder> service) { WatchdogBinderMediator::WatchdogBinderMediator( const android::sp<WatchdogProcessService>& watchdogProcessService, const android::sp<WatchdogPerfService>& watchdogPerfService, - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper, + const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper, const AddServiceFunction& addServiceHandler) : mWatchdogProcessService(watchdogProcessService), mWatchdogPerfService(watchdogPerfService), @@ -131,60 +132,73 @@ Result<void> WatchdogBinderMediator::init() { status_t WatchdogBinderMediator::dump(int fd, const Vector<String16>& args) { int numArgs = args.size(); + if (numArgs == 0) { + return dumpServices(fd, args); + } if (numArgs == 1 && (args[0] == String16(kHelpFlag) || args[0] == String16(kHelpShortFlag))) { - if (!dumpHelpText(fd, "")) { - ALOGW("Failed to write help text to fd"); - return FAILED_TRANSACTION; - } - return OK; + return dumpHelpText(fd, ""); } - if (numArgs >= 1 && - (args[0] == String16(kStartCustomCollectionFlag) || - args[0] == String16(kEndCustomCollectionFlag))) { - auto ret = mWatchdogPerfService->onCustomCollection(fd, args); - if (!ret.ok()) { + if (args[0] == String16(kStartCustomCollectionFlag) || + args[0] == String16(kEndCustomCollectionFlag)) { + if (auto result = mWatchdogPerfService->onCustomCollection(fd, args); !result.ok()) { std::string mode = args[0] == String16(kStartCustomCollectionFlag) ? "start" : "end"; std::string errorMsg = StringPrintf("Failed to %s custom I/O perf collection: %s", - mode.c_str(), ret.error().message().c_str()); - if (ret.error().code() == BAD_VALUE) { + mode.c_str(), result.error().message().c_str()); + if (result.error().code() == BAD_VALUE) { dumpHelpText(fd, errorMsg); } else { ALOGW("%s", errorMsg.c_str()); } - return ret.error().code(); + return result.error().code(); } return OK; } - - if (numArgs > 0) { - ALOGW("Car watchdog cannot recognize the given option(%s). Dumping the current state...", - Join(args, " ").c_str()); + if (numArgs == 2 && args[0] == String16(kResetResourceOveruseStatsFlag)) { + std::string value = std::string(String8(args[1])); + std::vector<std::string> packageNames = Split(value, ","); + if (value.empty() || packageNames.empty()) { + dumpHelpText(fd, + StringPrintf("Must provide valid package names: [%s]\n", value.c_str())); + return BAD_VALUE; + } + if (auto result = mIoOveruseMonitor->resetIoOveruseStats(packageNames); !result.ok()) { + ALOGW("Failed to reset stats for packages: [%s]", value.c_str()); + return FAILED_TRANSACTION; + } + return OK; } + dumpHelpText(fd, + StringPrintf("Invalid carwatchdog dumpsys options: [%s]\n", + Join(args, " ").c_str())); + return dumpServices(fd, args); +} - auto ret = mWatchdogProcessService->dump(fd, args); - if (!ret.ok()) { - ALOGW("Failed to dump carwatchdog process service: %s", ret.error().message().c_str()); - return ret.error().code(); +status_t WatchdogBinderMediator::dumpServices(int fd, const Vector<String16>& args) { + if (auto result = mWatchdogProcessService->dump(fd, args); !result.ok()) { + ALOGW("Failed to dump carwatchdog process service: %s", result.error().message().c_str()); + return result.error().code(); } - ret = mWatchdogPerfService->onDump(fd); - if (!ret.ok()) { - ALOGW("Failed to dump I/O perf collection: %s", ret.error().message().c_str()); - return ret.error().code(); + if (auto result = mWatchdogPerfService->onDump(fd); !result.ok()) { + ALOGW("Failed to dump carwatchdog perf service: %s", result.error().message().c_str()); + return result.error().code(); } return OK; } -bool WatchdogBinderMediator::dumpHelpText(int fd, std::string errorMsg) { +status_t WatchdogBinderMediator::dumpHelpText(const int fd, const std::string& errorMsg) { if (!errorMsg.empty()) { ALOGW("Error: %s", errorMsg.c_str()); if (!WriteStringToFd(StringPrintf("Error: %s\n\n", errorMsg.c_str()), fd)) { ALOGW("Failed to write error message to fd"); - return false; + return FAILED_TRANSACTION; } } - - return WriteStringToFd(StringPrintf(kHelpText, kHelpFlag, kHelpShortFlag), fd) && - mWatchdogPerfService->dumpHelpText(fd); + if (!WriteStringToFd(StringPrintf(kHelpText, kHelpFlag, kHelpShortFlag), fd) || + !mWatchdogPerfService->dumpHelpText(fd) || !mIoOveruseMonitor->dumpHelpText(fd)) { + ALOGW("Failed to write help text to fd"); + return FAILED_TRANSACTION; + } + return OK; } Status WatchdogBinderMediator::registerClient(const sp<ICarWatchdogClient>& client, diff --git a/cpp/watchdog/server/src/WatchdogBinderMediator.h b/cpp/watchdog/server/src/WatchdogBinderMediator.h index 63b687bd56..212352066b 100644 --- a/cpp/watchdog/server/src/WatchdogBinderMediator.h +++ b/cpp/watchdog/server/src/WatchdogBinderMediator.h @@ -56,13 +56,12 @@ class WatchdogBinderMediatorPeer; // the calls either to process ANR or performance services. class WatchdogBinderMediator : public BnCarWatchdog { public: - WatchdogBinderMediator( - const android::sp<WatchdogProcessService>& watchdogProcessService, - const android::sp<WatchdogPerfService>& watchdogPerfService, - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper, - const std::function<android::base::Result<void>(const char*, - const android::sp<android::IBinder>&)>& - addServiceHandler = nullptr); + WatchdogBinderMediator(const android::sp<WatchdogProcessService>& watchdogProcessService, + const android::sp<WatchdogPerfService>& watchdogPerfService, + const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper, + const std::function<android::base::Result<void>( + const char*, const android::sp<android::IBinder>&)>& + addServiceHandler = nullptr); ~WatchdogBinderMediator() { terminate(); } // Implements ICarWatchdog.aidl APIs. @@ -113,7 +112,8 @@ protected: } private: - bool dumpHelpText(int fd, std::string errorMsg); + status_t dumpServices(int fd, const Vector<String16>& args); + status_t dumpHelpText(const int fd, const std::string& errorMsg); android::sp<WatchdogProcessService> mWatchdogProcessService; android::sp<WatchdogPerfService> mWatchdogPerfService; diff --git a/cpp/watchdog/server/src/WatchdogInternalHandler.h b/cpp/watchdog/server/src/WatchdogInternalHandler.h index 098b0dd894..1f417e7683 100644 --- a/cpp/watchdog/server/src/WatchdogInternalHandler.h +++ b/cpp/watchdog/server/src/WatchdogInternalHandler.h @@ -45,7 +45,7 @@ class WatchdogInternalHandler : public android::automotive::watchdog::internal:: public: explicit WatchdogInternalHandler( const android::sp<WatchdogBinderMediator>& binderMediator, - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper, + const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper, const android::sp<WatchdogProcessService>& watchdogProcessService, const android::sp<WatchdogPerfService>& watchdogPerfService, const android::sp<IIoOveruseMonitor>& ioOveruseMonitor) : @@ -107,7 +107,7 @@ private: void checkAndRegisterIoOveruseMonitor(); android::sp<WatchdogBinderMediator> mBinderMediator; - android::sp<IWatchdogServiceHelperInterface> mWatchdogServiceHelper; + android::sp<IWatchdogServiceHelper> mWatchdogServiceHelper; android::sp<WatchdogProcessService> mWatchdogProcessService; android::sp<WatchdogPerfService> mWatchdogPerfService; android::sp<IIoOveruseMonitor> mIoOveruseMonitor; diff --git a/cpp/watchdog/server/src/WatchdogPerfService.cpp b/cpp/watchdog/server/src/WatchdogPerfService.cpp index 214a16ce61..030f318820 100644 --- a/cpp/watchdog/server/src/WatchdogPerfService.cpp +++ b/cpp/watchdog/server/src/WatchdogPerfService.cpp @@ -68,7 +68,7 @@ constexpr const char* kHelpText = "\t%s <seconds>: Modifies the maximum collection duration. Default behavior is to collect " "until %ld minutes before automatically stopping the custom collection and discarding " "the collected data.\n" - "\t%s <package name>,<package, name>,...: Comma-separated value containing package names. " + "\t%s <package name>,<package name>,...: Comma-separated value containing package names. " "When provided, the results are filtered only to the provided package names. Default " "behavior is to list the results for the top N packages.\n" "%s: Stops custom performance data collection and generates a dump of " diff --git a/cpp/watchdog/server/src/WatchdogProcessService.cpp b/cpp/watchdog/server/src/WatchdogProcessService.cpp index 952c83ebdf..b84f5a22d0 100644 --- a/cpp/watchdog/server/src/WatchdogProcessService.cpp +++ b/cpp/watchdog/server/src/WatchdogProcessService.cpp @@ -140,7 +140,7 @@ WatchdogProcessService::WatchdogProcessService(const sp<Looper>& handlerLooper) } } Result<void> WatchdogProcessService::registerWatchdogServiceHelper( - const sp<IWatchdogServiceHelperInterface>& helper) { + const sp<IWatchdogServiceHelper>& helper) { if (helper == nullptr) { return Error() << "Must provide a non-null watchdog service helper instance"; } diff --git a/cpp/watchdog/server/src/WatchdogProcessService.h b/cpp/watchdog/server/src/WatchdogProcessService.h index 288f99e441..fcc1b3d535 100644 --- a/cpp/watchdog/server/src/WatchdogProcessService.h +++ b/cpp/watchdog/server/src/WatchdogProcessService.h @@ -41,7 +41,7 @@ namespace android { namespace automotive { namespace watchdog { -class IWatchdogServiceHelperInterface; +class IWatchdogServiceHelper; class WatchdogProcessService : public android::RefBase { public: @@ -55,7 +55,7 @@ public: void doHealthCheck(int what); virtual android::base::Result<void> registerWatchdogServiceHelper( - const android::sp<IWatchdogServiceHelperInterface>& helper); + const android::sp<IWatchdogServiceHelper>& helper); virtual android::binder::Status registerClient(const android::sp<ICarWatchdogClient>& client, TimeoutLength timeout); @@ -95,7 +95,7 @@ private: userId(userId), type(ClientType::Regular), client(client) {} - ClientInfo(const android::sp<IWatchdogServiceHelperInterface>& helper, + ClientInfo(const android::sp<IWatchdogServiceHelper>& helper, const android::sp<android::IBinder>& binder, pid_t pid, userid_t userId) : pid(pid), userId(userId), @@ -125,7 +125,7 @@ private: ClientType type; android::sp<ICarWatchdogClient> client = nullptr; - android::sp<IWatchdogServiceHelperInterface> watchdogServiceHelper = nullptr; + android::sp<IWatchdogServiceHelper> watchdogServiceHelper = nullptr; android::sp<IBinder> watchdogServiceBinder = nullptr; }; @@ -246,7 +246,7 @@ private: mNotSupportedVhalProperties; android::sp<PropertyChangeListener> mPropertyChangeListener; HeartBeat mVhalHeartBeat GUARDED_BY(mMutex); - android::sp<IWatchdogServiceHelperInterface> mWatchdogServiceHelper GUARDED_BY(mMutex); + android::sp<IWatchdogServiceHelper> mWatchdogServiceHelper GUARDED_BY(mMutex); }; } // namespace watchdog diff --git a/cpp/watchdog/server/src/WatchdogServiceHelper.cpp b/cpp/watchdog/server/src/WatchdogServiceHelper.cpp index 03f3305be2..4c1258ffb6 100644 --- a/cpp/watchdog/server/src/WatchdogServiceHelper.cpp +++ b/cpp/watchdog/server/src/WatchdogServiceHelper.cpp @@ -203,6 +203,17 @@ Status WatchdogServiceHelper::latestIoOveruseStats( return service->latestIoOveruseStats(packageIoOveruseStats); } +Status WatchdogServiceHelper::resetResourceOveruseStats( + const std::vector<std::string>& packageNames) { + sp<ICarWatchdogServiceForSystem> service; + if (std::shared_lock readLock(mRWMutex); mService == nullptr) { + return fromExceptionCode(Status::EX_ILLEGAL_STATE, "Watchdog service is not initialized"); + } else { + service = mService; + } + return service->resetResourceOveruseStats(packageNames); +} + } // namespace watchdog } // namespace automotive } // namespace android diff --git a/cpp/watchdog/server/src/WatchdogServiceHelper.h b/cpp/watchdog/server/src/WatchdogServiceHelper.h index 03796f0811..390013a3de 100644 --- a/cpp/watchdog/server/src/WatchdogServiceHelper.h +++ b/cpp/watchdog/server/src/WatchdogServiceHelper.h @@ -45,7 +45,7 @@ class WatchdogServiceHelperPeer; } // namespace internal -class IWatchdogServiceHelperInterface : public android::IBinder::DeathRecipient { +class IWatchdogServiceHelper : public android::IBinder::DeathRecipient { public: virtual android::binder::Status registerService( const android::sp< @@ -68,6 +68,8 @@ public: virtual android::binder::Status latestIoOveruseStats( const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>& packageIoOveruseStats) = 0; + virtual android::binder::Status resetResourceOveruseStats( + const std::vector<std::string>& packageNames) = 0; protected: virtual android::base::Result<void> init( @@ -81,7 +83,7 @@ private: // WatchdogServiceHelper implements the helper functions for the outbound API requests to // the CarWatchdogService. This class doesn't handle the inbound APIs requests from // CarWatchdogService except the registration APIs. -class WatchdogServiceHelper final : public IWatchdogServiceHelperInterface { +class WatchdogServiceHelper final : public IWatchdogServiceHelper { public: WatchdogServiceHelper() : mService(nullptr), mWatchdogProcessService(nullptr) {} ~WatchdogServiceHelper(); @@ -107,6 +109,7 @@ public: android::binder::Status latestIoOveruseStats( const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>& packageIoOveruseStats); + android::binder::Status resetResourceOveruseStats(const std::vector<std::string>& packageNames); protected: android::base::Result<void> init( diff --git a/cpp/watchdog/server/tests/IoOveruseMonitorTest.cpp b/cpp/watchdog/server/tests/IoOveruseMonitorTest.cpp index 89dd263a1e..a3081269c9 100644 --- a/cpp/watchdog/server/tests/IoOveruseMonitorTest.cpp +++ b/cpp/watchdog/server/tests/IoOveruseMonitorTest.cpp @@ -37,8 +37,6 @@ constexpr std::chrono::seconds kTestMonitorInterval = 5s; using ::android::IPCThreadState; using ::android::RefBase; -using ::android::automotive::watchdog::internal::ApplicationCategoryType; -using ::android::automotive::watchdog::internal::ComponentType; using ::android::automotive::watchdog::internal::IoOveruseAlertThreshold; using ::android::automotive::watchdog::internal::PackageIdentifier; using ::android::automotive::watchdog::internal::PackageInfo; @@ -48,14 +46,11 @@ using ::android::base::Result; using ::android::base::StringAppendF; using ::android::binder::Status; using ::testing::_; -using ::testing::AllOf; using ::testing::DoAll; -using ::testing::Field; using ::testing::Return; using ::testing::ReturnRef; using ::testing::SaveArg; using ::testing::UnorderedElementsAreArray; -using ::testing::Value; namespace { @@ -208,6 +203,41 @@ protected: mIoOveruseMonitorPeer.clear(); } + void setUpPackagesAndConfigurations() { + std::unordered_map<uid_t, PackageInfo> packageInfoMapping = + {{1001000, + constructPackageInfo( + /*packageName=*/"system.daemon", /*uid=*/1001000, UidType::NATIVE)}, + {1112345, + constructPackageInfo( + /*packageName=*/"com.android.google.package", /*uid=*/1112345, + UidType::APPLICATION)}, + {1113999, + constructPackageInfo( + /*packageName=*/"com.android.google.package", /*uid=*/1113999, + UidType::APPLICATION)}, + {1212345, + constructPackageInfo( + /*packageName=*/"com.android.google.package", /*uid=*/1212345, + UidType::APPLICATION)}, + {1312345, + constructPackageInfo( + /*packageName=*/"com.android.google.package", /*uid=*/1312345, + UidType::APPLICATION)}}; + ON_CALL(*mMockPackageInfoResolver, getPackageInfosForUids(_)) + .WillByDefault(Return(packageInfoMapping)); + mMockIoOveruseConfigs->injectPackageConfigs({ + {"system.daemon", + {constructPerStateBytes(/*fgBytes=*/80'000, /*bgBytes=*/40'000, + /*gmBytes=*/100'000), + /*isSafeToKill=*/false}}, + {"com.android.google.package", + {constructPerStateBytes(/*fgBytes=*/70'000, /*bgBytes=*/30'000, + /*gmBytes=*/100'000), + /*isSafeToKill=*/true}}, + }); + } + void executeAsUid(uid_t uid, std::function<void()> func) { sp<ScopedChangeCallingUid> scopedChangeCallingUid = new ScopedChangeCallingUid(uid); ASSERT_NO_FATAL_FAILURE(func()); @@ -221,33 +251,7 @@ protected: }; TEST_F(IoOveruseMonitorTest, TestOnPeriodicCollection) { - std::unordered_map<uid_t, PackageInfo> packageInfoMapping = - {{1001000, - constructPackageInfo( - /*packageName=*/"system.daemon", /*uid=*/1001000, UidType::NATIVE)}, - {1112345, - constructPackageInfo( - /*packageName=*/"com.android.google.package", /*uid=*/1112345, - UidType::APPLICATION)}, - {1212345, - constructPackageInfo( - /*packageName=*/"com.android.google.package", /*uid=*/1212345, - UidType::APPLICATION)}, - {1113999, - constructPackageInfo( - /*packageName=*/"com.android.google.package", /*uid=*/1113999, - UidType::APPLICATION)}}; - ON_CALL(*mMockPackageInfoResolver, getPackageInfosForUids(_)) - .WillByDefault(Return(packageInfoMapping)); - mMockIoOveruseConfigs->injectPackageConfigs({ - {"system.daemon", - {constructPerStateBytes(/*fgBytes=*/80'000, /*bgBytes=*/40'000, /*gmBytes=*/100'000), - /*isSafeToKill=*/false}}, - {"com.android.google.package", - {constructPerStateBytes(/*fgBytes=*/70'000, /*bgBytes=*/30'000, /*gmBytes=*/100'000), - /*isSafeToKill=*/true}}, - }); - + setUpPackagesAndConfigurations(); sp<MockResourceOveruseListener> mockResourceOveruseListener = new MockResourceOveruseListener(); ASSERT_NO_FATAL_FAILURE(executeAsUid(1001000, [&]() { ASSERT_RESULT_OK(mIoOveruseMonitor->addIoOveruseListener(mockResourceOveruseListener)); @@ -404,32 +408,7 @@ TEST_F(IoOveruseMonitorTest, TestOnPeriodicCollectionWithZeroWriteBytes) { } TEST_F(IoOveruseMonitorTest, TestOnPeriodicCollectionWithSmallWrittenBytes) { - std::unordered_map<uid_t, PackageInfo> packageInfoMapping = - {{1001000, - constructPackageInfo( - /*packageName=*/"system.daemon", /*uid=*/1001000, UidType::NATIVE)}, - {1112345, - constructPackageInfo( - /*packageName=*/"com.android.google.package", /*uid=*/1112345, - UidType::APPLICATION)}, - {1212345, - constructPackageInfo( - /*packageName=*/"com.android.google.package", /*uid=*/1212345, - UidType::APPLICATION)}, - {1312345, - constructPackageInfo( - /*packageName=*/"com.android.google.package", /*uid=*/1312345, - UidType::APPLICATION)}}; - EXPECT_CALL(*mMockPackageInfoResolver, getPackageInfosForUids(_)) - .WillRepeatedly(Return(packageInfoMapping)); - mMockIoOveruseConfigs->injectPackageConfigs( - {{"system.daemon", - {constructPerStateBytes(/*fgBytes=*/80'000, /*bgBytes=*/40'000, /*gmBytes=*/100'000), - /*isSafeToKill=*/false}}, - {"com.android.google.package", - {constructPerStateBytes(/*fgBytes=*/70'000, /*bgBytes=*/30'000, /*gmBytes=*/100'000), - /*isSafeToKill=*/true}}}); - + setUpPackagesAndConfigurations(); sp<MockUidIoStats> mockUidIoStats = new MockUidIoStats(); /* * UID 1212345 current written bytes < |KTestMinSyncWrittenBytes| so the UID's stats are not @@ -657,18 +636,7 @@ TEST_F(IoOveruseMonitorTest, TestUnaddIoOveruseListenerOnUnlinkToDeathError) { } TEST_F(IoOveruseMonitorTest, TestGetIoOveruseStats) { - // Setup internal counters for a package. - ON_CALL(*mMockPackageInfoResolver, getPackageInfosForUids(_)) - .WillByDefault([]() -> std::unordered_map<uid_t, PackageInfo> { - return {{1001000, - constructPackageInfo(/*packageName=*/"system.daemon", /*uid=*/1001000, - UidType::NATIVE)}}; - }); - mMockIoOveruseConfigs->injectPackageConfigs( - {{"system.daemon", - {constructPerStateBytes(/*fgBytes=*/80'000, /*bgBytes=*/40'000, - /*gmBytes=*/100'000), - /*isSafeToKill=*/false}}}); + setUpPackagesAndConfigurations(); sp<MockUidIoStats> mockUidIoStats = new MockUidIoStats(); mockUidIoStats->expectDeltaStats( {{1001000, IoUsage(0, 0, /*fgWrBytes=*/90'000, /*bgWrBytes=*/20'000, 0, 0)}}); @@ -694,6 +662,50 @@ TEST_F(IoOveruseMonitorTest, TestGetIoOveruseStats) { << "\nActual: " << actual.toString(); } +TEST_F(IoOveruseMonitorTest, TestResetIoOveruseStats) { + setUpPackagesAndConfigurations(); + sp<MockUidIoStats> mockUidIoStats = new MockUidIoStats(); + mockUidIoStats->expectDeltaStats( + {{1001000, IoUsage(0, 0, /*fgWrBytes=*/90'000, /*bgWrBytes=*/20'000, 0, 0)}}); + + ASSERT_RESULT_OK( + mIoOveruseMonitor->onPeriodicCollection(std::chrono::system_clock::to_time_t( + std::chrono::system_clock::now()), + mockUidIoStats, nullptr, nullptr)); + + IoOveruseStats actual; + ASSERT_NO_FATAL_FAILURE(executeAsUid(1001000, [&]() { + ASSERT_RESULT_OK(mIoOveruseMonitor->getIoOveruseStats(&actual)); + })); + + EXPECT_NE(actual.totalOveruses, 0); + EXPECT_NE(actual.writtenBytes.foregroundBytes, 0); + EXPECT_NE(actual.writtenBytes.backgroundBytes, 0); + + std::vector<std::string> packageNames = {"system.daemon"}; + EXPECT_CALL(*mMockWatchdogServiceHelper, resetResourceOveruseStats(packageNames)) + .WillOnce(Return(Status::ok())); + + ASSERT_RESULT_OK(mIoOveruseMonitor->resetIoOveruseStats(packageNames)); + + ASSERT_NO_FATAL_FAILURE(executeAsUid(1001000, [&]() { + ASSERT_RESULT_OK(mIoOveruseMonitor->getIoOveruseStats(&actual)); + })); + + EXPECT_EQ(actual.totalOveruses, 0); + EXPECT_EQ(actual.writtenBytes.foregroundBytes, 0); + EXPECT_EQ(actual.writtenBytes.backgroundBytes, 0); +} + +TEST_F(IoOveruseMonitorTest, TestErrorsResetIoOveruseStatsOnWatchdogServiceHelperError) { + std::vector<std::string> packageNames = {"system.daemon"}; + EXPECT_CALL(*mMockWatchdogServiceHelper, resetResourceOveruseStats(packageNames)) + .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE))); + + ASSERT_FALSE(mIoOveruseMonitor->resetIoOveruseStats(packageNames).ok()) + << "Must return error when WatchdogServiceHelper fails to reset stats"; +} + TEST_F(IoOveruseMonitorTest, TestErrorsGetIoOveruseStatsOnNoStats) { ON_CALL(*mMockPackageInfoResolver, getPackageInfosForUids(_)) .WillByDefault([]() -> std::unordered_map<uid_t, PackageInfo> { diff --git a/cpp/watchdog/server/tests/MockCarWatchdogServiceForSystem.h b/cpp/watchdog/server/tests/MockCarWatchdogServiceForSystem.h index c8f161497c..d484463a07 100644 --- a/cpp/watchdog/server/tests/MockCarWatchdogServiceForSystem.h +++ b/cpp/watchdog/server/tests/MockCarWatchdogServiceForSystem.h @@ -51,6 +51,8 @@ public: android::binder::Status, latestIoOveruseStats, (const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>&), (override)); + MOCK_METHOD(android::binder::Status, resetResourceOveruseStats, + (const std::vector<std::string>&), (override)); private: android::sp<MockBinder> mBinder; diff --git a/cpp/watchdog/server/tests/MockIoOveruseMonitor.h b/cpp/watchdog/server/tests/MockIoOveruseMonitor.h index cb7d0c5f12..79ba932f40 100644 --- a/cpp/watchdog/server/tests/MockIoOveruseMonitor.h +++ b/cpp/watchdog/server/tests/MockIoOveruseMonitor.h @@ -36,6 +36,7 @@ public: } ~MockIoOveruseMonitor() {} MOCK_METHOD(bool, isInitialized, (), (override)); + MOCK_METHOD(bool, dumpHelpText, (int), (override)); MOCK_METHOD(android::base::Result<void>, updateResourceOveruseConfigurations, (const std::vector< android::automotive::watchdog::internal::ResourceOveruseConfiguration>&), @@ -54,6 +55,8 @@ public: MOCK_METHOD(android::base::Result<void>, removeIoOveruseListener, (const sp<IResourceOveruseListener>&), (override)); MOCK_METHOD(android::base::Result<void>, getIoOveruseStats, (IoOveruseStats*), (override)); + MOCK_METHOD(android::base::Result<void>, resetIoOveruseStats, (const std::vector<std::string>&), + (override)); }; } // namespace watchdog diff --git a/cpp/watchdog/server/tests/MockPackageInfoResolver.h b/cpp/watchdog/server/tests/MockPackageInfoResolver.h index e3da3b607e..34f4c605d9 100644 --- a/cpp/watchdog/server/tests/MockPackageInfoResolver.h +++ b/cpp/watchdog/server/tests/MockPackageInfoResolver.h @@ -33,8 +33,7 @@ class MockPackageInfoResolver : public IPackageInfoResolver { public: MockPackageInfoResolver() {} MOCK_METHOD(android::base::Result<void>, initWatchdogServiceHelper, - (const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper), - (override)); + (const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper), (override)); MOCK_METHOD((std::unordered_map<uid_t, std::string>), getPackageNamesForUids, (const std::vector<uid_t>& uids), (override)); MOCK_METHOD((std::unordered_map<uid_t, android::automotive::watchdog::internal::PackageInfo>), diff --git a/cpp/watchdog/server/tests/MockWatchdogProcessService.h b/cpp/watchdog/server/tests/MockWatchdogProcessService.h index b720d919f4..ac78b4542a 100644 --- a/cpp/watchdog/server/tests/MockWatchdogProcessService.h +++ b/cpp/watchdog/server/tests/MockWatchdogProcessService.h @@ -43,7 +43,7 @@ public: MOCK_METHOD(android::base::Result<void>, dump, (int fd, const Vector<android::String16>& args), (override)); MOCK_METHOD(android::base::Result<void>, registerWatchdogServiceHelper, - (const android::sp<IWatchdogServiceHelperInterface>& helper), (override)); + (const android::sp<IWatchdogServiceHelper>& helper), (override)); MOCK_METHOD(android::binder::Status, registerClient, (const sp<ICarWatchdogClient>& client, TimeoutLength timeout), (override)); diff --git a/cpp/watchdog/server/tests/MockWatchdogServiceHelper.h b/cpp/watchdog/server/tests/MockWatchdogServiceHelper.h index 847c56122f..3d713450d9 100644 --- a/cpp/watchdog/server/tests/MockWatchdogServiceHelper.h +++ b/cpp/watchdog/server/tests/MockWatchdogServiceHelper.h @@ -32,7 +32,7 @@ namespace android { namespace automotive { namespace watchdog { -class MockWatchdogServiceHelper : public IWatchdogServiceHelperInterface { +class MockWatchdogServiceHelper : public IWatchdogServiceHelper { public: MockWatchdogServiceHelper() {} ~MockWatchdogServiceHelper() {} @@ -59,7 +59,8 @@ public: android::binder::Status, latestIoOveruseStats, (const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>&), (override)); - + MOCK_METHOD(android::binder::Status, resetResourceOveruseStats, + (const std::vector<std::string>&), (override)); MOCK_METHOD(void, terminate, (), (override)); }; diff --git a/cpp/watchdog/server/tests/WatchdogBinderMediatorTest.cpp b/cpp/watchdog/server/tests/WatchdogBinderMediatorTest.cpp index 0cc35100ce..64f9c07c81 100644 --- a/cpp/watchdog/server/tests/WatchdogBinderMediatorTest.cpp +++ b/cpp/watchdog/server/tests/WatchdogBinderMediatorTest.cpp @@ -40,8 +40,8 @@ using ::android::base::StringAppendF; using ::android::binder::Status; using ::testing::_; using ::testing::DoAll; -using ::testing::NiceMock; using ::testing::Return; +using ::testing::SaveArg; using ::testing::SetArgPointee; using ::testing::UnorderedElementsAreArray; @@ -172,13 +172,13 @@ TEST_F(WatchdogBinderMediatorTest, TestTerminate) { EXPECT_EQ(mWatchdogBinderMediator->mWatchdogInternalHandler, nullptr); } -TEST_F(WatchdogBinderMediatorTest, TestHandlesEmptyDumpArgs) { +TEST_F(WatchdogBinderMediatorTest, TestDumpWithEmptyArgs) { EXPECT_CALL(*mMockWatchdogProcessService, dump(-1, _)).WillOnce(Return(Result<void>())); EXPECT_CALL(*mMockWatchdogPerfService, onDump(-1)).WillOnce(Return(Result<void>())); mWatchdogBinderMediator->dump(-1, Vector<String16>()); } -TEST_F(WatchdogBinderMediatorTest, TestHandlesStartCustomPerfCollection) { +TEST_F(WatchdogBinderMediatorTest, TestDumpWithStartCustomPerfCollection) { EXPECT_CALL(*mMockWatchdogPerfService, onCustomCollection(-1, _)) .WillOnce(Return(Result<void>())); @@ -187,7 +187,7 @@ TEST_F(WatchdogBinderMediatorTest, TestHandlesStartCustomPerfCollection) { ASSERT_EQ(mWatchdogBinderMediator->dump(-1, args), OK); } -TEST_F(WatchdogBinderMediatorTest, TestHandlesStopCustomPerfCollection) { +TEST_F(WatchdogBinderMediatorTest, TestDumpWithStopCustomPerfCollection) { EXPECT_CALL(*mMockWatchdogPerfService, onCustomCollection(-1, _)) .WillOnce(Return(Result<void>())); @@ -196,10 +196,36 @@ TEST_F(WatchdogBinderMediatorTest, TestHandlesStopCustomPerfCollection) { ASSERT_EQ(mWatchdogBinderMediator->dump(-1, args), OK); } -TEST_F(WatchdogBinderMediatorTest, TestErrorOnInvalidDumpArgs) { +TEST_F(WatchdogBinderMediatorTest, TestDumpWithResetResourceOveruseStats) { + std::vector<std::string> actualPackages; + EXPECT_CALL(*mMockIoOveruseMonitor, resetIoOveruseStats(_)) + .WillOnce(DoAll(SaveArg<0>(&actualPackages), Return(Result<void>()))); + + Vector<String16> args; + args.push_back(String16(kResetResourceOveruseStatsFlag)); + args.push_back(String16("packageA,packageB")); + ASSERT_EQ(mWatchdogBinderMediator->dump(-1, args), OK); + ASSERT_EQ(actualPackages, std::vector<std::string>({"packageA", "packageB"})); +} + +TEST_F(WatchdogBinderMediatorTest, TestFailsDumpWithInvalidResetResourceOveruseStatsArg) { + EXPECT_CALL(*mMockIoOveruseMonitor, resetIoOveruseStats(_)).Times(0); + + Vector<String16> args; + args.push_back(String16(kResetResourceOveruseStatsFlag)); + args.push_back(String16("")); + ASSERT_EQ(mWatchdogBinderMediator->dump(-1, args), BAD_VALUE); +} + +TEST_F(WatchdogBinderMediatorTest, TestDumpWithInvalidDumpArgs) { Vector<String16> args; args.push_back(String16("--invalid_option")); - ASSERT_EQ(mWatchdogBinderMediator->dump(-1, args), OK) << "Error returned on invalid args"; + int nullFd = open("/dev/null", O_RDONLY); + EXPECT_CALL(*mMockWatchdogProcessService, dump(nullFd, _)).WillOnce(Return(Result<void>())); + EXPECT_CALL(*mMockWatchdogPerfService, onDump(nullFd)).WillOnce(Return(Result<void>())); + + EXPECT_EQ(mWatchdogBinderMediator->dump(nullFd, args), OK) << "Error returned on invalid args"; + close(nullFd); } TEST_F(WatchdogBinderMediatorTest, TestRegisterClient) { diff --git a/cpp/watchdog/server/tests/WatchdogInternalHandlerTest.cpp b/cpp/watchdog/server/tests/WatchdogInternalHandlerTest.cpp index f5dd7dcb46..40d6389b1d 100644 --- a/cpp/watchdog/server/tests/WatchdogInternalHandlerTest.cpp +++ b/cpp/watchdog/server/tests/WatchdogInternalHandlerTest.cpp @@ -42,7 +42,6 @@ namespace watchdog { namespace aawi = ::android::automotive::watchdog::internal; -using aawi::ComponentType; using aawi::ICarWatchdogServiceForSystem; using aawi::ICarWatchdogServiceForSystemDefault; using aawi::PackageResourceOveruseAction; @@ -59,10 +58,9 @@ namespace { class MockWatchdogBinderMediator : public WatchdogBinderMediator { public: - MockWatchdogBinderMediator( - const android::sp<WatchdogProcessService>& watchdogProcessService, - const android::sp<WatchdogPerfService>& watchdogPerfService, - const android::sp<IWatchdogServiceHelperInterface>& watchdogServiceHelper) : + MockWatchdogBinderMediator(const android::sp<WatchdogProcessService>& watchdogProcessService, + const android::sp<WatchdogPerfService>& watchdogPerfService, + const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper) : WatchdogBinderMediator(watchdogProcessService, watchdogPerfService, watchdogServiceHelper, [](const char*, const android::sp<android::IBinder>&) -> Result<void> { return Result<void>{}; }) {} diff --git a/cpp/watchdog/server/tests/WatchdogServiceHelperTest.cpp b/cpp/watchdog/server/tests/WatchdogServiceHelperTest.cpp index 17edadfb2f..99b65df0cf 100644 --- a/cpp/watchdog/server/tests/WatchdogServiceHelperTest.cpp +++ b/cpp/watchdog/server/tests/WatchdogServiceHelperTest.cpp @@ -32,11 +32,9 @@ namespace aawi = ::android::automotive::watchdog::internal; using aawi::ApplicationCategoryType; using aawi::ComponentType; using aawi::ICarWatchdogServiceForSystem; -using aawi::ICarWatchdogServiceForSystemDefault; using aawi::PackageInfo; using aawi::PackageIoOveruseStats; using aawi::UidType; -using ::android::BBinder; using ::android::IBinder; using ::android::RefBase; using ::android::sp; @@ -47,7 +45,6 @@ using ::testing::_; using ::testing::DoAll; using ::testing::IsEmpty; using ::testing::Return; -using ::testing::SaveArg; using ::testing::SetArgPointee; using ::testing::UnorderedElementsAreArray; @@ -152,7 +149,7 @@ TEST_F(WatchdogServiceHelperTest, TestErrorOnInitWithErrorFromWatchdogProcessSer sp<MockWatchdogProcessService> mockWatchdogProcessService(new MockWatchdogProcessService()); EXPECT_CALL(*mockWatchdogProcessService, registerWatchdogServiceHelper(_)) - .WillOnce([](const sp<IWatchdogServiceHelperInterface>&) -> Result<void> { + .WillOnce([](const sp<IWatchdogServiceHelper>&) -> Result<void> { return Error() << "Failed to register"; }); @@ -388,21 +385,19 @@ TEST_F(WatchdogServiceHelperTest, TestLatestIoOveruseStats) { stats.ioOveruseStats.totalOveruses = 10; stats.shouldNotify = true; std::vector<PackageIoOveruseStats> expectedIoOveruseStats = {stats}; - std::vector<PackageIoOveruseStats> actualOveruseStats; registerCarWatchdogService(); EXPECT_CALL(*mMockCarWatchdogServiceForSystem, latestIoOveruseStats(expectedIoOveruseStats)) - .WillOnce(DoAll(SaveArg<0>(&actualOveruseStats), Return(Status::ok()))); + .WillOnce(Return(Status::ok())); Status status = mWatchdogServiceHelper->latestIoOveruseStats(expectedIoOveruseStats); ASSERT_TRUE(status.isOk()) << status; - EXPECT_THAT(actualOveruseStats, UnorderedElementsAreArray(expectedIoOveruseStats)); } TEST_F(WatchdogServiceHelperTest, - TestErrorsOnLatetstIoOveruseStatsWithNoCarWatchdogServiceRegistered) { + TestErrorsOnLatestIoOveruseStatsWithNoCarWatchdogServiceRegistered) { EXPECT_CALL(*mMockCarWatchdogServiceForSystem, latestIoOveruseStats(_)).Times(0); Status status = mWatchdogServiceHelper->latestIoOveruseStats({}); @@ -412,7 +407,7 @@ TEST_F(WatchdogServiceHelperTest, } TEST_F(WatchdogServiceHelperTest, - TestErrorsOnLatetstIoOveruseStatsWithErrorStatusFromCarWatchdogService) { + TestErrorsOnLatestIoOveruseStatsWithErrorStatusFromCarWatchdogService) { registerCarWatchdogService(); EXPECT_CALL(*mMockCarWatchdogServiceForSystem, latestIoOveruseStats(_)) @@ -424,6 +419,41 @@ TEST_F(WatchdogServiceHelperTest, "service API returns error"; } +TEST_F(WatchdogServiceHelperTest, TestResetResourceOveruseStats) { + registerCarWatchdogService(); + + std::vector<std::string> packageNames = {"system.daemon"}; + EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(packageNames)) + .WillOnce(Return(Status::ok())); + + Status status = mWatchdogServiceHelper->resetResourceOveruseStats(packageNames); + + ASSERT_TRUE(status.isOk()) << status; +} + +TEST_F(WatchdogServiceHelperTest, + TestErrorsOnResetResourceOveruseStatsWithNoCarWatchdogServiceRegistered) { + EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(_)).Times(0); + + Status status = mWatchdogServiceHelper->resetResourceOveruseStats({}); + + ASSERT_FALSE(status.isOk()) << "resetResourceOveruseStats should fail when no " + "car watchdog service registered with the helper"; +} + +TEST_F(WatchdogServiceHelperTest, + TestErrorsOnResetResourceOveruseStatsWithErrorStatusFromCarWatchdogService) { + registerCarWatchdogService(); + + EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(_)) + .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state"))); + + Status status = mWatchdogServiceHelper->resetResourceOveruseStats({}); + + ASSERT_FALSE(status.isOk()) << "resetResourceOveruseStats should fail when car watchdog " + "service API returns error"; +} + } // namespace watchdog } // namespace automotive } // namespace android |