mem-garnet: Use static allocation in Switch
authorDaniel R. Carvalho <odanrc@yahoo.com.br>
Wed, 8 Jan 2020 22:30:13 +0000 (23:30 +0100)
committerDaniel Carvalho <odanrc@yahoo.com.br>
Mon, 16 Mar 2020 22:23:16 +0000 (22:23 +0000)
Make pointers non pointers when possible.

Change-Id: I272387c2ac1fd77deef06feb637127e232910e30
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/24250
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Maintainer: Nikos Nikoleris <nikos.nikoleris@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
src/mem/ruby/network/simple/Switch.cc
src/mem/ruby/network/simple/Switch.hh

index 7cc635d2a1c8f3bf67a9ec4c19e36b840bef6d84..71b66365c6d549afcf7c11f410c55668eac8d564 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2020 Inria
  * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
  * All rights reserved.
  *
 #include "base/cast.hh"
 #include "base/stl_helpers.hh"
 #include "mem/ruby/network/MessageBuffer.hh"
-#include "mem/ruby/network/simple/PerfectSwitch.hh"
 #include "mem/ruby/network/simple/SimpleNetwork.hh"
-#include "mem/ruby/network/simple/Throttle.hh"
 
 using namespace std;
-using m5::stl_helpers::deletePointers;
 using m5::stl_helpers::operator<<;
 
-Switch::Switch(const Params *p) : BasicRouter(p)
+Switch::Switch(const Params *p)
+  : BasicRouter(p), perfectSwitch(m_id, this, p->virt_nets),
+    m_num_connected_buffers(0)
 {
-    m_perfect_switch = new PerfectSwitch(m_id, this, p->virt_nets);
-    m_port_buffers = p->port_buffers;
-    m_num_connected_buffers = 0;
-}
-
-Switch::~Switch()
-{
-    delete m_perfect_switch;
-
-    // Delete throttles (one per output port)
-    deletePointers(m_throttles);
-
-    // Delete MessageBuffers
-    deletePointers(m_port_buffers);
+    m_port_buffers.reserve(p->port_buffers.size());
+    for (auto& buffer : p->port_buffers) {
+        m_port_buffers.emplace_back(buffer);
+    }
 }
 
 void
 Switch::init()
 {
     BasicRouter::init();
-    m_perfect_switch->init(m_network_ptr);
+    perfectSwitch.init(m_network_ptr);
 }
 
 void
 Switch::addInPort(const vector<MessageBuffer*>& in)
 {
-    m_perfect_switch->addInPort(in);
+    perfectSwitch.addInPort(in);
 }
 
 void
@@ -78,36 +68,26 @@ Switch::addOutPort(const vector<MessageBuffer*>& out,
                    Cycles link_latency, int bw_multiplier)
 {
     // Create a throttle
-    RubySystem *rs = m_network_ptr->params()->ruby_system;
-    Throttle* throttle_ptr = new Throttle(m_id, rs, m_throttles.size(),
-                                          link_latency, bw_multiplier,
-                                          m_network_ptr->getEndpointBandwidth(),
-                                          this);
-
-    m_throttles.push_back(throttle_ptr);
+    throttles.emplace_back(m_id, m_network_ptr->params()->ruby_system,
+        throttles.size(), link_latency, bw_multiplier,
+        m_network_ptr->getEndpointBandwidth(), this);
 
     // Create one buffer per vnet (these are intermediaryQueues)
     vector<MessageBuffer*> intermediateBuffers;
 
     for (int i = 0; i < out.size(); ++i) {
         assert(m_num_connected_buffers < m_port_buffers.size());
-        MessageBuffer* buffer_ptr = m_port_buffers[m_num_connected_buffers];
+        MessageBuffer* buffer_ptr =
+            m_port_buffers[m_num_connected_buffers];
         m_num_connected_buffers++;
         intermediateBuffers.push_back(buffer_ptr);
     }
 
     // Hook the queues to the PerfectSwitch
-    m_perfect_switch->addOutPort(intermediateBuffers, routing_table_entry);
+    perfectSwitch.addOutPort(intermediateBuffers, routing_table_entry);
 
     // Hook the queues to the Throttle
-    throttle_ptr->addLinks(intermediateBuffers, out);
-}
-
-const Throttle*
-Switch::getThrottle(LinkID link_number) const
-{
-    assert(m_throttles[link_number] != NULL);
-    return m_throttles[link_number];
+    throttles.back().addLinks(intermediateBuffers, out);
 }
 
 void
