-2

I am currently running a java program. My java program is multithreaded. My computer has a multi-core processor. Suppose my java program has 4 threads.

I would like to know which CPU core was used by which thread. I would like to know how much time was spent by a thread on a given CPU core.

Is it possible to derive such information?

user207421
  • 305,947
  • 44
  • 307
  • 483
a3.14_Infinity
  • 5,653
  • 7
  • 42
  • 66
  • Is there a cogent reason for wanting this information? – user207421 May 18 '17 at 10:30
  • even though my program is multithreaded, we had observed that only one core is being used. – a3.14_Infinity May 18 '17 at 10:34
  • All the threads will jump around seemingly random between cores, as decided by the Operating System. – Andreas May 18 '17 at 10:34
  • so Operating system has an idea when it throws threads across cores. It could maintain that in some data structure and probably there might be an API around that data structure.. And probably an application could gather that information.. – a3.14_Infinity May 18 '17 at 10:36
  • 1
    I don't think you can do that in default JVM, but there are libraries you can "affine" a thread to certain CPU (https://github.com/OpenHFT/Java-Thread-Affinity) . By default anytime the thread waits for some IO operations it can be suspended / resumed on different core – gusto2 May 18 '17 at 10:36
  • 3
    @a3.14_Infinity "we had observed that only one core is being used" then maybe it is not multithreaded properly – gusto2 May 18 '17 at 10:38
  • so, we can conclude as of now, that such information could not be derived – a3.14_Infinity May 18 '17 at 12:27

1 Answers1

1

Don't know what your machine is showing, but when I start 4 threads that each uses 100% CPU for 10 seconds, my 4 core with hyperthreading shows that all 8 "cores" are used about 50%, evenly spread, i.e. my Windows OS is shuffling them around as it sees fit.

Test Code

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 4; i++)
            new Thread(Test::maxCpu).start();
    }
    private static void maxCpu() {
        for (long end = System.currentTimeMillis() + 10000; end > System.currentTimeMillis(); );
    }
}

CPU Usage

CPU Usage

Andreas
  • 154,647
  • 11
  • 152
  • 247