RFR: 8321242: Enable WorkerThreads to run tasks in caller thread [v5]

Stefan Karlsson stefank at openjdk.org
Wed Dec 6 14:27:36 UTC 2023


On Wed, 6 Dec 2023 14:04:48 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> `WorkerThreads` is a generic abstraction that allows running the task in multiple threads. There are cases, however, when we only ask for a single thread to carry the computation, and we can accept the caller running the computation instead of ping-ponging with the worker thread. As rendezvous with worker thread is a potential latency hiccup, we would like to avoid delegating work to worker threads unnecessarily. This improves latency on critical paths: the usual round-trip takes about 10..50us on systems I tried.
>> 
>> Nominally, we would like to handle the case for 1 worker requested. But I would argue we would also like to do this even if we are requesting N (N > 1) threads to carry the compute. We can submit (N-1) tasks to workers, and execute the other task in the caller. For lower N-s, this removes additional rendezvous point with workers, and allows caller to complete the bulk of the work while workers are waking up. This would also simplify testing: if caller path is always taken, this would verify the task can indeed be executed by caller.
>> 
>> We cannot, however, do this optimization unconditionally, because the caller thread might not be set up in the same way as workers are, and executing the code in caller might cause bugs. Therefore, it would be nice to have the opt-in option that allows running in caller thread. Since this looks to be the property of the task, I added it to task definition.
>> 
>> This PR is only the infrastructure code additions, without product code behavior changes. New option is sanity-tested by new gtest. Other PRs can then opt-in tasks into this, for example #16882.
>> 
>> Additional testing:
>>  - [x] New gtest
>>  - [x] Linux x86_64 server fastdebug, `tier{1,2,3}`
>>  - [x] Linux AArch64 server fastdebug, `tier{1,2,3}`
>
> Aleksey Shipilev has updated the pull request incrementally with one additional commit since the last revision:
> 
>   gtest touchups

I had hoped that we could get rid of even more special handling of the caller in the WorkerTaskDispatcher, but I see that the proposed patch still tweaks `_num_finished` and runs the caller class without partaking in the counter logic. I guess that the motivation for this is so that we skip all signaling and skip potentially waking up a worker when it isn't needed. I think I would prefer if we had a special-case for this, and make sure that all threads that participate in the WorkerTaskDispatcher use signaling and accounting.

I've created a tweaked patch that does this. Please, take a look and consider this approach:

https://github.com/openjdk/jdk/compare/master...stefank:jdk:pr_16945
https://github.com/stefank/jdk/tree/pr_16945
https://github.com/stefank/jdk/commit/c1c7295eb0e5bb5b3ad35bfae8316cbaed62cacb

Yet another alternative is to limit the change to only let the caller run if the task is only supposed to run with one worker. So, remove most changes, but keep:

void WorkerThreads::run_task(WorkerTask* task) {
  // Special-case for single-worker tasks that can run on the current thread.
  if (_active_workers == 1 && task->caller_can_run()) {
    task->work(0);
    return;
  }

-------------

PR Comment: https://git.openjdk.org/jdk/pull/16945#issuecomment-1842989088


More information about the hotspot-gc-dev mailing list