• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "perf_events_test.h"
17 
18 #include <chrono>
19 #include <cinttypes>
20 #include <cstdlib>
21 #include <thread>
22 #include <unistd.h>
23 
24 #include "debug_logger.h"
25 #include "utilities.h"
26 
27 using namespace testing::ext;
28 using namespace std;
29 
30 namespace OHOS {
31 namespace Developtools {
32 namespace HiPerf {
33 static constexpr uint64_t NANO_SECONDS_PER_SECOND = 1000000000;
34 static constexpr uint64_t TEN_THOUSAND = 10000u;
35 static constexpr uint64_t PAGE_SIZE = 1024u;
36 static constexpr uint64_t SEVENTEEN = 17u;
37 
38 class PerfEventsTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41     static void TearDownTestCase(void);
42     void SetUp();
43     void TearDown();
44 
45     static void TestCodeThread(void);
46     static void RunTestThreads(std::vector<std::thread> &threads);
47     static void SetAllConfig(PerfEvents &event);
48     static bool RecordCount(PerfEventRecord& record);
49     static void StatCount(
50         const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents);
51 
52     static constexpr int TEST_CODE_MEM_FILE_SIZE = 1024;
53     static constexpr auto TEST_CODE_SLEEP_BEFORE_RUN = 500ms;
54     static constexpr auto TEST_CODE_SLEEP_AFTER_RUN = 1000ms;
55     static constexpr int TEST_CODE_RUN_TIME = 10240;
56     static constexpr int DOUBLE = 2;
57     static constexpr int TRIPLE = 3;
58     static constexpr auto TEST_TIME = 3s;
59     static constexpr auto DEFAULT_TRACKING_TIME = 1000;
60     static constexpr auto DEFAULT_STAT_REPORT_TIME = 500;
61     static constexpr auto DEFAULT_SAMPLE_MMAPAGE = 256;
62 
63     static uint64_t gRecordCount;
64     static uint64_t gStatCount;
65 };
66 
SetUpTestCase()67 void PerfEventsTest::SetUpTestCase() {}
68 
TearDownTestCase()69 void PerfEventsTest::TearDownTestCase() {}
70 
SetUp()71 void PerfEventsTest::SetUp() {}
72 
TearDown()73 void PerfEventsTest::TearDown() {}
74 
75 uint64_t PerfEventsTest::gRecordCount = 0;
76 uint64_t PerfEventsTest::gStatCount = 0;
77 
RecordCount(PerfEventRecord & record)78 bool PerfEventsTest::RecordCount(PerfEventRecord& record)
79 {
80     gRecordCount++;
81     return true;
82 }
83 
StatCount(const std::map<std::string,std::unique_ptr<PerfEvents::CountEvent>> & countEvents)84 void PerfEventsTest::StatCount(
85     const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents)
86 {
87     gStatCount++;
88 }
89 
TestCodeThread()90 void PerfEventsTest::TestCodeThread()
91 {
92     std::vector<std::unique_ptr<char[]>> mems;
93     int tid = gettid();
94     printf("%s:%d ++\n", __FUNCTION__, tid);
95     for (int n = 0; n < TRIPLE; n++) {
96         std::this_thread::sleep_for(TEST_CODE_SLEEP_BEFORE_RUN);
97         constexpr size_t memSize {TEST_CODE_MEM_FILE_SIZE};
98         for (int i = 0; i < TEST_CODE_RUN_TIME; i++) {
99             if (i % DOUBLE == 0) {
100                 mems.push_back(std::make_unique<char[]>(memSize));
101             } else {
102                 mems.push_back(std::make_unique<char[]>(memSize * DOUBLE));
103             }
104         }
105 
106         for (int i = 0; i < TEST_CODE_RUN_TIME; i++) {
107             mems.pop_back();
108         }
109 
110         std::this_thread::sleep_for(TEST_CODE_SLEEP_AFTER_RUN);
111     }
112     printf("%s:%d --\n", __FUNCTION__, tid);
113 }
114 
RunTestThreads(std::vector<std::thread> & threads)115 void PerfEventsTest::RunTestThreads(std::vector<std::thread> &threads)
116 {
117     for (long i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) {
118         threads.emplace_back(std::thread(&TestCodeThread));
119     }
120 }
121 
122 // it isn't include sample and stat
SetAllConfig(PerfEvents & event)123 void PerfEventsTest::SetAllConfig(PerfEvents &event)
124 {
125     std::vector<pid_t> selectCpus_;
126     event.SetCpu(selectCpus_);
127     std::vector<pid_t> pids;
128     event.SetPid(pids);
129     event.SetSystemTarget(true);
130     event.SetTimeOut(DEFAULT_TRACKING_TIME);
131     event.SetInherit(false);
132     std::vector<std::string> trackedCommand_ {"ls"};
133     event.SetTrackedCommand(trackedCommand_);
134     const unsigned int frequency = 1000;
135     event.SetSampleFrequency(frequency);
136     const uint32_t dwarfSampleStackSize = 64;
137     event.SetDwarfSampleStackSize(dwarfSampleStackSize);
138     const int clockId = 1;
139     event.SetClockId(clockId);
140 
141     // addevent must be tail
142     event.AddDefaultEvent(PERF_TYPE_HARDWARE);
143     event.AddDefaultEvent(PERF_TYPE_SOFTWARE);
144 }
145 
RunTrack(PerfEvents & event)146 static void RunTrack(PerfEvents &event)
147 {
148     ASSERT_EQ(event.StartTracking(), true);
149 }
150 
151 /**
152  * @tc.name: Test
153  * @tc.desc:
154  * @tc.type: FUNC
155  */
156 HWTEST_F(PerfEventsTest, GetSupportEvents, TestSize.Level1)
157 {
158     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
159     StdoutRecord stdoutRecord;
160     stdoutRecord.Start();
161 
162     PerfEvents event;
163     perf_type_id id = PERF_TYPE_HARDWARE;
164     int index = 0;
165     bool value[] = {false, false, false, false, false, true};
166     while (id < PERF_TYPE_MAX) {
167         std::map<__u64, std::string> supportEvent = event.GetSupportEvents(id);
168         ASSERT_EQ(supportEvent.empty(), value[index++]);
169         for (auto it = supportEvent.begin(); it != supportEvent.end(); ++it) {
170             printf("[%lld]\t%s\n", it->first, it->second.c_str());
171         }
172         id = perf_type_id(id + 1);
173     }
174 
175     std::string stringOut = stdoutRecord.Stop();
176 }
177 
178 HWTEST_F(PerfEventsTest, GetTypeName, TestSize.Level1)
179 {
180     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
181     StdoutRecord stdoutRecord;
182     stdoutRecord.Start();
183 
184     PerfEvents event;
185     perf_type_id id = PERF_TYPE_HARDWARE;
186     while (id < PERF_TYPE_MAX) {
187         std::string typeName = event.GetTypeName(id);
188         EXPECT_GT(typeName.size(), 0u) << "the type should have name";
189         printf("type[%d]\tname : %s\n", id, typeName.c_str());
190         id = perf_type_id(id + 1);
191     }
192 
193     std::string stringOut = stdoutRecord.Stop();
194 }
195 
196 HWTEST_F(PerfEventsTest, RecordNormal, TestSize.Level1)
197 {
198     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
199     StdoutRecord stdoutRecord;
200     stdoutRecord.Start();
201 
202     PerfEvents event;
203     // prepare
204     gRecordCount = 0;
205     event.SetMmapPages(DEFAULT_SAMPLE_MMAPAGE);
206     event.SetRecordCallBack(RecordCount);
207 
208     std::vector<pid_t> selectCpus_;
209     event.SetCpu(selectCpus_);
210     std::vector<pid_t> pids;
211     event.SetPid(pids);
212     const unsigned int frequency = 1000;
213     event.SetSampleFrequency(frequency);
214     event.SetSystemTarget(true);
215     event.SetTimeOut(DEFAULT_TRACKING_TIME);
216     event.SetInherit(false);
217     std::vector<std::string> trackedCommand_ {"ls"};
218     event.SetTrackedCommand(trackedCommand_);
219     event.AddDefaultEvent(PERF_TYPE_SOFTWARE);
220     event.AddDefaultEvent(PERF_TYPE_HARDWARE);
221 
222     ASSERT_EQ(event.PrepareTracking(), true);
223     std::thread runThread(RunTrack, std::ref(event));
224     std::vector<std::thread> testThreads;
225     RunTestThreads(testThreads);
226 
227     std::this_thread::sleep_for(TEST_TIME);
228     EXPECT_EQ(event.PauseTracking(), true);
229     std::this_thread::sleep_for(TEST_TIME); // wait for clearing mmap buffer
230     uint64_t recordCount = gRecordCount;
231     std::this_thread::sleep_for(TEST_TIME);
232     EXPECT_EQ(recordCount, gRecordCount) << "now should have no record";
233     EXPECT_EQ(event.ResumeTracking(), true);
234     TestCodeThread();
235     std::this_thread::sleep_for(TEST_TIME);
236     EXPECT_EQ(event.StopTracking(), true);
237     runThread.join();
238     for (std::thread &t : testThreads) {
239         t.join();
240     }
241     ASSERT_GT(gRecordCount, recordCount) << "should have more records";
242 
243     size_t lostSamples = 0;
244     size_t lostNonSamples = 0;
245     event.GetLostSamples(lostSamples, lostNonSamples);
246 
247     std::string stringOut = stdoutRecord.Stop();
248 }
249 
250 HWTEST_F(PerfEventsTest, RecordSetAll, TestSize.Level1)
251 {
252     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
253     StdoutRecord stdoutRecord;
254     stdoutRecord.Start();
255 
256     PerfEvents event;
257     // prepare
258     gRecordCount = 0;
259     event.SetMmapPages(DEFAULT_SAMPLE_MMAPAGE);
260     event.SetRecordCallBack(RecordCount);
261     SetAllConfig(event);
262     ASSERT_EQ(event.PrepareTracking(), true);
263     std::thread runThread(RunTrack, std::ref(event));
264     std::vector<std::thread> testThreads;
265     RunTestThreads(testThreads);
266 
267     std::this_thread::sleep_for(TEST_TIME);
268     EXPECT_EQ(event.PauseTracking(), true);
269     std::this_thread::sleep_for(TEST_TIME); // wait for clearing mmap buffer
270     uint64_t recordCount = gRecordCount;
271     std::this_thread::sleep_for(TEST_TIME);
272     EXPECT_EQ(recordCount, gRecordCount) << "now should have no record";
273     EXPECT_EQ(event.ResumeTracking(), true);
274     TestCodeThread();
275     std::this_thread::sleep_for(TEST_TIME);
276     EXPECT_EQ(event.StopTracking(), true);
277     runThread.join();
278     for (std::thread &t : testThreads) {
279         t.join();
280     }
281     ASSERT_GT(gRecordCount, recordCount) << "should have more records";
282 
283     std::string stringOut = stdoutRecord.Stop();
284 }
285 
286 HWTEST_F(PerfEventsTest, StatNormal, TestSize.Level1)
287 {
288     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
289     StdoutRecord stdoutRecord;
290     stdoutRecord.Start();
291 
292     PerfEvents event;
293     // prepare
294     gStatCount = 0;
295     std::vector<pid_t> selectCpus_;
296     event.SetCpu(selectCpus_);
297     std::vector<pid_t> pids;
298     event.SetPid(pids);
299     event.SetSystemTarget(true);
300     event.SetTimeOut(DEFAULT_TRACKING_TIME);
301     event.SetTimeReport(DEFAULT_STAT_REPORT_TIME);
302     event.SetVerboseReport(false);
303     event.SetInherit(false);
304     std::vector<std::string> trackedCommand_ {"ls"};
305     event.SetTrackedCommand(trackedCommand_);
306     event.AddDefaultEvent(PERF_TYPE_SOFTWARE);
307     event.AddDefaultEvent(PERF_TYPE_TRACEPOINT);
308     event.SetStatCallBack(StatCount);
309     ASSERT_EQ(event.PrepareTracking(), true);
310     std::thread runThread(RunTrack, std::ref(event));
311     std::vector<std::thread> testThreads;
312     RunTestThreads(testThreads);
313 
314     std::this_thread::sleep_for(TEST_TIME);
315     EXPECT_EQ(event.PauseTracking(), true);
316     EXPECT_GT(gStatCount, 0u) << "should have stats";
317     uint64_t statCount = gStatCount;
318     std::this_thread::sleep_for(TEST_TIME);
319     EXPECT_EQ(event.ResumeTracking(), true);
320     std::this_thread::sleep_for(TEST_TIME);
321     EXPECT_EQ(event.StopTracking(), true);
322     runThread.join();
323     for (std::thread &t : testThreads) {
324         t.join();
325     }
326     EXPECT_GT(gStatCount, statCount) << "should have more stats";
327 
328     std::string stringOut = stdoutRecord.Stop();
329 }
330 
331 HWTEST_F(PerfEventsTest, CreateUpdateTimeThread2, TestSize.Level1)
332 {
333     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
334     StdoutRecord stdoutRecord;
335     stdoutRecord.Start();
336 
337     PerfEvents event;
338     event.backtrack_ = true;
339     event.eventGroupItem_.emplace_back();
340     event.eventGroupItem_[0].eventItems.emplace_back();
341     event.readRecordThreadRunning_ = true;
342     EXPECT_EQ(event.PrepareRecordThread(), true);
343     this_thread::sleep_for(1s);
344     std::vector<pid_t> tids = GetSubthreadIDs(getpid());
345     EXPECT_FALSE(tids.empty());
346     bool get = 0;
347     for (const pid_t tid : tids) {
348         std::string threadName = ReadFileToString(StringPrintf("/proc/%d/comm", tid));
349         while (threadName.back() == '\0' || threadName.back() == '\n') {
350             threadName.pop_back();
351         }
352         if (threadName == "timer_thread") {
353             get = true;
354             break;
355         }
356     }
357     EXPECT_EQ(get, true);
358     PerfEvents::updateTimeThreadRunning_ = false;
359     this_thread::sleep_for(1s);
360 }
361 
362 HWTEST_F(PerfEventsTest, IsOutputTracking, TestSize.Level1)
363 {
364     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
365     StdoutRecord stdoutRecord;
366     stdoutRecord.Start();
367 
368     PerfEvents event;
369     EXPECT_EQ(event.IsOutputTracking(), false);
370     event.outputTracking_ = true;
371     EXPECT_EQ(event.IsOutputTracking(), true);
372 }
373 
374 HWTEST_F(PerfEventsTest, SetBackTrack, TestSize.Level1)
375 {
376     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
377     StdoutRecord stdoutRecord;
378     stdoutRecord.Start();
379 
380     PerfEvents event;
381     event.SetBackTrack(true);
382     EXPECT_EQ(event.backtrack_, true);
383 }
384 
385 HWTEST_F(PerfEventsTest, CalcBufferSizeLittleMemory, TestSize.Level1)
386 {
387     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
388     StdoutRecord stdoutRecord;
389     stdoutRecord.Start();
390 
391     if (!LittleMemory()) {
392         return;
393     }
394 
395     PerfEvents event;
396     event.backtrack_ = false;
397     event.systemTarget_ = true;
398     EXPECT_EQ(event.CalcBufferSize(), PerfEvents::MAX_BUFFER_SIZE_LITTLE);
399 
400     event.backtrack_ = true;
401     event.cpuMmap_.clear();
402     EXPECT_EQ(event.CalcBufferSize(), PerfEvents::MIN_BUFFER_SIZE);
403 
404     event.cpuMmap_[0] = {};
405     event.mmapPages_ = TEN_THOUSAND;
406     event.pageSize_ = TEN_THOUSAND;
407     EXPECT_EQ(event.CalcBufferSize(), PerfEvents::MAX_BUFFER_SIZE_LITTLE);
408 
409     while (event.cpuMmap_.size() < SEVENTEEN) {
410         event.cpuMmap_[event.cpuMmap_.size()] = {};
411     }
412     event.mmapPages_ = PAGE_SIZE;
413     event.pageSize_ = PAGE_SIZE;
414     static constexpr size_t EXPECT_SIZE = SEVENTEEN * PAGE_SIZE * PAGE_SIZE * 4;
415     EXPECT_EQ(event.CalcBufferSize(), EXPECT_SIZE);
416 }
417 
418 HWTEST_F(PerfEventsTest, CalcBufferSizeLargeMemory, TestSize.Level1)
419 {
420     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
421     StdoutRecord stdoutRecord;
422     stdoutRecord.Start();
423 
424     if (LittleMemory()) {
425         return;
426     }
427 
428     PerfEvents event;
429     event.backtrack_ = false;
430     event.systemTarget_ = true;
431     EXPECT_EQ(event.CalcBufferSize(), PerfEvents::MAX_BUFFER_SIZE_LARGE);
432 
433     event.backtrack_ = true;
434     event.cpuMmap_.clear();
435     EXPECT_EQ(event.CalcBufferSize(), PerfEvents::MIN_BUFFER_SIZE);
436 
437     event.cpuMmap_[0] = {};
438     event.mmapPages_ = TEN_THOUSAND;
439     event.pageSize_ = TEN_THOUSAND;
440     EXPECT_EQ(event.CalcBufferSize(), PerfEvents::MAX_BUFFER_SIZE_LARGE);
441 
442     while (event.cpuMmap_.size() < SEVENTEEN) {
443         event.cpuMmap_[event.cpuMmap_.size()] = {};
444     }
445     event.mmapPages_ = PAGE_SIZE;
446     event.pageSize_ = PAGE_SIZE;
447     static constexpr size_t EXPECT_SIZE = SEVENTEEN * PAGE_SIZE * PAGE_SIZE * 4;
448     EXPECT_EQ(event.CalcBufferSize(), EXPECT_SIZE);
449 }
450 
451 HWTEST_F(PerfEventsTest, IsSkipRecordForBacktrack1, TestSize.Level1)
452 {
453     static constexpr size_t BACKTRACK_TIME = 2000u;
454     static constexpr size_t SAMPLE_TIME = 1234u;
455     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
456     StdoutRecord stdoutRecord;
457     stdoutRecord.Start();
458 
459     PerfEvents event;
460     event.outputTracking_ = false;
461     event.backtrackTime_ = BACKTRACK_TIME * NANO_SECONDS_PER_SECOND;
462     event.currentTimeSecond_.store(event.backtrackTime_);
463 
464     PerfRecordSample sample;
465     sample.data_.time = SAMPLE_TIME * NANO_SECONDS_PER_SECOND;
466 
467     EXPECT_EQ(event.IsSkipRecordForBacktrack(sample), true);
468 }
469 
470 HWTEST_F(PerfEventsTest, IsSkipRecordForBacktrack2, TestSize.Level1)
471 {
472     static constexpr size_t END_TIME = 2000u;
473     static constexpr size_t SAMPLE_TIME = 1234u;
474     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
475     StdoutRecord stdoutRecord;
476     stdoutRecord.Start();
477 
478     PerfEvents event;
479     event.outputTracking_ = true;
480     event.outputEndTime_ = END_TIME * NANO_SECONDS_PER_SECOND;
481 
482     PerfRecordSample sample;
483     sample.data_.time = SAMPLE_TIME * NANO_SECONDS_PER_SECOND;
484 
485     EXPECT_EQ(event.IsSkipRecordForBacktrack(sample), false);
486 }
487 
488 HWTEST_F(PerfEventsTest, IsSkipRecordForBacktrack3, TestSize.Level1)
489 {
490     static constexpr size_t END_TIME = 1000u;
491     static constexpr size_t SAMPLE_TIME = 1234u;
492     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
493     StdoutRecord stdoutRecord;
494     stdoutRecord.Start();
495 
496     PerfEvents event;
497     event.outputTracking_ = true;
498     event.outputEndTime_ = END_TIME;
499 
500     PerfRecordSample sample;
501     sample.data_.time = SAMPLE_TIME * NANO_SECONDS_PER_SECOND;
502 
503     EXPECT_EQ(event.IsSkipRecordForBacktrack(sample), true);
504     EXPECT_EQ(event.outputTracking_, false);
505     EXPECT_EQ(event.outputEndTime_, 0);
506 }
507 
508 HWTEST_F(PerfEventsTest, OutputTracking, TestSize.Level1)
509 {
510     static constexpr size_t TIME = 1234u;
511     ScopeDebugLevel tempLogLevel(LEVEL_DEBUG);
512     StdoutRecord stdoutRecord;
513     stdoutRecord.Start();
514 
515     PerfEvents event;
516     event.startedTracking_ = false;
517     EXPECT_EQ(event.OutputTracking(), false);
518 
519     event.startedTracking_ = true;
520     event.outputTracking_ = true;
521     EXPECT_EQ(event.OutputTracking(), true);
522 
523     event.outputTracking_ = false;
524     PerfEvents::currentTimeSecond_.store(TIME);
525     EXPECT_EQ(event.OutputTracking(), true);
526     EXPECT_EQ(event.outputEndTime_, TIME);
527     EXPECT_EQ(event.outputTracking_, true);
528 }
529 
530 HWTEST_F(PerfEventsTest, SetConfig, TestSize.Level1)
531 {
532     constexpr uint64_t config = 0x700010007;
533     constexpr uint64_t config1 = 8;
534     constexpr uint64_t config2 = 10;
535     PerfEvents event;
536     std::map<const std::string, uint64_t> speOptMap = {
537         {"branch_filter", 1},   {"load_filter", 1},
538         {"store_filter", 1},    {"ts_enable", 1},
539         {"pa_enable", 1},       {"jitter", 1},
540         {"min_latency", config2},      {"event_filter", config1},
541         {"pct_enable", 1},
542     };
543     event.SetConfig(speOptMap);
544     EXPECT_EQ(event.config_, config);
545     EXPECT_EQ(event.config1_, config1);
546     EXPECT_EQ(event.config2_, config2);
547 }
548 
549 HWTEST_F(PerfEventsTest, SetConfig1, TestSize.Level1)
550 {
551     constexpr uint64_t config = 0x700010003;
552     PerfEvents event;
553     std::map<const std::string, uint64_t> speOptMap = {
554         {"branch_filter", 1},   {"load_filter", 1},
555         {"store_filter", 1},    {"ts_enable", 1},
556         {"pa_enable", 1},       {"jitter", 1},
557         {"min_latency", 0},      {"event_filter", 0},
558         {"pct_enable", 0},
559     };
560     event.SetConfig(speOptMap);
561     EXPECT_EQ(event.config_, config);
562 }
563 } // namespace HiPerf
564 } // namespace Developtools
565 } // namespace OHOS
566