Skip to content

Commit 9cf36b7

Browse files
author
Jeff Brown
committed
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
1 parent 3c584f2 commit 9cf36b7

File tree

2 files changed

+112
-30
lines changed

2 files changed

+112
-30
lines changed

core/java/android/os/UEventObserver.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package android.os;
1818

19+
import android.util.Log;
20+
1921
import java.util.ArrayList;
2022
import java.util.HashMap;
2123

@@ -37,14 +39,20 @@
3739
* @hide
3840
*/
3941
public abstract class UEventObserver {
42+
private static final String TAG = "UEventObserver";
43+
private static final boolean DEBUG = false;
44+
4045
private static UEventThread sThread;
4146

42-
private static native void native_setup();
43-
private static native int next_event(byte[] buffer);
47+
private static native void nativeSetup();
48+
private static native String nativeWaitForNextEvent();
49+
private static native void nativeAddMatch(String match);
50+
private static native void nativeRemoveMatch(String match);
4451

4552
public UEventObserver() {
4653
}
4754

55+
@Override
4856
protected void finalize() throws Throwable {
4957
try {
5058
stopObserving();
@@ -78,10 +86,18 @@ private static UEventThread peekThread() {
7886
* This method can be called multiple times to register multiple matches.
7987
* Only one call to stopObserving is required even with multiple registered
8088
* matches.
81-
* @param match A substring of the UEvent to match. Use "" to match all
82-
* UEvent's
89+
*
90+
* @param match A substring of the UEvent to match. Try to be as specific
91+
* as possible to avoid incurring unintended additional cost from processing
92+
* irrelevant messages. Netlink messages can be moderately high bandwidth and
93+
* are expensive to parse. For example, some devices may send one netlink message
94+
* for each vsync period.
8395
*/
8496
public final void startObserving(String match) {
97+
if (match == null || match.isEmpty()) {
98+
throw new IllegalArgumentException("match substring must be non-empty");
99+
}
100+
85101
final UEventThread t = getThread();
86102
t.addObserver(match, this);
87103
}
@@ -117,7 +133,7 @@ public UEvent(String message) {
117133

118134
while (offset < length) {
119135
int equals = message.indexOf('=', offset);
120-
int at = message.indexOf(0, offset);
136+
int at = message.indexOf('\0', offset);
121137
if (at < 0) break;
122138

123139
if (equals > offset && equals < at) {
@@ -158,15 +174,17 @@ public UEventThread() {
158174
super("UEventObserver");
159175
}
160176

177+
@Override
161178
public void run() {
162-
native_setup();
179+
nativeSetup();
163180

164-
byte[] buffer = new byte[1024];
165-
int len;
166181
while (true) {
167-
len = next_event(buffer);
168-
if (len > 0) {
169-
sendEvent(new String(buffer, 0, len));
182+
String message = nativeWaitForNextEvent();
183+
if (message != null) {
184+
if (DEBUG) {
185+
Log.d(TAG, message);
186+
}
187+
sendEvent(message);
170188
}
171189
}
172190
}
@@ -176,7 +194,7 @@ private void sendEvent(String message) {
176194
final int N = mKeysAndObservers.size();
177195
for (int i = 0; i < N; i += 2) {
178196
final String key = (String)mKeysAndObservers.get(i);
179-
if (message.indexOf(key) != -1) {
197+
if (message.contains(key)) {
180198
final UEventObserver observer =
181199
(UEventObserver)mKeysAndObservers.get(i + 1);
182200
mTempObserversToSignal.add(observer);
@@ -199,6 +217,7 @@ public void addObserver(String match, UEventObserver observer) {
199217
synchronized (mKeysAndObservers) {
200218
mKeysAndObservers.add(match);
201219
mKeysAndObservers.add(observer);
220+
nativeAddMatch(match);
202221
}
203222
}
204223

@@ -208,7 +227,8 @@ public void removeObserver(UEventObserver observer) {
208227
for (int i = 0; i < mKeysAndObservers.size(); ) {
209228
if (mKeysAndObservers.get(i + 1) == observer) {
210229
mKeysAndObservers.remove(i + 1);
211-
mKeysAndObservers.remove(i);
230+
final String match = (String)mKeysAndObservers.remove(i);
231+
nativeRemoveMatch(match);
212232
} else {
213233
i += 2;
214234
}

core/jni/android_os_UEventObserver.cpp

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,103 @@
1515
*/
1616

1717
#define LOG_TAG "UEventObserver"
18+
//#define LOG_NDEBUG 0
19+
1820
#include "utils/Log.h"
1921

2022
#include "hardware_legacy/uevent.h"
2123
#include "jni.h"
2224
#include "JNIHelp.h"
2325
#include "android_runtime/AndroidRuntime.h"
2426

25-
namespace android
26-
{
27+
#include <utils/Mutex.h>
28+
#include <utils/Vector.h>
29+
#include <utils/String8.h>
30+
#include <ScopedUtfChars.h>
2731

28-
static void
29-
android_os_UEventObserver_native_setup(JNIEnv *env, jclass clazz)
30-
{
32+
namespace android {
33+
34+
static Mutex gMatchesMutex;
35+
static Vector<String8> gMatches;
36+
37+
static void nativeSetup(JNIEnv *env, jclass clazz) {
3138
if (!uevent_init()) {
3239
jniThrowException(env, "java/lang/RuntimeException",
33-
"Unable to open socket for UEventObserver");
40+
"Unable to open socket for UEventObserver");
3441
}
3542
}
3643

37-
static int
38-
android_os_UEventObserver_next_event(JNIEnv *env, jclass clazz, jbyteArray jbuffer)
39-
{
40-
int buf_sz = env->GetArrayLength(jbuffer);
41-
char *buffer = (char*)env->GetByteArrayElements(jbuffer, NULL);
44+
static bool isMatch(const char* buffer, size_t length) {
45+
AutoMutex _l(gMatchesMutex);
46+
47+
for (size_t i = 0; i < gMatches.size(); i++) {
48+
const String8& match = gMatches.itemAt(i);
49+
50+
// Consider all zero-delimited fields of the buffer.
51+
const char* field = buffer;
52+
const char* end = buffer + length;
53+
do {
54+
if (strstr(field, match.string())) {
55+
ALOGV("Matched uevent message with pattern: %s", match.string());
56+
return true;
57+
}
58+
field += strlen(field) + 1;
59+
} while (field != end);
60+
}
61+
return false;
62+
}
4263

43-
int length = uevent_next_event(buffer, buf_sz - 1);
64+
static jstring nativeWaitForNextEvent(JNIEnv *env, jclass clazz) {
65+
char buffer[1024];
4466

45-
env->ReleaseByteArrayElements(jbuffer, (jbyte*)buffer, 0);
67+
for (;;) {
68+
int length = uevent_next_event(buffer, sizeof(buffer) - 1);
69+
if (length <= 0) {
70+
return NULL;
71+
}
72+
buffer[length] = '\0';
4673

47-
return length;
74+
ALOGV("Received uevent message: %s", buffer);
75+
76+
if (isMatch(buffer, length)) {
77+
// Assume the message is ASCII.
78+
jchar message[length];
79+
for (int i = 0; i < length; i++) {
80+
message[i] = buffer[i];
81+
}
82+
return env->NewString(message, length);
83+
}
84+
}
85+
}
86+
87+
static void nativeAddMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
88+
ScopedUtfChars match(env, matchStr);
89+
90+
AutoMutex _l(gMatchesMutex);
91+
gMatches.add(String8(match.c_str()));
92+
}
93+
94+
static void nativeRemoveMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
95+
ScopedUtfChars match(env, matchStr);
96+
97+
AutoMutex _l(gMatchesMutex);
98+
for (size_t i = 0; i < gMatches.size(); i++) {
99+
if (gMatches.itemAt(i) == match.c_str()) {
100+
gMatches.removeAt(i);
101+
break; // only remove first occurrence
102+
}
103+
}
48104
}
49105

50106
static JNINativeMethod gMethods[] = {
51-
{"native_setup", "()V", (void *)android_os_UEventObserver_native_setup},
52-
{"next_event", "([B)I", (void *)android_os_UEventObserver_next_event},
107+
{ "nativeSetup", "()V",
108+
(void *)nativeSetup },
109+
{ "nativeWaitForNextEvent", "()Ljava/lang/String;",
110+
(void *)nativeWaitForNextEvent },
111+
{ "nativeAddMatch", "(Ljava/lang/String;)V",
112+
(void *)nativeAddMatch },
113+
{ "nativeRemoveMatch", "(Ljava/lang/String;)V",
114+
(void *)nativeRemoveMatch },
53115
};
54116

55117

@@ -64,7 +126,7 @@ int register_android_os_UEventObserver(JNIEnv *env)
64126
}
65127

66128
return AndroidRuntime::registerNativeMethods(env,
67-
"android/os/UEventObserver", gMethods, NELEM(gMethods));
129+
"android/os/UEventObserver", gMethods, NELEM(gMethods));
68130
}
69131

70132
} // namespace android

0 commit comments

Comments
 (0)