forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FuseboxTracer for DevTools tracing
Summary: Introduce a simplified and minimable tracing backend for Fusebox. This backend is sufficient to implement a pretty usable performance panel. Although the more I see how easy this is and how annoying working with Perfetto is, the more I think we should just maintain this going forward. Anyways we can figure that out incrementally. For now the plan is still for this to be temporary. Differential Revision: D57981944
- Loading branch information
1 parent
7061649
commit 39774fd
Showing
5 changed files
with
238 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/react-native/ReactCommon/reactperflogger/fusebox/FuseboxTracer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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 <mutex> | ||
#include <unordered_map> | ||
|
||
#include "FuseboxTracer.h" | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
static bool tracing = false; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
static std::vector<BufferEvent> buffer; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
static std::mutex mutex; | ||
|
||
/* static */ bool FuseboxTracer::isTracing() { | ||
return tracing; | ||
} | ||
|
||
/* static */ void FuseboxTracer::startTracing() { | ||
tracing = true; | ||
} | ||
|
||
/* static */ void FuseboxTracer::stopTracing( | ||
const std::function<void(const folly::dynamic& eventsChunk)>& | ||
resultCallback) { | ||
std::unique_lock<std::mutex> lock(mutex); | ||
auto traceEvents = folly::dynamic::array(); | ||
if (buffer.empty()) { | ||
return; | ||
} | ||
auto savedBuffer = std::move(buffer); | ||
buffer.clear(); | ||
tracing = false; | ||
|
||
std::unordered_map<std::string, uint64_t> trackIdMap; | ||
uint64_t nextTrack = 1000; | ||
|
||
// Name the main process. Only one process is supported currently. | ||
traceEvents.push_back(folly::dynamic::object( | ||
"args", folly::dynamic::object("name", "Main App"))("cat", "__metadata")( | ||
"name", "process_name")("ph", "M")("pid", 1000)("tid", 0)("ts", 0)); | ||
|
||
for (auto& event : savedBuffer) { | ||
if (!trackIdMap.contains(event.track)) { | ||
auto trackId = nextTrack++; | ||
trackIdMap[event.track] = trackId; | ||
// New track | ||
traceEvents.push_back(folly::dynamic::object( | ||
"args", folly::dynamic::object("name", event.track))( | ||
"cat", "__metadata")("name", "thread_name")("ph", "M")("pid", 1000)( | ||
"tid", trackId)("ts", 0)); | ||
} | ||
auto trackId = trackIdMap[event.track]; | ||
|
||
// New event | ||
traceEvents.push_back(folly::dynamic::object( | ||
"args", folly::dynamic::object())("cat", "react.native")( | ||
"dur", (event.end - event.start) * 1000)("name", event.name)("ph", "X")( | ||
"ts", event.start * 1000)("pid", 1000)("tid", trackId)); | ||
|
||
if (traceEvents.size() >= 1000) { | ||
resultCallback(traceEvents); | ||
traceEvents = folly::dynamic::array(); | ||
} | ||
} | ||
|
||
if (traceEvents.size() >= 1) { | ||
resultCallback(traceEvents); | ||
} | ||
} | ||
|
||
/* static */ void FuseboxTracer::addEvent( | ||
const std::string& name, | ||
uint64_t start, | ||
uint64_t end, | ||
const std::string& track) { | ||
if (!isTracing()) { | ||
return; | ||
} | ||
std::unique_lock<std::mutex> lock(mutex); | ||
buffer.push_back(BufferEvent{start, end, name, track}); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/react-native/ReactCommon/reactperflogger/fusebox/FuseboxTracer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <vector> | ||
#include "folly/json/dynamic.h" | ||
|
||
struct BufferEvent { | ||
uint64_t start; | ||
uint64_t end; | ||
std::string name; | ||
std::string track; | ||
}; | ||
|
||
class FuseboxTracer { | ||
public: | ||
static bool isTracing(); | ||
static void startTracing(); | ||
static void stopTracing( | ||
const std::function<void(const folly::dynamic& eventsChunk)>& | ||
resultCallback); | ||
static void addEvent( | ||
const std::string& name, | ||
uint64_t start, | ||
uint64_t end, | ||
const std::string& track); | ||
}; |
72 changes: 72 additions & 0 deletions
72
packages/react-native/ReactCommon/reactperflogger/fusebox/tests/FuseboxTracerTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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 <gtest/gtest.h> | ||
|
||
#include "reactperflogger/FuseboxTracer.h" | ||
|
||
folly::dynamic stopTracingAndCollect() { | ||
folly::dynamic trace = folly::dynamic::array; | ||
FuseboxTracer::stopTracing([&trace](const folly::dynamic& eventsChunk) { | ||
for (const auto& event : eventsChunk) { | ||
trace.push_back(event); | ||
} | ||
}); | ||
return trace; | ||
} | ||
|
||
class FuseboxTracerTest : public ::testing::Test { | ||
protected: | ||
FuseboxTracerTest() = default; | ||
|
||
~FuseboxTracerTest() override = default; | ||
|
||
void SetUp() override { | ||
stopTracingAndCollect(); | ||
} | ||
|
||
void TearDown() override { | ||
stopTracingAndCollect(); | ||
} | ||
}; | ||
|
||
TEST_F(FuseboxTracerTest, TracingOffByDefault) { | ||
EXPECT_FALSE(FuseboxTracer::isTracing()); | ||
} | ||
|
||
TEST_F(FuseboxTracerTest, TracingOn) { | ||
FuseboxTracer::startTracing(); | ||
EXPECT_TRUE(FuseboxTracer::isTracing()); | ||
} | ||
|
||
TEST_F(FuseboxTracerTest, DiscardEventWhenNotOn) { | ||
EXPECT_FALSE(FuseboxTracer::isTracing()); | ||
EXPECT_EQ(stopTracingAndCollect().size(), 0); | ||
FuseboxTracer::addEvent("test", 0, 0, "default track"); | ||
FuseboxTracer::addEvent("test", 0, 0, "default track"); | ||
EXPECT_EQ(stopTracingAndCollect().size(), 0); | ||
} | ||
|
||
TEST_F(FuseboxTracerTest, NoDefaultEvents) { | ||
FuseboxTracer::startTracing(); | ||
EXPECT_EQ(stopTracingAndCollect().size(), 0); | ||
} | ||
|
||
TEST_F(FuseboxTracerTest, SimpleEvent) { | ||
FuseboxTracer::startTracing(); | ||
FuseboxTracer::addEvent("test", 0, 0, "default track"); | ||
EXPECT_GE(stopTracingAndCollect().size(), 1); | ||
} | ||
|
||
TEST_F(FuseboxTracerTest, MultiEvents) { | ||
FuseboxTracer::startTracing(); | ||
for (int i = 0; i < 10; i++) { | ||
FuseboxTracer::addEvent("test", 0, 0, "default track"); | ||
} | ||
EXPECT_GE(stopTracingAndCollect().size(), 10); | ||
EXPECT_EQ(stopTracingAndCollect().size(), 0); | ||
} |