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 "UidCpuStatsCollector.h"
22 #include "UidIoStatsCollector.h"
23 #include "UidProcStatsCollector.h"
24 
25 #include <aidl/android/automotive/watchdog/internal/PackageInfo.h>
26 #include <android-base/result.h>
27 #include <utils/Mutex.h>
28 #include <utils/RefBase.h>
29 #include <utils/StrongPointer.h>
30 
31 #include <string>
32 #include <vector>
33 
34 namespace android {
35 namespace automotive {
36 namespace watchdog {
37 
38 // Forward declaration for testing use only.
39 namespace internal {
40 
41 class UidStatsCollectorPeer;
42 
43 }  // namespace internal
44 
45 struct UidStats {
46     aidl::android::automotive::watchdog::internal::PackageInfo packageInfo;
47     int64_t cpuTimeMillis = 0;
48     UidIoStats ioStats = {};
49     UidProcStats procStats = {};
50     // Returns true when package info is available.
51     bool hasPackageInfo() const;
52     // Returns package name if the |packageInfo| is available. Otherwise, returns the |uid|.
53     std::string genericPackageName() const;
54     // Returns the uid for the stats;
55     uid_t uid() const;
56 };
57 
58 // Collector/Aggregator for per-UID I/O and proc stats.
59 class UidStatsCollectorInterface : public RefBase {
60 public:
61     // Initializes the collector.
62     virtual void init() = 0;
63     // Collects the per-UID I/O and proc stats.
64     virtual android::base::Result<void> collect() = 0;
65     // Returns the latest per-uid I/O and proc stats.
66     virtual const std::vector<UidStats> latestStats() const = 0;
67     // Returns the delta of per-uid I/O and proc stats since the last before collection.
68     virtual const std::vector<UidStats> deltaStats() const = 0;
69     // Returns true only when the per-UID I/O or proc stats files are accessible.
70     virtual bool enabled() const = 0;
71 };
72 
73 class UidStatsCollector final : public UidStatsCollectorInterface {
74 public:
UidStatsCollector()75     UidStatsCollector() :
76           mPackageInfoResolver(PackageInfoResolver::getInstance()),
77           mUidCpuStatsCollector(android::sp<UidCpuStatsCollector>::make()),
78           mUidIoStatsCollector(android::sp<UidIoStatsCollector>::make()),
79           mUidProcStatsCollector(android::sp<UidProcStatsCollector>::make()) {}
80 
init()81     void init() override {
82         Mutex::Autolock lock(mMutex);
83         mUidCpuStatsCollector->init();
84         mUidIoStatsCollector->init();
85         mUidProcStatsCollector->init();
86     }
87 
88     android::base::Result<void> collect() override;
89 
latestStats()90     const std::vector<UidStats> latestStats() const override {
91         Mutex::Autolock lock(mMutex);
92         return mLatestStats;
93     }
94 
deltaStats()95     const std::vector<UidStats> deltaStats() const override {
96         Mutex::Autolock lock(mMutex);
97         return mDeltaStats;
98     }
99 
enabled()100     bool enabled() const override {
101         return mUidIoStatsCollector->enabled() || mUidProcStatsCollector->enabled();
102     }
103 
104 private:
105     std::vector<UidStats> process(
106             const std::unordered_map<uid_t, UidIoStats>& uidIoStatsByUid,
107             const std::unordered_map<uid_t, UidProcStats>& uidProcStatsByUid,
108             const std::unordered_map<uid_t, int64_t>& cpuTimeMillisByUid) const;
109 
110     // Local PackageInfoResolverInterface instance. Useful to mock in tests.
111     sp<PackageInfoResolverInterface> mPackageInfoResolver;
112 
113     mutable Mutex mMutex;
114 
115     android::sp<UidCpuStatsCollectorInterface> mUidCpuStatsCollector GUARDED_BY(mMutex);
116 
117     android::sp<UidIoStatsCollectorInterface> mUidIoStatsCollector GUARDED_BY(mMutex);
118 
119     android::sp<UidProcStatsCollectorInterface> mUidProcStatsCollector GUARDED_BY(mMutex);
120 
121     std::vector<UidStats> mLatestStats GUARDED_BY(mMutex);
122 
123     std::vector<UidStats> mDeltaStats GUARDED_BY(mMutex);
124 
125     // For unit tests.
126     friend class internal::UidStatsCollectorPeer;
127 };
128 
129 }  // namespace watchdog
130 }  // namespace automotive
131 }  // namespace android
132 
133 #endif  //  CPP_WATCHDOG_SERVER_SRC_UIDSTATSCOLLECTOR_H_
134