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 <fstream>
17 #include <regex>
18 #include <string>
19 #include <fcntl.h>
20 #include <gtest/gtest.h>
21 #include <hilog/log.h>
22
23 #include "hitrace_meter.h"
24 #include "hitrace_osal.h"
25 #include "parameters.h"
26
27 using namespace testing::ext;
28 using namespace std;
29 using namespace OHOS::HiviewDFX;
30 using namespace OHOS::HiviewDFX::HitraceOsal;
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 namespace HitraceTest {
35 const string TRACE_MARKER_PATH = "trace_marker";
36 const string TRACING_ON_PATH = "tracing_on";
37 const string TRACING_ON = "tracing_on";
38 const string TRACE_PATH = "trace";
39 const string TRACE_MARK_WRITE = "tracing_mark_write";
40 const string TRACE_PATTERN = "\\s*(.*?)-(.*?)\\s+(.*?)\\[(\\d+?)\\]\\s+(.*?)\\s+((\\d+).(\\d+)?):\\s+"
41 + TRACE_MARK_WRITE + ": ";
42 const string TRACE_START = TRACE_PATTERN + "B\\|(.*?)\\|H:";
43 const string TRACE_FINISH = TRACE_PATTERN + "E\\|";
44 const string TRACE_ASYNC_START = TRACE_PATTERN + "S\\|(.*?)\\|H:";
45 const string TRACE_ASYNC_FINISH = TRACE_PATTERN + "F\\|(.*?)\\|H:";
46 const string TRACE_COUNT = TRACE_PATTERN + "C\\|(.*?)\\|H:";
47 const string TRACE_PROPERTY = "debug.hitrace.tags.enableflags";
48 const string KEY_RO_DEBUGGABLE = "ro.debuggable";
49 const string KEY_APP_NUMBER = "debug.hitrace.app_number";
50 constexpr uint32_t TASK = 1;
51 constexpr uint32_t TID = 2;
52 constexpr uint32_t TGID = 3;
53 constexpr uint32_t CPU = 4;
54 constexpr uint32_t DNH2 = 5;
55 constexpr uint32_t TIMESTAMP = 6;
56 constexpr uint32_t PID = 9;
57 constexpr uint32_t TRACE_NAME = 10;
58 constexpr uint32_t NUM = 11;
59
60 constexpr uint32_t TRACE_FMA11 = 11;
61 constexpr uint32_t TRACE_FMA12 = 12;
62
63 constexpr uint64_t TRACE_INVALIDATE_TAG = 0x1000000;
64 constexpr uint64_t HITRACE_TAG = 0xD002D33;
65 const constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HITRACE_TAG, "Hitrace_TEST"};
66 const uint64_t TAG = HITRACE_TAG_OHOS;
67 static string g_traceRootPath;
68
69 bool SetProperty(const string& property, const string& value);
70 string GetProperty(const string& property, const string& value);
71 bool CleanTrace();
72 bool CleanFtrace();
73 bool SetFtrace(const string& filename, bool enabled);
74
75 class HitraceNDKTest : public testing::Test {
76 public:
77 static void SetUpTestCase(void);
78 static void TearDownTestCase(void);
79 void SetUp();
TearDown()80 void TearDown() {}
81 };
82
SetUpTestCase()83 void HitraceNDKTest::SetUpTestCase()
84 {
85 const string debugfsDir = "/sys/kernel/debug/tracing/";
86 const string tracefsDir = "/sys/kernel/tracing/";
87 if (access((debugfsDir + TRACE_MARKER_PATH).c_str(), F_OK) != -1) {
88 g_traceRootPath = debugfsDir;
89 } else if (access((tracefsDir + TRACE_MARKER_PATH).c_str(), F_OK) != -1) {
90 g_traceRootPath = tracefsDir;
91 } else {
92 HiLog::Error(LABEL, "Error: Finding trace folder failed");
93 }
94 CleanFtrace();
95 }
96
TearDownTestCase()97 void HitraceNDKTest::TearDownTestCase()
98 {
99 SetProperty(TRACE_PROPERTY, "0");
100 SetFtrace(TRACING_ON, false);
101 CleanTrace();
102 }
103
SetUp()104 void HitraceNDKTest::SetUp()
105 {
106 ASSERT_TRUE(CleanTrace());
107 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
108 string value = to_string(TAG);
109 SetProperty(TRACE_PROPERTY, value);
110 HiLog::Info(LABEL, "current tag is %{public}s", GetProperty(TRACE_PROPERTY, "0").c_str());
111 ASSERT_TRUE(GetProperty(TRACE_PROPERTY, "-123") == value);
112 UpdateTraceLabel();
113 }
114
115 struct Param {
116 string m_task;
117 string m_tid;
118 string m_tgid;
119 string m_cpu;
120 string m_dnh2;
121 string m_timestamp;
122 string m_pid;
123 string m_traceName;
124 string m_num;
125 };
126
127 class MyTrace {
128 Param m_param;
129 bool m_loaded = false;
130 public:
MyTrace()131 MyTrace() : m_loaded(false)
132 {
133 m_param.m_task = "";
134 m_param.m_tid = "";
135 m_param.m_tgid = "";
136 m_param.m_cpu = "";
137 m_param.m_dnh2 = "";
138 m_param.m_timestamp = "";
139 m_param.m_pid = "";
140 m_param.m_traceName = "";
141 m_param.m_num = "";
142 }
143
~MyTrace()144 ~MyTrace()
145 {
146 }
147
148 // task-pid ( tig) [cpu] ...1 timestamp: tracing_mark_write: B|pid|traceName
149 // task-pid ( tig) [cpu] ...1 timestamp: tracing_mark_write: E|pid
Load(const Param & param)150 void Load(const Param& param)
151 {
152 m_param.m_task = param.m_task;
153 m_param.m_pid = param.m_pid;
154 m_param.m_tid = param.m_tid;
155 m_param.m_tgid = param.m_tgid;
156 m_param.m_cpu = param.m_cpu;
157 m_param.m_dnh2 = param.m_dnh2;
158 m_param.m_timestamp = param.m_timestamp;
159 m_param.m_traceName = param.m_traceName;
160 m_param.m_num = param.m_num;
161 m_loaded = true;
162 }
163
GetTask()164 string GetTask()
165 {
166 return m_param.m_task;
167 }
168
GetPid()169 string GetPid()
170 {
171 if (m_loaded) {
172 return m_param.m_pid;
173 }
174 return "";
175 }
176
GetTgid()177 string GetTgid()
178 {
179 if (m_loaded) {
180 return m_param.m_tgid;
181 }
182 return "";
183 }
184
GetCpu()185 string GetCpu()
186 {
187 if (m_loaded) {
188 return m_param.m_cpu;
189 }
190 return "";
191 }
192
GetDnh2()193 string GetDnh2()
194 {
195 if (m_loaded) {
196 return m_param.m_dnh2;
197 }
198 return "";
199 }
200
GetTimestamp()201 string GetTimestamp()
202 {
203 if (m_loaded) {
204 return m_param.m_timestamp;
205 }
206 return "";
207 }
208
GetTraceName()209 string GetTraceName()
210 {
211 if (m_loaded) {
212 return m_param.m_traceName;
213 }
214 return "";
215 }
216
GetNum()217 string GetNum()
218 {
219 if (m_loaded) {
220 return m_param.m_num;
221 }
222 return "";
223 }
224
GetTid()225 string GetTid()
226 {
227 if (m_loaded) {
228 return m_param.m_tid;
229 }
230 return "";
231 }
232
IsLoaded() const233 bool IsLoaded() const
234 {
235 return m_loaded;
236 }
237 };
238
SetProperty(const string & property,const string & value)239 bool SetProperty(const string& property, const string& value)
240 {
241 bool result = false;
242 result = OHOS::system::SetParameter(property, value);
243 if (!result) {
244 HiLog::Error(LABEL, "Error: setting %s failed", property.c_str());
245 return false;
246 }
247 return true;
248 }
249
GetProperty(const string & property,const string & value)250 string GetProperty(const string& property, const string& value)
251 {
252 return OHOS::system::GetParameter(property, value);
253 }
254
GetTimeDuration(int64_t time1,int64_t time2,int64_t diffRange)255 bool GetTimeDuration(int64_t time1, int64_t time2, int64_t diffRange)
256 {
257 int64_t duration = time2 - time1;
258 return (duration > 0) && (duration <= diffRange ? true : false);
259 }
260
Trim(string & s)261 string& Trim(string& s)
262 {
263 if (s.empty()) {
264 return s;
265 }
266 s.erase(0, s.find_first_not_of(" "));
267 s.erase(s.find_last_not_of(" ") + 1);
268 return s;
269 }
270
GetTimeStamp(string str)271 int64_t GetTimeStamp(string str)
272 {
273 if (str == "") {
274 return 0;
275 }
276 int64_t time;
277 Trim(str);
278 time = atol(str.erase(str.find("."), 1).c_str());
279 return time;
280 }
281
GetTraceResult(const string & checkContent,const vector<string> & list)282 MyTrace GetTraceResult(const string& checkContent, const vector<string>& list)
283 {
284 MyTrace trace;
285 if (list.empty() || checkContent == "") {
286 return trace;
287 }
288 regex pattern(checkContent);
289 smatch match;
290 Param param {""};
291 for (int i = list.size() - 1; i >= 0; i--) {
292 if (regex_match(list[i], match, pattern)) {
293 param.m_task = match[TASK];
294 param.m_tid = match[TID];
295 param.m_tgid = match[TGID];
296 param.m_cpu = match[CPU];
297 param.m_dnh2 = match[DNH2];
298 param.m_timestamp = match[TIMESTAMP];
299 param.m_pid = match[PID];
300 if (match.size() == TRACE_FMA11) {
301 param.m_traceName = match[TRACE_NAME],
302 param.m_num = "";
303 } else if (match.size() == TRACE_FMA12) {
304 param.m_traceName = match[TRACE_NAME],
305 param.m_num = match[NUM];
306 } else {
307 param.m_traceName = "";
308 param.m_num = "";
309 }
310 trace.Load(param);
311 break;
312 }
313 }
314 return trace;
315 }
316
WriteStringToFile(const string & fileName,const string & str)317 static bool WriteStringToFile(const string& fileName, const string& str)
318 {
319 if (g_traceRootPath.empty()) {
320 HiLog::Error(LABEL, "Error: trace path not found.");
321 return false;
322 }
323 ofstream out;
324 out.open(g_traceRootPath + fileName, ios::out);
325 out << str;
326 out.close();
327 return true;
328 }
329
CleanTrace()330 bool CleanTrace()
331 {
332 if (g_traceRootPath.empty()) {
333 HiLog::Error(LABEL, "Error: trace path not found.");
334 return false;
335 }
336 ofstream ofs;
337 ofs.open(g_traceRootPath + TRACE_PATH, ofstream::out);
338 if (!ofs.is_open()) {
339 HiLog::Error(LABEL, "Error: opening trace path failed.");
340 return false;
341 }
342 ofs << "";
343 ofs.close();
344 return true;
345 }
346
ReadFile(const string & filename)347 static stringstream ReadFile(const string& filename)
348 {
349 stringstream ss;
350 char resolvedPath[PATH_MAX] = { 0 };
351 if (realpath(filename.c_str(), resolvedPath) == nullptr) {
352 fprintf(stderr, "Error: _fullpath %s failed", filename.c_str());
353 return ss;
354 }
355 ifstream fin(resolvedPath);
356 if (!fin.is_open()) {
357 fprintf(stderr, "opening file: %s failed!", filename.c_str());
358 return ss;
359 }
360 ss << fin.rdbuf();
361 fin.close();
362 return ss;
363 }
364
IsFileExisting(const string & filename)365 static bool IsFileExisting(const string& filename)
366 {
367 return access(filename.c_str(), F_OK) != -1;
368 }
369
SetFtrace(const string & filename,bool enabled)370 bool SetFtrace(const string& filename, bool enabled)
371 {
372 return WriteStringToFile(filename, enabled ? "1" : "0");
373 }
374
CleanFtrace()375 bool CleanFtrace()
376 {
377 return WriteStringToFile("set_event", "");
378 }
379
GetFinishTraceRegex(MyTrace & trace)380 string GetFinishTraceRegex(MyTrace& trace)
381 {
382 if (!trace.IsLoaded()) {
383 return "";
384 } else {
385 return "\\s*(.*?)-(" + trace.GetTid() + "?)\\s+(.*?)\\[(\\d+?)\\]\\s+(.*?)\\s+" + "((\\d+).(\\d+)?):\\s+" +
386 TRACE_MARK_WRITE + ": E\\|(" + trace.GetPid() + ")|(.*)";
387 }
388 }
389
ReadFile2string(const string & filename)390 vector<string> ReadFile2string(const string& filename)
391 {
392 vector<string> list;
393 if (IsFileExisting(filename)) {
394 stringstream ss = ReadFile(filename);
395 string line;
396 while (getline(ss, line)) {
397 list.emplace_back(move(line));
398 }
399 }
400 return list;
401 }
402
ReadTrace()403 vector<string> ReadTrace()
404 {
405 return ReadFile2string(g_traceRootPath + TRACE_PATH);
406 }
407
RunCmd(const string & cmdstr)408 bool RunCmd(const string& cmdstr)
409 {
410 FILE *fp = popen(cmdstr.c_str(), "r");
411 if (fp == nullptr) {
412 return false;
413 }
414 pclose(fp);
415 return true;
416 }
417
418 /**
419 * @tc.name: Hitrace
420 * @tc.desc: tracing_mark_write file node normal output start tracing and end tracing.
421 * @tc.type: FUNC
422 */
423 HWTEST_F(HitraceNDKTest, StartTrace_001, TestSize.Level0)
424 {
425 ASSERT_TRUE(CleanTrace());
426 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
427 StartTrace(TAG, "StartTraceTest001");
428 FinishTrace(TAG);
429 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
430 vector<string> list = ReadTrace();
431 MyTrace startTrace = GetTraceResult(TRACE_START + "(StartTraceTest001) ", list);
432 ASSERT_TRUE(startTrace.IsLoaded()) << "Can't find \"B|pid|StartTraceTest001\" from trace.";
433 MyTrace finishTrace = GetTraceResult(GetFinishTraceRegex(startTrace), list);
434 ASSERT_TRUE(finishTrace.IsLoaded()) << "Can't find \"E|\" from trace.";
435 }
436
437 /**
438 * @tc.name: Hitrace
439 * @tc.desc: tracing_mark_write file node has no output.
440 * @tc.type: FUNC
441 */
442 HWTEST_F(HitraceNDKTest, StartTrace_002, TestSize.Level0)
443 {
444 ASSERT_TRUE(CleanTrace());
445 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
446 StartTrace(TAG, "StartTraceTest002");
447 FinishTrace(TAG);
448 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
449 vector<string> list = ReadTrace();
450 MyTrace startTrace = GetTraceResult(TRACE_START + "(StartTraceTest002) ", list);
451 ASSERT_TRUE(startTrace.IsLoaded()) << "Can't find \"B|pid|StartTraceTest002\" from trace.";
452 MyTrace finishTrace = GetTraceResult(GetFinishTraceRegex(startTrace), list);
453 ASSERT_TRUE(finishTrace.IsLoaded()) << "Can't find \"E|\" from trace.";
454 }
455
456 /**
457 * @tc.name: Hitrace
458 * @tc.desc: tracing_mark_write file node normal output start trace and end trace.
459 * @tc.type: FUNC
460 */
461 HWTEST_F(HitraceNDKTest, StartTrace_003, TestSize.Level0)
462 {
463 ASSERT_TRUE(CleanTrace());
464 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
465 StartTrace(TAG, "StartTraceTest003 %s");
466 FinishTrace(TAG);
467 vector<string> list = ReadTrace();
468 MyTrace startTrace = GetTraceResult(TRACE_START + "(StartTraceTest003 %s) ", list);
469 ASSERT_TRUE(startTrace.IsLoaded()) << "Can't find \"B|pid|StartTraceTest003 %s\" from trace.";
470 MyTrace finishTrace = GetTraceResult(GetFinishTraceRegex(startTrace), list);
471 ASSERT_TRUE(finishTrace.IsLoaded()) << "Can't find \"E|\" from trace.";
472 ASSERT_TRUE(CleanTrace());
473 list.clear();
474 StartTrace(TAG, "StartTraceTest003 %p");
475 FinishTrace(TAG);
476 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
477 list = ReadTrace();
478 MyTrace startTrace2 = GetTraceResult(TRACE_START + "(StartTraceTest003 %p) ", list);
479 MyTrace finishTrace2 = GetTraceResult(GetFinishTraceRegex(startTrace), list);
480 ASSERT_TRUE(finishTrace2.IsLoaded()) << "Can't find \"E|\" from trace.";
481 }
482
483 /**
484 * @tc.name: Hitrace
485 * @tc.desc: test Input and output interval 1ms execution, time fluctuation 1ms
486 * @tc.type: FUNC
487 */
488 HWTEST_F(HitraceNDKTest, StartTrace_004, TestSize.Level0)
489 {
490 ASSERT_TRUE(CleanTrace());
491 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
492 StartTrace(TAG, "StartTraceTest004");
493 usleep(1000);
494 FinishTrace(TAG);
495 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
496 vector<string> list = ReadTrace();
497 MyTrace startTrace = GetTraceResult(TRACE_START + "(StartTraceTest004) ", list);
498 ASSERT_TRUE(startTrace.IsLoaded()) << "Can't find \"B|pid|StartTraceTest004\" from trace.";
499 MyTrace finishTrace = GetTraceResult(GetFinishTraceRegex(startTrace), list);
500 ASSERT_TRUE(finishTrace.IsLoaded()) << "Can't find \"E|\" from trace.";
501 }
502
503 /**
504 * @tc.name: Hitrace
505 * @tc.desc: tracing_mark_write file node normal output start trace and end trace
506 * @tc.type: FUNC
507 */
508 HWTEST_F(HitraceNDKTest, StartTrace_005, TestSize.Level0)
509 {
510 ASSERT_TRUE(CleanTrace());
511 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
512 StartAsyncTrace(TAG, "asyncTraceTest005", 123);
513 FinishAsyncTrace(TAG, "asyncTraceTest005", 123);
514 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
515 vector<string> list = ReadTrace();
516 MyTrace startTrace = GetTraceResult(TRACE_ASYNC_START + "(asyncTraceTest005) (.*)", list);
517 ASSERT_TRUE(startTrace.IsLoaded()) << "Can't find \"S|pid|asyncTraceTest005\" from trace.";
518 MyTrace finishTrace =
519 GetTraceResult(TRACE_ASYNC_FINISH + startTrace.GetTraceName() + " " + startTrace.GetNum(), list);
520 ASSERT_TRUE(finishTrace.IsLoaded()) << "Can't find \"F|\" from trace.";
521 }
522
523 /**
524 * @tc.name: Hitrace
525 * @tc.desc: tracing_mark_write file node normal output start trace and end trace
526 * @tc.type: FUNC
527 */
528 HWTEST_F(HitraceNDKTest, StartTrace_006, TestSize.Level0)
529 {
530 ASSERT_TRUE(CleanTrace());
531 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
532 CountTrace(TAG, "countTraceTest006", 1);
533 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
534 vector<string> list = ReadTrace();
535 MyTrace countTrace = GetTraceResult(TRACE_COUNT + "(countTraceTest006) (.*)", list);
536 ASSERT_TRUE(countTrace.IsLoaded()) << "Can't find \"C|\" from trace.";
537 }
538
539 /**
540 * @tc.name: Hitrace
541 * @tc.desc: tracing_mark_write file node normal output start trace and end trace.
542 * @tc.type: FUNC
543 */
544 HWTEST_F(HitraceNDKTest, StartTrace_007, TestSize.Level1)
545 {
546 ASSERT_TRUE(CleanTrace());
547 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
548 StartTrace(TRACE_INVALIDATE_TAG, "StartTraceTest007");
549 FinishTrace(TRACE_INVALIDATE_TAG);
550 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
551 vector<string> list = ReadTrace();
552 MyTrace startTrace = GetTraceResult(TRACE_START + "(StartTraceTest007)", list);
553 EXPECT_FALSE(startTrace.IsLoaded()) << "Can't find \"B|pid|StartTraceTest007\" from trace.";
554 MyTrace finishTrace = GetTraceResult(GetFinishTraceRegex(startTrace), list);
555 EXPECT_FALSE(finishTrace.IsLoaded()) << "Can't find \"E|\" from trace.";
556 }
557
558 /**
559 * @tc.name: Hitrace
560 * @tc.desc: tracing_mark_write file node normal output start trace and end trace.
561 * @tc.type: FUNC
562 */
563 HWTEST_F(HitraceNDKTest, StartTrace_008, TestSize.Level1)
564 {
565 ASSERT_TRUE(CleanTrace());
566 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
567 StartTrace(TRACE_INVALIDATE_TAG, "StartTraceTest008 %s");
568 FinishTrace(TRACE_INVALIDATE_TAG);
569 vector<string> list = ReadTrace();
570 MyTrace startTrace = GetTraceResult(TRACE_START + "(StartTraceTest008 %s)", list);
571 EXPECT_FALSE(startTrace.IsLoaded()) << "Can't find \"B|pid|StartTraceTest008 %s\" from trace.";
572 MyTrace finishTrace = GetTraceResult(GetFinishTraceRegex(startTrace), list);
573 EXPECT_FALSE(finishTrace.IsLoaded()) << "Can't find \"E|\" from trace.";
574 ASSERT_TRUE(CleanTrace());
575 list.clear();
576 StartTrace(TRACE_INVALIDATE_TAG, "StartTraceTest008 %p");
577 FinishTrace(TRACE_INVALIDATE_TAG);
578 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
579 list = ReadTrace();
580 MyTrace startTrace2 = GetTraceResult(TRACE_START + "(StartTraceTest008 %p)", list);
581 MyTrace finishTrace2 = GetTraceResult(GetFinishTraceRegex(startTrace), list);
582 EXPECT_FALSE(finishTrace2.IsLoaded()) << "Can't find \"E|\" from trace.";
583 }
584
585 /**
586 * @tc.name: Hitrace
587 * @tc.desc: tracing_mark_write file node normal output start trace and end trace
588 * @tc.type: FUNC
589 */
590 HWTEST_F(HitraceNDKTest, StartTrace_009, TestSize.Level1)
591 {
592 ASSERT_TRUE(CleanTrace());
593 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
594 StartAsyncTrace(TRACE_INVALIDATE_TAG, "asyncTraceTest009", 123);
595 FinishAsyncTrace(TRACE_INVALIDATE_TAG, "asyncTraceTest009", 123);
596 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
597 vector<string> list = ReadTrace();
598 MyTrace startTrace = GetTraceResult(TRACE_ASYNC_START + "(asyncTraceTest009)\\|(.*)", list);
599 EXPECT_FALSE(startTrace.IsLoaded()) << "Can't find \"S|pid|asyncTraceTest009\" from trace.";
600 MyTrace finishTrace = GetTraceResult(TRACE_ASYNC_FINISH + startTrace.GetTraceName() + "\\|"
601 + startTrace.GetNum(), list);
602 EXPECT_FALSE(finishTrace.IsLoaded()) << "Can't find \"F|\" from trace.";
603 }
604
605 /**
606 * @tc.name: Hitrace
607 * @tc.desc: tracing_mark_write file node normal output start trace and end trace
608 * @tc.type: FUNC
609 */
610 HWTEST_F(HitraceNDKTest, StartTrace_010, TestSize.Level1)
611 {
612 ASSERT_TRUE(CleanTrace());
613 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
614 CountTrace(TRACE_INVALIDATE_TAG, "countTraceTest010", 1);
615 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
616 vector<string> list = ReadTrace();
617 MyTrace countTrace = GetTraceResult(TRACE_COUNT + "(countTraceTest010)\\|(.*)", list);
618 EXPECT_FALSE(countTrace.IsLoaded()) << "Can't find \"C|\" from trace.";
619 }
620
621 /**
622 * @tc.name: Hitrace
623 * @tc.desc: tracing_mark_write file node general output start and end tracing for debugging.
624 * @tc.type: FUNC
625 */
626 HWTEST_F(HitraceNDKTest, StartTrace_011, TestSize.Level1)
627 {
628 ASSERT_TRUE(CleanTrace());
629 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
630 StartTraceDebug(true, TAG, "StartTraceTest011");
631 FinishTraceDebug(true, TAG);
632 }
633
634 /**
635 * @tc.name: Hitrace
636 * @tc.desc: tracing_mark_write file node general output start and end tracing for debugging.
637 * @tc.type: FUNC
638 */
639 HWTEST_F(HitraceNDKTest, StartTrace_012, TestSize.Level1)
640 {
641 ASSERT_TRUE(CleanTrace());
642 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
643 StartTraceDebug(true, TAG, "StartTraceTest012 %s");
644 FinishTraceDebug(true, TAG);
645 }
646
647 /**
648 * @tc.name: Hitrace
649 * @tc.desc: Testing StartAsyncTraceDebug and FinishAsyncTraceDebug functions
650 * @tc.type: FUNC
651 */
652 HWTEST_F(HitraceNDKTest, StartTrace_013, TestSize.Level1)
653 {
654 ASSERT_TRUE(CleanTrace());
655 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
656 StartAsyncTraceDebug(true, TAG, "asyncTraceTest013", 123);
657 FinishAsyncTraceDebug(true, TAG, "asyncTraceTest013", 123);
658 }
659
660 /**
661 * @tc.name: Hitrace
662 * @tc.desc: Testing CountTraceDebug function
663 * @tc.type: FUNC
664 */
665 HWTEST_F(HitraceNDKTest, StartTrace_014, TestSize.Level1)
666 {
667 ASSERT_TRUE(CleanTrace());
668 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
669 CountTraceDebug(true, TAG, "countTraceTest014", 1);
670 }
671
672 /**
673 * @tc.name: Hitrace
674 * @tc.desc: Testing MiddleTrace function
675 * @tc.type: FUNC
676 */
677 HWTEST_F(HitraceNDKTest, StartTrace_015, TestSize.Level1)
678 {
679 ASSERT_TRUE(CleanTrace());
680 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
681 MiddleTrace(TAG, "MiddleTraceTest015", "050tseTecarTelddiM");
682 }
683
684 /**
685 * @tc.name: Hitrace
686 * @tc.desc: Testing MiddleTraceDebug function
687 * @tc.type: FUNC
688 */
689 HWTEST_F(HitraceNDKTest, StartTrace_016, TestSize.Level1)
690 {
691 ASSERT_TRUE(CleanTrace());
692 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
693 MiddleTraceDebug(true, TAG, "MiddleTraceTest016", "061tseTecarTelddiM");
694 }
695
696 /**
697 * @tc.name: Hitrace
698 * @tc.desc: tracing_mark_write file node normal output start tracing and end tracing with args
699 * @tc.type: FUNC
700 */
701 HWTEST_F(HitraceNDKTest, StartTrace_017, TestSize.Level1)
702 {
703 ASSERT_TRUE(CleanTrace());
704 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
705 int var = 1;
706 StartTraceArgs(TAG, "StartTraceTest017-%d", var);
707 FinishTrace(TAG);
708 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
709 vector<string> list = ReadTrace();
710 MyTrace startTrace = GetTraceResult(TRACE_START + "(StartTraceTest017-1) ", list);
711 ASSERT_TRUE(startTrace.IsLoaded()) << "Can't find \"B|pid|StartTraceTest017-1\" from trace.";
712 MyTrace finishTrace = GetTraceResult(GetFinishTraceRegex(startTrace), list);
713 ASSERT_TRUE(finishTrace.IsLoaded()) << "Can't find \"E|\" from trace.";
714 }
715
716 /**
717 * @tc.name: Hitrace
718 * @tc.desc: tracing_mark_write file node normal output start trace and end trace async with args
719 * @tc.type: FUNC
720 */
721 HWTEST_F(HitraceNDKTest, StartTrace_018, TestSize.Level1)
722 {
723 ASSERT_TRUE(CleanTrace());
724 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
725 int var = 1;
726 StartAsyncTraceArgs(TAG, 123, "asyncTraceTest018-%d", var);
727 FinishAsyncTraceArgs(TAG, 123, "asyncTraceTest018-%d", var);
728 ASSERT_TRUE(SetFtrace(TRACING_ON, false)) << "Setting tracing_on failed.";
729 vector<string> list = ReadTrace();
730 MyTrace startTrace = GetTraceResult(TRACE_ASYNC_START + "(asyncTraceTest018-1) (.*)", list);
731 ASSERT_TRUE(startTrace.IsLoaded()) << "Can't find \"S|pid|asyncTraceTest018-1\" from trace.";
732 MyTrace finishTrace =
733 GetTraceResult(TRACE_ASYNC_FINISH + startTrace.GetTraceName() + " " + startTrace.GetNum(), list);
734 ASSERT_TRUE(finishTrace.IsLoaded()) << "Can't find \"F|\" from trace.";
735 }
736
737 /**
738 * @tc.name: Hitrace
739 * @tc.desc: Testing StartTraceArgsDebug function
740 * @tc.type: FUNC
741 */
742 HWTEST_F(HitraceNDKTest, StartTrace_019, TestSize.Level1)
743 {
744 ASSERT_TRUE(CleanTrace());
745 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
746 int var = 1;
747 StartTraceArgsDebug(true, TAG, "StartTraceTest019-%d", var);
748 FinishTrace(TAG);
749 }
750
751 /**
752 * @tc.name: Hitrace
753 * @tc.desc: Testing StartAsyncTraceArgsDebug and FinishAsyncTraceArgsDebug function
754 * @tc.type: FUNC
755 */
756 HWTEST_F(HitraceNDKTest, StartTrace_020, TestSize.Level1)
757 {
758 ASSERT_TRUE(CleanTrace());
759 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
760 int var = 1;
761 StartAsyncTraceArgsDebug(true, TAG, 123, "asyncTraceTest020-%d", var);
762 FinishAsyncTraceArgsDebug(true, TAG, 123, "asyncTraceTest020-%d", var);
763 }
764
765 /**
766 * @tc.name: Hitrace
767 * @tc.desc: Testing SetTraceDisabled function
768 * @tc.type: FUNC
769 */
770 HWTEST_F(HitraceNDKTest, StartTrace_021, TestSize.Level1)
771 {
772 ASSERT_TRUE(CleanTrace());
773 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
774 SetTraceDisabled(true);
775 }
776
777 /**
778 * @tc.name: Hitrace
779 * @tc.desc: Testing GetPropertyInner function
780 * @tc.type: FUNC
781 */
782 HWTEST_F(HitraceNDKTest, StartTrace_022, TestSize.Level1)
783 {
784 ASSERT_TRUE(SetProperty(TRACE_PROPERTY, "0"));
785 string tmp;
786 ASSERT_TRUE(GetPropertyInner(TRACE_PROPERTY, tmp) == "0") << "GetPropertyInner failed.";
787 }
788
789 /**
790 * @tc.name: Hitrace
791 * @tc.desc: Testing IsAppValid function
792 * @tc.type: FUNC
793 */
794 HWTEST_F(HitraceNDKTest, StartTrace_023, TestSize.Level1)
795 {
796 ASSERT_TRUE(CleanTrace());
797 ASSERT_TRUE(SetFtrace(TRACING_ON, true)) << "Setting tracing_on failed.";
798 SetProperty(KEY_RO_DEBUGGABLE, "true");
799 SetProperty(KEY_APP_NUMBER, "1");
800 StartTrace(TRACE_INVALIDATE_TAG, "StartTraceTest023");
801 FinishTrace(TRACE_INVALIDATE_TAG);
802 }
803
804 /**
805 * @tc.name: Hitrace
806 * @tc.desc: Testing trace cmd function
807 * @tc.type: FUNC
808 */
809 HWTEST_F(HitraceNDKTest, StartTrace_024, TestSize.Level1)
810 {
811 ASSERT_TRUE(CleanTrace());
812 ASSERT_TRUE(RunCmd("hitrace -h > /data/log/test1.txt"));
813 ASSERT_TRUE(RunCmd("hitrace -l > /data/log/test2.txt"));
814 ASSERT_TRUE(RunCmd("hitrace --list_categories > /data/log/test3.txt"));
815 ASSERT_TRUE(RunCmd("hitrace --trace_begin > /data/log/test4.txt"));
816 ASSERT_TRUE(RunCmd("hitrace --trace_dump > /data/log/test5.txt"));
817 ASSERT_TRUE(RunCmd("hitrace --trace_finish > /data/log/test6.txt"));
818 ASSERT_TRUE(RunCmd("hitrace --hlep > /data/log/test7.txt"));
819 ASSERT_TRUE(RunCmd("hitrace -a > /data/log/test8.txt"));
820 ASSERT_TRUE(RunCmd("hitrace --trace_clock > /data/log/test9.txt"));
821 ASSERT_TRUE(RunCmd("hitrace -t a > /data/log/test10.txt"));
822 ASSERT_TRUE(RunCmd("hitrace -t -1 > /data/log/test11.txt"));
823 ASSERT_TRUE(RunCmd("hitrace --time a > /data/log/test12.txt"));
824 ASSERT_TRUE(RunCmd("hitrace --time -1 > /data/log/test13.txt"));
825 ASSERT_TRUE(RunCmd("hitrace -b a > /data/log/test14.txt"));
826 ASSERT_TRUE(RunCmd("hitrace -b -1 > /data/log/test15.txt"));
827 ASSERT_TRUE(RunCmd("hitrace --buffer_size a > /data/log/test16.txt"));
828 ASSERT_TRUE(RunCmd("hitrace --buffer_size -1 > /data/log/test17.txt"));
829 ASSERT_TRUE(RunCmd("hitrace -z --time 1 --buffer_size 10240 --trace_clock clock ohos > /data/log/trace01"));
830 ASSERT_TRUE(RunCmd("hitrace -z -t 1 -b 10240 --trace_clock clock --overwrite ohos > /data/log/trace02"));
831 ASSERT_TRUE(RunCmd("hitrace -t 1 --trace_clock boot ohos > /data/log/trace03"));
832 ASSERT_TRUE(RunCmd("hitrace -t 1 --trace_clock global ohos > /data/log/trace04"));
833 ASSERT_TRUE(RunCmd("hitrace -t 1 --trace_clock mono ohos > /data/log/trace05"));
834 ASSERT_TRUE(RunCmd("hitrace -t 1 --trace_clock uptime ohos > /data/log/trace06"));
835 ASSERT_TRUE(RunCmd("hitrace -t 1 --trace_clock perf ohos > /data/log/trace07"));
836 }
837
838 /**
839 * @tc.name: Hitrace
840 * @tc.desc: Testing bytrace cmd function
841 * @tc.type: FUNC
842 */
843 HWTEST_F(HitraceNDKTest, StartTrace_025, TestSize.Level1)
844 {
845 ASSERT_TRUE(CleanTrace());
846 ASSERT_TRUE(RunCmd("bytrace -h > /data/log/test1.txt"));
847 ASSERT_TRUE(RunCmd("bytrace -l > /data/log/test2.txt"));
848 ASSERT_TRUE(RunCmd("bytrace --list_categories > /data/log/test3.txt"));
849 ASSERT_TRUE(RunCmd("bytrace --trace_begin > /data/log/test4.txt"));
850 ASSERT_TRUE(RunCmd("bytrace --trace_dump > /data/log/test5.txt"));
851 ASSERT_TRUE(RunCmd("bytrace --trace_finish > /data/log/test6.txt"));
852 ASSERT_TRUE(RunCmd("bytrace --hlep > /data/log/test7.txt"));
853 ASSERT_TRUE(RunCmd("bytrace -a > /data/log/test8.txt"));
854 ASSERT_TRUE(RunCmd("bytrace --trace_clock > /data/log/test9.txt"));
855 ASSERT_TRUE(RunCmd("bytrace -t a > /data/log/test10.txt"));
856 ASSERT_TRUE(RunCmd("bytrace -t -1 > /data/log/test11.txt"));
857 ASSERT_TRUE(RunCmd("bytrace --time a > /data/log/test12.txt"));
858 ASSERT_TRUE(RunCmd("bytrace --time -1 > /data/log/test13.txt"));
859 ASSERT_TRUE(RunCmd("bytrace -b a > /data/log/test14.txt"));
860 ASSERT_TRUE(RunCmd("bytrace -b -1 > /data/log/test15.txt"));
861 ASSERT_TRUE(RunCmd("bytrace --buffer_size a > /data/log/test16.txt"));
862 ASSERT_TRUE(RunCmd("bytrace --buffer_size -1 > /data/log/test17.txt"));
863 ASSERT_TRUE(RunCmd("bytrace -z --time 1 --buffer_size 10240 --trace_clock clock ohos > /data/log/trace01"));
864 ASSERT_TRUE(RunCmd("bytrace -z -t 1 -b 10240 --trace_clock clock --overwrite ohos > /data/log/trace02"));
865 ASSERT_TRUE(RunCmd("bytrace -t 1 --trace_clock boot ohos > /data/log/trace03"));
866 ASSERT_TRUE(RunCmd("bytrace -t 1 --trace_clock global ohos > /data/log/trace04"));
867 ASSERT_TRUE(RunCmd("bytrace -t 1 --trace_clock mono ohos > /data/log/trace05"));
868 ASSERT_TRUE(RunCmd("bytrace -t 1 --trace_clock uptime ohos > /data/log/trace06"));
869 ASSERT_TRUE(RunCmd("bytrace -t 1 --trace_clock perf ohos > /data/log/trace07"));
870 }
871
872 } // namespace HitraceTest
873 } // namespace HiviewDFX
874 } // namespace OHOS
875