@@ -115,15 +95,15 @@ Switch::regStats()
 {
     BasicRouter::regStats();
 
-    for (int link = 0; link < m_throttles.size(); link++) {
-        m_throttles[link]->regStats(name());
+    for (auto& throttle : throttles) {
+        throttle.regStats(name());
     }
 
     m_avg_utilization.name(name() + ".percent_links_utilized");
-    for (unsigned int i = 0; i < m_throttles.size(); i++) {
-        m_avg_utilization += m_throttles[i]->getUtilization();
+    for (const auto& throttle : throttles) {
+        m_avg_utilization += throttle.getUtilization();
     }
-    m_avg_utilization /= Stats::constant(m_throttles.size());
+    m_avg_utilization /= Stats::constant(throttles.size());
 
     for (unsigned int type = MessageSizeType_FIRST;
          type < MessageSizeType_NUM; ++type) {
@@ -138,8 +118,8 @@ Switch::regStats()
             .flags(Stats::nozero)
             ;
 
-        for (unsigned int i = 0; i < m_throttles.size(); i++) {
-            m_msg_counts[type] += m_throttles[i]->getMsgCount(type);
+        for (const auto& throttle : throttles) {
+            m_msg_counts[type] += throttle.getMsgCount(type);
         }
         m_msg_bytes[type] = m_msg_counts[type] * Stats::constant(
                 Network::MessageSizeType_to_int(MessageSizeType(type)));
@@ -149,18 +129,18 @@ Switch::regStats()
 void
 Switch::resetStats()
 {
-    m_perfect_switch->clearStats();
-    for (int i = 0; i < m_throttles.size(); i++) {
-        m_throttles[i]->clearStats();
+    perfectSwitch.clearStats();
+    for (auto& throttle : throttles) {
+        throttle.clearStats();
     }
 }
 
 void
 Switch::collateStats()
 {
-    m_perfect_switch->collateStats();
-    for (int i = 0; i < m_throttles.size(); i++) {
-        m_throttles[i]->collateStats();
+    perfectSwitch.collateStats();
+    for (auto& throttle : throttles) {
+        throttle.collateStats();
     }
 }
 
index b481ed53eec63a4bd7561f3f2bddbac54946640a..5d2690621ac89fc4164c242e5e7969b25afd964f 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2020 Inria
  * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
  * All rights reserved.
  *
 #define __MEM_RUBY_NETWORK_SIMPLE_SWITCH_HH__
 
 #include <iostream>
+#include <list>
 #include <vector>
 
 #include "mem/packet.hh"
 #include "mem/ruby/common/TypeDefines.hh"
 #include "mem/ruby/network/BasicRouter.hh"
+#include "mem/ruby/network/simple/PerfectSwitch.hh"
+#include "mem/ruby/network/simple/Throttle.hh"
 #include "mem/ruby/protocol/MessageSizeType.hh"
 #include "params/Switch.hh"
 
 class MessageBuffer;
-class PerfectSwitch;
 class NetDest;
 class SimpleNetwork;
-class Throttle;
 
 class Switch : public BasicRouter
 {
   public:
     typedef SwitchParams Params;
     Switch(const Params *p);
-    ~Switch();
+    ~Switch() = default;
     void init();
 
     void addInPort(const std::vector<MessageBuffer*>& in);
@@ -67,8 +69,6 @@ class Switch : public BasicRouter
                     const NetDest& routing_table_entry,
                     Cycles link_latency, int bw_multiplier);
 
-    const Throttle* getThrottle(LinkID link_number) const;
-
     void resetStats();
     void collateStats();
     void regStats();
@@ -86,11 +86,12 @@ class Switch : public BasicRouter
     Switch(const Switch& obj);
     Switch& operator=(const Switch& obj);
 
-    PerfectSwitch* m_perfect_switch;
+    PerfectSwitch perfectSwitch;
     SimpleNetwork* m_network_ptr;
-    std::vector<Throttle*> m_throttles;
-    std::vector<MessageBuffer*> m_port_buffers;
+    std::list<Throttle> throttles;
+
     unsigned m_num_connected_buffers;
+    std::vector<MessageBuffer*> m_port_buffers;
 
     // Statistical variables
     Stats::Formula m_avg_utilization;