Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8299b4bf77037bf299c46b474462bd79af39bf52
[simgrid.git] / src / surf / disk_s19.cpp
1 /* Copyright (c) 2013-2022. 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 "simgrid/sg_config.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
12 #include "src/kernel/EngineImpl.hpp"
13 #include "src/kernel/lmm/maxmin.hpp"
14 #include "src/kernel/resource/profile/Event.hpp"
15 #include "src/surf/disk_s19.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_disk);
18 /***********
19  * Options *
20  ***********/
21 static simgrid::config::Flag<std::string> cfg_disk_solver("disk/solver",
22                                                           "Set linear equations solver used by disk model", "maxmin",
23                                                           &simgrid::kernel::lmm::System::validate_solver);
24
25 /*********
26  * Model *
27  *********/
28
29 void surf_disk_model_init_S19()
30 {
31   auto disk_model = std::make_shared<simgrid::kernel::resource::DiskS19Model>("Disk");
32   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
33   engine->add_model(disk_model);
34   engine->get_netzone_root()->set_disk_model(disk_model);
35 }
36
37 namespace simgrid::kernel::resource {
38 /*********
39  * Model *
40  *********/
41
42 DiskS19Model::DiskS19Model(const std::string& name) : DiskModel(name)
43 {
44   set_maxmin_system(lmm::System::build(cfg_disk_solver.get(), true /* selective update */));
45 }
46
47
48 DiskImpl* DiskS19Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
49 {
50   return (new DiskS19(name, read_bandwidth, write_bandwidth))->set_model(this);
51 }
52
53 void DiskS19Model::update_actions_state(double /*now*/, double delta)
54 {
55   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
56     auto& action = *it;
57     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
58     action.update_remains(rint(action.get_rate() * delta));
59     action.update_max_duration(delta);
60
61     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
62         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
63       action.finish(Action::State::FINISHED);
64     }
65   }
66 }
67
68 DiskAction* DiskS19::io_start(sg_size_t size, s4u::Io::OpType type)
69 {
70   auto* action = new DiskS19Action(get_model(), static_cast<double>(size), not is_on());
71   get_model()->get_maxmin_system()->expand(get_constraint(), action->get_variable(), 1.0);
72   switch (type) {
73     case s4u::Io::OpType::READ:
74       get_model()->get_maxmin_system()->expand(get_read_constraint(), action->get_variable(), 1.0);
75       break;
76     case s4u::Io::OpType::WRITE:
77       get_model()->get_maxmin_system()->expand(get_write_constraint(), action->get_variable(), 1.0);
78       break;
79     default:
80       THROW_UNIMPLEMENTED;
81   }
82   if (const auto& factor_cb = get_factor_cb()) { // handling disk variability
83     action->set_rate_factor(factor_cb(size, type));
84   }
85   return action;
86 }
87
88 /************
89  * Resource *
90  ************/
91 void DiskS19::set_read_bandwidth(double value)
92 {
93   DiskImpl::set_read_bandwidth(value);
94   if (get_read_constraint()) {
95     get_model()->get_maxmin_system()->update_constraint_bound(get_read_constraint(), get_read_bandwidth());
96   }
97 }
98
99 void DiskS19::set_write_bandwidth(double value)
100 {
101   DiskImpl::set_write_bandwidth(value);
102   if (get_write_constraint()) {
103     get_model()->get_maxmin_system()->update_constraint_bound(get_write_constraint(), get_write_bandwidth());
104   }
105 }
106
107 void DiskS19::set_readwrite_bandwidth(double value)
108 {
109   DiskImpl::set_readwrite_bandwidth(value);
110   if (get_constraint()) {
111     get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), get_readwrite_bandwidth());
112   }
113 }
114
115 void DiskS19::apply_event(kernel::profile::Event* triggered, double value)
116 {
117   /* Find out which of my iterators was triggered, and react accordingly */
118   if (triggered == get_read_event()) {
119     set_read_bandwidth(value);
120     unref_read_event();
121   } else if (triggered == get_write_event()) {
122     set_write_bandwidth(value);
123     unref_write_event();
124   } else if (triggered == get_state_event()) {
125     if (value > 0)
126       turn_on();
127     else
128       turn_off();
129     unref_state_event();
130   } else {
131     xbt_die("Unknown event!\n");
132   }
133
134   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
135             get_constraint());
136 }
137
138 /**********
139  * Action *
140  **********/
141
142 DiskS19Action::DiskS19Action(Model* model, double cost, bool failed)
143     : DiskAction(model, cost, failed, model->get_maxmin_system()->variable_new(this, 1.0, -1.0, 3))
144 {
145 }
146
147 } // namespace simgrid::kernel::resource