PFPObject.h
1 /*
2  * PFPSim: Library for the Programmable Forwarding Plane Simulation Framework
3  *
4  * Copyright (C) 2016 Concordia Univ., Montreal
5  * Samar Abdi
6  * Umair Aftab
7  * Gordon Bailey
8  * Faras Dewal
9  * Shafigh Parsazad
10  * Eric Tremblay
11  *
12  * Copyright (C) 2016 Ericsson
13  * Bochra Boughzala
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  * 02110-1301, USA.
29  */
30 
31 #ifndef CORE_PFPOBJECT_H_
32 #define CORE_PFPOBJECT_H_
33 #include <string>
34 #include <vector>
35 #include <map>
36 #include "systemc.h" // NOLINT(build/include)
37 #include "tlm.h" // NOLINT(build/include)
38 #include "TrType.h"
39 #include "PFPConfig.h"
40 #include "LMTQueue.h"
41 #include "MTQueue.h"
42 #include "PFPObserver.h"
43 #include "./promptcolors.h"
44 #include "ConfigurationParameters.h"
45 
46 #define GetParam(key) get_param_value(configMap, key)
47 #define GetParamI(key) atoi(get_param_value(configMap, key).c_str())
48 
49 /*
50  * Pre-Processor for NPU Log Levels.
51  */
52 
53 // Format: CommonMacroName_No.ofargs
54 
55 // Concatenate macro name with number of inputs to base macro
56 #define CATMACRO(A, B) A ## B
57 // Select the name based on number of args.
58 #define SELECTMACRO(NAME, NUM) CATMACRO(NAME ## _, NUM)
59 // Get count for macros // Add here for additional number of arguments
60 #define GET_COUNT(_1, _2, COUNT, ...) COUNT
61 // Get args // Add here in reverse for additional number of args.
62 #define VA_SIZE(...) GET_COUNT(__VA_ARGS__, 2, 1)
63 #define VA_SELECT(NAME, ...) \
64  SELECTMACRO(NAME, VA_SIZE(__VA_ARGS__))(__VA_ARGS__) // selector
65 #define npulog(...) VA_SELECT(LOG, __VA_ARGS__) // Base Macro
66 
67 #define npu_error(error_msg) { \
68  cerr << "Error: " << (error_msg) << __FILE__ << ":" \
69  << __LINE__ << endl; sc_stop(); }
70 
71 #define LOG_1(STAT)\
72  if (pfp::core::PFPConfig::get().get_verbose_level() \
73  != pfp::core::PFPConfig::get().verbosity::minimal \
74  && pfp::core::PFPConfig::get().get_verbose_level() \
75  == pfp::core::PFPConfig::get().verbosity::debug) { \
76  sc_core::sc_time now = sc_time_stamp(); \
77  cout << txtgrn << std::fixed << now.to_double()/1000 << " ns " \
78  << txtrst << "@ " << txtrst << " "; \
79  STAT \
80  cout << txtrst; \
81 }
82 
83 #define LOG_2(LEVEL, STAT) \
84  if (pfp::core::PFPConfig::get().get_verbose_level() \
85  == pfp::core::PFPConfig::get().verbosity::LEVEL) { \
86  sc_core::sc_time now = sc_time_stamp(); \
87  std::string modnametemp = module_name_; \
88  cout << txtgrn << std::fixed << now.to_double()/1000 << " ns " \
89  << txtrst << "@ " << On_Purple << modnametemp << txtrst << " "; \
90  STAT \
91  cout << txtrst; \
92  }
93 
94 //-------------------------------------------------------------------
95 
96 namespace pfp {
97 namespace core {
98 inline std::string convert_to_string(sc_module_name nm) {
99  return static_cast<const char*>(nm);
100 }
101 
102 class PFPObject {
103  public:
104  PFPObject();
105  PFPObject(const std::string& module_name, PFPObject* parent = 0,
106  bool enable_dicp = false);
107  PFPObject(const std::string& module_name, std::string BaseConfigFile,
108  std::string InstanceConfigFile = "", PFPObject* parent = 0,
109  bool enable_dicp = false);
110  PFPObject(const PFPObject &other, bool enable_dicp = false);
111  virtual ~PFPObject() = default;
112 
113  /*--------Configuration of Modules -------------*/
114  void LoadBaseConfiguration(std::string);
115  void LoadInstanceConfiguration(std::string);
116 
117  pfp::core::ConfigurationParameterNode GetParameter(std::string param);
119  GetParameterfromParent(std::string param, PFPObject* parent);
120 
121  std::vector<std::string> ModuleHierarchy();
122  /* ----- DICP ----- */
123  const bool dicp_enabled;
124 
125  /* --- Module ---- */
126  const std::string& module_name() const;
127  PFPObject* GetParent();
128 
129  /* --- Observers --- */
136  virtual bool add_counter(const std::string& counter_name,
137  std::size_t counter_value = 0);
144  virtual bool set_counter(const std::string& counter_name,
145  std::size_t counter_value);
151  virtual bool remove_counter(const std::string& counter_name);
158  virtual std::size_t counter_value(const std::string& counter_name) const;
164  virtual bool increment_counter(const std::string& counter_name);
165  virtual bool increment_counter(const std::string& counter_name,
166  const int incr_amount);
172  virtual bool decrement_counter(const std::string& counter_name);
173  virtual bool decrement_counter(const std::string& counter_name,
174  const int decr_amount);
179  virtual void attach_observer(std::shared_ptr<PFPObserver> observer);
186  virtual void notify_counter_changed(const std::string& counter_name,
187  std::size_t counter_value, double sim_time);
193  virtual void notify_counter_added(const std::string& counter_name,
194  double sim_time);
200  virtual void notify_counter_removed(const std::string& counter_name,
201  double sim_time);
206  virtual std::size_t num_counters() const;
212  void AddChildModule(std::string module_name, PFPObject* module);
213 
214  // TODO(umair) why are all notify_data_* templated when PFPObserver only
215  // handles TrType anyway...
221  template <typename DATA_TYPE>
222  void notify_data_written(const std::shared_ptr<DATA_TYPE> data,
223  double sim_time) {
224  for (auto& each_observer : observers_) {
225  auto func = std::bind(&PFPObserver::data_written, each_observer,
226  module_name(), data, sim_time);
227  events_.push(func);
228  }
229  }
235  template <typename DATA_TYPE>
236  void notify_data_read(const std::shared_ptr<DATA_TYPE> data,
237  double sim_time) {
238  for (auto& each_observer : observers_) {
239  auto func = std::bind(&PFPObserver::data_read, each_observer,
240  module_name(), data, sim_time);
241  events_.push(func);
242  }
243  }
249  template <typename DATA_TYPE>
250  void notify_data_dropped(const std::shared_ptr<DATA_TYPE> data,
251  std::string& drop_reason, double sim_time) {
252  for (auto& each_observer : observers_) {
253  auto func = std::bind(&PFPObserver::data_dropped, each_observer,
254  module_name(), data, drop_reason, sim_time);
255  events_.push(func);
256  }
257  }
258 
259  template <typename DATA_TYPE>
260  void drop_data(const std::shared_ptr<DATA_TYPE> data,
261  std::string drop_reason) {
262  notify_data_dropped(data,
263  drop_reason,
264  sc_time_stamp().to_default_time_units());
265  }
266 
267  pfp::core::ConfigurationParameterNode SimulationParameters;
270  protected:
271  const std::string GlobalConfigPath;
272  const std::string module_name_;
274  std::map<std::string, std::string> configMap;
275  std::map<std::string, std::size_t> counters_;
278  std::vector<std::shared_ptr<PFPObserver>> observers_;
280  std::map<std::string, PFPObject*> childModules_;
281 };
282 
283 }; // namespace core
284 }; // namespace pfp
285 #endif // CORE_PFPOBJECT_H_
A multiple-producer, multiple-consumer, thread-safe queue implemented using SystemC primitives The MT...
Definition: MTQueue.h:49
virtual bool add_counter(const std::string &counter_name, std::size_t counter_value=0)
Add a counter to the PFPObject.
Definition: PFPObject.cpp:180
PFPObserver.h.
virtual bool remove_counter(const std::string &counter_name)
Remove a counter from the PFPObject.
Definition: PFPObject.cpp:208
void notify_data_dropped(const std::shared_ptr< DATA_TYPE > data, std::string &drop_reason, double sim_time)
Notify all observers of data drop.
Definition: PFPObject.h:250
virtual std::size_t counter_value(const std::string &counter_name) const
Get the value of a counter Note: This will throw a std::out_of_range exception if the counter does no...
Definition: PFPObject.cpp:224
void AddChildModule(std::string module_name, PFPObject *module)
Add a child module to the PFPObject.
Definition: PFPObject.cpp:322
PFPObject * parent_
Definition: PFPObject.h:273
virtual void data_written(const std::string &from_module, const std::shared_ptr< pfp::core::TrType > data, double simulation_time)=0
Function called by the NPU when data is written by a module.
Definition: ConfigurationParameters.h:48
std::vector< std::shared_ptr< PFPObserver > > observers_
List of observers attached to this PFPObject.
Definition: PFPObject.h:278
void notify_data_read(const std::shared_ptr< DATA_TYPE > data, double sim_time)
Notify all observers of data read.
Definition: PFPObject.h:236
Definition: PFPObject.h:102
virtual void notify_counter_removed(const std::string &counter_name, double sim_time)
Notify all attached observers when a counter is removed from the PFPObject.
Definition: PFPObject.cpp:309
virtual void data_read(const std::string &to_module, const std::shared_ptr< pfp::core::TrType > data, double simulation_time)=0
Function called by the NPU when data is read by a module.
virtual void data_dropped(const std::string &in_module, const std::shared_ptr< pfp::core::TrType > data, const std::string &drop_reason, double simulation_time)=0
Function called by the NPU when data is dropped in a module.
static MTQueue< std::function< void(void)> > events_
Definition: PFPObject.h:268
void push(const T &item)
Push an item onto the MTQueue.
Definition: MTQueue.h:95
virtual bool set_counter(const std::string &counter_name, std::size_t counter_value)
Set the value of a counter.
Definition: PFPObject.cpp:191
virtual std::size_t num_counters() const
Get the number of counters in the PFPObject.
Definition: PFPObject.cpp:318
std::map< std::string, PFPObject * > childModules_
Internal list of submodules.
Definition: PFPObject.h:280
virtual bool decrement_counter(const std::string &counter_name)
Decrement the specified counter.
Definition: PFPObject.cpp:253
void notify_data_written(const std::shared_ptr< DATA_TYPE > data, double sim_time)
Notify all observers of data written.
Definition: PFPObject.h:222
virtual void attach_observer(std::shared_ptr< PFPObserver > observer)
Attach an observer that will be notified when events occur.
Definition: PFPObject.cpp:286
std::map< std::string, std::string > configMap
Definition: PFPObject.h:274
std::map< std::string, std::size_t > counters_
Store counters and values.
Definition: PFPObject.h:276
virtual void notify_counter_added(const std::string &counter_name, double sim_time)
Notify all attached observers when a counter is added to the PFPObject.
Definition: PFPObject.cpp:302
virtual bool increment_counter(const std::string &counter_name)
Increment the specified counter.
Definition: PFPObject.cpp:228
virtual void notify_counter_changed(const std::string &counter_name, std::size_t counter_value, double sim_time)
Notify all attached observers when a counter is changed.
Definition: PFPObject.cpp:293
PacketBase.h.
Definition: ConfigurationParameters.cpp:36