1 /* Copyright (c) 2013-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <simgrid/s4u/Engine.hpp>
8 #include "src/instr/instr_private.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/resource/DiskImpl.hpp"
11 #include "src/kernel/resource/VirtualMachineImpl.hpp"
12 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
13 #include "surf/surf.hpp"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
27 XBT_DEBUG("Consume all trace events occurring before the starting time.");
28 double next_event_date;
29 while ((next_event_date = simgrid::kernel::profile::future_evt_set.next_date()) != -1.0) {
30 if (next_event_date > NOW)
34 simgrid::kernel::resource::Resource* resource = nullptr;
35 while (auto* event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource)) {
37 resource->apply_event(event, value);
41 XBT_DEBUG("Set every models in the right state by updating them to 0.");
42 for (auto const& model : simgrid::kernel::EngineImpl::get_instance()->get_all_models())
43 model->update_actions_state(NOW, 0.0);
46 double surf_solve(double max_date)
48 double time_delta = -1.0; /* duration */
50 simgrid::kernel::resource::Resource* resource = nullptr;
52 if (max_date != -1.0) {
53 xbt_assert(max_date >= NOW, "You asked to simulate up to %f, but that's in the past already", max_date);
55 time_delta = max_date - NOW;
58 XBT_DEBUG("Looking for next event in all models");
59 auto engine = simgrid::kernel::EngineImpl::get_instance();
60 for (auto model : engine->get_all_models()) {
61 if (not model->next_occurring_event_is_idempotent()) {
64 double next_event = model->next_occurring_event(NOW);
65 if ((time_delta < 0.0 || next_event < time_delta) && next_event >= 0.0) {
66 time_delta = next_event;
70 XBT_DEBUG("Min for resources (remember that NS3 don't update that value): %f", time_delta);
72 XBT_DEBUG("Looking for next trace event");
74 while (true) { // Handle next occurring events until none remains
75 double next_event_date = simgrid::kernel::profile::future_evt_set.next_date();
76 XBT_DEBUG("Next TRACE event: %f", next_event_date);
78 for (auto model : engine->get_all_models()) {
79 /* Skip all idempotent models, they were already treated above
80 * NS3 is the one to handled here */
81 if (model->next_occurring_event_is_idempotent())
84 if (next_event_date != -1.0) {
85 time_delta = std::min(next_event_date - NOW, time_delta);
87 time_delta = std::max(next_event_date - NOW, time_delta); // Get the positive component
90 XBT_DEBUG("Run the NS3 network at most %fs", time_delta);
91 // run until min or next flow
92 double model_next_action_end = model->next_occurring_event(time_delta);
94 XBT_DEBUG("Min for network : %f", model_next_action_end);
95 if (model_next_action_end >= 0.0)
96 time_delta = model_next_action_end;
99 if (next_event_date < 0.0 || (next_event_date > NOW + time_delta)) {
100 // next event may have already occurred or will after the next resource change, then bail out
101 XBT_DEBUG("no next usable TRACE event. Stop searching for it");
105 XBT_DEBUG("Updating models (min = %g, NOW = %g, next_event_date = %g)", time_delta, NOW, next_event_date);
107 while (auto* event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource)) {
108 if (resource->is_used() || (watched_hosts().find(resource->get_cname()) != watched_hosts().end())) {
109 time_delta = next_event_date - NOW;
110 XBT_DEBUG("This event invalidates the next_occurring_event() computation of models. Next event set to %f",
113 // FIXME: I'm too lame to update NOW live, so I change it and restore it so that the real update with surf_min
115 double round_start = NOW;
116 NOW = next_event_date;
117 /* update state of the corresponding resource to the new value. Does not touch lmm.
118 It will be modified if needed when updating actions */
119 XBT_DEBUG("Calling update_resource_state for resource %s", resource->get_cname());
120 resource->apply_event(event, value);
125 /* FIXME: Moved this test to here to avoid stopping simulation if there are actions running on cpus and all cpus are
126 * with availability = 0. This may cause an infinite loop if one cpu has a trace with periodicity = 0 and the other a
127 * trace with periodicity > 0.
128 * The options are: all traces with same periodicity(0 or >0) or we need to change the way how the events are managed
130 if (time_delta < 0) {
131 XBT_DEBUG("No next event at all. Bail out now.");
135 XBT_DEBUG("Duration set to %f", time_delta);
137 // Bump the time: jump into the future
138 NOW = NOW + time_delta;
140 // Inform the models of the date change
141 for (auto const& model : simgrid::kernel::EngineImpl::get_instance()->get_all_models())
142 model->update_actions_state(NOW, time_delta);
144 simgrid::s4u::Engine::on_time_advance(time_delta);