Skip to content

Commit bc3072e

Browse files
rubennortefacebook-github-bot
authored andcommitted
Implement native module to measure CPU time (#48454)
Summary: Pull Request resolved: #48454 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. Reviewed By: rshest Differential Revision: D67596312 fbshipit-source-id: dd712c0171aa998ddbb6fed9187b3c467cd5417d
1 parent eee2866 commit bc3072e

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
#ifdef USE_POSIX_TIME
11+
#include <time.h>
12+
#else
13+
#include <chrono>
14+
#endif
15+
16+
#ifdef USE_POSIX_TIME
17+
18+
namespace {
19+
const double NANOSECONDS_IN_A_SECOND = 1000000000;
20+
} // namespace
21+
22+
#endif
23+
24+
namespace facebook::react {
25+
26+
#ifdef USE_POSIX_TIME
27+
28+
inline double getCPUTimeNanos() {
29+
struct timespec time {};
30+
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time);
31+
return static_cast<double>(time.tv_sec) * NANOSECONDS_IN_A_SECOND +
32+
static_cast<double>(time.tv_nsec);
33+
}
34+
35+
inline bool hasAccurateCPUTimeNanosForBenchmarks() {
36+
return true;
37+
}
38+
39+
#else
40+
41+
inline double getCPUTimeNanos() {
42+
auto now = std::chrono::steady_clock::now();
43+
return static_cast<double>(
44+
std::chrono::duration_cast<std::chrono::nanoseconds>(
45+
now.time_since_epoch())
46+
.count());
47+
}
48+
49+
inline bool hasAccurateCPUTimeNanosForBenchmarks() {
50+
return false;
51+
}
52+
53+
#endif
54+
55+
} // namespace facebook::react
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
#include "NativeCPUTime.h"
9+
10+
#include "CPUTime.h"
11+
12+
#ifdef RN_DISABLE_OSS_PLUGIN_HEADER
13+
#include "Plugins.h"
14+
#endif
15+
16+
std::shared_ptr<facebook::react::TurboModule> NativeCPUTimeModuleProvider(
17+
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
18+
return std::make_shared<facebook::react::NativeCPUTime>(std::move(jsInvoker));
19+
}
20+
21+
namespace facebook::react {
22+
23+
NativeCPUTime::NativeCPUTime(std::shared_ptr<CallInvoker> jsInvoker)
24+
: NativeCPUTimeCxxSpec(std::move(jsInvoker)) {}
25+
26+
double NativeCPUTime::getCPUTimeNanos(jsi::Runtime& /*runtime*/) {
27+
return facebook::react::getCPUTimeNanos();
28+
}
29+
30+
bool NativeCPUTime::hasAccurateCPUTimeNanosForBenchmarks(
31+
jsi::Runtime& /*runtime*/) {
32+
return facebook::react::hasAccurateCPUTimeNanosForBenchmarks();
33+
}
34+
35+
} // namespace facebook::react
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
#if __has_include("rncoreJSI.h") // Cmake headers on Android
11+
#include "rncoreJSI.h"
12+
#elif __has_include("FBReactNativeSpecJSI.h") // CocoaPod headers on Apple
13+
#include "FBReactNativeSpecJSI.h"
14+
#else
15+
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
16+
#endif
17+
18+
namespace facebook::react {
19+
20+
class NativeCPUTime : public NativeCPUTimeCxxSpec<NativeCPUTime> {
21+
public:
22+
explicit NativeCPUTime(std::shared_ptr<CallInvoker> jsInvoker);
23+
24+
double getCPUTimeNanos(jsi::Runtime& runtime);
25+
bool hasAccurateCPUTimeNanosForBenchmarks(jsi::Runtime& runtime);
26+
};
27+
28+
} // namespace facebook::react
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
* @flow strict
8+
* @format
9+
*/
10+
11+
import type {TurboModule} from '../../../../Libraries/TurboModule/RCTExport';
12+
13+
import * as TurboModuleRegistry from '../../../../Libraries/TurboModule/TurboModuleRegistry';
14+
15+
/**
16+
* This is an internal native module meant to be used for performance
17+
* measurements and benchmarks. It is not meant to be used in production.
18+
*/
19+
export interface Spec extends TurboModule {
20+
+getCPUTimeNanos: () => number;
21+
+hasAccurateCPUTimeNanosForBenchmarks: () => boolean;
22+
}
23+
24+
export default (TurboModuleRegistry.getEnforcing<Spec>('CPUTimeCxx'): Spec);

0 commit comments

Comments
 (0)