Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5c84e91927712a33b94ee1cb7810f5e3547b5652
[simgrid.git] / include / simgrid / plugins / jbod.hpp
1 /* Copyright (c) 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 #ifndef SIMGRID_PLUGIN_JBOD_HPP
7 #define SIMGRID_PLUGIN_JBOD_HPP
8 #include <simgrid/s4u/Host.hpp>
9 #include <simgrid/s4u/Io.hpp>
10
11 namespace simgrid::plugin {
12
13 class JbodIo;
14 /** Smart pointer to a simgrid::s4u::Activity */
15 using JbodIoPtr = boost::intrusive_ptr<JbodIo>;
16 XBT_PUBLIC void intrusive_ptr_release(const JbodIo* io);
17 XBT_PUBLIC void intrusive_ptr_add_ref(const JbodIo* io);
18
19 class Jbod : public s4u::Host {
20 public:
21   enum class RAID {RAID0 = 0, RAID1 = 1, RAID4 = 4 , RAID5 = 5, RAID6 = 6};
22   int get_parity_disk_idx() { return parity_disk_idx_; }
23   void update_parity_disk_idx() { parity_disk_idx_ = (parity_disk_idx_- 1) % num_disks_; }
24
25   int get_next_read_disk_idx() { return (++read_disk_idx_) % num_disks_; }
26
27   JbodIoPtr read_async(sg_size_t size);
28   sg_size_t read(sg_size_t size);
29
30   JbodIoPtr write_async(sg_size_t size);
31   sg_size_t write(sg_size_t size);
32
33   static Jbod* create_jbod(s4u::NetZone* zone, const std::string& name, double speed, unsigned int num_disks,
34                            RAID raid_level, double read_bandwidth, double write_bandwidth);
35
36 protected:
37   void set_num_disks(unsigned int num_disks) { num_disks_ = num_disks; }
38   void set_parity_disk_idx(unsigned int index) { parity_disk_idx_ = index; }
39   void set_read_disk_idx(int index) { read_disk_idx_ = index; }
40   void set_raid_level(RAID raid_level) { raid_level_ = raid_level; }
41
42 private:
43   unsigned int num_disks_;
44   RAID raid_level_;
45   unsigned int parity_disk_idx_;
46   int read_disk_idx_;
47 };
48
49 class JbodIo {
50   const Jbod* jbod_;
51   s4u::CommPtr transfer_;
52   s4u::ExecPtr parity_block_comp_;
53   std::vector<s4u::IoPtr> pending_ios_;
54   s4u::Io::OpType type_;
55   std::atomic_int_fast32_t refcount_{0};
56 public:
57
58   explicit JbodIo(const Jbod* jbod, const s4u::CommPtr transfer, const s4u::ExecPtr parity_block_comp,
59                   const std::vector<s4u::IoPtr>& pending_ios, s4u::Io::OpType type)
60     : jbod_(jbod), transfer_(transfer), parity_block_comp_(parity_block_comp), pending_ios_(pending_ios), type_(type)
61     {}
62
63   void wait();
64
65 #ifndef DOXYGEN
66   friend void intrusive_ptr_release(JbodIo* io)
67   {
68     if (io->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
69       std::atomic_thread_fence(std::memory_order_acquire);
70       delete io;
71     }
72   }
73   friend void intrusive_ptr_add_ref(JbodIo* io) { io->refcount_.fetch_add(1, std::memory_order_relaxed); }
74 #endif
75 };
76
77 } // namespace simgrid::plugin
78 #endif