@Override public void onProducerStart(String requestId, String producerName) { if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) { return; } StringBuilder entryName = new StringBuilder(); entryName.append("FRESCO_PRODUCER_"); entryName.append(producerName.replace(':', '_')); Pair<Integer,String> requestPair = Pair.create(mCurrentID, entryName.toString()); Systrace.beginAsyncSection( Systrace.TRACE_TAG_REACT_FRESCO, requestPair.second, mCurrentID); mProducerID.put(requestId, requestPair); mCurrentID++; }
@Override public void onProducerFinishWithSuccess( String requestId, String producerName, Map<String, String> extraMap) { if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) { return; } if (mProducerID.containsKey(requestId)) { Pair<Integer, String> entry = mProducerID.get(requestId); Systrace.endAsyncSection( Systrace.TRACE_TAG_REACT_FRESCO, entry.second, entry.first); mProducerID.remove(requestId); } }
@Override public void onProducerFinishWithFailure( String requestId, String producerName, Throwable throwable, Map<String, String> extraMap) { if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) { return; } if (mProducerID.containsKey(requestId)) { Pair<Integer, String> entry = mProducerID.get(requestId); Systrace.endAsyncSection( Systrace.TRACE_TAG_REACT_FRESCO, entry.second, entry.first); mProducerID.remove(requestId); } }
@Override public void onProducerFinishWithCancellation( String requestId, String producerName, Map<String, String> extraMap) { if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) { return; } if (mProducerID.containsKey(requestId)) { Pair<Integer, String> entry = mProducerID.get(requestId); Systrace.endAsyncSection( Systrace.TRACE_TAG_REACT_FRESCO, entry.second, entry.first); mProducerID.remove(requestId); } }
@Override public void onProducerEvent(String requestId, String producerName, String producerEventName) { if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) { return; } StringBuilder entryName = new StringBuilder(); entryName.append("FRESCO_PRODUCER_EVENT_"); entryName.append(requestId.replace(':', '_')); entryName.append("_"); entryName.append(producerName.replace(':', '_')); entryName.append("_"); entryName.append(producerEventName.replace(':', '_')); Systrace.traceInstant( Systrace.TRACE_TAG_REACT_FRESCO, entryName.toString(), Systrace.EventScope.THREAD); }
@Override public void onRequestStart( ImageRequest request, Object callerContext, String requestId, boolean isPrefetch) { if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) { return; } StringBuilder entryName = new StringBuilder(); entryName.append("FRESCO_REQUEST_"); entryName.append(request.getSourceUri().toString().replace(':', '_')); Pair<Integer,String> requestPair = Pair.create(mCurrentID, entryName.toString()); Systrace.beginAsyncSection( Systrace.TRACE_TAG_REACT_FRESCO, requestPair.second, mCurrentID); mRequestsID.put(requestId, requestPair); mCurrentID++; }
@Override public void onRequestFailure( ImageRequest request, String requestId, Throwable throwable, boolean isPrefetch) { if (!Systrace.isTracing(Systrace.TRACE_TAG_REACT_FRESCO)) { return; } if (mRequestsID.containsKey(requestId)) { Pair<Integer, String> entry = mRequestsID.get(requestId); Systrace.endAsyncSection( Systrace.TRACE_TAG_REACT_FRESCO, entry.second, entry.first); mRequestsID.remove(requestId); } }
@Override public final List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { List<NativeModule> modules = new ArrayList<>(); for (ModuleSpec holder : getNativeModules(reactContext)) { NativeModule nativeModule; SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createNativeModule") .arg("module", holder.getType()) .flush(); try { ReactMarker.logMarker( ReactMarkerConstants.CREATE_MODULE_START, holder.getType().getSimpleName()); nativeModule = holder.getProvider().get(); ReactMarker.logMarker(ReactMarkerConstants.CREATE_MODULE_END); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } modules.add(nativeModule); } return modules; }
/** * Uses configured {@link ReactPackage} instances to create all view managers. */ public List<ViewManager> createAllViewManagers( ReactApplicationContext catalystApplicationContext) { ReactMarker.logMarker(CREATE_VIEW_MANAGERS_START); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAllViewManagers"); try { List<ViewManager> allViewManagers = new ArrayList<>(); for (ReactPackage reactPackage : mPackages) { allViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext)); } return allViewManagers; } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_VIEW_MANAGERS_END); } }
private void attachMeasuredRootViewToInstance( ReactRootView rootView, CatalystInstance catalystInstance) { Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance"); UiThreadUtil.assertOnUiThread(); // Reset view content as it's going to be populated by the application content from JS rootView.removeAllViews(); rootView.setId(View.NO_ID); UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); int rootTag = uiManagerModule.addMeasuredRootView(rootView); rootView.setRootViewTag(rootTag); @Nullable Bundle launchOptions = rootView.getLaunchOptions(); WritableMap initialProps = Arguments.makeNativeMap(launchOptions); String jsAppModuleName = rootView.getJSModuleName(); WritableNativeMap appParams = new WritableNativeMap(); appParams.putDouble("rootTag", rootTag); appParams.putMap("initialProps", initialProps); catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); rootView.onAttachedToReactInstance(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); }
private void processPackage( ReactPackage reactPackage, NativeModuleRegistryBuilder nativeModuleRegistryBuilder, JavaScriptModuleRegistry.Builder jsModulesBuilder) { SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "processPackage") .arg("className", reactPackage.getClass().getSimpleName()) .flush(); if (reactPackage instanceof ReactPackageLogger) { ((ReactPackageLogger) reactPackage).startProcessPackage(); } nativeModuleRegistryBuilder.processPackage(reactPackage); for (Class<? extends JavaScriptModule> jsModuleClass : reactPackage.createJSModules()) { jsModulesBuilder.add(jsModuleClass); } if (reactPackage instanceof ReactPackageLogger) { ((ReactPackageLogger) reactPackage).endProcessPackage(); } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); }
@Override public void doFrameGuarded(long frameTimeNanos) { if (mIsInIllegalUIState) { FLog.w( ReactConstants.TAG, "Not flushing pending UI operations because of previously thrown Exception"); return; } Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "dispatchNonBatchedUIOperations"); try { dispatchPendingNonBatchedOperations(frameTimeNanos); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } flushPendingBatches(); ReactChoreographer.getInstance().postFrameCallback( ReactChoreographer.CallbackType.DISPATCH_UI, this); }
/** * Sends the given Event to JS, coalescing eligible events if JS is backed up. */ public void dispatchEvent(Event event) { Assertions.assertCondition(event.isInitialized(), "Dispatched event hasn't been initialized"); for (EventDispatcherListener listener : mListeners) { listener.onEventDispatch(event); } synchronized (mEventsStagingLock) { mEventStaging.add(event); Systrace.startAsyncFlow( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, event.getEventName(), event.getUniqueID()); } if (mRCTEventEmitter != null) { // If the host activity is paused, the frame callback may not be currently // posted. Ensure that it is so that this event gets delivered promptly. mCurrentFrameCallback.maybePostFromNonUI(); } else { // No JS application has started yet, or resumed. This can happen when a ReactRootView is // added to view hierarchy, but ReactContext creation has not completed yet. In this case, any // touch event dispatch will hit this codepath, and we simply queue them so that they // are dispatched once ReactContext creation completes and JS app is running. } }
@Override public void doFrame(long frameTimeNanos) { UiThreadUtil.assertOnUiThread(); if (mShouldStop) { mIsPosted = false; } else { post(); } Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ScheduleDispatchFrameCallback"); try { moveStagedEventsToDispatchQueue(); if (mEventsToDispatchSize > 0 && !mHasDispatchScheduled) { mHasDispatchScheduled = true; Systrace.startAsyncFlow( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ScheduleDispatchFrameCallback", mHasDispatchScheduledCount); mReactContext.runOnJSQueueThread(mDispatchEventsRunnable); } } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } }
private NativeModule create() { SoftAssertions.assertCondition(mModule == null, "Creating an already created module."); ReactMarker.logMarker(CREATE_MODULE_START, mName); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createModule") .arg("name", mName) .flush(); NativeModule module = assertNotNull(mProvider).get(); mProvider = null; if (mInitializeNeeded) { doInitialize(module); mInitializeNeeded = false; } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_MODULE_END); return module; }
@Override public void runJSBundle() { Assertions.assertCondition(!mJSBundleHasLoaded, "JS bundle was already loaded!"); mJSBundleHasLoaded = true; // incrementPendingJSCalls(); mJSBundleLoader.loadScript(CatalystInstanceImpl.this); synchronized (mJSCallsPendingInitLock) { // Loading the bundle is queued on the JS thread, but may not have // run yet. It's safe to set this here, though, since any work it // gates will be queued on the JS thread behind the load. mAcceptCalls = true; for (PendingJSCall call : mJSCallsPendingInit) { jniCallJSFunction(call.mExecutorToken, call.mModule, call.mMethod, call.mArguments); } mJSCallsPendingInit.clear(); } // This is registered after JS starts since it makes a JS call Systrace.registerListener(mTraceListener); }
private void incrementPendingJSCalls() { int oldPendingCalls = mPendingJSCalls.getAndIncrement(); boolean wasIdle = oldPendingCalls == 0; Systrace.traceCounter( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, mJsPendingCallsTitleForTrace, oldPendingCalls + 1); if (wasIdle && !mBridgeIdleListeners.isEmpty()) { mNativeModulesQueueThread.runOnQueue(new Runnable() { @Override public void run() { for (NotThreadSafeBridgeIdleDebugListener listener : mBridgeIdleListeners) { listener.onTransitionToBridgeBusy(); } } }); } }
private void decrementPendingJSCalls() { int newPendingCalls = mPendingJSCalls.decrementAndGet(); // TODO(9604406): handle case of web workers injecting messages to main thread //Assertions.assertCondition(newPendingCalls >= 0); boolean isNowIdle = newPendingCalls == 0; Systrace.traceCounter( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, mJsPendingCallsTitleForTrace, newPendingCalls); if (isNowIdle && !mBridgeIdleListeners.isEmpty()) { mNativeModulesQueueThread.runOnQueue(new Runnable() { @Override public void run() { for (NotThreadSafeBridgeIdleDebugListener listener : mBridgeIdleListeners) { listener.onTransitionToBridgeIdle(); } } }); } }
void notifyJSInstanceInitialized() { mReactApplicationContext.assertOnNativeModulesQueueThread("From version React Native v0.44, " + "native modules are explicitly not initialized on the UI thread. See " + "https://github.com/facebook/react-native/wiki/Breaking-Changes#d4611211-reactnativeandroidbreaking-move-nativemodule-initialization-off-ui-thread---aaachiuuu " + " for more details."); ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_START); Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "NativeModuleRegistry_notifyJSInstanceInitialized"); try { for (ModuleHolder module : mModules.values()) { module.initialize(); } } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_END); } }
private void processArguments() { if (mArgumentsProcessed) { return; } SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "processArguments") .arg("method", mModuleWrapper.getName() + "." + mMethod.getName()) .flush(); mArgumentsProcessed = true; mArgumentExtractors = buildArgumentExtractors(mParameterTypes); mSignature = buildSignature(mMethod, mParameterTypes, (mType.equals(BaseJavaModule.METHOD_TYPE_SYNC))); // Since native methods are invoked from a message queue executed on a single thread, it is // safe to allocate only one arguments object per method that can be reused across calls mArguments = new Object[mParameterTypes.length]; mJSArgumentsNeeded = calculateJSArgumentsNeeded(); com.facebook.systrace.Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); }
@Override public List<Class<? extends JavaScriptModule>> createJSModules() { List<Class<? extends JavaScriptModule>> jsModules = new ArrayList<>(Arrays.asList( DeviceEventManagerModule.RCTDeviceEventEmitter.class, JSTimersExecution.class, RCTEventEmitter.class, RCTNativeAppEventEmitter.class, AppRegistry.class, com.facebook.react.bridge.Systrace.class, HMRClient.class)); if (ReactBuildConfig.DEBUG) { jsModules.add(DebugComponentOwnershipModule.RCTDebugComponentOwnership.class); jsModules.add(JSCHeapCapture.HeapCapture.class); jsModules.add(JSCSamplingProfiler.SamplingProfiler.class); } return jsModules; }
private UIManagerModule createUIManager(ReactApplicationContext reactContext) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); try { List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers( reactContext); return new UIManagerModule( reactContext, viewManagersList, mUIImplementationProvider, mLazyViewManagersEnabled); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END); } }
public JavaMethod(Method method, boolean isSync) { mMethod = method; mMethod.setAccessible(true); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "callGetParameterTypes"); mParameterTypes = mMethod.getParameterTypes(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); mParamLength = mParameterTypes.length; if (isSync) { mType = METHOD_TYPE_SYNC; } else if (mParamLength > 0 && (mParameterTypes[mParamLength - 1] == Promise.class)) { mType = METHOD_TYPE_PROMISE; } mTraceName = BaseJavaModule.this.getName() + "." + mMethod.getName(); }
private void findMethods() { if (mMethods == null) { Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "findMethods"); mMethods = new HashMap<>(); Method[] targetMethods = getClass().getDeclaredMethods(); for (Method targetMethod : targetMethods) { ReactMethod annotation = targetMethod.getAnnotation(ReactMethod.class); if (annotation != null) { String methodName = targetMethod.getName(); if (mMethods.containsKey(methodName)) { // We do not support method overloading since js sees a function as an object regardless // of number of params. throw new IllegalArgumentException( "Java Module " + getName() + " method name already registered: " + methodName); } mMethods.put( methodName, new JavaMethod(targetMethod, annotation.isBlockingSynchronousMethod())); } } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } }