Alex Rivera | Logout

How to reduce jitter for Java?

Asked 2011-11-15T17:20:52.577
24

To resolve this issue I have created an open source Java Thread Affinity library

When I have a number of thread interacting closely it can reduce latency and increase throughput. For single threaded tasks it can still reduce jitter quite a bit.


This program looks at the difference in time between calls to System.nanoTime() and reports those over 10x,000 ns.

public class TimeJumpingMain {
    static final long IGNORE_TIME = 1000 * 1000 * 1000; // the first second to allow warmup.
    static final int minJump = 10; // smallest jump of 10 us.
    static final int midJump = 100; // mid size jump of 100 us.
    static final int bigJump = 1000; // big jump of 1 ms.

    public static void main(String... args) {
        int[] intervalTimings = new int[1000];
        int[] jumpTimings = new int[1000];

        long start = System.nanoTime();
        long prev = start;
        long prevJump = start;
        int jumpCount = 0;
        int midJumpCount = 0;
        int bigJumpCount = 0;

        while (true) {
            long now = System.nanoTime();
            long jump = (now - prev) / 1000;
            if (jump > minJump && now - start > IGNORE_TIME) {
                long interval = (now - prevJump) / 1000;
                if (jumpCount < intervalTimings.length) {
                    intervalTimings[jumpCount] = (int) interval;
                    jumpTimings[jumpCount] = (int) jump;
                }
                if (jump >= midJump)
                    midJumpCount++;
                if (jump >= bigJump)
                    bigJumpCount++;
                prevJump = now;
                jumpCount++;
            }
            prev = now;
            if (now - start > 120L * 1000 * 1000 * 1000 + IGNORE_TIME)
                break;
        }
        System.out.println("interval us\tdelay us");
        for (
Edit
Report

1 Answer

17

There's system jitter and JVM jitter.

For the former you can use the isolcpus parameter at boot time to ensure that nothing but your application code can run on those cpus

http://www.novell.com/support/viewContent.do?externalId=7009596&sliceId=1

Ideally you'd do a jni call (to your own jni lib) down to sched_setaffinity just for the active thread so that you really do have nothing but that thread running there.

In my experience, system jitter is minimised by use of isolcpus with interrupts handled by specific cores only, hyper threading switched off and absolutely all use of power management removed (these are bios options when they're available to turn off all the c-state & p-state management) while running your app on shielded cores. The bios specific options are obviously specific to your motherboard so you'll need to look into that based on your motherboard model.

Another thing look at at the system level is the local APIC interrupt (LOC, local interrupt counter) frequency. Is this a "low latency desktop" using 1kHz interrupts? either way, you can expect jitter to be clustered around the interrupt interval

2 more I know practically nothing about but am aware of as sources of jitter; kernel tlb flush interrupts and userspace tlb flush interrupts. Some RT kernels offer options to control these so this might be another thing to look into. You can also look at this site about building RT apps on the RT kernel for more tips.

answered 2011-11-16T11:07:35.353

Your Answer