FileDocCategorySizeDatePackage
BinderThreadPriorityTest.javaAPI DocAndroid 5.1 API5838Thu Mar 12 22:22:12 GMT 2015android.os

BinderThreadPriorityTest

public class BinderThreadPriorityTest extends android.test.AndroidTestCase
Test whether Binder calls inherit thread priorities correctly.

Fields Summary
private static final String
TAG
private IBinderThreadPriorityService
mService
private int
mSavedPriority
private android.content.ServiceConnection
mConnection
Constructors Summary
Methods Summary
public static java.lang.StringexpectedSchedulerGroup(int prio)

        return prio < Process.THREAD_PRIORITY_BACKGROUND ? "/" : "/bg_non_interactive";
    
public static java.lang.StringgetSchedulerGroup()

        String fn = "/proc/" + Process.myPid() + "/task/" + Process.myTid() + "/cgroup";
        try {
            String cgroup = FileUtils.readTextFile(new File(fn), 1024, null);
            for (String line : cgroup.split("\n")) {
                String fields[] = line.trim().split(":");
                    if (fields.length == 3 && fields[1].equals("cpu")) return fields[2];
            }
        } catch (IOException e) {
            Log.e(TAG, "Can't read: " + fn, e);
        }
        return null;  // Unknown
    
protected voidsetUp()

        super.setUp();

        getContext().bindService(
                new Intent(getContext(), BinderThreadPriorityService.class),
                mConnection, Context.BIND_AUTO_CREATE);

        synchronized (this) {
            if (mService == null) {
                try {
                    wait(30000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                assertNotNull("Gave up waiting for BinderThreadPriorityService", mService);
            }
        }

        mSavedPriority = Process.getThreadPriority(Process.myTid());
        Process.setThreadPriority(mSavedPriority);  // To realign priority & cgroup, if needed
        assertEquals(expectedSchedulerGroup(mSavedPriority), getSchedulerGroup());
        Log.i(TAG, "Saved priority: " + mSavedPriority);
    
protected voidtearDown()

        // HACK -- see bug 2665914 -- setThreadPriority() doesn't always set the
        // scheduler group reliably unless we start out with background priority.
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        Process.setThreadPriority(mSavedPriority);
        assertEquals(mSavedPriority, Process.getThreadPriority(Process.myTid()));
        assertEquals(expectedSchedulerGroup(mSavedPriority), getSchedulerGroup());

        getContext().unbindService(mConnection);
        super.tearDown();
    
public voidtestCallBackFromServiceWithPriority()

        for (int prio = -20; prio <= 19; prio++) {
            final int expected = prio;
            mService.setPriorityAndCallBack(prio, new ServiceStub() {
                public void callBack(IBinderThreadPriorityService cb) {
                    assertEquals(expected, Process.getThreadPriority(Process.myTid()));
                    assertEquals(expectedSchedulerGroup(expected), getSchedulerGroup());
                }
            });

            assertEquals(mSavedPriority, Process.getThreadPriority(Process.myTid()));

            // BROKEN -- see bug 2665954 -- scheduler group doesn't get reset
            // properly after a back-call with a different priority.
            // assertEquals(expectedSchedulerGroup(mSavedPriority), getSchedulerGroup());
        }
    
public voidtestPassPriorityToService()

        for (int prio = 19; prio >= -20; prio--) {
            Process.setThreadPriority(prio);

            // Local
            assertEquals(prio, Process.getThreadPriority(Process.myTid()));
            assertEquals(expectedSchedulerGroup(prio), getSchedulerGroup());

            // Remote
            assertEquals(prio, mService.getThreadPriority());
            assertEquals(expectedSchedulerGroup(prio), mService.getThreadSchedulerGroup());
        }