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 "test_content.h"
16
17 #include <charconv>
18
19 #include "cjson_util.h"
20 #include "event.h"
21 #include "hiview_global.h"
22 #include "hiview_logger.h"
23 #include "trace_caller.h"
24 #include "trace_collector.h"
25
26 namespace OHOS::HiviewDFX {
27 DEFINE_LOG_TAG("TraceTestPlugin");
28 using namespace UCollectUtil;
29 using namespace UCollect;
30
31 namespace {
32 const std::string COMMON_EVENT_TELEMETRY_START = "telemetryStart";
33 const std::string COMMON_EVENT_TELEMETRY_END = "telemetryEnd";
34 const std::string CONFIG_PATH = "/data/log/hiview/unified_collection/trace/telemetry/test_data.json";
35 }
36
TestXperfDump(const EventFwk::CommonEventData & data)37 void TestXperfDump(const EventFwk::CommonEventData &data)
38 {
39 std::string action = data.GetWant().GetAction();
40 auto result = TraceCollector::Create()->DumpTrace(XPERF);
41 if (result.data.empty()) {
42 HIVIEW_LOGW("result event:%{public}s, code:%{public}d empty data file",
43 action.c_str(), result.retCode);
44 return;
45 }
46 HIVIEW_LOGI("result event:%{public}s, code:%{public}d, fileCount:%{public}d, headFile:%{public}s",
47 action.c_str(), result.retCode, result.data.size(), result.data[0].c_str());
48 }
49
TestReliabilityDump(const EventFwk::CommonEventData & data)50 void TestReliabilityDump(const EventFwk::CommonEventData &data)
51 {
52 std::string action = data.GetWant().GetAction();
53 auto result = TraceCollector::Create()->DumpTrace(RELIABILITY);
54 if (result.data.empty()) {
55 HIVIEW_LOGW("result event:%{public}s, code:%{public}d empty data file",
56 action.c_str(), result.retCode);
57 return;
58 }
59 HIVIEW_LOGI("result event:%{public}s, code:%{public}d, fileCount:%{public}d, headFile:%{public}s",
60 action.c_str(), result.retCode, result.data.size(), result.data[0].c_str());
61 }
62
TestXpowerDump(const EventFwk::CommonEventData & data)63 void TestXpowerDump(const EventFwk::CommonEventData &data)
64 {
65 std::string action = data.GetWant().GetAction();
66 auto result = TraceCollector::Create()->DumpTrace(XPOWER);
67 if (result.data.empty()) {
68 HIVIEW_LOGW("result event:%{public}s, code:%{public}d empty data file",
69 action.c_str(), result.retCode);
70 return;
71 }
72 HIVIEW_LOGI("result event:%{public}s, code:%{public}d, fileCount:%{public}d, headFile:%{public}s",
73 action.c_str(), result.retCode, result.data.size(), result.data[0].c_str());
74 }
75
TestOtherDump(const EventFwk::CommonEventData & data)76 void TestOtherDump(const EventFwk::CommonEventData &data)
77 {
78 std::string action = data.GetWant().GetAction();
79 auto result = TraceCollector::Create()->DumpTrace(OTHER);
80 if (result.data.empty()) {
81 HIVIEW_LOGW("result event:%{public}s, code:%{public}d empty data file",
82 action.c_str(), result.retCode);
83 return;
84 }
85 HIVIEW_LOGI("result event:%{public}s, code:%{public}d, fileCount:%{public}d, headFile:%{public}s",
86 action.c_str(), result.retCode, result.data.size(), result.data[0].c_str());
87 }
88
TestScreenDump(const EventFwk::CommonEventData & data)89 void TestScreenDump(const EventFwk::CommonEventData &data)
90 {
91 std::string action = data.GetWant().GetAction();
92 auto result = TraceCollector::Create()->DumpTrace(SCREEN);
93 if (result.data.empty()) {
94 HIVIEW_LOGW("result event:%{public}s, code:%{public}d empty data file",
95 action.c_str(), result.retCode);
96 return;
97 }
98 HIVIEW_LOGI("result event:%{public}s, code:%{public}d, fileCount:%{public}d, headFile:%{public}s",
99 action.c_str(), result.retCode, result.data.size(), result.data[0].c_str());
100 }
101
MakeTelemetryEvent()102 std::shared_ptr<Event> MakeTelemetryEvent()
103 {
104 cJSON* root = CJsonUtil::ParseJsonRoot(CONFIG_PATH);
105 if (root == nullptr) {
106 HIVIEW_LOGI("trace jsonArgs parse error");
107 return nullptr;
108 }
109 if (!cJSON_IsObject(root)) {
110 cJSON_Delete(root);
111 HIVIEW_LOGI("trace jsonArgs parse error");
112 }
113 auto event = std::make_shared<Event>("TelemetrySubscriber");
114 if (auto telemetryId = CJsonUtil::GetStringValue(root, "telemetryId"); !telemetryId.empty()) {
115 event->SetValue("telemetryId", telemetryId);
116 }
117 if (auto traceArgs = CJsonUtil::GetStringValue(root, "traceArgs"); !traceArgs.empty()) {
118 event->SetValue("traceArgs", traceArgs);
119 }
120 if (auto tracePolicy = CJsonUtil::GetStringValue(root, "tracePolicy"); !tracePolicy.empty()) {
121 event->SetValue("tracePolicy", tracePolicy);
122 }
123 if (auto traceOpenTime = CJsonUtil::GetStringValue(root, "traceOpenTime"); !traceOpenTime.empty()) {
124 event->SetValue("traceOpenTime", traceOpenTime);
125 }
126 if (auto appFilterName = CJsonUtil::GetStringValue(root, "appFilterName"); !appFilterName.empty()) {
127 event->SetValue("appFilterName", appFilterName);
128 }
129 if (auto saNames = CJsonUtil::GetStringValue(root, "saNames"); !saNames.empty()) {
130 event->SetValue("saNames", saNames);
131 }
132 if (auto traceDuration = CJsonUtil::GetStringValue(root, "traceDuration"); !traceDuration.empty()) {
133 event->SetValue("traceDuration", traceDuration);
134 }
135 if (auto traceCompressRatio = CJsonUtil::GetStringValue(root, "traceCompressRatio"); !traceCompressRatio.empty()) {
136 event->SetValue("traceCompressRatio", traceCompressRatio);
137 }
138 if (auto xperfTraceQuota = CJsonUtil::GetStringValue(root, "xperfTraceQuota"); !xperfTraceQuota.empty()) {
139 event->SetValue("xperfTraceQuota", xperfTraceQuota);
140 }
141 if (auto xpowerTraceQuota = CJsonUtil::GetStringValue(root, "xpowerTraceQuota"); !xpowerTraceQuota.empty()) {
142 event->SetValue("xpowerTraceQuota", xpowerTraceQuota);
143 }
144 if (auto reliabilityTraceQuota = CJsonUtil::GetStringValue(root, "reliabilityTraceQuota");
145 !reliabilityTraceQuota.empty()) {
146 event->SetValue("reliabilityTraceQuota", reliabilityTraceQuota);
147 }
148 if (auto traceQuota = CJsonUtil::GetStringValue(root, "traceQuota"); !traceQuota.empty()) {
149 event->SetValue("traceQuota", traceQuota);
150 }
151 return event;
152 }
153
TestTelemetryStart(const EventFwk::CommonEventData & data)154 void TestTelemetryStart(const EventFwk::CommonEventData &data)
155 {
156 std::string action = data.GetWant().GetAction();
157 auto event = MakeTelemetryEvent();
158 if (event == nullptr) {
159 HIVIEW_LOGI("event == nullptr check config file parse");
160 return;
161 }
162 event->messageType_ = Event::TELEMETRY_EVENT;
163 event->eventName_ = COMMON_EVENT_TELEMETRY_START;
164 event->SetValue("telemetryStatus", "on");
165 HiviewGlobal::GetInstance()->PostUnorderedEvent(event);
166 }
167
TestTelemetryEnd(const EventFwk::CommonEventData & data)168 void TestTelemetryEnd(const EventFwk::CommonEventData &data)
169 {
170 std::string action = data.GetWant().GetAction();
171 auto event = MakeTelemetryEvent();
172 event->messageType_ = Event::TELEMETRY_EVENT;
173 event->eventName_ = COMMON_EVENT_TELEMETRY_END;
174 event->SetValue("telemetryStatus", "off");
175 HiviewGlobal::GetInstance()->PostUnorderedEvent(event);
176 }
177
TelemetryDumpXpower(const EventFwk::CommonEventData & data)178 void TelemetryDumpXpower(const EventFwk::CommonEventData &data)
179 {
180 std::string action = data.GetWant().GetAction();
181 auto result = TraceCollector::Create()->DumpTraceWithFilter(TeleModule::XPOWER, 0, 0);
182 HIVIEW_LOGI("result event:%{public}s, code:%{public}d", action.c_str(), result.retCode);
183 }
184
TelemetryDumpXPerf(const EventFwk::CommonEventData & data)185 void TelemetryDumpXPerf(const EventFwk::CommonEventData &data)
186 {
187 std::string action = data.GetWant().GetAction();
188 auto result = TraceCollector::Create()->DumpTraceWithFilter(TeleModule::XPERF, 0, 0);
189 HIVIEW_LOGE("result event:%{public}s, code:%{public}d", action.c_str(), result.retCode);
190 }
191
TestTraceOnXpower(const EventFwk::CommonEventData & data)192 void TestTraceOnXpower(const EventFwk::CommonEventData &data)
193 {
194 CollectResult<int32_t> result;
195 std::string timeStr = data.GetData();
196 if (timeStr.empty()) {
197 result = TraceCollector::Create()->FilterTraceOn(TeleModule::XPOWER);
198 HIVIEW_LOGE("traceOn, code:%{public}d", result.retCode);
199 } else {
200 int64_t time = std::stoi(timeStr);
201 result = TraceCollector::Create()->FilterTraceOn(TeleModule::XPOWER, time);
202 HIVIEW_LOGE("postOn delay:%{public} " PRId64 ", code:%{public}d", time, result.retCode);
203 }
204 }
205
TestTraceOffXpower(const EventFwk::CommonEventData & data)206 void TestTraceOffXpower(const EventFwk::CommonEventData &data)
207 {
208 std::string action = data.GetWant().GetAction();
209 auto result = TraceCollector::Create()->FilterTraceOff(TeleModule::XPOWER);
210 HIVIEW_LOGE("result event:%{public}s, code:%{public}d", action.c_str(), result.retCode);
211 }
212
TestTraceOnXPerf(const EventFwk::CommonEventData & data)213 void TestTraceOnXPerf(const EventFwk::CommonEventData &data)
214 {
215 CollectResult<int32_t> result;
216 std::string timeStr = data.GetData();
217 if (timeStr.empty()) {
218 result = TraceCollector::Create()->FilterTraceOn(TeleModule::XPERF);
219 HIVIEW_LOGE("traceOn, code:%{public}d", result.retCode);
220 } else {
221 int64_t time = 0;
222 auto ret = std::from_chars(timeStr.c_str(), timeStr.c_str() + timeStr.size(), time);
223 if (ret.ec != std::errc()) {
224 HIVIEW_LOGE("get time error");
225 return;
226 }
227 result = TraceCollector::Create()->FilterTraceOn(TeleModule::XPERF, time);
228 HIVIEW_LOGE("postOn delay:%{public} " PRId64 ", code:%{public}d", time, result.retCode);
229 }
230 }
231
TestTraceOffXPerf(const EventFwk::CommonEventData & data)232 void TestTraceOffXPerf(const EventFwk::CommonEventData &data)
233 {
234 std::string action = data.GetWant().GetAction();
235 auto result = TraceCollector::Create()->FilterTraceOff(TeleModule::XPERF);
236 HIVIEW_LOGE("result event:%{public}s, code:%{public}d", action.c_str(), result.retCode);
237 }
238 }
239