Skip to content

Commit

Permalink
Add reset logic for RN perf counters
Browse files Browse the repository at this point in the history
Reviewed By: alexeylang

Differential Revision: D13419565

fbshipit-source-id: 0b355ca2b672fd3ac8844e326c57cb5718da04c5
  • Loading branch information
johnislarry authored and facebook-github-bot committed Dec 20, 2018
1 parent d7025d2 commit 8e79a74
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public void initializeWithInstance(CatalystInstance catalystInstance) {
mJSMessageQueueThread = queueConfig.getJSQueueThread();
}

public void resetPerfStats() {
if (mNativeModulesMessageQueueThread != null) {
mNativeModulesMessageQueueThread.resetPerfStats();
}
if (mJSMessageQueueThread != null) {
mJSMessageQueueThread.resetPerfStats();
}
}

public void setNativeModuleCallExceptionHandler(
@Nullable NativeModuleCallExceptionHandler nativeModuleCallExceptionHandler) {
mNativeModuleCallExceptionHandler = nativeModuleCallExceptionHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
}

Expand Down Expand Up @@ -139,8 +139,27 @@ public void quitSynchronous() {

@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() {
Expand Down Expand Up @@ -197,21 +216,25 @@ private static MessageQueueThreadImpl startNewBackgroundThread(
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() {
@Override
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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8e79a74

Please sign in to comment.