Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify writing in model setup + may fix issue with unit-tests
[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   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
27   engine->add_model(disk_model);
28   engine->get_netzone_root()->set_disk_model(disk_model);
29 }
30
31 namespace simgrid {
32 namespace kernel {
33 namespace resource {
34
35 DiskImpl* DiskS19Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
36 {
37   return (new DiskS19(name, read_bandwidth, write_bandwidth))->set_model(this);
38 }
39
40 void DiskS19Model::update_actions_state(double /*now*/, double delta)
41 {
42   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
43     auto& action = *it;
44     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
45     action.update_remains(rint(action.get_rate() * delta));
46     action.update_max_duration(delta);
47
48     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
49         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
50       action.finish(Action::State::FINISHED);
51     }
52   }
53 }
54
55 DiskAction* DiskS19Model::io_start(const DiskImpl* disk, sg_size_t size, s4u::Io::OpType type)
56 {
57   auto* action = new DiskS19Action(this, static_cast<double>(size), not disk->is_on());
58   get_maxmin_system()->expand(disk->get_constraint(), action->get_variable(), 1.0);
59   switch (type) {
60     case s4u::Io::OpType::READ:
61       get_maxmin_system()->expand(disk->get_read_constraint(), action->get_variable(), 1.0);
62       break;
63     case s4u::Io::OpType::WRITE:
64       get_maxmin_system()->expand(disk->get_write_constraint(), action->get_variable(), 1.0);
65       break;
66     default:
67       THROW_UNIMPLEMENTED;
68   }
69   const auto& factor_cb = disk->get_factor_cb();
70   if (factor_cb) { // handling disk variability
71     action->set_rate_factor(factor_cb(size, type));
72   }
73   return action;
74 }
75
76 /************
77  * Resource *
78  ************/
79 void DiskS19::set_read_bandwidth(double value)
80 {
81   read_bw_.peak = value;
82
83   if (get_read_constraint()) {
84     get_model()->get_maxmin_system()->update_constraint_bound(get_read_constraint(), read_bw_.peak * read_bw_.scale);
85   }
86 }
87
88 void DiskS19::set_write_bandwidth(double value)
89 {
90   write_bw_.peak = value;
91
92   if (get_write_constraint()) {
93     get_model()->get_maxmin_system()->update_constraint_bound(get_write_constraint(), write_bw_.peak * write_bw_.scale);
94   }
95 }
96
97 void DiskS19::set_readwrite_bandwidth(double value)
98 {
99   readwrite_bw_ = value;
100   if (get_constraint()) {
101     get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), readwrite_bw_);
102   }
103 }
104
105 void DiskS19::apply_event(kernel::profile::Event* triggered, double value)
106 {
107   /* Find out which of my iterators was triggered, and react accordingly */
108   if (triggered == read_bw_.event) {
109     set_read_bandwidth(value);
110     tmgr_trace_event_unref(&read_bw_.event);
111
112   } else if (triggered == write_bw_.event) {
113     set_write_bandwidth(value);
114     tmgr_trace_event_unref(&write_bw_.event);
115
116   } else if (triggered == state_event_) {
117     if (value > 0)
118       turn_on();
119     else
120       turn_off();
121     tmgr_trace_event_unref(&state_event_);
122   } else {
123     xbt_die("Unknown event!\n");
124   }
125
126   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
127             get_constraint());
128 }
129
130 /**********
131  * Action *
132  **********/
133
134 DiskS19Action::DiskS19Action(Model* model, double cost, bool failed)
135     : DiskAction(model, cost, failed, model->get_maxmin_system()->variable_new(this, 1.0, -1.0, 3))
136 {
137 }
138
139 void DiskS19Action::update_remains_lazy(double /*now*/)
140 {
141   THROW_IMPOSSIBLE;
142 }
143 } // namespace resource
144 } // namespace kernel
145 } // namespace simgrid