1 /*
2 * Copyright (c) 2021 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 "subcommand_record_test.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cinttypes>
21 #include <sched.h>
22 #include <sstream>
23 #include <sys/stat.h>
24 #include <sys/utsname.h>
25 #include <thread>
26 #include <poll.h>
27 #include <unistd.h>
28
29 #include "command.h"
30 #include "debug_logger.h"
31 #include "hisysevent_manager.h"
32 #include "subcommand_dump.h"
33 #include "subcommand_report.h"
34 #include "subcommand_test.h"
35 #include "test_hiperf_event_listener.h"
36 #include "test_utilities.h"
37 #include "utilities.h"
38
39 using namespace std::literals::chrono_literals;
40 using namespace testing::ext;
41 namespace OHOS {
42 namespace Developtools {
43 namespace HiPerf {
44 static const std::string TEST_FILE = "/data/local/tmp/perf.data";
45 const std::string PERF_CPU_TIME_MAX_PERCENT = "/proc/sys/kernel/perf_cpu_time_max_percent";
46 static const std::chrono::milliseconds CONTROL_WAITREPY_TOMEOUT = 2ms;
47
48 static constexpr size_t TEST_SIZE_F100_DWARF_SYSTEM = 1.4E4 * 1024;
49 static constexpr size_t TEST_SIZE_F500_DWARF_SYSTEM = 3.6E4 * 1024;
50 static constexpr size_t TEST_SIZE_F1000_DWARF_SYSTEM = 5.9E4 * 1024;
51 static constexpr size_t TEST_SIZE_F2000_DWARF_SYSTEM = 8.3E4 * 1024;
52 static constexpr size_t TEST_SIZE_F4000_DWARF_SYSTEM = 1.7E5 * 1024;
53 static constexpr size_t TEST_SIZE_F8000_DWARF_SYSTEM = 3.5E5 * 1024;
54 static constexpr size_t TEST_SIZE_F100_FP_SYSTEM = 10E3 * 1024;
55 static constexpr size_t TEST_SIZE_F500_FP_SYSTEM = 2E4 * 1024;
56 static constexpr size_t TEST_SIZE_F1000_FP_SYSTEM = 3E4 * 1024;
57 static constexpr size_t TEST_SIZE_F2000_FP_SYSTEM = 5E4 * 1024;
58 static constexpr size_t TEST_SIZE_F4000_FP_SYSTEM = 1E5 * 1024;
59 static constexpr size_t TEST_SIZE_F8000_FP_SYSTEM = 2E5 * 1024;
60
61 static constexpr size_t TEST_SIZE_F100_DWARF_PROCESS = 5.6E3 * 1024;
62 static constexpr size_t TEST_SIZE_F500_DWARF_PROCESS = 1.6E4 * 1024;
63 static constexpr size_t TEST_SIZE_F1000_DWARF_PROCESS = 2.9E4 * 1024;
64 static constexpr size_t TEST_SIZE_F2000_DWARF_PROCESS = 6.1E4 * 1024;
65 static constexpr size_t TEST_SIZE_F4000_DWARF_PROCESS = 5.8E4 * 1024;
66 static constexpr size_t TEST_SIZE_F8000_DWARF_PROCESS = 1.2E5 * 1024;
67 static constexpr size_t TEST_SIZE_F100_FP_PROCESS = 3.6E3 * 1024;
68 static constexpr size_t TEST_SIZE_F500_FP_PROCESS = 8.8E3 * 1024;
69 static constexpr size_t TEST_SIZE_F1000_FP_PROCESS = 1.5E4 * 1024;
70 static constexpr size_t TEST_SIZE_F2000_FP_PROCESS = 3.1E4 * 1024;
71 static constexpr size_t TEST_SIZE_F4000_FP_PROCESS = 6.2E4 * 1024;
72 static constexpr size_t TEST_SIZE_F8000_FP_PROCESS = 1.3E5 * 1024;
73
74 class SubCommandRecordTest : public testing::Test {
75 public:
76 static void SetUpTestCase(void);
77 static void TearDownTestCase(void);
78 void SetUp();
79 void TearDown();
80
81 void TestEvents(std::string &opt, std::string &uk, bool isFork = true);
82
83 static void ForkAndRunTest(const std::string& cmd, bool expect = true, bool fixPid = true,
84 SubCommandRecord::CheckRecordCallBack callback = nullptr);
85
86 static void TestRecordCommand(const std::string &option, bool expect = true, bool fixPid = true,
87 SubCommandRecord::CheckRecordCallBack callback = nullptr);
88
89 size_t GetFileSize(const char* fileName);
90
91 static std::string testProcesses;
92 };
93
94 std::string SubCommandRecordTest::testProcesses = "com.ohos.sceneboard";
95
SetUpTestCase()96 void SubCommandRecordTest::SetUpTestCase() {}
97
TearDownTestCase()98 void SubCommandRecordTest::TearDownTestCase() {}
99
SetUp()100 void SubCommandRecordTest::SetUp()
101 {
102 if (!CheckTestApp(SubCommandRecordTest::testProcesses)) {
103 SubCommandRecordTest::testProcesses = "com.ohos.launcher";
104 }
105 SubCommand::ClearSubCommands(); // clear the subCommands left from other UT
106 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
107 SubCommand::RegisterSubCommand("record", std::make_unique<SubCommandRecord>());
108 ASSERT_EQ(SubCommand::GetSubCommands().size(), 1u);
109 SubCommand::RegisterSubCommand("dump", std::make_unique<SubCommandDump>());
110 SubCommand::RegisterSubCommand("report", std::make_unique<SubCommandReport>());
111 SubCommand::RegisterSubCommand("TEST_CMD_1", std::make_unique<SubCommandTest>("TEST_CMD_1"));
112 }
113
TearDown()114 void SubCommandRecordTest::TearDown()
115 {
116 ASSERT_EQ(SubCommand::GetSubCommands().size(), 4u);
117 SubCommand::ClearSubCommands();
118 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
119 MemoryHold::Get().Clean();
120 }
121
ForkAndRunTest(const std::string & cmd,bool expect,bool fixPid,SubCommandRecord::CheckRecordCallBack callback)122 void SubCommandRecordTest::ForkAndRunTest(const std::string& cmd, bool expect, bool fixPid,
123 SubCommandRecord::CheckRecordCallBack callback)
124 {
125 pid_t pid = fork();
126 if (pid < 0) {
127 FAIL() << "Fork test process failed";
128 return;
129 }
130 if (pid == 0) {
131 TestRecordCommand(cmd, expect, fixPid, callback);
132 _exit(0);
133 }
134 int status;
135 int ret = wait(&status);
136 GTEST_LOG_(INFO) << "Status:" << status << " Result:" << ret;
137 ASSERT_EQ(status, 0);
138 }
139
TestRecordCommand(const std::string & option,bool expect,bool fixPid,SubCommandRecord::CheckRecordCallBack callback)140 void SubCommandRecordTest::TestRecordCommand(const std::string &option, bool expect, bool fixPid,
141 SubCommandRecord::CheckRecordCallBack callback)
142 {
143 StdoutRecord stdoutRecord;
144
145 std::string cmdString = "record ";
146 if (fixPid) {
147 cmdString += "--app ";
148 cmdString += " " + testProcesses;
149 }
150 cmdString += " " + option;
151 printf("command : %s\n", cmdString.c_str());
152
153 if (callback != nullptr) {
154 std::string subcommandName = "record";
155 SubCommand* subcommand = SubCommand::FindSubCommand(subcommandName);
156 ASSERT_NE(subcommand, nullptr);
157 SubCommandRecord* subcommandRecord = static_cast<SubCommandRecord*>(subcommand);
158 subcommandRecord->SetCheckRecordCallback(callback);
159 }
160
161 // it need load some symbols and much more log
162 stdoutRecord.Start();
163 const auto startTime = std::chrono::steady_clock::now();
164 bool ret = Command::DispatchCommand(cmdString);
165 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
166 std::chrono::steady_clock::now() - startTime);
167 std::string stringOut = stdoutRecord.Stop();
168 if (expect) {
169 EXPECT_EQ(stringOut.find("Sample records:") != std::string::npos, true);
170 }
171 printf("run %" PRId64 " ms return %s(expect %s)\n", (uint64_t)costMs.count(), ret ? "true" : "false",
172 expect ? "true" : "false");
173 EXPECT_EQ(expect, ret);
174 }
175
GetFileSize(const char * fileName)176 size_t SubCommandRecordTest::GetFileSize(const char* fileName)
177 {
178 if (fileName == nullptr) {
179 return 0;
180 }
181 struct stat statbuf;
182 if (stat(fileName, &statbuf) == -1) {
183 return 0;
184 }
185 size_t fileSize = statbuf.st_size;
186 return fileSize;
187 }
188
CheckIntFromProcFile(const std::string & proc,int expect)189 static bool CheckIntFromProcFile(const std::string& proc, int expect)
190 {
191 int value = -1;
192 if (!ReadIntFromProcFile(proc, value)) {
193 return false;
194 }
195
196 return value == expect;
197 }
198
199 // app package name
200 HWTEST_F(SubCommandRecordTest, PackageName, TestSize.Level1)
201 {
202 ForkAndRunTest("-d 2 ", true, true);
203 }
204
205 HWTEST_F(SubCommandRecordTest, PackageNameErr, TestSize.Level1)
206 {
207 TestRecordCommand("-d 2 --app package_name ", false, false);
208 }
209
210 // check app milliseconds
211 /**
212 * @tc.name: CheckAppMsMin
213 * @tc.desc: Test chkms minimum value
214 * @tc.type: FUNC
215 * @tc.require: issueI5R305
216 */
217 HWTEST_F(SubCommandRecordTest, CheckAppMsMin, TestSize.Level1)
218 {
219 ForkAndRunTest("-d 0.5 --chkms 1 ");
220 }
221
222 /**
223 * @tc.name: CheckAppMsMinErr
224 * @tc.desc: Test chkms less than minimum value
225 * @tc.type: FUNC
226 * @tc.require: issueI5R305
227 */
228 HWTEST_F(SubCommandRecordTest, CheckAppMsMinErr, TestSize.Level1)
229 {
230 TestRecordCommand("-d 0.5 --chkms 0 ", false);
231 }
232
233 /**
234 * @tc.name: CheckAppMsMax
235 * @tc.desc: Test chkms maximum value
236 * @tc.type: FUNC
237 * @tc.require: issueI5R305
238 */
239 HWTEST_F(SubCommandRecordTest, CheckAppMsMax, TestSize.Level1)
240 {
241 ForkAndRunTest("-d 0.5 --chkms 200 ");
242 }
243
244 /**
245 * @tc.name: CheckAppMsMaxErr
246 * @tc.desc: Test chkms more than maximum value
247 * @tc.type: FUNC
248 * @tc.require: issueI5R305
249 */
250 HWTEST_F(SubCommandRecordTest, CheckAppMsMaxErr, TestSize.Level1)
251 {
252 TestRecordCommand("-d 0.5 --chkms 201 ", false);
253 }
254
255 /**
256 * @tc.name: CheckAppMsInputErr
257 * @tc.desc: Test erro type of chkms
258 * @tc.type: FUNC
259 * @tc.require: issueI5R305
260 */
261 HWTEST_F(SubCommandRecordTest, CheckAppMsInputErr, TestSize.Level1)
262 {
263 TestRecordCommand("-d 0.5 --chkms abc ", false);
264 }
265 // stop seconds
266 HWTEST_F(SubCommandRecordTest, StopSecondsMin, TestSize.Level1)
267 {
268 ForkAndRunTest("-d 0.1 ");
269 }
270
271 HWTEST_F(SubCommandRecordTest, StopSecondsMinErr, TestSize.Level1)
272 {
273 TestRecordCommand("-d 0.099 ", false);
274 }
275
276 HWTEST_F(SubCommandRecordTest, StopSecondsMax, TestSize.Level1)
277 {
278 std::string opt = "-d 10000.0 ";
279 opt += " ls "; // because UT don't need wait so long
280 ForkAndRunTest(opt, true, false);
281 }
282
283 HWTEST_F(SubCommandRecordTest, StopSecondsMaxErr, TestSize.Level1)
284 {
285 std::string opt = "-d 10000.1 ";
286 opt += " ";
287 TestRecordCommand(opt, false);
288 }
289
290 HWTEST_F(SubCommandRecordTest, ReportCommand, TestSize.Level1)
291 {
292 std::shared_ptr<HiperfEventListener> eventListener = std::make_shared<HiperfEventListener>();
293 std::vector<ListenerRule> sysRules;
294 sysRules.emplace_back(OHOS::HiviewDFX::HiSysEvent::Domain::PROFILER, "HIPERF_USAGE", RuleType::WHOLE_WORD);
295 HiSysEventManager::AddListener(eventListener, sysRules);
296
297 ForkAndRunTest("-d 2 -a ", true, false);
298
299 std::this_thread::sleep_for(std::chrono::seconds(1));
300 HiSysEventManager::RemoveListener(eventListener);
301 std::shared_ptr<HiviewDFX::HiSysEventRecord> eventRecord = eventListener->GetLastEvent();
302 ASSERT_NE(eventRecord, nullptr);
303
304 std::string value = "";
305 EXPECT_EQ(eventRecord->GetParamValue("MAIN_CMD", value), VALUE_PARSED_SUCCEED);
306 EXPECT_EQ(value, "record");
307
308 EXPECT_EQ(eventRecord->GetParamValue("SUB_CMD", value), VALUE_PARSED_SUCCEED);
309 EXPECT_EQ(value, " record -d 2 -a");
310
311 EXPECT_EQ(eventRecord->GetParamValue("CALLER", value), VALUE_PARSED_SUCCEED);
312 EXPECT_EQ(value, "./hiperf_unittest");
313
314 EXPECT_EQ(eventRecord->GetParamValue("TARGET_PROCESS", value), VALUE_PARSED_SUCCEED);
315 EXPECT_EQ(value, "ALL");
316
317 EXPECT_EQ(eventRecord->GetParamValue("ERROR_CODE", value), VALUE_PARSED_SUCCEED);
318 EXPECT_EQ(value, "0");
319
320 EXPECT_EQ(eventRecord->GetParamValue("ERROR_MESSAGE", value), VALUE_PARSED_SUCCEED);
321 EXPECT_EQ(value, "NO_ERR");
322 }
323
324 // system wide
325 HWTEST_F(SubCommandRecordTest, SystemWide, TestSize.Level1)
326 {
327 ForkAndRunTest("-d 2 -a ", true, false);
328 }
329
330 // trackedCommand_
331 HWTEST_F(SubCommandRecordTest, TrackedCommandErr, TestSize.Level1)
332 {
333 TestRecordCommand("-d 2 -a aa ", false, false);
334 }
335
336 // --app and -p
337 HWTEST_F(SubCommandRecordTest, HasTargetErr, TestSize.Level1)
338 {
339 TestRecordCommand("--app test -p 123 -d 3 ", false, false);
340 }
341
342 HWTEST_F(SubCommandRecordTest, HasTargetErr1, TestSize.Level1)
343 {
344 TestRecordCommand("-d 3 ", false, false);
345 }
346
347 // exclude hiperf
348 HWTEST_F(SubCommandRecordTest, ExcludePerf, TestSize.Level1)
349 {
__anonf79c7ea80102(const PerfEventRecord& record) 350 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
351 if (record.GetType() == PERF_RECORD_SAMPLE) {
352 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
353 if (recordSample.data_.pid == getpid()) {
354 _exit(1);
355 }
356 }
357 };
358 ForkAndRunTest("-d 2 -a --exclude-hiperf ", true, false, callback);
359 }
360
361 HWTEST_F(SubCommandRecordTest, ExcludePerfErr, TestSize.Level1)
362 {
363 TestRecordCommand("-d 2 --exclude-hiperf ", false, true);
364 }
365
366 // select cpu
367 HWTEST_F(SubCommandRecordTest, SelectCpu, TestSize.Level1)
368 {
__anonf79c7ea80202(const PerfEventRecord& record) 369 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
370 if (record.GetType() == PERF_RECORD_SAMPLE) {
371 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
372 if (recordSample.data_.cpu != 0) {
373 _exit(1);
374 }
375 }
376 };
377 ForkAndRunTest("-d 2 -c 0 ", true, true, callback);
378 }
379
380 HWTEST_F(SubCommandRecordTest, SelectCpuMulti, TestSize.Level1)
381 {
382 int maxCpuid = sysconf(_SC_NPROCESSORS_CONF);
383 std::string opt = "-d 2 -e sw-task-clock -c ";
384 for (int i = 0; i < maxCpuid; i++) {
385 opt += std::to_string(i);
386 opt += ",";
387 }
388 opt.pop_back();
389 opt += " ";
390
__anonf79c7ea80302(const PerfEventRecord& record) 391 SubCommandRecord::CheckRecordCallBack callback = [maxCpuid](const PerfEventRecord& record) {
392 if (record.GetType() == PERF_RECORD_SAMPLE) {
393 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
394 if (recordSample.data_.cpu >= maxCpuid) {
395 _exit(1);
396 }
397 }
398 };
399 ForkAndRunTest(opt, true, true, callback);
400 }
401
402 HWTEST_F(SubCommandRecordTest, SelectCpuMinErr, TestSize.Level1)
403 {
404 TestRecordCommand("-d 2 -c -1 ", false);
405 }
406
407 HWTEST_F(SubCommandRecordTest, SelectCpuMaxErr, TestSize.Level1)
408 {
409 int maxCpuid = sysconf(_SC_NPROCESSORS_CONF);
410 std::string opt = "-d 2 -c ";
411 opt += std::to_string(maxCpuid);
412 opt += " ";
413 TestRecordCommand(opt, false);
414 }
415
416 HWTEST_F(SubCommandRecordTest, SelectCpuInputErr, TestSize.Level1)
417 {
418 TestRecordCommand("-d 2 -c abc ", false);
419 }
420
421 // --control
422 HWTEST_F(SubCommandRecordTest, CheckControlErr, TestSize.Level1)
423 {
424 TestRecordCommand("-a --control st", false, false);
425 }
426
427 // cpu percent
428 HWTEST_F(SubCommandRecordTest, CpuLimitMin, TestSize.Level1)
429 {
430 ForkAndRunTest("-d 2 --cpu-limit 1 ");
431 EXPECT_EQ(CheckIntFromProcFile(PERF_CPU_TIME_MAX_PERCENT, 1), true);
432 }
433
434 HWTEST_F(SubCommandRecordTest, CpuLimitErr, TestSize.Level1)
435 {
436 TestRecordCommand("-d 2 --cpu-limit 0 ", false);
437 }
438
439 HWTEST_F(SubCommandRecordTest, CpuLimitMax, TestSize.Level1)
440 {
441 ForkAndRunTest("-d 2 --cpu-limit 100 ");
442 EXPECT_EQ(CheckIntFromProcFile(PERF_CPU_TIME_MAX_PERCENT, 100), true);
443 }
444
445 HWTEST_F(SubCommandRecordTest, CpuLimitMaxErr, TestSize.Level1)
446 {
447 TestRecordCommand("-d 2 --cpu-limit 101 ", false);
448 }
449
450 HWTEST_F(SubCommandRecordTest, CpuLimitInputErr, TestSize.Level1)
451 {
452 TestRecordCommand("-d 2 --cpu-limit abc ", false);
453 }
454
455 // frequency
456 HWTEST_F(SubCommandRecordTest, FrequncyMin, TestSize.Level1)
457 {
458 ForkAndRunTest("-d 2 -f 1 ");
459 }
460
461 HWTEST_F(SubCommandRecordTest, FrequncyMinErr, TestSize.Level1)
462 {
463 TestRecordCommand("-d 2 -f 0 ", false);
464 }
465
466 HWTEST_F(SubCommandRecordTest, FrequncyMax, TestSize.Level1)
467 {
468 ForkAndRunTest("-d 2 -f 100000 ");
469 }
470
471 HWTEST_F(SubCommandRecordTest, FrequncyMaxErr, TestSize.Level1)
472 {
473 TestRecordCommand("-d 2 -f 100001 ", false);
474 }
475
476 HWTEST_F(SubCommandRecordTest, FrequncyInputErr, TestSize.Level1)
477 {
478 TestRecordCommand("-d 2 -f abc ", false);
479 }
480
481 // period
482 HWTEST_F(SubCommandRecordTest, PeriodMin, TestSize.Level1)
483 {
484 ForkAndRunTest("-d 2 --period 1 ");
485 }
486
487 HWTEST_F(SubCommandRecordTest, PeriodMinErr, TestSize.Level1)
488 {
489 TestRecordCommand("-d 2 --period 0 ", false);
490 }
491
492 HWTEST_F(SubCommandRecordTest, PeriodMax, TestSize.Level1)
493 {
494 std::string opt = "-d 2 --period ";
495 opt += std::to_string(INT_MAX);
496 opt += " ";
497 ForkAndRunTest(opt);
498 }
499
500 HWTEST_F(SubCommandRecordTest, PeriodMaxErr, TestSize.Level1)
501 {
502 std::string opt = "-d 2 --period ";
503 uint32_t value = static_cast<uint32_t>(INT_MAX) + 1;
504 opt += std::to_string(value);
505 opt += " ";
506 TestRecordCommand(opt, false);
507 }
508
509 HWTEST_F(SubCommandRecordTest, PeriodInputErr, TestSize.Level1)
510 {
511 TestRecordCommand("-d 2 --period abc ", false);
512 }
513
514 HWTEST_F(SubCommandRecordTest, PeriodAndFrequncyConflict, TestSize.Level1)
515 {
516 TestRecordCommand("-d 2 -f 2000 --period 10 ", false);
517 }
518
TestEvents(std::string & opt,std::string & uk,bool isFork)519 void SubCommandRecordTest::TestEvents(std::string &opt, std::string &uk, bool isFork)
520 {
521 PerfEvents perfEvents;
522 utsname unameBuf;
523 bool isHM = false;
524 if ((uname(&unameBuf)) == 0) {
525 std::string osrelease = unameBuf.release;
526 isHM = osrelease.find(HMKERNEL) != std::string::npos;
527 }
528 perfEvents.SetHM(isHM);
529 for (auto type : TYPE_CONFIGS) {
530 auto configs = perfEvents.GetSupportEvents(type.first);
531 if (configs.empty()) {
532 continue;
533 }
534
535 const int MAX_TESTEVENT = 5;
536 int testEventCount = MAX_TESTEVENT;
537 std::string cmdline = opt;
538 for (auto config : configs) {
539 if (testEventCount <= 0) {
540 break;
541 }
542 cmdline += config.second;
543 cmdline += uk;
544 cmdline += ",";
545 testEventCount--;
546 }
547 cmdline.pop_back(); // remove the last ','
548 if (isFork) {
549 ForkAndRunTest(cmdline);
550 } else {
551 TestRecordCommand(cmdline);
552 }
553 TearDown();
554 SetUp();
555 }
556 }
557
558 // select events
559 HWTEST_F(SubCommandRecordTest, SelectEvents, TestSize.Level1)
560 {
561 std::string opt = "-d 2 -c 0 -e ";
562 std::string uk = "";
563 TestEvents(opt, uk);
564 }
565
566 HWTEST_F(SubCommandRecordTest, SelectEventsUser, TestSize.Level1)
567 {
568 std::string opt = "-d 2 -c 0 -e ";
569 std::string uk = ":u";
570 TestEvents(opt, uk);
571 }
572
573 HWTEST_F(SubCommandRecordTest, SelectEventsKernel, TestSize.Level1)
574 {
575 std::string opt = "-d 2 -c 0 -e ";
576 std::string uk = ":k";
577 TestEvents(opt, uk);
578 }
579
580 HWTEST_F(SubCommandRecordTest, SelectEventsKernel_2, TestSize.Level1)
581 {
582 std::string opt = "-d 2 -c 0 -e ";
583 std::string uk = ":k";
584 TestEvents(opt, uk, false);
585 }
586
587 HWTEST_F(SubCommandRecordTest, SelectEventsErr, TestSize.Level1)
588 {
589 ForkAndRunTest("-d 2 -c 0 -e what ", false);
590 }
591
592 // select group events
593 HWTEST_F(SubCommandRecordTest, GroupEvents, TestSize.Level1)
594 {
595 std::string opt = "-d 2 -c 0 -g ";
596 std::string uk = "";
597 TestEvents(opt, uk);
598 }
599
600 HWTEST_F(SubCommandRecordTest, GroupEventsUser, TestSize.Level1)
601 {
602 std::string opt = "-d 2 -c 0 -g ";
603 std::string uk = ":u";
604 TestEvents(opt, uk);
605 }
606
607 HWTEST_F(SubCommandRecordTest, GroupEventsKernal, TestSize.Level1)
608 {
609 std::string opt = "-d 2 -c 0 -g ";
610 std::string uk = ":k";
611 TestEvents(opt, uk);
612 }
613
614 HWTEST_F(SubCommandRecordTest, GroupEventsErr, TestSize.Level1)
615 {
616 ForkAndRunTest("-d 2 -c 0 -g what ", false);
617 }
618
619 HWTEST_F(SubCommandRecordTest, NoInherit, TestSize.Level1)
620 {
621 ForkAndRunTest("-d 2 --no-inherit ");
622 }
623
624 // select pid
625 HWTEST_F(SubCommandRecordTest, SelectPid, TestSize.Level1)
626 {
627 ForkAndRunTest("-d 2 -p 1 ", true, false);
628 }
629
630 HWTEST_F(SubCommandRecordTest, KernelSymbols, TestSize.Level1)
631 {
632 TestRecordCommand("-d 2 -p 2 -s dwarf ", true, false);
633 }
634
635 HWTEST_F(SubCommandRecordTest, SelectPidMulti, TestSize.Level1)
636 {
637 ForkAndRunTest("-d 2 -p 1,2,3 ", true, false);
638 }
639
640 HWTEST_F(SubCommandRecordTest, SelectPidMinErr, TestSize.Level1)
641 {
642 TestRecordCommand("-d 2 -p 0 ", false, false);
643 }
644
645 HWTEST_F(SubCommandRecordTest, SelectPidMinErr1, TestSize.Level1)
646 {
647 TestRecordCommand("-d 2 -p -1 ", false, false);
648 }
649
650 HWTEST_F(SubCommandRecordTest, SelectPidErr, TestSize.Level1)
651 {
652 TestRecordCommand("-d 2 -p 99999999 ", false, false);
653 }
654
655 HWTEST_F(SubCommandRecordTest, SelectPidInputErr, TestSize.Level1)
656 {
657 TestRecordCommand("-d 2 -p abc ", false, false);
658 }
659
660 HWTEST_F(SubCommandRecordTest, SelectPidInputConflict, TestSize.Level1)
661 {
662 ForkAndRunTest("-d 2 -a -p 1 ", false, false);
663 }
664
665 // select tid
666 HWTEST_F(SubCommandRecordTest, SelectTid, TestSize.Level1)
667 {
668 ForkAndRunTest("-d 2 -t 1 ", true, false);
669 }
670
671 HWTEST_F(SubCommandRecordTest, SelectTidMulti, TestSize.Level1)
672 {
673 ForkAndRunTest("-d 2 -t 1,2,3 ", true, false);
674 }
675
676 HWTEST_F(SubCommandRecordTest, SelectTidMinErr, TestSize.Level1)
677 {
678 TestRecordCommand("-d 2 -t 0 ", false, false);
679 }
680
681 HWTEST_F(SubCommandRecordTest, SelectTidErr, TestSize.Level1)
682 {
683 TestRecordCommand("-d 2 -t 99999999 ", false, false);
684 }
685
686 HWTEST_F(SubCommandRecordTest, SelectTidInputErr, TestSize.Level1)
687 {
688 TestRecordCommand("-d 2 -t abc ", false, false);
689 }
690
691 // cpu off
692 HWTEST_F(SubCommandRecordTest, CpuOff, TestSize.Level1)
693 {
694 ForkAndRunTest("-d 2 --offcpu -o /data/local/tmp/offcpu_perf.data");
695 }
696
697 HWTEST_F(SubCommandRecordTest, BranchFilterInputErr, TestSize.Level1)
698 {
699 TestRecordCommand("-d 2 -j what ", false);
700 }
701
702 HWTEST_F(SubCommandRecordTest, BranchFilterInputMoreErr, TestSize.Level1)
703 {
704 TestRecordCommand("-d 2 -j any,n ", false);
705 }
706
707 // call stack
708 HWTEST_F(SubCommandRecordTest, CallStackFp, TestSize.Level1)
709 {
710 ForkAndRunTest("-d 2 --call-stack fp ");
711 TearDown();
712 SetUp();
713 ForkAndRunTest("-d 2 -s fp ");
714 }
715
716 HWTEST_F(SubCommandRecordTest, CallStackFpInputMoreErr, TestSize.Level1)
717 {
718 TestRecordCommand("-d 2 --call-stack fp,abc ", false);
719 TearDown();
720 SetUp();
721 TestRecordCommand("-d 2 -s fp,abc ", false);
722 }
723
724 HWTEST_F(SubCommandRecordTest, CallStackInputErr, TestSize.Level1)
725 {
726 TestRecordCommand("-d 2 --call-stack what ", false);
727 TearDown();
728 SetUp();
729 TestRecordCommand("-d 2 -s what ", false);
730 }
731
732 HWTEST_F(SubCommandRecordTest, CallStackDwarfSizeMin, TestSize.Level1)
733 {
734 // it will cause some crash in -fprofile-arcs and -ftest-coverage
735 // we will fix it latter
736 ForkAndRunTest("-d 2 --call-stack dwarf,8 ");
737 TearDown();
738 SetUp();
739 ForkAndRunTest("-d 2 -s dwarf,8 ");
740 }
741
742 HWTEST_F(SubCommandRecordTest, CallStackDwarfSizeMinErr, TestSize.Level1)
743 {
744 TestRecordCommand("-d 2 --call-stack dwarf,7 ", false);
745 TearDown();
746 SetUp();
747 TestRecordCommand("-d 2 -s dwarf,7 ", false);
748 }
749
750 HWTEST_F(SubCommandRecordTest, CallStackDwarfSizeMax, TestSize.Level1)
751 {
752 ForkAndRunTest("-d 2 --call-stack dwarf,65528 ");
753 TearDown();
754 SetUp();
755 ForkAndRunTest("-d 2 -s dwarf,65528 ");
756 }
757
758 HWTEST_F(SubCommandRecordTest, CallStackDwarfSizeMaxErr, TestSize.Level1)
759 {
760 TestRecordCommand("-d 2 --call-stack dwarf,65529 ", false);
761 TearDown();
762 SetUp();
763 TestRecordCommand("-d 2 -s dwarf,65529 ", false);
764 }
765
766 HWTEST_F(SubCommandRecordTest, CallStackDwarfSizeErr, TestSize.Level1)
767 {
768 TestRecordCommand("-d 2 --call-stack dwarf,15 ", false);
769 TearDown();
770 SetUp();
771 TestRecordCommand("-d 2 -s dwarf,15 ", false);
772 }
773
774 HWTEST_F(SubCommandRecordTest, CallStackDwarfSizeInputErr, TestSize.Level1)
775 {
776 TestRecordCommand("-d 2 --call-stack dwarf,abc ", false);
777 TearDown();
778 SetUp();
779 TestRecordCommand("-d 2 -s dwarf,abc ", false);
780 }
781
782 HWTEST_F(SubCommandRecordTest, CallStackDwarfSizeInputMoreErr, TestSize.Level1)
783 {
784 TestRecordCommand("-d 2 --call-stack dwarf,16,32 ", false);
785 TearDown();
786 SetUp();
787 TestRecordCommand("-d 2 -s dwarf,16,32 ", false);
788 }
789
790 HWTEST_F(SubCommandRecordTest, CallStackUsageErr, TestSize.Level1)
791 {
792 TestRecordCommand("-d 2 -s abc --call-stack bcd", false);
793 }
794
795 // unwind
796 HWTEST_F(SubCommandRecordTest, DlayUnwind, TestSize.Level1)
797 {
798 ForkAndRunTest("-d 2 -s dwarf,16 --delay-unwind ");
799 }
800
801 HWTEST_F(SubCommandRecordTest, DisableUnwind, TestSize.Level1)
802 {
803 ForkAndRunTest("-d 2 -s dwarf,16 --disable-unwind ");
804 }
805
806 HWTEST_F(SubCommandRecordTest, DisableCallstackMerge, TestSize.Level1)
807 {
808 ForkAndRunTest("-d 2 -s dwarf,16 --disable-callstack-expand ");
809 }
810
811 // symbol dir
812 HWTEST_F(SubCommandRecordTest, SymbolDir, TestSize.Level1)
813 {
814 ForkAndRunTest("-d 2 --symbol-dir ./ ");
815 }
816
817 HWTEST_F(SubCommandRecordTest, SymbolDirErr, TestSize.Level1)
818 {
819 TestRecordCommand("-d 2 --symbol-dir where ", false);
820 }
821
822 // clock id
823 HWTEST_F(SubCommandRecordTest, ClockIdMonotonic, TestSize.Level1)
824 {
825 ForkAndRunTest("-d 2 --clockid monotonic ");
826 }
827
828 HWTEST_F(SubCommandRecordTest, ClockIdMonotonicRaw, TestSize.Level1)
829 {
830 ForkAndRunTest("-d 2 --clockid monotonic_raw ");
831 }
832
833 HWTEST_F(SubCommandRecordTest, ClockIdBoottime, TestSize.Level1)
834 {
835 ForkAndRunTest("-c 0 -d 2 -e sw-task-clock --clockid boottime ");
836 }
837
838 HWTEST_F(SubCommandRecordTest, ClockIdRealtime, TestSize.Level1)
839 {
840 ForkAndRunTest("-c 0 -d 2 -e sw-task-clock --clockid realtime ");
841 }
842
843 HWTEST_F(SubCommandRecordTest, ClockIdClockTai, TestSize.Level1)
844 {
845 ForkAndRunTest("-c 0 -d 2 -e sw-task-clock --clockid clock_tai ");
846 }
847
848 HWTEST_F(SubCommandRecordTest, ClockIdInputErr, TestSize.Level1)
849 {
850 TestRecordCommand("-c 0 -d 2 --clockid what ", false);
851 }
852
853 // mmap pages
854 HWTEST_F(SubCommandRecordTest, MmapPagesPower2Err, TestSize.Level1)
855 {
856 TestRecordCommand("-d 2 -m 101 ", false);
857 }
858
859 HWTEST_F(SubCommandRecordTest, MmapPagesMin, TestSize.Level1)
860 {
861 ForkAndRunTest("-d 2 -m 2 ");
862 }
863
864 HWTEST_F(SubCommandRecordTest, MmapPagesMinErr, TestSize.Level1)
865 {
866 TestRecordCommand("-d 2 -m 1 ", false);
867 }
868
869 HWTEST_F(SubCommandRecordTest, MmapPagesMax, TestSize.Level1)
870 {
871 ForkAndRunTest("-d 2 -m 1024 ");
872 }
873
874 HWTEST_F(SubCommandRecordTest, MmapPagesMaxErr, TestSize.Level1)
875 {
876 TestRecordCommand("-d 2 -m 1025 ", false);
877 }
878
879 HWTEST_F(SubCommandRecordTest, MmapPagesInputErr, TestSize.Level1)
880 {
881 TestRecordCommand("-d 2 -m abc ", false);
882 }
883
884 // output file name
885 HWTEST_F(SubCommandRecordTest, OutputFileName, TestSize.Level1)
886 {
887 ForkAndRunTest("-d 2 -o /data/local/tmp/output.perf.data ");
888 }
889
890 HWTEST_F(SubCommandRecordTest, OutputFileNameErr, TestSize.Level1)
891 {
892 TestRecordCommand("-d 2 -o nopath/output.perf.data ", false);
893 }
894
895 // data size limit
896 HWTEST_F(SubCommandRecordTest, DataLimit, TestSize.Level1)
897 {
898 ForkAndRunTest("-d 2 --data-limit 1K ");
899 TearDown();
900 SetUp();
901 ForkAndRunTest("-d 2 --data-limit 1M ");
902 TearDown();
903 SetUp();
904 ForkAndRunTest("-d 2 --data-limit 1G ");
905 }
906
907 HWTEST_F(SubCommandRecordTest, DataLimit1, TestSize.Level1)
908 {
909 ForkAndRunTest("-a --data-limit 1K ", true, false);
910 }
911
912 HWTEST_F(SubCommandRecordTest, DataLimitErr, TestSize.Level1)
913 {
914 TestRecordCommand("-d 2 --data-limit 10A ", false);
915 TearDown();
916 SetUp();
917 TestRecordCommand("-d 2 --data-limit 0G ", false);
918 }
919
920 HWTEST_F(SubCommandRecordTest, RecordCompress, TestSize.Level1)
921 {
922 ForkAndRunTest("-d 2 -z -o /data/local/tmp/perf.data.tar.gz");
923 }
924
925 HWTEST_F(SubCommandRecordTest, Verbose, TestSize.Level1)
926 {
927 ForkAndRunTest("-d 2 --verbose ");
928 }
929
930 HWTEST_F(SubCommandRecordTest, DumpOptions, TestSize.Level1)
931 {
932 StdoutRecord stdoutRecord;
933 stdoutRecord.Start();
934 SubCommandRecord cmd;
935 cmd.DumpOptions();
936 std::string stringOut = stdoutRecord.Stop();
937 EXPECT_TRUE(stringOut.find("cpuPercent: 25") != std::string::npos);
938 EXPECT_TRUE(stringOut.find("mmapPages_: 1024") != std::string::npos);
939 }
940
941 /**
942 * @tc.name: FileSizeOnFrequency100_DWARF_SYSTEM
943 * @tc.desc: Test size of file generated under system wide frequency 100 and dwarf unwind
944 * @tc.type: FUNC
945 */
946 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency100_DWARF_SYSTEM, TestSize.Level1)
947 {
948 ForkAndRunTest("-d 10 -a -f 100 -s dwarf", true, false);
949 std::string fileName = TEST_FILE;
950 size_t fileSize = GetFileSize(fileName.c_str());
951 EXPECT_LE(fileSize, TEST_SIZE_F100_DWARF_SYSTEM);
952 }
953
954 /**
955 * @tc.name: FileSizeOnFrequency500_DWARF_SYSTEM
956 * @tc.desc: Test size of file generated under system wide frequency 500 and dwarf unwind
957 * @tc.type: FUNC
958 */
959 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency500_DWARF_SYSTEM, TestSize.Level1)
960 {
961 ForkAndRunTest("-d 10 -a -f 500 -s dwarf", true, false);
962 std::string fileName = TEST_FILE;
963 size_t fileSize = GetFileSize(fileName.c_str());
964 EXPECT_LE(fileSize, TEST_SIZE_F500_DWARF_SYSTEM);
965 }
966
967 /**
968 * @tc.name: FileSizeOnFrequency1000_DWARF_SYSTEM
969 * @tc.desc: Test size of file generated under system wide frequency 1000 and dwarf unwind
970 * @tc.type: FUNC
971 */
972 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency1000_DWARF_SYSTEM, TestSize.Level1)
973 {
974 ForkAndRunTest("-d 10 -a -f 1000 -s dwarf", true, false);
975 std::string fileName = TEST_FILE;
976 size_t fileSize = GetFileSize(fileName.c_str());
977 EXPECT_LE(fileSize, TEST_SIZE_F1000_DWARF_SYSTEM);
978 }
979
980 /**
981 * @tc.name: FileSizeOnFrequency2000_DWARF_SYSTEM
982 * @tc.desc: Test size of file generated under system wide frequency 2000 and dwarf unwind
983 * @tc.type: FUNC
984 */
985 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency2000_DWARF_SYSTEM, TestSize.Level1)
986 {
987 ForkAndRunTest("-d 10 -a -f 2000 -s dwarf", true, false);
988 std::string fileName = TEST_FILE;
989 size_t fileSize = GetFileSize(fileName.c_str());
990 EXPECT_LE(fileSize, TEST_SIZE_F2000_DWARF_SYSTEM);
991 }
992
993 /**
994 * @tc.name: FileSizeOnFrequency4000_DWARF_SYSTEM
995 * @tc.desc: Test size of file generated under system wide frequency 4000 and dwarf unwind
996 * @tc.type: FUNC
997 */
998 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency4000_DWARF_SYSTEM, TestSize.Level1)
999 {
1000 ForkAndRunTest("-d 10 -a -f 4000 -s dwarf", true, false);
1001 std::string fileName = TEST_FILE;
1002 size_t fileSize = GetFileSize(fileName.c_str());
1003 EXPECT_LE(fileSize, TEST_SIZE_F4000_DWARF_SYSTEM);
1004 }
1005
1006 /**
1007 * @tc.name: FileSizeOnFrequency8000_DWARF_SYSTEM
1008 * @tc.desc: Test size of file generated under system wide frequency 8000 and dwarf unwind
1009 * @tc.type: FUNC
1010 */
1011 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency8000_DWARF_SYSTEM, TestSize.Level1)
1012 {
1013 ForkAndRunTest("-d 10 -a -f 8000 -s dwarf", true, false);
1014 std::string fileName = TEST_FILE;
1015 size_t fileSize = GetFileSize(fileName.c_str());
1016 EXPECT_LE(fileSize, TEST_SIZE_F8000_DWARF_SYSTEM);
1017 }
1018
1019 /**
1020 * @tc.name: FileSizeOnFrequency100_FP_SYSTEM
1021 * @tc.desc: Test size of file generated under system wide frequency 100 and fp unwind
1022 * @tc.type: FUNC
1023 */
1024 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency100_FP_SYSTEM, TestSize.Level1)
1025 {
1026 ForkAndRunTest("-d 10 -a -f 100 -s fp", true, false);
1027 std::string fileName = TEST_FILE;
1028 size_t fileSize = GetFileSize(fileName.c_str());
1029 EXPECT_LE(fileSize, TEST_SIZE_F100_FP_SYSTEM);
1030 }
1031
1032 /**
1033 * @tc.name: FileSizeOnFrequency500_FP_SYSTEM
1034 * @tc.desc: Test size of file generated under system wide frequency 500 and fp unwind
1035 * @tc.type: FUNC
1036 */
1037 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency500_FP_SYSTEM, TestSize.Level1)
1038 {
1039 ForkAndRunTest("-d 10 -a -f 500 -s fp", true, false);
1040 std::string fileName = TEST_FILE;
1041 size_t fileSize = GetFileSize(fileName.c_str());
1042 EXPECT_LE(fileSize, TEST_SIZE_F500_FP_SYSTEM);
1043 }
1044
1045 /**
1046 * @tc.name: FileSizeOnFrequency1000_FP_SYSTEM
1047 * @tc.desc: Test size of file generated under system wide frequency 1000 and fp unwind
1048 * @tc.type: FUNC
1049 */
1050 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency1000_FP_SYSTEM, TestSize.Level1)
1051 {
1052 ForkAndRunTest("-d 10 -a -f 1000 -s fp", true, false);
1053 std::string fileName = TEST_FILE;
1054 size_t fileSize = GetFileSize(fileName.c_str());
1055 EXPECT_LE(fileSize, TEST_SIZE_F1000_FP_SYSTEM);
1056 }
1057
1058 /**
1059 * @tc.name: FileSizeOnFrequency2000_FP_SYSTEM
1060 * @tc.desc: Test size of file generated under system wide frequency 2000 and fp unwind
1061 * @tc.type: FUNC
1062 */
1063 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency2000_FP_SYSTEM, TestSize.Level1)
1064 {
1065 ForkAndRunTest("-d 10 -a -f 2000 -s fp", true, false);
1066 std::string fileName = TEST_FILE;
1067 size_t fileSize = GetFileSize(fileName.c_str());
1068 EXPECT_LE(fileSize, TEST_SIZE_F2000_FP_SYSTEM);
1069 }
1070
1071 /**
1072 * @tc.name: FileSizeOnFrequency4000_FP_SYSTEM
1073 * @tc.desc: Test size of file generated under system wide frequency 4000 and fp unwind
1074 * @tc.type: FUNC
1075 */
1076 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency4000_FP_SYSTEM, TestSize.Level1)
1077 {
1078 ForkAndRunTest("-d 10 -a -f 4000 -s fp", true, false);
1079 std::string fileName = TEST_FILE;
1080 size_t fileSize = GetFileSize(fileName.c_str());
1081 EXPECT_LE(fileSize, TEST_SIZE_F4000_FP_SYSTEM);
1082 }
1083
1084 /**
1085 * @tc.name: FileSizeOnFrequency8000_FP_SYSTEM
1086 * @tc.desc: Test size of file generated under system wide frequency 8000 and fp unwind
1087 * @tc.type: FUNC
1088 */
1089 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency8000_FP_SYSTEM, TestSize.Level1)
1090 {
1091 ForkAndRunTest("-d 10 -a -f 8000 -s fp", true, false);
1092 std::string fileName = TEST_FILE;
1093 size_t fileSize = GetFileSize(fileName.c_str());
1094 EXPECT_LE(fileSize, TEST_SIZE_F8000_FP_SYSTEM);
1095 }
1096
1097 /**
1098 * @tc.name: FileSizeOnFrequency100_DWARF_PROCESS
1099 * @tc.desc: Test size of file generated under one process frequency 100 and dwarf unwind
1100 * @tc.type: FUNC
1101 */
1102 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency100_DWARF_PROCESS, TestSize.Level1)
1103 {
1104 ForkAndRunTest("-d 10 -f 100 -s dwarf", true, true);
1105 std::string fileName = TEST_FILE;
1106 size_t fileSize = GetFileSize(fileName.c_str());
1107 EXPECT_LE(fileSize, TEST_SIZE_F100_DWARF_PROCESS);
1108 }
1109
1110 /**
1111 * @tc.name: FileSizeOnFrequency500_DWARF_PROCESS
1112 * @tc.desc: Test size of file generated under one process frequency 500 and dwarf unwind
1113 * @tc.type: FUNC
1114 */
1115 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency500_DWARF_PROCESS, TestSize.Level1)
1116 {
1117 ForkAndRunTest("-d 10 -f 500 -s dwarf", true, true);
1118 std::string fileName = TEST_FILE;
1119 size_t fileSize = GetFileSize(fileName.c_str());
1120 EXPECT_LE(fileSize, TEST_SIZE_F500_DWARF_PROCESS);
1121 }
1122
1123 /**
1124 * @tc.name: FileSizeOnFrequency1000_DWARF_PROCESS
1125 * @tc.desc: Test size of file generated under one process frequency 1000 and dwarf unwind
1126 * @tc.type: FUNC
1127 */
1128 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency1000_DWARF_PROCESS, TestSize.Level1)
1129 {
1130 ForkAndRunTest("-d 10 -f 1000 -s dwarf", true, true);
1131 std::string fileName = TEST_FILE;
1132 size_t fileSize = GetFileSize(fileName.c_str());
1133 EXPECT_LE(fileSize, TEST_SIZE_F1000_DWARF_PROCESS);
1134 }
1135
1136 /**
1137 * @tc.name: FileSizeOnFrequency2000_DWARF_PROCESS
1138 * @tc.desc: Test size of file generated under one process frequency 2000 and dwarf unwind
1139 * @tc.type: FUNC
1140 */
1141 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency2000_DWARF_PROCESS, TestSize.Level1)
1142 {
1143 ForkAndRunTest("-d 10 -f 2000 -s dwarf", true, true);
1144 std::string fileName = TEST_FILE;
1145 size_t fileSize = GetFileSize(fileName.c_str());
1146 EXPECT_LE(fileSize, TEST_SIZE_F2000_DWARF_PROCESS);
1147 }
1148
1149 /**
1150 * @tc.name: FileSizeOnFrequency4000_DWARF_PROCESS
1151 * @tc.desc: Test size of file generated under one process frequency 4000 and dwarf unwind
1152 * @tc.type: FUNC
1153 */
1154 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency4000_DWARF_PROCESS, TestSize.Level1)
1155 {
1156 ForkAndRunTest("-d 10 -f 4000 -s dwarf", true, true);
1157 std::string fileName = TEST_FILE;
1158 size_t fileSize = GetFileSize(fileName.c_str());
1159 EXPECT_LE(fileSize, TEST_SIZE_F4000_DWARF_PROCESS);
1160 }
1161
1162 /**
1163 * @tc.name: FileSizeOnFrequency8000_DWARF_PROCESS
1164 * @tc.desc: Test size of file generated under one process frequency 8000 and dwarf unwind
1165 * @tc.type: FUNC
1166 */
1167 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency8000_DWARF_PROCESS, TestSize.Level1)
1168 {
1169 ForkAndRunTest("-d 10 -f 8000 -s dwarf", true, true);
1170 std::string fileName = TEST_FILE;
1171 size_t fileSize = GetFileSize(fileName.c_str());
1172 EXPECT_LE(fileSize, TEST_SIZE_F8000_DWARF_PROCESS);
1173 }
1174
1175 /**
1176 * @tc.name: FileSizeOnFrequency100_FP_PROCESS
1177 * @tc.desc: Test size of file generated under one process frequency 100 and fp unwind
1178 * @tc.type: FUNC
1179 */
1180 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency100_FP_PROCESS, TestSize.Level1)
1181 {
1182 ForkAndRunTest("-d 10 -f 100 -s fp", true, true);
1183 std::string fileName = TEST_FILE;
1184 size_t fileSize = GetFileSize(fileName.c_str());
1185 EXPECT_LE(fileSize, TEST_SIZE_F100_FP_PROCESS);
1186 }
1187
1188 /**
1189 * @tc.name: FileSizeOnFrequency500_FP_PROCESS
1190 * @tc.desc: Test size of file generated under one process frequency 500 and fp unwind
1191 * @tc.type: FUNC
1192 */
1193 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency500_FP_PROCESS, TestSize.Level1)
1194 {
1195 ForkAndRunTest("-d 10 -f 500 -s fp", true, true);
1196 std::string fileName = TEST_FILE;
1197 size_t fileSize = GetFileSize(fileName.c_str());
1198 EXPECT_LE(fileSize, TEST_SIZE_F500_FP_PROCESS);
1199 }
1200
1201 /**
1202 * @tc.name: FileSizeOnFrequency1000_FP_PROCESS
1203 * @tc.desc: Test size of file generated under one process frequency 1000 and fp unwind
1204 * @tc.type: FUNC
1205 */
1206 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency1000_FP_PROCESS, TestSize.Level1)
1207 {
1208 ForkAndRunTest("-d 10 -f 1000 -s fp", true, true);
1209 std::string fileName = TEST_FILE;
1210 size_t fileSize = GetFileSize(fileName.c_str());
1211 EXPECT_LE(fileSize, TEST_SIZE_F1000_FP_PROCESS);
1212 }
1213
1214 /**
1215 * @tc.name: FileSizeOnFrequency2000_FP_PROCESS
1216 * @tc.desc: Test size of file generated under one process frequency 2000 and fp unwind
1217 * @tc.type: FUNC
1218 */
1219 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency2000_FP_PROCESS, TestSize.Level1)
1220 {
1221 ForkAndRunTest("-d 10 -f 2000 -s fp", true, true);
1222 std::string fileName = TEST_FILE;
1223 size_t fileSize = GetFileSize(fileName.c_str());
1224 EXPECT_LE(fileSize, TEST_SIZE_F2000_FP_PROCESS);
1225 }
1226
1227 /**
1228 * @tc.name: FileSizeOnFrequency4000_FP_PROCESS
1229 * @tc.desc: Test size of file generated under one process frequency 4000 and fp unwind
1230 * @tc.type: FUNC
1231 */
1232 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency4000_FP_PROCESS, TestSize.Level1)
1233 {
1234 ForkAndRunTest("-d 10 -f 4000 -s fp", true, true);
1235 std::string fileName = TEST_FILE;
1236 size_t fileSize = GetFileSize(fileName.c_str());
1237 EXPECT_LE(fileSize, TEST_SIZE_F4000_FP_PROCESS);
1238 }
1239
1240 /**
1241 * @tc.name: FileSizeOnFrequency8000_FP_PROCESS
1242 * @tc.desc: Test size of file generated under one process frequency 8000 and fp unwind
1243 * @tc.type: FUNC
1244 */
1245 HWTEST_F(SubCommandRecordTest, FileSizeOnFrequency8000_FP_PROCESS, TestSize.Level1)
1246 {
1247 ForkAndRunTest("-d 10 -f 8000 -s fp", true, true);
1248 std::string fileName = TEST_FILE;
1249 size_t fileSize = GetFileSize(fileName.c_str());
1250 EXPECT_LE(fileSize, TEST_SIZE_F8000_FP_PROCESS);
1251 }
1252
1253 /**
1254 * @tc.name: ExcludeThreadName
1255 * @tc.desc: Test --exclude-thread option sucess
1256 * @tc.type: FUNC
1257 */
1258 HWTEST_F(SubCommandRecordTest, ExcludeThreadName, TestSize.Level1)
1259 {
__anonf79c7ea80402(const PerfEventRecord& record) 1260 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
1261 if (record.GetType() == PERF_RECORD_SAMPLE) {
1262 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
1263 std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", recordSample.data_.tid));
1264 if (threadName == "DfxWatchdog") {
1265 _exit(1);
1266 }
1267 }
1268 };
1269 ForkAndRunTest("-d 2 --exclude-thread DfxWatchdog ", true, true, callback);
1270 }
1271
1272 /**
1273 * @tc.name: ExcludeThreadNames
1274 * @tc.desc: Test --exclude-thread option multi threads
1275 * @tc.type: FUNC
1276 */
1277 HWTEST_F(SubCommandRecordTest, ExcludeThreadNames, TestSize.Level1)
1278 {
__anonf79c7ea80502(const PerfEventRecord& record) 1279 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
1280 if (record.GetType() == PERF_RECORD_SAMPLE) {
1281 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
1282 std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", recordSample.data_.tid));
1283 if (threadName == "DfxWatchdog" || threadName == "GC_WorkerThread") {
1284 _exit(1);
1285 }
1286 }
1287 };
1288 ForkAndRunTest("-d 2 --exclude-thread DfxWatchdog,GC_WorkerThread ", true, true, callback);
1289 }
1290
1291 /**
1292 * @tc.name: ExcludeErrorThreadName
1293 * @tc.desc: Test --exclude-thread option error thread name
1294 * @tc.type: FUNC
1295 */
1296 HWTEST_F(SubCommandRecordTest, ExcludeErrorThreadName, TestSize.Level1)
1297 {
__anonf79c7ea80602(const PerfEventRecord& record) 1298 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
1299 if (record.GetType() == PERF_RECORD_SAMPLE) {
1300 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
1301 std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", recordSample.data_.tid));
1302 if (threadName == "test") {
1303 _exit(1);
1304 }
1305 }
1306 };
1307 ForkAndRunTest("-d 2 --exclude-thread test ", true, true, callback);
1308 }
1309
1310 /**
1311 * @tc.name: ExcludeErrorThreadNames
1312 * @tc.desc: Test --exclude-thread option multi error thread names
1313 * @tc.type: FUNC
1314 */
1315 HWTEST_F(SubCommandRecordTest, ExcludeErrorThreadNames, TestSize.Level1)
1316 {
__anonf79c7ea80702(const PerfEventRecord& record) 1317 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
1318 if (record.GetType() == PERF_RECORD_SAMPLE) {
1319 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
1320 std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", recordSample.data_.tid));
1321 if (threadName == "test1" || threadName == "test2") {
1322 _exit(1);
1323 }
1324 }
1325 };
1326 ForkAndRunTest("-d 2 --exclude-thread test1,test2 ", true, true, callback);
1327 }
1328
1329 /**
1330 * @tc.name: ExcludeTids
1331 * @tc.desc: Test --exclude-tid
1332 * @tc.type: FUNC
1333 */
1334 HWTEST_F(SubCommandRecordTest, ExcludeTids, TestSize.Level1)
1335 {
__anonf79c7ea80802(const PerfEventRecord& record) 1336 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
1337 if (record.GetType() == PERF_RECORD_SAMPLE) {
1338 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
1339 if (recordSample.data_.tid == 200) {
1340 _exit(1);
1341 }
1342 }
1343 };
1344 ForkAndRunTest("-d 2 -s dwarf -f 2000 --exclude-tid 200", true, true, callback);
1345 }
1346
1347 /**
1348 * @tc.name: ExcludeThread
1349 * @tc.desc: Test --exclude-thread
1350 * @tc.type: FUNC
1351 */
1352 HWTEST_F(SubCommandRecordTest, ExcludeThread, TestSize.Level1)
1353 {
__anonf79c7ea80902(const PerfEventRecord& record) 1354 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
1355 if (record.GetType() == PERF_RECORD_SAMPLE) {
1356 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
1357 std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", recordSample.data_.tid));
1358 if (threadName == "com.app.test") {
1359 _exit(1);
1360 }
1361 }
1362 };
1363 ForkAndRunTest("-d 2 -s dwarf -f 2000 --exclude-thread com.app.test", true, true, callback);
1364 }
1365
1366 /**
1367 * @tc.name: ExcludeMixedThreadName
1368 * @tc.desc: Test --exclude-thread option mixed correct name and error name
1369 * @tc.type: FUNC
1370 */
1371 HWTEST_F(SubCommandRecordTest, ExcludeMixedThreadName, TestSize.Level1)
1372 {
__anonf79c7ea80a02(const PerfEventRecord& record) 1373 SubCommandRecord::CheckRecordCallBack callback = [](const PerfEventRecord& record) {
1374 if (record.GetType() == PERF_RECORD_SAMPLE) {
1375 const PerfRecordSample& recordSample = static_cast<const PerfRecordSample&>(record);
1376 std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", recordSample.data_.tid));
1377 if (threadName == "DfxWatchdog" || threadName == "test") {
1378 _exit(1);
1379 }
1380 }
1381 };
1382 ForkAndRunTest("-d 2 --exclude-thread DfxWatchdog,test ", true, true, callback);
1383 }
1384
1385 // --restart
1386 HWTEST_F(SubCommandRecordTest, ReStartNotApp1, TestSize.Level1)
1387 {
1388 TestRecordCommand("-p 5 --restart ", false, false);
1389 }
1390
1391 HWTEST_F(SubCommandRecordTest, ReStartNotApp2, TestSize.Level1)
1392 {
1393 TestRecordCommand("-a --restart ", false, false);
1394 }
1395
1396 HWTEST_F(SubCommandRecordTest, ReStartNotApp3, TestSize.Level1)
1397 {
1398 TestRecordCommand("-p 5 -a --restart ", false, false);
1399 }
1400
1401 HWTEST_F(SubCommandRecordTest, ReStartConflict, TestSize.Level1)
1402 {
1403 TestRecordCommand("--restart -a ", false, true);
1404 }
1405
1406 HWTEST_F(SubCommandRecordTest, ReStart, TestSize.Level1)
1407 {
1408 TestRecordCommand("--restart ", false, true);
1409 }
1410
1411 // --exclude-tid
1412 HWTEST_F(SubCommandRecordTest, ExcludeTidConflict, TestSize.Level1)
1413 {
1414 TestRecordCommand("--exclude-tid 5 --exclude-thread test ", false, true);
1415 }
1416
1417 /**
1418 * @tc.name: CmdLinesSizeSucess
1419 * @tc.desc: Test --cmdline-size option
1420 * @tc.type: FUNC
1421 */
1422 HWTEST_F(SubCommandRecordTest, CmdLinesSizeSucess, TestSize.Level1)
1423 {
1424 ForkAndRunTest("-d 2 --cmdline-size 1024 ", true);
1425 }
1426
1427 /**
1428 * @tc.name: CmdLinesSizeOutRange
1429 * @tc.desc: Test --cmdline-size option
1430 * @tc.type: FUNC
1431 */
1432 HWTEST_F(SubCommandRecordTest, CmdLinesSizeOutRange, TestSize.Level1)
1433 {
1434 TestRecordCommand("-d 2 --cmdline-size 8192 ", false);
1435 }
1436
1437 /**
1438 * @tc.name: CmdLinesSizeNotPowerOf2
1439 * @tc.desc: Test --cmdline-size option
1440 * @tc.type: FUNC
1441 */
1442 HWTEST_F(SubCommandRecordTest, CmdLinesSizeNotPowerOf2, TestSize.Level1)
1443 {
1444 TestRecordCommand("-d 2 --cmdline-size 1000 ", false);
1445 }
1446
1447 /**
1448 * @tc.name: EnableDebugInfoSymbolicFp
1449 * @tc.desc: Test --enable-debuginfo-symbolic option with -s fp
1450 * @tc.type: FUNC
1451 */
1452 HWTEST_F(SubCommandRecordTest, EnableDebugInfoSymbolicFp, TestSize.Level1)
1453 {
1454 ForkAndRunTest("-d 2 -s fp --enable-debuginfo-symbolic ", true);
1455 }
1456
1457 /**
1458 * @tc.name: EnableDebugInfoSymbolicDwarf
1459 * @tc.desc: Test --enable-debuginfo-symbolic option with -s dwarf
1460 * @tc.type: FUNC
1461 */
1462 HWTEST_F(SubCommandRecordTest, EnableDebugInfoSymbolicDwarf, TestSize.Level1)
1463 {
1464 ForkAndRunTest("-d 2 -s dwarf --enable-debuginfo-symbolic ", true);
1465 }
1466
1467 /**
1468 * @tc.name: CallChainUserOnlyFp
1469 * @tc.desc: Test --callchain-useronly option with fp
1470 * @tc.type: FUNC
1471 */
1472 HWTEST_F(SubCommandRecordTest, CallChainUserOnlyFp, TestSize.Level1)
1473 {
1474 ForkAndRunTest("-d 2 -s fp --callchain-useronly", true, true);
1475 }
1476
1477 /**
1478 * @tc.name: CallChainUserOnlyDwarf
1479 * @tc.desc: Test --callchain-useronly option with dwarf
1480 * @tc.type: FUNC
1481 */
1482 HWTEST_F(SubCommandRecordTest, CallChainUserOnlyDwarf, TestSize.Level1)
1483 {
1484 ForkAndRunTest("-d 2 -s dwarf --callchain-useronly", true, true);
1485 }
1486
1487 /**
1488 * @tc.name: CallChainUserOnlyError
1489 * @tc.desc: Test --callchain-useronly option without fp/dwarf
1490 * @tc.type: FUNC
1491 */
1492 HWTEST_F(SubCommandRecordTest, CallChainUserOnlyError, TestSize.Level1)
1493 {
1494 ForkAndRunTest("-d 2 --callchain-useronly", false, true);
1495 }
1496
1497 /**
1498 * @tc.name: DedupStack
1499 * @tc.desc: Test --dedup_stack option
1500 * @tc.type: FUNC
1501 */
1502 HWTEST_F(SubCommandRecordTest, DedupStack, TestSize.Level1)
1503 {
1504 ForkAndRunTest("-d 2 -s dwarf --dedup_stack", true, true);
1505 }
1506
1507 /**
1508 * @tc.name: DedupStackErr
1509 * @tc.desc: Test --dedup_stack option with -a
1510 * @tc.type: FUNC
1511 */
1512 HWTEST_F(SubCommandRecordTest, DedupStackErr, TestSize.Level1)
1513 {
1514 TestRecordCommand("-d 2 -a -s dwarf --dedup_stack", false, false);
1515 }
1516
1517 /**
1518 * @tc.name: TestNoFork
1519 * @tc.desc: Test no fork
1520 * @tc.type: FUNC
1521 */
1522 HWTEST_F(SubCommandRecordTest, TestNoFork, TestSize.Level1)
1523 {
1524 TestRecordCommand("-d 2 -s dwarf --dedup_stack -f 2000 --cmdline-size 1024", true, true);
1525 }
1526
1527 /**
1528 * @tc.name: TestAllNoFork
1529 * @tc.desc: Test no fork with -a
1530 * @tc.type: FUNC
1531 */
1532 HWTEST_F(SubCommandRecordTest, TestAllNoFork, TestSize.Level1)
1533 {
1534 TestRecordCommand("-d 2 -a -s dwarf --clockid monotonic --exclude-hiperf", true, false);
1535 }
1536
1537 /**
1538 * @tc.name: CreateFifoServer
1539 * @tc.desc: Test create Fipo server
1540 * @tc.type: FUNC
1541 */
1542 HWTEST_F(SubCommandRecordTest, CreateFifoServer, TestSize.Level1)
1543 {
1544 SubCommandRecord cmd;
1545 EXPECT_EQ(cmd.CreateFifoServer(), false);
1546 }
1547
1548 /**
1549 * @tc.name: SendFifoAndWaitReply
1550 * @tc.desc: Test send Fifo and wait reply
1551 * @tc.type: FUNC
1552 */
1553 HWTEST_F(SubCommandRecordTest, SendFifoAndWaitReply, TestSize.Level1)
1554 {
1555 SubCommandRecord cmd;
1556 std::string test = "test";
1557 EXPECT_EQ(cmd.SendFifoAndWaitReply(test, CONTROL_WAITREPY_TOMEOUT), false);
1558 }
1559
1560 /**
1561 * @tc.name: ReportErr
1562 * @tc.desc: Test report option error
1563 * @tc.type: FUNC
1564 */
1565 HWTEST_F(SubCommandRecordTest, ReportErr, TestSize.Level1)
1566 {
1567 TestRecordCommand("-d 2 -a --report ", false, false);
1568 }
1569
1570 /**
1571 * @tc.name: TestHasReport
1572 * @tc.desc: Test --report option
1573 * @tc.type: FUNC
1574 */
1575 HWTEST_F(SubCommandRecordTest, TestHasReport, TestSize.Level1)
1576 {
1577 TestRecordCommand("-d 2 -s dwarf --report", true, true);
1578 }
1579
1580 /**
1581 * @tc.name: TraceCommand
1582 * @tc.desc: Test TraceCommand option
1583 * @tc.type: FUNC
1584 */
1585 HWTEST_F(SubCommandRecordTest, TraceCommand, TestSize.Level1)
1586 {
1587 TestRecordCommand("-d 2 -s dwarf ls", true, false);
1588 }
1589
1590 /**
1591 * @tc.name: TraceCommandErr
1592 * @tc.desc: Test InvalidCommand option
1593 * @tc.type: FUNC
1594 */
1595 HWTEST_F(SubCommandRecordTest, TraceCommandErr, TestSize.Level1)
1596 {
1597 TestRecordCommand("-d 2 -s dwarf invalidcommand", false, false);
1598 }
1599
1600 /**
1601 * @tc.name: TestInputErr
1602 * @tc.desc: Test input with -a
1603 * @tc.type: FUNC
1604 */
1605 HWTEST_F(SubCommandRecordTest, TestInputErr, TestSize.Level1)
1606 {
1607 TestRecordCommand("-d 2 -a -s dwarf -f 2000 --pipe_input", false, false);
1608 }
1609
1610 /**
1611 * @tc.name: TestOutputErr
1612 * @tc.desc: Test output with -a
1613 * @tc.type: FUNC
1614 */
1615 HWTEST_F(SubCommandRecordTest, TestOutputErr, TestSize.Level1)
1616 {
1617 TestRecordCommand("-d 2 -a -s dwarf -f 2000 --pipe_output", false, false);
1618 }
1619
1620 /**
1621 * @tc.name: TestBranchFilterErr
1622 * @tc.desc: Test branch filter with -a
1623 * @tc.type: FUNC
1624 */
1625 HWTEST_F(SubCommandRecordTest, TestBranchFilterErr, TestSize.Level1)
1626 {
1627 TestRecordCommand("-d 2 -a -s dwarf -f 2000 -j", false, false);
1628 }
1629
1630 /**
1631 * @tc.name: TestCallStackErr
1632 * @tc.desc: Test call stack with -a
1633 * @tc.type: FUNC
1634 */
1635 HWTEST_F(SubCommandRecordTest, TestCallStackErr, TestSize.Level1)
1636 {
1637 TestRecordCommand("-d 2 -a -f 2000 --call-stack", false, false);
1638 }
1639
1640 /**
1641 * @tc.name: TestEventGroupErr
1642 * @tc.desc: Test event group with -a
1643 * @tc.type: FUNC
1644 */
1645 HWTEST_F(SubCommandRecordTest, TestEventGroupErr, TestSize.Level1)
1646 {
1647 TestRecordCommand("-d 2 -a -f 2000 -g", false, false);
1648 }
1649
1650 /**
1651 * @tc.name: TestExcludeThreadErr
1652 * @tc.desc: Test exclude-thread with -a
1653 * @tc.type: FUNC
1654 */
1655 HWTEST_F(SubCommandRecordTest, TestExcludeThreadErr, TestSize.Level1)
1656 {
1657 TestRecordCommand("-d 2 -a -f 2000 --exclude-thread", false, false);
1658 }
1659
1660 /**
1661 * @tc.name: TestSymbolDirErr
1662 * @tc.desc: Test symbol-dir with -a
1663 * @tc.type: FUNC
1664 */
1665 HWTEST_F(SubCommandRecordTest, TestSymbolDirErr, TestSize.Level1)
1666 {
1667 TestRecordCommand("-d 2 -a -f 2000 --symbol-dir", false, false);
1668 }
1669
1670 /**
1671 * @tc.name: TestControlErr
1672 * @tc.desc: Test control with -a
1673 * @tc.type: FUNC
1674 */
1675 HWTEST_F(SubCommandRecordTest, TestControlErr, TestSize.Level1)
1676 {
1677 TestRecordCommand("-d 2 -a -f 2000 --control", false, false);
1678 }
1679
1680 /**
1681 * @tc.name: TestCmdlineSizeErr
1682 * @tc.desc: Test cmdline-size with -a
1683 * @tc.type: FUNC
1684 */
1685 HWTEST_F(SubCommandRecordTest, TestCmdlineSizeErr, TestSize.Level1)
1686 {
1687 TestRecordCommand("-d 2 -a -f 2000 --cmdline-size", false, false);
1688 }
1689
1690 /**
1691 * @tc.name: AddReportArgs
1692 * @tc.desc: Test AddReportArgs with -a
1693 * @tc.type: FUNC
1694 */
1695 HWTEST_F(SubCommandRecordTest, ReportSampleAll, TestSize.Level1)
1696 {
1697 SubCommandRecord command;
1698 command.targetSystemWide_ = true;
1699
1700 CommandReporter reporter("record");
1701 reporter.isReported_ = true;
1702 command.AddReportArgs(reporter);
1703 EXPECT_EQ(reporter.targetProcess_, "ALL");
1704 }
1705
1706 /**
1707 * @tc.name: AddReportArgs
1708 * @tc.desc: Test AddReportArgs with -p
1709 * @tc.type: FUNC
1710 */
1711 HWTEST_F(SubCommandRecordTest, ReportSamplePid, TestSize.Level1)
1712 {
1713 SubCommandRecord command;
1714 command.selectPids_ = { getpid() };
1715 std::string name = GetProcessName(getpid());
1716
1717 CommandReporter reporter("record");
1718 reporter.isReported_ = true;
1719 command.AddReportArgs(reporter);
1720 EXPECT_EQ(reporter.targetProcess_, name);
1721 }
1722
1723 /**
1724 * @tc.name: AddReportArgs
1725 * @tc.desc: Test AddReportArgs with --app
1726 * @tc.type: FUNC
1727 */
1728 HWTEST_F(SubCommandRecordTest, ReportSampleApp, TestSize.Level1)
1729 {
1730 SubCommandRecord command;
1731 command.appPackage_ = "com.test.app";
1732
1733 CommandReporter reporter("record");
1734 reporter.isReported_ = true;
1735 command.AddReportArgs(reporter);
1736 EXPECT_EQ(reporter.targetProcess_, "com.test.app");
1737 }
1738
1739 /**
1740 * @tc.name: ChecKernel
1741 * @tc.desc: Test kernel version
1742 * @tc.type: FUNC
1743 */
1744 HWTEST_F(SubCommandRecordTest, ChecKernel, TestSize.Level1)
1745 {
1746 utsname unameBuf;
1747 if ((uname(&unameBuf)) == 0) {
1748 std::string osrelease = unameBuf.release;
1749 std::string sysname = unameBuf.sysname;
1750 EXPECT_EQ(osrelease.find(HMKERNEL) != std::string::npos || sysname.find("Linux") != std::string::npos, true);
1751 }
1752 }
1753
1754 /**
1755 * @tc.name: RecordAndReport
1756 * @tc.desc: Test record and report
1757 * @tc.type: FUNC
1758 */
1759 HWTEST_F(SubCommandRecordTest, RecordAndReport, TestSize.Level1)
1760 {
1761 TestRecordCommand("-d 5 -s dwarf -o /data/local/tmp/test_perf.data", true, true);
1762 StdoutRecord stdoutRecord;
1763 stdoutRecord.Start();
1764 EXPECT_EQ(Command::DispatchCommand("report -i /data/local/tmp/test_perf.data"), true);
1765 std::string stringOut = stdoutRecord.Stop();
1766 EXPECT_EQ(stringOut.find("report done") != std::string::npos, true);
1767 }
1768
1769 /**
1770 * @tc.name: GetInstance
1771 * @tc.desc: Test GetInstance
1772 * @tc.type: FUNC
1773 */
1774 HWTEST_F(SubCommandRecordTest, GetInstance, TestSize.Level1)
1775 {
1776 StdoutRecord stdoutRecord;
1777 stdoutRecord.Start();
1778
1779 EXPECT_EQ(SubCommandRecord::GetInstance().Name(), "record");
1780 }
1781
1782 /**
1783 * @tc.name: CheckExcludeArgs
1784 * @tc.desc: Test CheckExcludeArgs
1785 * @tc.type: FUNC
1786 */
1787 HWTEST_F(SubCommandRecordTest, CheckExcludeArgs, TestSize.Level1)
1788 {
1789 StdoutRecord stdoutRecord;
1790 stdoutRecord.Start();
1791
1792 SubCommandRecord record;
1793 record.targetSystemWide_ = true;
1794 record.excludeTidArgs_ = { 1, 2, 3 };
1795 EXPECT_EQ(record.CheckExcludeArgs(), false);
1796
1797 record.excludeTidArgs_ = {};
1798 record.excludeThreadNameArgs_ = { "a" };
1799 EXPECT_EQ(record.CheckExcludeArgs(), false);
1800
1801 record.targetSystemWide_ = false;
1802 record.excludeProcessNameArgs_ = { "a" };
1803 EXPECT_EQ(record.CheckExcludeArgs(), false);
1804
1805 record.excludeProcessNameArgs_ = {};
1806 record.excludeHiperf_ = true;
1807 EXPECT_EQ(record.CheckExcludeArgs(), false);
1808
1809 record.excludeHiperf_ = false;
1810 EXPECT_EQ(record.CheckExcludeArgs(), true);
1811 }
1812
1813 /**
1814 * @tc.name: ParseControlCmd
1815 * @tc.desc: Test ParseControlCmd
1816 * @tc.type: FUNC
1817 */
1818 HWTEST_F(SubCommandRecordTest, ParseControlCmd, TestSize.Level1)
1819 {
1820 StdoutRecord stdoutRecord;
1821 stdoutRecord.Start();
1822
1823 SubCommandRecord record;
1824 std::vector<std::string> strs {"prepare", "start", "pause", "resume", "output", "stop", ""};
1825 for (const auto& str : strs) {
1826 EXPECT_TRUE(record.ParseControlCmd(str));
1827 }
1828
1829 EXPECT_FALSE(record.ParseControlCmd("ABC"));
1830 }
1831
1832 /**
1833 * @tc.name: PostOutputRecordFile
1834 * @tc.desc: Test PostOutputRecordFile
1835 * @tc.type: FUNC
1836 */
1837 HWTEST_F(SubCommandRecordTest, PostOutputRecordFile, TestSize.Level1)
1838 {
1839 StdoutRecord stdoutRecord;
1840 stdoutRecord.Start();
1841
1842 SubCommandRecord record;
1843 record.fileWriter_ = std::make_unique<PerfFileWriter>();
1844 record.outputEnd_ = false;
1845 EXPECT_EQ(record.PostOutputRecordFile(false), true);
1846 EXPECT_EQ(record.fileWriter_, nullptr);
1847 EXPECT_EQ(record.outputEnd_, true);
1848 }
1849
1850 /**
1851 * @tc.name: InitControlCommandHandlerMap
1852 * @tc.desc: Test InitControlCommandHandlerMap
1853 * @tc.type: FUNC
1854 */
1855 HWTEST_F(SubCommandRecordTest, InitControlCommandHandlerMap, TestSize.Level1)
1856 {
1857 StdoutRecord stdoutRecord;
1858 stdoutRecord.Start();
1859
1860 SubCommandRecord record;
1861 record.InitControlCommandHandlerMap();
1862 EXPECT_EQ(record.controlCommandHandlerMap_.size(), 7u);
1863 }
1864
1865 /**
1866 * @tc.name: CollectExcludeThread
1867 * @tc.desc: Test CollectExcludeThread
1868 * @tc.type: FUNC
1869 */
1870 HWTEST_F(SubCommandRecordTest, CollectExcludeThread1, TestSize.Level1)
1871 {
1872 StdoutRecord stdoutRecord;
1873 stdoutRecord.Start();
1874
1875 SubCommandRecord record;
1876 record.excludeHiperf_ = false;
1877 record.excludeTids_ = {};
1878 pid_t pid = getpid();
1879 std::string name = GetProcessName(pid);
1880 size_t pos = name.find_last_of("/");
1881 if (pos != std::string::npos) {
1882 name = name.substr(pos + 1);
1883 }
1884 record.excludeProcessNameArgs_ = { name };
1885 record.excludeTidArgs_ = {};
1886 record.CollectExcludeThread();
1887
1888 ASSERT_GE(record.excludePids_.size(), 1u);
1889 EXPECT_EQ(record.excludeTids_.size(), 0u);
1890 bool get = false;
1891 for (pid_t id : record.excludePids_) {
1892 if (pid == id) {
1893 get = true;
1894 break;
1895 }
1896 }
1897 EXPECT_EQ(get, true);
1898 }
1899
1900 /**
1901 * @tc.name: SetExcludeHiperf
1902 * @tc.desc: Test SetExcludeHiperf
1903 * @tc.type: FUNC
1904 */
1905 HWTEST_F(SubCommandRecordTest, SetExcludeHiperf, TestSize.Level1)
1906 {
1907 StdoutRecord stdoutRecord;
1908 stdoutRecord.Start();
1909
1910 SubCommandRecord record;
1911 record.excludeHiperf_ = true;
1912 pid_t pid = getpid();
1913 record.SetExcludeHiperf();
1914
1915 ASSERT_EQ(record.excludePids_.size(), 1u);
1916 EXPECT_EQ(*(record.excludePids_.begin()), pid);
1917 }
1918
1919 /**
1920 * @tc.name: CollectExcludeThread
1921 * @tc.desc: Test CollectExcludeThread
1922 * @tc.type: FUNC
1923 */
1924 HWTEST_F(SubCommandRecordTest, CollectExcludeThread3, TestSize.Level1)
1925 {
1926 StdoutRecord stdoutRecord;
1927 stdoutRecord.Start();
1928
1929 SubCommandRecord record;
1930 record.excludeHiperf_ = false;
1931 record.excludeTids_ = {};
1932 record.excludeProcessNameArgs_ = {};
1933 record.excludeTidArgs_ = {1, 2, 3};
1934 record.CollectExcludeThread();
1935
1936 ASSERT_EQ(record.excludePids_.size(), 0u);
1937 ASSERT_EQ(record.excludeTids_.size(), 3u);
1938
1939 for (const auto& tid : record.excludeTidArgs_) {
1940 EXPECT_TRUE(record.excludeTids_.find(tid) != record.excludeTids_.end());
1941 }
1942 }
1943
1944 /**
1945 * @tc.name: IsThreadExcluded
1946 * @tc.desc: Test IsThreadExcluded
1947 * @tc.type: FUNC
1948 */
1949 HWTEST_F(SubCommandRecordTest, IsThreadExcluded, TestSize.Level1)
1950 {
1951 StdoutRecord stdoutRecord;
1952 stdoutRecord.Start();
1953
1954 SubCommandRecord record;
1955 record.excludePids_ = { 1, 2 };
1956 record.excludeTids_ = { 1, 2, 3 };
1957
1958 EXPECT_EQ(record.IsThreadExcluded(1, 1), true);
1959 EXPECT_EQ(record.IsThreadExcluded(1, 3), true);
1960 EXPECT_EQ(record.IsThreadExcluded(1, 4), true);
1961 EXPECT_EQ(record.IsThreadExcluded(3, 1), true);
1962 EXPECT_EQ(record.IsThreadExcluded(3, 5), false);
1963 }
1964
1965 /**
1966 * @tc.name: CheckBacktrackOption
1967 * @tc.desc: Test CheckBacktrackOption
1968 * @tc.type: FUNC
1969 */
1970 HWTEST_F(SubCommandRecordTest, CheckBacktrackOption, TestSize.Level1)
1971 {
1972 StdoutRecord stdoutRecord;
1973 stdoutRecord.Start();
1974
1975 SubCommandRecord record;
1976 record.backtrack_ = false;
1977 EXPECT_EQ(record.CheckBacktrackOption(), true);
1978
1979 record.backtrack_ = true;
1980 record.controlCmd_ = {};
1981 record.clientPipeInput_ = -1;
1982 EXPECT_EQ(record.CheckBacktrackOption(), false);
1983
1984 record.clientPipeInput_ = 0;
1985 record.clockId_ = "";
1986 EXPECT_EQ(record.CheckBacktrackOption(), true);
1987
1988 record.clockId_ = "realtime";
1989 EXPECT_EQ(record.CheckBacktrackOption(), false);
1990
1991 record.clockId_ = "boottime";
1992 EXPECT_EQ(record.CheckBacktrackOption(), true);
1993 }
1994
1995 /**
1996 * @tc.name: GetSpeOptions
1997 * @tc.desc: Test GetSpeOptions
1998 * @tc.type: FUNC
1999 */
2000 HWTEST_F(SubCommandRecordTest, GetSpeOptions, TestSize.Level1)
2001 {
2002 constexpr uint64_t disable = 0;
2003 constexpr uint64_t enable = 1;
2004 constexpr uint64_t minLatency = 10;
2005 constexpr uint64_t eventFilter = 0x8;
2006 SubCommandRecord command;
2007 command.selectEvents_ = {"arm_spe_0/load_filter=1", "branch_filter=1", "pct_enable=1",
2008 "store_filter=0", "ts_enable=1", "pa_enable=0", "jitter=1",
2009 "min_latency=10", "event_filter=0x8/"};
2010 EXPECT_EQ(command.GetSpeOptions(), true);
2011 EXPECT_EQ(command.speOptMap_["ts_enable"], enable);
2012 EXPECT_EQ(command.speOptMap_["pa_enable"], disable);
2013 EXPECT_EQ(command.speOptMap_["pct_enable"], enable);
2014 EXPECT_EQ(command.speOptMap_["branch_filter"], enable);
2015 EXPECT_EQ(command.speOptMap_["load_filter"], enable);
2016 EXPECT_EQ(command.speOptMap_["store_filter"], disable);
2017 EXPECT_EQ(command.speOptMap_["jitter"], enable);
2018 EXPECT_EQ(command.speOptMap_["min_latency"], minLatency);
2019 EXPECT_EQ(command.speOptMap_["event_filter"], eventFilter);
2020 }
2021
2022 /**
2023 * @tc.name: CheckSpeOption
2024 * @tc.desc: Test CheckSpeOption
2025 * @tc.type: FUNC
2026 */
2027 HWTEST_F(SubCommandRecordTest, CheckSpeOption, TestSize.Level1)
2028 {
2029 constexpr uint64_t disable = 0;
2030 constexpr uint64_t enable = 1;
2031 SubCommandRecord command;
2032 command.speOptMap_["ts_enable"] = enable; // 2 : invalid value
2033 command.speOptMap_["pa_enable"] = enable;
2034 command.speOptMap_["pct_enable"] = enable;
2035 command.speOptMap_["branch_filter"] = enable;
2036 command.speOptMap_["load_filter"] = disable;
2037 command.speOptMap_["store_filter"] = enable;
2038 command.speOptMap_["jitter"] = enable;
2039 EXPECT_EQ(command.CheckSpeOption(), true);
2040 }
2041
2042 /**
2043 * @tc.name: CheckSpeOptionErr
2044 * @tc.desc: Test CheckSpeOption
2045 * @tc.type: FUNC
2046 */
2047 HWTEST_F(SubCommandRecordTest, CheckSpeOptionErr, TestSize.Level1)
2048 {
2049 constexpr uint64_t disable = 0;
2050 constexpr uint64_t enable = 1;
2051 constexpr uint64_t invalid = 20;
2052 SubCommandRecord command;
2053 command.speOptMap_["branch_filter"] = invalid;
2054 command.speOptMap_["load_filter"] = disable;
2055 command.speOptMap_["jitter"] = enable;
2056 EXPECT_EQ(command.CheckSpeOption(), false);
2057 }
2058
2059 HWTEST_F(SubCommandRecordTest, CheckThreadName, TestSize.Level1)
2060 {
2061 bool checkRet = false;
2062 SubCommandRecord cmd;
2063 PerfEvents event;
2064 pid_t timeTid = 0;
2065 event.backtrack_ = true;
2066 event.eventGroupItem_.emplace_back();
2067 event.eventGroupItem_[0].eventItems.emplace_back();
2068 event.readRecordThreadRunning_ = true;
2069
__anonf79c7ea80b02(PerfEventRecord& record) 2070 auto saveRecord = [](PerfEventRecord& record) -> bool {
2071 return true;
2072 };
2073 cmd.virtualRuntime_.SetRecordMode(saveRecord);
2074 EXPECT_EQ(event.PrepareRecordThread(), true);
2075 std::vector<pid_t> tids = GetSubthreadIDs(getpid());
2076 EXPECT_FALSE(tids.empty());
2077 bool get = false;
2078 for (const pid_t tid : tids) {
2079 std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", tid));
2080 while (threadName.back() == '\0' || threadName.back() == '\n') {
2081 threadName.pop_back();
2082 }
2083 if (threadName == "timer_thread") {
2084 timeTid = tid;
2085 get = true;
2086 break;
2087 }
2088 }
2089 EXPECT_EQ(get, true);
2090
2091 std::string recordCommand = "-t " + std::to_string(timeTid) + " -d 5 -s dwarf -o /data/local/tmp/tid_name.data";
2092 TestRecordCommand(recordCommand, true, false);
2093 StdoutRecord stdoutRecord;
2094 stdoutRecord.Start();
2095 EXPECT_EQ(Command::DispatchCommand("report -i /data/local/tmp/tid_name.data -o /data/local/tmp/tid_name.report"),
2096 true);
2097 std::string stringOut = stdoutRecord.Stop();
2098 EXPECT_EQ(stringOut.find("report done") != std::string::npos, true);
2099
2100 std::ifstream ifs("/data/local/tmp/tid_name.report", std::ifstream::in);
2101 EXPECT_EQ(ifs.is_open(), true);
2102 std::string line;
2103 while (getline(ifs, line)) {
2104 if (line.find("timer_thread") != std::string::npos) {
2105 checkRet = true;
2106 break;
2107 }
2108 }
2109 ifs.close();
2110 EXPECT_EQ(checkRet, true);
2111 }
2112
2113 HWTEST_F(SubCommandRecordTest, CheckDevhostMapOffset, TestSize.Level1)
2114 {
2115 utsname unameBuf;
2116 bool isHM = true;
2117 if ((uname(&unameBuf)) == 0) {
2118 std::string osrelease = unameBuf.release;
2119 std::string sysname = unameBuf.sysname;
2120 isHM = osrelease.find(HMKERNEL) != std::string::npos;
2121 }
2122 if (isHM) {
2123 bool checkRet = false;
2124 SubCommandRecord cmd;
2125 cmd.SetHM();
2126 VirtualThread &kthread = cmd.virtualRuntime_.GetThread(cmd.virtualRuntime_.devhostPid_,
2127 cmd.virtualRuntime_.devhostPid_);
2128 kthread.ParseDevhostMap(cmd.virtualRuntime_.devhostPid_);
2129 TestRecordCommand("-d 5 -s dwarf -o /data/local/tmp/test_maps.data", true, true);
2130 StdoutRecord stdoutRecord;
2131 stdoutRecord.Start();
2132 EXPECT_EQ(Command::DispatchCommand("dump -i /data/local/tmp/test_maps.data"), true);
2133 std::string stringOut = stdoutRecord.Stop();
2134 std::istringstream stream(stringOut);
2135
2136 std::string line;
2137 bool isMmapRecord = false;
2138 bool isMmapFirstLine = false;
2139 uint64_t mapOffset = 0;
2140 while (getline(stream, line)) {
2141 if (strstr(line.c_str(), "record mmap:") != nullptr) {
2142 isMmapRecord = true;
2143 continue;
2144 }
2145 if (strstr(line.c_str(), "record sample:") != nullptr) {
2146 break;
2147 }
2148 if (isMmapFirstLine) {
2149 isMmapFirstLine = false;
2150 uint64_t pgoff = 0;
2151 int ret = sscanf_s(line.c_str(), " %*s 0x%" PRIx64 ", %*s %*s", &pgoff);
2152 constexpr int numSlices {1};
2153 if (ret != numSlices) {
2154 printf("unknown line %d: '%s' \n", ret, line.c_str());
2155 continue;
2156 }
2157 EXPECT_EQ(mapOffset, pgoff);
2158 checkRet = true;
2159 continue;
2160 }
2161
2162 if (isMmapRecord) {
2163 isMmapRecord = false;
2164 isMmapFirstLine = GetMemMapOffset(cmd.virtualRuntime_.devhostPid_, mapOffset, kthread.memMaps_, line);
2165 }
2166 }
2167 EXPECT_EQ(checkRet, true);
2168 }
2169 }
2170
2171 HWTEST_F(SubCommandRecordTest, CheckGetCountFromFile, TestSize.Level1)
2172 {
2173 SubCommandRecord cmd;
2174 uint32_t cpuPresent = cmd.GetCountFromFile("/sys/devices/system/cpu/present");
2175 ASSERT_GT(cpuPresent, 1);
2176 uint32_t cpuOnline = cmd.GetCountFromFile("/sys/devices/system/cpu/online");
2177 ASSERT_GT(cpuOnline, 1);
2178 }
2179 } // namespace HiPerf
2180 } // namespace Developtools
2181 } // namespace OHOS
2182