Guard all NativeModulePerfLogger calls with a null check

Summary:
## Motivation
We got this crash T67304907, which shows a `EXC_BAD_ACCESS / KERN_INVALID_ADDRESS` when calling this line:
```
  NativeModulePerfLogger::getInstance().asyncMethodCallBatchPreprocessStart();
```
There are no arguments in that call, so I figured the only error could be when we try to invoke `getInstance()` or `asyncMethodCallBatchPreprocessStart()`.

This diff:
1. Removes the `NativeModulePerfLogger::getInstance()` bit. Now NativeModulePerfLogger is used via regular static C functions. So, there's no way that simply invoking one of the logging functions crashes the application: there's no vtable lookup.
2. Inside each logging function, when perf-logging is disabled, the global perflogger should be `nullptr`. This diff makes it so that in that case, we won't execute any code in the control group of the perf-logging experiment.

## Changes
**How do we enable NativeModule perf-logging?**
- Previously:
   - `NativeModulePerfLogger::setInstance(std::make_shared<FBReactNativeModulePerfLogger>(...))`
   - `TurboModulePerfLogger::setInstance(std::make_shared<FBReactNativeModulePerfLogger>(...))`.
- Now:
   - `BridgeNativeModulePerfLogger::enableLogging(std::make_unique<FBReactNativeModulePerfLogger>(...))`
   - `TurboModulePerfLogger::enableLogging(std::make_unique<FBReactNativeModulePerfLogger>(...))`

**How do we do NativeModule perf-logging now?**
- Previously:
   -  `NativeModulePerfLogger::getInstance().command(...args)`
   -  `TurboModulePerfLogger::getInstance().command(...args)`.
- Now:
   - `BridgeNativeModulePerfLogger::command(...args)`
   - `TurboModulePerfLogger::command(...args)`.

The benefit of this approach is that each method in `BridgeNativeModulePerfLogger` is guarded with an if check. Example:

```
void moduleCreateConstructStart(const char *moduleName, int32_t id) {
  NativeModulePerfLogger *logger = g_perfLogger.get();
  if (logger != nullptr) {
    logger->moduleCreateConstructStart(moduleName, id);
  }
}
```

Therefore, we don't actually execute any code when perf-logging is disabled.

Changelog:
[Internal]

Reviewed By: fkgozali

Differential Revision: D21669888

fbshipit-source-id: 80c73754c430ce787404b563878bad146295e01f
This commit is contained in:
Ramanpreet Nara
2020-05-20 20:19:30 -07:00
committed by Facebook GitHub Bot
parent c75e8ae4ff
commit 4830085f40
16 changed files with 956 additions and 304 deletions
+8 -15
View File
@@ -8,7 +8,7 @@
#include "ModuleRegistry.h"
#include <glog/logging.h>
#include <reactperflogger/NativeModulePerfLogger.h>
#include <reactperflogger/BridgeNativeModulePerfLogger.h>
#include "NativeModule.h"
#include "SystraceSection.h"
@@ -100,24 +100,19 @@ folly::Optional<ModuleConfig> ModuleRegistry::getConfig(
if (it == modulesByName_.end()) {
if (unknownModules_.find(name) != unknownModules_.end()) {
NativeModulePerfLogger::getInstance().moduleJSRequireBeginningFail(
name.c_str());
NativeModulePerfLogger::getInstance().moduleJSRequireEndingStart(
name.c_str());
BridgeNativeModulePerfLogger::moduleJSRequireBeginningFail(name.c_str());
BridgeNativeModulePerfLogger::moduleJSRequireEndingStart(name.c_str());
return folly::none;
}
if (!moduleNotFoundCallback_) {
unknownModules_.insert(name);
NativeModulePerfLogger::getInstance().moduleJSRequireBeginningFail(
name.c_str());
NativeModulePerfLogger::getInstance().moduleJSRequireEndingStart(
name.c_str());
BridgeNativeModulePerfLogger::moduleJSRequireBeginningFail(name.c_str());
BridgeNativeModulePerfLogger::moduleJSRequireEndingStart(name.c_str());
return folly::none;
}
NativeModulePerfLogger::getInstance().moduleJSRequireBeginningEnd(
name.c_str());
BridgeNativeModulePerfLogger::moduleJSRequireBeginningEnd(name.c_str());
bool wasModuleLazilyLoaded = moduleNotFoundCallback_(name);
it = modulesByName_.find(name);
@@ -126,14 +121,12 @@ folly::Optional<ModuleConfig> ModuleRegistry::getConfig(
wasModuleLazilyLoaded && it != modulesByName_.end();
if (!wasModuleRegisteredWithRegistry) {
NativeModulePerfLogger::getInstance().moduleJSRequireEndingStart(
name.c_str());
BridgeNativeModulePerfLogger::moduleJSRequireEndingStart(name.c_str());
unknownModules_.insert(name);
return folly::none;
}
} else {
NativeModulePerfLogger::getInstance().moduleJSRequireBeginningEnd(
name.c_str());
BridgeNativeModulePerfLogger::moduleJSRequireBeginningEnd(name.c_str());
}
// If we've gotten this far, then we've signaled moduleJSRequireBeginningEnd