1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NET_STATS_CACHED_H 17 #define NET_STATS_CACHED_H 18 19 #include <map> 20 #include <mutex> 21 #include <vector> 22 23 #include "net_stats_callback.h" 24 #include "net_stats_info.h" 25 #include "netmanager_base_common_utils.h" 26 27 #include "timer.h" 28 29 namespace OHOS { 30 namespace NetManagerStandard { 31 class NetStatsCached { 32 public: 33 NetStatsCached() = default; 34 ~NetStatsCached() = default; 35 void ForceUpdateStats(); 36 37 int32_t StartCached(); 38 39 void SetCycleThreshold(uint32_t threshold); 40 41 void GetUidStatsCached(std::vector<NetStatsInfo> &uidStatsInfo); 42 43 void GetIfaceStatsCached(std::vector<NetStatsInfo> &ifaceStatsInfo); 44 SetTrafficThreshold(uint64_t threshold)45 inline void SetTrafficThreshold(uint64_t threshold) 46 { 47 trafficThreshold_ = threshold; 48 } 49 SetDateThreshold(uint64_t threshold)50 inline void SetDateThreshold(uint64_t threshold) 51 { 52 cycleThreshold_ = threshold; 53 } 54 SetCallbackManager(const std::shared_ptr<NetStatsCallback> & callbackManager)55 inline void SetCallbackManager(const std::shared_ptr<NetStatsCallback> &callbackManager) 56 { 57 stats_.SetNotifier(callbackManager); 58 } 59 60 void Reset(); 61 62 private: 63 class CachedInfo { 64 public: PushUidStats(NetStatsInfo & info)65 void PushUidStats(NetStatsInfo &info) 66 { 67 if (info.HasNoData()) { 68 return; 69 } 70 info.date_ = CommonUtils::GetCurrentSecond(); 71 uidStatsInfo_.push_back(info); 72 currentUidStats_ += info.GetStats(); 73 if (netStatsCallbackManager_ != nullptr) { 74 netStatsCallbackManager_->NotifyNetUidStatsChanged(info.iface_, info.uid_); 75 } 76 } 77 PushIfaceStats(NetStatsInfo & info)78 void PushIfaceStats(NetStatsInfo &info) 79 { 80 if (info.HasNoData()) { 81 return; 82 } 83 info.date_ = CommonUtils::GetCurrentSecond(); 84 ifaceStatsInfo_.push_back(info); 85 currentIfaceStats_ += info.GetStats(); 86 if (netStatsCallbackManager_ != nullptr) { 87 netStatsCallbackManager_->NotifyNetIfaceStatsChanged(info.iface_); 88 } 89 } 90 GetUidStatsInfo()91 inline std::vector<NetStatsInfo> &GetUidStatsInfo() 92 { 93 return uidStatsInfo_; 94 } 95 GetIfaceStatsInfo()96 inline std::vector<NetStatsInfo> &GetIfaceStatsInfo() 97 { 98 return ifaceStatsInfo_; 99 } 100 GetCurrentUidStats()101 inline uint64_t GetCurrentUidStats() const 102 { 103 return currentUidStats_; 104 } 105 GetCurrentIfaceStats()106 inline uint64_t GetCurrentIfaceStats() const 107 { 108 return currentIfaceStats_; 109 } 110 ResetUidStats()111 void ResetUidStats() 112 { 113 uidStatsInfo_.clear(); 114 currentUidStats_ = 0; 115 } 116 ResetIfaceStats()117 void ResetIfaceStats() 118 { 119 ifaceStatsInfo_.clear(); 120 currentIfaceStats_ = 0; 121 } 122 SetNotifier(const std::shared_ptr<NetStatsCallback> & callbackManager)123 inline void SetNotifier(const std::shared_ptr<NetStatsCallback> &callbackManager) 124 { 125 netStatsCallbackManager_ = callbackManager; 126 } 127 128 private: 129 uint64_t currentUidStats_ = 0; 130 uint64_t currentIfaceStats_ = 0; 131 std::vector<NetStatsInfo> uidStatsInfo_; 132 std::vector<NetStatsInfo> ifaceStatsInfo_; 133 std::shared_ptr<NetStatsCallback> netStatsCallbackManager_ = nullptr; 134 }; 135 136 static constexpr uint32_t DEFAULT_CACHE_CYCLE_MS = 30 * 60 * 1000; 137 static constexpr uint64_t DEFAULT_TRAFFIC_STATISTICS_THRESHOLD_BYTES = 2 * 1024 * 1024; 138 static constexpr uint64_t DEFAULT_DATA_CYCLE_S = 180 * 24 * 60 * 60; 139 static constexpr uint64_t CACHE_DATE_TIME_S = 1 * 24 * 60 * 60; 140 static constexpr uint64_t STATS_PACKET_CYCLE_MS = 1 * 60 * 60 * 1000; 141 142 CachedInfo stats_; 143 std::mutex lock_; 144 bool isForce_ = false; 145 std::unique_ptr<Timer> cacheTimer_ = nullptr; 146 std::unique_ptr<Timer> writeTimer_ = nullptr; 147 uint32_t cycleThreshold_ = DEFAULT_CACHE_CYCLE_MS; 148 uint64_t trafficThreshold_ = DEFAULT_TRAFFIC_STATISTICS_THRESHOLD_BYTES; 149 uint64_t dateCycle_ = DEFAULT_DATA_CYCLE_S; 150 std::vector<NetStatsInfo> lastUidStatsInfo_; 151 std::map<std::string, NetStatsInfo> lastIfaceStatsMap_; 152 153 void CacheStats(); 154 void CacheUidStats(); 155 void CacheIfaceStats(); 156 157 void WriteStats(); 158 void WriteUidStats(); 159 void WriteIfaceStats(); 160 CheckUidStor()161 inline bool CheckUidStor() 162 { 163 return stats_.GetCurrentUidStats() >= trafficThreshold_; 164 } 165 CheckIfaceStor()166 inline bool CheckIfaceStor() 167 { 168 return stats_.GetCurrentIfaceStats() >= trafficThreshold_; 169 } 170 }; 171 } // namespace NetManagerStandard 172 } // namespace OHOS 173 174 #endif // NET_STATS_CACHED_H 175