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 #include "plugin_stats_event.h"
16
17 #include "hiview_event_common.h"
18
19 namespace OHOS {
20 namespace HiviewDFX {
21 namespace {
22 constexpr int TOP_K_NUM = 3;
23 constexpr uint32_t NUM_ADD_PER = 1;
24 const std::vector<uint32_t> INIT_TOP_K_TIME(TOP_K_NUM, 0);
25 const std::vector<std::string> INIT_TOP_K_EVENT(TOP_K_NUM, "");
26 }
27 using namespace PluginStatsEventSpace;
28
PluginStatsEvent(const std::string & name,HiSysEvent::EventType type)29 PluginStatsEvent::PluginStatsEvent(const std::string &name, HiSysEvent::EventType type)
30 : LoggerEvent(name, type)
31 {
32 paramMap_ = {
33 {std::string(KEY_OF_PLUGIN_NAME), std::string(DEFAULT_STRING)},
34 {std::string(KEY_OF_PROC_NAME), std::string(DEFAULT_STRING)},
35 {std::string(KEY_OF_PROC_TIME), DEFAULT_UINT32}, {std::string(KEY_OF_TOTAL_TIME), DEFAULT_UINT64},
36 {std::string(KEY_OF_AVG_TIME), DEFAULT_UINT32}, {std::string(KEY_OF_TOP_K_TIME), INIT_TOP_K_TIME},
37 {std::string(KEY_OF_TOP_K_EVENT), INIT_TOP_K_EVENT}, {std::string(KEY_OF_TOTAL), DEFAULT_UINT32}
38 };
39 }
40
UpdateTotalNum()41 void PluginStatsEvent::UpdateTotalNum()
42 {
43 paramMap_[KEY_OF_TOTAL] = paramMap_[KEY_OF_TOTAL].GetUint32() + NUM_ADD_PER;
44 }
45
UpdateTotalTime()46 void PluginStatsEvent::UpdateTotalTime()
47 {
48 paramMap_[KEY_OF_TOTAL_TIME] = paramMap_[KEY_OF_TOTAL_TIME].GetUint64() + paramMap_[KEY_OF_PROC_TIME].GetUint32();
49 }
50
UpdateAvgTime()51 void PluginStatsEvent::UpdateAvgTime()
52 {
53 paramMap_[KEY_OF_AVG_TIME] = static_cast<uint32_t>(paramMap_[KEY_OF_TOTAL_TIME].GetUint64()
54 / paramMap_[KEY_OF_TOTAL].GetUint32());
55 }
56
UpdateTopK()57 void PluginStatsEvent::UpdateTopK()
58 {
59 uint32_t procTime = paramMap_[KEY_OF_PROC_TIME].GetUint32();
60 auto topKTime = paramMap_[KEY_OF_TOP_K_TIME].GetUint32Vec();
61 if (procTime <= topKTime.back()) {
62 return;
63 }
64
65 auto it = std::find_if(topKTime.begin(), topKTime.end(), [procTime] (auto t) {
66 return procTime > t;
67 });
68 auto index = it - topKTime.begin();
69 topKTime.insert(topKTime.begin() + index, procTime);
70 topKTime.pop_back();
71 paramMap_[KEY_OF_TOP_K_TIME] = topKTime;
72
73 auto topKEvent = paramMap_[KEY_OF_TOP_K_EVENT].GetStringVec();
74 topKEvent.insert(topKEvent.begin() + index, paramMap_[KEY_OF_PROC_NAME].GetString());
75 topKEvent.pop_back();
76 paramMap_[KEY_OF_TOP_K_EVENT] = topKEvent;
77 }
78
InnerUpdate(const std::string & name,const ParamValue & value)79 void PluginStatsEvent::InnerUpdate(const std::string &name, const ParamValue& value)
80 {
81 LoggerEvent::InnerUpdate(name, value);
82 if (name.compare(KEY_OF_PROC_TIME) == 0) {
83 UpdateTotalNum();
84 UpdateTotalTime();
85 UpdateAvgTime();
86 UpdateTopK();
87 }
88 }
89
Report()90 void PluginStatsEvent::Report()
91 {
92 HiSysEventWrite(HiSysEvent::Domain::HIVIEWDFX, eventName_, eventType_,
93 KEY_OF_PLUGIN_NAME, paramMap_[KEY_OF_PLUGIN_NAME].GetString(),
94 KEY_OF_AVG_TIME, paramMap_[KEY_OF_AVG_TIME].GetUint32(),
95 KEY_OF_TOP_K_TIME, paramMap_[KEY_OF_TOP_K_TIME].GetUint32Vec(),
96 KEY_OF_TOP_K_EVENT, paramMap_[KEY_OF_TOP_K_EVENT].GetStringVec(),
97 KEY_OF_TOTAL, paramMap_[KEY_OF_TOTAL].GetUint32());
98 }
99 } // namespace HiviewDFX
100 } // namespace OHOS
101