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