Build 3540. I have a Vista VM with 4 processors allocated to it. When I check, Vista sees 4 processors. However, when I run a multithreaded Java program, only one core is used. When I run the same program on MacOS X, all 4 processors run flat out. Same is true if I run it in Vista_64 on a real PC with multiple processors. Java 6 is being used in all cases. The Java code is very simple, and does nothing tricky - essentially it just starts a bunch of threads, which use the CPU heavily. Both the host's "Activity Monitor", and the Java program itself, show just one core being used. Is this a bug in Parallels? Or can anyone else suggest what is going on?
Can you plz check... Can you please check it another way, e.g. running N busy loop programs or N java's (each of java must consume 1CPU at least)... I will recheck everything locally on my machine, but looks like >90% of CPU is used on my 4 CPU host when I run CPU hogs in guest. Also please specify PD4 version you use.
Like I said, I am using PD 3540, on a Mac Pro 1,1. Here is the Java program used: import java.lang.management.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicLong; public class MultiCoreTester { private static final int THREADS = 20; private static CountDownLatch ct = new CountDownLatch(THREADS); private static AtomicLong total = new AtomicLong(); public static void main(String[] args) throws InterruptedException { long elapsedTime = System.nanoTime(); for (int i = 0; i < THREADS; i++) { Thread thread = new Thread() { public void run() { total.addAndGet(measureThreadCpuTime()); ct.countDown(); } }; thread.start(); } ct.await(); elapsedTime = System.nanoTime() - elapsedTime; System.out.println("Total elapsed time " + elapsedTime); System.out.println("Total thread CPU time " + total.get()); double factor = total.get(); factor /= elapsedTime; System.out.printf("Factor: %.2f%n", factor); } private static long measureThreadCpuTime() { ThreadMXBean tm = ManagementFactory.getThreadMXBean(); long cpuTime = tm.getCurrentThreadCpuTime(); double x = 1; for (long i = 0; i < 1000 * 1000 * 1000; i++) { x += 1.0d; // keep ourselves busy for a while ... } cpuTime = tm.getCurrentThreadCpuTime() - cpuTime; System.out.println(Thread.currentThread() + ": cpuTime = " + cpuTime + " x=" + x); return cpuTime; } }
OK, clearly there is something wrong here: The java one-liner to count the number of processors: System.out.println("Number of Processors: " + ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors()); returns a value 1. This is despite the System showing it has 4 processors. It seems whatever Parallels is doing to make Vista into thinking there are 4 processors available is failing to convince Java.
I have an idea. Parallels Desktop will indeed report a single processor, but it will be one quad-core processor. I don't know Java, but probably there's a way to force it to work with cores, not CPUs?
That's not the problem. Java "knows" on other Vista systems how many cores there are without being told, and uses them all. I will try reinstalling Java, in case this fixed at the time Java was installed, when I was running Parallels 3.
Hmm. Even after completely removing and reinstalling java, I am still only seeing 1 for the number of processors. Anyway, this code is supposed to return the number of logical processors.
I have tried the same thing under a Linux guest OS, with exactly the same result: only one processor is shown instead of the four expected.
OK, the problem in Linux was using GCJ instead of Java. Real Sun java works fine, gcj does not. This still does not explain why Sun Java in Windows Vista only think there is one processor.
Why do not you try public class Main { public void displayAvailableProcessors() { Runtime runtime = Runtime.getRuntime(); int nrOfProcessors = runtime.availableProcessors(); System.out.println("Number of processors available to the Java Virtual Machine: " + nrOfProcessors); } public static void main(String[] args) { new Main().displayAvailableProcessors(); }
OK, this is not just a java issue at all. When I run msconfig and go to the boot tab, and click advanced, and then try to change the number of processors, the drop down menu only shows an option for one processor. In short, somehow, some part of Vista thinks there is only one processor - despite the hardware tab showing 4 processors. Has no-one else had this problem?! Any suggestions as to how I can fix it?
OK, FINALLY solved it. Run msconfig go to boot tab, click on Advanced. Click the checkbox on "Detect HAL", Click off the checkbox on number of processors. Reboot. ACPI gets installed. Reboot. Run msconfig again. Now set number of processors to 4. Reboot. I can now see 4 processors in Java. This may help other people who dont have all processors active....