Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add omrthread_get_self_thread_time() #7491

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thallium
Copy link
Contributor

This function returns the user and system cpu time of the calling thread.
Related: eclipse-openj9/openj9#20186

thallium added a commit to thallium/openj9 that referenced this pull request Oct 10, 2024
port/common/omrthread.c Outdated Show resolved Hide resolved
@tajila
Copy link
Contributor

tajila commented Oct 10, 2024

@babsingh Please review

@0xdaryl
Copy link
Contributor

0xdaryl commented Oct 10, 2024

Minor nit: please fix the spelling of "thread" in your commit and PR titles. Thanks.

@thallium thallium changed the title Add omrthread_get_self_thraed_time() Add omrthread_get_self_thread_time() Oct 10, 2024
@thallium
Copy link
Contributor Author

Fixed spelling

@babsingh
Copy link
Contributor

babsingh commented Oct 11, 2024

The new function needs to be added similar to the existing functions in thrprof.c: example.

In the new function, the output of omrthread_get_cpu_time and omrthread_get_user_time is aggregated and returned. thrprof.c should have basic functionalities. The aggregation should be done in the upstream project. In other words, the new function shouldn't reside in OMR.

omrthread_get_user_time doesn't have support for Linux. Does anything prevent us to add Linux support in omrthread_get_user_time?

@tajila
Copy link
Contributor

tajila commented Oct 11, 2024

omrthread_get_user_time doesn't have support for Linux. Does anything prevent us to add Linux support in omrthread_get_user_time?

AFAIK, the only way to get user time reliably on linux is with getrusage(RUSAGE_THREAD, ... which can only be called on the current thread. This is why I proposed making a new API since the existing one takes an arbritrary thread as a param.

The goal is to do something like omrthread_get_process_times which returns omrthread_process_time_t

port/common/omrthread.c Outdated Show resolved Hide resolved
@thallium thallium force-pushed the openj9 branch 2 times, most recently from 7b3ceba to 52e7090 Compare October 11, 2024 15:33
port/common/omrthread.c Outdated Show resolved Hide resolved
port/common/omrthread.c Outdated Show resolved Hide resolved
port/common/omrthread.c Outdated Show resolved Hide resolved
port/common/omrthread.c Outdated Show resolved Hide resolved
port/common/omrthread.c Outdated Show resolved Hide resolved
@babsingh
Copy link
Contributor

babsingh commented Oct 11, 2024

The new function needs to be added similar to the existing functions in thrprof.c: example.

@thallium port and thread libraries are standalone. a thread library function shouldn't be added to the port library. see an existing function such as thrprof.c:omrthread_get_process_times and follow its implementation. the correct usage of the new function should require #include "omrthread.h" or #include "thread_api.h".

Also, include the justification in the commit on why the new function is needed.

@thallium
Copy link
Contributor Author

Moved the function to thrprof.c

thallium added a commit to thallium/openj9 that referenced this pull request Oct 11, 2024
thread/common/thrprof.c Outdated Show resolved Hide resolved
thallium added a commit to thallium/openj9 that referenced this pull request Oct 11, 2024
thread/common/thrprof.c Outdated Show resolved Hide resolved
thread/common/thrprof.c Outdated Show resolved Hide resolved
This function returns the user and system cpu time of the calling
thread. This new function is needed because the only way that we're
aware of to get the user CPU time of a thread is to use
getrusage(RUSAGE_THREAD, ...) which can only be called on the current
thread.

Related: eclipse-openj9/openj9#20186

Signed-off-by: Gengchen Tuo <[email protected]>
include_core/thread_api.h Show resolved Hide resolved
include_core/thread_api.h Show resolved Hide resolved
thread/common/thrprof.c Show resolved Hide resolved
thread/common/thrprof.c Show resolved Hide resolved
@babsingh
Copy link
Contributor

@thallium Also, can you add tests for the new function in fvtest/threadextendedtest/processTimeTest.cpp?

Comment on lines +1052 to +1053
int64_t userTime = omrthread_get_user_time(self);
int64_t cpuTime = omrthread_get_cpu_time(self);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given these existing API functions, what's the real value of adding this function?

If we decide to go ahead with this change, this should use omrthread_get_self_cpu_time() and omrthread_get_self_user_time() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. Yeah, I think the best way is to add Linux implementation to omrthread_get_self_user_time() so we don't need to add a new function. Then we'll be calling omrthread_get_self_cpu_time() and omrthread_get_self_user_time() on the J9 side. @tajila what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to avoid calling getrusage twice, since we need both cpu and user time. What does the support look like on other platforms, are they separate system calls or can those be bundled as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like on Windows and AIX they can be bundled as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're concerned about making two system calls where one would suffice, I suggest this function should be used by omrthread_get_self_cpu_time() and omrthread_get_self_user_time() to reduce code duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good idea because on some platforms omrthread_get_self_cpu_time get CPU time directly rather than by adding user time and system time.

@thallium
Copy link
Contributor Author

@babsingh how can I add tests for specific platforms? (since omrthread_get_thread_times will fail on unsupported platforms)

@babsingh
Copy link
Contributor

how can I add tests for specific platforms? (since omrthread_get_thread_times will fail on unsupported platforms)

You can use ifdefs:

#if SUPPORTED_PLATFORMS
   Assert(SUCCESS == rc)
   ...
#else
   Assert(FAILURE == rc)
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants