1

There are third-party libraries that check for CPU count for licensing purposes without an obvious way to tell them "use 1 core".

To get around this restriction, I want to be able to start the CPU affinity very early in the process.

Is there a way to set CPU affinity for a process launched from within Eclipse? I want to keep the workflow relatively the same, meaning I still want to be able to debug, etc.

I'm fine with modifying the program if that's the only solution, though I'd prefer solutions that rely on JVM options or Eclipse runner parameters.

Makoto
  • 104,088
  • 27
  • 192
  • 230
levant pied
  • 3,886
  • 5
  • 37
  • 56
  • This is a bit puzzling. [You likely can't do it without JNI](http://stackoverflow.com/q/2238272/1079354), but the *really* puzzling thing is what I suspect you're doing; why is your codebase relying on a license that checks *per core* given that most modern servers have more than 16? – Makoto Sep 13 '16 at 18:09
  • Which OS is this? – that other guy Sep 13 '16 at 18:13
  • @Makoto "why is your codebase relying on a license that checks per core" First sentence. It's not "my codebase" - codebase of 3rd party libraries that I don't control. Thanks for the link, I'll check it out. – levant pied Sep 13 '16 at 18:19
  • @thatotherguy Windows. But it would be good to know how to do it in Linux too. – levant pied Sep 13 '16 at 18:19
  • 1
    @levantpied Dumb question: do these commercial libs check for the number of cores *used* to run them, or the number of cores *available* to run them? I ask because all commercial software I've used does the latter (through system calls), and I'd that's the case wit your libs, then simply restricting them to only use one care may not solve your licensing issue. – Dan Bron Sep 13 '16 at 18:48
  • @DanBron In this case, setting the affinity manually via Task Manager works, so looks like it's the former. Just need to automate this, so it defaults to this. – levant pied Sep 13 '16 at 19:00
  • @levantpied Ok, cool. Detail worth adding to your Q, maybe. – Dan Bron Sep 13 '16 at 19:16
  • @Makoto Can you add your comment as an answer, since I think the link you provided gives instructions that would work on Linux? I'd like to give you credit and it could be useful for others in the future. Thanks again for the help, it pushed me in the right direction. – levant pied Sep 13 '16 at 20:11
  • It's more straightforward to close it as a duplicate, since it's already answered and I shouldn't be taking credit for an answer I didn't put together on my own. I did want to get some insight into it before I blanket-closed it though, but if you feel satisfied that the other answer has answered your question, I see no problem in closing this one. – Makoto Sep 13 '16 at 20:15
  • @Makoto Sure, all good with me, thanks for taking care of it! – levant pied Sep 13 '16 at 20:34

1 Answers1

1

Looks like this:

How to use Java Native Access to set process affinity for processes besides Java.exe?

provides a solution in the question itself, for Windows at least. Copying here for posterity:

   private void setDesiredCpuAffinity() {
      int pid = -1; // -1 means current process
      AffinityKernel instance = (AffinityKernel) Native.loadLibrary("Kernel32", AffinityKernel.class);
      int affinityMask = 7; // Use 3 CPUs
      System.out.println(instance.SetProcessAffinityMask(new HANDLE(new Pointer(pid)), affinityMask));
   }

   private static interface AffinityKernel extends Kernel32 {
      public boolean SetProcessAffinityMask(HANDLE hProcess, int dwProcessAffinityMask);
   }

Per this:

the first parameter of SetProcessAffinityMask is HANDLE hProcess.

Per this:

hProcess = -1 means current process.

Community
  • 1
  • 1
levant pied
  • 3,886
  • 5
  • 37
  • 56