Skip to content

Commit

Permalink
Add tests for omrthread_get_thread_times
Browse files Browse the repository at this point in the history
Signed-off-by: Gengchen Tuo <[email protected]>
  • Loading branch information
thallium committed Oct 21, 2024
1 parent fb9758e commit d26b584
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
39 changes: 37 additions & 2 deletions fvtest/threadextendedtest/processTimeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ userTimeCpuBurn()
}
}

TEST(ThreadCpuTime, userCpuTimeIncreasesMonotonically)
TEST(ProcessCpuTime, userCpuTimeIncreasesMonotonically)
{
omrthread_process_time_t cpuTime;
omrthread_process_time_t prevCpuTime;
Expand All @@ -74,7 +74,7 @@ TEST(ThreadCpuTime, userCpuTimeIncreasesMonotonically)
}
}

TEST(ThreadCpuTime, systemCpuTimeIncreasesMonotonically)
TEST(ProcessCpuTime, systemCpuTimeIncreasesMonotonically)
{
omrthread_process_time_t cpuTime;
omrthread_process_time_t prevCpuTime;
Expand Down Expand Up @@ -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) */
39 changes: 16 additions & 23 deletions thread/common/thrprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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) */
}

0 comments on commit d26b584

Please sign in to comment.