From 054ca8141cc683765251a78c43b33116a73512e9 Mon Sep 17 00:00:00 2001 From: Alexander Bondarenko <486682+dpwiz@users.noreply.github.com> Date: Thu, 28 Mar 2024 18:26:17 +0200 Subject: [PATCH] unify init_haskell to allow nse profiling --- apps/ios/Shared/SimpleXApp.swift | 6 +--- .../ios/SimpleX NSE/NotificationService.swift | 4 +-- apps/ios/SimpleXChat/hs_init.c | 31 ++++++++++++++----- apps/ios/SimpleXChat/hs_init.h | 2 +- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/apps/ios/Shared/SimpleXApp.swift b/apps/ios/Shared/SimpleXApp.swift index 25fbae043b..dcf18470eb 100644 --- a/apps/ios/Shared/SimpleXApp.swift +++ b/apps/ios/Shared/SimpleXApp.swift @@ -23,11 +23,7 @@ struct SimpleXApp: App { init() { DispatchQueue.global(qos: .background).sync { // we have to use debug profile file name without extension here because .hp extension is added by profiler - haskell_init( - getAppEventLogPath().path, - getAppDebugProfilePrefixPath().path - ) -// hs_init(0, nil) + haskell_init(0, getAppEventLogPath().path, nil) } UserDefaults.standard.register(defaults: appDefaults) setGroupDefaults() diff --git a/apps/ios/SimpleX NSE/NotificationService.swift b/apps/ios/SimpleX NSE/NotificationService.swift index 3da4775382..2329014390 100644 --- a/apps/ios/SimpleX NSE/NotificationService.swift +++ b/apps/ios/SimpleX NSE/NotificationService.swift @@ -396,7 +396,7 @@ func startChat() -> DBMigrationResult? { startLock.wait() defer { startLock.signal() } - + if hasChatCtrl() { return switch NSEChatState.shared.value { case .created: doStartChat() @@ -415,7 +415,7 @@ func startChat() -> DBMigrationResult? { func doStartChat() -> DBMigrationResult? { logger.debug("NotificationService: doStartChat") - haskell_init_nse() + haskell_init(1, nil, getAppDebugProfilePrefixPath().path) let (_, dbStatus) = chatMigrateInit(confirmMigrations: defaultMigrationConfirmation(), backgroundMode: true) logger.debug("NotificationService: doStartChat \(String(describing: dbStatus))") if dbStatus != .ok { diff --git a/apps/ios/SimpleXChat/hs_init.c b/apps/ios/SimpleXChat/hs_init.c index c9f554fdc6..fae05d650b 100644 --- a/apps/ios/SimpleXChat/hs_init.c +++ b/apps/ios/SimpleXChat/hs_init.c @@ -11,16 +11,29 @@ extern void hs_init_with_rtsopts(int * argc, char **argv[]); -void haskell_init(const char *eventlog, const char *heap_profile) { +void haskell_init(int nse, const char *eventlog, const char *heap_profile) { // setup static arena for bump allocation and passing to RTS char *argv[32] = {0,}; int argc = 0; // number of arguments used so far, always stands at the first NULL in argv // common args - argv[argc++] = "simplex"; // fake program name + if (nse) { + argv[argc++] = "simplex-nse"; // fake program name + } else { + argv[argc++] = "simplex"; + } argv[argc++] = "+RTS"; // start adding RTS options - argv[argc++] = "-T"; // make GC counters available from inside the program - argv[argc++] = "-A64m"; // chunk size for new allocations (less frequent GC) - argv[argc++] = "-H64m"; // larger heap size on start (faster boot) + if (nse) { + argv[argc++] = "-S"; // spam stdout with GC stats + argv[argc++] = "-A1m"; // chunk size for new allocations (less frequent GC) + argv[argc++] = "-H2m"; // larger heap size on start (faster boot) + argv[argc++] = "-M12m"; // hard limit on heap + argv[argc++] = "-F0.5"; // heap growth triggering GC + argv[argc++] = "-Fd1"; // memory return + } else { + argv[argc++] = "-T"; // make GC counters available from inside the program + argv[argc++] = "-A64m"; // chunk size for new allocations (less frequent GC) + argv[argc++] = "-H64m"; // larger heap size on start (faster boot) + } // argv[argc++] = "-M8G"; // keep memory usage under 8G, collecting more aggressively when approaching it (and crashing sooner rather than taking down the whole system) if (eventlog) { static char ol[1024] = "-ol"; @@ -34,8 +47,12 @@ void haskell_init(const char *eventlog, const char *heap_profile) { argv[argc++] = po; // adds ".hp" extension argv[argc++] = "-hT"; // emit heap profile by closure type } - int non_moving_gc = !heap_profile; // not compatible with heap profile - if (non_moving_gc) argv[argc++] = "-xn"; + if (nse) { + argv[argc++] = "-c"; // compacting garbage collector + } else { + int non_moving_gc = !heap_profile; // not compatible with heap profile + if (non_moving_gc) argv[argc++] = "-xn"; + } // wrap args as expected by RTS char **pargv = argv; hs_init_with_rtsopts(&argc, &pargv); diff --git a/apps/ios/SimpleXChat/hs_init.h b/apps/ios/SimpleXChat/hs_init.h index 7c3cfea548..db64ae45a7 100644 --- a/apps/ios/SimpleXChat/hs_init.h +++ b/apps/ios/SimpleXChat/hs_init.h @@ -9,7 +9,7 @@ #ifndef hs_init_h #define hs_init_h -void haskell_init(const char *eventlog, const char *heap_profile); +void haskell_init(int nse, const char *eventlog, const char *heap_profile); void haskell_init_nse(void);