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