Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the files related to the platform parsing to kernel/xml
[simgrid.git] / src / kernel / resource / models / disk_s19.cpp
1 /* Copyright (c) 2013-2023. 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/models/disk_s19.hpp"
15 #include "src/kernel/resource/profile/Event.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 SIMGRID_REGISTER_DISK_MODEL(S19, "Simplistic disk model.", []() {
30   auto disk_model = std::make_shared<simgrid::kernel::resource::DiskS19Model>("Disk");
31   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
32   engine->add_model(disk_model);
33   engine->get_netzone_root()->set_disk_model(disk_model);
34 });
35
36 namespace simgrid::kernel::resource {
37 /*********
38  * Model *
39  *********/
40
41 DiskS19Model::DiskS19Model(const std::string& name) : DiskModel(name)
42 {
43   set_maxmin_system(lmm::System::build(cfg_disk_solver.get(), true /* selective update */));
44 }
45
46 DiskImpl* DiskS19Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
47 {
48   return (new DiskS19(name, read_bandwidth, write_bandwidth))->set_model(this);
49 }
50
51 void DiskS19Model::update_actions_state(double /*now*/, double delta)
52 {
53   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
54     auto& action = *it;
55     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
56     action.update_remains(rint(action.get_rate() * delta));
57     action.update_max_duration(delta);
58
59     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
60         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
61       action.finish(Action::State::FINISHED);
62     }
63   }
64 }
65
66 /************
67  * Resource *
68  ************/
69
70 void DiskS19::apply_event(kernel::profile::Event* triggered, double value)
71 {
72   /* Find out which of my iterators was triggered, and react accordingly */
73   if (triggered == get_read_event()) {
74     set_read_bandwidth(value);
75     unref_read_event();
76   } else if (triggered == get_write_event()) {
77     set_write_bandwidth(value);
78     unref_write_event();
79   } else if (triggered == get_state_event()) {
80     if (value > 0)
81       turn_on();
82     else
83       turn_off();
84     unref_state_event();
85   } else {
86     xbt_die("Unknown event!\n");
87   }
88
89   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
90             get_constraint());
91 }
92
93 /**********
94  * Action *
95  **********/
96
97 DiskS19Action::DiskS19Action(Model* model, double cost, bool failed)
98     : DiskAction(model, cost, failed, model->get_maxmin_system()->variable_new(this, 1.0, -1.0, 3))
99 {
100 }
101
102 DiskAction* DiskS19::io_start(sg_size_t size, s4u::Io::OpType type)
103 {
104   auto* action = new DiskS19Action(get_model(), static_cast<double>(size), not is_on());
105   get_model()->get_maxmin_system()->expand(get_constraint(), action->get_variable(), 1.0);
106   switch (type) {
107     case s4u::Io::OpType::READ:
108       get_model()->get_maxmin_system()->expand(get_read_constraint(), action->get_variable(), 1.0);
109       break;
110     case s4u::Io::OpType::WRITE:
111       get_model()->get_maxmin_system()->expand(get_write_constraint(), action->get_variable(), 1.0);
112       break;
113     default:
114       THROW_UNIMPLEMENTED;
115   }
116   if (const auto& factor_cb = get_factor_cb()) { // handling disk variability
117     action->set_rate_factor(factor_cb(size, type));
118   }
119   return action;
120 }
121
122 } // namespace simgrid::kernel::resource