1 /*
2 * Copyright (c) 2023-2024 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 "decorator.h"
17
18 namespace OHOS {
19 namespace HiviewDFX {
20 namespace UCollectUtil {
21 const auto UC_STAT_LOG_PATH = "/data/log/hiview/unified_collection/ucollection_stat_detail.log";
22 const auto UC_SEPARATOR = "::";
23 const auto UC_STAT_DATE = "Date:";
24 const auto UC_API_STAT_TITLE = "API statistics:";
25 const auto UC_API_STAT_ITEM = "API TotalCall FailCall AvgLatency(us) MaxLatency(us) TotalTimeSpent(us)";
26
UpdateStatInfo(uint64_t startTime,uint64_t endTime,const std::string & funcName,bool isCallSucc)27 void StatInfoWrapper::UpdateStatInfo(uint64_t startTime, uint64_t endTime, const std::string& funcName, bool isCallSucc)
28 {
29 std::lock_guard<std::mutex> lock(mutex_);
30 uint64_t latency = (endTime - startTime) > 0 ? (endTime - startTime) : 0;
31 if (statInfos_.find(funcName) == statInfos_.end()) {
32 StatInfo statInfo = {
33 .name = funcName,
34 .totalCall = 1,
35 .failCall = isCallSucc ? 0 : 1,
36 .avgLatency = latency,
37 .maxLatency = latency,
38 .totalTimeSpend = latency,
39 };
40 statInfos_.insert(std::make_pair(funcName, statInfo));
41 return;
42 }
43
44 StatInfo& statInfo = statInfos_[funcName];
45 statInfo.totalCall += 1;
46 statInfo.failCall += isCallSucc ? 0 : 1;
47 statInfo.totalTimeSpend += latency;
48 statInfo.maxLatency = statInfo.maxLatency < latency ? latency : statInfo.maxLatency;
49 if (statInfo.totalCall > 0) {
50 statInfo.avgLatency = statInfo.totalTimeSpend / statInfo.totalCall;
51 }
52 }
53
GetStatInfo()54 std::map<std::string, StatInfo> StatInfoWrapper::GetStatInfo()
55 {
56 std::lock_guard<std::mutex> lock(mutex_);
57 return statInfos_;
58 }
59
ResetStatInfo()60 void StatInfoWrapper::ResetStatInfo()
61 {
62 std::lock_guard<std::mutex> lock(mutex_);
63 statInfos_.clear();
64 }
65 } // namespace UCollectUtil
66 } // namespace HiviewDFX
67 } // namespace OHOS
68