1 /* 2 * Copyright (c) 2021-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 #include "perf_stat.h" 17 18 #include "account_log_wrapper.h" 19 20 namespace OHOS { 21 namespace AccountSA { PerfStat()22PerfStat::PerfStat() 23 {} 24 ~PerfStat()25PerfStat::~PerfStat() 26 { 27 Reset(); 28 } 29 30 /* Account Bind process statistic */ GetAccountBindStartTime() const31int64_t PerfStat::GetAccountBindStartTime() const 32 { 33 return accountBindBegin_; 34 } 35 SetAccountBindStartTime(int64_t time)36void PerfStat::SetAccountBindStartTime(int64_t time) 37 { 38 accountBindBegin_ = (time > 0) ? time : 0; 39 } 40 GetAccountBindEndTime() const41int64_t PerfStat::GetAccountBindEndTime() const 42 { 43 return accountBindEnd_; 44 } 45 SetAccountBindEndTime(int64_t time)46void PerfStat::SetAccountBindEndTime(int64_t time) 47 { 48 accountBindEnd_ = (time > 0 && time > accountBindBegin_) ? time : accountBindBegin_; 49 } 50 51 /* Account Add process statistic */ GetAccountAddStartTime() const52int64_t PerfStat::GetAccountAddStartTime() const 53 { 54 return accountAddBegin_; 55 } 56 SetAccountAddStartTime(int64_t time)57void PerfStat::SetAccountAddStartTime(int64_t time) 58 { 59 accountAddBegin_ = (time > 0) ? time : 0; 60 } 61 GetAccountAddEndTime() const62int64_t PerfStat::GetAccountAddEndTime() const 63 { 64 return accountAddEnd_; 65 } 66 SetAccountAddEndTime(int64_t time)67void PerfStat::SetAccountAddEndTime(int64_t time) 68 { 69 accountAddEnd_ = (time > 0 && time > accountAddBegin_) ? time : accountAddBegin_; 70 } 71 72 /* Account Delete process statistic */ GetAccountDelStartTime() const73int64_t PerfStat::GetAccountDelStartTime() const 74 { 75 return accountDelBegin_; 76 } 77 SetAccountDelStartTime(int64_t time)78void PerfStat::SetAccountDelStartTime(int64_t time) 79 { 80 accountDelBegin_ = (time > 0) ? time : 0; 81 } 82 GetAccountDelEndTime() const83int64_t PerfStat::GetAccountDelEndTime() const 84 { 85 return accountDelEnd_; 86 } 87 SetAccountDelEndTime(int64_t time)88void PerfStat::SetAccountDelEndTime(int64_t time) 89 { 90 accountDelEnd_ = (time > 0 && time > accountDelBegin_) ? time : accountDelBegin_; 91 } 92 93 /* Account Query process statistic */ GetAccountQueryStartTime() const94int64_t PerfStat::GetAccountQueryStartTime() const 95 { 96 return accountQueryBegin_; 97 } 98 SetAccountQueryStartTime(int64_t time)99void PerfStat::SetAccountQueryStartTime(int64_t time) 100 { 101 accountQueryBegin_ = (time > 0) ? time : 0; 102 } 103 GetAccountQueryEndTime() const104int64_t PerfStat::GetAccountQueryEndTime() const 105 { 106 return accountQueryEnd_; 107 } 108 SetAccountQueryEndTime(int64_t time)109void PerfStat::SetAccountQueryEndTime(int64_t time) 110 { 111 accountQueryEnd_ = (time > 0 && time > accountQueryBegin_) ? time : accountQueryBegin_; 112 } 113 114 /* Account Service process statistic */ SetInstanceStartTime(int64_t time)115void PerfStat::SetInstanceStartTime(int64_t time) 116 { 117 serviceOnStart_ = time; 118 } 119 SetInstanceStopTime(int64_t time)120void PerfStat::SetInstanceStopTime(int64_t time) 121 { 122 serviceOnStop_ = time; 123 } 124 SetInstanceCreateTime(int64_t time)125void PerfStat::SetInstanceCreateTime(int64_t time) 126 { 127 instanceCreate_ = time; 128 } 129 SetInstanceInitTime(int64_t time)130void PerfStat::SetInstanceInitTime(int64_t time) 131 { 132 serviceInit_ = time; 133 } 134 135 /* Account state machine process statistic */ SetAccountStateChangeTime(const std::string & stateStr,int64_t time)136void PerfStat::SetAccountStateChangeTime(const std::string &stateStr, int64_t time) 137 { 138 accountStateChangeRecords_[stateStr] = time; 139 } 140 141 /* Set/Get perf statistic enable state */ GetPerfStatEnabled() const142bool PerfStat::GetPerfStatEnabled() const 143 { 144 return enableStat_; 145 } 146 SetPerfStatEnabled(bool enable)147void PerfStat::SetPerfStatEnabled(bool enable) 148 { 149 enableStat_ = enable; 150 } 151 152 /* reset to default */ Reset()153void PerfStat::Reset() 154 { 155 accountBindBegin_ = 0; 156 accountBindEnd_ = 0; 157 158 accountAddBegin_ = 0; 159 accountAddEnd_ = 0; 160 161 accountDelBegin_ = 0; 162 accountDelEnd_ = 0; 163 164 accountQueryBegin_ = 0; 165 accountQueryEnd_ = 0; 166 167 instanceCreate_ = 0; 168 serviceOnStart_ = 0; 169 serviceOnStop_ = 0; 170 serviceInit_ = 0; 171 172 accountStateChangeRecords_.clear(); 173 174 enableStat_ = true; 175 } 176 Dump(std::string & result) const177void PerfStat::Dump(std::string& result) const 178 { 179 if (!enableStat_) { 180 ACCOUNT_LOGI("statistics disabled!"); 181 return; 182 } 183 184 if (instanceCreate_ > 0) { 185 result.append("ServiceInstanceCreateTime: ").append(std::to_string(instanceCreate_)).append("\n"); 186 } 187 188 if (serviceInit_ > 0) { 189 result.append("ServiceInitTick: ").append(std::to_string(serviceInit_)).append("\n"); 190 } 191 192 if (serviceOnStart_ > 0) { 193 result.append("ServiceStartTick: ").append(std::to_string(serviceOnStart_)).append("\n"); 194 } 195 196 if (serviceOnStop_ > 0) { 197 result.append("ServiceStopTick: ").append(std::to_string(serviceOnStop_)).append("\n"); 198 } 199 200 if (accountBindEnd_ > accountBindBegin_) { 201 result.append("AccountBindTime: ") 202 .append(std::to_string(accountBindEnd_ - accountBindBegin_)) 203 .append("\n"); 204 } 205 206 if (accountAddEnd_ > accountAddBegin_) { 207 result.append("AccountAddTime: ") 208 .append(std::to_string(accountAddEnd_ - accountAddBegin_)) 209 .append("\n"); 210 } 211 212 if (accountDelEnd_ > accountDelBegin_) { 213 result.append("AccountDelTime: ") 214 .append(std::to_string(accountDelEnd_ - accountDelBegin_)) 215 .append("\n"); 216 } 217 218 if (accountQueryEnd_ > accountQueryBegin_) { 219 result.append("AccountQueryTime: ") 220 .append(std::to_string((accountQueryEnd_ - accountQueryBegin_))) 221 .append("\n"); 222 } 223 224 auto iter = accountStateChangeRecords_.begin(); 225 for (; iter != accountStateChangeRecords_.end(); iter++) { 226 result.append(iter->first.c_str()) 227 .append(": ") 228 .append(std::to_string(iter->second)) 229 .append(" Ticks\n"); 230 } 231 } 232 } // namespace AccountSA 233 } // namespace OHOS 234