Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RDART-1045: Expose setrlimit ios #1700

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ jobs:
differentiator: fi${{ github.run_id }}${{ github.run_attempt }}

flutter-tests-ios:
runs-on: macos-12
runs-on: macos-14
name: Flutter Tests iOS
timeout-minutes: 45
needs:
Expand Down
32 changes: 32 additions & 0 deletions packages/realm_dart/lib/src/native/realm_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3971,6 +3971,33 @@ class RealmLibrary {
late final _realm_dart_scheduler_invoke = _realm_dart_scheduler_invokePtr
.asFunction<void Function(int, ffi.Pointer<ffi.Void>)>();

/// implemented for iOS only (for now - valid for all posix)
/// /**
/// * Set the soft limit on number of open files
/// * @param limit The requested limit. If less than zero no attempt is made.
/// * @param[out] out_limit The actual limit set.
/// *
/// * @return true if no error occurred.
/// *
/// * @throws RLM_ERR_FILE_PERMISSION_DENIED if the operation was not permitted.
/// */
bool realm_dart_set_and_get_rlimit(
int limit,
ffi.Pointer<ffi.Long> out_limit,
) {
return _realm_dart_set_and_get_rlimit(
limit,
out_limit,
);
}

late final _realm_dart_set_and_get_rlimitPtr = _lookup<
ffi
.NativeFunction<ffi.Bool Function(ffi.Long, ffi.Pointer<ffi.Long>)>>(
'realm_dart_set_and_get_rlimit');
late final _realm_dart_set_and_get_rlimit = _realm_dart_set_and_get_rlimitPtr
.asFunction<bool Function(int, ffi.Pointer<ffi.Long>)>();

bool realm_dart_sync_after_reset_handler_callback(
ffi.Pointer<ffi.Void> userdata,
ffi.Pointer<realm_t> before_realm,
Expand Down Expand Up @@ -11833,6 +11860,11 @@ class _SymbolAddresses {
.NativeFunction<ffi.Void Function(ffi.Uint64, ffi.Pointer<ffi.Void>)>>
get realm_dart_scheduler_invoke =>
_library._realm_dart_scheduler_invokePtr;
ffi.Pointer<
ffi
.NativeFunction<ffi.Bool Function(ffi.Long, ffi.Pointer<ffi.Long>)>>
get realm_dart_set_and_get_rlimit =>
_library._realm_dart_set_and_get_rlimitPtr;
ffi.Pointer<
ffi.NativeFunction<
ffi.Bool Function(
Expand Down
8 changes: 8 additions & 0 deletions packages/realm_dart/lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,12 @@ class RealmCore {
String _getFilesPath() {
return realmLib.realm_dart_get_files_path().cast<Utf8>().toRealmDartString()!;
}

int setAndGetRLimit(int limit) {
return using((arena) {
final outLimit = arena<Long>();
realmLib.realm_dart_set_and_get_rlimit(limit, outLimit).raiseLastErrorIfFalse();
return outLimit.value;
});
}
}
38 changes: 35 additions & 3 deletions packages/realm_dart/src/ios/platform.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#include <string>
#include "../realm_dart.h"
#import <sys/utsname.h>
#include <string>
#include <sys/utsname.h>
#include <sys/resource.h>
#include <system_error>
#include <realm/object-store/c_api/util.hpp>

static std::string filesDir;
static std::string deviceModel;
Expand All @@ -48,7 +51,6 @@
}
}


RLM_API const char* realm_dart_get_files_path() {
if (filesDir == "") {
filesDir = default_realm_file_directory();
Expand All @@ -74,3 +76,33 @@

return deviceVersion.c_str();
}

namespace {
using namespace realm::c_api;

rlimit get_rlimit() {
rlimit rlim;
int status = getrlimit(RLIMIT_NOFILE, &rlim);
if (status < 0)
throw std::system_error(errno, std::system_category(), "getrlimit() failed");
return rlim;
}

long set_and_get_rlimit(long limit) {
if (limit > 0) {
auto rlim = get_rlimit();
rlim.rlim_cur = limit;
int status = setrlimit(RLIMIT_NOFILE, &rlim);
if (status < 0)
throw std::system_error(errno, std::system_category(), "setrlimit() failed");
}
return get_rlimit().rlim_cur;
}
}

RLM_API bool realm_dart_set_and_get_rlimit(long limit, long* out_limit) {
return wrap_err([&]() {
*out_limit = set_and_get_rlimit(limit);
return true;
});
}
12 changes: 12 additions & 0 deletions packages/realm_dart/src/realm_dart.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ RLM_API const char* realm_dart_get_device_version();
// implemented for Android only
RLM_API const char* realm_dart_get_bundle_id();

// implemented for iOS only (for now - valid for all posix)
/**
* Set the soft limit on number of open files
* @param limit The requested limit. If less than zero no attempt is made.
* @param[out] out_limit The actual limit set.
*
* @return true if no error occurred.
*
* @throws RLM_ERR_FILE_PERMISSION_DENIED if the operation was not permitted.
*/
RLM_API bool realm_dart_set_and_get_rlimit(long limit, long* out_limit);

RLM_API const char* realm_get_library_cpu_arch();

typedef struct realm_dart_userdata_async* realm_dart_userdata_async_t;
Expand Down
5 changes: 5 additions & 0 deletions packages/realm_dart/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ void setupTests() {
printOnFailure('${record.category} ${record.level.name}: ${record.message}');
});

if (Platform.isIOS) {
final maxFiles = realmCore.setAndGetRLimit(1024);
print('Max files: $maxFiles');
}

// Enable this to print platform info, including current PID
_printPlatformInfo();
});
Expand Down
Loading