3

I'm reading Stallings' "Operating Systems Internals and Design Principles", and in the chapter about concurrency and mutual exclusion it reads:

In a uniprocessor system, concurrent processes cannot have overlapped execution; they can only be interleaved. Furthermore, a process will continue to run until it invokes an OS service or until it is interrupted.Therefore, to guarantee mutual exclusion, it is sufficient to prevent a process from being interrupted. This capability can be provided in the form of primitives defined by the OS kernel for disabling and enabling interrupts.

I've found this post in SO that seems to address this question, but the OP doesn't give specifics about the type of interrupt he wants to stop, and the post is dead without ever getting an accepted answer (besides the OP deleted the profile), so I was hoping to get to the bottom of this here. Assuming that I want the CPU fully dedicated to my code (this is only in theory, I know this is very dangereous), how would I be able to achieve this with Java?

Community
  • 1
  • 1
Pedro Gordo
  • 1,825
  • 3
  • 21
  • 45
  • 1
    You cannot do that from pure Java. The only way to set that is from native code. I'm not aware of any JNI library for linux that allows to change the priority (niceness) and/or ignore all interrupts. – Augusto Feb 23 '16 at 13:45
  • 1
    You want to give your code exclusive use of your system's CPU, when your code is running in a Java **Virtual Machine** on top of an operating system **that you don't control**? – Andrew Henle Feb 23 '16 at 13:45
  • The answer from @JohnSkeet in the link you provided exactly answers your question (in the general case, unless you do some very low level modifications as described by Peter Lawrey below) ... – Andreas Fester Feb 23 '16 at 13:46
  • 1
    @AndreasFester I saw his answer, but he says he's not sure about it, hence creating my post, but thanks for going over the other thread. – Pedro Gordo Feb 23 '16 at 13:47
  • The quote seems to use "overlapped" as a synonym for "simultaneous", but most of the literature I've read uses "overlapped" precisely in those cases where they want to _avoid_ saying "simultaneous". In the nomenclature that I've seen, an "event" is something that happens at some instant of time, an "interval" is defined by two events separated in time, and two intervals "overlap" if the start event or the end event (or both) of one interval occurs between the start and the end of the other. By that definition, two processes _can_ overlap even on a uniprocessor system. – Solomon Slow Feb 23 '16 at 15:27

1 Answers1

6

The best solution I have found in practice is to isolate the CPU i.e. add isolcpus= to you linux boot, configure IRQ Balance to not use it and, bind specific threads to these isolated CPUs. This reduces jitter to around 10 - 40 micro-seconds. This is a library I wrote to support this https://github.com/OpenHFT/Java-Thread-Affinity It is often used in low latency trading systems to minimize jitter.

Note: there are non-maskable interrupts which cannot be turned off this way, and I suspect it would be unsafe to do so.

You can use a machine code instruction to disable interrupts and enable them again, however you would have to be a privileged user and almost certainly things in the OS would break if you did this for any length of time.

Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • But even that doesn't address the almost-certainly-multithreaded nature of the JVM. – Andrew Henle Feb 23 '16 at 13:48
  • @AndrewHenle I am not sure what you mean? If a thread is bound to an isolated CPU, it will only be disturbed by non-maskable interrupts. – Peter Lawrey Feb 23 '16 at 13:49
  • You start by refering Linux, so can I assume this is not at all possible in Windows (no idea about what's IRQ Balance as well)? If yes, I would like to understand why it's not possible but obviously am not expecting a full explanation here, so could you please give me some words to google so I can understand the why this can't be done on Windows? – Pedro Gordo Feb 23 '16 at 13:52
  • @sedulam you would need to ask the designers of the operating system why they haven't done something. There is no technical reason AFAIK, so I assume there is not enough commercial reason to support it, whereas Linux doesn't need a commercial reason to do something. – Peter Lawrey Feb 23 '16 at 13:54
  • 1
    @PeterLawrey ok so that answers my question about if it's a limitation from Windows itself. Thanks! I can start to see why Linux is preferable if we're aiming for deep tinkering :) – Pedro Gordo Feb 23 '16 at 13:58
  • @PeterLawrey as I said this is only theorical so no need to use your library (and I've never used Linux), but thanks anyway for sharing your work! – Pedro Gordo Feb 23 '16 at 13:58
  • 1
    @PeterLawrey - I probably should have said it can't be guaranteed, for example in the case of more threads than CPUs. To me, the question itself has connotations of "How do I safely cross this minefield on a pogo stick while carrying a piano with a pride of lions trying to eat me?" – Andrew Henle Feb 23 '16 at 13:59
  • 1
    @sedulam Note the binding library works on Windows, but I have found that unless you bind to an isolated CPU (Windows or Linux) it doesn't make much difference. – Peter Lawrey Feb 23 '16 at 14:00
  • @AndrewHenle I didn't mention you get more jitter if you ever give up the CPU and thus lose your CPU cache e.g. a blocking operation or sleep, so you have to avoid those too. ;) – Peter Lawrey Feb 23 '16 at 14:01
  • 1
    @AndrewHenle In short, you have to really, really want to do this. – Peter Lawrey Feb 23 '16 at 14:02