Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / io-degradation / s4u-io-degradation.cpp
1 /* Copyright (c) 2017-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 /* This example shows how to simulate a non-linear resource sharing for disk
7  * operations.
8  *
9  * It is inspired on the paper
10  * "Adding Storage Simulation Capacities to the SimGridToolkit: Concepts, Models, and API"
11  * Available at : https://hal.inria.fr/hal-01197128/document
12  *
13  * It shows how to simulate concurrent operations degrading overall performance of IO
14  * operations (specifically the effects presented in Fig. 8 of the paper).
15  */
16
17 #include <simgrid/s4u.hpp>
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(disk_test, "Messages specific for this simulation");
20 namespace sg4 = simgrid::s4u;
21
22 /** @brief Calculates the bandwidth for disk doing async operations */
23 static void estimate_bw(const sg4::Disk* disk, int n_flows, bool read)
24 {
25   unsigned long long size = 100000;
26   double cur_time         = sg4::Engine::get_clock();
27   std::vector<sg4::IoPtr> activities;
28   for (int i = 0; i < n_flows; i++) {
29     sg4::IoPtr act;
30     if (read)
31       act = disk->read_async(size);
32     else
33       act = disk->write_async(size);
34
35     activities.push_back(act);
36   }
37
38   for (const auto& act : activities)
39     act->wait();
40
41   double elapsed_time = sg4::Engine::get_clock() - cur_time;
42   double estimated_bw = static_cast<double>(size * n_flows) / elapsed_time;
43   XBT_INFO("Disk: %s, concurrent %s: %d, estimated bandwidth: %lf", disk->get_cname(), read ? "read" : "write", n_flows,
44            estimated_bw);
45 }
46
47 static void host()
48 {
49   /* - Estimating bw for each disk and considering concurrent flows */
50   for (int n = 1; n < 15; n += 2) {
51     for (const auto* disk : sg4::Host::current()->get_disks()) {
52       estimate_bw(disk, n, true);
53       estimate_bw(disk, n, false);
54     }
55   }
56 }
57
58 /**
59  * @brief Non-linear resource callback for SSD disks
60  *
61  * In this case, we have measurements for some resource sharing and directly use them to return the
62  * correct value
63  * @param disk Disk on which the operation is happening (defined by the user through the std::bind)
64  * @param op read or write operation (defined by the user through the std::bind)
65  * @param capacity Resource current capacity in SimGrid
66  * @param n Number of activities sharing this resource
67  */
68 static double ssd_dynamic_sharing(const sg4::Disk* /*disk*/, const std::string& op, double capacity, int n)
69 {
70   /* measurements for SSD disks */
71   using DiskCapacity                                                   = std::unordered_map<int, double>;
72   static const std::unordered_map<std::string, DiskCapacity> SSD_SPEED = {{"write", {{1, 131.}}},
73                                                                           {"read",
74                                                                            {{1, 152.},
75                                                                             {2, 161.},
76                                                                             {3, 184.},
77                                                                             {4, 197.},
78                                                                             {5, 207.},
79                                                                             {6, 215.},
80                                                                             {7, 220.},
81                                                                             {8, 224.},
82                                                                             {9, 227.},
83                                                                             {10, 231.},
84                                                                             {11, 233.},
85                                                                             {12, 235.},
86                                                                             {13, 237.},
87                                                                             {14, 238.},
88                                                                             {15, 239.}}}};
89
90   const auto& data = SSD_SPEED.at(op);
91   const auto value = data.find(n);
92   /* no special bandwidth for this disk sharing N flows, just returns maximal capacity */
93   return value == data.end() ? capacity : value->second;
94 }
95
96 /**
97  * @brief Non-linear resource callback for SATA disks
98  *
99  * In this case, the degradation for read operations is linear and we have a formula that represents it.
100  *
101  * @param disk Disk on which the operation is happening (defined by the user through the std::bind)
102  * @param capacity Resource current capacity in SimGrid
103  * @param n Number of activities sharing this resource
104  */
105 static double sata_dynamic_sharing(const sg4::Disk* /*disk*/, double /*capacity*/, int n)
106 {
107   return 68.3 - 1.7 * n;
108 }
109
110 /** @brief Creates an SSD disk, setting the appropriate callback for non-linear resource sharing */
111 static void create_ssd_disk(sg4::Host* host, const std::string& disk_name)
112 {
113   auto* disk = host->create_disk(disk_name, "240MBps", "170MBps");
114   disk->set_sharing_policy(sg4::Disk::Operation::READ, sg4::Disk::SharingPolicy::NONLINEAR,
115                            std::bind(&ssd_dynamic_sharing, disk, "read", std::placeholders::_1, std::placeholders::_2));
116   disk->set_sharing_policy(
117       sg4::Disk::Operation::WRITE, sg4::Disk::SharingPolicy::NONLINEAR,
118       std::bind(&ssd_dynamic_sharing, disk, "write", std::placeholders::_1, std::placeholders::_2));
119   disk->set_sharing_policy(sg4::Disk::Operation::READWRITE, sg4::Disk::SharingPolicy::LINEAR);
120 }
121
122 /** @brief Same for a SATA disk, only read operation follows a non-linear resource sharing */
123 static void create_sata_disk(sg4::Host* host, const std::string& disk_name)
124 {
125   auto* disk = host->create_disk(disk_name, "68MBps", "50MBps");
126   disk->set_sharing_policy(sg4::Disk::Operation::READ, sg4::Disk::SharingPolicy::NONLINEAR,
127                            std::bind(&sata_dynamic_sharing, disk, std::placeholders::_1, std::placeholders::_2));
128   /* this is the default behavior, expliciting only to make it clearer */
129   disk->set_sharing_policy(sg4::Disk::Operation::WRITE, sg4::Disk::SharingPolicy::LINEAR);
130   disk->set_sharing_policy(sg4::Disk::Operation::READWRITE, sg4::Disk::SharingPolicy::LINEAR);
131 }
132
133 int main(int argc, char** argv)
134 {
135   sg4::Engine e(&argc, argv);
136   /* simple platform containing 1 host and 2 disk */
137   auto* zone = sg4::create_full_zone("bob_zone");
138   auto* bob  = zone->create_host("bob", 1e6);
139   create_ssd_disk(bob, "Edel (SSD)");
140   create_sata_disk(bob, "Griffon (SATA II)");
141   zone->seal();
142
143   sg4::Actor::create("", bob, host);
144
145   e.run();
146   XBT_INFO("Simulated time: %g", sg4::Engine::get_clock());
147
148   return 0;
149 }