#endif
/******************************* Networking ***********************************/
-extern unsigned smx_context_stack_size;
-extern unsigned smx_context_guard_size;
-
SG_BEGIN_DECL
XBT_ATTRIB_DEPRECATED_v331("Please use sg_actor_by_pid() instead.") XBT_PUBLIC smx_actor_t
SIMIX_process_from_PID(aid_t pid);
/* parallelism */
-XBT_PUBLIC int SIMIX_context_is_parallel();
-XBT_PUBLIC int SIMIX_context_get_nthreads();
-XBT_PUBLIC void SIMIX_context_set_nthreads(int nb_threads);
-XBT_PUBLIC e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode();
-XBT_PUBLIC void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::is_parallel()") XBT_PUBLIC int SIMIX_context_is_parallel();
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::get_nthreads()") XBT_PUBLIC int SIMIX_context_get_nthreads();
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::set_nthreads()") XBT_PUBLIC
+ void SIMIX_context_set_nthreads(int nb_threads);
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::get_parallel_mode()") XBT_PUBLIC e_xbt_parmap_mode_t
+ SIMIX_context_get_parallel_mode();
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::set_parallel_mode()") XBT_PUBLIC
+ void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
XBT_PUBLIC int SIMIX_is_maestro();
/********************************** Global ************************************/
"If you think you've found a bug in SimGrid, please report it along with a\n"
"Minimal Working Example (MWE) reproducing your problem and a full backtrace\n"
"of the fault captured with gdb or valgrind.\n",
- smx_context_stack_size / 1024);
+ simgrid::kernel::context::stack_size / 1024);
} else if (siginfo->si_signo == SIGSEGV) {
fprintf(stderr, "Segmentation fault.\n");
#if HAVE_SMPI
{
pid_ = maxpid++;
simcall_.issuer_ = this;
- stacksize_ = smx_context_stack_size;
+ stacksize_ = context::stack_size;
}
ActorImpl::~ActorImpl()
namespace context {
ContextFactoryInitializer factory_initializer = nullptr;
+static e_xbt_parmap_mode_t parallel_synchronization_mode = XBT_PARMAP_DEFAULT;
+static int parallel_contexts = 1;
+unsigned stack_size;
+unsigned guard_size;
+
+/** @brief Returns whether some parallel threads are used for the user contexts. */
+int is_parallel()
+{
+ return parallel_contexts > 1;
+}
+
+/**
+ * @brief Returns the number of parallel threads used for the user contexts.
+ * @return the number of threads (1 means no parallelism)
+ */
+int get_nthreads()
+{
+ return parallel_contexts;
+}
+
+/**
+ * @brief Sets the number of parallel threads to use for the user contexts.
+ *
+ * This function should be called before initializing SIMIX.
+ * A value of 1 means no parallelism (1 thread only).
+ * If the value is greater than 1, the thread support must be enabled.
+ *
+ * @param nb_threads the number of threads to use
+ */
+void set_nthreads(int nb_threads)
+{
+ if (nb_threads <= 0) {
+ nb_threads = std::thread::hardware_concurrency();
+ XBT_INFO("Auto-setting contexts/nthreads to %d", nb_threads);
+ }
+ parallel_contexts = nb_threads;
+}
+
+/**
+ * @brief Sets the synchronization mode to use when actors are run in parallel.
+ * @param mode how to synchronize threads if actors are run in parallel
+ */
+void set_parallel_mode(e_xbt_parmap_mode_t mode)
+{
+ parallel_synchronization_mode = mode;
+}
+
+/**
+ * @brief Returns the synchronization mode used when actors are run in parallel.
+ * @return how threads are synchronized if actors are run in parallel
+ */
+e_xbt_parmap_mode_t get_parallel_mode()
+{
+ return parallel_synchronization_mode;
+}
ContextFactory::~ContextFactory() = default;
#ifndef SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
#define SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
-#include "simgrid/forward.h"
+#include <simgrid/forward.h>
+#include <xbt/parmap.h>
+
#include "src/kernel/activity/ActivityImpl.hpp"
#include <csignal>
namespace simgrid {
namespace kernel {
namespace context {
+extern unsigned stack_size;
+extern unsigned guard_size;
class XBT_PUBLIC ContextFactory {
public:
XBT_PRIVATE ContextFactory* raw_factory();
XBT_PRIVATE ContextFactory* boost_factory();
+XBT_PUBLIC int is_parallel();
+XBT_PUBLIC int get_nthreads();
+XBT_PUBLIC void set_nthreads(int nb_threads);
+XBT_PUBLIC void set_parallel_mode(e_xbt_parmap_mode_t mode);
+XBT_PUBLIC e_xbt_parmap_mode_t get_parallel_mode();
} // namespace context
} // namespace kernel
} // namespace simgrid
: Context(std::move(code), actor), factory_(*factory)
{
// Save maestro (=first created context) in preparation for run_all
- if (not SIMIX_context_is_parallel() && factory_.maestro_context_ == nullptr)
+ if (not is_parallel() && factory_.maestro_context_ == nullptr)
factory_.maestro_context_ = this;
if (has_code()) {
xbt_assert((actor->get_stacksize() & 0xf) == 0, "Actor stack size should be multiple of 16");
- if (smx_context_guard_size > 0 && not MC_is_active()) {
+ if (guard_size > 0 && not MC_is_active()) {
#if PTH_STACKGROWTH != -1
xbt_die(
"Stack overflow protection is known to be broken on your system: you stacks grow upwards (or detection is "
* Protected pages need to be put after the stack when PTH_STACKGROWTH == 1. */
#endif
- size_t size = actor->get_stacksize() + smx_context_guard_size;
+ size_t size = actor->get_stacksize() + guard_size;
#if SIMGRID_HAVE_MC
/* Cannot use posix_memalign when SIMGRID_HAVE_MC. Align stack by hand, and save the
* pointer returned by xbt_malloc0. */
#ifndef _WIN32
/* This is fatal. We are going to fail at some point when we try reusing this. */
xbt_assert(
- mprotect(this->stack_, smx_context_guard_size, PROT_NONE) != -1,
+ mprotect(this->stack_, guard_size, PROT_NONE) != -1,
"Failed to protect stack: %s.\n"
"If you are running a lot of actors, you may be exceeding the amount of mappings allowed per process.\n"
"On Linux systems, change this value with sudo sysctl -w vm.max_map_count=newvalue (default value: 65536)\n"
"for more information.",
strerror(errno));
#endif
- this->stack_ = this->stack_ + smx_context_guard_size;
+ this->stack_ = this->stack_ + guard_size;
} else {
this->stack_ = static_cast<unsigned char*>(xbt_malloc0(actor->get_stacksize()));
}
#endif
#ifndef _WIN32
- if (smx_context_guard_size > 0 && not MC_is_active()) {
- stack_ = stack_ - smx_context_guard_size;
- if (mprotect(stack_, smx_context_guard_size, PROT_READ | PROT_WRITE) == -1) {
+ if (guard_size > 0 && not MC_is_active()) {
+ stack_ = stack_ - guard_size;
+ if (mprotect(stack_, guard_size, PROT_READ | PROT_WRITE) == -1) {
XBT_WARN("Failed to remove page protection: %s", strerror(errno));
/* try to pursue anyway */
}
* stuff It is much easier to understand what happens if you see the working threads as bodies that swap their soul
* for the ones of the simulated processes that must run.
*/
- if (SIMIX_context_is_parallel()) {
+ if (is_parallel()) {
// We lazily create the parmap so that all options are actually processed when doing so.
if (parmap_ == nullptr)
- parmap_ = std::make_unique<simgrid::xbt::Parmap<smx_actor_t>>(SIMIX_context_get_nthreads(),
- SIMIX_context_get_parallel_mode());
+ parmap_ = std::make_unique<simgrid::xbt::Parmap<smx_actor_t>>(get_nthreads(), get_parallel_mode());
// Usually, Parmap::apply() executes the provided function on all elements of the array.
// Here, the executed function does not return the control to the parmap before all the array is processed:
void SwappedContext::resume()
{
auto* old = static_cast<SwappedContext*>(self());
- if (SIMIX_context_is_parallel()) {
+ if (is_parallel()) {
// Save my current soul (either maestro, or one of the minions) in a thread-specific area
worker_context_ = old;
}
void SwappedContext::suspend()
{
SwappedContext* next_context;
- if (SIMIX_context_is_parallel()) {
+ if (is_parallel()) {
// Get some more work to directly swap into the next executable actor instead of yielding back to the parmap
boost::optional<smx_actor_t> next_work = factory_.parmap_->next();
if (next_work) {
ThreadContextFactory::ThreadContextFactory() : ContextFactory()
{
- if (smx_context_stack_size != 8 * 1024 * 1024)
+ if (stack_size != 8 * 1024 * 1024)
XBT_INFO("Stack size modifications are ignored by thread factory.");
- if (SIMIX_context_is_parallel())
+ if (is_parallel())
ParallelThreadContext::initialize();
}
ThreadContextFactory::~ThreadContextFactory()
{
- if (SIMIX_context_is_parallel())
+ if (is_parallel())
ParallelThreadContext::finalize();
}
ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
{
- if (SIMIX_context_is_parallel())
+ if (is_parallel())
return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
else
return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
void ThreadContextFactory::run_all()
{
- if (SIMIX_context_is_parallel()) {
+ if (is_parallel()) {
// Parallel execution
ParallelThreadContext::run_all();
} else {
void ParallelThreadContext::initialize()
{
- thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
+ thread_sem_ = new xbt::OsSemaphore(get_nthreads());
}
void ParallelThreadContext::finalize()
#if SIMGRID_HAVE_MC
if (MC_is_active()) {
- MC_register_stack_area(get_stack(), &(this->uc_), smx_context_stack_size);
+ MC_register_stack_area(get_stack(), &(this->uc_), stack_size);
}
#endif
}
#include "simgrid/sg_config.hpp"
#include "src/instr/instr_private.hpp"
#include "src/internal_config.h"
+#include "src/kernel/context/Context.hpp"
#include "src/kernel/lmm/maxmin.hpp"
#include "src/mc/mc_config.hpp"
#include "src/mc/mc_replay.hpp"
static void _sg_cfg_cb_contexts_parallel_mode(const std::string& mode_name)
{
if (mode_name == "posix") {
- SIMIX_context_set_parallel_mode(XBT_PARMAP_POSIX);
+ simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_POSIX);
} else if (mode_name == "futex") {
- SIMIX_context_set_parallel_mode(XBT_PARMAP_FUTEX);
+ simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_FUTEX);
} else if (mode_name == "busy_wait") {
- SIMIX_context_set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
+ simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
} else {
xbt_die("Command line setting of the parallel synchronization mode should "
"be one of \"posix\", \"futex\" or \"busy_wait\"");
"no");
simgrid::config::declare_flag<int>("contexts/stack-size", "Stack size of contexts in KiB (not with threads)",
- 8 * 1024, [](int value) { smx_context_stack_size = value * 1024; });
+ 8 * 1024, [](int value) { simgrid::kernel::context::stack_size = value * 1024; });
/* guard size for contexts stacks in memory pages */
#if defined(_WIN32) || (PTH_STACKGROWTH != -1)
#endif
simgrid::config::declare_flag<int>("contexts/guard-size", "Guard size for contexts stacks in memory pages",
default_guard_size,
- [](int value) { smx_context_guard_size = value * xbt_pagesize; });
+ [](int value) { simgrid::kernel::context::guard_size = value * xbt_pagesize; });
simgrid::config::declare_flag<int>("contexts/nthreads", "Number of parallel threads used to execute user contexts", 1,
- &SIMIX_context_set_nthreads);
+ &simgrid::kernel::context::set_nthreads);
/* synchronization mode for parallel user contexts */
#if HAVE_FUTEX_H
sg_config_cmd_line(argc, argv);
- xbt_mallocator_initialization_is_done(SIMIX_context_is_parallel());
+ xbt_mallocator_initialization_is_done(simgrid::kernel::context::is_parallel());
}
void sg_config_finalize()
context_factory_name("contexts/factory", (std::string("Possible values: ") + contexts_list()).c_str(),
context_factories.begin()->first);
-unsigned smx_context_stack_size;
-unsigned smx_context_guard_size;
-static int smx_parallel_contexts = 1;
-static e_xbt_parmap_mode_t smx_parallel_synchronization_mode = XBT_PARMAP_DEFAULT;
-
-/** @brief Returns whether some parallel threads are used for the user contexts. */
-int SIMIX_context_is_parallel() {
- return smx_parallel_contexts > 1;
+int SIMIX_context_is_parallel() // XBT_ATTRIB_DEPRECATED_v333
+{
+ return simgrid::kernel::context::is_parallel();
}
-/**
- * @brief Returns the number of parallel threads used for the user contexts.
- * @return the number of threads (1 means no parallelism)
- */
-int SIMIX_context_get_nthreads() {
- return smx_parallel_contexts;
+int SIMIX_context_get_nthreads() // XBT_ATTRIB_DEPRECATED_v333
+{
+ return simgrid::kernel::context::get_nthreads();
}
-/**
- * @brief Sets the number of parallel threads to use
- * for the user contexts.
- *
- * This function should be called before initializing SIMIX.
- * A value of 1 means no parallelism (1 thread only).
- * If the value is greater than 1, the thread support must be enabled.
- *
- * @param nb_threads the number of threads to use
- */
-void SIMIX_context_set_nthreads(int nb_threads) {
- if (nb_threads<=0) {
- nb_threads = std::thread::hardware_concurrency();
- XBT_INFO("Auto-setting contexts/nthreads to %d", nb_threads);
- }
- smx_parallel_contexts = nb_threads;
+void SIMIX_context_set_nthreads(int nb_threads) // XBT_ATTRIB_DEPRECATED_v333
+{
+ simgrid::kernel::context::set_nthreads(nb_threads);
}
-/**
- * @brief Returns the synchronization mode used when processes are run in
- * parallel.
- * @return how threads are synchronized if processes are run in parallel
- */
-e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode() {
- return smx_parallel_synchronization_mode;
+e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode() // XBT_ATTRIB_DEPRECATED_v333
+{
+ return simgrid::kernel::context::get_parallel_mode();
}
-/**
- * @brief Sets the synchronization mode to use when processes are run in
- * parallel.
- * @param mode how to synchronize threads if processes are run in parallel
- */
-void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode) {
- smx_parallel_synchronization_mode = mode;
+void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode) // XBT_ATTRIB_DEPRECATED_v333
+{
+ simgrid::kernel::context::set_parallel_mode(mode);
}
namespace simgrid {
XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes);
XBT_INFO("%s", "");
- SIMIX_context_set_nthreads(nthreads);
+ simgrid::kernel::context::set_nthreads(nthreads);
XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):");
bench_all_modes(nthreads, timeout, modes, true, &fun_small_comp);
int status = 0;
xbt_log_control_set("parmap_test.fmt:[%c/%p]%e%m%n");
simgrid::s4u::Engine e(&argc, argv);
- SIMIX_context_set_nthreads(16); // dummy value > 1
+ simgrid::kernel::context::set_nthreads(16); // dummy value > 1
XBT_INFO("Basic testing posix");
status += test_parmap_basic(XBT_PARMAP_POSIX);