1 /* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved. */
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. */
6 /* This example shows how to simulate a non-linear resource sharing for disk
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
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).
17 #include <simgrid/s4u.hpp>
19 namespace sg4 = simgrid::s4u;
21 XBT_LOG_NEW_DEFAULT_CATEGORY(disk_test, "Messages specific for this simulation");
23 /** @brief Calculates the bandwidth for disk doing async operations */
24 static void estimate_bw(sg4::Disk* disk, int n_flows, bool read)
26 unsigned long long size = 100000;
27 double cur_time = sg4::Engine::get_clock();
28 std::vector<sg4::IoPtr> activities;
29 for (int i = 0; i < n_flows; i++) {
32 act = disk->read_async(size);
34 act = disk->write_async(size);
36 activities.push_back(act);
39 for (auto& act : activities)
42 double elapsed_time = sg4::Engine::get_clock() - cur_time;
43 double estimated_bw = size * n_flows / elapsed_time;
44 XBT_INFO("Disk: %s, concurrent %s: %d, estimated bandwidth: %lf", disk->get_cname(), read ? "read" : "write", n_flows,
50 /* - Estimating bw for each disk and considering concurrent flows */
51 for (int n = 1; n < 15; n += 2) {
52 for (auto* disk : sg4::Host::current()->get_disks()) {
53 estimate_bw(disk, n, true);
54 estimate_bw(disk, n, false);
60 * @brief Non-linear resource callback for SSD disks
62 * In this case, we have measurements for some resource sharing and directly use them to return the
64 * @param disk Disk on which the operation is happening (defined by the user through the std::bind)
65 * @param op read or write operation (defined by the user through the std::bind)
66 * @param capacity Resource current capacity in SimGrid
67 * @param n Number of activities sharing this resource
69 static double ssd_dynamic_sharing(const sg4::Disk* disk, const std::string& op, double capacity, int n)
71 /* measurements for SSD disks */
72 using DiskCapacity = std::unordered_map<int, double>;
73 static const std::unordered_map<std::string, DiskCapacity> SSD_SPEED = {{"write", {{1, 131.}}},
91 const auto& data = SSD_SPEED.at(op);
92 /* no special bandwidth for this disk sharing N flows, just returns maximal capacity */
93 if (data.find(n) != data.end())
94 capacity = data.at(n);
96 // XBT_INFO("Disk %s, %s operation between %d flows, capacity %lf", disk->get_cname(), op.c_str(), n, capacity);
102 * @brief Non-linear resource callback for SATA disks
104 * In this case, the degradation for read operations is linear and we have a formula that represents it.
106 * @param disk Disk on which the operation is happening (defined by the user through the std::bind)
107 * @param capacity Resource current capacity in SimGrid
108 * @param n Number of activities sharing this resource
110 static double sata_dynamic_sharing(const sg4::Disk* disk, double capacity, int n)
112 capacity = 68.3 - 1.7 * n;
113 // XBT_INFO("Disk %s, read operation between %d flows, capacity %lf", disk->get_cname(), n, capacity);
118 /** @brief Creates an SSD disk, setting the appropriate callback for non-linear resource sharing */
119 static void create_ssd_disk(sg4::Host* host, const std::string& disk_name)
121 auto* disk = host->create_disk(disk_name, "240MBps", "170MBps");
122 disk->set_sharing_policy(sg4::Disk::Operation::READ, sg4::Disk::SharingPolicy::NONLINEAR,
123 std::bind(&ssd_dynamic_sharing, disk, "read", std::placeholders::_1, std::placeholders::_2));
124 disk->set_sharing_policy(
125 sg4::Disk::Operation::WRITE, sg4::Disk::SharingPolicy::NONLINEAR,
126 std::bind(&ssd_dynamic_sharing, disk, "write", std::placeholders::_1, std::placeholders::_2));
127 disk->set_sharing_policy(sg4::Disk::Operation::READWRITE, sg4::Disk::SharingPolicy::LINEAR);
130 /** @brief Same for a SATA disk, only read operation follows a non-linear resource sharing */
131 static void create_sata_disk(sg4::Host* host, const std::string& disk_name)
133 auto* disk = host->create_disk(disk_name, "68MBps", "50MBps");
134 disk->set_sharing_policy(sg4::Disk::Operation::READ, sg4::Disk::SharingPolicy::NONLINEAR,
135 std::bind(&sata_dynamic_sharing, disk, std::placeholders::_1, std::placeholders::_2));
136 /* this is the default behavior, expliciting only to make it clearer */
137 disk->set_sharing_policy(sg4::Disk::Operation::WRITE, sg4::Disk::SharingPolicy::LINEAR);
138 disk->set_sharing_policy(sg4::Disk::Operation::READWRITE, sg4::Disk::SharingPolicy::LINEAR);
141 int main(int argc, char** argv)
143 sg4::Engine e(&argc, argv);
144 /* simple platform containing 1 host and 2 disk */
145 auto* zone = sg4::create_full_zone("bob_zone");
146 auto* bob = zone->create_host("bob", 1e6);
147 create_ssd_disk(bob, "Edel (SSD)");
148 create_sata_disk(bob, "Griffon (SATA II)");
151 simgrid::s4u::Actor::create("", bob, host);
154 XBT_INFO("Simulated time: %g", simgrid::s4u::Engine::get_clock());