Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ add_react_common_subdir(cxxreact)
add_react_common_subdir(jsc)
add_react_common_subdir(jsi)
add_react_common_subdir(callinvoker)
add_react_common_subdir(oscompat)
add_react_common_subdir(jsinspector-modern)
add_react_common_subdir(jsinspector-modern/tracing)
add_react_common_subdir(hermes/executor)
Expand Down Expand Up @@ -169,6 +170,7 @@ add_library(reactnative
$<TARGET_OBJECTS:jsireact>
$<TARGET_OBJECTS:logger>
$<TARGET_OBJECTS:mapbufferjni>
$<TARGET_OBJECTS:oscompat>
$<TARGET_OBJECTS:react_bridging>
$<TARGET_OBJECTS:react_codegen_rncore>
$<TARGET_OBJECTS:react_cxxreact>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ target_include_directories(jsinspector_tracing PUBLIC ${REACT_COMMON_DIR})

target_link_libraries(jsinspector_tracing
folly_runtime
oscompat
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@

#include "PerformanceTracer.h"

#include <oscompat/OSCompat.h>

#include <folly/json.h>

#include <mutex>
#include <unordered_set>

namespace facebook::react::jsinspector_modern {

namespace {

/** Process ID for all emitted events. */
const uint64_t PID = 1000;

/** Default/starting track ID for the "Timings" track. */
const uint64_t USER_TIMINGS_DEFAULT_TRACK = 1000;

uint64_t getUnixTimestampOfNow() {
return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch())
Expand All @@ -35,6 +30,9 @@ PerformanceTracer& PerformanceTracer::getInstance() {
return tracer;
}

PerformanceTracer::PerformanceTracer()
: processId_(oscompat::getCurrentProcessId()) {}

bool PerformanceTracer::startTracing() {
std::lock_guard lock(mutex_);
if (tracing_) {
Expand All @@ -46,8 +44,8 @@ bool PerformanceTracer::startTracing() {
.cat = "disabled-by-default-devtools.timeline",
.ph = 'I',
.ts = getUnixTimestampOfNow(),
.pid = PID, // FIXME: This should be the real process ID.
.tid = 0, // FIXME: This should be the real thread ID.
.pid = processId_,
.tid = oscompat::getCurrentThreadId(),
.args = folly::dynamic::object("data", folly::dynamic::object()),
});

Expand Down Expand Up @@ -106,9 +104,8 @@ void PerformanceTracer::reportMark(
.cat = "blink.user_timing",
.ph = 'I',
.ts = start,
.pid = PID, // FIXME: This should be the real process ID.
.tid = USER_TIMINGS_DEFAULT_TRACK, // FIXME: This should be the real
// thread ID.
.pid = processId_,
.tid = oscompat::getCurrentThreadId(),
});
}

Expand All @@ -132,15 +129,15 @@ void PerformanceTracer::reportMeasure(
}

++performanceMeasureCount_;
auto currentThreadId = oscompat::getCurrentThreadId();
buffer_.push_back(TraceEvent{
.id = performanceMeasureCount_,
.name = std::string(name),
.cat = "blink.user_timing",
.ph = 'b',
.ts = start,
.pid = PID, // FIXME: This should be the real process ID.
.tid = USER_TIMINGS_DEFAULT_TRACK, // FIXME: This should be the real
// thread ID.
.pid = processId_,
.tid = currentThreadId,
.args = beginEventArgs,
});
buffer_.push_back(TraceEvent{
Expand All @@ -149,9 +146,42 @@ void PerformanceTracer::reportMeasure(
.cat = "blink.user_timing",
.ph = 'e',
.ts = start + duration,
.pid = PID, // FIXME: This should be the real process ID.
.tid = USER_TIMINGS_DEFAULT_TRACK, // FIXME: This should be the real
// thread ID.
.pid = processId_,
.tid = currentThreadId,
});
}

void PerformanceTracer::reportProcess(uint64_t id, const std::string& name) {
std::lock_guard<std::mutex> lock(mutex_);
if (!tracing_) {
return;
}

buffer_.push_back(TraceEvent{
.name = "process_name",
.cat = "__metadata",
.ph = 'M',
.ts = 0,
.pid = id,
.tid = 0,
.args = folly::dynamic::object("name", name),
});
}

void PerformanceTracer::reportThread(uint64_t id, const std::string& name) {
std::lock_guard<std::mutex> lock(mutex_);
if (!tracing_) {
return;
}

buffer_.push_back(TraceEvent{
.name = "thread_name",
.cat = "__metadata",
.ph = 'M',
.ts = 0,
.pid = processId_,
.tid = id,
.args = folly::dynamic::object("name", name),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <folly/dynamic.h>

#include <functional>
#include <mutex>
#include <optional>
#include <unordered_map>
#include <vector>

namespace facebook::react::jsinspector_modern {
Expand Down Expand Up @@ -67,15 +67,26 @@ class PerformanceTracer {
uint64_t duration,
const std::optional<DevToolsTrackEntryPayload>& trackMetadata);

/**
* Record a corresponding Trace Event for OS-level process.
*/
void reportProcess(uint64_t id, const std::string& name);

/**
* Record a corresponding Trace Event for OS-level thread.
*/
void reportThread(uint64_t id, const std::string& name);

private:
PerformanceTracer() = default;
PerformanceTracer();
PerformanceTracer(const PerformanceTracer&) = delete;
PerformanceTracer& operator=(const PerformanceTracer&) = delete;
~PerformanceTracer() = default;

folly::dynamic serializeTraceEvent(TraceEvent event) const;

bool tracing_{false};
uint64_t processId_;
uint32_t performanceMeasureCount_{0};
std::vector<TraceEvent> buffer_;
std::mutex mutex_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ Pod::Spec.new do |s|
end

s.dependency "RCT-Folly"
s.dependency "React-oscompat"
end
14 changes: 14 additions & 0 deletions packages/react-native/ReactCommon/oscompat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.

cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)

add_compile_options(-fexceptions)

file(GLOB oscompat_SRC CONFIGURE_DEPENDS *.cpp)
add_library(oscompat OBJECT ${oscompat_SRC})

target_include_directories(oscompat PUBLIC .)
18 changes: 18 additions & 0 deletions packages/react-native/ReactCommon/oscompat/OSCompat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 <cstdint>

namespace facebook::react::oscompat {

uint64_t getCurrentProcessId();

uint64_t getCurrentThreadId();

} // namespace facebook::react::oscompat
63 changes: 63 additions & 0 deletions packages/react-native/ReactCommon/oscompat/OSCompatPosix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.
*/

#if defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || \
defined(__ANDROID__)

#include "OSCompat.h"

#include <cassert>

#include <pthread.h>
#include <unistd.h>

#if defined(__MACH__)
#include <mach/mach.h>
#endif // defined(__MACH__)

#if defined(__linux__)
#include <sys/syscall.h>
#endif // defined(__linux__)

namespace facebook::react::oscompat {

uint64_t getCurrentProcessId() {
return getpid();
}

#if defined(__APPLE__) && defined(__MACH__)

uint64_t getCurrentThreadId() {
uint64_t tid = 0;
auto ret = pthread_threadid_np(nullptr, &tid);
assert(
ret == 0 &&
"Unexpected pthread_threadid_np failure while getting thread ID");
(void)ret;
return tid;
}

#elif defined(__ANDROID__)

uint64_t getCurrentThreadId() {
return gettid();
}

#elif defined(__linux__)

uint64_t getCurrentThreadId() {
return syscall(__NR_gettid);
}

#else
#error "getCurrentThreadID() is not supported on this platform"
#endif

} // namespace facebook::react::oscompat

#endif // defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) ||
// defined(__ANDROID__)
26 changes: 26 additions & 0 deletions packages/react-native/ReactCommon/oscompat/OSCompatWindows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

#if defined(_WIN32) || defined(_MSC_VER)

#include "OSCompat.h"

#include <windows.h>

namespace facebook::react::oscompat {

uint64_t getCurrentProcessId() {
return GetCurrentProcessId();
}

uint64_t getCurrentThreadId() {
return GetCurrentThreadId();
}

} // namespace facebook::react::oscompat

#endif // defined(_WIN32) || defined(_MSC_VER)
31 changes: 31 additions & 0 deletions packages/react-native/ReactCommon/oscompat/React-oscompat.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

require "json"

package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json")))
version = package['version']

source = { :git => 'https://github.com/facebook/react-native.git' }
if version == '1000.0.0'
# This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in.
source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1")
else
source[:tag] = "v#{version}"
end

Pod::Spec.new do |s|
s.name = "React-oscompat"
s.version = version
s.summary = "Small set of OS-level utilities for React Native"
s.homepage = "https://reactnative.dev/"
s.license = package["license"]
s.author = "Meta Platforms, Inc. and its affiliates"
s.platforms = min_supported_versions
s.source = source
s.source_files = "*.{cpp,h}"
s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "" }
s.header_dir = "oscompat"
end
1 change: 1 addition & 0 deletions packages/react-native/scripts/cocoapods/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ def self.react_native_pods
"React-jsinspector",
"React-jsitooling",
"React-logger",
"React-oscompat",
"React-perflogger",
"React-rncore",
"React-runtimeexecutor",
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/scripts/react_native_pods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def use_react_native! (
pod 'React-rendererdebug', :path => "#{prefix}/ReactCommon/react/renderer/debug"
pod 'React-rendererconsistency', :path => "#{prefix}/ReactCommon/react/renderer/consistency"
pod 'React-perflogger', :path => "#{prefix}/ReactCommon/reactperflogger"
pod 'React-oscompat', :path => "#{prefix}/ReactCommon/oscompat"
pod 'React-logger', :path => "#{prefix}/ReactCommon/logger"
pod 'ReactCommon/turbomodule/core', :path => "#{prefix}/ReactCommon", :modular_headers => true
pod 'React-NativeModulesApple', :path => "#{prefix}/ReactCommon/react/nativemodule/core/platform/ios", :modular_headers => true
Expand Down
Loading