Files
react-native/Libraries/WebPerformance/NativePerformance.cpp
T
Xin Chen 70fb2dce45 Add performance.memory API
Summary:
This diff adds new performance API `memory`, which is a read-only property that gets the current JS heap size from native side.

Note that the JSI API returns an unordered map with unknown list of memory information, which is different from the [web spec](https://fburl.com/p0vpbt33). We may enforce specific memory info type on the JSI API so that it can be properly translate to the web spec.

- Update the JS spec
- Update Native implementation and return memory information with JSI API `jsi::instrumentation()::getHeapInfo()`
- Add native performance module to catalyst package

Changelog:
[General][Added] - Add performance memory API with native memory Info

Reviewed By: rubennorte

Differential Revision: D43137071

fbshipit-source-id: 319f1a6ba78fce61e665b00849ecf2579094af83
2023-02-15 20:52:48 -08:00

61 lines
1.7 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "NativePerformance.h"
#include <glog/logging.h>
#include <jsi/instrumentation.h>
#include "PerformanceEntryReporter.h"
namespace facebook::react {
NativePerformance::NativePerformance(std::shared_ptr<CallInvoker> jsInvoker)
: NativePerformanceCxxSpec(std::move(jsInvoker)) {}
void NativePerformance::mark(
jsi::Runtime &rt,
std::string name,
double startTime,
double duration) {
PerformanceEntryReporter::getInstance().mark(name, startTime, duration);
}
void NativePerformance::clearMarks(
jsi::Runtime &rt,
std::optional<std::string> markName) {
PerformanceEntryReporter::getInstance().clearMarks(markName);
}
void NativePerformance::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) {
PerformanceEntryReporter::getInstance().measure(
name, startTime, endTime, duration, startMark, endMark);
}
void NativePerformance::clearMeasures(
jsi::Runtime &rt,
std::optional<std::string> measureName) {
PerformanceEntryReporter::getInstance().clearMeasures(measureName);
}
std::unordered_map<std::string, double> NativePerformance::getSimpleMemoryInfo(
jsi::Runtime &rt) {
auto heapInfo = rt.instrumentation().getHeapInfo(false);
std::unordered_map<std::string, double> heapInfoToJs;
for (auto &entry : heapInfo) {
heapInfoToJs[entry.first] = static_cast<double>(entry.second);
}
return heapInfoToJs;
}
} // namespace facebook::react