Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force maximum CPU usage

I am doing currently hardware temperature tests and I was wondering how you would do a maximum performance Task to keep all 4 cores of my device busy in order to measure the peak temperature?

I could of course start n Threads with endless loops, but I think there might be better ways to solve this.

while (true) {
        try {
            new Thread() {
                public void run() {
                    while (true) {
                        try {
                            Runtime.getRuntime().exec("ps");
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        } catch (Error e) {
            // typically will be OutOfMemoryerror during Thread alloc
        }
    }

Also in your Manifest:

<application
    android:largeHeap="true"
...

adb shell top:

User 99%, System 0%, IOW 0%, IRQ 0%
User 1216 + Nice 0 + Sys 4 + Idle 0 + IOW 0 + IRQ 0 + SIRQ 0 = 1220

PID PR CPU% S  #THR     VSS     RSS PCY UID      Name
3534  0  99% S  1292 1990880K  32784K  fg u0_a56   com.myapp.cpupressure

But it is still not as effective as AnTuTu stability:

temp curve

like image 321
paulgavrikov Avatar asked Nov 15 '13 08:11

paulgavrikov


People also ask

Can I run my CPU at 100% usage for a long time?

CPUs are designed to run safely at 100% CPU utilization. However, you'll want to avoid these situations whenever they cause perceptible slowness in games.


1 Answers

You can try this multicore test that will use up all the cores. And can overload as you want.

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 + ")");
        }
    }
}
like image 185
amalBit Avatar answered Nov 14 '22 23:11

amalBit