11

While playing with some jcstress code, I noticed two parameters that are very new to me: StressLCM and StressGCM.

The very first thing to do for me was searching for these in the source code itself and while I have found some things, it is still unclear what they actually do. I was really hoping to see some comments in the source code that would shed some light, but no luck.

I also found the bug description where these have been added, but the explanation made no sense for me:

Randomize instruction scheduling in LCM/GCM.

Can someone explain what they do, if possible in plain english?

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Useful link further - [Close Encounters of The Java Memory Model Kind#Hardware and Runtime Modes](https://readtiger.com/https/shipilev.net/blog/2016/close-encounters-of-jmm-kind/#_hardware_and_runtime_modes) – Naman May 23 '19 at 11:31
  • 3
    I suppose LCM / GCM stands for Local Code Motion / Global Code Motion. Compiler may reorder independent instructions (without changing the semantics of the code) to optimize CPU utilization. The given "stress" options make such reordering nondeterministic for the purpose of testing more combinations of instruction interleaving. – apangin May 23 '19 at 11:34
  • @apangin doesn't the *reorder independent instructions* is on by default? also, when you say that they are made "nondeterministic" - you mean that the semantics of the code is changed (potentially wrong)? slightly confused... – Eugene May 23 '19 at 11:39
  • 2
    Yes, instruction reordering (scheduling) is on by default. That's what LCM/GCM do. They try to choose the most optimal schedule. StressLCM/GCM do not choose the optimal schedule - instead they randomize the schedule within the allowed constraints, so that the code still remains valid from the programmer's point of view. – apangin May 23 '19 at 11:53
  • 1
    @apangin excellent! why not make it an answer? – Eugene May 23 '19 at 11:56

1 Answers1

18

LCM / GCM stands for Local Code Motion / Global Code Motion. To optimize CPU utilization, compiler may reorder independent instructions without changing the semantics of the code. Compiler tries to find the most optimal (from performance perspective) order of instructions. This is called instruction scheduling, and that's what LCM / GCM do.

With -XX:+StressLCM / -XX:+StressGCM options the instruction scheduling works in a bit different way. It no longer tries to find the best schedule, but instead chooses a random instruction order within the allowed constraints, still keeping the original semantics unchanged. Such nondeterministic behavior helps to test more combinations of instruction interleaving, which is essential in finding subtle concurrency issues.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
apangin
  • 92,924
  • 10
  • 193
  • 247