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 #pragma once
9 
10 #include <ABI49_0_0FBReactNativeSpec/ABI49_0_0FBReactNativeSpecJSI.h>
11 #include <memory>
12 #include <string>
13 
14 #include "ABI49_0_0NativePerformanceObserver.h"
15 
16 namespace ABI49_0_0facebook::ABI49_0_0React {
17 class PerformanceEntryReporter;
18 
19 #pragma mark - Structs
20 
21 using ABI49_0_0ReactNativeStartupTiming =
22     NativePerformanceCxxBaseABI49_0_0ReactNativeStartupTiming<
23         int32_t, // Start time of the ABI49_0_0RN app startup process
24         int32_t, // End time of the ABI49_0_0RN app startup process
25         int32_t, // Start time that ABI49_0_0RN app execute the JS bundle
26         int32_t // End time that ABI49_0_0RN app execute the JS bundle
27         >;
28 
29 template <>
30 struct Bridging<ABI49_0_0ReactNativeStartupTiming>
31     : NativePerformanceCxxBaseABI49_0_0ReactNativeStartupTimingBridging<
32           int32_t,
33           int32_t,
34           int32_t,
35           int32_t> {};
36 
37 #pragma mark - implementation
38 
39 class NativePerformance : public ABI49_0_0NativePerformanceCxxSpec<NativePerformance>,
40                           std::enable_shared_from_this<NativePerformance> {
41  public:
42   NativePerformance(std::shared_ptr<CallInvoker> jsInvoker);
43 
44   void
45   mark(jsi::Runtime &rt, std::string name, double startTime, double duration);
46 
47   void measure(
48       jsi::Runtime &rt,
49       std::string name,
50       double startTime,
51       double endTime,
52       std::optional<double> duration,
53       std::optional<std::string> startMark,
54       std::optional<std::string> endMark);
55 
56   // To align with web API, we will make sure to return three properties
57   // (jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize) + anything needed from
58   // the VM side.
59   // `jsHeapSizeLimit`: The maximum size of the heap, in bytes, that
60   // is available to the context.
61   // `totalJSHeapSize`: The total allocated heap size, in bytes.
62   // `usedJSHeapSize`: The currently active segment of JS heap, in
63   // bytes.
64   //
65   // Note that we use int64_t here and it's ok to lose precision in JS doubles
66   // for heap size information, as double's 2^53 sig bytes is large enough.
67   std::unordered_map<std::string, double> getSimpleMemoryInfo(jsi::Runtime &rt);
68 
69   // Collect and return the ABI49_0_0RN app startup timing information for performance
70   // tracking.
71   ABI49_0_0ReactNativeStartupTiming getABI49_0_0ReactNativeStartupTiming(jsi::Runtime &rt);
72 
73  private:
74 };
75 
76 } // namespace ABI49_0_0facebook::ABI49_0_0React
77