User threads are interrupted uninterruptibly (see /proc/interrupts) and pinning your thread/process to a particular CPU does not guarantee that the OS won't run other threads on the same CPU, just that yours won't be run on others. More importantly, however, in this case is the fact that System.nanoTime() is used to keep the CPU busy. System.nanoTime() translates to clock_gettime(CLOCK_MONOTONIC) and that thing is far more complex than just 'rdtsc' and a couple of arithmetic operations. For one thing, it occasionally makes system calls. Not only those system calls take much longer to execute, you relinquish control to the kernel at a well defined point so it may decide to run stuff it wouldn't during scheduling interrupts. Anyway, all that to support a simple idea that after 5 seconds of "spinning" in System.nanoTime() you can be sure that cached 'calculation', 'rand' and whatever 'rand' uses internally are long gone. That is to say that when you measure your computation for the first time after busyPause() you measure, among other things, how long it takes to read all those values from lower levels of the memory hierarchy. My one-liner was intended to show that if we 'prefetch' the values the measured time goes down significantly. Whatever other theory you may have, it now has to provide an account for that change as well. This is not a full solution to the mystery you started with but an important factor not to be neglected.
-- Oleg On Saturday, May 13, 2017 at 8:03:22 PM UTC-7, J Crawford wrote: > > > Hi Oleg, > > I made sure to use thread pinning so the thread was pinned to an isolated > CPU core. Furthermore, because the thread is spinning, I wouldn't think it > is being interrupted or released from the CPU core. > > Ivan from SO came up with a theory that you must also warmup dynamically > the pauses, check it out: http://stackoverflow.com/a/43699146/7833248 > > Also not sure what you are trying to accomplish / prove with that extra > line after busyPause. Can you elaborate? > > Thanks! > > -JC > > On Saturday, May 13, 2017 at 2:04:55 AM UTC-5, Oleg Mazurov wrote: >> >> Here is my theory. After warmup, every call to busyPause() takes 5 >> seconds. >> During that time the thread will be interrupted many times. Even if it is >> run >> on the same CPU after every interrupt, the cache state will be gone. >> Basically, what you measure after busyPause() is repopulating your caches. >> To test this theory, add one line after busyPause() which does that >> proactively >> before measurement: >> >> double x = busyPause(interval); >> + calculation += x / (rand.nextInt(5) + 1); >> >> >> -- Oleg >> > -- You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group. To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-sympathy+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.