Skip to content

Commit a6903a6

Browse files
mkopcinsmsluszniak
andcommitted
feat: added emulator detection (#1021)
Added emulator/simulator detection for logging purposes - [ ] Yes - [x] No - [ ] Bug fix (change which fixes an issue) - [x] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) - [x] iOS - [x] Android 1. Launch any example app (i.e. apps/computer-vision) 2. Connect to Ru machine via ssh, then `cd telemetry && docker compose logs --follow api` 3. Download any model (make sure the model is not currently downloaded) 4. You should see a log looking something like this: ``` api-1 | /downloads { api-1 | modelName: 'efficientnet-v2-s-quantized', api-1 | countryCode: 'US', api-1 | isEmulator: true, api-1 | libVersion: '0.9.0' api-1 | } ``` <!-- Add screenshots here, if applicable --> <!-- Link related issues here using #issue-number --> - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. --> --------- Co-authored-by: Mateusz Sluszniak <56299341+msluszniak@users.noreply.github.com>
1 parent 74f92bb commit a6903a6

File tree

7 files changed

+59
-6
lines changed

7 files changed

+59
-6
lines changed

packages/react-native-executorch/android/src/main/cpp/ETInstallerModule.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <rnexecutorch/RnExecutorchInstaller.h>
44

5+
#include "EmulatorDetection.h"
6+
57
#include <jni.h>
68
#include <jsi/jsi.h>
79

@@ -64,8 +66,10 @@ void ETInstallerModule::injectJSIBindings() {
6466
return std::vector<std::byte>(dataBytePtr, dataBytePtr + size);
6567
};
6668

69+
auto _isEmulator = isEmulator();
70+
6771
RnExecutorchInstaller::injectJSIBindings(jsiRuntime_, jsCallInvoker_,
68-
fetchDataByUrl);
72+
fetchDataByUrl, _isEmulator);
6973
}
7074
} // namespace rnexecutorch
7175

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <sys/system_properties.h>
5+
6+
namespace rnexecutorch {
7+
8+
inline bool isEmulator() {
9+
auto readProp = [](const char *key) -> std::string {
10+
#if __ANDROID_API__ >= 26
11+
const prop_info *pi = __system_property_find(key);
12+
if (pi == nullptr) {
13+
return "";
14+
}
15+
std::string result;
16+
__system_property_read_callback(
17+
pi,
18+
[](void *cookie, const char * /*__name*/, const char *value,
19+
uint32_t /*__serial*/) {
20+
*static_cast<std::string *>(cookie) = value;
21+
},
22+
&result);
23+
return result;
24+
#else
25+
char value[PROP_VALUE_MAX] = {0};
26+
__system_property_get(key, value);
27+
return {value};
28+
#endif
29+
};
30+
31+
std::string fp = readProp("ro.build.fingerprint");
32+
std::string hw = readProp("ro.hardware");
33+
return fp.find("generic") == 0 || hw == "goldfish" || hw == "ranchu";
34+
}
35+
36+
} // namespace rnexecutorch

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <rnexecutorch/models/object_detection/ObjectDetection.h>
1111
#include <rnexecutorch/models/ocr/OCR.h>
1212
#include <rnexecutorch/models/speech_to_text/SpeechToText.h>
13-
#include <rnexecutorch/models/text_to_speech/TextToSpeech.h>
1413
#include <rnexecutorch/models/style_transfer/StyleTransfer.h>
1514
#include <rnexecutorch/models/text_to_image/TextToImage.h>
15+
#include <rnexecutorch/models/text_to_speech/TextToSpeech.h>
1616
#include <rnexecutorch/models/vertical_ocr/VerticalOCR.h>
1717
#include <rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.h>
1818
#include <rnexecutorch/threads/GlobalThreadPool.h>
@@ -33,9 +33,12 @@ FetchUrlFunc_t fetchUrlFunc;
3333

3434
void RnExecutorchInstaller::injectJSIBindings(
3535
jsi::Runtime *jsiRuntime, std::shared_ptr<react::CallInvoker> jsCallInvoker,
36-
FetchUrlFunc_t fetchDataFromUrl) {
36+
FetchUrlFunc_t fetchDataFromUrl, bool isEmulator) {
3737
fetchUrlFunc = fetchDataFromUrl;
3838

39+
jsiRuntime->global().setProperty(*jsiRuntime, "__rne_isEmulator",
40+
jsi::Value(isEmulator));
41+
3942
jsiRuntime->global().setProperty(
4043
*jsiRuntime, "loadStyleTransfer",
4144
RnExecutorchInstaller::loadModel<models::style_transfer::StyleTransfer>(

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RnExecutorchInstaller {
2222
static void
2323
injectJSIBindings(jsi::Runtime *jsiRuntime,
2424
std::shared_ptr<react::CallInvoker> jsCallInvoker,
25-
FetchUrlFunc_t fetchDataFromUrl);
25+
FetchUrlFunc_t fetchDataFromUrl, bool isEmulator);
2626

2727
private:
2828
template <typename ModelT>

packages/react-native-executorch/ios/RnExecutorch/ETInstaller.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#import <React/RCTCallInvoker.h>
66
#import <ReactCommon/RCTTurboModule.h>
7+
#import <TargetConditionals.h>
78
#include <rnexecutorch/RnExecutorchInstaller.h>
89
#include <stdexcept>
910

@@ -41,8 +42,9 @@ @implementation ETInstaller
4142
throw std::runtime_error("Error fetching data from a url");
4243
}
4344
};
45+
bool isEmulator = TARGET_OS_SIMULATOR;
4446
rnexecutorch::RnExecutorchInstaller::injectJSIBindings(
45-
jsiRuntime, jsCallInvoker, fetchUrl);
47+
jsiRuntime, jsCallInvoker, fetchUrl, isEmulator);
4648

4749
NSLog(@"Successfully installed JSI bindings for react-native-executorch!");
4850
return @true;

packages/react-native-executorch/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ declare global {
4646
symbols: string,
4747
independentCharacters?: boolean
4848
) => any;
49+
// eslint-disable-next-line camelcase
50+
var __rne_isEmulator: boolean;
4951
}
5052
// eslint-disable no-var
5153
if (
@@ -63,7 +65,8 @@ if (
6365
global.loadSpeechToText == null ||
6466
global.loadTextToSpeechKokoro == null ||
6567
global.loadOCR == null ||
66-
global.loadVerticalOCR == null
68+
global.loadVerticalOCR == null ||
69+
global.__rne_isEmulator == null
6770
) {
6871
if (!ETInstallerNativeModule) {
6972
throw new Error(

packages/react-native-executorch/src/utils/ResourceFetcherUtils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ export namespace ResourceFetcherUtils {
176176
}
177177
}
178178

179+
export function isEmulator(): boolean {
180+
return global.__rne_isEmulator;
181+
}
182+
179183
export async function checkFileExists(fileUri: string) {
180184
const fileInfo = await getInfoAsync(fileUri);
181185
return fileInfo.exists;
@@ -214,6 +218,7 @@ export namespace ResourceFetcherUtils {
214218
body: JSON.stringify({
215219
modelName: getModelNameFromUri(uri),
216220
countryCode: getCountryCode(),
221+
isEmulator: isEmulator(),
217222
libVersion: LIB_VERSION,
218223
}),
219224
});

0 commit comments

Comments
 (0)