1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #include <memory>
9 
10 #include <ABI49_0_0cxxreact/ABI49_0_0ReactMarker.h>
11 #include <ABI49_0_0jsi/ABI49_0_0instrumentation.h>
12 #include "ABI49_0_0NativePerformance.h"
13 #include "ABI49_0_0PerformanceEntryReporter.h"
14 
15 #include "ABI49_0_0Plugins.h"
16 
NativePerformanceModuleProvider(std::shared_ptr<ABI49_0_0facebook::ABI49_0_0React::CallInvoker> jsInvoker)17 std::shared_ptr<ABI49_0_0facebook::ABI49_0_0React::TurboModule> NativePerformanceModuleProvider(
18     std::shared_ptr<ABI49_0_0facebook::ABI49_0_0React::CallInvoker> jsInvoker) {
19   return std::make_shared<ABI49_0_0facebook::ABI49_0_0React::NativePerformance>(
20       std::move(jsInvoker));
21 }
22 
23 namespace ABI49_0_0facebook::ABI49_0_0React {
24 
NativePerformance(std::shared_ptr<CallInvoker> jsInvoker)25 NativePerformance::NativePerformance(std::shared_ptr<CallInvoker> jsInvoker)
26     : ABI49_0_0NativePerformanceCxxSpec(std::move(jsInvoker)) {}
27 
mark(jsi::Runtime & rt,std::string name,double startTime,double duration)28 void NativePerformance::mark(
29     jsi::Runtime &rt,
30     std::string name,
31     double startTime,
32     double duration) {
33   PerformanceEntryReporter::getInstance().mark(name, startTime, duration);
34 }
35 
measure(jsi::Runtime & rt,std::string name,double startTime,double endTime,std::optional<double> duration,std::optional<std::string> startMark,std::optional<std::string> endMark)36 void NativePerformance::measure(
37     jsi::Runtime &rt,
38     std::string name,
39     double startTime,
40     double endTime,
41     std::optional<double> duration,
42     std::optional<std::string> startMark,
43     std::optional<std::string> endMark) {
44   PerformanceEntryReporter::getInstance().measure(
45       name, startTime, endTime, duration, startMark, endMark);
46 }
47 
getSimpleMemoryInfo(jsi::Runtime & rt)48 std::unordered_map<std::string, double> NativePerformance::getSimpleMemoryInfo(
49     jsi::Runtime &rt) {
50   auto heapInfo = rt.instrumentation().getHeapInfo(false);
51   std::unordered_map<std::string, double> heapInfoToJs;
52   for (auto &entry : heapInfo) {
53     heapInfoToJs[entry.first] = static_cast<double>(entry.second);
54   }
55   return heapInfoToJs;
56 }
57 
getABI49_0_0ReactNativeStartupTiming(jsi::Runtime & rt)58 ABI49_0_0ReactNativeStartupTiming NativePerformance::getABI49_0_0ReactNativeStartupTiming(
59     jsi::Runtime &rt) {
60   ABI49_0_0ReactNativeStartupTiming result = {0, 0, 0, 0};
61 
62   ABI49_0_0ReactMarker::StartupLogger &startupLogger =
63       ABI49_0_0ReactMarker::StartupLogger::getInstance();
64   result.startTime = startupLogger.getAppStartTime();
65   result.executeJavaScriptBundleEntryPointStart =
66       startupLogger.getRunJSBundleStartTime();
67   result.executeJavaScriptBundleEntryPointEnd =
68       startupLogger.getRunJSBundleEndTime();
69   result.endTime = startupLogger.getRunJSBundleEndTime();
70 
71   return result;
72 }
73 
74 } // namespace ABI49_0_0facebook::ABI49_0_0React
75