The Sparta Modeling Framework
Loading...
Searching...
No Matches
Resource.hpp
Go to the documentation of this file.
1// <Resource.hpp> -*- C++ -*-
2
9#pragma once
10
11#include <string>
12#include <ostream>
13#include <type_traits>
14
21#include "sparta/utils/Utils.hpp"
22#include "simdb/Errors.hpp"
23
24namespace sparta {
25
26 class TreeNode;
27
32 {
33 public:
34 // Empty for now
35 };
36
37
44 {
46 friend class TreeNode;
47
48 public:
49
57
68 const std::string& name);
69
79 Resource(const std::string & name,
80 const Clock * clk);
81
83 virtual ~Resource();
84
88 const Clock * getClock() const;
89
93 Scheduler * getScheduler(const bool must_exist = true) const;
94
98 std::string getName() const;
99
105
110 const TreeNode* getContainer() const;
111
116
121
128 virtual void addLink(TreeNode *node, const std::string &label)
129 {
130 (void)node; (void)label;
131 /* Resource that do not overload this method does not use the links */
132 }
133
139 virtual void activateLink(const std::string &label)
140 {
141 (void)label;
142 /* Resource that do not overload this method does not use the links */
143 }
144
146 Resource(const Resource &) = delete;
147 Resource &operator=(const Resource &) = delete;
148
149 private:
150
159 virtual void simulationTerminating_();
160
175 virtual void validatePostRun_(const PostRunValidationInfo& info) const;
176
197 virtual void dumpDebugContent_(std::ostream& output) const;
198
211 virtual void onStartingTeardown_();
212
218 virtual void onBindTreeEarly_() {;}
219
225 virtual void onBindTreeLate_() {;}
226
230 TreeNode* res_container_;
231
235 std::string name_;
236
240 const Clock * clk_;
241 };
242
247 GENERATE_HAS_ATTR(getClock)
248
249
253 {
257 void appendTickData_(std::ostream& ss, Scheduler * sched)
258 {
259 if(sched) {
260 ss << "tick: " << sched->getCurrentTick();
261 }
262 else {
263 ss << "(no scheduler associated)";
264 }
265 }
266
270 void appendClockData_(std::ostream& ss, const Clock* clk)
271 {
272 Scheduler * sched = nullptr;
273 if(clk){
274 ss << "at cycle: " << clk->currentCycle() << " ";
275 sched = clk->getScheduler();
276 }else{
277 ss << "(no clock associated) ";
278 }
279 // also always include the tick data along with the clock.
280 appendTickData_(ss, sched);
281 }
282 public:
283
284
288 template <class CTXT>
289 std::string getContextDescription(CTXT* ctxt,
290 typename std::enable_if<!std::is_base_of<Resource, CTXT>::value
291 && !std::is_base_of<TreeNode, CTXT>::value
292 && !has_attr_getClock<CTXT>::value>::type* dummy = nullptr) {
293 (void) ctxt;
294 (void) dummy;
295 std::stringstream ss;
296 ss << "(from non-sparta context at ";
297 appendTickData_(ss, nullptr);
298 ss << ")";
299
300 return ss.str();
301 }
302
311 template <class CTXT>
312 std::string getContextDescription(CTXT* ctxt,
313 typename std::enable_if<!std::is_base_of<Resource, CTXT>::value
314 && !std::is_base_of<TreeNode, CTXT>::value
315 && has_attr_getClock<CTXT>::value>::type* dummy = 0) {
316 (void) dummy;
317 std::stringstream ss;
318 static_assert(std::is_pointer<decltype(ctxt->getClock())>::value,
319 "Type of this pointer is a class having a getClock method, but this "
320 "method does not return a pointer to a sparta::Clock");
321 static_assert(std::is_base_of<sparta::Clock,
322 typename std::remove_pointer<typename std::remove_cv<decltype(ctxt->getClock())>::type>::type>::value != false,
323 "Type of this pointer is a class having a getClock method, but this "
324 "method doe not return a sparta::Clock");
325 auto clock = ctxt->getClock();
326 // write the clock and tick to the stream.
327 appendClockData_(ss, clock);
328
329 return ss.str();
330 }
331
335 template <class CTXT>
337 typename std::enable_if<std::is_base_of<Resource, CTXT>::value>::type* dummy = 0) {
338 (void) dummy;
339 std::stringstream ss;
340 const ResourceContainer* rc = ctxt->getResourceContainer();
341 if(!rc){
342 ss << "(within uncontained resource)";
343 }else{
344 ss << "within resource at: " << rc->getLocation() << " ";
345 const sparta::Clock* clock = ctxt->getClock();
346 // write the clock and tick to the stream.
347 appendClockData_(ss, clock);
348
349 }
350 return ss.str();
351 }
352
356 template <class CTXT>
358 typename std::enable_if<std::is_base_of<TreeNode, CTXT>::value>::type* dummy = 0) {
359 (void) dummy;
360 std::stringstream ss;
361 if(!ctxt){
362 ss << "(within null treenode)"; // This should not happen, but could
363 }else{
364 ss << "within TreeNode: " << ctxt->getLocation() << " ";
365 const sparta::Clock* clock = ctxt->getClock();
366 // write the clock and tick to the stream.
367 appendClockData_(ss, clock);
368 }
369 return ss.str();
370 }
371 };
372
379 #define ADD_CONTEXT_INFORMATION(ex, thisptr) \
380 sparta::AssertContext ac; \
381 ex << " " << ac.getContextDescription<typename std::remove_pointer<decltype(thisptr)>::type>(thisptr);
382
402 #define sparta_assert_context(e, insertions) \
403 if(__builtin_expect(!(e), 0)) { sparta::SpartaException ex(std::string(#e) + ": " ); \
404 ex << insertions; \
405 ADD_FILE_INFORMATION(ex, __FILE__, __LINE__); \
406 ADD_CONTEXT_INFORMATION(ex, this); \
407 throw ex; }
408
409
410} // namespace sparta
File that defines the Clock class.
Object with a name which holds a Resource.
A simple time-based, event precedence based scheduler.
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Basic Node framework in sparta device tree composite pattern.
Creates a helper traits class for determining whehther a type has a member named getClock.
Definition Resource.hpp:253
std::string getContextDescription(const sparta::Resource *ctxt, typename std::enable_if< std::is_base_of< Resource, CTXT >::value >::type *dummy=0)
Handle case where pointer is a subclass of (or is a) sparta::Resource.
Definition Resource.hpp:336
std::string getContextDescription(const sparta::TreeNode *ctxt, typename std::enable_if< std::is_base_of< TreeNode, CTXT >::value >::type *dummy=0)
Handle case where pointer is a subclass of (or is a) sparta::TreeNode.
Definition Resource.hpp:357
std::string getContextDescription(CTXT *ctxt, typename std::enable_if<!std::is_base_of< Resource, CTXT >::value &&!std::is_base_of< TreeNode, CTXT >::value &&!has_attr_getClock< CTXT >::value >::type *dummy=nullptr)
Handle default case where pointer is not a sparta::Resource.
Definition Resource.hpp:289
std::string getContextDescription(CTXT *ctxt, typename std::enable_if<!std::is_base_of< Resource, CTXT >::value &&!std::is_base_of< TreeNode, CTXT >::value &&has_attr_getClock< CTXT >::value >::type *dummy=0)
Handle case where pointer is a not subclass of (and not a) sparta::Resource, but still has a getClock...
Definition Resource.hpp:312
A representation of simulated time.
Definition Clock.hpp:51
Cycle currentCycle() const
Get the current cycle (uses current tick from the Scheduler)
Definition Clock.hpp:176
Scheduler * getScheduler() const
Definition Clock.hpp:302
virtual std::string getLocation() const =0
Returns the location of this node in device tree which can be used to navigate the device tree in met...
Information describing the type of validation being done.
Definition Resource.hpp:32
PhasedObject which can hold 0 or 1 Resource pointers to an associatedresource. Contains logic for set...
The is the base class for all types of resources used by the SPARTA framework.
Definition Resource.hpp:44
Scheduler * getScheduler(const bool must_exist=true) const
virtual void addLink(TreeNode *node, const std::string &label)
Definition Resource.hpp:128
ResourceContainer * getResourceContainer()
Gets the ResourceContainer for this resource (if any)
const Clock * getClock() const
TreeNode * getContainer()
Gets the TreeNode (container) for this resource (if any)
std::string getName() const
virtual ~Resource()
Destroy!
Resource(const Resource &)=delete
Disallow copying.
Resource(const std::string &name, const Clock *clk)
Construct a Resource with the given name and clock having NO association with a resource container....
const ResourceContainer * getResourceContainer() const
Gets the ResourceContainer for this resource (if any)
Resource(TreeNode *rc, const std::string &name)
Construct resource with a specific name and resource container.
friend class TreeNode
Allow TreeNode access to onStartingTeardown_.
Definition Resource.hpp:46
Resource(TreeNode *rc)
Construct resource with a resource container.
virtual void activateLink(const std::string &label)
Definition Resource.hpp:139
const TreeNode * getContainer() const
Gets the TreeNode (container) for this resource (if any)
A class that lets you schedule events now and in the future.
Tick getCurrentTick() const noexcept
The current tick the Scheduler is working on or just finished.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
std::string getLocation() const override final
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
Macros for handling exponential backoff.