/* * Multi-core evaluation. Takes number of theads as an * optional argument. On a 4-core device, run with "1" and * again with "4". If the times are about the same, all * cores were used. If the 4-core run takes twice as long, * only two cores were used. * * (Ported from http://bigflake.com/cpu-spinner.c.txt) * * Instructions (assumes rooted device): * javac MultiCore.java * dx --dex --output=mc.zip MultiCore*.class * adb push mc.zip /sdcard/ * adb shell dalvikvm -cp /sdcard/mc.zip MultiCore 4 */ public class MultiCore { private static final int SPIN_COUNT = 2000; public static void main(String[] args) { int numThreads = 4; if (args.length == 1) { numThreads = Integer.valueOf(args[0]); } System.out.println("Starting " + numThreads + " threads"); long startWhen = System.nanoTime(); SpinThread threads[] = new SpinThread[numThreads]; for (int i = 0; i < numThreads; i++) { threads[i] = new SpinThread(i); threads[i].start(); } for (int i = 0; i < numThreads; i++) { try { threads[i].join(); } catch (InterruptedException ie) { System.err.println("join " + i + " failed: " + ie); } } long endWhen = System.nanoTime(); System.out.println("All threads finished in " + ((endWhen - startWhen) / 1000000) + "ms"); } static class SpinThread extends Thread { private int mTid; SpinThread(int tid) { mTid = tid; } public void run() { long startWhen = System.nanoTime(); System.out.println("Thread " + mTid + " started"); int tid = mTid; int reps = SPIN_COUNT + tid; int ret = 0; for (int i = 0; i < reps; i++) { for (int j = 0; j < 100000; j++) { ret += i * j; } } long endWhen = System.nanoTime(); System.out.println("Thread " + mTid + " finished in " + ((endWhen - startWhen) / 1000000) + "ms (" + ret + ")"); } } }