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 #include "subcommand_stat_test.h"
16
17 #include <algorithm>
18 #include <cinttypes>
19 #include <regex>
20 #include <sstream>
21 #include <thread>
22
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <hilog/log.h>
26 #include <sched.h>
27
28 #include "perf_events.h"
29 #include "tracked_command.h"
30
31 using namespace testing::ext;
32 using namespace std;
33 using namespace OHOS::HiviewDFX;
34 namespace OHOS {
35 namespace Developtools {
36 namespace HiPerf {
37 class SubCommandStatTest : public testing::Test {
38 public:
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43
44 static void TestCodeThread(int &tid);
45 bool FindExpectStr(const std::string &stringOut, const std::string &counterNames) const;
46 uint EffectiveCounter(const std::string &stringOut,
47 const std::vector<std::string> &counterNames,
48 uint &effectiveHeadCounter) const;
49 uint EffectiveCounter(const std::string &stringOut, const std::string &counterNames,
50 uint &effectiveHeadCounter) const;
51 int CounterValue(const std::string &stringOut, const std::string &configName) const;
52 void CheckGroupCoverage(const std::string &stringOut,
53 const std::string &groupCounterName) const;
54
55 const std::vector<std::string> defaultConfigNames_ = {
56 "hw-branch-misses",
57 "hw-cpu-cycles",
58 "hw-instructions",
59 #if defined(__aarch64__)
60 "hw-stalled-cycles-backend",
61 "hw-stalled-cycles-frontend",
62 #endif
63 "sw-context-switches",
64 "sw-page-faults",
65 "sw-task-clock",
66 };
67
68 const int defaultRunTimeoutMs = 4100;
69 const std::string timeReportStr = "Report at ";
70 };
71
SetUpTestCase()72 void SubCommandStatTest::SetUpTestCase() {}
73
TearDownTestCase()74 void SubCommandStatTest::TearDownTestCase() {}
75
SetUp()76 void SubCommandStatTest::SetUp()
77 {
78 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
79 ASSERT_EQ(RegisterSubCommandStat(), true);
80 }
81
TearDown()82 void SubCommandStatTest::TearDown()
83 {
84 SubCommand::ClearSubCommands();
85 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
86 }
87
TestCodeThread(int & tid)88 void SubCommandStatTest::TestCodeThread(int &tid)
89 {
90 std::vector<std::unique_ptr<char[]>> mems;
91 tid = gettid();
92 printf("TestCodeThread:%d ++\n", tid);
93 constexpr int sleepTime {500};
94 const int sum = 10;
95 const int num = 2;
96 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
97
98 constexpr size_t memSize {1024};
99 for (uint i = 0; i < sum * memSize; i++) {
100 if (i % num == 0) {
101 mems.push_back(std::make_unique<char[]>(memSize));
102 } else {
103 mems.push_back(std::make_unique<char[]>(memSize * num));
104 }
105 }
106
107 for (uint i = 0; i < sum * memSize; i++) {
108 mems.pop_back();
109 }
110
111 std::this_thread::sleep_for(std::chrono::milliseconds(num * sleepTime));
112 printf("TestCodeThread:%d --\n", tid);
113 }
114
EffectiveCounter(const std::string & stringOut,const std::string & counterNames,uint & effectiveHeadCounter) const115 uint SubCommandStatTest::EffectiveCounter(const std::string &stringOut,
116 const std::string &counterNames,
117 uint &effectiveHeadCounter) const
118 {
119 std::string filterCounterNames {};
120 filterCounterNames = StringReplace(counterNames, ":u", "");
121 filterCounterNames = StringReplace(filterCounterNames, ":k", "");
122 return EffectiveCounter(stringOut, StringSplit(filterCounterNames, ","), effectiveHeadCounter);
123 }
124
FindExpectStr(const std::string & stringOut,const std::string & counterNames) const125 bool SubCommandStatTest::FindExpectStr(const std::string &stringOut,
126 const std::string &counterNames) const
127 {
128 auto lines = StringSplit(stringOut, "\n");
129 for (auto line : lines) {
130 if (line.find(counterNames.c_str()) != std::string::npos) {
131 return true;
132 }
133 }
134
135 return false;
136 }
137
EffectiveCounter(const std::string & stringOut,const std::vector<std::string> & counterNames,uint & effectiveHeadCounter) const138 uint SubCommandStatTest::EffectiveCounter(const std::string &stringOut,
139 const std::vector<std::string> &counterNames,
140 uint &effectiveHeadCounter) const
141 {
142 uint effectiveCounter = 0;
143 for (auto name : counterNames) {
144 EXPECT_NE(stringOut.find(name), std::string::npos);
145 }
146 auto lines = StringSplit(stringOut, "\n");
147 for (auto line : lines) {
148 if (line.find(timeReportStr.c_str()) != std::string::npos) {
149 printf("reset the count because found: '%s'\n", timeReportStr.c_str());
150 // reset the count
151 effectiveCounter = 0;
152 effectiveHeadCounter++;
153 continue;
154 }
155 auto tokens = StringSplit(line.c_str(), " ");
156 constexpr size_t sizeLimit {2};
157 std::regex pattern("^\\d+[,\\d{3}]*");
158 if (tokens.size() > sizeLimit &&
159 (IsDigits(tokens[0]) || std::regex_match(tokens[0], pattern))) {
160 if (find(counterNames.begin(), counterNames.end(), tokens[1]) != counterNames.end()) {
161 uint64_t count = std::stoull(tokens[0]);
162 effectiveCounter++;
163 printf("[%u] found %s:%s count %" PRIu64 "\n", effectiveCounter, tokens[1].c_str(),
164 tokens[0].c_str(), count);
165 }
166 }
167 }
168
169 // no more count than max
170 printf("effectiveCounter %u \n", effectiveCounter);
171 printf("effectiveHeadCounter %u \n", effectiveHeadCounter);
172
173 return effectiveCounter;
174 }
175
CounterValue(const std::string & stringOut,const std::string & configName) const176 int SubCommandStatTest::CounterValue(const std::string &stringOut,
177 const std::string &configName) const
178 {
179 int res {-1};
180 auto lines = StringSplit(stringOut, "\n");
181 for (auto line : lines) {
182 auto tokens = StringSplit(line.c_str(), " ");
183 constexpr size_t sizeLimit {2};
184 if (tokens.size() > sizeLimit and IsDigits(tokens[0])) {
185 if (tokens[1] == configName) {
186 uint64_t count = std::stoull(tokens[0]);
187 res += count;
188 }
189 }
190 }
191 if (res != -1) {
192 ++res;
193 }
194 return res;
195 }
196
CheckGroupCoverage(const std::string & stringOut,const std::string & groupCounterName) const197 void SubCommandStatTest::CheckGroupCoverage(const std::string &stringOut,
198 const std::string &groupCounterName) const
199 {
200 std::string filterGroupCounterName = StringReplace(groupCounterName, ":u", "");
201 filterGroupCounterName = StringReplace(filterGroupCounterName, ":k", "");
202 auto groupCounterNames = StringSplit(filterGroupCounterName, ",");
203
204 for (auto name : groupCounterNames) {
205 EXPECT_NE(stringOut.find(name), std::string::npos);
206 }
207 std::string groupCoverage;
208 auto lines = StringSplit(stringOut, "\n");
209 for (auto line : lines) {
210 auto tokens = StringSplit(line.c_str(), " ");
211 if (find(groupCounterNames.begin(), groupCounterNames.end(), tokens[1]) !=
212 groupCounterNames.end()) {
213 if (groupCoverage.empty()) {
214 groupCoverage = tokens.back();
215 } else {
216 EXPECT_EQ(groupCoverage, tokens.back());
217 }
218 }
219 }
220 }
221
222 /**
223 * @tc.name: TestOnSubCommand_a
224 * @tc.desc: -a
225 * @tc.type: FUNC
226 */
227 HWTEST_F(SubCommandStatTest, TestOnSubCommand_a, TestSize.Level1)
228 {
229 StdoutRecord stdoutRecord;
230 stdoutRecord.Start();
231 const auto startTime = chrono::steady_clock::now();
232 EXPECT_EQ(Command::DispatchCommand("stat -a -c 0 -d 3 --dumpoptions"), true);
233 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
234 chrono::steady_clock::now() - startTime);
235 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
236
237 std::string stringOut = stdoutRecord.Stop();
238 if (HasFailure()) {
239 printf("output:\n%s", stringOut.c_str());
240 }
241
242 // some times 'sw-page-faults' is 0
243 uint effectiveHeadCounter = 0;
244 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
245 (defaultConfigNames_.size() - 1));
246 }
247
248 /**
249 * @tc.name: TestOnSubCommand_a1
250 * @tc.desc: -a
251 * @tc.type: FUNC
252 */
253 HWTEST_F(SubCommandStatTest, TestOnSubCommand_a1, TestSize.Level1)
254 {
255 StdoutRecord stdoutRecord;
256 stdoutRecord.Start();
257 const auto startTime = chrono::steady_clock::now();
258 EXPECT_EQ(Command::DispatchCommand("stat -a -d 3 --dumpoptions"), true);
259 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
260 chrono::steady_clock::now() - startTime);
261 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
262
263 std::string stringOut = stdoutRecord.Stop();
264 printf("output:\n%s", stringOut.c_str());
265 if (HasFailure()) {
266 printf("output:\n%s", stringOut.c_str());
267 }
268
269 // some times 'sw-page-faults' is 0
270 uint effectiveHeadCounter = 0;
271 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
272 (defaultConfigNames_.size() - 1));
273 }
274
275 /**
276 * @tc.name: TestOnSubCommand_a2
277 * @tc.desc: -a
278 * @tc.type: FUNC
279 */
280 HWTEST_F(SubCommandStatTest, TestOnSubCommand_a2, TestSize.Level1)
281 {
282 StdoutRecord stdoutRecord;
283 stdoutRecord.Start();
284 const auto startTime = chrono::steady_clock::now();
285 EXPECT_EQ(Command::DispatchCommand("stat -a -d 3"), true);
286 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
287 chrono::steady_clock::now() - startTime);
288 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
289
290 std::string stringOut = stdoutRecord.Stop();
291 if (HasFailure()) {
292 printf("output:\n%s", stringOut.c_str());
293 }
294
295 // some times 'sw-page-faults' is 0
296 uint effectiveHeadCounter = 0;
297 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
298 (defaultConfigNames_.size() - 1));
299 }
300
301 /**
302 * @tc.name: TestOnSubCommand_a3
303 * @tc.desc: -a
304 * @tc.type: FUNC
305 */
306 HWTEST_F(SubCommandStatTest, TestOnSubCommand_a3, TestSize.Level1)
307 {
308 StdoutRecord stdoutRecord;
309 stdoutRecord.Start();
310 const auto startTime = chrono::steady_clock::now();
311 EXPECT_EQ(Command::DispatchCommand("stat -a -c 0 -d 3"), true);
312 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
313 chrono::steady_clock::now() - startTime);
314 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
315
316 std::string stringOut = stdoutRecord.Stop();
317 if (HasFailure()) {
318 printf("output:\n%s", stringOut.c_str());
319 }
320
321 // some times 'sw-page-faults' is 0
322 uint effectiveHeadCounter = 0;
323 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
324 (defaultConfigNames_.size() - 1));
325 }
326
327 /**
328 * @tc.name: TestOnSubCommand_a4
329 * @tc.desc: -a
330 * @tc.type: FUNC
331 */
332 HWTEST_F(SubCommandStatTest, TestOnSubCommand_a4, TestSize.Level1)
333 {
334 StdoutRecord stdoutRecord;
335 stdoutRecord.Start();
336 const auto startTime = chrono::steady_clock::now();
337 EXPECT_EQ(Command::DispatchCommand("stat -a test"), false);
338 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
339 chrono::steady_clock::now() - startTime);
340 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
341
342 std::string stringOut = stdoutRecord.Stop();
343 if (HasFailure()) {
344 printf("output:\n%s", stringOut.c_str());
345 }
346
347 // some times 'sw-page-faults' is 0
348 std::string expectStr = "failed";
349 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
350 }
351
352 /**
353 * @tc.name: TestOnSubCommand_c
354 * @tc.desc: -c
355 * @tc.type: FUNC
356 */
357 HWTEST_F(SubCommandStatTest, TestOnSubCommand_c, TestSize.Level1)
358 {
359 int tid1 = 0;
360 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
361
362 printf("wait child thread run.\n");
363 while (tid1 == 0) {
364 std::this_thread::sleep_for(std::chrono::milliseconds(10));
365 }
366 // we need bound us to cpu which we selelct
367 cpu_set_t mask, oldMask;
368 CPU_ZERO(&mask);
369 CPU_SET(1, &mask);
370
371 sched_getaffinity(0, sizeof(cpu_set_t), &oldMask);
372 sched_setaffinity(0, sizeof(cpu_set_t), &mask);
373 EXPECT_LE(CPU_COUNT(&mask), CPU_COUNT(&oldMask));
374
375 std::string cmdstr = "stat -p ";
376 cmdstr += std::to_string(tid1);
377 cmdstr += " -c 0 -d 3 --dumpoptions";
378
379 StdoutRecord stdoutRecord;
380 stdoutRecord.Start();
381 const auto startTime = chrono::steady_clock::now();
382 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
383 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
384 chrono::steady_clock::now() - startTime);
385 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
386
387 std::string stringOut = stdoutRecord.Stop();
388 if (HasFailure()) {
389 printf("output:\n%s", stringOut.c_str());
390 }
391 // some times 'sw-page-faults' is 0
392 uint effectiveHeadCounter = 0u;
393 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
394 (defaultConfigNames_.size() - 1));
395
396 if (stringOut.find("event not support") == std::string::npos) {
397 EXPECT_NE(stringOut.find("Timeout exit"), std::string::npos);
398 }
399
400 sched_setaffinity(0, sizeof(cpu_set_t), &oldMask);
401 sched_getaffinity(0, sizeof(cpu_set_t), &mask);
402 EXPECT_EQ(CPU_COUNT(&mask), CPU_COUNT(&oldMask));
403 t1.join();
404 }
405
406 /**
407 * @tc.name: TestOnSubCommand_c1
408 * @tc.desc: -c
409 * @tc.type: FUNC
410 */
411 HWTEST_F(SubCommandStatTest, TestOnSubCommand_c1, TestSize.Level1)
412 {
413 int tid1 = 0;
414 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
415 while (tid1 == 0) {
416 std::this_thread::sleep_for(std::chrono::milliseconds(10));
417 }
418
419 std::string cmdstr = "stat -p ";
420 cmdstr += std::to_string(tid1);
421 cmdstr += " -c 1 -d 3";
422
423 StdoutRecord stdoutRecord;
424 stdoutRecord.Start();
425 const auto startTime = chrono::steady_clock::now();
426 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
427 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
428 chrono::steady_clock::now() - startTime);
429 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
430
431 std::string stringOut = stdoutRecord.Stop();
432 if (HasFailure()) {
433 printf("output:\n%s", stringOut.c_str());
434 }
435
436 // some times 'sw-page-faults' is 0
437 uint effectiveHeadCounter = 0;
438 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
439 (defaultConfigNames_.size() - 1));
440 t1.join();
441 }
442
443 /**
444 * @tc.name: TestOnSubCommand_c2
445 * @tc.desc: -c
446 * @tc.type: FUNC
447 */
448 HWTEST_F(SubCommandStatTest, TestOnSubCommand_c2, TestSize.Level1)
449 {
450 int tid1 = 0;
451 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
452 while (tid1 == 0) {
453 std::this_thread::sleep_for(std::chrono::milliseconds(10));
454 }
455
456 std::string cmdstr = "stat -p ";
457 cmdstr += std::to_string(tid1);
458 cmdstr += " -c 0,1 -d 3";
459
460 StdoutRecord stdoutRecord;
461 stdoutRecord.Start();
462 const auto startTime = chrono::steady_clock::now();
463 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
464 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
465 chrono::steady_clock::now() - startTime);
466 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
467
468 std::string stringOut = stdoutRecord.Stop();
469 if (HasFailure()) {
470 printf("output:\n%s", stringOut.c_str());
471 }
472
473 // some times 'sw-page-faults' is 0
474 uint effectiveHeadCounter = 0;
475 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
476 (defaultConfigNames_.size() - 1));
477 t1.join();
478 }
479
480 /**
481 * @tc.name: TestOnSubCommand_c3
482 * @tc.desc: -c
483 * @tc.type: FUNC
484 */
485 HWTEST_F(SubCommandStatTest, TestOnSubCommand_c3, TestSize.Level1)
486 {
487 StdoutRecord stdoutRecord;
488 stdoutRecord.Start();
489 const auto startTime = chrono::steady_clock::now();
490 EXPECT_EQ(Command::DispatchCommand("stat -a -c 0,1 -d 3"), true);
491 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
492 chrono::steady_clock::now() - startTime);
493 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
494
495 std::string stringOut = stdoutRecord.Stop();
496 if (HasFailure()) {
497 printf("output:\n%s", stringOut.c_str());
498 }
499
500 // some times 'sw-page-faults' is 0
501 uint effectiveHeadCounter = 0;
502 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
503 (defaultConfigNames_.size() - 1));
504 }
505
506 /**
507 * @tc.name: TestOnSubCommand_c4
508 * @tc.desc: -c
509 * @tc.type: FUNC
510 */
511 HWTEST_F(SubCommandStatTest, TestOnSubCommand_c4, TestSize.Level1)
512 {
513 int tid1 = 0;
514 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
515 while (tid1 == 0) {
516 std::this_thread::sleep_for(std::chrono::milliseconds(10));
517 }
518
519 std::string cmdstr = "stat -p ";
520 cmdstr += std::to_string(tid1);
521 cmdstr += " -c test -d 3";
522
523 StdoutRecord stdoutRecord;
524 stdoutRecord.Start();
525 const auto startTime = chrono::steady_clock::now();
526 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
527 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
528 chrono::steady_clock::now() - startTime);
529 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
530
531 std::string stringOut = stdoutRecord.Stop();
532 if (HasFailure()) {
533 printf("output:\n%s", stringOut.c_str());
534 }
535
536 // some times 'sw-page-faults' is 0
537 std::string expectStr = "incorrect option";
538 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
539 t1.join();
540 }
541
542 /**
543 * @tc.name: TestOnSubCommand_c5
544 * @tc.desc: -c
545 * @tc.type: FUNC
546 */
547 HWTEST_F(SubCommandStatTest, TestOnSubCommand_c5, TestSize.Level1)
548 {
549 int tid1 = 0;
550 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
551 while (tid1 == 0) {
552 std::this_thread::sleep_for(std::chrono::milliseconds(10));
553 }
554
555 std::string cmdstr = "stat -p ";
556 cmdstr += std::to_string(tid1);
557 cmdstr += " -c -2 -d 3";
558
559 StdoutRecord stdoutRecord;
560 stdoutRecord.Start();
561 const auto startTime = chrono::steady_clock::now();
562 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
563 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
564 chrono::steady_clock::now() - startTime);
565 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
566
567 std::string stringOut = stdoutRecord.Stop();
568 if (HasFailure()) {
569 printf("output:\n%s", stringOut.c_str());
570 }
571
572 // some times 'sw-page-faults' is 0
573 std::string expectStr = "Invalid -c value";
574 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
575 t1.join();
576 }
577
578 /**
579 * @tc.name: TestOnSubCommand_d
580 * @tc.desc: -d
581 * @tc.type: FUNC
582 */
583 HWTEST_F(SubCommandStatTest, TestOnSubCommand_d, TestSize.Level1)
584 {
585 int tid1 = 0;
586 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
587 while (tid1 == 0) {
588 std::this_thread::sleep_for(std::chrono::milliseconds(10));
589 }
590
591 std::string cmdstr = "stat -p ";
592 cmdstr += std::to_string(tid1);
593 cmdstr += " -c 0 -d 3 --dumpoptions";
594
595 StdoutRecord stdoutRecord;
596 stdoutRecord.Start();
597 const auto startTime = chrono::steady_clock::now();
598 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
599 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
600 chrono::steady_clock::now() - startTime);
601 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
602
603 std::string stringOut = stdoutRecord.Stop();
604 if (HasFailure()) {
605 printf("output:\n%s", stringOut.c_str());
606 }
607 // some times 'sw-page-faults' is 0
608 uint effectiveHeadCounter = 0u;
609 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
610 (defaultConfigNames_.size() - 1));
611 t1.join();
612 }
613
614 /**
615 * @tc.name: TestOnSubCommand_p
616 * @tc.desc: -p
617 * @tc.type: FUNC
618 */
619 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p, TestSize.Level1)
620 {
621 int tid1 = 0;
622 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
623 while (tid1 == 0) {
624 std::this_thread::sleep_for(std::chrono::milliseconds(10));
625 }
626
627 std::string cmdstr = "stat -p -1 -d 3";
628
629 StdoutRecord stdoutRecord;
630 stdoutRecord.Start();
631 const auto startTime = chrono::steady_clock::now();
632 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
633 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
634 chrono::steady_clock::now() - startTime);
635 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
636
637 std::string stringOut = stdoutRecord.Stop();
638 if (HasFailure()) {
639 printf("output:\n%s", stringOut.c_str());
640 }
641
642 // some times 'sw-page-faults' is 0
643 std::string expectStr = "Invalid -p value";
644 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
645 t1.join();
646 }
647
648 /**
649 * @tc.name: TestOnSubCommand_p
650 * @tc.desc: -p
651 * @tc.type: FUNC
652 */
653 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p1, TestSize.Level1)
654 {
655 int tid1 = 0;
656 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
657 while (tid1 == 0) {
658 std::this_thread::sleep_for(std::chrono::milliseconds(10));
659 }
660
661 std::string cmdstr = "stat -a --app test -d 3";
662
663 StdoutRecord stdoutRecord;
664 stdoutRecord.Start();
665 const auto startTime = chrono::steady_clock::now();
666 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
667 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
668 chrono::steady_clock::now() - startTime);
669 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
670
671 std::string stringOut = stdoutRecord.Stop();
672 if (HasFailure()) {
673 printf("output:\n%s", stringOut.c_str());
674 }
675
676 // some times 'sw-page-faults' is 0
677 std::string expectStr = "You cannot specify -a and --app at the same time";
678 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
679 t1.join();
680 }
681
682 /**
683 * @tc.name: TestOnSubCommand_p2
684 * @tc.desc: -p
685 * @tc.type: FUNC
686 */
687 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p2, TestSize.Level1)
688 {
689 int tid1 = 0;
690 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
691 while (tid1 == 0) {
692 std::this_thread::sleep_for(std::chrono::milliseconds(10));
693 }
694
695 std::string cmdstr = "stat --app test -p 1234 -d 3";
696
697 StdoutRecord stdoutRecord;
698 stdoutRecord.Start();
699 const auto startTime = chrono::steady_clock::now();
700 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
701 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
702 chrono::steady_clock::now() - startTime);
703 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
704
705 std::string stringOut = stdoutRecord.Stop();
706 if (HasFailure()) {
707 printf("output:\n%s", stringOut.c_str());
708 }
709
710 // some times 'sw-page-faults' is 0
711 std::string expectStr = "You cannot specify --app and -t/-p at the same time";
712 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
713 t1.join();
714 }
715
716 /**
717 * @tc.name: TestOnSubCommand_chkms
718 * @tc.desc: --chkms
719 * @tc.type: FUNC
720 */
721 HWTEST_F(SubCommandStatTest, TestOnSubCommand_ch, TestSize.Level1)
722 {
723 int tid1 = 0;
724 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
725 while (tid1 == 0) {
726 std::this_thread::sleep_for(std::chrono::milliseconds(10));
727 }
728
729 std::string cmdstr = "stat -a -d 3 --chkms 201";
730
731 StdoutRecord stdoutRecord;
732 stdoutRecord.Start();
733 const auto startTime = chrono::steady_clock::now();
734 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
735 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
736 chrono::steady_clock::now() - startTime);
737 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
738
739 std::string stringOut = stdoutRecord.Stop();
740 if (HasFailure()) {
741 printf("output:\n%s", stringOut.c_str());
742 }
743
744 // some times 'sw-page-faults' is 0
745 std::string expectStr = "Invalid --chkms value '201'";
746 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
747 t1.join();
748 }
749
750 /**
751 * @tc.name: TestOnSubCommand_aa
752 * @tc.desc: aa
753 * @tc.type: FUNC
754 */
755 HWTEST_F(SubCommandStatTest, TestOnSubCommand_aa, TestSize.Level1)
756 {
757 int tid1 = 0;
758 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
759 while (tid1 == 0) {
760 std::this_thread::sleep_for(std::chrono::milliseconds(10));
761 }
762
763 std::string cmdstr = "stat aa --app 123 -d 3";
764
765 StdoutRecord stdoutRecord;
766 stdoutRecord.Start();
767 const auto startTime = chrono::steady_clock::now();
768 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
769 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
770 chrono::steady_clock::now() - startTime);
771 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
772
773 std::string stringOut = stdoutRecord.Stop();
774 if (HasFailure()) {
775 printf("output:\n%s", stringOut.c_str());
776 }
777
778 // some times 'sw-page-faults' is 0
779 std::string expectStr = "You cannot specify a cmd and --app at the same time";
780 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
781 t1.join();
782 }
783
784 /**
785 * @tc.name: TestOnSubCommand_d1
786 * @tc.desc: -d
787 * @tc.type: FUNC
788 */
789 HWTEST_F(SubCommandStatTest, TestOnSubCommand_d1, TestSize.Level1)
790 {
791 StdoutRecord stdoutRecord;
792 stdoutRecord.Start();
793 const auto startTime = chrono::steady_clock::now();
794 EXPECT_EQ(Command::DispatchCommand("stat -a -d 3 --dumpoptions"), true);
795 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
796 chrono::steady_clock::now() - startTime);
797 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
798
799 std::string stringOut = stdoutRecord.Stop();
800 if (HasFailure()) {
801 printf("output:\n%s", stringOut.c_str());
802 }
803 // some times 'sw-page-faults' is 0
804 uint effectiveHeadCounter = 0u;
805 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
806 (defaultConfigNames_.size() - 1));
807 }
808
809 /**
810 * @tc.name: TestOnSubCommand_d2
811 * @tc.desc: -d
812 * @tc.type: FUNC
813 */
814 HWTEST_F(SubCommandStatTest, TestOnSubCommand_d2, TestSize.Level1)
815 {
816 int tid1 = 0;
817 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
818 while (tid1 == 0) {
819 std::this_thread::sleep_for(std::chrono::milliseconds(10));
820 }
821
822 std::string cmdstr = "stat -p ";
823 cmdstr += std::to_string(tid1);
824 cmdstr += " -d -1";
825
826 StdoutRecord stdoutRecord;
827 stdoutRecord.Start();
828 const auto startTime = chrono::steady_clock::now();
829 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
830 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
831 chrono::steady_clock::now() - startTime);
832 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
833
834 std::string stringOut = stdoutRecord.Stop();
835 if (HasFailure()) {
836 printf("output:\n%s", stringOut.c_str());
837 }
838 // some times 'sw-page-faults' is 0
839 std::string expectStr = "failed";
840 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
841 t1.join();
842 }
843
844 /**
845 * @tc.name: TestOnSubCommand_d3
846 * @tc.desc: -d
847 * @tc.type: FUNC
848 */
849 HWTEST_F(SubCommandStatTest, TestOnSubCommand_d3, TestSize.Level1)
850 {
851 int tid1 = 0;
852 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
853 while (tid1 == 0) {
854 std::this_thread::sleep_for(std::chrono::milliseconds(10));
855 }
856
857 std::string cmdstr = "stat -p ";
858 cmdstr += std::to_string(tid1);
859 cmdstr += " -d test";
860
861 StdoutRecord stdoutRecord;
862 stdoutRecord.Start();
863 const auto startTime = chrono::steady_clock::now();
864 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
865 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
866 chrono::steady_clock::now() - startTime);
867 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
868
869 std::string stringOut = stdoutRecord.Stop();
870 if (HasFailure()) {
871 printf("output:\n%s", stringOut.c_str());
872 }
873 // some times 'sw-page-faults' is 0
874 std::string expectStr = "incorrect option";
875 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
876 t1.join();
877 }
878
879 /**
880 * @tc.name: TestOnSubCommand_d4
881 * @tc.desc: -d
882 * @tc.type: FUNC
883 */
884 HWTEST_F(SubCommandStatTest, TestOnSubCommand_d4, TestSize.Level1)
885 {
886 int tid1 = 0;
887 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
888 while (tid1 == 0) {
889 std::this_thread::sleep_for(std::chrono::milliseconds(10));
890 }
891
892 std::string cmdstr = "stat -p ";
893 cmdstr += std::to_string(tid1);
894 cmdstr += " -c 0,1 -d 1";
895
896 StdoutRecord stdoutRecord;
897 stdoutRecord.Start();
898 const auto startTime = chrono::steady_clock::now();
899 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
900 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
901 chrono::steady_clock::now() - startTime);
902 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
903
904 std::string stringOut = stdoutRecord.Stop();
905 if (HasFailure()) {
906 printf("output:\n%s", stringOut.c_str());
907 }
908 // some times 'sw-page-faults' is 0
909 uint effectiveHeadCounter = 0u;
910 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
911 (defaultConfigNames_.size() - 1));
912 t1.join();
913 }
914
915 /**
916 * @tc.name: TestOnSubCommand_i
917 * @tc.desc: -i
918 * @tc.type: FUNC
919 */
920 HWTEST_F(SubCommandStatTest, TestOnSubCommand_i, TestSize.Level1)
921 {
922 int tid1 = 0;
923 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
924 while (tid1 == 0) {
925 std::this_thread::sleep_for(std::chrono::milliseconds(10));
926 }
927
928 std::string cmdstr = "stat -p ";
929 cmdstr += std::to_string(tid1);
930 cmdstr += " -c 0 -d 3 -i 1000 --dumpoptions";
931
932 StdoutRecord stdoutRecord;
933 stdoutRecord.Start();
934 const auto startTime = chrono::steady_clock::now();
935 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
936 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
937 chrono::steady_clock::now() - startTime);
938 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
939
940 std::string stringOut = stdoutRecord.Stop();
941 if (HasFailure()) {
942 printf("output:\n%s", stringOut.c_str());
943 }
944 // some times 'sw-page-faults' is 0
945 uint effectiveHeadCounter = 0u;
946 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
947 (defaultConfigNames_.size() - 1));
948
949 if (stringOut.find("event not support") == std::string::npos) {
950 EXPECT_GE(effectiveHeadCounter, 3u);
951 }
952 t1.join();
953 }
954
955 /**
956 * @tc.name: TestOnSubCommand_i1
957 * @tc.desc: -i
958 * @tc.type: FUNC
959 */
960 HWTEST_F(SubCommandStatTest, TestOnSubCommand_i1, TestSize.Level1)
961 {
962 int tid1 = 0;
963 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
964 while (tid1 == 0) {
965 std::this_thread::sleep_for(std::chrono::milliseconds(10));
966 }
967
968 std::string cmdstr = "stat -p ";
969 cmdstr += std::to_string(tid1);
970 cmdstr += " -c 0 -d 3 -i 500 --dumpoptions";
971
972 StdoutRecord stdoutRecord;
973 stdoutRecord.Start();
974 const auto startTime = chrono::steady_clock::now();
975 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
976 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
977 chrono::steady_clock::now() - startTime);
978 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
979
980 std::string stringOut = stdoutRecord.Stop();
981 if (HasFailure()) {
982 printf("output:\n%s", stringOut.c_str());
983 }
984 // some times 'sw-page-faults' is 0
985 uint effectiveHeadCounter = 0u;
986 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
987 (defaultConfigNames_.size() - 1));
988
989 EXPECT_GE(effectiveHeadCounter, 3u);
990 t1.join();
991 }
992
993 /**
994 * @tc.name: TestOnSubCommand_i2
995 * @tc.desc: -i
996 * @tc.type: FUNC
997 */
998 HWTEST_F(SubCommandStatTest, TestOnSubCommand_i2, TestSize.Level1)
999 {
1000 int tid1 = 0;
1001 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1002 while (tid1 == 0) {
1003 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1004 }
1005
1006 std::string cmdstr = "stat -p ";
1007 cmdstr += std::to_string(tid1);
1008 cmdstr += " -c 0 -d 3 -i -1 --dumpoptions";
1009
1010 StdoutRecord stdoutRecord;
1011 stdoutRecord.Start();
1012 const auto startTime = chrono::steady_clock::now();
1013 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
1014 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1015 chrono::steady_clock::now() - startTime);
1016 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1017
1018 std::string stringOut = stdoutRecord.Stop();
1019 if (HasFailure()) {
1020 printf("output:\n%s", stringOut.c_str());
1021 }
1022 // some times 'sw-page-faults' is 0
1023 std::string expectStr = "failed";
1024 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
1025 t1.join();
1026 }
1027
1028 /**
1029 * @tc.name: TestOnSubCommand_i2
1030 * @tc.desc: -i
1031 * @tc.type: FUNC
1032 */
1033 HWTEST_F(SubCommandStatTest, TestOnSubCommand_i3, TestSize.Level1)
1034 {
1035 int tid1 = 0;
1036 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1037 while (tid1 == 0) {
1038 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1039 }
1040
1041 std::string cmdstr = "stat -p ";
1042 cmdstr += std::to_string(tid1);
1043 cmdstr += " -c 0 -d 3 -i test --dumpoptions";
1044
1045 StdoutRecord stdoutRecord;
1046 stdoutRecord.Start();
1047 const auto startTime = chrono::steady_clock::now();
1048 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
1049 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1050 chrono::steady_clock::now() - startTime);
1051 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1052
1053 std::string stringOut = stdoutRecord.Stop();
1054 if (HasFailure()) {
1055 printf("output:\n%s", stringOut.c_str());
1056 }
1057 // some times 'sw-page-faults' is 0
1058 std::string expectStr = "incorrect";
1059 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
1060 t1.join();
1061 }
1062
1063 /**
1064 * @tc.name: TestOnSubCommand_i4
1065 * @tc.desc: -i
1066 * @tc.type: FUNC
1067 */
1068 HWTEST_F(SubCommandStatTest, TestOnSubCommand_i4, TestSize.Level1)
1069 {
1070 int tid1 = 0;
1071 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1072 while (tid1 == 0) {
1073 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1074 }
1075
1076 std::string cmdstr = "stat -p ";
1077 cmdstr += std::to_string(tid1);
1078 cmdstr += " -c 0 -d 1 -i 100 --dumpoptions";
1079
1080 StdoutRecord stdoutRecord;
1081 stdoutRecord.Start();
1082 const auto startTime = chrono::steady_clock::now();
1083 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1084 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1085 chrono::steady_clock::now() - startTime);
1086 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1087
1088 std::string stringOut = stdoutRecord.Stop();
1089 if (HasFailure()) {
1090 printf("output:\n%s", stringOut.c_str());
1091 }
1092 // some times 'sw-page-faults' is 0
1093 uint effectiveHeadCounter = 0u;
1094 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
1095 (defaultConfigNames_.size() - 1));
1096
1097 EXPECT_GE(effectiveHeadCounter, 3u);
1098 t1.join();
1099 }
1100
1101 /**
1102 * @tc.name: TestOnSubCommand_e
1103 * @tc.desc: -e261
1104 * @tc.type: FUNC
1105 */
1106 HWTEST_F(SubCommandStatTest, TestOnSubCommand_e, TestSize.Level1)
1107 {
1108 int tid1 = 0;
1109 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1110 while (tid1 == 0) {
1111 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1112 }
1113
1114 std::string cmdstr = "stat -p ";
1115 cmdstr += std::to_string(tid1);
1116 cmdstr += " -e hw-instructions -c 0 -d 3 --dumpoptions";
1117
1118 StdoutRecord stdoutRecord;
1119 stdoutRecord.Start();
1120 const auto startTime = chrono::steady_clock::now();
1121 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1122 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1123 chrono::steady_clock::now() - startTime);
1124 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1125
1126 std::string stringOut = stdoutRecord.Stop();
1127 if (HasFailure()) {
1128 printf("output:\n%s", stringOut.c_str());
1129 }
1130 const std::vector<std::string> configNmaes = {"hw-instructions"};
1131 uint effectiveHeadCounter = 0u;
1132 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1133 t1.join();
1134 }
1135
1136 /**
1137 * @tc.name: TestOnSubCommand_e1
1138 * @tc.desc: -e261
1139 * @tc.type: FUNC
1140 */
1141 HWTEST_F(SubCommandStatTest, TestOnSubCommand_e1, TestSize.Level1)
1142 {
1143 int tid1 = 0;
1144 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1145 while (tid1 == 0) {
1146 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1147 }
1148
1149 std::string cmdstr = "stat -p ";
1150 cmdstr += std::to_string(tid1);
1151 cmdstr += " -e hw-branch-misses -c 0 -d 3 --dumpoptions";
1152
1153 StdoutRecord stdoutRecord;
1154 stdoutRecord.Start();
1155 const auto startTime = chrono::steady_clock::now();
1156 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1157 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1158 chrono::steady_clock::now() - startTime);
1159 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1160
1161 std::string stringOut = stdoutRecord.Stop();
1162 if (HasFailure()) {
1163 printf("output:\n%s", stringOut.c_str());
1164 }
1165 const std::vector<std::string> configNmaes = {"hw-branch-misses"};
1166 uint effectiveHeadCounter = 0u;
1167 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1168 t1.join();
1169 }
1170
1171 /**
1172 * @tc.name: TestOnSubCommand_e2
1173 * @tc.desc: -e261
1174 * @tc.type: FUNC
1175 */
1176 HWTEST_F(SubCommandStatTest, TestOnSubCommand_e2, TestSize.Level1)
1177 {
1178 int tid1 = 0;
1179 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1180 while (tid1 == 0) {
1181 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1182 }
1183
1184 std::string cmdstr = "stat -p ";
1185 cmdstr += std::to_string(tid1);
1186 cmdstr += " -e hw-cpu-cycles -c 0 -d 3 --dumpoptions";
1187
1188 StdoutRecord stdoutRecord;
1189 stdoutRecord.Start();
1190 const auto startTime = chrono::steady_clock::now();
1191 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1192 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1193 chrono::steady_clock::now() - startTime);
1194 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1195
1196 std::string stringOut = stdoutRecord.Stop();
1197 if (HasFailure()) {
1198 printf("output:\n%s", stringOut.c_str());
1199 }
1200 const std::vector<std::string> configNmaes = {"hw-cpu-cycles"};
1201 uint effectiveHeadCounter = 0u;
1202 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1203 t1.join();
1204 }
1205
1206 /**
1207 * @tc.name: TestOnSubCommand_e3
1208 * @tc.desc: -e261
1209 * @tc.type: FUNC
1210 */
1211 HWTEST_F(SubCommandStatTest, TestOnSubCommand_e3, TestSize.Level1)
1212 {
1213 int tid1 = 0;
1214 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1215 while (tid1 == 0) {
1216 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1217 }
1218
1219 std::string cmdstr = "stat -p ";
1220 cmdstr += std::to_string(tid1);
1221 cmdstr += " -e hw-instructions -c 0 -d 3 --dumpoptions";
1222
1223 StdoutRecord stdoutRecord;
1224 stdoutRecord.Start();
1225 const auto startTime = chrono::steady_clock::now();
1226 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1227 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1228 chrono::steady_clock::now() - startTime);
1229 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1230
1231 std::string stringOut = stdoutRecord.Stop();
1232 if (HasFailure()) {
1233 printf("output:\n%s", stringOut.c_str());
1234 }
1235 const std::vector<std::string> configNmaes = {"hw-instructions"};
1236 uint effectiveHeadCounter = 0u;
1237 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1238 t1.join();
1239 }
1240
1241 /**
1242 * @tc.name: TestOnSubCommand_e4
1243 * @tc.desc: -e261
1244 * @tc.type: FUNC
1245 */
1246 HWTEST_F(SubCommandStatTest, TestOnSubCommand_e4, TestSize.Level1)
1247 {
1248 int tid1 = 0;
1249 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1250 while (tid1 == 0) {
1251 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1252 }
1253
1254 std::string cmdstr = "stat -p ";
1255 cmdstr += std::to_string(tid1);
1256 cmdstr += " -e hw-branch-test -c 0 -d 3 --dumpoptions";
1257
1258 StdoutRecord stdoutRecord;
1259 stdoutRecord.Start();
1260 const auto startTime = chrono::steady_clock::now();
1261 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
1262 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1263 chrono::steady_clock::now() - startTime);
1264 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1265
1266 std::string stringOut = stdoutRecord.Stop();
1267 if (HasFailure()) {
1268 printf("output:\n%s", stringOut.c_str());
1269 }
1270 std::string expectStr = "event is not supported";
1271 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
1272 t1.join();
1273 }
1274
1275 /**
1276 * @tc.name: TestOnSubCommand_g
1277 * @tc.desc: -g
1278 * @tc.type: FUNC
1279 */
1280 HWTEST_F(SubCommandStatTest, TestOnSubCommand_g, TestSize.Level1)
1281 {
1282 int tid1 = 0;
1283 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1284 while (tid1 == 0) {
1285 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1286 }
1287
1288 std::string cmdstr = "stat -p ";
1289 cmdstr += std::to_string(tid1);
1290 cmdstr += " -g hw-branch-misses"
1291 " -g hw-cpu-cycles,hw-instructions"
1292 " -c 0 -d 3 --dumpoptions";
1293
1294 StdoutRecord stdoutRecord;
1295 stdoutRecord.Start();
1296 const auto startTime = chrono::steady_clock::now();
1297 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1298 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1299 chrono::steady_clock::now() - startTime);
1300 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1301
1302 const std::string stringOut = stdoutRecord.Stop();
1303 if (HasFailure()) {
1304 printf("output:\n%s", stringOut.c_str());
1305 }
1306
1307 const std::vector<std::string> configNmaes = {
1308 "hw-branch-misses",
1309 "hw-cpu-cycles",
1310 "hw-instructions",
1311 };
1312 uint effectiveHeadCounter = 0u;
1313 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1314 t1.join();
1315 }
1316
1317 /**
1318 * @tc.name: TestOnSubCommand_g1
1319 * @tc.desc: -g
1320 * @tc.type: FUNC
1321 */
1322 HWTEST_F(SubCommandStatTest, TestOnSubCommand_g1, TestSize.Level1)
1323 {
1324 int tid1 = 0;
1325 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1326 while (tid1 == 0) {
1327 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1328 }
1329
1330 std::string cmdstr = "stat -p ";
1331 cmdstr += std::to_string(tid1);
1332 cmdstr += " -g hw-instructions,hw-branch-misses"
1333 " -c 0 -d 3 --dumpoptions";
1334
1335 StdoutRecord stdoutRecord;
1336 stdoutRecord.Start();
1337 const auto startTime = chrono::steady_clock::now();
1338 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1339 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1340 chrono::steady_clock::now() - startTime);
1341 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1342
1343 const std::string stringOut = stdoutRecord.Stop();
1344 if (HasFailure()) {
1345 printf("output:\n%s", stringOut.c_str());
1346 }
1347
1348 const std::vector<std::string> configNmaes = {
1349 "hw-instructions",
1350 "hw-branch-misses",
1351 };
1352 uint effectiveHeadCounter = 0u;
1353 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1354 t1.join();
1355 }
1356
1357 /**
1358 * @tc.name: TestOnSubCommand_g2
1359 * @tc.desc: -g
1360 * @tc.type: FUNC
1361 */
1362 HWTEST_F(SubCommandStatTest, TestOnSubCommand_g2, TestSize.Level1)
1363 {
1364 int tid1 = 0;
1365 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1366 while (tid1 == 0) {
1367 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1368 }
1369
1370 std::string cmdstr = "stat -p ";
1371 cmdstr += std::to_string(tid1);
1372 cmdstr += " -g hw-cpu-cycles,hw-instructions"
1373 " -c 0 -d 3 --dumpoptions";
1374
1375 StdoutRecord stdoutRecord;
1376 stdoutRecord.Start();
1377 const auto startTime = chrono::steady_clock::now();
1378 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1379 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1380 chrono::steady_clock::now() - startTime);
1381 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1382
1383 const std::string stringOut = stdoutRecord.Stop();
1384 if (HasFailure()) {
1385 printf("output:\n%s", stringOut.c_str());
1386 }
1387
1388 const std::vector<std::string> configNmaes = {
1389 "hw-cpu-cycles",
1390 "hw-instructions",
1391 };
1392 uint effectiveHeadCounter = 0u;
1393 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1394 t1.join();
1395 }
1396
1397 /**
1398 * @tc.name: TestOnSubCommand_g3
1399 * @tc.desc: -g
1400 * @tc.type: FUNC
1401 */
1402 HWTEST_F(SubCommandStatTest, TestOnSubCommand_g3, TestSize.Level1)
1403 {
1404 int tid1 = 0;
1405 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1406 while (tid1 == 0) {
1407 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1408 }
1409
1410 std::string cmdstr = "stat -p ";
1411 cmdstr += std::to_string(tid1);
1412 cmdstr += " -g hw-cpu-test,hw-instructions"
1413 " -c 0 -d 3 --dumpoptions";
1414
1415 StdoutRecord stdoutRecord;
1416 stdoutRecord.Start();
1417 const auto startTime = chrono::steady_clock::now();
1418 EXPECT_EQ(Command::DispatchCommand(cmdstr), false);
1419 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1420 chrono::steady_clock::now() - startTime);
1421 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1422
1423 const std::string stringOut = stdoutRecord.Stop();
1424 if (HasFailure()) {
1425 printf("output:\n%s", stringOut.c_str());
1426 }
1427
1428 std::string expectStr = "event is not supported";
1429 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
1430 t1.join();
1431 }
1432
1433 /**
1434 * @tc.name: TestOnSubCommand_g_uk
1435 * @tc.desc: -g u:k
1436 * @tc.type: FUNC
1437 */
1438 HWTEST_F(SubCommandStatTest, TestOnSubCommand_g_uk, TestSize.Level1)
1439 {
1440 int tid1 = 0;
1441 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1442 while (tid1 == 0) {
1443 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1444 }
1445
1446 std::string cmdstr = "stat -p ";
1447 cmdstr += std::to_string(tid1);
1448 cmdstr += " -g hw-branch-misses:k"
1449 " -g hw-cpu-cycles:k,hw-instructions:k"
1450 " -c 0 -d 3 --dumpoptions";
1451
1452 StdoutRecord stdoutRecord;
1453 stdoutRecord.Start();
1454 const auto startTime = chrono::steady_clock::now();
1455 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1456 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1457 chrono::steady_clock::now() - startTime);
1458 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1459
1460 std::string stringOut = stdoutRecord.Stop();
1461 if (HasFailure()) {
1462 printf("output:\n%s", stringOut.c_str());
1463 }
1464 const std::vector<std::string> configNmaes = {
1465 "hw-branch-misses:k",
1466 "hw-cpu-cycles:k",
1467 "hw-instructions:k",
1468 };
1469 // some times 'sw-page-faults' is 0
1470 uint effectiveHeadCounter = 0u;
1471 EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size());
1472 CheckGroupCoverage(stringOut, "hw-branch-misses:k");
1473 CheckGroupCoverage(stringOut, "hw-cpu-cycles:k,hw-instructions:k");
1474 t1.join();
1475 }
1476
1477 /**
1478 * @tc.name: TestOnSubCommand_p_t
1479 * @tc.desc: -p -t
1480 * @tc.type: FUNC
1481 */
1482 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p_t, TestSize.Level1)
1483 {
1484 int tid1 = 0;
1485 int tid2 = 0;
1486 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1487 std::thread t2(SubCommandStatTest::TestCodeThread, std::ref(tid2));
1488
1489 printf("wait child thread run.\n");
1490 while (tid1 * tid2 == 0) {
1491 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1492 }
1493
1494 StdoutRecord stdoutRecord;
1495 stdoutRecord.Start();
1496 const auto startTime = chrono::steady_clock::now();
1497
1498 std::string tidString = " -t ";
1499 tidString += std::to_string(tid1) + ",";
1500 tidString += std::to_string(tid2);
1501
1502 std::string cmdString = "stat";
1503 cmdString += tidString;
1504 cmdString += " -c 0 -d 3 --dumpoptions";
1505
1506 EXPECT_EQ(Command::DispatchCommand(cmdString), true);
1507 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1508 chrono::steady_clock::now() - startTime);
1509 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1510
1511 std::string stringOut = stdoutRecord.Stop();
1512 if (HasFailure()) {
1513 printf("output:\n%s", stringOut.c_str());
1514 }
1515 // some times 'sw-page-faults' is 0
1516 uint effectiveHeadCounter = 0u;
1517 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
1518 (defaultConfigNames_.size() - 1));
1519
1520 t1.join();
1521 t2.join();
1522 }
1523
1524 /**
1525 * @tc.name: TestOnSubCommand_p_t1
1526 * @tc.desc: -p -t
1527 * @tc.type: FUNC
1528 */
1529 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p_t1, TestSize.Level1)
1530 {
1531 int tid1 = 0;
1532 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1533 while (tid1 == 0) {
1534 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1535 }
1536
1537 StdoutRecord stdoutRecord;
1538 stdoutRecord.Start();
1539 const auto startTime = chrono::steady_clock::now();
1540
1541 std::string tidString = " -t ";
1542 tidString += std::to_string(tid1);
1543
1544 std::string cmdString = "stat";
1545 cmdString += tidString;
1546 cmdString += " -c 0 -d 3 --dumpoptions";
1547
1548 EXPECT_EQ(Command::DispatchCommand(cmdString), true);
1549 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1550 chrono::steady_clock::now() - startTime);
1551 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1552
1553 std::string stringOut = stdoutRecord.Stop();
1554 if (HasFailure()) {
1555 printf("output:\n%s", stringOut.c_str());
1556 }
1557 // some times 'sw-page-faults' is 0
1558 uint effectiveHeadCounter = 0u;
1559 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
1560 (defaultConfigNames_.size() - 1));
1561 t1.join();
1562 }
1563
1564 /**
1565 * @tc.name: TestOnSubCommand_p_t2
1566 * @tc.desc: -p -t
1567 * @tc.type: FUNC
1568 */
1569 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p_t2, TestSize.Level1)
1570 {
1571 StdoutRecord stdoutRecord;
1572 stdoutRecord.Start();
1573 const auto startTime = chrono::steady_clock::now();
1574
1575 std::string tidString = " -t ";
1576 tidString += "-1";
1577
1578 std::string cmdString = "stat";
1579 cmdString += tidString;
1580 cmdString += " -c 0 -d 3 --dumpoptions";
1581
1582 EXPECT_EQ(Command::DispatchCommand(cmdString), false);
1583 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1584 chrono::steady_clock::now() - startTime);
1585 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1586
1587 std::string stringOut = stdoutRecord.Stop();
1588 if (HasFailure()) {
1589 printf("output:\n%s", stringOut.c_str());
1590 }
1591
1592 std::string expectStr = "failed";
1593 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
1594 }
1595
1596 /**
1597 * @tc.name: TestOnSubCommand_p_t3
1598 * @tc.desc: -p -t
1599 * @tc.type: FUNC
1600 */
1601 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p_t3, TestSize.Level1)
1602 {
1603 StdoutRecord stdoutRecord;
1604 stdoutRecord.Start();
1605 const auto startTime = chrono::steady_clock::now();
1606
1607 std::string tidString = " -t ";
1608 tidString += "test";
1609
1610 std::string cmdString = "stat";
1611 cmdString += tidString;
1612 cmdString += " -c 0 -d 3 --dumpoptions";
1613
1614 EXPECT_EQ(Command::DispatchCommand(cmdString), false);
1615 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1616 chrono::steady_clock::now() - startTime);
1617 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1618
1619 std::string stringOut = stdoutRecord.Stop();
1620 if (HasFailure()) {
1621 printf("output:\n%s", stringOut.c_str());
1622 }
1623
1624 std::string expectStr = "incorrect";
1625 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
1626 }
1627
1628 /**
1629 * @tc.name: TestOnSubCommand_p_t4
1630 * @tc.desc: -p -t
1631 * @tc.type: FUNC
1632 */
1633 HWTEST_F(SubCommandStatTest, TestOnSubCommand_p_t4, TestSize.Level1)
1634 {
1635 int tid1 = 0;
1636 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1637
1638 printf("wait child thread run.\n");
1639 while (tid1 == 0) {
1640 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1641 }
1642
1643 StdoutRecord stdoutRecord;
1644 stdoutRecord.Start();
1645 const auto startTime = chrono::steady_clock::now();
1646
1647 std::string tidString = " -t ";
1648 tidString += std::to_string(tid1);
1649
1650 std::string cmdString = "stat";
1651 cmdString += tidString;
1652 cmdString += " -c 0 -d 3 --dumpoptions";
1653
1654 EXPECT_EQ(Command::DispatchCommand(cmdString), true);
1655 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1656 chrono::steady_clock::now() - startTime);
1657 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1658
1659 std::string stringOut = stdoutRecord.Stop();
1660 if (HasFailure()) {
1661 printf("output:\n%s", stringOut.c_str());
1662 }
1663 // some times 'sw-page-faults' is 0
1664 uint effectiveHeadCounter = 0u;
1665 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
1666 (defaultConfigNames_.size() - 1));
1667 t1.join();
1668 }
1669
1670 /**
1671 * @tc.name: TestOnSubCommand_verbose
1672 * @tc.desc: -p -t
1673 * @tc.type: FUNC
1674 */
1675 HWTEST_F(SubCommandStatTest, TestOnSubCommand_verbose, TestSize.Level1)
1676 {
1677 int tid1 = 0;
1678 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1679
1680 printf("wait child thread run.\n");
1681 while (tid1 == 0) {
1682 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1683 }
1684
1685 StdoutRecord stdoutRecord;
1686 stdoutRecord.Start();
1687 const auto startTime = chrono::steady_clock::now();
1688
1689 std::string tidString = " -t ";
1690 tidString += std::to_string(tid1);
1691
1692 std::string cmdString = "stat";
1693 cmdString += tidString;
1694 cmdString += " -c 0 -d 3 --verbose";
1695
1696 EXPECT_EQ(Command::DispatchCommand(cmdString), true);
1697 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1698 chrono::steady_clock::now() - startTime);
1699 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1700
1701 std::string stringOut = stdoutRecord.Stop();
1702 if (HasFailure()) {
1703 printf("output:\n%s", stringOut.c_str());
1704 }
1705
1706 std::string expectStr = "time_enabled:";
1707 EXPECT_EQ(FindExpectStr(stringOut, expectStr), true);
1708 t1.join();
1709 }
1710
1711 /**
1712 * @tc.name: TestOnSubCommand_verbose1
1713 * @tc.desc: -p -t
1714 * @tc.type: FUNC
1715 */
1716 HWTEST_F(SubCommandStatTest, TestOnSubCommand_verbose1, TestSize.Level1)
1717 {
1718 int tid1 = 0;
1719 std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1));
1720
1721 printf("wait child thread run.\n");
1722 while (tid1 == 0) {
1723 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1724 }
1725
1726 StdoutRecord stdoutRecord;
1727 stdoutRecord.Start();
1728 const auto startTime = chrono::steady_clock::now();
1729
1730 std::string tidString = " -t ";
1731 tidString += std::to_string(tid1);
1732
1733 std::string cmdString = "stat";
1734 cmdString += tidString;
1735 cmdString += " -c 0 -d 3";
1736
1737 EXPECT_EQ(Command::DispatchCommand(cmdString), true);
1738 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1739 chrono::steady_clock::now() - startTime);
1740 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1741
1742 std::string stringOut = stdoutRecord.Stop();
1743 if (HasFailure()) {
1744 printf("output:\n%s", stringOut.c_str());
1745 }
1746
1747 std::string expectStr = "time_enabled:";
1748 EXPECT_EQ(FindExpectStr(stringOut, expectStr), false);
1749 t1.join();
1750 }
1751
1752 /**
1753 * @tc.name: TestOnSubCommand_cmd
1754 * @tc.desc: hiperf stat <cmd>
1755 * @tc.type: FUNC
1756 */
1757 HWTEST_F(SubCommandStatTest, TestOnSubCommand_cmd, TestSize.Level1)
1758 {
1759 std::string cmdstr = "stat -c 0 -d 3 --dumpoptions ls -l";
1760
1761 StdoutRecord stdoutRecord;
1762 stdoutRecord.Start();
1763 const auto startTime = chrono::steady_clock::now();
1764 EXPECT_EQ(Command::DispatchCommand(cmdstr), true);
1765 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
1766 chrono::steady_clock::now() - startTime);
1767 EXPECT_LE(costMs.count(), defaultRunTimeoutMs);
1768
1769 std::string stringOut = stdoutRecord.Stop();
1770 if (HasFailure()) {
1771 printf("output:\n%s", stringOut.c_str());
1772 }
1773 // some times 'sw-page-faults' is 0
1774 uint effectiveHeadCounter = 0u;
1775 EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter),
1776 (defaultConfigNames_.size() - 1));
1777 }
1778
1779 /**
1780 * @tc.name: TestOnSubCommand_ni
1781 * @tc.desc: --no-inherit
1782 * @tc.type: FUNC
1783 */
1784 HWTEST_F(SubCommandStatTest, TestOnSubCommand_ni, TestSize.Level1)
1785 {
1786 StdoutRecord stdoutRecord;
1787 const std::string configName {"hw-cpu-cycles"};
1788
1789 stdoutRecord.Start();
1790 std::string testCMD = "stat --no-inherit -p 2 -c 0 -d 3 --dumpoptions -e ";
1791 testCMD += configName;
1792 const auto tick2 = std::chrono::steady_clock::now();
1793 EXPECT_EQ(Command::DispatchCommand(testCMD), true);
1794 const auto tock2 = std::chrono::steady_clock::now();
1795 const auto costMs2 = std::chrono::duration_cast<std::chrono::milliseconds>(tock2 - tick2);
1796 EXPECT_LE(costMs2.count(), defaultRunTimeoutMs);
1797 std::string stringOut = stdoutRecord.Stop();
1798 if (HasFailure()) {
1799 printf("output:\n%s", stringOut.c_str());
1800 }
1801 int counterValueWithoutInherit = CounterValue(stringOut, configName);
1802 EXPECT_NE(counterValueWithoutInherit, 0);
1803 HLOGD("%s %d", configName.c_str(), counterValueWithoutInherit);
1804 }
1805
1806 // ParseOption DumpOptions PrintUsage
1807 /**
1808 * @tc.name: TestParseOption_ni
1809 * @tc.desc: --no-inherit
1810 * @tc.type: FUNC
1811 */
1812 HWTEST_F(SubCommandStatTest, TestParseOption, TestSize.Level1)
1813 {
1814 SubCommandStat cmdStat;
1815 std::vector<std::string> args;
1816 args = {"-h"};
1817 EXPECT_EQ(cmdStat.ParseOption(args), true);
1818 args = {"-a"};
1819 EXPECT_EQ(cmdStat.ParseOption(args), true);
1820 args = {"-c"};
1821 EXPECT_EQ(cmdStat.ParseOption(args), false);
1822 args = {"-d"};
1823 EXPECT_EQ(cmdStat.ParseOption(args), false);
1824 args = {"-i"};
1825 EXPECT_EQ(cmdStat.ParseOption(args), false);
1826 args = {"-e"};
1827 EXPECT_EQ(cmdStat.ParseOption(args), false);
1828 args = {"-g"};
1829 EXPECT_EQ(cmdStat.ParseOption(args), false);
1830 args = {"--no-inherit"};
1831 EXPECT_EQ(cmdStat.ParseOption(args), true);
1832 args = {"-p"};
1833 EXPECT_EQ(cmdStat.ParseOption(args), false);
1834 args = {"-t"};
1835 EXPECT_EQ(cmdStat.ParseOption(args), false);
1836 args = {"--verbose"};
1837 EXPECT_EQ(cmdStat.ParseOption(args), true);
1838 args.clear();
1839 EXPECT_EQ(cmdStat.ParseOption(args), true);
1840 }
1841
1842 /**
1843 * @tc.name: TestDumpOptions
1844 * @tc.desc:
1845 * @tc.type: FUNC
1846 */
1847 HWTEST_F(SubCommandStatTest, TestDumpOptions, TestSize.Level1)
1848 {
1849 StdoutRecord stdoutRecord;
1850 stdoutRecord.Start();
1851 SubCommandStat cmdStat;
1852 cmdStat.DumpOptions();
1853 std::string stringOut = stdoutRecord.Stop();
1854 EXPECT_TRUE(stringOut.find("10000.000000 sec") != std::string::npos);
1855 }
1856
1857 /**
1858 * @tc.name: TestPrintUsage
1859 * @tc.desc:
1860 * @tc.type: FUNC
1861 */
1862 HWTEST_F(SubCommandStatTest, TestPrintUsage, TestSize.Level1)
1863 {
1864 StdoutRecord stdoutRecord;
1865 stdoutRecord.Start();
1866 SubCommandStat cmdStat;
1867 cmdStat.PrintUsage();
1868 std::string stringOut = stdoutRecord.Stop();
1869 EXPECT_TRUE(stringOut.find("Usage: hiperf stat [options] [command [command-args]]") !=
1870 std::string::npos);
1871 }
1872
1873 /**
1874 * @tc.name: TestCheckOptions
1875 * @tc.desc:
1876 * @tc.type: FUNC
1877 */
1878 HWTEST_F(SubCommandStatTest, TestCheckOptions, TestSize.Level1)
1879 {
1880 SubCommandStat cmdStat;
1881 std::vector<pid_t> pids;
1882
1883 cmdStat.timeStopSec_ = -1;
1884 EXPECT_EQ(cmdStat.CheckOptions(pids), false);
1885
1886 cmdStat.timeReportMs_ = -1;
1887 EXPECT_EQ(cmdStat.CheckOptions(pids), false);
1888
1889 cmdStat.targetSystemWide_ = true;
1890 pids = {1112, 1113};
1891 EXPECT_EQ(cmdStat.CheckOptions(pids), false);
1892
1893 cmdStat.trackedCommand_ = {"test"};
1894 EXPECT_EQ(cmdStat.CheckOptions(pids), false);
1895
1896 cmdStat.targetSystemWide_ = false;
1897 EXPECT_EQ(cmdStat.CheckOptions(pids), false);
1898 }
1899
1900 /**
1901 * @tc.name: TestReport
1902 * @tc.desc:
1903 * @tc.type: FUNC
1904 */
1905 HWTEST_F(SubCommandStatTest, TestReport, TestSize.Level1)
1906 {
1907 StdoutRecord stdoutRecord;
1908 stdoutRecord.Start();
1909 SubCommandStat cmdStat;
1910 std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> countEvents;
1911 std::unique_ptr<PerfEvents::CountEvent> testEvent(std::make_unique<PerfEvents::CountEvent>());
1912 std::string test = "test";
1913 countEvents[test] = std::move(testEvent);
1914 cmdStat.Report(countEvents);
1915 std::string stringOut = stdoutRecord.Stop();
1916 EXPECT_TRUE(stringOut.find("test") != std::string::npos);
1917 EXPECT_TRUE(stringOut.find("count name") != std::string::npos);
1918 }
1919
1920 /**
1921 * @tc.name: TestReport_Piling
1922 * @tc.desc:
1923 * @tc.type: FUNC
1924 */
1925 HWTEST_F(SubCommandStatTest, TestReport_Piling, TestSize.Level1)
1926 {
1927 SubCommandStat cmdStat;
1928 std::vector<std::string> eventNames = {
1929 "hw-branch-instructions", "hw-branch-misses", "hw-cpu-cycles", "hw-instructions",
1930 "sw-context-switches", "sw-page-faults", "sw-task-clock", "sw-cpu-migrations"};
1931 StdoutRecord stdoutRecord;
1932 stdoutRecord.Start();
1933 std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> countEvents;
1934 for (int i = 0; i < 8; i++) {
1935 auto countEvent = make_unique<PerfEvents::CountEvent>(PerfEvents::CountEvent {});
1936 std::string configName = eventNames[i];
1937 countEvents[configName] = std::move(countEvent);
1938 countEvents[configName]->userOnly = false;
1939 countEvents[configName]->kernelOnly = false;
1940 std::unique_ptr<PerfEvents::CountEvent> &countEventTmp = countEvents[configName];
1941 if (i == 0) {
1942 countEventTmp->eventCount = 20283000 * 10;
1943 } else if (i == 4) {
1944 countEventTmp->eventCount = 2028300;
1945 } else if (i == 5) {
1946 countEventTmp->eventCount = 2000;
1947 } else if (i == 7) {
1948 countEventTmp->eventCount = 20;
1949 } else {
1950 countEventTmp->eventCount = 20283000;
1951 }
1952 countEventTmp->time_enabled = 2830280;
1953 countEventTmp->time_running = 2278140;
1954 countEventTmp->id = 0;
1955 countEventTmp->used_cpus = countEventTmp->eventCount / 1e9;
1956 }
1957 cmdStat.Report(countEvents);
1958 std::string stringOut = stdoutRecord.Stop();
1959 printf("output: %s\n", stringOut.c_str());
1960 EXPECT_EQ(FindExpectStr(stringOut, "G/sec"), true);
1961 EXPECT_EQ(FindExpectStr(stringOut, "M/sec"), true);
1962 EXPECT_EQ(FindExpectStr(stringOut, "K/sec"), true);
1963 EXPECT_EQ(FindExpectStr(stringOut, "/sec"), true);
1964 }
1965
1966 /**
1967 * @tc.name: CheckOptionPidAndApp
1968 * @tc.desc: Test handle other config
1969 * @tc.type: FUNC
1970 */
1971 HWTEST_F(SubCommandStatTest, CheckOptionPidAndApp, TestSize.Level1)
1972 {
1973 SubCommandStat stat;
1974 std::vector<pid_t> pids;
1975 EXPECT_EQ(stat.CheckOptionPidAndApp(pids), true);
1976 pids.push_back(1);
1977 pids.push_back(2); // 2: pid
1978 EXPECT_EQ(stat.CheckOptionPidAndApp(pids), true);
1979 pids.push_back(700011); // 700011: invalid pid
1980 EXPECT_EQ(stat.CheckOptionPidAndApp(pids), false);
1981 }
1982 } // namespace HiPerf
1983 } // namespace Developtools
1984 } // namespace OHOS
1985