From d26b5841710d5c8adb7d811371356a1d57a101c3 Mon Sep 17 00:00:00 2001 From: Gengchen Tuo Date: Fri, 18 Oct 2024 11:40:48 -0400 Subject: [PATCH] Add tests for omrthread_get_thread_times Signed-off-by: Gengchen Tuo --- fvtest/threadextendedtest/processTimeTest.cpp | 39 ++++++++++++++++++- thread/common/thrprof.c | 39 ++++++++----------- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/fvtest/threadextendedtest/processTimeTest.cpp b/fvtest/threadextendedtest/processTimeTest.cpp index 681b921cc02..309f77d67db 100644 --- a/fvtest/threadextendedtest/processTimeTest.cpp +++ b/fvtest/threadextendedtest/processTimeTest.cpp @@ -60,7 +60,7 @@ userTimeCpuBurn() } } -TEST(ThreadCpuTime, userCpuTimeIncreasesMonotonically) +TEST(ProcessCpuTime, userCpuTimeIncreasesMonotonically) { omrthread_process_time_t cpuTime; omrthread_process_time_t prevCpuTime; @@ -74,7 +74,7 @@ TEST(ThreadCpuTime, userCpuTimeIncreasesMonotonically) } } -TEST(ThreadCpuTime, systemCpuTimeIncreasesMonotonically) +TEST(ProcessCpuTime, systemCpuTimeIncreasesMonotonically) { omrthread_process_time_t cpuTime; omrthread_process_time_t prevCpuTime; @@ -369,3 +369,38 @@ TEST(ThreadExtendedTest, DISABLED_TestThreadCpuTime) */ ASSERT_TRUE(cpuUsageAft.systemJvmCpuTime >= cpuUsageBef.systemJvmCpuTime); } + +/* Currently, omrthread_get_thread_times is only supported on certain platforms. */ +#if defined(LINUX) \ + || (defined(OMR_OS_WINDOWS) && !defined(BREW)) \ + || defined(AIXPPC) +TEST(ThreadCpuTime, userCpuTimeIncreasesMonotonically) +{ + omrthread_thread_time_t cpuTime; + omrthread_thread_time_t prevCpuTime; + ASSERT_EQ(omrthread_get_thread_times(&prevCpuTime), 0); + + for (size_t i = 0; i < 500; i += 1) { + userTimeCpuBurn(); + ASSERT_EQ(omrthread_get_thread_times(&cpuTime), 0); + ASSERT_GE(cpuTime.userTime, prevCpuTime.userTime); + prevCpuTime = cpuTime; + } +} + +TEST(ThreadCpuTime, systemCpuTimeIncreasesMonotonically) +{ + omrthread_thread_time_t cpuTime; + omrthread_thread_time_t prevCpuTime; + ASSERT_EQ(omrthread_get_thread_times(&prevCpuTime), 0); + + for (size_t i = 0; i < 500; i += 1) { + systemTimeCpuBurn(); + ASSERT_EQ(omrthread_get_thread_times(&cpuTime), 0); + ASSERT_GE(cpuTime.sysTime, prevCpuTime.sysTime); + prevCpuTime = cpuTime; + } +} +#endif /* defined(LINUX) \ + || (defined(OMR_OS_WINDOWS) && !defined(BREW)) \ + || defined(AIXPPC) */ diff --git a/thread/common/thrprof.c b/thread/common/thrprof.c index f9b5eff978d..6a69d4971f0 100644 --- a/thread/common/thrprof.c +++ b/thread/common/thrprof.c @@ -1038,7 +1038,7 @@ omrthread_get_thread_times(omrthread_thread_time_t *threadTime) struct rusage rUsage; memset(&rUsage, 0, sizeof(rUsage)); - if (-1 != getrusage(RUSAGE_THREAD, &rUsage)) { + if (0 == getrusage(RUSAGE_THREAD, &rUsage)) { threadTime->userTime = (SEC_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_sec) + (MICRO_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_usec); threadTime->sysTime = (SEC_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_stime.tv_sec) @@ -1049,14 +1049,18 @@ omrthread_get_thread_times(omrthread_thread_time_t *threadTime) return -1; #elif defined(OMR_OS_WINDOWS) && !defined(BREW) /* defined(LINUX) */ - omrthread_t self = omrthread_self(); - FILETIME creationTime, exitTime, kernelTime, userTime; - - /* WARNING! Not supported on Win95! Need to test to ensure this fails gracefully */ + FILETIME creationTime; + FILETIME exitTime; + FILETIME kernelTime; + FILETIME userTime; + memset(&creationTime, 0, sizeof(creationTime)); + memset(&exitTime, 0, sizeof(exitTime)); + memset(&kernelTime, 0, sizeof(kernelTime)); + memset(&userTime, 0, sizeof(userTime)); if (GetThreadTimes(self->handle, &creationTime, &exitTime, &kernelTime, &userTime)) { - /* Time is in 100's of nanos. Convert to nanos */ + /* Time is in 100's of nanos. Convert to nanos. */ threadTime->sysTime = ((int64_t)kernelTime.dwLowDateTime | ((int64_t)kernelTime.dwHighDateTime << 32)) * 100; threadTime->userTime = ((int64_t)userTime.dwLowDateTime | ((int64_t)userTime.dwHighDateTime << 32)) * 100; @@ -1065,16 +1069,14 @@ omrthread_get_thread_times(omrthread_thread_time_t *threadTime) return -1; #elif defined(AIXPPC) /* defined(OMR_OS_WINDOWS) && !defined(BREW) */ - omrthread_t self = omrthread_self(); /* AIX provides a function call that returns an entire structure of * information about the thread. */ - struct rusage rUsage; - memset(&rUsage, 0, sizeof(rUsage)); + if (0 == pthread_getrusage_np(self->handle, &rUsage, PTHRDSINFO_RUSAGE_COLLECT)) { threadTime->userTime = (SEC_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_sec) + (MICRO_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_usec); @@ -1086,18 +1088,9 @@ omrthread_get_thread_times(omrthread_thread_time_t *threadTime) return -1; #else /* defined(AIXPPC) */ - omrthread_t self = omrthread_self(); - - int64_t userTime = omrthread_get_self_user_time(self); - int64_t cpuTime = omrthread_get_self_cpu_time(self); - - if ((-1 == cpuTime) || (-1 == userTime)) { - return -1; - } - - threadTime->sysTime = cpuTime - userTime; - threadTime->userTime = userTime; - - return 0; -#endif + /* Currently getting user time is only supported on Windows, Linux, and AIX, so + * we can just return -1. + */ + return -1; +#endif /* defined(LINUX) */ }