1 /*
2 * Copyright (c) 2023 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 "unified_collection_stat.h"
17
18 #include "file_util.h"
19 #include "hisysevent.h"
20 #include "logger.h"
21 #include "time_util.h"
22
23 #include "cpu_decorator.h"
24 #include "gpu_decorator.h"
25 #include "io_decorator.h"
26 #ifdef HAS_HIPROFILER
27 #include "mem_profiler_decorator.h"
28 #endif
29 #include "memory_decorator.h"
30 #include "network_decorator.h"
31 #ifdef HAS_HIPERF
32 #include "perf_decorator.h"
33 #endif
34 #include "trace_decorator.h"
35
36 namespace OHOS {
37 namespace HiviewDFX {
38 namespace UCollectUtil {
39 DEFINE_LOG_TAG("UCollectUtil-UCStat");
40 const std::string UC_STAT_DATE = "Date:";
41 const std::string UC_API_STAT_TITLE = "API statistics:";
42 const std::string UC_API_STAT_ITEM =
43 "API TotalCall FailCall AvgLatency(us) MaxLatency(us) TotalTimeSpent(us)";
44
45 namespace {
GetCurrentDate()46 std::string GetCurrentDate()
47 {
48 return TimeUtil::TimestampFormatToDate(std::time(nullptr), "%Y-%m-%d");
49 }
50 }
51
52 std::string UnifiedCollectionStat::date_ = GetCurrentDate();
53
Report()54 void UnifiedCollectionStat::Report()
55 {
56 if (date_ == GetCurrentDate()) {
57 return;
58 }
59 HIVIEW_LOGI("date_=%{public}s, curDate=%{public}s", date_.c_str(), GetCurrentDate().c_str());
60 SaveAllStatInfo();
61 ResetAllStatInfo();
62 date_ = GetCurrentDate();
63 }
64
SaveAllStatInfo()65 void UnifiedCollectionStat::SaveAllStatInfo()
66 {
67 UCDecorator::WriteLinesToFile({UC_STAT_DATE, date_}, true);
68 UCDecorator::WriteLinesToFile({UC_API_STAT_TITLE, UC_API_STAT_ITEM}, false);
69 CpuDecorator::SaveStatCommonInfo();
70 GpuDecorator::SaveStatCommonInfo();
71 IoDecorator::SaveStatCommonInfo();
72 MemoryDecorator::SaveStatCommonInfo();
73 NetworkDecorator::SaveStatCommonInfo();
74 TraceDecorator::SaveStatCommonInfo();
75 #ifdef HAS_HIPROFILER
76 MemProfilerDecorator::SaveStatCommonInfo();
77 #endif
78 #ifdef HAS_HIPERF
79 PerfDecorator::SaveStatCommonInfo();
80 #endif
81
82 TraceDecorator::SaveStatSpecialInfo();
83
84 int32_t ret = HiSysEventWrite(
85 HiSysEvent::Domain::HIVIEWDFX,
86 "UC_API_STAT",
87 HiSysEvent::EventType::FAULT,
88 "STAT_DATE", date_);
89 if (ret != 0) {
90 HIVIEW_LOGW("report collection stat event fail, ret=%{public}d", ret);
91 }
92 }
93
ResetAllStatInfo()94 void UnifiedCollectionStat::ResetAllStatInfo()
95 {
96 CpuDecorator::ResetStatInfo();
97 GpuDecorator::ResetStatInfo();
98 IoDecorator::ResetStatInfo();
99 MemoryDecorator::ResetStatInfo();
100 NetworkDecorator::ResetStatInfo();
101 TraceDecorator::ResetStatInfo();
102 #ifdef HAS_HIPROFILER
103 MemProfilerDecorator::ResetStatInfo();
104 #endif
105 #ifdef HAS_HIPERF
106 PerfDecorator::ResetStatInfo();
107 #endif
108 }
109 } // namespace UCollectUtil
110 } // namespace HiviewDFX
111 } // namespace OHOS
112