Skip to content

Commit 2a49353

Browse files
Adam LesinskiAndroid (Google) Code Review
authored andcommitted
Merge "BatteryStats: Decode wakeup reasons in Java" into mnc-dev
2 parents f8acd1d + 515702c commit 2a49353

2 files changed

Lines changed: 53 additions & 21 deletions

File tree

services/core/java/com/android/server/am/BatteryStatsService.java

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
import java.io.FileDescriptor;
5959
import java.io.IOException;
6060
import java.io.PrintWriter;
61+
import java.nio.ByteBuffer;
62+
import java.nio.CharBuffer;
63+
import java.nio.charset.CharsetDecoder;
64+
import java.nio.charset.CodingErrorAction;
65+
import java.nio.charset.StandardCharsets;
6166
import java.util.List;
6267

6368
/**
@@ -897,7 +902,10 @@ public void enforceCallingPermission() {
897902
}
898903

899904
final class WakeupReasonThread extends Thread {
900-
final String[] mReason = new String[1];
905+
private static final int MAX_REASON_SIZE = 512;
906+
private CharsetDecoder mDecoder;
907+
private ByteBuffer mUtf8Buffer;
908+
private CharBuffer mUtf16Buffer;
901909

902910
WakeupReasonThread() {
903911
super("BatteryStats_wakeupReason");
@@ -906,25 +914,53 @@ final class WakeupReasonThread extends Thread {
906914
public void run() {
907915
Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
908916

917+
mDecoder = StandardCharsets.UTF_8
918+
.newDecoder()
919+
.onMalformedInput(CodingErrorAction.REPLACE)
920+
.onUnmappableCharacter(CodingErrorAction.REPLACE)
921+
.replaceWith("?");
922+
923+
mUtf8Buffer = ByteBuffer.allocateDirect(MAX_REASON_SIZE);
924+
mUtf16Buffer = CharBuffer.allocate(MAX_REASON_SIZE);
925+
909926
try {
910-
int num;
911-
while ((num = nativeWaitWakeup(mReason)) >= 0) {
927+
String reason;
928+
while ((reason = waitWakeup()) != null) {
912929
synchronized (mStats) {
913-
// num will be either 0 or 1.
914-
if (num > 0) {
915-
mStats.noteWakeupReasonLocked(mReason[0]);
916-
} else {
917-
mStats.noteWakeupReasonLocked("unknown");
918-
}
930+
mStats.noteWakeupReasonLocked(reason);
919931
}
920932
}
921933
} catch (RuntimeException e) {
922934
Slog.e(TAG, "Failure reading wakeup reasons", e);
923935
}
924936
}
937+
938+
private String waitWakeup() {
939+
mUtf8Buffer.clear();
940+
mUtf16Buffer.clear();
941+
mDecoder.reset();
942+
943+
int bytesWritten = nativeWaitWakeup(mUtf8Buffer);
944+
if (bytesWritten < 0) {
945+
return null;
946+
} else if (bytesWritten == 0) {
947+
return "unknown";
948+
}
949+
950+
// Set the buffer's limit to the number of bytes written.
951+
mUtf8Buffer.limit(bytesWritten);
952+
953+
// Decode the buffer from UTF-8 to UTF-16.
954+
// Unmappable characters will be replaced.
955+
mDecoder.decode(mUtf8Buffer, mUtf16Buffer, true);
956+
mUtf16Buffer.flip();
957+
958+
// Create a String from the UTF-16 buffer.
959+
return mUtf16Buffer.toString();
960+
}
925961
}
926962

927-
private static native int nativeWaitWakeup(String[] outReason);
963+
private static native int nativeWaitWakeup(ByteBuffer outBuffer);
928964

929965
private void dumpHelp(PrintWriter pw) {
930966
pw.println("Battery stats (batterystats) dump options:");

services/core/jni/com_android_server_am_BatteryStatsService.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ static void wakeup_callback(bool success)
5959
}
6060
}
6161

62-
static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobjectArray outReasons)
62+
static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobject outBuf)
6363
{
64-
if (outReasons == NULL) {
64+
if (outBuf == NULL) {
6565
jniThrowException(env, "java/lang/NullPointerException", "null argument");
6666
return -1;
6767
}
@@ -99,11 +99,11 @@ static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobjectArray outReasons
9999
return -1;
100100
}
101101

102-
ALOGV("Reading wakeup reasons");
102+
char* mergedreason = (char*)env->GetDirectBufferAddress(outBuf);
103+
int remainreasonlen = (int)env->GetDirectBufferCapacity(outBuf);
103104

104-
char mergedreason[MAX_REASON_SIZE];
105+
ALOGV("Reading wakeup reasons");
105106
char* mergedreasonpos = mergedreason;
106-
int remainreasonlen = MAX_REASON_SIZE;
107107
char reasonline[128];
108108
int i = 0;
109109
while (fgets(reasonline, sizeof(reasonline), fp) != NULL) {
@@ -161,21 +161,17 @@ static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobjectArray outReasons
161161
ALOGV("Got %d reasons", i);
162162
if (i > 0) {
163163
*mergedreasonpos = 0;
164-
ScopedLocalRef<jstring> reasonString(env, env->NewStringUTF(mergedreason));
165-
env->SetObjectArrayElement(outReasons, 0, reasonString.get());
166-
i = 1;
167164
}
168165

169166
if (fclose(fp) != 0) {
170167
ALOGE("Failed to close %s", LAST_RESUME_REASON);
171168
return -1;
172169
}
173-
174-
return i;
170+
return mergedreasonpos - mergedreason;
175171
}
176172

177173
static JNINativeMethod method_table[] = {
178-
{ "nativeWaitWakeup", "([Ljava/lang/String;)I", (void*)nativeWaitWakeup },
174+
{ "nativeWaitWakeup", "(Ljava/nio/ByteBuffer;)I", (void*)nativeWaitWakeup },
179175
};
180176

181177
int register_android_server_BatteryStatsService(JNIEnv *env)

0 commit comments

Comments
 (0)