Skip to content

Commit

Permalink
Add omrthread_get_self_thread_time()
Browse files Browse the repository at this point in the history
This function returns the user and system cpu time of the calling
thread.
Related: eclipse-openj9/openj9#20186

Signed-off-by: Gengchen Tuo <[email protected]>
  • Loading branch information
thallium committed Oct 10, 2024
1 parent d68a419 commit f6726b6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include_core/omrport.h
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,8 @@ typedef struct OMRPortLibrary {
/** see @ref omrcuda.cpp::omrcuda_streamWaitEvent "omrcuda_streamWaitEvent" */
int32_t (*cuda_streamWaitEvent)(struct OMRPortLibrary *portLibrary, uint32_t deviceId, J9CudaStream stream, J9CudaEvent event);
#endif /* OMR_OPT_CUDA */
/** see @ref omrthread.c::omrthread_get_self_thread_time "omrthread_get_self_thread_time" */
int32_t (*omrthread_get_self_thread_time)(struct OMRPortLibrary *portLibrary, struct omrthread_thread_time_t *thread_time);
} OMRPortLibrary;

/**
Expand Down Expand Up @@ -3339,6 +3341,8 @@ extern J9_CFUNC int32_t omrport_getVersion(struct OMRPortLibrary *portLibrary);
privateOmrPortLibrary->cuda_streamWaitEvent(privateOmrPortLibrary, (deviceId), (stream), (event))
#endif /* OMR_OPT_CUDA */

#define omrthread_get_self_thread_time(threadTime) privateOmrPortLibrary->omrthread_get_self_thread_time(privateOmrPortLibrary, (threadTime))

#endif /* !defined(OMRPORT_LIBRARY_DEFINE) */

/** @} */
Expand Down
5 changes: 5 additions & 0 deletions include_core/omrthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ or omrthread priority value.

#define omrthread_monitor_init(pMon,flags) omrthread_monitor_init_with_name(pMon,flags, #pMon)

typedef struct omrthread_thread_time_t {
int64_t userTime;
int64_t sysTime;
} omrthread_thread_time_t;

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions port/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ list(APPEND OBJECTS
omrsignal.c
omrsock.c
omrsockptb.c
omrthread.c
)

if(NOT OMR_OS_WINDOWS)
Expand Down
1 change: 1 addition & 0 deletions port/common/omrport.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ static OMRPortLibrary MainPortLibraryTable = {
omrcuda_streamSynchronize, /* cuda_streamSynchronize */
omrcuda_streamWaitEvent, /* cuda_streamWaitEvent */
#endif /* OMR_OPT_CUDA */
omrthread_get_self_thread_time,
};

/**
Expand Down
41 changes: 41 additions & 0 deletions port/common/omrthread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#if defined (LINUX)
#define _GNU_SOURCE
#include <sys/resource.h>
#endif /* defined (LINUX) */

#include <stdlib.h>

#include "omrport.h"
#include "omrportpriv.h"
#include "omrthread.h"

int32_t omrthread_get_self_thread_time(struct OMRPortLibrary *portLibrary, omrthread_thread_time_t *threadTime) {
omrthread_t self = omrthread_self();

int64_t cpuTime = omrthread_get_cpu_time(self);
if (-1 == cpuTime) {
return -1;
}

#if defined (LINUX)
struct rusage rUsage = {0};
int64_t userTime = -1;
int rc = getrusage(RUSAGE_THREAD, &rUsage);
if (0 == rc) {
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);
} else {
userTime = rc;
}
#else /* defined (LINUX) */
int64_t userTime = omrthread_get_user_time(self);
#endif /* defined (LINUX) */

if (-1 == userTime) {
return -1;
}

threadTime->sysTime = cpuTime - userTime;
threadTime->userTime = userTime;
return 0;
}
4 changes: 4 additions & 0 deletions port/omrportpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1110,4 +1110,8 @@ extern J9_CFUNC int32_t
omrcuda_streamWaitEvent(struct OMRPortLibrary *portLibrary, uint32_t deviceId, J9CudaStream stream, J9CudaEvent event);
#endif /* OMR_OPT_CUDA */

/* omrthread */
extern J9_CFUNC int32_t
omrthread_get_self_thread_time(struct OMRPortLibrary *portLibrary, struct omrthread_thread_time_t *thread_time);

#endif /* omrportlibraryprivatedefines_h */
2 changes: 2 additions & 0 deletions port/port_objects.mk
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ ifeq (1,$(OMR_OPT_CUDA))
OBJECTS += omrcuda
endif

OBJECTS += omrthread

# Append OBJEXT to the complete list of OBJECTS
# except for .res files for windows
OBJECTS := $(sort $(addsuffix $(OBJEXT),$(filter-out %.res,$(OBJECTS))) $(filter %.res,$(OBJECTS)))
Expand Down

0 comments on commit f6726b6

Please sign in to comment.