diff options
author | Jeff Brown <jeffbrown@google.com> | 2012-10-10 17:17:58 -0700 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2012-10-10 21:46:55 -0700 |
commit | 9cf36b7a77bb8e821f9e593fdbb200f8a1742ff0 (patch) | |
tree | e84975fdc589a0a9457265626dd5bea21afca2b8 /core/java/android | |
parent | 3c584f20ac8fe9378c094ad3b63936bca35954ba (diff) | |
download | frameworks_base-9cf36b7a77bb8e821f9e593fdbb200f8a1742ff0.zip frameworks_base-9cf36b7a77bb8e821f9e593fdbb200f8a1742ff0.tar.gz frameworks_base-9cf36b7a77bb8e821f9e593fdbb200f8a1742ff0.tar.bz2 |
Don't process UEvents in Dalvik unless they match a pattern.
On some devices, vsync is delivered from the kernel to userspace
over a netlink socket via UEvent. The result is that a thread wakes
up, reads a message, creates a new String then searches it for matches
against a pattern on every single frame.
Reduce the overhead by performing the initial pattern matching in
native code.
Bug: 7326329
Change-Id: Icb22db1c38330694207bec1153840e0c06f502d6
Diffstat (limited to 'core/java/android')
-rw-r--r-- | core/java/android/os/UEventObserver.java | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/core/java/android/os/UEventObserver.java b/core/java/android/os/UEventObserver.java index d33382b..9dbfd50 100644 --- a/core/java/android/os/UEventObserver.java +++ b/core/java/android/os/UEventObserver.java @@ -16,6 +16,8 @@ package android.os; +import android.util.Log; + import java.util.ArrayList; import java.util.HashMap; @@ -37,14 +39,20 @@ import java.util.HashMap; * @hide */ public abstract class UEventObserver { + private static final String TAG = "UEventObserver"; + private static final boolean DEBUG = false; + private static UEventThread sThread; - private static native void native_setup(); - private static native int next_event(byte[] buffer); + private static native void nativeSetup(); + private static native String nativeWaitForNextEvent(); + private static native void nativeAddMatch(String match); + private static native void nativeRemoveMatch(String match); public UEventObserver() { } + @Override protected void finalize() throws Throwable { try { stopObserving(); @@ -78,10 +86,18 @@ public abstract class UEventObserver { * This method can be called multiple times to register multiple matches. * Only one call to stopObserving is required even with multiple registered * matches. - * @param match A substring of the UEvent to match. Use "" to match all - * UEvent's + * + * @param match A substring of the UEvent to match. Try to be as specific + * as possible to avoid incurring unintended additional cost from processing + * irrelevant messages. Netlink messages can be moderately high bandwidth and + * are expensive to parse. For example, some devices may send one netlink message + * for each vsync period. */ public final void startObserving(String match) { + if (match == null || match.isEmpty()) { + throw new IllegalArgumentException("match substring must be non-empty"); + } + final UEventThread t = getThread(); t.addObserver(match, this); } @@ -117,7 +133,7 @@ public abstract class UEventObserver { while (offset < length) { int equals = message.indexOf('=', offset); - int at = message.indexOf(0, offset); + int at = message.indexOf('\0', offset); if (at < 0) break; if (equals > offset && equals < at) { @@ -158,15 +174,17 @@ public abstract class UEventObserver { super("UEventObserver"); } + @Override public void run() { - native_setup(); + nativeSetup(); - byte[] buffer = new byte[1024]; - int len; while (true) { - len = next_event(buffer); - if (len > 0) { - sendEvent(new String(buffer, 0, len)); + String message = nativeWaitForNextEvent(); + if (message != null) { + if (DEBUG) { + Log.d(TAG, message); + } + sendEvent(message); } } } @@ -176,7 +194,7 @@ public abstract class UEventObserver { final int N = mKeysAndObservers.size(); for (int i = 0; i < N; i += 2) { final String key = (String)mKeysAndObservers.get(i); - if (message.indexOf(key) != -1) { + if (message.contains(key)) { final UEventObserver observer = (UEventObserver)mKeysAndObservers.get(i + 1); mTempObserversToSignal.add(observer); @@ -199,6 +217,7 @@ public abstract class UEventObserver { synchronized (mKeysAndObservers) { mKeysAndObservers.add(match); mKeysAndObservers.add(observer); + nativeAddMatch(match); } } @@ -208,7 +227,8 @@ public abstract class UEventObserver { for (int i = 0; i < mKeysAndObservers.size(); ) { if (mKeysAndObservers.get(i + 1) == observer) { mKeysAndObservers.remove(i + 1); - mKeysAndObservers.remove(i); + final String match = (String)mKeysAndObservers.remove(i); + nativeRemoveMatch(match); } else { i += 2; } |