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 #include "perform_reporter.h"
17 #include "window_manager_hilog.h"
18
19 #include <hisysevent.h>
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "PerformReporter"};
25 }
26
27 /**
28 * @brief Construct a new Perform Reporter:: Perform Reporter object
29 *
30 * @param tag A tag that in report string
31 * @param timeSpiltsMs The time-interval that data statistic, details look up the comments in function body
32 * @param reportInterval Report data after reportInterval round start-end
33 */
PerformReporter(const std::string & tag,const std::vector<int64_t> & timeSpiltsMs,uint32_t reportInterval)34 PerformReporter::PerformReporter(const std::string& tag,
35 const std::vector<int64_t>& timeSpiltsMs, uint32_t reportInterval)
36 : tag_(tag), reportInterval_(reportInterval)
37 {
38 // re-organ data struct
39 // a, b, c, d -->
40 // (0, a] : cnt=0, (a, b] : cnt=0, (b, c] : cnt=0, (c, d] : cnt=0
41 for (auto split : timeSpiltsMs) {
42 timeSplitCount_[split] = 0;
43 }
44 // (d, +limit] : cnt=0
45 timeSplitCount_[BARRIER] = 0;
46 totalCount_ = 0;
47 }
48
start()49 void PerformReporter::start()
50 {
51 startTime_ = std::chrono::steady_clock::now();
52 }
53
end()54 void PerformReporter::end()
55 {
56 auto currentTime = std::chrono::steady_clock::now();
57 int64_t costTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime_).count();
58
59 count(costTime);
60
61 bool repSucc = report();
62 if (repSucc) {
63 clear();
64 }
65 }
66
report()67 bool PerformReporter::report()
68 {
69 if (totalCount_ < reportInterval_) {
70 return false;
71 }
72
73 std::ostringstream oss;
74 oss << tag_ << ": ";
75 auto maxSplit = 0;
76 for (const auto& iter: timeSplitCount_) {
77 if (iter.first != BARRIER) {
78 oss << "BELLOW" << iter.first << "(ms): " << iter.second << ", ";
79 maxSplit = iter.first;
80 }
81 }
82 oss << "ABOVE" << maxSplit << "(ms): " << timeSplitCount_[BARRIER];
83
84 int32_t ret = OHOS::HiviewDFX::HiSysEvent::Write(
85 OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER, tag_,
86 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "MSG", oss.str());
87 WLOGFI("Write HiSysEvent ret:%{public}d", ret);
88 return ret == 0;
89 }
90
count(int64_t costTime)91 void PerformReporter::count(int64_t costTime)
92 {
93 totalCount_++;
94 for (auto& iter: timeSplitCount_) {
95 if (costTime <= iter.first) {
96 iter.second++;
97 break;
98 }
99 }
100
101 std::ostringstream oss;
102 oss << tag_ << " cost " << costTime << "ms, total count " << totalCount_;
103 WLOGFI("%{public}s", oss.str().c_str());
104 }
105
clear()106 void PerformReporter::clear()
107 {
108 totalCount_ = 0;
109 for (auto& iter: timeSplitCount_) {
110 iter.second = 0;
111 }
112 }
113 }
114 }