1 /* 2 * Copyright (c) 2021, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CPP_WATCHDOG_SERVER_SRC_UIDSTATSCOLLECTOR_H_ 18 #define CPP_WATCHDOG_SERVER_SRC_UIDSTATSCOLLECTOR_H_ 19 20 #include "PackageInfoResolver.h" 21 #include "UidIoStatsCollector.h" 22 #include "UidProcStatsCollector.h" 23 24 #include <android-base/result.h> 25 #include <android/automotive/watchdog/internal/PackageInfo.h> 26 #include <utils/Mutex.h> 27 #include <utils/RefBase.h> 28 #include <utils/StrongPointer.h> 29 30 #include <string> 31 #include <vector> 32 33 namespace android { 34 namespace automotive { 35 namespace watchdog { 36 37 // Forward declaration for testing use only. 38 namespace internal { 39 40 class UidStatsCollectorPeer; 41 42 } // namespace internal 43 44 struct UidStats { 45 android::automotive::watchdog::internal::PackageInfo packageInfo; 46 UidIoStats ioStats = {}; 47 UidProcStats procStats = {}; 48 // Returns true when package info is available. 49 bool hasPackageInfo() const; 50 // Returns package name if the |packageInfo| is available. Otherwise, returns the |uid|. 51 std::string genericPackageName() const; 52 // Returns the uid for the stats; 53 uid_t uid() const; 54 }; 55 56 // Collector/Aggregator for per-UID I/O and proc stats. 57 class UidStatsCollectorInterface : public RefBase { 58 public: 59 // Collects the per-UID I/O and proc stats. 60 virtual android::base::Result<void> collect() = 0; 61 // Returns the latest per-uid I/O and proc stats. 62 virtual const std::vector<UidStats> latestStats() const = 0; 63 // Returns the delta of per-uid I/O and proc stats since the last before collection. 64 virtual const std::vector<UidStats> deltaStats() const = 0; 65 // Returns true only when the per-UID I/O or proc stats files are accessible. 66 virtual bool enabled() const = 0; 67 }; 68 69 class UidStatsCollector final : public UidStatsCollectorInterface { 70 public: UidStatsCollector()71 UidStatsCollector() : 72 mPackageInfoResolver(PackageInfoResolver::getInstance()), 73 mUidIoStatsCollector(android::sp<UidIoStatsCollector>::make()), 74 mUidProcStatsCollector(android::sp<UidProcStatsCollector>::make()) {} 75 76 android::base::Result<void> collect() override; 77 latestStats()78 const std::vector<UidStats> latestStats() const override { 79 Mutex::Autolock lock(mMutex); 80 return mLatestStats; 81 } 82 deltaStats()83 const std::vector<UidStats> deltaStats() const override { 84 Mutex::Autolock lock(mMutex); 85 return mDeltaStats; 86 } 87 enabled()88 bool enabled() const override { 89 return mUidIoStatsCollector->enabled() || mUidProcStatsCollector->enabled(); 90 } 91 92 private: 93 std::vector<UidStats> process( 94 const std::unordered_map<uid_t, UidIoStats>& uidIoStatsByUid, 95 const std::unordered_map<uid_t, UidProcStats>& uidProcStatsByUid) const; 96 // Local IPackageInfoResolver instance. Useful to mock in tests. 97 sp<IPackageInfoResolver> mPackageInfoResolver; 98 99 mutable Mutex mMutex; 100 101 android::sp<UidIoStatsCollectorInterface> mUidIoStatsCollector; 102 103 android::sp<UidProcStatsCollectorInterface> mUidProcStatsCollector; 104 105 std::vector<UidStats> mLatestStats; 106 107 std::vector<UidStats> mDeltaStats; 108 109 // For unit tests. 110 friend class internal::UidStatsCollectorPeer; 111 }; 112 113 } // namespace watchdog 114 } // namespace automotive 115 } // namespace android 116 117 #endif // CPP_WATCHDOG_SERVER_SRC_UIDSTATSCOLLECTOR_H_ 118