From 110373c0669c78e3f425d8cdce2261c56c60ee43 Mon Sep 17 00:00:00 2001 From: Galaxy Date: Mon, 22 Apr 2024 03:23:15 -0700 Subject: [PATCH] Add parameter for PmuCollect. PmuCollect collects data every 100 milliseconds, which is hard coded. Now users are allowed to specify the interval to handle needs of different scenarios. The min value of the interval is set to 100 millisecond by empirical tests. --- include/pmu.h | 3 ++- pmu/pmu.cpp | 20 +++++++++++--------- test/test_perf/test_api.cpp | 19 ++++++++++--------- test/test_symbol/CMakeLists.txt | 1 + 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/include/pmu.h b/include/pmu.h index 1f97d36..92c1c54 100644 --- a/include/pmu.h +++ b/include/pmu.h @@ -164,9 +164,10 @@ int PmuDisable(int pd); * Collect milliseconds. If is equal to - 1 and the PID list is not empty, the collection * is performed until all processes are complete. * @param milliseconds + * @param interval internal collect period. Unit: millisecond. Must be larger than or equal to 100. * @return int */ -int PmuCollect(int pd, int milliseconds); +int PmuCollect(int pd, int milliseconds, unsigned interval); /** * @brief diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index 2f05b57..48208d9 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -249,9 +249,8 @@ int PmuAppendData(struct PmuData *fromData, struct PmuData **toData) return toLen; } -static int DoCollectCounting(int pd, int milliseconds) +static int DoCollectCounting(int pd, int milliseconds, unsigned collectInterval) { - constexpr int collectInterval = 100; constexpr int usecPerMilli = 1000; // Collect every milliseconds, // and read data from ring buffer. @@ -284,9 +283,8 @@ static int DoCollectCounting(int pd, int milliseconds) return SUCCESS; } -static int DoCollectNonCounting(int pd, int milliseconds) +static int DoCollectNonCounting(int pd, int milliseconds, unsigned collectInterval) { - constexpr int collectInterval = 100; constexpr int usecPerMilli = 1000; // Collect every milliseconds, // and read data from ring buffer. @@ -325,15 +323,15 @@ static int DoCollectNonCounting(int pd, int milliseconds) return SUCCESS; } -static int DoCollect(int pd, int milliseconds) +static int DoCollect(int pd, int milliseconds, unsigned interval) { if (PmuList::GetInstance()->GetTaskType(pd) == COUNTING) { - return DoCollectCounting(pd, milliseconds); + return DoCollectCounting(pd, milliseconds, interval); } - return DoCollectNonCounting(pd, milliseconds); + return DoCollectNonCounting(pd, milliseconds, interval); } -int PmuCollect(int pd, int milliseconds) +int PmuCollect(int pd, int milliseconds, unsigned interval) { int err = SUCCESS; string errMsg = ""; @@ -346,11 +344,15 @@ int PmuCollect(int pd, int milliseconds) New(LIBPERF_ERR_INVALID_TIME); return -1; } + if (interval < 100) { + New(LIBPERF_ERR_INVALID_TIME); + return -1; + } pdMutex.tryLock(pd); runningStatus[pd] = true; pdMutex.releaseLock(pd); - err = DoCollect(pd, milliseconds); + err = DoCollect(pd, milliseconds, interval); } catch (std::bad_alloc&) { err = COMMON_ERR_NOMEM; } catch (exception& ex) { diff --git a/test/test_perf/test_api.cpp b/test/test_perf/test_api.cpp index 2a807e7..800b941 100644 --- a/test/test_perf/test_api.cpp +++ b/test/test_perf/test_api.cpp @@ -132,6 +132,7 @@ protected: static const unsigned numEvt = 1; static const string expectFilename; static const unsigned expectLine = 17; + static const unsigned collectInterval = 100; int *cpuList = nullptr; char *evtList[numEvt] = {"cycles"}; @@ -157,7 +158,7 @@ TEST_F(TestAPI, SampleCollectSuccess) { auto attr = GetPmuAttribute(); pd = PmuOpen(SAMPLING, &attr); - int ret = PmuCollect(pd, 10); + int ret = PmuCollect(pd, 10, collectInterval); ASSERT_TRUE(ret == SUCCESS); } @@ -165,7 +166,7 @@ TEST_F(TestAPI, SampleReadSuccess) { auto attr = GetPmuAttribute(); pd = PmuOpen(SAMPLING, &attr); - int ret = PmuCollect(pd, 1000); + int ret = PmuCollect(pd, 1000, collectInterval); int len = PmuRead(pd, &data); EXPECT_TRUE(data != nullptr); ASSERT_TRUE(HasExpectSource(data, len)); @@ -183,7 +184,7 @@ TEST_F(TestAPI, SpeCollectSuccess) auto attr = GetSpeAttribute(); pd = PmuOpen(SPE_SAMPLING, &attr); ASSERT_TRUE(pd != -1); - int ret = PmuCollect(pd, 10); + int ret = PmuCollect(pd, 10, collectInterval); ASSERT_TRUE(ret == SUCCESS); } @@ -191,7 +192,7 @@ TEST_F(TestAPI, SpeReadSuccess) { auto attr = GetSpeAttribute(); pd = PmuOpen(SPE_SAMPLING, &attr); - int ret = PmuCollect(pd, 1000); + int ret = PmuCollect(pd, 1000, collectInterval); ASSERT_TRUE(pd != -1); int len = PmuRead(pd, &data); EXPECT_TRUE(data != nullptr); @@ -248,7 +249,7 @@ TEST_F(TestAPI, SampleCollectBadEvt) TEST_F(TestAPI, SampleCollectBadPd) { - auto ret = PmuCollect(3, 1000); + auto ret = PmuCollect(3, 1000, collectInterval); ASSERT_EQ(Perrorno(), LIBPERF_ERR_INVALID_PD); } @@ -269,7 +270,7 @@ TEST_F(TestAPI, SampleSystem) attr.pidList = nullptr; attr.numPid = 0; pd = PmuOpen(SAMPLING, &attr); - int ret = PmuCollect(pd, 100); + int ret = PmuCollect(pd, 100, collectInterval); int len = PmuRead(pd, &data); EXPECT_TRUE(data != nullptr); ASSERT_TRUE(HasExpectSource(data, len)); @@ -282,7 +283,7 @@ TEST_F(TestAPI, SpeSystem) attr.numPid = 0; pd = PmuOpen(SPE_SAMPLING, &attr); ASSERT_TRUE(pd != -1); - int ret = PmuCollect(pd, 1000); + int ret = PmuCollect(pd, 1000, collectInterval); int len = PmuRead(pd, &data); EXPECT_TRUE(data != nullptr); ASSERT_TRUE(HasExpectSymbol(data, len)); @@ -300,7 +301,7 @@ TEST_F(TestAPI, StopSuccess) pd = PmuOpen(SAMPLING, &attr); thread th(Stop, pd); auto start = GetCurrentTime(); - int ret = PmuCollect(pd, 1000 * 10); + int ret = PmuCollect(pd, 1000 * 10, collectInterval); auto end = GetCurrentTime(); th.join(); ASSERT_LE(end - start, 5000); @@ -318,7 +319,7 @@ TEST_F(TestAPI, CollectInvalidTime) { auto attr = GetPmuAttribute(); pd = PmuOpen(SAMPLING, &attr); - int ret = PmuCollect(pd, -2); + int ret = PmuCollect(pd, -2, collectInterval); ASSERT_EQ(Perrorno(), LIBPERF_ERR_INVALID_TIME); } diff --git a/test/test_symbol/CMakeLists.txt b/test/test_symbol/CMakeLists.txt index 0b38073..303c01f 100644 --- a/test/test_symbol/CMakeLists.txt +++ b/test/test_symbol/CMakeLists.txt @@ -1,6 +1,7 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}/../../symbol) include_directories(${CMAKE_CURRENT_LIST_DIR}/../../util) include_directories(${PROJECT_TOP_DIR}/include) +set(CMAKE_CXX_STANDARD 14) aux_source_directory(. SOURCE_SRC) add_executable(test_symbol ${SOURCE_SRC} ${CMAKE_CURRENT_LIST_DIR}/../../util/pcerr.cpp) target_link_libraries(test_symbol sym gtest m gmock gtest_main elf_static dwarf_static pthread) -- Gitee