1 /*
2 * Copyright (c) 2025 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 "xperf_event_builder.h"
16 #include <cstdlib>
17 #include <cstring>
18 #include <securec.h>
19 #include "hiview_logger.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23
24 DEFINE_LOG_LABEL(0xD002D66, "Hiview-XPerformance");
25
XperfEventBuilder()26 XperfEventBuilder::XperfEventBuilder()
27 {
28 }
29
~XperfEventBuilder()30 XperfEventBuilder::~XperfEventBuilder()
31 {
32 }
33
SetParamName(HiSysEventParam & param,const char * name)34 void XperfEventBuilder::SetParamName(HiSysEventParam& param, const char* name)
35 {
36 int ret = strcpy_s(param.name, sizeof(param.name), name);
37 if (ret != 0) {
38 HIVIEW_LOGE("XperfEventBuilder::SetParamName error, err is %{public}d.", ret);
39 }
40 }
41
EventName(const std::string & eventName)42 XperfEventBuilder& XperfEventBuilder::EventName(const std::string& eventName)
43 {
44 this->evtName = eventName;
45 return *this;
46 }
47
EventType(const HiSysEventEventType & eventType)48 XperfEventBuilder& XperfEventBuilder::EventType(const HiSysEventEventType& eventType)
49 {
50 this->evtType = eventType;
51 return *this;
52 }
53
Param(const char * name,bool value)54 XperfEventBuilder& XperfEventBuilder::Param(const char* name, bool value)
55 {
56 HiSysEventParam param = {
57 .t = HISYSEVENT_BOOL,
58 .v = { .b = value },
59 .arraySize = 0,
60 };
61 SetParamName(param, name);
62 paramList.push_back(param);
63 return *this;
64 }
65
Param(const char * name,int8_t value)66 XperfEventBuilder& XperfEventBuilder::Param(const char* name, int8_t value)
67 {
68 HiSysEventParam param = {
69 .t = HISYSEVENT_INT8,
70 .v = { .i8 = value },
71 .arraySize = 0,
72 };
73 SetParamName(param, name);
74 paramList.push_back(param);
75 return *this;
76 }
77
Param(const char * name,uint8_t value)78 XperfEventBuilder& XperfEventBuilder::Param(const char* name, uint8_t value)
79 {
80 HiSysEventParam param = {
81 .t = HISYSEVENT_UINT8,
82 .v = { .ui8 = value },
83 .arraySize = 0,
84 };
85 SetParamName(param, name);
86 paramList.push_back(param);
87 return *this;
88 }
89
Param(const char * name,int16_t value)90 XperfEventBuilder& XperfEventBuilder::Param(const char* name, int16_t value)
91 {
92 HiSysEventParam param = {
93 .t = HISYSEVENT_INT16,
94 .v = { .i16 = value },
95 .arraySize = 0,
96 };
97 SetParamName(param, name);
98 paramList.push_back(param);
99 return *this;
100 }
101
Param(const char * name,uint16_t value)102 XperfEventBuilder& XperfEventBuilder::Param(const char* name, uint16_t value)
103 {
104 HiSysEventParam param = {
105 .t = HISYSEVENT_UINT16,
106 .v = { .ui16 = value },
107 .arraySize = 0,
108 };
109 SetParamName(param, name);
110 paramList.push_back(param);
111 return *this;
112 }
113
Param(const char * name,int32_t value)114 XperfEventBuilder& XperfEventBuilder::Param(const char* name, int32_t value)
115 {
116 HiSysEventParam param = {
117 .t = HISYSEVENT_INT32,
118 .v = { .i32 = value },
119 .arraySize = 0,
120 };
121 SetParamName(param, name);
122 paramList.push_back(param);
123 return *this;
124 }
125
Param(const char * name,uint32_t value)126 XperfEventBuilder& XperfEventBuilder::Param(const char* name, uint32_t value)
127 {
128 HiSysEventParam param = {
129 .t = HISYSEVENT_UINT32,
130 .v = { .ui32 = value },
131 .arraySize = 0,
132 };
133 SetParamName(param, name);
134 paramList.push_back(param);
135 return *this;
136 }
137
Param(const char * name,int64_t value)138 XperfEventBuilder& XperfEventBuilder::Param(const char* name, int64_t value)
139 {
140 HiSysEventParam param = {
141 .t = HISYSEVENT_INT64,
142 .v = { .i64 = value },
143 .arraySize = 0,
144 };
145 SetParamName(param, name);
146 paramList.push_back(param);
147 return *this;
148 }
149
Param(const char * name,uint64_t value)150 XperfEventBuilder& XperfEventBuilder::Param(const char* name, uint64_t value)
151 {
152 HiSysEventParam param = {
153 .t = HISYSEVENT_UINT64,
154 .v = { .ui64 = value },
155 .arraySize = 0,
156 };
157 SetParamName(param, name);
158 paramList.push_back(param);
159 return *this;
160 }
161
Param(const char * name,float value)162 XperfEventBuilder& XperfEventBuilder::Param(const char* name, float value)
163 {
164 HiSysEventParam param = {
165 .t = HISYSEVENT_FLOAT,
166 .v = { .f= value },
167 .arraySize = 0,
168 };
169 SetParamName(param, name);
170 paramList.push_back(param);
171 return *this;
172 }
173
Param(const char * name,double value)174 XperfEventBuilder& XperfEventBuilder::Param(const char* name, double value)
175 {
176 HiSysEventParam param = {
177 .t = HISYSEVENT_DOUBLE,
178 .v = { .d = value },
179 .arraySize = 0,
180 };
181 SetParamName(param, name);
182 paramList.push_back(param);
183 return *this;
184 }
185
Param(const char * name,const std::vector<uint16_t> & value)186 XperfEventBuilder& XperfEventBuilder::Param(const char* name, const std::vector<uint16_t>& value)
187 {
188 HiSysEventParam param = {
189 .t = HISYSEVENT_UINT16_ARRAY,
190 .v = { .array = static_cast<void*>(const_cast<uint16_t*>(value.data())) },
191 .arraySize = value.size(),
192 };
193 SetParamName(param, name);
194 paramList.push_back(param);
195 return *this;
196 }
197
Param(const char * name,const std::string value)198 XperfEventBuilder& XperfEventBuilder::Param(const char* name, const std::string value)
199 {
200 int len = static_cast<int>(value.length() + 1);
201 char* cStr = new char[len];
202 int ret = strcpy_s(cStr, len, value.c_str());
203 if (ret != 0) {
204 HIVIEW_LOGE("XperfEventBuilder::InsertParam string error, err is %{public}d.", ret);
205 }
206 return this->Param(name, cStr);
207 }
208
Param(const char * name,char * value)209 XperfEventBuilder& XperfEventBuilder::Param(const char* name, char* value)
210 {
211 HiSysEventParam param = {
212 .t = HISYSEVENT_STRING,
213 .v = { .s = value },
214 .arraySize = 0,
215 };
216 SetParamName(param, name);
217 paramList.push_back(param);
218 return *this;
219 }
220
Build()221 OHOS::HiviewDFX::XperfEvent XperfEventBuilder::Build()
222 {
223 if (!Check()) {
224 OHOS::HiviewDFX::XperfEvent defaultEvent;
225 return defaultEvent;
226 }
227 int size = static_cast<int>(paramList.size());
228 OHOS::HiviewDFX::XperfEvent event(this->evtName, this->evtType, size);
229 for (int i = 0; i < size; i++) {
230 event.params[i] = paramList.at(i);
231 }
232 return event;
233 }
234
Check()235 bool XperfEventBuilder::Check()
236 {
237 if (evtName.empty()) {
238 HIVIEW_LOGE("[XperfEventBuilder::Check] evtName is empty");
239 Release();
240 return false;
241 }
242 if (paramList.size() == 0) {
243 HIVIEW_LOGE("[XperfEventBuilder::Check] paramList is empty");
244 return false;
245 }
246 return true;
247 }
248
Release()249 void XperfEventBuilder::Release()
250 {
251 HIVIEW_LOGD("[XperfEventBuilder::Release]");
252 for (auto it = paramList.begin(); it != paramList.end(); it++) {
253 if (((*it).t == HISYSEVENT_STRING) && ((*it).v.s != nullptr)) {
254 delete[] (*it).v.s;
255 (*it).v.s = nullptr;
256 }
257 }
258 }
259
260 }
261 }