forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement native module to measure CPU time
Summary: Changelog: [internal] This implements a native module for Fantom to provide information about the CPU time used by the current process. This will be used by Fantom as the clock to run benchmarks more accurately. It provides 2 implementations: 1. One based on `clock_gettime` with `CLOCK_THREAD_CPUTIME_ID` that's available on Linux. This provides the CPU time for the current process with decent precision (tens of nanoseconds). 2. A fallback implementation that uses a monotonic clock (not actually CPU time). We can add a MacOS equivalent in a following diff. Differential Revision: D67596312
- Loading branch information
1 parent
430f7d8
commit a2ac3d8
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/react-native/ReactCommon/react/nativemodule/cputime/CPUTime.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,55 @@ | ||
/* | ||
* 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 | ||
|
||
#ifdef USE_POSIX_TIME | ||
#include <time.h> | ||
#else | ||
#include <chrono> | ||
#endif | ||
|
||
#ifdef USE_POSIX_TIME | ||
|
||
namespace { | ||
const double NANOSECONDS_IN_A_SECOND = 1000000000; | ||
} // namespace | ||
|
||
#endif | ||
|
||
namespace facebook::react { | ||
|
||
#ifdef USE_POSIX_TIME | ||
|
||
inline double getCPUTimeNanos() { | ||
struct timespec time {}; | ||
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time); | ||
return static_cast<double>(time.tv_sec) * NANOSECONDS_IN_A_SECOND + | ||
static_cast<double>(time.tv_nsec); | ||
} | ||
|
||
inline bool hasAccurateCPUTimeNanos() { | ||
return true; | ||
} | ||
|
||
#else | ||
|
||
inline double getCPUTimeNanos() { | ||
auto now = std::chrono::steady_clock::now(); | ||
return static_cast<double>( | ||
std::chrono::duration_cast<std::chrono::nanoseconds>( | ||
now.time_since_epoch()) | ||
.count()); | ||
} | ||
|
||
inline bool hasAccurateCPUTimeNanos() { | ||
return false; | ||
} | ||
|
||
#endif | ||
|
||
} // namespace facebook::react |
34 changes: 34 additions & 0 deletions
34
packages/react-native/ReactCommon/react/nativemodule/cputime/NativeCPUTime.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,34 @@ | ||
/* | ||
* 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 "NativeCPUTime.h" | ||
|
||
#include "CPUTime.h" | ||
|
||
#ifdef RN_DISABLE_OSS_PLUGIN_HEADER | ||
#include "Plugins.h" | ||
#endif | ||
|
||
std::shared_ptr<facebook::react::TurboModule> NativeCPUTimeModuleProvider( | ||
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) { | ||
return std::make_shared<facebook::react::NativeCPUTime>(std::move(jsInvoker)); | ||
} | ||
|
||
namespace facebook::react { | ||
|
||
NativeCPUTime::NativeCPUTime(std::shared_ptr<CallInvoker> jsInvoker) | ||
: NativeCPUTimeCxxSpec(std::move(jsInvoker)) {} | ||
|
||
double NativeCPUTime::getCPUTimeNanos(jsi::Runtime& /*runtime*/) { | ||
return facebook::react::getCPUTimeNanos(); | ||
} | ||
|
||
bool NativeCPUTime::hasAccurateCPUTimeNanos(jsi::Runtime& /*runtime*/) { | ||
return facebook::react::hasAccurateCPUTimeNanos(); | ||
} | ||
|
||
} // namespace facebook::react |
28 changes: 28 additions & 0 deletions
28
packages/react-native/ReactCommon/react/nativemodule/cputime/NativeCPUTime.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,28 @@ | ||
/* | ||
* 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 | ||
|
||
#if __has_include("rncoreJSI.h") // Cmake headers on Android | ||
#include "rncoreJSI.h" | ||
#elif __has_include("FBReactNativeSpecJSI.h") // CocoaPod headers on Apple | ||
#include "FBReactNativeSpecJSI.h" | ||
#else | ||
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h> | ||
#endif | ||
|
||
namespace facebook::react { | ||
|
||
class NativeCPUTime : public NativeCPUTimeCxxSpec<NativeCPUTime> { | ||
public: | ||
explicit NativeCPUTime(std::shared_ptr<CallInvoker> jsInvoker); | ||
|
||
double getCPUTimeNanos(jsi::Runtime& runtime); | ||
bool hasAccurateCPUTimeNanos(jsi::Runtime& runtime); | ||
}; | ||
|
||
} // namespace facebook::react |
24 changes: 24 additions & 0 deletions
24
packages/react-native/src/private/specs/modules/NativeCPUTime.js
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,24 @@ | ||
/** | ||
* 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. | ||
* | ||
* @flow strict | ||
* @format | ||
*/ | ||
|
||
import type {TurboModule} from '../../../../Libraries/TurboModule/RCTExport'; | ||
|
||
import * as TurboModuleRegistry from '../../../../Libraries/TurboModule/TurboModuleRegistry'; | ||
|
||
/** | ||
* This is an internal native module meant to be used for performance | ||
* measurements and benchmarks. It is not meant to be used in production. | ||
*/ | ||
export interface Spec extends TurboModule { | ||
+getCPUTimeNanos: () => number; | ||
+hasAccurateCPUTimeNanos: () => boolean; | ||
} | ||
|
||
export default (TurboModuleRegistry.getEnforcing<Spec>('CPUTimeCxx'): Spec); |