Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2ccb39eded5101d3bbae0dc2ba3f161d4de19593
[simgrid.git] / src / surf / disk_s19.cpp
1 /* Copyright (c) 2013-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "disk_s19.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "src/kernel/EngineImpl.hpp"
12 #include "src/kernel/lmm/maxmin.hpp"
13 #include "src/kernel/resource/profile/Event.hpp"
14 #include "src/surf/xml/platf.hpp"
15 #include "surf/surf.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_disk);
18
19 /*********
20  * Model *
21  *********/
22
23 void surf_disk_model_init_default()
24 {
25   auto disk_model = std::make_shared<simgrid::kernel::resource::DiskS19Model>("Disk");
26   simgrid::kernel::EngineImpl::get_instance()->add_model(disk_model);
27   simgrid::s4u::Engine::get_instance()->get_netzone_root()->get_impl()->set_disk_model(disk_model);
28 }
29
30 namespace simgrid {
31 namespace kernel {
32 namespace resource {
33
34 DiskImpl* DiskS19Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
35 {
36   return (new DiskS19(name, read_bandwidth, write_bandwidth))->set_model(this);
37 }
38
39 void DiskS19Model::update_actions_state(double /*now*/, double delta)
40 {
41   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
42     auto& action = *it;
43     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
44     action.update_remains(rint(action.get_variable()->get_value() * delta));
45     action.update_max_duration(delta);
46
47     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
48         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
49       action.finish(Action::State::FINISHED);
50     }
51   }
52 }
53
54 DiskAction* DiskS19Model::io_start(const DiskImpl* disk, sg_size_t size, s4u::Io::OpType type)
55 {
56   auto* action = new DiskS19Action(this, static_cast<double>(size), not disk->is_on());
57   get_maxmin_system()->expand(disk->get_constraint(), action->get_variable(), 1.0);
58   switch (type) {
59     case s4u::Io::OpType::READ:
60       get_maxmin_system()->expand(disk->get_read_constraint(), action->get_variable(), 1.0);
61       break;
62     case s4u::Io::OpType::WRITE:
63       get_maxmin_system()->expand(disk->get_write_constraint(), action->get_variable(), 1.0);
64       break;
65     default:
66       THROW_UNIMPLEMENTED;
67   }
68   return action;
69 }
70
71 /************
72  * Resource *
73  ************/
74 void DiskS19::update_penalties(double delta)
75 {
76   const kernel::lmm::Element* elem     = nullptr;
77   const kernel::lmm::Element* nextelem = nullptr;
78   int numelem                          = 0;
79   while (const auto* var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem)) {
80     auto* action = static_cast<DiskS19Action*>(var->get_id());
81     action->sharing_penalty_ += delta;
82     if (not action->is_suspended())
83       get_model()->get_maxmin_system()->update_variable_penalty(action->get_variable(), action->sharing_penalty_);
84   }
85 }
86
87 void DiskS19::set_read_bandwidth(double value)
88 {
89   read_bw_.peak = value;
90
91   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
92                                                             sg_bandwidth_factor * (read_bw_.peak * read_bw_.scale));
93
94   double delta = 1.0 / value - 1.0 / (read_bw_.peak * read_bw_.scale);
95   update_penalties(delta);
96 }
97
98 void DiskS19::set_write_bandwidth(double value)
99 {
100   write_bw_.peak = value;
101
102   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
103                                                             sg_bandwidth_factor * (write_bw_.peak * write_bw_.scale));
104
105   double delta = 1.0 / value - 1.0 / (write_bw_.peak * write_bw_.scale);
106   update_penalties(delta);
107 }
108
109 void DiskS19::apply_event(kernel::profile::Event* triggered, double value)
110 {
111   /* Find out which of my iterators was triggered, and react accordingly */
112   if (triggered == read_bw_.event) {
113     set_read_bandwidth(value);
114     tmgr_trace_event_unref(&read_bw_.event);
115
116   } else if (triggered == write_bw_.event) {
117     set_write_bandwidth(value);
118     tmgr_trace_event_unref(&write_bw_.event);
119
120   } else if (triggered == state_event_) {
121     if (value > 0)
122       turn_on();
123     else
124       turn_off();
125     tmgr_trace_event_unref(&state_event_);
126   } else {
127     xbt_die("Unknown event!\n");
128   }
129
130   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
131             get_constraint());
132 }
133
134 /**********
135  * Action *
136  **********/
137
138 DiskS19Action::DiskS19Action(Model* model, double cost, bool failed)
139     : DiskAction(model, cost, failed, model->get_maxmin_system()->variable_new(this, 1.0, -1.0, 3))
140 {
141 }
142
143 void DiskS19Action::update_remains_lazy(double /*now*/)
144 {
145   THROW_IMPOSSIBLE;
146 }
147 } // namespace resource
148 } // namespace kernel
149 } // namespace simgrid