Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
59a69e2d0cecf7ccda978b90a99a5d475a7b2dd6
[simgrid.git] / examples / cpp / io-disk-raw / s4u-io-disk-raw.cpp
1 /* Copyright (c) 2017-2022. 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/s4u.hpp"
7 #include <string>
8 #include <unordered_map>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(disk_test, "Messages specific for this simulation");
11 namespace sg4 = simgrid::s4u;
12
13 static void host()
14 {
15   /* -Add an extra disk in a programmatic way */
16   sg4::Host::current()->create_disk("Disk3", /*read bandwidth*/ 9.6e7, /*write bandwidth*/ 6.4e7)->seal();
17
18   /* - Display information on the disks mounted by the current host */
19   XBT_INFO("*** Storage info on %s ***", sg4::Host::current()->get_cname());
20
21   /* - Retrieve all disks from current host */
22   std::vector<sg4::Disk*> const& disk_list = sg4::Host::current()->get_disks();
23
24   /* - For each disk mounted on host, display disk name and mount point */
25   for (auto const& disk : disk_list)
26     XBT_INFO("Disk name: %s (read: %.0f B/s -- write: %.0f B/s", disk->get_cname(), disk->get_read_bandwidth(),
27              disk->get_write_bandwidth());
28
29   /* - Write 400,000 bytes on Disk1 */
30   sg4::Disk* disk          = disk_list.front();
31   sg_size_t write          = disk->write(400000);
32   XBT_INFO("Wrote %llu bytes on '%s'", write, disk->get_cname());
33
34   /*  - Now read 200,000 bytes */
35   sg_size_t read = disk->read(200000);
36   XBT_INFO("Read %llu bytes on '%s'", read, disk->get_cname());
37
38   /* - Write 800,000 bytes on Disk3 */
39   const sg4::Disk* disk3          = disk_list.back();
40   sg_size_t write_on_disk3        = disk3->write(800000);
41   XBT_INFO("Wrote %llu bytes on '%s'", write_on_disk3, disk3->get_cname());
42
43   /* - Attach some user data to disk1 */
44   XBT_INFO("*** Get/set data for storage element: Disk1 ***");
45
46   const auto* data = disk->get_data<std::string>();
47
48   XBT_INFO("Get storage data: '%s'", data ? data->c_str() : "No user data");
49
50   disk->set_data(new std::string("Some user data"));
51   data = disk->get_data<std::string>();
52   XBT_INFO("Set and get data: '%s'", data->c_str());
53   delete data;
54 }
55
56 int main(int argc, char** argv)
57 {
58   sg4::Engine e(&argc, argv);
59   e.load_platform(argv[1]);
60
61   /* - Display Host properties */
62   for (auto h : e.get_all_hosts()) {
63     XBT_INFO("*** %s properties ****", h->get_cname());
64     for (auto const& [key, value] : *h->get_properties())
65       XBT_INFO("  %s -> %s", key.c_str(), value.c_str());
66   }
67
68   sg4::Actor::create("", e.host_by_name("bob"), host);
69
70   e.run();
71   XBT_INFO("Simulated time: %g", sg4::Engine::get_clock());
72
73   return 0;
74 }