am d92a3957: am eee604ff: am 2a493532: Merge "BatteryStats: Decode wakeup reasons in Java" into mnc-dev

* commit 'd92a39574e3248b0493bfbf49366632141e6e180':
  BatteryStats: Decode wakeup reasons in Java
This commit is contained in:
Adam Lesinski
2015-07-25 16:14:17 +00:00
committed by Android Git Automerger
2 changed files with 53 additions and 21 deletions

View File

@ -58,6 +58,11 @@ import java.io.File;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
/** /**
@ -897,7 +902,10 @@ public final class BatteryStatsService extends IBatteryStats.Stub
} }
final class WakeupReasonThread extends Thread { final class WakeupReasonThread extends Thread {
final String[] mReason = new String[1]; private static final int MAX_REASON_SIZE = 512;
private CharsetDecoder mDecoder;
private ByteBuffer mUtf8Buffer;
private CharBuffer mUtf16Buffer;
WakeupReasonThread() { WakeupReasonThread() {
super("BatteryStats_wakeupReason"); super("BatteryStats_wakeupReason");
@ -906,25 +914,53 @@ public final class BatteryStatsService extends IBatteryStats.Stub
public void run() { public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
mDecoder = StandardCharsets.UTF_8
.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?");
mUtf8Buffer = ByteBuffer.allocateDirect(MAX_REASON_SIZE);
mUtf16Buffer = CharBuffer.allocate(MAX_REASON_SIZE);
try { try {
int num; String reason;
while ((num = nativeWaitWakeup(mReason)) >= 0) { while ((reason = waitWakeup()) != null) {
synchronized (mStats) { synchronized (mStats) {
// num will be either 0 or 1. mStats.noteWakeupReasonLocked(reason);
if (num > 0) {
mStats.noteWakeupReasonLocked(mReason[0]);
} else {
mStats.noteWakeupReasonLocked("unknown");
}
} }
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
Slog.e(TAG, "Failure reading wakeup reasons", e); Slog.e(TAG, "Failure reading wakeup reasons", e);
} }
} }
private String waitWakeup() {
mUtf8Buffer.clear();
mUtf16Buffer.clear();
mDecoder.reset();
int bytesWritten = nativeWaitWakeup(mUtf8Buffer);
if (bytesWritten < 0) {
return null;
} else if (bytesWritten == 0) {
return "unknown";
}
// Set the buffer's limit to the number of bytes written.
mUtf8Buffer.limit(bytesWritten);
// Decode the buffer from UTF-8 to UTF-16.
// Unmappable characters will be replaced.
mDecoder.decode(mUtf8Buffer, mUtf16Buffer, true);
mUtf16Buffer.flip();
// Create a String from the UTF-16 buffer.
return mUtf16Buffer.toString();
}
} }
private static native int nativeWaitWakeup(String[] outReason); private static native int nativeWaitWakeup(ByteBuffer outBuffer);
private void dumpHelp(PrintWriter pw) { private void dumpHelp(PrintWriter pw) {
pw.println("Battery stats (batterystats) dump options:"); pw.println("Battery stats (batterystats) dump options:");

View File

@ -59,9 +59,9 @@ static void wakeup_callback(bool success)
} }
} }
static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobjectArray outReasons) static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobject outBuf)
{ {
if (outReasons == NULL) { if (outBuf == NULL) {
jniThrowException(env, "java/lang/NullPointerException", "null argument"); jniThrowException(env, "java/lang/NullPointerException", "null argument");
return -1; return -1;
} }
@ -99,11 +99,11 @@ static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobjectArray outReasons
return -1; return -1;
} }
ALOGV("Reading wakeup reasons"); char* mergedreason = (char*)env->GetDirectBufferAddress(outBuf);
int remainreasonlen = (int)env->GetDirectBufferCapacity(outBuf);
char mergedreason[MAX_REASON_SIZE]; ALOGV("Reading wakeup reasons");
char* mergedreasonpos = mergedreason; char* mergedreasonpos = mergedreason;
int remainreasonlen = MAX_REASON_SIZE;
char reasonline[128]; char reasonline[128];
int i = 0; int i = 0;
while (fgets(reasonline, sizeof(reasonline), fp) != NULL) { while (fgets(reasonline, sizeof(reasonline), fp) != NULL) {
@ -161,21 +161,17 @@ static jint nativeWaitWakeup(JNIEnv *env, jobject clazz, jobjectArray outReasons
ALOGV("Got %d reasons", i); ALOGV("Got %d reasons", i);
if (i > 0) { if (i > 0) {
*mergedreasonpos = 0; *mergedreasonpos = 0;
ScopedLocalRef<jstring> reasonString(env, env->NewStringUTF(mergedreason));
env->SetObjectArrayElement(outReasons, 0, reasonString.get());
i = 1;
} }
if (fclose(fp) != 0) { if (fclose(fp) != 0) {
ALOGE("Failed to close %s", LAST_RESUME_REASON); ALOGE("Failed to close %s", LAST_RESUME_REASON);
return -1; return -1;
} }
return mergedreasonpos - mergedreason;
return i;
} }
static JNINativeMethod method_table[] = { static JNINativeMethod method_table[] = {
{ "nativeWaitWakeup", "([Ljava/lang/String;)I", (void*)nativeWaitWakeup }, { "nativeWaitWakeup", "(Ljava/nio/ByteBuffer;)I", (void*)nativeWaitWakeup },
}; };
int register_android_server_BatteryStatsService(JNIEnv *env) int register_android_server_BatteryStatsService(JNIEnv *env)