Merge "Stop deferring wake lock logging." into sc-dev

This commit is contained in:
Michael Wright
2021-05-26 16:36:08 +00:00
committed by Android (Google) Code Review
2 changed files with 30 additions and 97 deletions

View File

@@ -16,16 +16,12 @@
package com.android.server.power; package com.android.server.power;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager; import android.os.PowerManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Slog; import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread; import com.android.internal.os.BackgroundThread;
import com.android.internal.os.SomeArgs;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@@ -74,8 +70,6 @@ final class WakeLockLog {
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private static final int MSG_ON_WAKE_LOCK_EVENT = 1;
private static final int TYPE_TIME_RESET = 0x0; private static final int TYPE_TIME_RESET = 0x0;
private static final int TYPE_ACQUIRE = 0x1; private static final int TYPE_ACQUIRE = 0x1;
private static final int TYPE_RELEASE = 0x2; private static final int TYPE_RELEASE = 0x2;
@@ -130,7 +124,6 @@ final class WakeLockLog {
private final Injector mInjector; private final Injector mInjector;
private final TheLog mLog; private final TheLog mLog;
private final TagDatabase mTagDatabase; private final TagDatabase mTagDatabase;
private final Handler mHandler;
private final SimpleDateFormat mDumpsysDateFormat; private final SimpleDateFormat mDumpsysDateFormat;
WakeLockLog() { WakeLockLog() {
@@ -140,7 +133,6 @@ final class WakeLockLog {
@VisibleForTesting @VisibleForTesting
WakeLockLog(Injector injector) { WakeLockLog(Injector injector) {
mInjector = injector; mInjector = injector;
mHandler = new WakeLockLogHandler(injector.getLooper());
mTagDatabase = new TagDatabase(injector); mTagDatabase = new TagDatabase(injector);
EntryByteTranslator translator = new EntryByteTranslator(mTagDatabase); EntryByteTranslator translator = new EntryByteTranslator(mTagDatabase);
mLog = new TheLog(injector, translator, mTagDatabase); mLog = new TheLog(injector, translator, mTagDatabase);
@@ -172,7 +164,7 @@ final class WakeLockLog {
* Dumps all the wake lock data currently saved in the wake lock log to the specified * Dumps all the wake lock data currently saved in the wake lock log to the specified
* {@code PrintWriter}. * {@code PrintWriter}.
* *
* @param The {@code PrintWriter} to write to. * @param pw The {@code PrintWriter} to write to.
*/ */
public void dump(PrintWriter pw) { public void dump(PrintWriter pw) {
dump(pw, false); dump(pw, false);
@@ -221,9 +213,6 @@ final class WakeLockLog {
/** /**
* Adds a new entry to the log based on the specified wake lock parameters. * Adds a new entry to the log based on the specified wake lock parameters.
* *
* Grabs the current time for the event and then posts the rest of the logic (actually
* adding it to the log) to a background thread.
*
* @param eventType The type of event (ACQUIRE, RELEASE); * @param eventType The type of event (ACQUIRE, RELEASE);
* @param tag The wake lock's identifying tag. * @param tag The wake lock's identifying tag.
* @param ownerUid The owner UID of the wake lock. * @param ownerUid The owner UID of the wake lock.
@@ -239,15 +228,11 @@ final class WakeLockLog {
} }
final long time = mInjector.currentTimeMillis(); final long time = mInjector.currentTimeMillis();
final int translatedFlags = eventType == TYPE_ACQUIRE
SomeArgs args = SomeArgs.obtain(); ? translateFlagsFromPowerManager(flags)
args.arg1 = tagNameReducer(tag); : 0;
args.argi1 = eventType; handleWakeLockEventInternal(eventType, tagNameReducer(tag), ownerUid, translatedFlags,
args.argi2 = ownerUid; time);
args.argi3 = eventType == TYPE_ACQUIRE ? translateFlagsFromPowerManager(flags) : 0;
args.argi4 = (int) ((time >> 32) & 0xFFFFFFFFL);
args.argi5 = (int) (time & 0xFFFFFFFFL);
mHandler.obtainMessage(MSG_ON_WAKE_LOCK_EVENT, args).sendToTarget();
} }
/** /**
@@ -273,8 +258,8 @@ final class WakeLockLog {
* flags, {@code WakeLockLog.FLAG_*}, and a log-level, {@code WakeLockLog.LEVEL_*}. * flags, {@code WakeLockLog.FLAG_*}, and a log-level, {@code WakeLockLog.LEVEL_*}.
* *
* @param flags Wake lock flags including {@code PowerManager.*_WAKE_LOCK} * @param flags Wake lock flags including {@code PowerManager.*_WAKE_LOCK}
* {@link PowerManager.ACQUIRE_CAUSES_WAKEUP}, and * {@link PowerManager#ACQUIRE_CAUSES_WAKEUP}, and
* {@link PowerManager.ON_AFTER_RELEASE}. * {@link PowerManager#ON_AFTER_RELEASE}.
* @return The compressed flags value. * @return The compressed flags value.
*/ */
int translateFlagsFromPowerManager(int flags) { int translateFlagsFromPowerManager(int flags) {
@@ -328,9 +313,9 @@ final class WakeLockLog {
} }
String reduciblePrefix = null; String reduciblePrefix = null;
for (int tp = 0; tp < REDUCED_TAG_PREFIXES.length; tp++) { for (String reducedTagPrefix : REDUCED_TAG_PREFIXES) {
if (tag.startsWith(REDUCED_TAG_PREFIXES[tp])) { if (tag.startsWith(reducedTagPrefix)) {
reduciblePrefix = REDUCED_TAG_PREFIXES[tp]; reduciblePrefix = reducedTagPrefix;
break; break;
} }
} }
@@ -339,7 +324,7 @@ final class WakeLockLog {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
// add prefix first // add prefix first
sb.append(tag.substring(0, reduciblePrefix.length())); sb.append(tag, 0, reduciblePrefix.length());
// Stop looping on final marker // Stop looping on final marker
final int end = Math.max(tag.lastIndexOf("/"), tag.lastIndexOf(".")); final int end = Math.max(tag.lastIndexOf("/"), tag.lastIndexOf("."));
@@ -604,7 +589,7 @@ final class WakeLockLog {
* @return The number of bytes written to buffer, or required to write to the buffer. * @return The number of bytes written to buffer, or required to write to the buffer.
*/ */
int toBytes(LogEntry entry, byte[] bytes, long timeReference) { int toBytes(LogEntry entry, byte[] bytes, long timeReference) {
int sizeNeeded = -1; final int sizeNeeded;
switch (entry.type) { switch (entry.type) {
case TYPE_ACQUIRE: { case TYPE_ACQUIRE: {
sizeNeeded = 3; sizeNeeded = 3;
@@ -696,8 +681,9 @@ final class WakeLockLog {
* {@link EntryByteTranslator} to convert byte {@link LogEntry} to bytes within the buffer. * {@link EntryByteTranslator} to convert byte {@link LogEntry} to bytes within the buffer.
* *
* This class also implements the logic around TIME_RESET events. Since the LogEntries store * This class also implements the logic around TIME_RESET events. Since the LogEntries store
* their time (8-bit) relative to the previous event, this class can add {@link TYPE_TIME_RESET} * their time (8-bit) relative to the previous event, this class can add
* LogEntries as necessary to allow a LogEntry's relative time to fit within that range. * {@link #TYPE_TIME_RESET} LogEntries as necessary to allow a LogEntry's relative time to fit
* within that range.
*/ */
static class TheLog { static class TheLog {
private final EntryByteTranslator mTranslator; private final EntryByteTranslator mTranslator;
@@ -711,7 +697,7 @@ final class WakeLockLog {
/** /**
* Second temporary buffer used when reading and writing bytes from the buffer. * Second temporary buffer used when reading and writing bytes from the buffer.
* A second temporary buffer is necessary since additional items can be read concurrently * A second temporary buffer is necessary since additional items can be read concurrently
* from {@link mTempBuffer}. E.g., Adding an entry to a full buffer requires removing * from {@link #mTempBuffer}. E.g., Adding an entry to a full buffer requires removing
* other entries from the buffer. * other entries from the buffer.
*/ */
private final byte[] mReadWriteTempBuffer = new byte[MAX_LOG_ENTRY_BYTE_SIZE]; private final byte[] mReadWriteTempBuffer = new byte[MAX_LOG_ENTRY_BYTE_SIZE];
@@ -832,7 +818,7 @@ final class WakeLockLog {
* Returns an {@link Iterator} of {@link LogEntry}s for all the entries in the log. * Returns an {@link Iterator} of {@link LogEntry}s for all the entries in the log.
* *
* If the log is modified while the entries are being read, the iterator will throw a * If the log is modified while the entries are being read, the iterator will throw a
* {@link ConcurrentModificationExceptoin}. * {@link ConcurrentModificationException}.
* *
* @param tempEntry A temporary {@link LogEntry} instance to use so that new instances * @param tempEntry A temporary {@link LogEntry} instance to use so that new instances
* aren't allocated with every call to {@code Iterator.next}. * aren't allocated with every call to {@code Iterator.next}.
@@ -1063,7 +1049,7 @@ final class WakeLockLog {
* instanced into bytes. * instanced into bytes.
* *
* If a new tag is added when the database is full, the oldest tag is removed. The oldest tag * If a new tag is added when the database is full, the oldest tag is removed. The oldest tag
* is calcualted using {@link TagData.lastUsedTime}. * is calculated using {@link TagData#lastUsedTime}.
*/ */
static class TagDatabase { static class TagDatabase {
private final int mInvalidIndex; private final int mInvalidIndex;
@@ -1086,9 +1072,9 @@ final class WakeLockLog {
int byteEstimate = 0; int byteEstimate = 0;
int tagSize = 0; int tagSize = 0;
int tags = 0; int tags = 0;
for (int i = 0; i < mArray.length; i++) { for (TagData tagData : mArray) {
byteEstimate += 8; // reference pointer byteEstimate += 8; // reference pointer
TagData data = mArray[i]; TagData data = tagData;
if (data != null) { if (data != null) {
entries++; entries++;
byteEstimate += data.getByteSize(); byteEstimate += data.getByteSize();
@@ -1280,10 +1266,6 @@ final class WakeLockLog {
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder();
if (DEBUG) {
sb.append("(").append(index).append(")");
}
return "[" + ownerUid + " ; " + tag + "]"; return "[" + ownerUid + " ; " + tag + "]";
} }
@@ -1308,10 +1290,6 @@ final class WakeLockLog {
* Injector used by {@link WakeLockLog} for testing purposes. * Injector used by {@link WakeLockLog} for testing purposes.
*/ */
public static class Injector { public static class Injector {
public Looper getLooper() {
return BackgroundThread.get().getLooper();
}
public int getTagDatabaseSize() { public int getTagDatabaseSize() {
return TAG_DATABASE_SIZE; return TAG_DATABASE_SIZE;
} }
@@ -1328,28 +1306,4 @@ final class WakeLockLog {
return DATE_FORMAT; return DATE_FORMAT;
} }
} }
private class WakeLockLogHandler extends Handler {
WakeLockLogHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message message) {
switch(message.what) {
case MSG_ON_WAKE_LOCK_EVENT:
final SomeArgs args = (SomeArgs) message.obj;
final String tag = (String) args.arg1;
final int eventType = args.argi1;
final int ownerUid = args.argi2;
final int flags = args.argi3;
final long time = (((long) args.argi4) << 32) + (args.argi5 & 0xFFFFFFFFL);
args.recycle();
handleWakeLockEventInternal(eventType, tag, ownerUid, flags, time);
break;
default:
break;
}
}
}
} }

View File

@@ -20,12 +20,8 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.os.Looper;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.test.TestLooper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.PrintWriter; import java.io.PrintWriter;
@@ -38,17 +34,6 @@ import java.util.TimeZone;
*/ */
public class WakeLockLogTest { public class WakeLockLogTest {
private TestLooper mTestLooper;
@Before
public void setUp() throws Exception {
mTestLooper = new TestLooper();
}
@After
public void tearDown() throws Exception {
}
@Test @Test
public void testAddTwoItems() { public void testAddTwoItems() {
final int tagDatabaseSize = 128; final int tagDatabaseSize = 128;
@@ -70,7 +55,7 @@ public class WakeLockLogTest {
+ " -\n" + " -\n"
+ " Events: 2, Time-Resets: 0\n" + " Events: 2, Time-Resets: 0\n"
+ " Buffer, Bytes used: 6\n", + " Buffer, Bytes used: 6\n",
dispatchAndDump(log, false)); dumpLog(log, false));
} }
@Test @Test
@@ -92,7 +77,7 @@ public class WakeLockLogTest {
+ " -\n" + " -\n"
+ " Events: 2, Time-Resets: 1\n" + " Events: 2, Time-Resets: 1\n"
+ " Buffer, Bytes used: 15\n", + " Buffer, Bytes used: 15\n",
dispatchAndDump(log, false)); dumpLog(log, false));
} }
@Test @Test
@@ -114,7 +99,7 @@ public class WakeLockLogTest {
+ " -\n" + " -\n"
+ " Events: 2, Time-Resets: 0\n" + " Events: 2, Time-Resets: 0\n"
+ " Buffer, Bytes used: 6\n", + " Buffer, Bytes used: 6\n",
dispatchAndDump(log, false)); dumpLog(log, false));
} }
@Test @Test
@@ -142,7 +127,7 @@ public class WakeLockLogTest {
+ " -\n" + " -\n"
+ " Events: 3, Time-Resets: 0\n" + " Events: 3, Time-Resets: 0\n"
+ " Buffer, Bytes used: 9\n", + " Buffer, Bytes used: 9\n",
dispatchAndDump(log, false)); dumpLog(log, false));
} }
@Test @Test
@@ -160,7 +145,7 @@ public class WakeLockLogTest {
+ " -\n" + " -\n"
+ " Events: 0, Time-Resets: 0\n" + " Events: 0, Time-Resets: 0\n"
+ " Buffer, Bytes used: 0\n", + " Buffer, Bytes used: 0\n",
dispatchAndDump(log, false)); dumpLog(log, false));
} }
@Test @Test
@@ -179,7 +164,7 @@ public class WakeLockLogTest {
+ " -\n" + " -\n"
+ " Events: 1, Time-Resets: 0\n" + " Events: 1, Time-Resets: 0\n"
+ " Buffer, Bytes used: 3\n", + " Buffer, Bytes used: 3\n",
dispatchAndDump(log, false)); dumpLog(log, false));
} }
@Test @Test
@@ -201,7 +186,7 @@ public class WakeLockLogTest {
+ " Events: 2, Time-Resets: 0\n" + " Events: 2, Time-Resets: 0\n"
+ " Buffer, Bytes used: 5\n" + " Buffer, Bytes used: 5\n"
+ " Tag Database: size(5), entries: 1, Bytes used: 80\n", + " Tag Database: size(5), entries: 1, Bytes used: 80\n",
dispatchAndDump(log, true)); dumpLog(log, true));
} }
@Test @Test
@@ -223,11 +208,10 @@ public class WakeLockLogTest {
+ " -\n" + " -\n"
+ " Events: 1, Time-Resets: 0\n" + " Events: 1, Time-Resets: 0\n"
+ " Buffer, Bytes used: 3\n", + " Buffer, Bytes used: 3\n",
dispatchAndDump(log, false)); dumpLog(log, false));
} }
private String dispatchAndDump(WakeLockLog log, boolean includeTagDb) { private String dumpLog(WakeLockLog log, boolean includeTagDb) {
mTestLooper.dispatchAll();
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
log.dump(pw, includeTagDb); log.dump(pw, includeTagDb);
@@ -243,11 +227,6 @@ public class WakeLockLogTest {
mLogSize = logSize; mLogSize = logSize;
} }
@Override
public Looper getLooper() {
return mTestLooper.getLooper();
}
@Override @Override
public int getTagDatabaseSize() { public int getTagDatabaseSize() {
return mTagDatabaseSize; return mTagDatabaseSize;