From 64916077c34b6f8a40ed91c868e9bd81f2e11de1 Mon Sep 17 00:00:00 2001
From: Florian Schmaus <flow@cs.fau.de>
Date: Wed, 9 Feb 2022 16:41:40 +0100
Subject: [PATCH] [Context] Make context size and assumed page size
 configurable

Also keep the context size at 64 KiB (there was a comment that
errorneously indicated that the context size is 4 MiB).
---
 emper/Context.hpp  |  6 +++++-
 emper/Emper.hpp    |  5 +++++
 emper/lib/math.hpp |  4 ++++
 meson.build        |  2 ++
 meson_options.txt  | 14 ++++++++++++++
 5 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/emper/Context.hpp b/emper/Context.hpp
index fee4f8bb..49f5c287 100644
--- a/emper/Context.hpp
+++ b/emper/Context.hpp
@@ -13,12 +13,15 @@
 #include "Common.hpp"	 // for func_t, DIE, ALIGN_TO_CACHE_LINE
 #include "Debug.hpp"	 // for LOGD, LogSubsystem, LogSubsystem::C, Logger
 #include "Emper.hpp"	 // for Emper::DEBUG
+#include "lib/math.hpp"
 
 class AbstractFiber;
 class ContextManager;
 class Dispatcher;
 class Fibril;
 
+namespace em = emper::lib::math;
+
 extern "C" [[noreturn]] void switch_and_load_context(void** toTos);
 // *Not* marked as 'noreturn' because save_and_switch_context does
 // *actually return at some point, unlike the other switch_*
@@ -28,7 +31,8 @@ extern "C" [[noreturn]] void switch_context(void** toTos);
 
 class ALIGN_TO_CACHE_LINE Context : Logger<LogSubsystem::C> {
  private:
-	static const unsigned int CONTEXT_SIZE = 0xffff;	// 1024 * 1024 * 4;
+	static constexpr size_t CONTEXT_SIZE =
+			em::roundUp(emper::MIN_CONTEXT_STACK_SIZE, emper::ASSUME_PAGE_SIZE);
 
 	static thread_local Context* currentContext;
 
diff --git a/emper/Emper.hpp b/emper/Emper.hpp
index 488b9bf8..1e805285 100644
--- a/emper/Emper.hpp
+++ b/emper/Emper.hpp
@@ -278,4 +278,9 @@ static const bool CONTEXT_MANAGER_WITH_MEMORY_MANAGER =
 		false
 #endif
 		;
+
+static const size_t ASSUME_PAGE_SIZE = EMPER_ASSUME_PAGE_SIZE;
+
+static const size_t MIN_CONTEXT_STACK_SIZE = EMPER_MIN_CONTEXT_STACK_SIZE;
+
 }	 // namespace emper
diff --git a/emper/lib/math.hpp b/emper/lib/math.hpp
index 953d7445..a2e8e95b 100644
--- a/emper/lib/math.hpp
+++ b/emper/lib/math.hpp
@@ -65,4 +65,8 @@ class RunningAverage {
 	}
 };
 
+constexpr size_t roundUp(size_t number, size_t multiple) {
+	size_t multiple_count = (number + multiple - 1) / multiple;
+	return multiple_count * multiple;
+}
 }	 // namespace emper::lib::math
diff --git a/meson.build b/meson.build
index 91d72dd8..5b3a16e7 100644
--- a/meson.build
+++ b/meson.build
@@ -70,6 +70,8 @@ conf_data.set('EMPER_CONTINUATION_STEALING_MODE', continuation_stealing_mode)
 conf_data.set('EMPER_CONTINUATION_STEALING_MADVISE_STACK', get_option('continuation_stealing_madvise_stack'))
 conf_data.set('EMPER_BUILD_WITH_CLANG', cpp_is_clang)
 conf_data.set('EMPER_CONTEXT_MANAGER_WITH_MEMORY_MANAGER', get_option('context_manager_with_memory_manager'))
+conf_data.set('EMPER_ASSUME_PAGE_SIZE', get_option('assume_page_size'))
+conf_data.set('EMPER_MIN_CONTEXT_STACK_SIZE', get_option('min_context_stack_size'))
 
 if continuation_stealing_mode == 'locked' and not locked_ws_queue
   error('*Locked* continuation stealing only works with locked work-stealing queues (locked_ws_queue=true)')
diff --git a/meson_options.txt b/meson_options.txt
index 0f7cabd9..cdf68da0 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -237,3 +237,17 @@ option(
   description: 'Use the memory manager in context manager for better context caching. This may increases the memory footprint',
   value: true
 )
+option(
+  'assume_page_size',
+  type: 'integer',
+  description: 'Assume the given page size in bytes',
+  value: 4096,
+  min: 1,
+)
+option(
+  'min_context_stack_size',
+  type: 'integer',
+  description: 'Set the size in bytes of stacks used by an execution context (will potentially be rounded up to the next page size)',
+  value: 65536,
+  min: 1,
+)
-- 
GitLab