Add reset logic for RN perf counters

Reviewed By: alexeylang

Differential Revision: D13419565

fbshipit-source-id: 0b355ca2b672fd3ac8844e326c57cb5718da04c5
This commit is contained in:
Max Sherman
2018-12-20 10:36:34 -08:00
committed by Facebook Github Bot
parent d7025d2220
commit 8e79a74bc2
4 changed files with 66 additions and 11 deletions
@@ -70,6 +70,15 @@ public class ReactContext extends ContextWrapper {
mJSMessageQueueThread = queueConfig.getJSQueueThread();
}
public void resetPerfStats() {
if (mNativeModulesMessageQueueThread != null) {
mNativeModulesMessageQueueThread.resetPerfStats();
}
if (mJSMessageQueueThread != null) {
mJSMessageQueueThread.resetPerfStats();
}
}
public void setNativeModuleCallExceptionHandler(
@Nullable NativeModuleCallExceptionHandler nativeModuleCallExceptionHandler) {
mNativeModuleCallExceptionHandler = nativeModuleCallExceptionHandler;
@@ -60,9 +60,16 @@ public interface MessageQueueThread {
void quitSynchronous();
/**
* Returns the time in milliseconds at which this thread was started. This
* Returns the perf counters taken when the framework was started. This
* method is intended to be used for instrumentation purposes.
*/
@DoNotStrip
long getStartTimeMillis();
MessageQueueThreadPerfStats getPerfStats();
/**
* Resets the perf counters. This is useful if the RN threads are being re-used.
* This method is intended to be used for instrumentation purposes.
*/
@DoNotStrip
void resetPerfStats();
}
@@ -32,25 +32,25 @@ public class MessageQueueThreadImpl implements MessageQueueThread {
private final Looper mLooper;
private final MessageQueueThreadHandler mHandler;
private final String mAssertionErrorMessage;
private long mStartTimeMillis;
private MessageQueueThreadPerfStats mPerfStats;
private volatile boolean mIsFinished = false;
private MessageQueueThreadImpl(
String name,
Looper looper,
QueueThreadExceptionHandler exceptionHandler) {
this(name, looper, exceptionHandler, -1);
this(name, looper, exceptionHandler, null);
}
private MessageQueueThreadImpl(
String name,
Looper looper,
QueueThreadExceptionHandler exceptionHandler,
long startTimeMillis) {
MessageQueueThreadPerfStats stats) {
mName = name;
mLooper = looper;
mHandler = new MessageQueueThreadHandler(looper, exceptionHandler);
mStartTimeMillis = startTimeMillis;
mPerfStats = stats;
mAssertionErrorMessage = "Expected to be called from the '" + getName() + "' thread!";
}
@@ -139,8 +139,27 @@ public class MessageQueueThreadImpl implements MessageQueueThread {
@DoNotStrip
@Override
public long getStartTimeMillis() {
return mStartTimeMillis;
public MessageQueueThreadPerfStats getPerfStats() {
return mPerfStats;
}
@DoNotStrip
@Override
public void resetPerfStats() {
assignToPerfStats(mPerfStats, -1, -1);
runOnQueue(new Runnable() {
@Override
public void run() {
long wallTime = SystemClock.uptimeMillis();
long cpuTime = SystemClock.currentThreadTimeMillis();
assignToPerfStats(mPerfStats, wallTime, cpuTime);
}
});
}
private static void assignToPerfStats(MessageQueueThreadPerfStats stats, long wall, long cpu) {
stats.wallTime = wall;
stats.cpuTime = cpu;
}
public Looper getLooper() {
@@ -197,7 +216,7 @@ public class MessageQueueThreadImpl implements MessageQueueThread {
final String name,
long stackSize,
QueueThreadExceptionHandler exceptionHandler) {
final SimpleSettableFuture<Pair<Looper, Long>> dataFuture = new SimpleSettableFuture<>();
final SimpleSettableFuture<Pair<Looper, MessageQueueThreadPerfStats>> dataFuture = new SimpleSettableFuture<>();
long startTimeMillis;
Thread bgThread = new Thread(null,
new Runnable() {
@@ -205,13 +224,17 @@ public class MessageQueueThreadImpl implements MessageQueueThread {
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
Looper.prepare();
dataFuture.set(new Pair<>(Looper.myLooper(), SystemClock.uptimeMillis()));
MessageQueueThreadPerfStats stats = new MessageQueueThreadPerfStats();
long wallTime = SystemClock.uptimeMillis();
long cpuTime = SystemClock.currentThreadTimeMillis();
assignToPerfStats(stats, wallTime, cpuTime);
dataFuture.set(new Pair<>(Looper.myLooper(), stats));
Looper.loop();
}
}, "mqt_" + name, stackSize);
bgThread.start();
Pair<Looper, Long> pair = dataFuture.getOrThrow();
Pair<Looper, MessageQueueThreadPerfStats> pair = dataFuture.getOrThrow();
return new MessageQueueThreadImpl(name, pair.first, exceptionHandler, pair.second);
}
}
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.bridge.queue;
/**
* This class holds perf counters' values at the beginning of an RN startup.
*/
public class MessageQueueThreadPerfStats {
public long wallTime;
public long cpuTime;
}