The Sparta Modeling Framework
Loading...
Searching...
No Matches
PipelineCollector.hpp
Go to the documentation of this file.
1// <PipelineCollector.hpp> -*- C++ -*-
2
9#pragma once
10
11#include <set>
12#include <map>
13
16#include "sparta/simulation/TreeNodePrivateAttorney.hpp"
22#include "sparta/events/GlobalOrderingPoint.hpp"
24
25namespace sparta::app{
26 class Simulation;
27 extern void onPipelineCollectionShutdown(Simulation* sim);
28}
29
30namespace sparta{
31namespace collection
32{
67 {
68 class CollectablesByClock
69 {
70 public:
71 CollectablesByClock(const Clock * clk,
72 SchedulingPhase collection_phase) :
73 ev_set_(nullptr)
74 {
75 switch(collection_phase)
76 {
77 case SchedulingPhase::Trigger:
79 (&ev_set_, sparta::notNull(clk)->getName() + "_auto_collection_event_trigger",
80 CREATE_SPARTA_HANDLER(CollectablesByClock, performCollection), 1));
81 break;
84 (&ev_set_, sparta::notNull(clk)->getName() + "_auto_collection_event_update",
85 CREATE_SPARTA_HANDLER(CollectablesByClock, performCollection), 1));
86 break;
89 (&ev_set_, sparta::notNull(clk)->getName() + "_auto_collection_event_portupdate",
90 CREATE_SPARTA_HANDLER(CollectablesByClock, performCollection), 1));
91 break;
94 (&ev_set_, sparta::notNull(clk)->getName() + "_auto_collection_event_flush",
95 CREATE_SPARTA_HANDLER(CollectablesByClock, performCollection), 1));
96 break;
99 (&ev_set_, sparta::notNull(clk)->getName() + "_auto_collection_event_collection",
100 CREATE_SPARTA_HANDLER(CollectablesByClock, performCollection), 1));
101 break;
104 (&ev_set_, sparta::notNull(clk)->getName() + "_auto_collection_event_tick",
105 CREATE_SPARTA_HANDLER(CollectablesByClock, performCollection), 1));
106 break;
109 (&ev_set_, sparta::notNull(clk)->getName() + "_auto_collection_event_posttick",
110 CREATE_SPARTA_HANDLER(CollectablesByClock, performCollection), 1));
111 break;
112 case SchedulingPhase::__last_scheduling_phase:
113 sparta_assert(!"Should not have gotten here");
114 break;
115 // NO DEFAULT! Allows for compiler errors if the enum
116 // class is updated.
117 }
118 ev_collect_->setScheduleableClock(clk);
119 ev_collect_->setScheduler(clk->getScheduler());
120 ev_collect_->setContinuing(false);
121 }
122
123 void enable(CollectableTreeNode * ctn) {
124 enabled_ctns_.insert(ctn);
125
126 const auto * clk = ctn->getClock();
127 const auto period = clk->getPeriod();
128 const auto now = clk->getScheduler()->getCurrentTick();
129 const auto rel_tick = period - (now % period);
130 ev_collect_->scheduleRelativeTick(rel_tick, clk->getScheduler());
131 }
132
133 void disable(CollectableTreeNode * ctn) {
134 enabled_ctns_.erase(ctn);
135 }
136
137 bool anyCollected() const {
138 return !enabled_ctns_.empty();
139 }
140
141 void performCollection() {
142 for(auto & ctn : enabled_ctns_) {
143 if(ctn->isCollected()) {
144 ctn->collect();
145 }
146 }
147 if(!enabled_ctns_.empty()) {
148 ev_collect_->schedule();
149 }
150 }
151
152 void print() {
153 for(auto ctn : enabled_ctns_) {
154 std::cout << '\t' << ctn->getName() << std::endl;
155 }
156 }
157 private:
158 EventSet ev_set_;
159
160 std::unique_ptr<sparta::Scheduleable> ev_collect_;
161 std::set<CollectableTreeNode*> enabled_ctns_;
162 };
163
164 // A map of the clock pointer and the structures that
165 // represent collectables on that clock.
166 std::map<const sparta::Clock *,
167 std::array<std::unique_ptr<CollectablesByClock>,
168 sparta::NUM_SCHEDULING_PHASES>> clock_ctn_map_;
169
170 // Registered collectables
171 std::set<CollectableTreeNode*> registered_collectables_;
172
173 // Simulation from the root node. This is used to ensure the SimDB
174 // pipeline is flushed on simulator exceptions, any newly-seen enum
175 // values get stringified into the DB, ensure newly-seen std::string
176 // IDs in the TinyStrings table get written to the DB, etc.
177 //
178 // Without proper teardown, the database blobs could be unparseable
179 // for the collected data that just occurred prior to the exception.
180 // Either that or there is just missing data that should be there.
181 app::Simulation* simulation_ = nullptr;
182
183 public:
184
212 PipelineCollector(const std::string& filepath, Scheduler::Tick heartbeat_interval,
213 const sparta::Clock* root_clk, const sparta::TreeNode* root,
214 Scheduler* scheduler=nullptr) :
215 Collector("PipelineCollector"),
216 scheduler_(scheduler != nullptr ? scheduler : root_clk->getScheduler())
217 {
218 // Sanity check - pipeline collection cannot occur without a scheduler
219 sparta_assert(scheduler_);
220
221 sparta_assert(root != nullptr, "Pipeline Collection will not be able to create location file because it was passed a nullptr root treenode.");
222 sparta_assert(root->isFinalized(), "Pipeline collection cannot be constructed until the sparta tree has been finalized.");
223 // Assert that we got valid pointers necessary for pipeline collection.
224 sparta_assert(root_clk != nullptr, "Cannot construct PipelineCollector because root clock is a nullptr");
225 sparta_assert(scheduler_->isFinalized() == false, "Pipeline Collection cannot be instantiated after scheduler finalization -- it creates events");
226
227 // Initialize the clock/collectable map and find the fastest clock
228 const sparta::Clock* fastest_clk = nullptr;
229 std::function<void (const sparta::Clock*)> addClks;
230 addClks = [&addClks, this, &fastest_clk] (const sparta::Clock* clk)
231 {
232 if(clk != nullptr){
233 auto & u_p = clock_ctn_map_[clk];
234 for(uint32_t i = 0; i < NUM_SCHEDULING_PHASES; ++i) {
235 u_p[i].reset(new CollectablesByClock(clk, static_cast<SchedulingPhase>(i)));
236 }
238 const sparta::Clock* child_clk = dynamic_cast<const sparta::Clock*>(child);
239 if(child_clk){
240 auto clk_period = child_clk->getPeriod();
241 // If this clock has a non-1 period (i.e. not the root clock)
242 // AND there is either
243 // (A) no fastest clock yet
244 // or
245 // (B) this clock is a higher frequency than the fastest clock.
246 // then choose this as the fastest
247 if(clk_period != 1 && (!fastest_clk || (clk_period < fastest_clk->getPeriod()))) {
248 fastest_clk = child_clk;
249 }
250 addClks(child_clk);
251 }
252 }
253 }
254 };
255 addClks(root_clk);
256 if(fastest_clk == nullptr){
257 fastest_clk = root_clk;
258 }
259
260 // A multiple to multiply against the fastest clock when no heartbeat was set.
263 static const uint32_t heartbeat_multiplier = 200;
264 // round heartbeat using a multiple of the fastest clock if one was not set.
265 if (heartbeat_interval == 0)
266 {
267 sparta_assert(fastest_clk->getPeriod() != 0);
268 heartbeat_interval = fastest_clk->getPeriod() * heartbeat_multiplier; //round up to the nearest multiple of 100
269 heartbeat_interval = heartbeat_interval + (100 - (heartbeat_interval % 100));
270 // pipeViewer requires that intervals be a multiple of 100.
271 sparta_assert(heartbeat_interval % 100 == 0)
272 }
273
274 // Initialize some values.
275 filepath_ = filepath;
276 root_clk_ = root_clk;
277 }
278
280 // XXX This is a little goofy looking. Should we make sparta_abort that takes conditional
281 // be sparta_abort_unless()?
282 sparta_abort(collection_active_ != true,
283 "The PipelineCollector was not torn down properly. Before "
284 "tearing down the simulation tree, you must call "
285 "destroy() on the collector");
286 }
287
294 void destroy()
295 {
296 if(collection_active_) {
297 for(auto & ctn : registered_collectables_) {
298 if(ctn->isCollected()) {
299 ctn->closeRecord(true); // set true for simulation termination
300 }
301 }
302 }
303 registered_collectables_.clear();
304 collection_active_ = false;
305 onPipelineCollectionShutdown(simulation_);
306 }
307
308 void reactivate(const std::string& filepath)
309 {
310 sparta_assert(!collection_active_,
311 "You can only reactivate the PipelineCollector after you destroy it");
312 filepath_ = filepath;
313 }
314
328 {
329 // Recursively collect the start node and children
330 std::function<void (sparta::TreeNode* starting_node)> recursiveCollect;
331 recursiveCollect = [&recursiveCollect, this] (sparta::TreeNode* starting_node)
332 {
333 // First turn on this node if it's actually a CollectableTreeNode
334 CollectableTreeNode* c_node = dynamic_cast<CollectableTreeNode*>(starting_node);
335 if(c_node != nullptr) {
336 c_node->startCollecting(this);
337 if(!c_node->isIterableCollectorBin()) {
338 registered_collectables_.insert(c_node);
339 }
340 }
341
342 // Recursive step. Go through the children and turn them on as well.
344 {
345 recursiveCollect(node);
346 }
347 };
348
349 recursiveCollect(starting_node);
350 }
351
358 void stopCollection(sparta::TreeNode* starting_node)
359 {
360 std::function<void (sparta::TreeNode* starting_node)> recursiveStopCollect;
361 recursiveStopCollect = [&recursiveStopCollect, this] (sparta::TreeNode* starting_node) {
362 // First turn off this node if it's actually a CollectableTreeNode
363 CollectableTreeNode* c_node = dynamic_cast<CollectableTreeNode*>(starting_node);
364 if(c_node != nullptr)
365 {
366 c_node->stopCollecting(this);
367 registered_collectables_.erase(c_node);
368 }
369
370 // Recursive step. Go through the children and turn them on as well.
372 {
373 recursiveStopCollect(node);
374 }
375 };
376 recursiveStopCollect(starting_node);
377
378 collection_active_ = !registered_collectables_.empty();
379 }
380
387 for(auto & col : registered_collectables_) {
388 col->stopCollecting(this);
389 }
390 registered_collectables_.clear();
391 onPipelineCollectionShutdown(simulation_);
392 }
393
407 SchedulingPhase collection_phase = SchedulingPhase::Tick)
408 {
409 auto ccm_pair = clock_ctn_map_.find(ctn->getClock());
410 sparta_assert(ccm_pair != clock_ctn_map_.end());
411 ccm_pair->second[static_cast<uint32_t>(collection_phase)]->enable(ctn);
412 }
413
426 {
427 auto ccm_pair = clock_ctn_map_.find(ctn->getClock());
428 sparta_assert(ccm_pair != clock_ctn_map_.end());
429 for(auto & u_p : ccm_pair->second) {
430 u_p->disable(ctn);
431 }
432 }
433
444 bool isCollectionActive() const {
445 return collection_active_;
446 }
447
449 const std::string & getFilePath() const {
450 return filepath_;
451 }
452
455 return scheduler_;
456 }
457
458 private:
459
462 const sparta::Clock * root_clk_ = nullptr;
463
465 std::string filepath_;
466
468 Scheduler * scheduler_;
469
471 bool collection_active_ = false;
472 };
473
474}// namespace collection
475}// namespace sparta
File that defines the Clock class.
define a CollectableTreeNode type TreeNode.
Define a base Collector class.
File that defines the EventSet class.
A simple time-based, event precedence based scheduler.
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_abort(...)
Simple variatic assertion that will print a message to std::cerr and call std::terminate()
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
#define CREATE_SPARTA_HANDLER(clname, meth)
Basic Node framework in sparta device tree composite pattern.
File that defines the UniqueEvent class.
A representation of simulated time.
Definition Clock.hpp:44
Period getPeriod() const
Returns the period of this clock in scheduler ticks.
Definition Clock.hpp:149
Scheduler * getScheduler() const
Definition Clock.hpp:289
Set of Events that a unit (or sparta::TreeNode, sparta::Resource) contains and are visible through a ...
Definition EventSet.hpp:26
virtual bool isFinalized() const
Is this node (and thus the entire tree above it) "finalized".
A class that lets you schedule events now and in the future.
uint64_t Tick
Typedef for our unit of time.
Tick getCurrentTick() const noexcept
The current tick the Scheduler is working on or just finished.
bool isFinalized() const noexcept override
Is the scheduler finalized.
static const TreeNode::ChildrenVector & getAllChildren(const TreeNode &node)
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:204
const std::string & getName() const override
Gets the name of this node.
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
A type of Event that uniquely schedules itself on the schedule within a single time quantum....
Simulator which builds a sparta DeviceTree.
An abstract type of TreeNode that has virtual calls to start collection on this node,...
void startCollecting(Collector *collector)
Method that tells this treenode that is now running collection.
bool isCollected() const
Determine whether or not this node has collection turned on or off.
void stopCollecting(Collector *collector)
Method that tells this treenode that is now not running collection.
bool isIterableCollectorBin() const
Check if this CollectableTreeNode is a Collectable inside an IterableCollector.
A non-templated base class that all Collectors should inherit from.
Definition Collector.hpp:23
A class that facilitates all universal pipeline collection operations such as outputting finalized re...
void startCollection(sparta::TreeNode *starting_node)
Turn on collection for everything below a TreeNode. Recursively transverse the tree and turn on child...
PipelineCollector(const std::string &filepath, Scheduler::Tick heartbeat_interval, const sparta::Clock *root_clk, const sparta::TreeNode *root, Scheduler *scheduler=nullptr)
Instantiate the collector with required parameters before pipeline collection can occur.
void removeFromAutoCollection(CollectableTreeNode *ctn)
Remove the given CollectableTreeNode from collection.
bool isCollectionActive() const
Return true if the collector is actively collecting.
void stopCollection()
Stop pipeline collection on only those CollectableTreeNodes that this PipelineCollector was started w...
const std::string & getFilePath() const
void destroy()
Teardown the pipeline collector.
void addToAutoCollection(CollectableTreeNode *ctn, SchedulingPhase collection_phase=SchedulingPhase::Tick)
Add the CollectableTreeNode to auto collection.
void stopCollection(sparta::TreeNode *starting_node)
Stop pipeline collection on only those CollectableTreeNodes given.
Sparta Application framework.
Macros for handling exponential backoff.
T * notNull(T *p)
Ensures that a pointer is not null.
Definition Utils.hpp:235
const uint32_t NUM_SCHEDULING_PHASES
The number of phases.
SchedulingPhase
The SchedulingPhases used for events (Tick, Update, PortUpdate, etc)
@ Update
Resources are updated in this phase.
@ PortUpdate
N-cycle Ports are updated in this phase.
@ Tick
Most operations (combinational logic) occurs in this phase.
@ Flush
Phase where flushing of pipelines, etc can occur.
@ PostTick
Operations such as post-tick pipeline collection occur here.
@ Collection
Pipeline collection occurs here.