@onekeyfe/react-native-perf-memory 1.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -0
- package/ReactNativePerfMemory.podspec +30 -0
- package/android/CMakeLists.txt +24 -0
- package/android/build.gradle +130 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/reactnativeperfmemory/ReactNativePerfMemory.kt +67 -0
- package/android/src/main/java/com/margelo/nitro/reactnativeperfmemory/ReactNativePerfMemoryPackage.kt +24 -0
- package/ios/ReactNativePerfMemory.swift +41 -0
- package/lib/module/ReactNativePerfMemory.nitro.js +4 -0
- package/lib/module/ReactNativePerfMemory.nitro.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/ReactNativePerfMemory.nitro.d.ts +11 -0
- package/lib/typescript/src/ReactNativePerfMemory.nitro.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/nitro.json +17 -0
- package/nitrogen/generated/android/c++/JHybridReactNativePerfMemorySpec.cpp +67 -0
- package/nitrogen/generated/android/c++/JHybridReactNativePerfMemorySpec.hpp +65 -0
- package/nitrogen/generated/android/c++/JMemoryUsage.hpp +57 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativeperfmemory/HybridReactNativePerfMemorySpec.kt +58 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativeperfmemory/MemoryUsage.kt +38 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativeperfmemory/reactnativeperfmemoryOnLoad.kt +35 -0
- package/nitrogen/generated/android/reactnativeperfmemory+autolinking.cmake +81 -0
- package/nitrogen/generated/android/reactnativeperfmemory+autolinking.gradle +27 -0
- package/nitrogen/generated/android/reactnativeperfmemoryOnLoad.cpp +44 -0
- package/nitrogen/generated/android/reactnativeperfmemoryOnLoad.hpp +25 -0
- package/nitrogen/generated/ios/ReactNativePerfMemory+autolinking.rb +60 -0
- package/nitrogen/generated/ios/ReactNativePerfMemory-Swift-Cxx-Bridge.cpp +49 -0
- package/nitrogen/generated/ios/ReactNativePerfMemory-Swift-Cxx-Bridge.hpp +113 -0
- package/nitrogen/generated/ios/ReactNativePerfMemory-Swift-Cxx-Umbrella.hpp +47 -0
- package/nitrogen/generated/ios/ReactNativePerfMemoryAutolinking.mm +33 -0
- package/nitrogen/generated/ios/ReactNativePerfMemoryAutolinking.swift +25 -0
- package/nitrogen/generated/ios/c++/HybridReactNativePerfMemorySpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridReactNativePerfMemorySpecSwift.hpp +78 -0
- package/nitrogen/generated/ios/swift/Func_void_MemoryUsage.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
- package/nitrogen/generated/ios/swift/HybridReactNativePerfMemorySpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridReactNativePerfMemorySpec_cxx.swift +138 -0
- package/nitrogen/generated/ios/swift/MemoryUsage.swift +36 -0
- package/nitrogen/generated/shared/c++/HybridReactNativePerfMemorySpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridReactNativePerfMemorySpec.hpp +64 -0
- package/nitrogen/generated/shared/c++/MemoryUsage.hpp +75 -0
- package/package.json +169 -0
- package/src/ReactNativePerfMemory.nitro.ts +10 -0
- package/src/index.tsx +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 OneKey
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# react-native-perf-memory
|
|
2
|
+
|
|
3
|
+
react-native-perf-memory
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install react-native-perf-memory react-native-nitro-modules
|
|
9
|
+
|
|
10
|
+
> `react-native-nitro-modules` is required as this library relies on [Nitro Modules](https://nitro.margelo.com/).
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { ReactNativePerfMemory } from 'react-native-perf-memory';
|
|
17
|
+
|
|
18
|
+
// ...
|
|
19
|
+
|
|
20
|
+
const result = await ReactNativePerfMemory.hello({ message: 'World' });
|
|
21
|
+
console.log(result); // { success: true, data: 'Hello, World!' }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Contributing
|
|
25
|
+
|
|
26
|
+
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
27
|
+
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
28
|
+
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
MIT
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "ReactNativePerfMemory"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/OneKeyHQ/app-modules/react-native-perf-memory.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = [
|
|
17
|
+
"ios/**/*.{swift}",
|
|
18
|
+
"ios/**/*.{m,mm}",
|
|
19
|
+
"cpp/**/*.{hpp,cpp}",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
s.dependency 'React-jsi'
|
|
23
|
+
s.dependency 'React-callinvoker'
|
|
24
|
+
s.dependency 'ReactNativeNativeLogger'
|
|
25
|
+
|
|
26
|
+
load 'nitrogen/generated/ios/ReactNativePerfMemory+autolinking.rb'
|
|
27
|
+
add_nitrogen_files(s)
|
|
28
|
+
|
|
29
|
+
install_modules_dependencies(s)
|
|
30
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
project(reactnativeperfmemory)
|
|
2
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
3
|
+
|
|
4
|
+
set(PACKAGE_NAME reactnativeperfmemory)
|
|
5
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
6
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
7
|
+
|
|
8
|
+
# Define C++ library and add all sources
|
|
9
|
+
add_library(${PACKAGE_NAME} SHARED src/main/cpp/cpp-adapter.cpp)
|
|
10
|
+
|
|
11
|
+
# Add Nitrogen specs :)
|
|
12
|
+
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/reactnativeperfmemory+autolinking.cmake)
|
|
13
|
+
|
|
14
|
+
# Set up local includes
|
|
15
|
+
include_directories("src/main/cpp" "../cpp")
|
|
16
|
+
|
|
17
|
+
find_library(LOG_LIB log)
|
|
18
|
+
|
|
19
|
+
# Link all libraries together
|
|
20
|
+
target_link_libraries(
|
|
21
|
+
${PACKAGE_NAME}
|
|
22
|
+
${LOG_LIB}
|
|
23
|
+
android # <-- Android core
|
|
24
|
+
)
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.getExtOrDefault = {name ->
|
|
3
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['ReactNativePerfMemory_' + name]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
13
|
+
// noinspection DifferentKotlinGradleVersion
|
|
14
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
def reactNativeArchitectures() {
|
|
19
|
+
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
|
20
|
+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
apply plugin: "com.android.library"
|
|
24
|
+
apply plugin: "kotlin-android"
|
|
25
|
+
apply from: '../nitrogen/generated/android/reactnativeperfmemory+autolinking.gradle'
|
|
26
|
+
|
|
27
|
+
apply plugin: "com.facebook.react"
|
|
28
|
+
|
|
29
|
+
def getExtOrIntegerDefault(name) {
|
|
30
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ReactNativePerfMemory_" + name]).toInteger()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
android {
|
|
34
|
+
namespace "com.margelo.nitro.reactnativeperfmemory"
|
|
35
|
+
|
|
36
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
37
|
+
|
|
38
|
+
defaultConfig {
|
|
39
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
40
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
41
|
+
|
|
42
|
+
externalNativeBuild {
|
|
43
|
+
cmake {
|
|
44
|
+
cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
|
|
45
|
+
arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
|
|
46
|
+
abiFilters (*reactNativeArchitectures())
|
|
47
|
+
|
|
48
|
+
buildTypes {
|
|
49
|
+
debug {
|
|
50
|
+
cppFlags "-O1 -g"
|
|
51
|
+
}
|
|
52
|
+
release {
|
|
53
|
+
cppFlags "-O2"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
externalNativeBuild {
|
|
61
|
+
cmake {
|
|
62
|
+
path "CMakeLists.txt"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
packagingOptions {
|
|
67
|
+
excludes = [
|
|
68
|
+
"META-INF",
|
|
69
|
+
"META-INF/**",
|
|
70
|
+
"**/libc++_shared.so",
|
|
71
|
+
"**/libfbjni.so",
|
|
72
|
+
"**/libjsi.so",
|
|
73
|
+
"**/libfolly_json.so",
|
|
74
|
+
"**/libfolly_runtime.so",
|
|
75
|
+
"**/libglog.so",
|
|
76
|
+
"**/libhermes.so",
|
|
77
|
+
"**/libhermes-executor-debug.so",
|
|
78
|
+
"**/libhermes_executor.so",
|
|
79
|
+
"**/libreactnative.so",
|
|
80
|
+
"**/libreactnativejni.so",
|
|
81
|
+
"**/libturbomodulejsijni.so",
|
|
82
|
+
"**/libreact_nativemodule_core.so",
|
|
83
|
+
"**/libjscexecutor.so"
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
buildFeatures {
|
|
88
|
+
buildConfig true
|
|
89
|
+
prefab true
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
buildTypes {
|
|
93
|
+
release {
|
|
94
|
+
minifyEnabled false
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
lintOptions {
|
|
99
|
+
disable "GradleCompatible"
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
compileOptions {
|
|
103
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
104
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
sourceSets {
|
|
108
|
+
main {
|
|
109
|
+
java.srcDirs += [
|
|
110
|
+
"generated/java",
|
|
111
|
+
"generated/jni"
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
repositories {
|
|
118
|
+
mavenCentral()
|
|
119
|
+
google()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
123
|
+
|
|
124
|
+
dependencies {
|
|
125
|
+
implementation "com.facebook.react:react-android"
|
|
126
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
127
|
+
implementation project(":react-native-nitro-modules")
|
|
128
|
+
|
|
129
|
+
implementation project(":onekeyfe_react-native-native-logger")
|
|
130
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
|
package/android/src/main/java/com/margelo/nitro/reactnativeperfmemory/ReactNativePerfMemory.kt
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
package com.margelo.nitro.reactnativeperfmemory
|
|
2
|
+
|
|
3
|
+
import android.app.ActivityManager
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.os.Debug
|
|
6
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
7
|
+
import com.margelo.nitro.core.Promise
|
|
8
|
+
import com.margelo.nitro.NitroModules
|
|
9
|
+
import com.margelo.nitro.nativelogger.OneKeyLog
|
|
10
|
+
import java.io.BufferedReader
|
|
11
|
+
import java.io.FileReader
|
|
12
|
+
|
|
13
|
+
@DoNotStrip
|
|
14
|
+
class ReactNativePerfMemory : HybridReactNativePerfMemorySpec() {
|
|
15
|
+
|
|
16
|
+
override fun getMemoryUsage(): Promise<MemoryUsage> {
|
|
17
|
+
return Promise.async {
|
|
18
|
+
val rssBytes = readVmRssBytesFromProcStatus()
|
|
19
|
+
|
|
20
|
+
val context = NitroModules.applicationContext
|
|
21
|
+
val am = context?.getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager
|
|
22
|
+
|
|
23
|
+
if (am == null) {
|
|
24
|
+
if (rssBytes != null) {
|
|
25
|
+
return@async MemoryUsage(rss = rssBytes.toDouble())
|
|
26
|
+
}
|
|
27
|
+
OneKeyLog.warn("PerfMemory", "ActivityManager unavailable and /proc/self/status unreadable")
|
|
28
|
+
return@async MemoryUsage(rss = 0.0)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
val pid = android.os.Process.myPid()
|
|
32
|
+
val memInfos = am.getProcessMemoryInfo(intArrayOf(pid))
|
|
33
|
+
|
|
34
|
+
if (memInfos == null || memInfos.isEmpty()) {
|
|
35
|
+
if (rssBytes != null) {
|
|
36
|
+
return@async MemoryUsage(rss = rssBytes.toDouble())
|
|
37
|
+
}
|
|
38
|
+
return@async MemoryUsage(rss = 0.0)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// totalPss is in KB
|
|
42
|
+
val pssBytes = memInfos[0].totalPss.toLong() * 1024L
|
|
43
|
+
val finalRss = rssBytes ?: pssBytes
|
|
44
|
+
|
|
45
|
+
MemoryUsage(rss = finalRss.toDouble())
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private fun readVmRssBytesFromProcStatus(): Long? {
|
|
50
|
+
try {
|
|
51
|
+
BufferedReader(FileReader("/proc/self/status")).use { reader ->
|
|
52
|
+
var line: String?
|
|
53
|
+
while (reader.readLine().also { line = it } != null) {
|
|
54
|
+
if (!line!!.startsWith("VmRSS:")) continue
|
|
55
|
+
// Example: "VmRSS:\t 123456 kB"
|
|
56
|
+
val parts = line!!.trim().split("\\s+".toRegex())
|
|
57
|
+
if (parts.size < 2) return null
|
|
58
|
+
val kb = parts[1].toLong()
|
|
59
|
+
return kb * 1024L
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} catch (_: Exception) {
|
|
63
|
+
// ignore
|
|
64
|
+
}
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package com.margelo.nitro.reactnativeperfmemory
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
|
+
|
|
8
|
+
class ReactNativePerfMemoryPackage : BaseReactPackage() {
|
|
9
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
10
|
+
return null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
14
|
+
return ReactModuleInfoProvider { HashMap() }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
companion object {
|
|
18
|
+
init {
|
|
19
|
+
System.loadLibrary("reactnativeperfmemory")
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import NitroModules
|
|
2
|
+
import ReactNativeNativeLogger
|
|
3
|
+
|
|
4
|
+
class ReactNativePerfMemory: HybridReactNativePerfMemorySpec {
|
|
5
|
+
|
|
6
|
+
func getMemoryUsage() throws -> Promise<MemoryUsage> {
|
|
7
|
+
return Promise.async {
|
|
8
|
+
// Prefer phys_footprint when available (closest to "real" memory impact)
|
|
9
|
+
var vmInfo = task_vm_info_data_t()
|
|
10
|
+
var count = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<natural_t>.size)
|
|
11
|
+
|
|
12
|
+
let kr = withUnsafeMutablePointer(to: &vmInfo) { ptr in
|
|
13
|
+
ptr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { intPtr in
|
|
14
|
+
task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), intPtr, &count)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if kr == KERN_SUCCESS {
|
|
19
|
+
let footprint = Double(vmInfo.phys_footprint)
|
|
20
|
+
return MemoryUsage(rss: footprint)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Fallback: resident size via mach_task_basic_info
|
|
24
|
+
var basicInfo = mach_task_basic_info_data_t()
|
|
25
|
+
var basicCount = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info_data_t>.size / MemoryLayout<natural_t>.size)
|
|
26
|
+
|
|
27
|
+
let basicKr = withUnsafeMutablePointer(to: &basicInfo) { ptr in
|
|
28
|
+
ptr.withMemoryRebound(to: integer_t.self, capacity: Int(basicCount)) { intPtr in
|
|
29
|
+
task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), intPtr, &basicCount)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if basicKr == KERN_SUCCESS {
|
|
34
|
+
return MemoryUsage(rss: Double(basicInfo.resident_size))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
OneKeyLog.warn("PerfMemory", "Failed to get memory usage, kern_return: \(kr)")
|
|
38
|
+
return MemoryUsage(rss: 0)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["ReactNativePerfMemory.nitro.ts"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
4
|
+
const ReactNativePerfMemoryHybridObject = NitroModules.createHybridObject('ReactNativePerfMemory');
|
|
5
|
+
export const ReactNativePerfMemory = ReactNativePerfMemoryHybridObject;
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["NitroModules","ReactNativePerfMemoryHybridObject","createHybridObject","ReactNativePerfMemory"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;AAGzD,MAAMC,iCAAiC,GACrCD,YAAY,CAACE,kBAAkB,CAA4B,uBAAuB,CAAC;AAErF,OAAO,MAAMC,qBAAqB,GAAGF,iCAAiC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules';
|
|
2
|
+
export interface MemoryUsage {
|
|
3
|
+
rss: number;
|
|
4
|
+
}
|
|
5
|
+
export interface ReactNativePerfMemory extends HybridObject<{
|
|
6
|
+
ios: 'swift';
|
|
7
|
+
android: 'kotlin';
|
|
8
|
+
}> {
|
|
9
|
+
getMemoryUsage(): Promise<MemoryUsage>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=ReactNativePerfMemory.nitro.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReactNativePerfMemory.nitro.d.ts","sourceRoot":"","sources":["../../../src/ReactNativePerfMemory.nitro.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE/D,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,qBACf,SAAQ,YAAY,CAAC;IAAE,GAAG,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,CAAC;IACzD,cAAc,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;CACxC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,IAAI,yBAAyB,EAAE,MAAM,+BAA+B,CAAC;AAKxG,eAAO,MAAM,qBAAqB,2BAAoC,CAAC;AACvE,mBAAmB,+BAA+B,CAAC"}
|
package/nitro.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cxxNamespace": ["reactnativeperfmemory"],
|
|
3
|
+
"ios": {
|
|
4
|
+
"iosModuleName": "ReactNativePerfMemory"
|
|
5
|
+
},
|
|
6
|
+
"android": {
|
|
7
|
+
"androidNamespace": ["reactnativeperfmemory"],
|
|
8
|
+
"androidCxxLibName": "reactnativeperfmemory"
|
|
9
|
+
},
|
|
10
|
+
"autolinking": {
|
|
11
|
+
"ReactNativePerfMemory": {
|
|
12
|
+
"swift": "ReactNativePerfMemory",
|
|
13
|
+
"kotlin": "ReactNativePerfMemory"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"ignorePaths": ["node_modules"]
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JHybridReactNativePerfMemorySpec.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include "JHybridReactNativePerfMemorySpec.hpp"
|
|
9
|
+
|
|
10
|
+
// Forward declaration of `MemoryUsage` to properly resolve imports.
|
|
11
|
+
namespace margelo::nitro::reactnativeperfmemory { struct MemoryUsage; }
|
|
12
|
+
|
|
13
|
+
#include "MemoryUsage.hpp"
|
|
14
|
+
#include <NitroModules/Promise.hpp>
|
|
15
|
+
#include <NitroModules/JPromise.hpp>
|
|
16
|
+
#include "JMemoryUsage.hpp"
|
|
17
|
+
|
|
18
|
+
namespace margelo::nitro::reactnativeperfmemory {
|
|
19
|
+
|
|
20
|
+
jni::local_ref<JHybridReactNativePerfMemorySpec::jhybriddata> JHybridReactNativePerfMemorySpec::initHybrid(jni::alias_ref<jhybridobject> jThis) {
|
|
21
|
+
return makeCxxInstance(jThis);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
void JHybridReactNativePerfMemorySpec::registerNatives() {
|
|
25
|
+
registerHybrid({
|
|
26
|
+
makeNativeMethod("initHybrid", JHybridReactNativePerfMemorySpec::initHybrid),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
size_t JHybridReactNativePerfMemorySpec::getExternalMemorySize() noexcept {
|
|
31
|
+
static const auto method = javaClassStatic()->getMethod<jlong()>("getMemorySize");
|
|
32
|
+
return method(_javaPart);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void JHybridReactNativePerfMemorySpec::dispose() noexcept {
|
|
36
|
+
static const auto method = javaClassStatic()->getMethod<void()>("dispose");
|
|
37
|
+
method(_javaPart);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
std::string JHybridReactNativePerfMemorySpec::toString() {
|
|
41
|
+
static const auto method = javaClassStatic()->getMethod<jni::JString()>("toString");
|
|
42
|
+
auto javaString = method(_javaPart);
|
|
43
|
+
return javaString->toStdString();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Properties
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
// Methods
|
|
50
|
+
std::shared_ptr<Promise<MemoryUsage>> JHybridReactNativePerfMemorySpec::getMemoryUsage() {
|
|
51
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("getMemoryUsage");
|
|
52
|
+
auto __result = method(_javaPart);
|
|
53
|
+
return [&]() {
|
|
54
|
+
auto __promise = Promise<MemoryUsage>::create();
|
|
55
|
+
__result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
|
|
56
|
+
auto __result = jni::static_ref_cast<JMemoryUsage>(__boxedResult);
|
|
57
|
+
__promise->resolve(__result->toCpp());
|
|
58
|
+
});
|
|
59
|
+
__result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
|
|
60
|
+
jni::JniException __jniError(__throwable);
|
|
61
|
+
__promise->reject(std::make_exception_ptr(__jniError));
|
|
62
|
+
});
|
|
63
|
+
return __promise;
|
|
64
|
+
}();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
} // namespace margelo::nitro::reactnativeperfmemory
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridReactNativePerfMemorySpec.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <NitroModules/JHybridObject.hpp>
|
|
11
|
+
#include <fbjni/fbjni.h>
|
|
12
|
+
#include "HybridReactNativePerfMemorySpec.hpp"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
namespace margelo::nitro::reactnativeperfmemory {
|
|
18
|
+
|
|
19
|
+
using namespace facebook;
|
|
20
|
+
|
|
21
|
+
class JHybridReactNativePerfMemorySpec: public jni::HybridClass<JHybridReactNativePerfMemorySpec, JHybridObject>,
|
|
22
|
+
public virtual HybridReactNativePerfMemorySpec {
|
|
23
|
+
public:
|
|
24
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/reactnativeperfmemory/HybridReactNativePerfMemorySpec;";
|
|
25
|
+
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis);
|
|
26
|
+
static void registerNatives();
|
|
27
|
+
|
|
28
|
+
protected:
|
|
29
|
+
// C++ constructor (called from Java via `initHybrid()`)
|
|
30
|
+
explicit JHybridReactNativePerfMemorySpec(jni::alias_ref<jhybridobject> jThis) :
|
|
31
|
+
HybridObject(HybridReactNativePerfMemorySpec::TAG),
|
|
32
|
+
HybridBase(jThis),
|
|
33
|
+
_javaPart(jni::make_global(jThis)) {}
|
|
34
|
+
|
|
35
|
+
public:
|
|
36
|
+
~JHybridReactNativePerfMemorySpec() override {
|
|
37
|
+
// Hermes GC can destroy JS objects on a non-JNI Thread.
|
|
38
|
+
jni::ThreadScope::WithClassLoader([&] { _javaPart.reset(); });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public:
|
|
42
|
+
size_t getExternalMemorySize() noexcept override;
|
|
43
|
+
void dispose() noexcept override;
|
|
44
|
+
std::string toString() override;
|
|
45
|
+
|
|
46
|
+
public:
|
|
47
|
+
inline const jni::global_ref<JHybridReactNativePerfMemorySpec::javaobject>& getJavaPart() const noexcept {
|
|
48
|
+
return _javaPart;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public:
|
|
52
|
+
// Properties
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
public:
|
|
56
|
+
// Methods
|
|
57
|
+
std::shared_ptr<Promise<MemoryUsage>> getMemoryUsage() override;
|
|
58
|
+
|
|
59
|
+
private:
|
|
60
|
+
friend HybridBase;
|
|
61
|
+
using HybridBase::HybridBase;
|
|
62
|
+
jni::global_ref<JHybridReactNativePerfMemorySpec::javaobject> _javaPart;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
} // namespace margelo::nitro::reactnativeperfmemory
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JMemoryUsage.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include "MemoryUsage.hpp"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
namespace margelo::nitro::reactnativeperfmemory {
|
|
16
|
+
|
|
17
|
+
using namespace facebook;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The C++ JNI bridge between the C++ struct "MemoryUsage" and the the Kotlin data class "MemoryUsage".
|
|
21
|
+
*/
|
|
22
|
+
struct JMemoryUsage final: public jni::JavaClass<JMemoryUsage> {
|
|
23
|
+
public:
|
|
24
|
+
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/reactnativeperfmemory/MemoryUsage;";
|
|
25
|
+
|
|
26
|
+
public:
|
|
27
|
+
/**
|
|
28
|
+
* Convert this Java/Kotlin-based struct to the C++ struct MemoryUsage by copying all values to C++.
|
|
29
|
+
*/
|
|
30
|
+
[[maybe_unused]]
|
|
31
|
+
[[nodiscard]]
|
|
32
|
+
MemoryUsage toCpp() const {
|
|
33
|
+
static const auto clazz = javaClassStatic();
|
|
34
|
+
static const auto fieldRss = clazz->getField<double>("rss");
|
|
35
|
+
double rss = this->getFieldValue(fieldRss);
|
|
36
|
+
return MemoryUsage(
|
|
37
|
+
rss
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public:
|
|
42
|
+
/**
|
|
43
|
+
* Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
|
|
44
|
+
*/
|
|
45
|
+
[[maybe_unused]]
|
|
46
|
+
static jni::local_ref<JMemoryUsage::javaobject> fromCpp(const MemoryUsage& value) {
|
|
47
|
+
using JSignature = JMemoryUsage(double);
|
|
48
|
+
static const auto clazz = javaClassStatic();
|
|
49
|
+
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
50
|
+
return create(
|
|
51
|
+
clazz,
|
|
52
|
+
value.rss
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
} // namespace margelo::nitro::reactnativeperfmemory
|