File tree Expand file tree Collapse file tree
ReactCommon/react/nativemodule/cputime
src/private/specs/modules Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments