1

Actually I'm using java to monitor the cpu usage for a certain java process.Here are my questions: First,is there a limit that a single process can only consume cpu processing time on 1 or limited cpu cores?Or it can use cpu time on each of the cpu core? Second,if I want to monitor the cpu usage of a certain java process for each cpu core,how can I do that? And I prefer to handle it using pure java,not native method.

user998899
  • 21
  • 2
  • possible duplicate of [How to do I check CPU and Memory Usage in Java?](http://stackoverflow.com/questions/74674/how-to-do-i-check-cpu-and-memory-usage-in-java) – trashgod Oct 18 '11 at 02:26
  • @user998899: you can limit a Java process to certain cores by setting the "CPU affinity" for the Java process (I've done it on Linux and Windows, never tried on OS X). But sadly AFAIK there's no way to do this natively from Java. – TacticalCoder Oct 18 '11 at 02:27
  • @user988052 I just want to do the monitoring work,and setting CPU affinity actually is not just monitoring.... – user998899 Oct 18 '11 at 02:33
  • @user998899: It's not an answer, hence the comment and not an answer. But you specifically wrote: *"is there a limit that a single process can only consume cpu processing time on 1 or limited cpu cores"* [sic] So, as far as I understand that sentence, I thought you may be interested in knowing that there was indeed a way to have a Java process *"only consume processing time on 1 core"* [sic]. – TacticalCoder Oct 18 '11 at 11:38

1 Answers1

1

To the operating system, a single thread (which I assume is what you mean by "Java process") essentially cannot use CPU on more than one "processor" (which may or may not mean a physical core-- see below) simultaneously.

Generally, whenever a given thread gets a "turn at running", Windows (and I assume other operating systems) will attempt to schedule a given thread on to the same "processor" that it last ran on.

However, the situation is complicated by hyperthreading CPUs which actually present to the operating system several "processors" for what is actually a single core physical core. In this case, it is actually the CPU itself that switches between what instruction of what thread is running on which component of the given core at any one time. (Because, e.g. the core's arithmetic unit could be performing an arithmetic instruction for Thread 1 while the load/store unit is fetching data from memory for an instruction for Thread 2, etc.)

So given the complexity of this situation, even if you can get per-core measurements, I'm not entirely sure quite what useful meaning you would attach to them

P.S. If you'll permit the plug, I don't know if this Java-focussed article on thread scheduling that I wrote a couple of years ago might be useful. I should say I wrote it before either Windows 7 or the latest Intel Core CPUs were released, and there may be one or two updates to the information that would be pertinent (in particular, I don't address the issue of variable core speeds and how that could affect scheduling).

Neil Coffey
  • 20,815
  • 6
  • 58
  • 78