diff options
author | Dan Egnor <egnor@google.com> | 2010-05-10 09:56:41 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2010-05-10 09:56:41 -0700 |
commit | 07704f1f66e235e01734e8dc2e085649a4779126 (patch) | |
tree | 73de9fba79ec0762d4f6aa9ca856765ba190211e /core/tests/coretests | |
parent | 4e664773be0e7907d78c68ade80e9d10e8709162 (diff) | |
parent | 6bff8df839ba53084b5f29e5e0435628f4657873 (diff) | |
download | frameworks_base-07704f1f66e235e01734e8dc2e085649a4779126.zip frameworks_base-07704f1f66e235e01734e8dc2e085649a4779126.tar.gz frameworks_base-07704f1f66e235e01734e8dc2e085649a4779126.tar.bz2 |
am 6bff8df8: am 1a373db0: am 5610c287: am 7d167376: Merge "Add unit test for passing thread priority & cgroup through Binder." into froyo
Diffstat (limited to 'core/tests/coretests')
5 files changed, 239 insertions, 11 deletions
diff --git a/core/tests/coretests/Android.mk b/core/tests/coretests/Android.mk index c067b80..245c67c 100644 --- a/core/tests/coretests/Android.mk +++ b/core/tests/coretests/Android.mk @@ -7,9 +7,9 @@ LOCAL_MODULE_TAGS := tests # Include all test java files. LOCAL_SRC_FILES := \ $(call all-java-files-under, src) \ + $(call all-Iaidl-files-under, src) \ $(call all-java-files-under, DisabledTestApp/src) \ - $(call all-java-files-under, EnabledTestApp/src) \ - src/android/os/IAidlTest.aidl + $(call all-java-files-under, EnabledTestApp/src) LOCAL_STATIC_JAVA_LIBRARIES += android-common diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml index 30855d1..a77717f 100644 --- a/core/tests/coretests/AndroidManifest.xml +++ b/core/tests/coretests/AndroidManifest.xml @@ -1179,8 +1179,11 @@ <!-- Application components used for os tests --> <service android:name="android.os.MessengerService" - android:process=":messengerService"> - </service> + android:process=":messengerService" /> + + <!-- Used by BinderThreadPriorityTest --> + <service android:name="android.os.BinderThreadPriorityService" + android:process=":BinderThreadPriorityService" /> <!-- Application components used for search manager tests --> @@ -1198,13 +1201,6 @@ android:authorities="android.app.SuggestionProvider"> </provider> - <!-- Used to test IPC. --> - <service android:name="com.android.frameworks.coretests.binder.BinderTestService" - android:process="binder.BinderTestService" /> - <service android:name="com.android.frameworks.coretests.binder.BinderPerformanceService" - android:process="binder.BinderPerformanceService" /> - <service android:name="com.android.frameworks.coretests.binder.BinderVsMessagingService" - android:process="binder.BinderVsMessagingService" /> </application> <instrumentation diff --git a/core/tests/coretests/src/android/os/BinderThreadPriorityService.java b/core/tests/coretests/src/android/os/BinderThreadPriorityService.java new file mode 100644 index 0000000..47a4483 --- /dev/null +++ b/core/tests/coretests/src/android/os/BinderThreadPriorityService.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.os; + +import android.app.Service; +import android.content.Intent; +import android.text.TextUtils; +import android.util.Log; + +/** + * Service used by {@link BinderThreadPriorityTest} to verify + * the conveyance of thread priorities over Binder. + */ +public class BinderThreadPriorityService extends Service { + private static final String TAG = "BinderThreadPriorityService"; + + private final IBinderThreadPriorityService.Stub mBinder = + new IBinderThreadPriorityService.Stub() { + public int getThreadPriority() { + return Process.getThreadPriority(Process.myTid()); + } + + public String getThreadSchedulerGroup() { + return BinderThreadPriorityTest.getSchedulerGroup(); + } + + public void callBack(IBinderThreadPriorityService recurse) { + try { + recurse.callBack(this); + } catch (RemoteException e) { + Log.e(TAG, "Binder callback failed", e); + } + } + + public void setPriorityAndCallBack(int priority, IBinderThreadPriorityService recurse) { + Process.setThreadPriority(priority); + try { + recurse.callBack(this); + } catch (RemoteException e) { + Log.e(TAG, "Binder callback failed", e); + } + } + }; + + public IBinder onBind(Intent intent) { + return mBinder; + } +} diff --git a/core/tests/coretests/src/android/os/BinderThreadPriorityTest.java b/core/tests/coretests/src/android/os/BinderThreadPriorityTest.java new file mode 100644 index 0000000..7a4980a --- /dev/null +++ b/core/tests/coretests/src/android/os/BinderThreadPriorityTest.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.os; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.MediumTest; +import android.util.Log; + +import java.io.File; +import java.io.IOException; + +/** + * Test whether Binder calls inherit thread priorities correctly. + */ +public class BinderThreadPriorityTest extends AndroidTestCase { + private static final String TAG = "BinderThreadPriorityTest"; + private IBinderThreadPriorityService mService; + private int mSavedPriority; + + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName name, IBinder service) { + synchronized (BinderThreadPriorityTest.this) { + mService = IBinderThreadPriorityService.Stub.asInterface(service); + BinderThreadPriorityTest.this.notifyAll(); + } + } + + public void onServiceDisconnected(ComponentName name) { + mService = null; + } + }; + + private static class ServiceStub extends IBinderThreadPriorityService.Stub { + public int getThreadPriority() { fail(); return -999; } + public String getThreadSchedulerGroup() { fail(); return null; } + public void setPriorityAndCallBack(int p, IBinderThreadPriorityService cb) { fail(); } + public void callBack(IBinderThreadPriorityService cb) { fail(); } + private static void fail() { throw new RuntimeException("unimplemented"); } + } + + @Override + protected void setUp() throws Exception { + 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); + } + + @Override + protected void tearDown() throws Exception { + // 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 static String getSchedulerGroup() { + 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 + } + + public static String expectedSchedulerGroup(int prio) { + return prio < Process.THREAD_PRIORITY_BACKGROUND ? "/" : "/bg_non_interactive"; + } + + public void testPassPriorityToService() throws Exception { + 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()); + } + } + + public void testCallBackFromServiceWithPriority() throws Exception { + 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()); + } + } +} diff --git a/core/tests/coretests/src/android/os/IBinderThreadPriorityService.aidl b/core/tests/coretests/src/android/os/IBinderThreadPriorityService.aidl new file mode 100644 index 0000000..b30f04c --- /dev/null +++ b/core/tests/coretests/src/android/os/IBinderThreadPriorityService.aidl @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.os; + +interface IBinderThreadPriorityService { + int getThreadPriority(); + String getThreadSchedulerGroup(); + void callBack(IBinderThreadPriorityService recurse); + void setPriorityAndCallBack(int priority, IBinderThreadPriorityService recurse); +} |