Skip to content

Commit dbd3fb1

Browse files
committed
Add omrthread_get_thread_times()
This function returns the user and system cpu time of the calling thread. This new function is needed so we can get both user time and system time using only one system call. Related: eclipse-openj9/openj9#20186 Signed-off-by: Gengchen Tuo <[email protected]>
1 parent d68a419 commit dbd3fb1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Diff for: include_core/thread_api.h

+14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ typedef struct omrthread_process_time_t {
6262
int64_t _userTime;
6363
} omrthread_process_time_t;
6464

65+
typedef struct omrthread_thread_time_t {
66+
int64_t userTime;
67+
int64_t sysTime;
68+
} omrthread_thread_time_t;
69+
6570
typedef struct omrthread_state_t {
6671
uintptr_t flags;
6772
omrthread_monitor_t blocker;
@@ -1240,6 +1245,15 @@ omrthread_get_jvm_cpu_usage_info(J9ThreadsCpuUsage *cpuUsage);
12401245
void
12411246
omrthread_get_jvm_cpu_usage_info_error_recovery(void);
12421247

1248+
/**
1249+
* Gets the system and user CPU time of the current thread.
1250+
*
1251+
* @param[out] threadTime the pointer to the thread time structure
1252+
* @return 0 on success or -1 on failure
1253+
*/
1254+
intptr_t
1255+
omrthread_get_thread_times(omrthread_thread_time_t *threadTime);
1256+
12431257
/* ---------------- omrthreadattr.c ---------------- */
12441258

12451259
/**

Diff for: thread/common/thrprof.c

+66
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
* APIs for querying per-thread statistics: CPU usage, stack usage.
2929
*/
3030

31+
#if defined(LINUX)
32+
#define _GNU_SOURCE
33+
#include <sys/resource.h>
34+
#endif /* defined(LINUX) */
35+
3136
#include <string.h> /* for memset() */
3237
#include "omrcfg.h"
3338

@@ -1025,3 +1030,64 @@ omrthread_get_jvm_cpu_usage_info_error_recovery(void)
10251030
GLOBAL_UNLOCK_SIMPLE(lib);
10261031
}
10271032
}
1033+
1034+
intptr_t
1035+
omrthread_get_thread_times(omrthread_thread_time_t *threadTime)
1036+
{
1037+
#if defined(LINUX)
1038+
struct rusage rUsage;
1039+
memset(&rUsage, 0, sizeof(rUsage));
1040+
1041+
if (0 == getrusage(RUSAGE_THREAD, &rUsage)) {
1042+
threadTime->userTime = (SEC_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_sec)
1043+
+ (MICRO_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_usec);
1044+
threadTime->sysTime = (SEC_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_stime.tv_sec)
1045+
+ (MICRO_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_stime.tv_usec);
1046+
1047+
return 0;
1048+
}
1049+
1050+
return -1;
1051+
#elif defined(OMR_OS_WINDOWS) && !defined(BREW) /* defined(LINUX) */
1052+
omrthread_t self = omrthread_self();
1053+
FILETIME creationTime;
1054+
FILETIME exitTime;
1055+
FILETIME kernelTime;
1056+
FILETIME userTime;
1057+
memset(&creationTime, 0, sizeof(creationTime));
1058+
memset(&exitTime, 0, sizeof(exitTime));
1059+
memset(&kernelTime, 0, sizeof(kernelTime));
1060+
memset(&userTime, 0, sizeof(userTime));
1061+
1062+
if (GetThreadTimes(self->handle, &creationTime, &exitTime, &kernelTime, &userTime)) {
1063+
/* Time is in 100's of nanos. Convert to nanos. */
1064+
threadTime->sysTime = ((int64_t)kernelTime.dwLowDateTime | ((int64_t)kernelTime.dwHighDateTime << 32)) * 100;
1065+
threadTime->userTime = ((int64_t)userTime.dwLowDateTime | ((int64_t)userTime.dwHighDateTime << 32)) * 100;
1066+
1067+
return 0;
1068+
}
1069+
1070+
return -1;
1071+
#elif defined(AIXPPC) /* defined(OMR_OS_WINDOWS) && !defined(BREW) */
1072+
omrthread_t self = omrthread_self();
1073+
1074+
/* AIX provides a function call that returns an entire structure of
1075+
* information about the thread.
1076+
*/
1077+
struct rusage rUsage;
1078+
memset(&rUsage, 0, sizeof(rUsage));
1079+
1080+
if (0 == pthread_getrusage_np(self->handle, &rUsage, PTHRDSINFO_RUSAGE_COLLECT)) {
1081+
threadTime->userTime = (SEC_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_sec)
1082+
+ (MICRO_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_utime.tv_usec);
1083+
threadTime->sysTime = (SEC_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_stime.tv_sec)
1084+
+ (MICRO_TO_NANO_CONVERSION_CONSTANT * (int64_t)rUsage.ru_stime.tv_usec);
1085+
return 0;
1086+
}
1087+
1088+
return -1;
1089+
#else /* defined(AIXPPC) */
1090+
/* Return -1 since the user time can only be retrieved on Windows, Linux, and AIX. */
1091+
return -1;
1092+
#endif /* defined(LINUX) */
1093+
}

0 commit comments

Comments
 (0)