1 /* 2 * Copyright (c) 2025, 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 #pragma once 18 19 #include "PackageInfoResolver.h" 20 #include "UidIoStatsCollector.h" 21 22 #include <aidl/android/automotive/watchdog/internal/PackageInfo.h> 23 #include <android-base/result.h> 24 #include <utils/Mutex.h> 25 #include <utils/RefBase.h> 26 #include <utils/StrongPointer.h> 27 28 #include <string> 29 #include <vector> 30 31 namespace android { 32 namespace automotive { 33 namespace watchdog { 34 35 // Forward declaration for testing use only. 36 namespace internal { 37 38 class UidStatsCollectorBasePeer; 39 40 } // namespace internal 41 42 struct UidBaseStats { 43 aidl::android::automotive::watchdog::internal::PackageInfo packageInfo; 44 UidIoStats ioStats = {}; 45 // Returns package name if the |packageInfo| is available. Otherwise, returns the |uid|. 46 std::string genericPackageName() const; 47 // Returns true when package info is available. 48 bool hasPackageInfo() const; 49 // Returns the uid for the stats; 50 uid_t uid() const; 51 }; 52 53 // Collector/Aggregator for per-UID I/O stats. 54 class UidStatsCollectorBaseInterface : virtual public RefBase { 55 public: 56 // Initializes the collector. 57 virtual void init() = 0; 58 // Collects the per-UID I/O stats. 59 virtual android::base::Result<void> collect() = 0; 60 // Returns the latest per-uid I/O stats. 61 virtual const std::vector<UidBaseStats> latestBaseStats() const = 0; 62 // Returns the delta of per-uid I/O stats since the last before collection. 63 virtual const std::vector<UidBaseStats> deltaBaseStats() const = 0; 64 // Returns true only when the per-UID I/O stats files are accessible. 65 virtual bool enabled() const = 0; 66 }; 67 68 class UidStatsCollectorBase : public UidStatsCollectorBaseInterface { 69 public: UidStatsCollectorBase()70 UidStatsCollectorBase() : 71 mPackageInfoResolver(PackageInfoResolver::getInstance()), 72 mUidIoStatsCollector(android::sp<UidIoStatsCollector>::make()) {} 73 init()74 void init() override { 75 Mutex::Autolock lock(mMutex); 76 mUidIoStatsCollector->init(); 77 } 78 79 android::base::Result<void> collect() override; 80 latestBaseStats()81 const std::vector<UidBaseStats> latestBaseStats() const override { 82 Mutex::Autolock lock(mMutex); 83 return mLatestBaseStats; 84 } 85 deltaBaseStats()86 const std::vector<UidBaseStats> deltaBaseStats() const override { 87 Mutex::Autolock lock(mMutex); 88 return mDeltaBaseStats; 89 } 90 enabled()91 bool enabled() const override { return mUidIoStatsCollector->enabled(); } 92 93 protected: 94 // Local PackageInfoResolverInterface instance. Useful to mock in tests. 95 std::shared_ptr<PackageInfoResolverInterface> mPackageInfoResolver; 96 97 mutable Mutex mMutex; 98 99 android::sp<UidIoStatsCollectorInterface> mUidIoStatsCollector GUARDED_BY(mMutex); 100 101 std::vector<UidBaseStats> mLatestBaseStats GUARDED_BY(mMutex); 102 103 std::vector<UidBaseStats> mDeltaBaseStats GUARDED_BY(mMutex); 104 105 private: 106 std::vector<UidBaseStats> process( 107 const std::unordered_map<uid_t, UidIoStats>& uidIoStatsByUid) const; 108 109 // For unit tests. 110 friend class internal::UidStatsCollectorBasePeer; 111 }; 112 113 } // namespace watchdog 114 } // namespace automotive 115 } // namespace android 116