Skip to content

Commit 2330843

Browse files
johnislarryfacebook-github-bot
authored andcommitted
Add API for instrumentation to learn the starting wall time of bg threads
Summary: Computing things like "page faults since this thread was created" or "cpu time spent on this thread since it was created" is pretty easy - just measure it when you want it. However, if you want to know "how much wall time has elapsed since this thread was created" you need to record some timing info when the thread is created. This diff adds a an API for querying that from the RN thread holder abstraction. Reviewed By: alexeylang Differential Revision: D13246235 fbshipit-source-id: d36af61dbe27f662980fe508b2644e9d5255bb7e
1 parent 9e0b791 commit 2330843

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/queue/MessageQueueThread.java

+7
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ public interface MessageQueueThread {
5858
*/
5959
@DoNotStrip
6060
void quitSynchronous();
61+
62+
/**
63+
* Returns the time in milliseconds at which this thread was started. This
64+
* method is intended to be used for instrumentation purposes.
65+
*/
66+
@DoNotStrip
67+
long getStartTimeMillis();
6168
}

ReactAndroid/src/main/java/com/facebook/react/bridge/queue/MessageQueueThreadImpl.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import java.util.concurrent.Callable;
1111
import java.util.concurrent.Future;
1212

13+
import android.os.SystemClock;
1314
import android.os.Looper;
1415
import android.os.Process;
15-
16+
import android.util.Pair;
1617
import com.facebook.common.logging.FLog;
1718
import com.facebook.proguard.annotations.DoNotStrip;
1819
import com.facebook.react.bridge.AssertionException;
@@ -31,15 +32,25 @@ public class MessageQueueThreadImpl implements MessageQueueThread {
3132
private final Looper mLooper;
3233
private final MessageQueueThreadHandler mHandler;
3334
private final String mAssertionErrorMessage;
35+
private long mStartTimeMillis;
3436
private volatile boolean mIsFinished = false;
3537

3638
private MessageQueueThreadImpl(
3739
String name,
3840
Looper looper,
3941
QueueThreadExceptionHandler exceptionHandler) {
42+
this(name, looper, exceptionHandler, -1);
43+
}
44+
45+
private MessageQueueThreadImpl(
46+
String name,
47+
Looper looper,
48+
QueueThreadExceptionHandler exceptionHandler,
49+
long startTimeMillis) {
4050
mName = name;
4151
mLooper = looper;
4252
mHandler = new MessageQueueThreadHandler(looper, exceptionHandler);
53+
mStartTimeMillis = startTimeMillis;
4354
mAssertionErrorMessage = "Expected to be called from the '" + getName() + "' thread!";
4455
}
4556

@@ -126,6 +137,12 @@ public void quitSynchronous() {
126137
}
127138
}
128139

140+
@DoNotStrip
141+
@Override
142+
public long getStartTimeMillis() {
143+
return mStartTimeMillis;
144+
}
145+
129146
public Looper getLooper() {
130147
return mLooper;
131148
}
@@ -180,21 +197,21 @@ private static MessageQueueThreadImpl startNewBackgroundThread(
180197
final String name,
181198
long stackSize,
182199
QueueThreadExceptionHandler exceptionHandler) {
183-
final SimpleSettableFuture<Looper> looperFuture = new SimpleSettableFuture<>();
200+
final SimpleSettableFuture<Pair<Looper, Long>> dataFuture = new SimpleSettableFuture<>();
201+
long startTimeMillis;
184202
Thread bgThread = new Thread(null,
185203
new Runnable() {
186204
@Override
187205
public void run() {
188206
Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
189207
Looper.prepare();
190-
191-
looperFuture.set(Looper.myLooper());
208+
dataFuture.set(new Pair<>(Looper.myLooper(), SystemClock.uptimeMillis()));
192209
Looper.loop();
193210
}
194211
}, "mqt_" + name, stackSize);
195212
bgThread.start();
196213

197-
Looper myLooper = looperFuture.getOrThrow();
198-
return new MessageQueueThreadImpl(name, myLooper, exceptionHandler);
214+
Pair<Looper, Long> pair = dataFuture.getOrThrow();
215+
return new MessageQueueThreadImpl(name, pair.first, exceptionHandler, pair.second);
199216
}
200217
}

0 commit comments

Comments
 (0)