LCOV - code coverage report
Current view: top level - builds/DaniloBorquez/flex-net-sim/src - defragmentator.cpp (source / functions) Hit Total Coverage
Test: commit SHA1 Lines: 57 58 98.3 %
Date: 2026-11-18 13:04:43 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "connection.hpp"
       2             : #include "defragmentator.hpp"
       3             : #include "numeric.hpp"
       4             : #include <cstddef>
       5             : #include <set>
       6             : #include <stdexcept>
       7             : #include <utility>
       8             : #include <vector>
       9             : 
      10           5 : Defragmentator::Defragmentator(std::shared_ptr<Network> network) {
      11           5 :   this->network = network;
      12           5 :   this->name = std::string("No name");
      13           5 : }
      14             : 
      15          12 : Defragmentator::Defragmentator(void) {
      16          12 :   this->network = nullptr;
      17          12 :   this->name = "No name";
      18          12 : }
      19             : 
      20           1 : void Defragmentator::exec(std::vector<Connection> connections) {
      21             :   throw std::runtime_error("You must implement a method to defragmentate the "
      22             :                            "network. You can do this "
      23           1 :                            "by making an inherited class from Allocator.");
      24             : }
      25             : 
      26           6 : std::string Defragmentator::getName() { return this->name; }
      27             : 
      28           1 : void Defragmentator::setName(std::string name) { this->name = name; }
      29             : 
      30         310 : void Defragmentator::setNetwork(std::shared_ptr<Network> network) {
      31         310 :   this->network = network;
      32         310 : }
      33             : 
      34        1317 : void Defragmentator::realloc(Connection connection) {
      35        1317 :   long long idConnection = connection.getId();
      36        4214 :   for (std::size_t i = 0; i < this->connections.size(); i++) {
      37        4213 :     if (this->connections[i].id == idConnection) { // Find the connection
      38        1316 :       Connection *con = &(this->connections[i]);
      39             :       // Validate every target slot before changing anything, so a rejected
      40             :       // reallocation leaves the network and connections intact.
      41        1316 :       if (connection.links.size() != connection.slots.size()) {
      42             :         throw std::runtime_error(
      43           0 :             "Reallocated connection must have one slot list per link.");
      44             :       }
      45        2632 :       std::set<std::pair<int, std::size_t>> ownSlots;
      46        4611 :       for (std::size_t j = 0; j < con->links.size(); j++) {
      47        9885 :         for (std::size_t slot : con->slots[j]) {
      48        6590 :           ownSlots.insert(std::make_pair(con->links[j], slot));
      49             :         }
      50             :       }
      51        4607 :       for (std::size_t j = 0; j < connection.links.size(); j++) {
      52        9876 :         for (std::size_t slot : connection.slots[j]) {
      53       19414 :           if (this->network->isSlotUsedNative(connection.links[j], slot) &&
      54       12830 :               ownSlots.count(std::make_pair(connection.links[j], slot)) == 0) {
      55             :             throw std::runtime_error(
      56             :                 "Cannot reallocate a connection onto slots used by another "
      57           1 :                 "connection.");
      58             :           }
      59             :         }
      60             :       }
      61             : 
      62        4605 :       for (std::size_t j = 0; j < con->links.size(); j++) {
      63        9873 :         for (std::size_t k = 0; k < con->slots[j].size(); k++) {
      64        6582 :           this->network->unuseSlotNative(con->links[j], con->slots[j][k]);
      65             :         }
      66             :       }
      67             : 
      68        1314 :       con->slots.clear();
      69        1314 :       con->links.clear();
      70        1314 :       con->links = connection.links;
      71        1314 :       con->slots = connection.slots;
      72             : 
      73        4605 :       for (std::size_t j = 0; j < con->links.size(); j++) {
      74        9873 :         for (std::size_t k = 0; k < con->slots[j].size(); k++) {
      75        6582 :           this->network->useSlotNative(con->links[j], con->slots[j][k]);
      76             :         }
      77             :       }
      78        1314 :       break;
      79             :     }
      80             :   }
      81        1315 : }
      82             : 
      83           1 : void Defragmentator::setPaths(std::shared_ptr<Paths> path) {
      84           1 :   this->paths = path;
      85           1 : }
      86             : 
      87        7714 : bool Defragmentator::isSlotInConnectionNative(Connection connection,
      88             :                                               std::size_t slot) {
      89       15428 :   std::vector<std::vector<std::size_t>> slots = connection.getSlotsNative();
      90        7714 :   if (slots.empty())
      91           1 :     return false;
      92             : 
      93       15426 :   std::vector<std::size_t> firstSlot = slots.front();
      94             : 
      95       13277 :   for (std::size_t i = 0; i < firstSlot.size(); i++) {
      96       12139 :     if (firstSlot[i] == slot) {
      97        6575 :       return true;
      98             :     }
      99             :   }
     100        1138 :   return false;
     101             : }
     102             : 
     103        7714 : bool Defragmentator::isSlotInConnection(Connection connection, int slot) {
     104       15427 :   return this->isSlotInConnectionNative(connection,
     105       15426 :                                         fnsim_numeric::intToSize(slot, "slot"));
     106             : }

Generated by: LCOV version 1.13