1 /* Copyright (c) 2017-2022. 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(const 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 (const auto& act : activities)
42 double elapsed_time = sg4::Engine::get_clock() - cur_time;
43 double estimated_bw = static_cast<double>(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 (const 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);
100 * @brief Non-linear resource callback for SATA disks
102 * In this case, the degradation for read operations is linear and we have a formula that represents it.
104 * @param disk Disk on which the operation is happening (defined by the user through the std::bind)
105 * @param capacity Resource current capacity in SimGrid
106 * @param n Number of activities sharing this resource
108 static double sata_dynamic_sharing(const sg4::Disk* /*disk*/, double /*capacity*/, int n)
110 return 68.3 - 1.7 * n;
113 /** @brief Creates an SSD disk, setting the appropriate callback for non-linear resource sharing */
114 static void create_ssd_disk(sg4::Host* host, const std::string& disk_name)
116 auto* disk = host->create_disk(disk_name, "240MBps", "170MBps");
117 disk->set_sharing_policy(sg4::Disk::Operation::READ, sg4::Disk::SharingPolicy::NONLINEAR,
118 std::bind(&ssd_dynamic_sharing, disk, "read", std::placeholders::_1, std::placeholders::_2));
119 disk->set_sharing_policy(
120 sg4::Disk::Operation::WRITE, sg4::Disk::SharingPolicy::NONLINEAR,
121 std::bind(&ssd_dynamic_sharing, disk, "write", std::placeholders::_1, std::placeholders::_2));
122 disk->set_sharing_policy(sg4::Disk::Operation::READWRITE, sg4::Disk::SharingPolicy::LINEAR);
125 /** @brief Same for a SATA disk, only read operation follows a non-linear resource sharing */
126 static void create_sata_disk(sg4::Host* host, const std::string& disk_name)
128 auto* disk = host->create_disk(disk_name, "68MBps", "50MBps");
129 disk->set_sharing_policy(sg4::Disk::Operation::READ, sg4::Disk::SharingPolicy::NONLINEAR,
130 std::bind(&sata_dynamic_sharing, disk, std::placeholders::_1, std::placeholders::_2));
131 /* this is the default behavior, expliciting only to make it clearer */
132 disk->set_sharing_policy(sg4::Disk::Operation::WRITE, sg4::Disk::SharingPolicy::LINEAR);
133 disk->set_sharing_policy(sg4::Disk::Operation::READWRITE, sg4::Disk::SharingPolicy::LINEAR);
136 int main(int argc, char** argv)
138 sg4::Engine e(&argc, argv);
139 /* simple platform containing 1 host and 2 disk */
140 auto* zone = sg4::create_full_zone("bob_zone");
141 auto* bob = zone->create_host("bob", 1e6);
142 create_ssd_disk(bob, "Edel (SSD)");
143 create_sata_disk(bob, "Griffon (SATA II)");
146 simgrid::s4u::Actor::create("", bob, host);
149 XBT_INFO("Simulated time: %g", simgrid::s4u::Engine::get_clock());